Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 90b5ce6

Browse files
Standardize failure case ordering (#509)
* Standardize processor failure-case ordering * Fix token-swap test
1 parent 759fe14 commit 90b5ce6

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

token-swap/program/src/processor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2086,7 +2086,7 @@ mod tests {
20862086
) = accounts.setup_token_accounts(
20872087
&user_key,
20882088
&withdrawer_key,
2089-
initial_a,
2089+
withdraw_amount,
20902090
initial_b,
20912091
withdraw_amount,
20922092
);

token/program/src/processor.rs

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,12 @@ impl Processor {
161161
let mut source_account = Account::unpack(&source_account_info.data.borrow())?;
162162
let mut dest_account = Account::unpack(&dest_account_info.data.borrow())?;
163163

164-
if source_account.amount < amount {
165-
return Err(TokenError::InsufficientFunds.into());
166-
}
167164
if source_account.is_frozen() || dest_account.is_frozen() {
168165
return Err(TokenError::AccountFrozen.into());
169166
}
167+
if source_account.amount < amount {
168+
return Err(TokenError::InsufficientFunds.into());
169+
}
170170
if source_account.mint != dest_account.mint {
171171
return Err(TokenError::MintMismatch.into());
172172
}
@@ -478,17 +478,17 @@ impl Processor {
478478
let mut source_account = Account::unpack(&source_account_info.data.borrow())?;
479479
let mut mint = Mint::unpack(&mint_info.data.borrow())?;
480480

481+
if source_account.is_frozen() {
482+
return Err(TokenError::AccountFrozen.into());
483+
}
481484
if source_account.is_native() {
482485
return Err(TokenError::NativeNotSupported.into());
483486
}
484-
if mint_info.key != &source_account.mint {
485-
return Err(TokenError::MintMismatch.into());
486-
}
487487
if source_account.amount < amount {
488488
return Err(TokenError::InsufficientFunds.into());
489489
}
490-
if source_account.is_frozen() {
491-
return Err(TokenError::AccountFrozen.into());
490+
if mint_info.key != &source_account.mint {
491+
return Err(TokenError::MintMismatch.into());
492492
}
493493

494494
if let Some(expected_decimals) = expected_decimals {
@@ -588,15 +588,15 @@ impl Processor {
588588
let authority_info = next_account_info(account_info_iter)?;
589589

590590
let mut source_account = Account::unpack(&source_account_info.data.borrow())?;
591+
if freeze && source_account.is_frozen() || !freeze && !source_account.is_frozen() {
592+
return Err(TokenError::InvalidState.into());
593+
}
591594
if source_account.is_native() {
592595
return Err(TokenError::NativeNotSupported.into());
593596
}
594597
if mint_info.key != &source_account.mint {
595598
return Err(TokenError::MintMismatch.into());
596599
}
597-
if freeze && source_account.is_frozen() || !freeze && !source_account.is_frozen() {
598-
return Err(TokenError::InvalidState.into());
599-
}
600600

601601
let mint = Mint::unpack(&mint_info.data.borrow_mut())?;
602602
match mint.freeze_authority {
@@ -3402,9 +3402,6 @@ mod tests {
34023402
],
34033403
)
34043404
.unwrap();
3405-
let mut account = Account::unpack_unchecked(&mismatch_account.data).unwrap();
3406-
account.mint = mint2_key;
3407-
Account::pack(account, &mut mismatch_account.data).unwrap();
34083405

34093406
// mint to account
34103407
do_process_instruction(
@@ -3413,6 +3410,16 @@ mod tests {
34133410
)
34143411
.unwrap();
34153412

3413+
// mint to mismatch account and change mint key
3414+
do_process_instruction(
3415+
mint_to(&program_id, &mint_key, &mismatch_key, &owner_key, &[], 1000).unwrap(),
3416+
vec![&mut mint_account, &mut mismatch_account, &mut owner_account],
3417+
)
3418+
.unwrap();
3419+
let mut account = Account::unpack_unchecked(&mismatch_account.data).unwrap();
3420+
account.mint = mint2_key;
3421+
Account::pack(account, &mut mismatch_account.data).unwrap();
3422+
34163423
// missing signer
34173424
let mut instruction =
34183425
burn(&program_id, &account_key, &mint_key, &delegate_key, &[], 42).unwrap();
@@ -3471,7 +3478,7 @@ mod tests {
34713478
.unwrap();
34723479

34733480
let mint = Mint::unpack_unchecked(&mint_account.data).unwrap();
3474-
assert_eq!(mint.supply, 1000 - 42);
3481+
assert_eq!(mint.supply, 2000 - 42);
34753482
let account = Account::unpack_unchecked(&account_account.data).unwrap();
34763483
assert_eq!(account.amount, 1000 - 42);
34773484

@@ -3541,7 +3548,7 @@ mod tests {
35413548

35423549
// match
35433550
let mint = Mint::unpack_unchecked(&mint_account.data).unwrap();
3544-
assert_eq!(mint.supply, 1000 - 42 - 84);
3551+
assert_eq!(mint.supply, 2000 - 42 - 84);
35453552
let account = Account::unpack_unchecked(&account_account.data).unwrap();
35463553
assert_eq!(account.amount, 1000 - 42 - 84);
35473554

0 commit comments

Comments
 (0)