-
Notifications
You must be signed in to change notification settings - Fork 356
/
Copy pathenumerable.rs
210 lines (182 loc) · 7.1 KB
/
enumerable.rs
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
use cosmwasm_std::{Deps, Order, StdResult};
use cw20::{AllAccountsResponse, AllAllowancesResponse, AllowanceInfo};
use crate::state::{ALLOWANCES, BALANCES};
use cw_storage_plus::Bound;
// settings for pagination
const MAX_LIMIT: u32 = 30;
const DEFAULT_LIMIT: u32 = 10;
pub fn query_all_allowances(
deps: Deps,
owner: String,
start_after: Option<String>,
limit: Option<u32>,
) -> StdResult<AllAllowancesResponse> {
let owner_addr = deps.api.addr_validate(&owner)?;
let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize;
let start = start_after.map(|s| Bound::ExclusiveRaw(s.into_bytes()));
let allowances = ALLOWANCES
.prefix(&owner_addr)
.range(deps.storage, start, None, Order::Ascending)
.take(limit)
.map(|item| {
item.map(|(addr, allow)| AllowanceInfo {
spender: addr.into(),
allowance: allow.allowance,
expires: allow.expires,
})
})
.collect::<StdResult<_>>()?;
Ok(AllAllowancesResponse { allowances })
}
pub fn query_all_accounts(
deps: Deps,
start_after: Option<String>,
limit: Option<u32>,
) -> StdResult<AllAccountsResponse> {
let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize;
let start = start_after.map(|s| Bound::ExclusiveRaw(s.into()));
let accounts = BALANCES
.keys(deps.storage, start, None, Order::Ascending)
.take(limit)
.map(|item| item.map(Into::into))
.collect::<StdResult<_>>()?;
Ok(AllAccountsResponse { accounts })
}
#[cfg(test)]
mod tests {
use super::*;
use cosmwasm_std::testing::{mock_dependencies_with_balance, mock_env, mock_info};
use cosmwasm_std::{coins, DepsMut, Uint128};
use cw20::{Cw20Coin, Expiration, TokenInfoResponse};
use crate::contract::{execute, instantiate, query_token_info};
use crate::msg::{ExecuteMsg, InstantiateMsg};
// this will set up the instantiation for other tests
fn do_instantiate(mut deps: DepsMut, addr: &str, amount: Uint128) -> TokenInfoResponse {
let instantiate_msg = InstantiateMsg {
name: "Auto Gen".to_string(),
symbol: "AUTO".to_string(),
decimals: 3,
initial_balances: vec![Cw20Coin {
address: addr.into(),
amount,
}],
mint: None,
marketing: None,
};
let info = mock_info("creator", &[]);
let env = mock_env();
instantiate(deps.branch(), env, info, instantiate_msg).unwrap();
query_token_info(deps.as_ref()).unwrap()
}
#[test]
fn query_all_allowances_works() {
let mut deps = mock_dependencies_with_balance(&coins(2, "token"));
let owner = String::from("owner");
// these are in alphabetical order same than insert order
let spender1 = String::from("earlier");
let spender2 = String::from("later");
let info = mock_info(owner.as_ref(), &[]);
let env = mock_env();
do_instantiate(deps.as_mut(), &owner, Uint128::new(12340000));
// no allowance to start
let allowances = query_all_allowances(deps.as_ref(), owner.clone(), None, None).unwrap();
assert_eq!(allowances.allowances, vec![]);
// set allowance with height expiration
let allow1 = Uint128::new(7777);
let expires = Expiration::AtHeight(5432);
let msg = ExecuteMsg::IncreaseAllowance {
spender: spender1.clone(),
amount: allow1,
expires: Some(expires),
};
execute(deps.as_mut(), env.clone(), info.clone(), msg).unwrap();
// set allowance with no expiration
let allow2 = Uint128::new(54321);
let msg = ExecuteMsg::IncreaseAllowance {
spender: spender2.clone(),
amount: allow2,
expires: None,
};
execute(deps.as_mut(), env, info, msg).unwrap();
// query list gets 2
let allowances = query_all_allowances(deps.as_ref(), owner.clone(), None, None).unwrap();
assert_eq!(allowances.allowances.len(), 2);
// first one is spender1 (order of CanonicalAddr uncorrelated with String)
let allowances = query_all_allowances(deps.as_ref(), owner.clone(), None, Some(1)).unwrap();
assert_eq!(allowances.allowances.len(), 1);
let allow = &allowances.allowances[0];
assert_eq!(&allow.spender, &spender1);
assert_eq!(&allow.expires, &expires);
assert_eq!(&allow.allowance, &allow1);
// next one is spender2
let allowances = query_all_allowances(
deps.as_ref(),
owner,
Some(allow.spender.clone()),
Some(10000),
)
.unwrap();
assert_eq!(allowances.allowances.len(), 1);
let allow = &allowances.allowances[0];
assert_eq!(&allow.spender, &spender2);
assert_eq!(&allow.expires, &Expiration::Never {});
assert_eq!(&allow.allowance, &allow2);
}
#[test]
fn query_all_accounts_works() {
let mut deps = mock_dependencies_with_balance(&coins(2, "token"));
// insert order and lexicographical order are different
let acct1 = String::from("acct01");
let acct2 = String::from("zebra");
let acct3 = String::from("nice");
let acct4 = String::from("aaaardvark");
let expected_order = [acct4.clone(), acct1.clone(), acct3.clone(), acct2.clone()];
do_instantiate(deps.as_mut(), &acct1, Uint128::new(12340000));
// put money everywhere (to create balanaces)
let info = mock_info(acct1.as_ref(), &[]);
let env = mock_env();
execute(
deps.as_mut(),
env.clone(),
info.clone(),
ExecuteMsg::Transfer {
recipient: acct2,
amount: Uint128::new(222222),
},
)
.unwrap();
execute(
deps.as_mut(),
env.clone(),
info.clone(),
ExecuteMsg::Transfer {
recipient: acct3,
amount: Uint128::new(333333),
},
)
.unwrap();
execute(
deps.as_mut(),
env,
info,
ExecuteMsg::Transfer {
recipient: acct4,
amount: Uint128::new(444444),
},
)
.unwrap();
// make sure we get the proper results
let accounts = query_all_accounts(deps.as_ref(), None, None).unwrap();
assert_eq!(accounts.accounts, expected_order);
// let's do pagination
let accounts = query_all_accounts(deps.as_ref(), None, Some(2)).unwrap();
assert_eq!(accounts.accounts, expected_order[0..2].to_vec());
let accounts =
query_all_accounts(deps.as_ref(), Some(accounts.accounts[1].clone()), Some(1)).unwrap();
assert_eq!(accounts.accounts, expected_order[2..3].to_vec());
let accounts =
query_all_accounts(deps.as_ref(), Some(accounts.accounts[0].clone()), Some(777))
.unwrap();
assert_eq!(accounts.accounts, expected_order[3..].to_vec());
}
}