Macro vipers::assert_is_zero_token_account
source · [−]macro_rules! assert_is_zero_token_account {
($token_account: expr $(,)?) => { ... };
($token_account: expr, $err_code: ident $(,)?) => { ... };
($token_account: expr, $msg: literal $(,)?) => { ... };
($token_account: expr, $err: expr $(,)?) => { ... };
($token_account: expr, $err: expr, $msg: expr $(,)?) => { ... };
}
Expand description
Asserts that a token account is “zero”.
This means that:
- the
amount
is zero - the
delegate
isNone
- the
close_authority
isNone
This is useful for checking to see that a bad actor cannot modify PDA-owned token accounts.
Example
#[error_code]
pub enum ErrorCode { MyError }
let mut zero_account = spl_token::state::Account::default();
assert_does_not_throw!({
assert_is_zero_token_account!(zero_account);
});
let mut non_zero_account = spl_token::state::Account::default();
non_zero_account.amount = 10;
assert_throws!({
assert_is_zero_token_account!(non_zero_account);
}, vipers::VipersError::TokenAccountIsNonZero);
non_zero_account = spl_token::state::Account::default();
non_zero_account.delegate = spl_token::ID.into();
assert_throws!({
assert_is_zero_token_account!(non_zero_account);
}, vipers::VipersError::TokenAccountIsNonZero);
non_zero_account = spl_token::state::Account::default();
non_zero_account.close_authority = spl_token::ID.into();
assert_throws!({
assert_is_zero_token_account!(non_zero_account);
}, vipers::VipersError::TokenAccountIsNonZero);