Skip to content

Commit

Permalink
check err number
Browse files Browse the repository at this point in the history
  • Loading branch information
skrrb committed Mar 1, 2024
1 parent 77d1008 commit fce0d60
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 67 deletions.
146 changes: 79 additions & 67 deletions programs/openbook-v2/tests/cases/test_create_market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,27 @@ async fn test_with_single_oracle() -> Result<(), TransportError> {
.await
.is_ok());

assert!(send_tx(
solana,
CreateMarketInstruction {
payer,
market: market_b,
quote_lot_size: 100,
base_lot_size: 100,
base_mint: mints[0].pubkey,
quote_mint: mints[1].pubkey,
..CreateMarketInstruction::with_new_book_and_heap(solana, None, Some(tokens[1].oracle))
assert_eq!(
send_tx_and_get_ix_custom_error(
solana,
CreateMarketInstruction {
payer,
market: market_b,
quote_lot_size: 100,
base_lot_size: 100,
base_mint: mints[0].pubkey,
quote_mint: mints[1].pubkey,
..CreateMarketInstruction::with_new_book_and_heap(
solana,
None,
Some(tokens[1].oracle)
)
.await
},
)
.await
.is_err());
},
)
.await,
Some(openbook_v2::error::OpenBookError::InvalidSecondOracle.into())
);

Ok(())
}
Expand All @@ -58,25 +64,27 @@ async fn test_with_same_oracles() -> Result<(), TransportError> {
let market = TestKeypair::new();
let fake_oracle_a = solana.create_account_from_len(&payer.pubkey(), 100).await;

assert!(send_tx(
solana,
CreateMarketInstruction {
payer,
market,
quote_lot_size: 100,
base_lot_size: 100,
base_mint: mints[0].pubkey,
quote_mint: mints[1].pubkey,
..CreateMarketInstruction::with_new_book_and_heap(
solana,
Some(fake_oracle_a),
Some(fake_oracle_a),
)
.await
},
)
.await
.is_err());
assert_eq!(
send_tx_and_get_ix_custom_error(
solana,
CreateMarketInstruction {
payer,
market,
quote_lot_size: 100,
base_lot_size: 100,
base_mint: mints[0].pubkey,
quote_mint: mints[1].pubkey,
..CreateMarketInstruction::with_new_book_and_heap(
solana,
Some(fake_oracle_a),
Some(fake_oracle_a),
)
.await
},
)
.await,
Some(anchor_lang::error::ErrorCode::RequireKeysNeqViolated.into())
);

Ok(())
}
Expand All @@ -95,41 +103,45 @@ async fn test_with_wrong_oracle_types() -> Result<(), TransportError> {
let fake_oracle_a = solana.create_account_from_len(&payer.pubkey(), 100).await;
let fake_oracle_b = solana.create_account_from_len(&payer.pubkey(), 100).await;

assert!(send_tx(
solana,
CreateMarketInstruction {
payer,
market: market_a,
quote_lot_size: 100,
base_lot_size: 100,
base_mint: mints[0].pubkey,
quote_mint: mints[1].pubkey,
..CreateMarketInstruction::with_new_book_and_heap(solana, Some(fake_oracle_a), None,)
assert_eq!(
send_tx_and_get_ix_custom_error(
solana,
CreateMarketInstruction {
payer,
market: market_a,
quote_lot_size: 100,
base_lot_size: 100,
base_mint: mints[0].pubkey,
quote_mint: mints[1].pubkey,
..CreateMarketInstruction::with_new_book_and_heap(solana, Some(fake_oracle_a), None)
.await
},
)
.await,
Some(openbook_v2::error::OpenBookError::UnknownOracleType.into())
);

assert_eq!(
send_tx_and_get_ix_custom_error(
solana,
CreateMarketInstruction {
payer,
market: market_ab,
quote_lot_size: 100,
base_lot_size: 100,
base_mint: mints[0].pubkey,
quote_mint: mints[1].pubkey,
..CreateMarketInstruction::with_new_book_and_heap(
solana,
Some(fake_oracle_a),
Some(fake_oracle_b)
)
.await
},
)
.await
.is_err());

assert!(send_tx(
solana,
CreateMarketInstruction {
payer,
market: market_ab,
quote_lot_size: 100,
base_lot_size: 100,
base_mint: mints[0].pubkey,
quote_mint: mints[1].pubkey,
..CreateMarketInstruction::with_new_book_and_heap(
solana,
Some(fake_oracle_a),
Some(fake_oracle_b)
)
.await
},
)
.await
.is_err());
},
)
.await,
Some(openbook_v2::error::OpenBookError::UnknownOracleType.into())
);

Ok(())
}
19 changes: 19 additions & 0 deletions programs/openbook-v2/tests/program_test/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,25 @@ pub async fn send_tx<CI: ClientInstruction>(
Ok(accounts)
}

pub async fn send_tx_and_get_ix_custom_error<CI: ClientInstruction>(
solana: &SolanaCookie,
ix: CI,
) -> Option<u32> {
let tx_result = send_tx(solana, ix).await;

if let Err(TransportError::TransactionError(
solana_sdk::transaction::TransactionError::InstructionError(
_,
solana_sdk::instruction::InstructionError::Custom(err_num),
),
)) = tx_result
{
Some(err_num)
} else {
None
}
}

/// Build a transaction from multiple instructions
pub struct ClientTransaction {
solana: Arc<SolanaCookie>,
Expand Down

0 comments on commit fce0d60

Please sign in to comment.