Skip to content

Commit

Permalink
test: add fees overflow test (#329)
Browse files Browse the repository at this point in the history
* add fee overflow test

* clippy

* rename arg
  • Loading branch information
guibescos authored Jan 15, 2025
1 parent c08fb3e commit d0f182d
Show file tree
Hide file tree
Showing 10 changed files with 201 additions and 43 deletions.
19 changes: 8 additions & 11 deletions contracts/svm/testing/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use {
solana_sdk::{
instruction::{
Instruction,
InstructionError::Custom,
InstructionError,
},
native_token::LAMPORTS_PER_SOL,
pubkey::Pubkey,
Expand All @@ -14,12 +14,10 @@ use {
Transaction,
TransactionError::{
self,
InstructionError,
},
},
},
};

pub const TX_FEE: u64 = 10_000; // TODO: make this programmatic? FeeStructure is currently private field within LiteSVM

#[allow(clippy::result_large_err)]
Expand Down Expand Up @@ -56,16 +54,15 @@ pub fn warp_to_unix(svm: &mut litesvm::LiteSVM, unix_timestamp: i64) {
svm.set_sysvar(&clock);
}

pub fn assert_custom_error(error: TransactionError, instruction_index: u8, custom_error: u32) {
pub fn assert_custom_error(
error: TransactionError,
instruction_index: u8,
instruction_error: InstructionError,
) {
match error {
InstructionError(index, error_variant) => {
TransactionError::InstructionError(index, error_variant) => {
assert_eq!(index, instruction_index);
match error_variant {
Custom(code) => {
assert_eq!(code, custom_error);
}
_ => panic!("Unexpected error code"),
}
assert_eq!(error_variant, instruction_error);
}
_ => panic!("Unexpected error variant"),
}
Expand Down
17 changes: 14 additions & 3 deletions contracts/svm/testing/tests/initialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use {
error::ErrorCode,
state::FEE_SPLIT_PRECISION,
},
solana_sdk::signer::Signer,
solana_sdk::{
instruction::InstructionError,
signer::Signer,
},
testing::{
express_relay::helpers::get_express_relay_metadata,
helpers::assert_custom_error,
Expand Down Expand Up @@ -55,7 +58,11 @@ fn test_initialize_fail_high_split_router() {

match setup_result {
Ok(_) => panic!("expected setup to fail"),
Err(err) => assert_custom_error(err, 0, ErrorCode::FeeSplitLargerThanPrecision.into()),
Err(err) => assert_custom_error(
err,
0,
InstructionError::Custom(ErrorCode::FeeSplitLargerThanPrecision.into()),
),
}
}

Expand All @@ -72,6 +79,10 @@ fn test_initialize_fail_high_split_relayer() {

match setup_result {
Ok(_) => panic!("expected setup to fail"),
Err(err) => assert_custom_error(err, 0, ErrorCode::FeeSplitLargerThanPrecision.into()),
Err(err) => assert_custom_error(
err,
0,
InstructionError::Custom(ErrorCode::FeeSplitLargerThanPrecision.into()),
),
}
}
7 changes: 6 additions & 1 deletion contracts/svm/testing/tests/set_admin.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use {
anchor_lang::error::ErrorCode as AnchorErrorCode,
solana_sdk::{
instruction::InstructionError,
signature::Keypair,
signer::Signer,
},
Expand Down Expand Up @@ -47,5 +48,9 @@ fn test_set_admin_fail_wrong_admin() {
let tx_result = submit_transaction(&mut svm, &[set_admin_ix], &wrong_admin, &[&wrong_admin])
.expect_err("Transaction should have failed");

assert_custom_error(tx_result.err, 0, AnchorErrorCode::ConstraintHasOne.into());
assert_custom_error(
tx_result.err,
0,
InstructionError::Custom(AnchorErrorCode::ConstraintHasOne.into()),
);
}
9 changes: 7 additions & 2 deletions contracts/svm/testing/tests/set_protocol_split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use {
state::FEE_SPLIT_PRECISION,
},
solana_sdk::{
instruction::InstructionError,
signature::Keypair,
signer::Signer,
},
Expand Down Expand Up @@ -59,7 +60,11 @@ fn test_set_router_split_fail_wrong_admin() {
)
.expect_err("Transaction should have failed");

assert_custom_error(tx_result.err, 0, AnchorErrorCode::ConstraintHasOne.into());
assert_custom_error(
tx_result.err,
0,
InstructionError::Custom(AnchorErrorCode::ConstraintHasOne.into()),
);
}

#[test]
Expand All @@ -78,6 +83,6 @@ fn test_set_router_split_fail_high_split() {
assert_custom_error(
tx_result.err,
0,
ErrorCode::FeeSplitLargerThanPrecision.into(),
InstructionError::Custom(ErrorCode::FeeSplitLargerThanPrecision.into()),
);
}
7 changes: 6 additions & 1 deletion contracts/svm/testing/tests/set_relayer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use {
anchor_lang::error::ErrorCode as AnchorErrorCode,
solana_sdk::{
instruction::InstructionError,
signature::Keypair,
signer::Signer,
},
Expand Down Expand Up @@ -55,5 +56,9 @@ fn test_set_relayer_fail_wrong_admin() {
let tx_result = submit_transaction(&mut svm, &[set_relayer_ix], &wrong_admin, &[&wrong_admin])
.expect_err("Transaction should have failed");

assert_custom_error(tx_result.err, 0, AnchorErrorCode::ConstraintHasOne.into());
assert_custom_error(
tx_result.err,
0,
InstructionError::Custom(AnchorErrorCode::ConstraintHasOne.into()),
);
}
11 changes: 8 additions & 3 deletions contracts/svm/testing/tests/set_splits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use {
error::ErrorCode,
state::FEE_SPLIT_PRECISION,
},
solana_sdk::instruction::InstructionError,
testing::{
express_relay::{
helpers::get_express_relay_metadata,
Expand Down Expand Up @@ -54,7 +55,11 @@ fn test_set_splits_fail_wrong_admin() {
let tx_result = submit_transaction(&mut svm, &[set_splits_ix], &wrong_admin, &[&wrong_admin])
.expect_err("Transaction should have failed");

assert_custom_error(tx_result.err, 0, AnchorErrorCode::ConstraintHasOne.into());
assert_custom_error(
tx_result.err,
0,
InstructionError::Custom(AnchorErrorCode::ConstraintHasOne.into()),
);
}

#[test]
Expand All @@ -73,7 +78,7 @@ fn test_set_splits_fail_high_split_router() {
assert_custom_error(
tx_result.err,
0,
ErrorCode::FeeSplitLargerThanPrecision.into(),
InstructionError::Custom(ErrorCode::FeeSplitLargerThanPrecision.into()),
);
}

Expand All @@ -93,6 +98,6 @@ fn test_set_splits_fail_high_split_relayer() {
assert_custom_error(
tx_result.err,
0,
ErrorCode::FeeSplitLargerThanPrecision.into(),
InstructionError::Custom(ErrorCode::FeeSplitLargerThanPrecision.into()),
);
}
9 changes: 7 additions & 2 deletions contracts/svm/testing/tests/set_swap_platform_fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use {
state::FEE_SPLIT_PRECISION,
},
litesvm::LiteSVM,
solana_sdk::instruction::InstructionError,
testing::{
express_relay::{
helpers::get_express_relay_metadata,
Expand Down Expand Up @@ -63,7 +64,11 @@ fn test_set_swap_platform_fee_wrong_admin() {
)
.expect_err("Transaction should have failed");

assert_custom_error(tx_result.err, 0, AnchorErrorCode::ConstraintHasOne.into());
assert_custom_error(
tx_result.err,
0,
InstructionError::Custom(AnchorErrorCode::ConstraintHasOne.into()),
);
}

#[test]
Expand All @@ -78,6 +83,6 @@ fn test_set_swap_platform_fee_fail_high_split_router() {
assert_custom_error(
tx_result.err,
0,
ErrorCode::FeeSplitLargerThanPrecision.into(),
InstructionError::Custom(ErrorCode::FeeSplitLargerThanPrecision.into()),
);
}
61 changes: 50 additions & 11 deletions contracts/svm/testing/tests/submit_bid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use {
state::FEE_SPLIT_PRECISION,
},
solana_sdk::{
instruction::Instruction,
instruction::{
Instruction,
InstructionError,
},
native_token::LAMPORTS_PER_SOL,
pubkey::Pubkey,
rent::Rent,
Expand Down Expand Up @@ -175,7 +178,11 @@ fn test_bid_fail_wrong_relayer_signer() {
)
.expect_err("Transaction should have failed");

assert_custom_error(tx_result.err, 0, AnchorErrorCode::ConstraintHasOne.into());
assert_custom_error(
tx_result.err,
0,
InstructionError::Custom(AnchorErrorCode::ConstraintHasOne.into()),
);
}

#[test]
Expand Down Expand Up @@ -209,7 +216,11 @@ fn test_bid_fail_wrong_relayer_fee_receiver() {
submit_transaction(&mut svm, &bid_ixs, &searcher, &[&searcher, &relayer_signer])
.expect_err("Transaction should have failed");

assert_custom_error(tx_result.err, 0, AnchorErrorCode::ConstraintHasOne.into());
assert_custom_error(
tx_result.err,
0,
InstructionError::Custom(AnchorErrorCode::ConstraintHasOne.into()),
);
}

#[test]
Expand Down Expand Up @@ -247,7 +258,7 @@ fn test_bid_fail_insufficient_searcher_rent() {
assert_custom_error(
tx_result.err,
0,
ErrorCode::InsufficientSearcherFunds.into(),
InstructionError::Custom(ErrorCode::InsufficientSearcherFunds.into()),
);
}

Expand Down Expand Up @@ -282,7 +293,11 @@ fn test_bid_fail_insufficient_router_rent() {
submit_transaction(&mut svm, &bid_ixs, &searcher, &[&searcher, &relayer_signer])
.expect_err("Transaction should have failed");

assert_custom_error(tx_result.err, 0, ErrorCode::InsufficientRent.into());
assert_custom_error(
tx_result.err,
0,
InstructionError::Custom(ErrorCode::InsufficientRent.into()),
);
}

#[test]
Expand Down Expand Up @@ -329,7 +344,11 @@ fn test_bid_fail_insufficient_relayer_fee_receiver_rent() {
submit_transaction(&mut svm, &bid_ixs, &searcher, &[&searcher, &relayer_signer])
.expect_err("Transaction should have failed");

assert_custom_error(tx_result.err, 0, ErrorCode::InsufficientRent.into());
assert_custom_error(
tx_result.err,
0,
InstructionError::Custom(ErrorCode::InsufficientRent.into()),
);
}

#[test]
Expand Down Expand Up @@ -363,7 +382,11 @@ fn test_bid_fail_passed_deadline() {
submit_transaction(&mut svm, &bid_ixs, &searcher, &[&searcher, &relayer_signer])
.expect_err("Transaction should have failed");

assert_custom_error(tx_result.err, 0, ErrorCode::DeadlinePassed.into());
assert_custom_error(
tx_result.err,
0,
InstructionError::Custom(ErrorCode::DeadlinePassed.into()),
);
}

#[test]
Expand Down Expand Up @@ -397,7 +420,11 @@ fn test_bid_fail_wrong_permission_key() {
submit_transaction(&mut svm, &bid_ixs, &searcher, &[&searcher, &relayer_signer])
.expect_err("Transaction should have failed");

assert_custom_error(tx_result.err, 1, ErrorCode::MissingPermission.into());
assert_custom_error(
tx_result.err,
1,
InstructionError::Custom(ErrorCode::MissingPermission.into()),
);
}

#[test]
Expand Down Expand Up @@ -431,7 +458,11 @@ fn test_bid_fail_wrong_router_key() {
submit_transaction(&mut svm, &bid_ixs, &searcher, &[&searcher, &relayer_signer])
.expect_err("Transaction should have failed");

assert_custom_error(tx_result.err, 1, ErrorCode::MissingPermission.into());
assert_custom_error(
tx_result.err,
1,
InstructionError::Custom(ErrorCode::MissingPermission.into()),
);
}

#[test]
Expand All @@ -451,7 +482,11 @@ fn test_bid_fail_no_permission() {
let tx_result = submit_transaction(&mut svm, &ixs, &searcher, &[&searcher])
.expect_err("Transaction should have failed");

assert_custom_error(tx_result.err, 0, ErrorCode::MissingPermission.into());
assert_custom_error(
tx_result.err,
0,
InstructionError::Custom(ErrorCode::MissingPermission.into()),
);
}

#[test]
Expand Down Expand Up @@ -494,5 +529,9 @@ fn test_bid_fail_duplicate_permission() {
submit_transaction(&mut svm, &bid_ixs, &searcher, &[&searcher, &relayer_signer])
.expect_err("Transaction should have failed");

assert_custom_error(tx_result.err, 0, ErrorCode::MultiplePermissions.into());
assert_custom_error(
tx_result.err,
0,
InstructionError::Custom(ErrorCode::MultiplePermissions.into()),
);
}
Loading

0 comments on commit d0f182d

Please sign in to comment.