Skip to content

Commit 25285e8

Browse files
committed
Test you can't transfer more than balance
1 parent 2edf12c commit 25285e8

2 files changed

Lines changed: 89 additions & 14 deletions

File tree

solana/programs/example-native-token-transfers/tests/sdk/instructions/admin.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use anchor_lang::{prelude::Pubkey, system_program::System, Id, InstructionData, ToAccountMetas};
2-
use example_native_token_transfers::instructions::SetPeerArgs;
2+
use example_native_token_transfers::instructions::{SetOutboundLimitArgs, SetPeerArgs};
33
use solana_sdk::instruction::Instruction;
44

55
use crate::sdk::accounts::NTT;
@@ -23,7 +23,7 @@ pub fn set_peer(ntt: &NTT, accounts: SetPeer, args: SetPeerArgs) -> Instruction
2323
};
2424

2525
Instruction {
26-
program_id: example_native_token_transfers::ID,
26+
program_id: ntt.program(),
2727
accounts: accounts.to_account_metas(None),
2828
data: data.data(),
2929
}
@@ -42,7 +42,7 @@ pub fn set_paused(ntt: &NTT, accounts: SetPaused, pause: bool) -> Instruction {
4242
};
4343

4444
Instruction {
45-
program_id: example_native_token_transfers::ID,
45+
program_id: ntt.program(),
4646
accounts: accounts.to_account_metas(None),
4747
data: data.data(),
4848
}
@@ -107,7 +107,31 @@ pub fn set_threshold(ntt: &NTT, accounts: SetThreshold, threshold: u8) -> Instru
107107
};
108108

109109
Instruction {
110-
program_id: example_native_token_transfers::ID,
110+
program_id: ntt.program(),
111+
accounts: accounts.to_account_metas(None),
112+
data: data.data(),
113+
}
114+
}
115+
116+
pub struct SetOutboundLimit {
117+
pub owner: Pubkey,
118+
}
119+
120+
pub fn set_outbound_limit(
121+
ntt: &NTT,
122+
accounts: SetOutboundLimit,
123+
args: SetOutboundLimitArgs,
124+
) -> Instruction {
125+
let data = example_native_token_transfers::instruction::SetOutboundLimit { args };
126+
127+
let accounts = example_native_token_transfers::accounts::SetOutboundLimit {
128+
config: ntt.config(),
129+
owner: accounts.owner,
130+
rate_limit: ntt.outbox_rate_limit(),
131+
};
132+
133+
Instruction {
134+
program_id: ntt.program(),
111135
accounts: accounts.to_account_metas(None),
112136
data: data.data(),
113137
}

solana/programs/example-native-token-transfers/tests/transfer.rs

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use common::setup::{TestData, OTHER_CHAIN};
77
use example_native_token_transfers::{
88
bitmap::Bitmap,
99
error::NTTError,
10-
instructions::TransferArgs,
10+
instructions::{SetOutboundLimitArgs, TransferArgs},
1111
queue::outbox::{OutboxItem, OutboxRateLimit},
1212
transceivers::wormhole::ReleaseOutboundArgs,
1313
transfer::Payload,
@@ -28,7 +28,7 @@ use wormhole_anchor_sdk::wormhole::PostedVaa;
2828
use crate::{
2929
common::{
3030
query::GetAccountDataAnchor,
31-
setup::{ANOTHER_CHAIN, OUTBOUND_LIMIT},
31+
setup::{ANOTHER_CHAIN, OUTBOUND_LIMIT, UNREGISTERED_CHAIN},
3232
},
3333
sdk::{
3434
accounts::{good_ntt, NTTAccounts},
@@ -39,7 +39,7 @@ use crate::{
3939
common::{setup::OTHER_MANAGER, submit::Submittable},
4040
sdk::{
4141
instructions::{
42-
admin::{set_paused, SetPaused},
42+
admin::{set_outbound_limit, set_paused, SetOutboundLimit, SetPaused},
4343
transfer::{
4444
approve_token_authority, approve_token_authority_with_token_program_id, transfer,
4545
transfer_with_token_program_id,
@@ -56,13 +56,6 @@ pub mod sdk;
5656

5757
use crate::common::setup::{setup, setup_with_transfer_fee};
5858

59-
// TODO: some more tests
60-
// - unregistered peer can't transfer
61-
// - can't transfer to unregistered peer
62-
// - can't transfer more than balance
63-
// - wrong inbox accounts
64-
// - paused contracts
65-
6659
/// Helper function for setting up transfer accounts and args.
6760
/// It sets the accounts up properly, so for negative testing we just modify the
6861
/// result.
@@ -662,6 +655,64 @@ async fn test_transfer_wrong_mode() {
662655
);
663656
}
664657

658+
#[tokio::test]
659+
async fn test_cant_transfer_more_than_balance() {
660+
let (mut ctx, test_data) = setup(Mode::Locking).await;
661+
662+
let outbox_item = Keypair::new();
663+
664+
let token_account: TokenAccount = ctx
665+
.get_account_data_anchor(test_data.user_token_account)
666+
.await;
667+
let more_than_balance = token_account.amount.checked_add(100).unwrap();
668+
669+
// extend outbound limit to avoid TransferExceedsRateLimit error
670+
set_outbound_limit(
671+
&good_ntt,
672+
SetOutboundLimit {
673+
owner: test_data.program_owner.pubkey(),
674+
},
675+
SetOutboundLimitArgs {
676+
limit: more_than_balance,
677+
},
678+
)
679+
.submit_with_signers(&[&test_data.program_owner], &mut ctx)
680+
.await
681+
.unwrap();
682+
683+
// try to transfer more than balance
684+
let (accs, args) = init_accs_args(
685+
&good_ntt,
686+
&mut ctx,
687+
&test_data,
688+
outbox_item.pubkey(),
689+
more_than_balance,
690+
false,
691+
);
692+
693+
approve_token_authority(
694+
&good_ntt,
695+
&test_data.user_token_account,
696+
&test_data.user.pubkey(),
697+
&args,
698+
)
699+
.submit_with_signers(&[&test_data.user], &mut ctx)
700+
.await
701+
.unwrap();
702+
let err = transfer(&good_ntt, accs, args, Mode::Locking)
703+
.submit_with_signers(&[&outbox_item], &mut ctx)
704+
.await
705+
.unwrap_err();
706+
707+
assert_eq!(
708+
err.unwrap(),
709+
TransactionError::InstructionError(
710+
0,
711+
InstructionError::Custom(spl_token::error::TokenError::InsufficientFunds as u32,)
712+
)
713+
);
714+
}
715+
665716
async fn assert_queued(ctx: &mut ProgramTestContext, outbox_item: Pubkey) {
666717
let outbox_item_account: OutboxItem = ctx.get_account_data_anchor(outbox_item).await;
667718

0 commit comments

Comments
 (0)