-
Notifications
You must be signed in to change notification settings - Fork 24
/
register_queries.rs
244 lines (215 loc) · 9.01 KB
/
register_queries.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
use crate::bindings::query::NeutronQuery;
use crate::interchain_queries::types::{
QueryType, TransactionFilterItem, TransactionFilterOp, TransactionFilterValue, BANK_STORE_KEY,
DISTRIBUTION_STORE_KEY, GOV_STORE_KEY, HEIGHT_FIELD, KEY_BOND_DENOM, PARAMS_STORE_KEY,
RECIPIENT_FIELD, STAKING_STORE_KEY,
};
use crate::{
bindings::{msg::NeutronMsg, types::KVKey},
errors::error::NeutronResult,
interchain_queries::{
helpers::{
create_account_denom_balance_key, create_delegation_key, create_fee_pool_key,
create_gov_proposal_key, create_params_store_key, create_total_denom_key,
create_validator_key, decode_and_convert,
},
types::QueryPayload,
},
};
use cosmwasm_std::{Binary, DepsMut, Env};
#[allow(clippy::too_many_arguments)]
/// Creates a message to register an Interchain Query with provided params
pub fn new_register_interchain_query_msg(
_deps: DepsMut<NeutronQuery>,
_env: Env,
connection_id: String,
query_type: QueryType,
kv_keys: Vec<KVKey>,
transactions_filter: Vec<TransactionFilterItem>,
update_period: u64,
) -> NeutronResult<NeutronMsg> {
match query_type {
QueryType::KV => NeutronMsg::register_interchain_query(
QueryPayload::KV(kv_keys),
connection_id,
update_period,
),
QueryType::TX => NeutronMsg::register_interchain_query(
QueryPayload::TX(transactions_filter),
connection_id,
update_period,
),
}
}
/// Creates a message to register an Interchain Query to get balance of account on remote chain for particular denom
///
/// * **connection_id** is an IBC connection identifier between Neutron and remote chain;
/// * **addr** address of an account on remote chain for which you want to get balances;
/// * **denom** denomination of the coin for which you want to get balance;
/// * **update_period** is used to say how often the query must be updated.
pub fn new_register_balance_query_msg(
connection_id: String,
addr: String,
denom: String,
update_period: u64,
) -> NeutronResult<NeutronMsg> {
let converted_addr_bytes = decode_and_convert(addr.as_str())?;
let balance_key = create_account_denom_balance_key(converted_addr_bytes, denom)?;
let kv_key = KVKey {
path: BANK_STORE_KEY.to_string(),
key: Binary(balance_key),
};
NeutronMsg::register_interchain_query(
QueryPayload::KV(vec![kv_key]),
connection_id,
update_period,
)
}
/// Creates a message to register an Interchain Query to get total supply on remote chain for particular denom
///
/// * **connection_id** is an IBC connection identifier between Neutron and remote chain;
/// * **denom** denomination of the coin for which you want to get total supply;
/// * **update_period** is used to say how often the query must be updated.
pub fn new_register_bank_total_supply_query_msg(
connection_id: String,
denoms: Vec<String>,
update_period: u64,
) -> NeutronResult<NeutronMsg> {
let mut kv_keys: Vec<KVKey> = Vec::with_capacity(denoms.len());
for denom in denoms {
let supply_key = create_total_denom_key(denom)?;
let kv_key = KVKey {
path: BANK_STORE_KEY.to_string(),
key: Binary(supply_key),
};
kv_keys.push(kv_key)
}
NeutronMsg::register_interchain_query(QueryPayload::KV(kv_keys), connection_id, update_period)
}
/// Creates a message to register an Interchain Query to get fee pool on remote chain from distribution module
///
/// * **connection_id** is an IBC connection identifier between Neutron and remote chain;
/// * **update_period** is used to say how often the query must be updated.
pub fn new_register_distribution_fee_pool_query_msg(
connection_id: String,
update_period: u64,
) -> NeutronResult<NeutronMsg> {
let kv_key = KVKey {
path: DISTRIBUTION_STORE_KEY.to_string(),
key: Binary(create_fee_pool_key()?),
};
NeutronMsg::register_interchain_query(
QueryPayload::KV(vec![kv_key]),
connection_id,
update_period,
)
}
/// Creates a message to register an Interchain Query to get governance proposal on remote chain
///
/// * **connection_id** is an IBC connection identifier between Neutron and remote chain;
/// * **proposal_id** is a proposal id from remote chain.
/// * **update_period** is used to say how often the query must be updated.
pub fn new_register_gov_proposal_query_msg(
connection_id: String,
proposals_ids: Vec<u64>,
update_period: u64,
) -> NeutronResult<NeutronMsg> {
let mut kv_keys: Vec<KVKey> = Vec::with_capacity(proposals_ids.len());
for id in proposals_ids {
let kv_key = KVKey {
path: GOV_STORE_KEY.to_string(),
key: Binary(create_gov_proposal_key(id)?),
};
kv_keys.push(kv_key)
}
NeutronMsg::register_interchain_query(QueryPayload::KV(kv_keys), connection_id, update_period)
}
/// Creates a message to register an Interchain Query to get validator info on remote chain
///
/// * **connection_id** is an IBC connection identifier between Neutron and remote chain;
/// * **validator** is an validator operator address of an account on remote chain for which you want to get rewards ;
/// * **update_period** is used to say how often the query must be updated.
pub fn new_register_staking_validators_query_msg(
connection_id: String,
validators: Vec<String>,
update_period: u64,
) -> NeutronResult<NeutronMsg> {
let mut kv_keys: Vec<KVKey> = Vec::with_capacity(validators.len());
for validator in validators {
let val_addr = decode_and_convert(&validator)?;
let kv_key = KVKey {
path: STAKING_STORE_KEY.to_string(),
key: Binary(create_validator_key(&val_addr)?),
};
kv_keys.push(kv_key)
}
NeutronMsg::register_interchain_query(QueryPayload::KV(kv_keys), connection_id, update_period)
}
/// Creates a message to register an Interchain Query to get delegations of particular delegator on remote chain.
///
/// * **connection_id** is an IBC connection identifier between Neutron and remote chain;
/// * **delegator** is an address of an account on remote chain for which you want to get list of delegations;
/// * **validators** is a list of validators addresses for which you want to get delegations from particular **delegator**;
/// * **update_period** is used to say how often the query must be updated.
pub fn new_register_delegator_delegations_query_msg(
connection_id: String,
delegator: String,
validators: Vec<String>,
update_period: u64,
) -> NeutronResult<NeutronMsg> {
let delegator_addr = decode_and_convert(delegator.as_str())?;
// Allocate memory for such KV keys as:
// * staking module params to get staking denomination
// * validators structures to calculate amount of delegated tokens
// * delegations structures to get info about delegations itself
let mut keys: Vec<KVKey> = Vec::with_capacity(validators.len() * 2 + 1);
// create KV key to get BondDenom from staking module params
keys.push(KVKey {
path: PARAMS_STORE_KEY.to_string(),
key: Binary(create_params_store_key(STAKING_STORE_KEY, KEY_BOND_DENOM)),
});
for v in &validators {
// create delegation key to get delegation structure
let val_addr = decode_and_convert(v.as_str())?;
keys.push(KVKey {
path: STAKING_STORE_KEY.to_string(),
key: Binary(create_delegation_key(&delegator_addr, &val_addr)?),
});
// create validator key to get validator structure
keys.push(KVKey {
path: STAKING_STORE_KEY.to_string(),
key: Binary(create_validator_key(&val_addr)?),
})
}
NeutronMsg::register_interchain_query(QueryPayload::KV(keys), connection_id, update_period)
}
/// Creates a message to register an Interchain Query to get transfer events to a recipient on a remote chain.
///
/// * **connection_id** is an IBC connection identifier between Neutron and remote chain;
/// * **recipient** is an address of an account on remote chain for which you want to get list of transfer transactions;
/// * **update_period** is used to say how often the query must be updated.
/// * **min_height** is used to set min height for query (by default = 0).
pub fn new_register_transfers_query_msg(
connection_id: String,
recipient: String,
update_period: u64,
min_height: Option<u64>,
) -> NeutronResult<NeutronMsg> {
let mut query_data = vec![TransactionFilterItem {
field: RECIPIENT_FIELD.to_string(),
op: TransactionFilterOp::Eq,
value: TransactionFilterValue::String(recipient),
}];
if let Some(min_height) = min_height {
query_data.push(TransactionFilterItem {
field: HEIGHT_FIELD.to_string(),
op: TransactionFilterOp::Gte,
value: TransactionFilterValue::Int(min_height),
})
}
NeutronMsg::register_interchain_query(
QueryPayload::TX(query_data),
connection_id,
update_period,
)
}