1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#[macro_export]
macro_rules! get_accounts {
($rpc:expr, $($key:expr => $type:ty),+ $(,)?) => {{
async {
use anchor_lang::AccountDeserialize;
let keys = vec![$($key),+];
let accounts = $rpc
.get_multiple_accounts(&keys)
.await?
.into_iter();
let mut iter = accounts.into_iter();
let result = (
$(
{
let account = iter
.next()
.ok_or_else(|| anyhow::anyhow!("Missing account"))?
.ok_or_else(|| anyhow::anyhow!("Account not found"))?;
<$type>::try_deserialize(
&mut account.data.as_slice()
)?
}
),+
);
anyhow::Ok(result)
}
}};
}
#[macro_export]
macro_rules! try_get_accounts {
($rpc:expr, $($key:expr => $type:ty),+ $(,)?) => {{
async {
let keys = vec![$($key),+];
let accounts = $rpc.get_multiple_accounts(&keys).await?;
let mut idx = 0;
#[allow(unused_assignments)]
let result = (
$(
{
match accounts[idx].as_ref() {
Some(acc) => {
let deserialized = <$type>::try_deserialize(&mut &acc.data[..])?;
idx += 1;
Some(deserialized)
},
None => None
}
}
),+
);
Ok::<_, anyhow::Error>(result)
}
}};
}
#[macro_export]
macro_rules! get_accounts_owners {
($rpc:expr, $($key:expr),+ $(,)?) => {{
async {
let keys = vec![$($key),+];
let accounts = $rpc.get_multiple_accounts(&keys).await?;
let mut idx = 0;
#[allow(unused_assignments)]
let result = (
$(
{
let _ = $key;
let account = accounts
.get(idx)
.ok_or_else(|| anyhow::anyhow!("Account at index {} not found", idx))?
.as_ref()
.ok_or_else(|| anyhow::anyhow!("Account at index {} is None", idx))?
.owner;
idx += 1;
account
}
),+
);
anyhow::Ok(result)
}
}};
}