Skip to content

Commit

Permalink
Check that in_atoms is backed up by actual funds (#258)
Browse files Browse the repository at this point in the history
* Check that in_atoms is backed up by actual funds

* fmt

* Swap tests

* fmt

* Comment

* comment'
  • Loading branch information
brittcyr authored Nov 7, 2024
1 parent 5b35c7d commit 86223f3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
26 changes: 26 additions & 0 deletions programs/manifest/src/program/processor/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,32 @@ pub(crate) fn process_swap(

trace!("swap in_atoms:{in_atoms} out_atoms:{out_atoms} is_base_in:{is_base_in} is_exact_in:{is_exact_in}");

// This check is redundant with the check that will be done within token
// program on deposit, but it is done here to future proof in case we later
// remove checked math.
// This actually adds a new restriction that the wallet can fully fund the
// swap instead of a combination of wallet and existing withdrawable
// balance.
if is_exact_in {
if is_base_in {
require!(
in_atoms <= trader_base_account.get_balance_atoms(),
ManifestError::Overflow,
"Insufficient base in atoms for swap has: {} requires: {}",
trader_base_account.get_balance_atoms(),
in_atoms,
)?;
} else {
require!(
in_atoms <= trader_quote_account.get_balance_atoms(),
ManifestError::Overflow,
"Insufficient quote in atoms for swap has: {} requires: {}",
trader_quote_account.get_balance_atoms(),
in_atoms,
)?;
}
}

// this is a virtual credit to ensure matching always proceeds
// net token transfers will be handled later
dynamic_account.deposit(trader_index, in_atoms, is_base_in)?;
Expand Down
8 changes: 8 additions & 0 deletions programs/manifest/tests/cases/loaders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,14 @@ async fn swap_wrong_token_accounts() -> anyhow::Result<()> {
test_fixture
.deposit(Token::USDC, USDC_UNIT_SIZE * 1_000)
.await?;
test_fixture
.sol_mint_fixture
.mint_to(&test_fixture.payer_sol_fixture.key, 1 * SOL_UNIT_SIZE)
.await;
test_fixture
.usdc_mint_fixture
.mint_to(&test_fixture.payer_usdc_fixture.key, 1 * USDC_UNIT_SIZE)
.await;

test_fixture
.place_order(
Expand Down
5 changes: 5 additions & 0 deletions programs/manifest/tests/cases/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ use crate::{
async fn swap_test() -> anyhow::Result<()> {
let mut test_fixture: TestFixture = TestFixture::new().await;

test_fixture
.sol_mint_fixture
.mint_to(&test_fixture.payer_sol_fixture.key, 1 * SOL_UNIT_SIZE)
.await;

// No deposits or seat claims needed
test_fixture.swap(SOL_UNIT_SIZE, 0, true, true).await?;

Expand Down

0 comments on commit 86223f3

Please sign in to comment.