-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Query wasm contract state #NTRN-476 #96
Changes from 7 commits
f4e855d
3d616ab
4b5067f
dc2cabb
03b004c
3ce6204
d685b79
4cf3899
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,33 +2,39 @@ use cosmos_sdk_proto::cosmos::bank::v1beta1::MsgSend; | |
use cosmos_sdk_proto::cosmos::tx::v1beta1::{TxBody, TxRaw}; | ||
use cosmwasm_std::{ | ||
entry_point, to_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdError, StdResult, | ||
Uint128, | ||
}; | ||
use cw2::set_contract_version; | ||
use prost::Message as ProstMessage; | ||
|
||
use crate::msg::{ExecuteMsg, GetRecipientTxsResponse, InstantiateMsg, MigrateMsg, QueryMsg}; | ||
use crate::msg::{ | ||
Cw20BalanceResponse, ExecuteMsg, GetRecipientTxsResponse, InstantiateMsg, MigrateMsg, QueryMsg, | ||
}; | ||
use crate::state::{Transfer, RECIPIENT_TXS, TRANSFERS}; | ||
use neutron_sdk::bindings::msg::NeutronMsg; | ||
use neutron_sdk::bindings::query::{NeutronQuery, QueryRegisteredQueryResponse}; | ||
use neutron_sdk::bindings::types::{Height, KVKey}; | ||
use neutron_sdk::interchain_queries::get_registered_query; | ||
use neutron_sdk::interchain_queries::v045::queries::{ | ||
query_balance, query_bank_total, query_delegations, query_distribution_fee_pool, | ||
query_government_proposals, query_staking_validators, | ||
}; | ||
use neutron_sdk::interchain_queries::v045::{ | ||
new_register_balance_query_msg, new_register_bank_total_supply_query_msg, | ||
new_register_delegator_delegations_query_msg, new_register_distribution_fee_pool_query_msg, | ||
new_register_gov_proposal_query_msg, new_register_staking_validators_query_msg, | ||
new_register_transfers_query_msg, | ||
use neutron_sdk::interchain_queries::{ | ||
check_query_type, get_registered_query, query_kv_result, | ||
v045::{ | ||
new_register_balance_query_msg, new_register_bank_total_supply_query_msg, | ||
new_register_delegator_delegations_query_msg, new_register_distribution_fee_pool_query_msg, | ||
new_register_gov_proposal_query_msg, new_register_staking_validators_query_msg, | ||
new_register_transfers_query_msg, | ||
register_queries::new_register_wasm_contract_store_query_msg, | ||
types::{COSMOS_SDK_TRANSFER_MSG_URL, RECIPIENT_FIELD}, | ||
}, | ||
}; | ||
use neutron_sdk::sudo::msg::SudoMsg; | ||
use neutron_sdk::{NeutronError, NeutronResult}; | ||
|
||
use neutron_sdk::interchain_queries::types::{ | ||
TransactionFilterItem, TransactionFilterOp, TransactionFilterValue, | ||
QueryType, TransactionFilterItem, TransactionFilterOp, TransactionFilterValue, | ||
}; | ||
use neutron_sdk::interchain_queries::v045::types::{COSMOS_SDK_TRANSFER_MSG_URL, RECIPIENT_FIELD}; | ||
use serde_json_wasm; | ||
|
||
/// defines the incoming transfers limit to make a case of failed callback possible. | ||
|
@@ -95,6 +101,17 @@ pub fn execute( | |
update_period, | ||
min_height, | ||
} => register_transfers_query(connection_id, recipient, update_period, min_height), | ||
ExecuteMsg::RegisterCw20BalanceQuery { | ||
connection_id, | ||
update_period, | ||
cw20_contract_address, | ||
account_address, | ||
} => register_cw20_balance_query( | ||
connection_id, | ||
update_period, | ||
cw20_contract_address, | ||
account_address, | ||
), | ||
ExecuteMsg::UpdateInterchainQuery { | ||
query_id, | ||
new_keys, | ||
|
@@ -183,6 +200,28 @@ pub fn register_transfers_query( | |
Ok(Response::new().add_message(msg)) | ||
} | ||
|
||
pub fn register_cw20_balance_query( | ||
connection_id: String, | ||
update_period: u64, | ||
cw20_contract_address: String, | ||
account_address: String, | ||
) -> NeutronResult<Response<NeutronMsg>> { | ||
// cw_storage_plus uses this prefix for maps | ||
let mut storage_key = vec![0u8, 7u8]; | ||
|
||
storage_key.extend_from_slice("balance".as_bytes()); | ||
storage_key.extend_from_slice(account_address.as_bytes()); | ||
Comment on lines
+210
to
+213
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This stuff definitely should be in the SDK helpers not here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately, it shouldn't be there, because these values are contract-dependent. I wish I could make a cw20 balance helper, but it is impossible to do. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think Mike means that it's not only There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but now I wonder if we need such a helper — to query a cw20 balance There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, I like it how it is now, a general There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I could implement some kind of storage key factory like this: let key: Vec<u8> =
StorageKeyFactory::new()
.push_bytes([0, 7])
.push_string("balance")
.push_string(account_address)
.into_bytes(); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am very hesitant creating even some kind of "general helper which works for canonical cw20-base contract" because users will definitely try to use it and it won't work in some cases. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
To be honest it doesn't make anything easier to use, I think I should abandon this idea. |
||
|
||
let msg = new_register_wasm_contract_store_query_msg( | ||
connection_id, | ||
cw20_contract_address, | ||
&storage_key, | ||
update_period, | ||
)?; | ||
|
||
Ok(Response::new().add_message(msg)) | ||
} | ||
|
||
pub fn update_interchain_query( | ||
query_id: u64, | ||
new_keys: Option<Vec<KVKey>>, | ||
|
@@ -227,6 +266,9 @@ pub fn query(deps: Deps<NeutronQuery>, env: Env, msg: QueryMsg) -> NeutronResult | |
QueryMsg::GetDelegations { query_id } => { | ||
Ok(to_binary(&query_delegations(deps, env, query_id)?)?) | ||
} | ||
QueryMsg::Cw20Balance { query_id } => { | ||
Ok(to_binary(&query_cw20_balance(deps, env, query_id)?)?) | ||
} | ||
QueryMsg::GetRegisteredQuery { query_id } => { | ||
Ok(to_binary(&get_registered_query(deps, query_id)?)?) | ||
} | ||
|
@@ -241,6 +283,19 @@ fn query_recipient_txs(deps: Deps<NeutronQuery>, recipient: String) -> NeutronRe | |
Ok(to_binary(&GetRecipientTxsResponse { transfers: txs })?) | ||
} | ||
|
||
pub fn query_cw20_balance( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if we create a |
||
deps: Deps<NeutronQuery>, | ||
_env: Env, | ||
registered_query_id: u64, | ||
) -> NeutronResult<Cw20BalanceResponse> { | ||
let registered_query = get_registered_query(deps, registered_query_id)?; | ||
|
||
check_query_type(registered_query.registered_query.query_type, QueryType::KV)?; | ||
|
||
let balance: Uint128 = query_kv_result(deps, registered_query_id)?; | ||
Ok(Cw20BalanceResponse { balance }) | ||
} | ||
|
||
#[cfg_attr(not(feature = "library"), entry_point)] | ||
pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> StdResult<Response> { | ||
deps.api.debug("WASMDEBUG: migrate"); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
use crate::state::Transfer; | ||
use cosmwasm_std::Uint128; | ||
use neutron_sdk::bindings::types::KVKey; | ||
use schemars::JsonSchema; | ||
use serde::{Deserialize, Serialize}; | ||
|
@@ -46,6 +47,12 @@ pub enum ExecuteMsg { | |
connection_id: String, | ||
update_period: u64, | ||
}, | ||
RegisterCw20BalanceQuery { | ||
connection_id: String, | ||
update_period: u64, | ||
cw20_contract_address: String, | ||
account_address: String, | ||
}, | ||
UpdateInterchainQuery { | ||
query_id: u64, | ||
new_keys: Option<Vec<KVKey>>, | ||
|
@@ -66,10 +73,17 @@ pub enum QueryMsg { | |
StakingValidators { query_id: u64 }, | ||
GovernmentProposals { query_id: u64 }, | ||
GetDelegations { query_id: u64 }, | ||
Cw20Balance { query_id: u64 }, | ||
GetRegisteredQuery { query_id: u64 }, | ||
GetRecipientTxs { recipient: String }, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] | ||
#[serde(rename_all = "snake_case")] | ||
pub struct Cw20BalanceResponse { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if we create a new_register_cw20_balance_query_msg in the helpers package, we should move this one to the helpers package as well |
||
pub balance: Uint128, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] | ||
#[serde(rename_all = "snake_case")] | ||
pub struct GetRecipientTxsResponse { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
storage/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There must be an instruction how did you get this prefix, it's not obvious
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bold of you to assume I remember how I did that.Okay, I will write some extensive docs on how to examine contract storage paths.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have added some docs, please check them out