Skip to content

Commit 451ded3

Browse files
authored
Improve assert_keys_eq and assert_keys_neq macros (#16)
1 parent 8b6fe13 commit 451ded3

File tree

2 files changed

+55
-36
lines changed

2 files changed

+55
-36
lines changed

src/assert.rs

Lines changed: 54 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ macro_rules! assert_ata {
3232
let __mint = anchor_lang::Key::key(&$mint);
3333
let __ata = anchor_lang::Key::key(&$ata);
3434
let __real_ata =
35-
spl_associated_token_account::get_associated_token_address(&__owner, &__mint);
35+
$crate::spl_associated_token_account::get_associated_token_address(&__owner, &__mint);
3636
if __real_ata != __ata {
3737
msg!(
3838
"ATA mismatch: {}: {} (left) != {} (right)",
@@ -59,7 +59,7 @@ macro_rules! assert_is_ata {
5959
let __mint = $ata.mint;
6060
let __ata = anchor_lang::Key::key(&$ata);
6161
let __real_ata =
62-
spl_associated_token_account::get_associated_token_address(&__owner, &__mint);
62+
$crate::spl_associated_token_account::get_associated_token_address(&__owner, &__mint);
6363
if __real_ata != __ata {
6464
msg!(
6565
"Invalid ATA: {}: {} (left) != {} (right)",
@@ -133,28 +133,37 @@ macro_rules! assert_owner {
133133
#[macro_export]
134134
macro_rules! assert_keys_eq {
135135
($account_a: expr, $account_b: expr $(,)?) => {
136-
assert_keys_eq!($account_a, $account_b, $crate::VipersError::KeyMismatch)
136+
assert_keys_eq!($account_a, $account_b, $crate::VipersError::KeyMismatch);
137137
};
138-
($account_a: expr, $account_b: expr, $err: ident) => {
139-
assert_keys_eq!($account_a, $account_b, crate::ErrorCode::$ident)
138+
($account_a: expr, $account_b: expr, $err_code: ident $(,)?) => {
139+
assert_keys_eq!($account_a, $account_b, crate::ErrorCode::$err_code);
140140
};
141-
($account_a: expr, $account_b: expr, $msg: literal) => {
142-
let __account_a = anchor_lang::Key::key(&$account_a);
143-
let __account_b = anchor_lang::Key::key(&$account_b);
144-
if __account_a != __account_b {
145-
msg!(
146-
"Key mismatch: {}: {} (left) != {} (right)",
147-
$msg,
148-
__account_a,
149-
__account_b
150-
);
151-
return Err($crate::VipersError::KeyMismatch.into());
152-
}
141+
($account_a: expr, $account_b: expr, $msg: literal $(,)?) => {
142+
assert_keys_eq!(
143+
$account_a,
144+
$account_b,
145+
$crate::VipersError::KeyMismatch,
146+
&*format!("Key mismatch: {}", $msg),
147+
);
153148
};
154149
($account_a: expr, $account_b: expr, $err: expr $(,)?) => {
155-
let __account_a = anchor_lang::Key::key(&$account_a);
156-
let __account_b = anchor_lang::Key::key(&$account_b);
150+
assert_keys_eq!(
151+
$account_a,
152+
$account_b,
153+
$err,
154+
&*format!("{:?}: {}", $err, $err)
155+
);
156+
};
157+
($account_a: expr, $account_b: expr, $err: expr, $msg: expr $(,)?) => {
158+
let __account_a = $account_a.key();
159+
let __account_b = $account_b.key();
157160
if __account_a != __account_b {
161+
msg!($msg);
162+
msg!(stringify!($account_a != $account_b));
163+
msg!("Left:");
164+
msg!("{}", __account_a);
165+
msg!("Right:");
166+
msg!("{}", __account_b);
158167
return Err($err.into());
159168
}
160169
};
@@ -182,28 +191,37 @@ macro_rules! assert_keys_neq {
182191
$account_a,
183192
$account_b,
184193
$crate::VipersError::KeysMustNotMatch
185-
)
194+
);
186195
};
187-
($account_a: expr, $account_b: expr, $err: ident) => {
188-
assert_keys_neq!($account_a, $account_b, crate::ErrorCode::$ident)
196+
($account_a: expr, $account_b: expr, $err_code: ident $(,)?) => {
197+
assert_keys_neq!($account_a, $account_b, crate::ErrorCode::$err_code);
189198
};
190-
($account_a: expr, $account_b: expr, $msg: literal) => {
191-
let __account_a = anchor_lang::Key::key(&$account_a);
192-
let __account_b = anchor_lang::Key::key(&$account_b);
193-
if __account_a == __account_b {
194-
msg!(
195-
"Keys must not match: {}: {} (left) == {} (right)",
196-
$msg,
197-
__account_a,
198-
__account_b
199-
);
200-
return Err($crate::VipersError::KeysMustNotMatch.into());
201-
}
199+
($account_a: expr, $account_b: expr, $msg: literal $(,)?) => {
200+
assert_keys_neq!(
201+
$account_a,
202+
$account_b,
203+
$crate::VipersError::KeysMustNotMatch,
204+
&*format!("Keys must not match: {}", $msg),
205+
);
202206
};
203207
($account_a: expr, $account_b: expr, $err: expr $(,)?) => {
204-
let __account_a = anchor_lang::Key::key(&$account_a);
205-
let __account_b = anchor_lang::Key::key(&$account_b);
208+
assert_keys_neq!(
209+
$account_a,
210+
$account_b,
211+
$err,
212+
&*format!("{:?}: {}", $err, $err)
213+
);
214+
};
215+
($account_a: expr, $account_b: expr, $err: expr, $msg: expr $(,)?) => {
216+
let __account_a = $account_a.key();
217+
let __account_b = $account_b.key();
206218
if __account_a == __account_b {
219+
msg!($msg);
220+
msg!(stringify!($account_a == $account_b));
221+
msg!("Left:");
222+
msg!("{}", __account_a);
223+
msg!("Right:");
224+
msg!("{}", __account_b);
207225
return Err($err.into());
208226
}
209227
};

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pub mod assert;
77
pub mod validate;
88

99
use anchor_lang::prelude::*;
10+
pub use spl_associated_token_account;
1011

1112
declare_id!("VipersTest111111111111111111111111111111111");
1213

0 commit comments

Comments
 (0)