From 9de76247f99cf7b1e52face36df3e19239b9be14 Mon Sep 17 00:00:00 2001 From: larry <26318510+larry0x@users.noreply.github.com> Date: Tue, 12 Jul 2022 16:20:50 +0100 Subject: [PATCH 01/25] add bank supply query --- packages/std/src/mock.rs | 63 +++++++++++++++++++++++++++++++--- packages/std/src/query/bank.rs | 11 ++++++ packages/std/src/query/mod.rs | 2 +- 3 files changed, 71 insertions(+), 5 deletions(-) diff --git a/packages/std/src/mock.rs b/packages/std/src/mock.rs index 3b0db5c1c6..0ab1235c45 100644 --- a/packages/std/src/mock.rs +++ b/packages/std/src/mock.rs @@ -15,8 +15,10 @@ use crate::ibc::{ IbcEndpoint, IbcOrder, IbcPacket, IbcPacketAckMsg, IbcPacketReceiveMsg, IbcPacketTimeoutMsg, IbcTimeoutBlock, }; +use crate::math::Uint128; use crate::query::{ - AllBalanceResponse, BalanceResponse, BankQuery, CustomQuery, QueryRequest, WasmQuery, + AllBalanceResponse, BalanceResponse, BankQuery, CustomQuery, QueryRequest, SupplyResponse, + WasmQuery, }; #[cfg(feature = "staking")] use crate::query::{ @@ -564,20 +566,44 @@ impl Default for WasmQuerier { #[derive(Clone, Default)] pub struct BankQuerier { + supplies: HashMap, balances: HashMap>, } impl BankQuerier { pub fn new(balances: &[(&str, &[Coin])]) -> Self { - let mut map = HashMap::new(); + let mut supplies_map = HashMap::new(); + let mut balances_map = HashMap::new(); for (addr, coins) in balances.iter() { - map.insert(addr.to_string(), coins.to_vec()); + balances_map.insert(addr.to_string(), coins.to_vec()); + for coin in coins.iter() { + *supplies_map + .entry(coin.denom.clone()) + .or_insert_with(Uint128::zero) += coin.amount; + } + } + BankQuerier { + supplies: supplies_map, + balances: balances_map, } - BankQuerier { balances: map } } pub fn query(&self, request: &BankQuery) -> QuerierResult { let contract_result: ContractResult = match request { + BankQuery::Supply { denom } => { + let amount = self + .supplies + .get(denom) + .cloned() + .unwrap_or_else(Uint128::zero); + let bank_res = SupplyResponse { + amount: Coin { + amount, + denom: denom.to_string(), + }, + }; + to_binary(&bank_res).into() + } BankQuery::Balance { address, denom } => { // proper error on not found, serialize result on found let amount = self @@ -1060,6 +1086,35 @@ mod tests { assert_eq!(res.unwrap_err(), VerificationError::InvalidPubkeyFormat); } + #[test] + fn bank_querier_supply() { + let addr1 = String::from("foo"); + let balance1 = vec![coin(123, "ELF"), coin(777, "FLY")]; + + let addr2 = String::from("bar"); + let balance2 = coins(321, "ELF"); + + let bank = BankQuerier::new(&[(&addr1, &balance1), (&addr2, &balance2)]); + + let elf = bank + .query(&BankQuery::Supply { + denom: "ELF".to_string(), + }) + .unwrap() + .unwrap(); + let res: SupplyResponse = from_binary(&elf).unwrap(); + assert_eq!(res.amount, coin(444, "ELF")); + + let fly = bank + .query(&BankQuery::Supply { + denom: "FLY".to_string(), + }) + .unwrap() + .unwrap(); + let res: SupplyResponse = from_binary(&fly).unwrap(); + assert_eq!(res.amount, coin(777, "FLY")); + } + #[test] fn bank_querier_all_balances() { let addr = String::from("foobar"); diff --git a/packages/std/src/query/bank.rs b/packages/std/src/query/bank.rs index 08824323b8..0315571248 100644 --- a/packages/std/src/query/bank.rs +++ b/packages/std/src/query/bank.rs @@ -7,6 +7,9 @@ use crate::Coin; #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] #[serde(rename_all = "snake_case")] pub enum BankQuery { + /// This calls into the native bank module for querying the total supply on one denomination. + /// Return value is SupplyResponse + Supply { denom: String }, /// This calls into the native bank module for one denomination /// Return value is BalanceResponse Balance { address: String, denom: String }, @@ -16,6 +19,14 @@ pub enum BankQuery { AllBalances { address: String }, } +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] +#[serde(rename_all = "snake_case")] +pub struct SupplyResponse { + /// Always returns a Coin with the requested denom. + /// This may be of 0 amount if no such funds. + pub amount: Coin, +} + #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] #[serde(rename_all = "snake_case")] pub struct BalanceResponse { diff --git a/packages/std/src/query/mod.rs b/packages/std/src/query/mod.rs index 0fb490dc5e..d56af40061 100644 --- a/packages/std/src/query/mod.rs +++ b/packages/std/src/query/mod.rs @@ -10,7 +10,7 @@ mod ibc; mod staking; mod wasm; -pub use bank::{AllBalanceResponse, BalanceResponse, BankQuery}; +pub use bank::{AllBalanceResponse, BalanceResponse, BankQuery, SupplyResponse}; #[cfg(feature = "stargate")] pub use ibc::{ChannelResponse, IbcQuery, ListChannelsResponse, PortIdResponse}; #[cfg(feature = "staking")] From eb66d7a2eae258772e1af8db77cd28f41693aec5 Mon Sep 17 00:00:00 2001 From: larry <26318510+larry0x@users.noreply.github.com> Date: Tue, 12 Jul 2022 18:14:13 +0100 Subject: [PATCH 02/25] export `SupplyResponse` --- packages/std/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/std/src/lib.rs b/packages/std/src/lib.rs index b3072e677b..cd08395450 100644 --- a/packages/std/src/lib.rs +++ b/packages/std/src/lib.rs @@ -48,7 +48,7 @@ pub use crate::math::{ }; pub use crate::query::{ AllBalanceResponse, BalanceResponse, BankQuery, ContractInfoResponse, CustomQuery, - QueryRequest, WasmQuery, + QueryRequest, SupplyResponse, WasmQuery, }; #[cfg(feature = "staking")] pub use crate::query::{ From de275e82a597e162b68501d58730941882626ccb Mon Sep 17 00:00:00 2001 From: larry <26318510+larry0x@users.noreply.github.com> Date: Tue, 12 Jul 2022 18:17:25 +0100 Subject: [PATCH 03/25] add a helper function for querying supply --- packages/std/src/traits.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/std/src/traits.rs b/packages/std/src/traits.rs index e99ef08d81..057b968943 100644 --- a/packages/std/src/traits.rs +++ b/packages/std/src/traits.rs @@ -18,6 +18,7 @@ use crate::query::{ }; use crate::results::{ContractResult, Empty, SystemResult}; use crate::serde::{from_binary, to_binary, to_vec}; +use crate::SupplyResponse; /// Storage provides read and write access to a persistent storage. /// If you only want to provide read access, provide `&Storage` @@ -197,6 +198,15 @@ impl<'a, C: CustomQuery> QuerierWrapper<'a, C> { } } + pub fn query_supply(&self, denom: impl Into) -> StdResult { + let request = BankQuery::Supply { + denom: denom.into(), + } + .into(); + let res: SupplyResponse = self.query(&request)?; + Ok(res.amount) + } + pub fn query_balance( &self, address: impl Into, From ddd4e3c6b4d6a11023ac98e1d1a25a853a6ee09f Mon Sep 17 00:00:00 2001 From: larry <26318510+larry0x@users.noreply.github.com> Date: Tue, 12 Jul 2022 19:35:13 +0100 Subject: [PATCH 04/25] update schema --- contracts/reflect/schema/query_msg.json | 21 +++++++++++++++++++++ packages/std/schema/query_request.json | 21 +++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/contracts/reflect/schema/query_msg.json b/contracts/reflect/schema/query_msg.json index 8de592c95f..63eb7d4058 100644 --- a/contracts/reflect/schema/query_msg.json +++ b/contracts/reflect/schema/query_msg.json @@ -108,6 +108,27 @@ "definitions": { "BankQuery": { "oneOf": [ + { + "description": "This calls into the native bank module for querying the total supply on one denomination. Return value is SupplyResponse", + "type": "object", + "required": [ + "supply" + ], + "properties": { + "supply": { + "type": "object", + "required": [ + "denom" + ], + "properties": { + "denom": { + "type": "string" + } + } + } + }, + "additionalProperties": false + }, { "description": "This calls into the native bank module for one denomination Return value is BalanceResponse", "type": "object", diff --git a/packages/std/schema/query_request.json b/packages/std/schema/query_request.json index 6417cdaa51..c1500291b0 100644 --- a/packages/std/schema/query_request.json +++ b/packages/std/schema/query_request.json @@ -42,6 +42,27 @@ "definitions": { "BankQuery": { "oneOf": [ + { + "description": "This calls into the native bank module for querying the total supply on one denomination. Return value is SupplyResponse", + "type": "object", + "required": [ + "supply" + ], + "properties": { + "supply": { + "type": "object", + "required": [ + "denom" + ], + "properties": { + "denom": { + "type": "string" + } + } + } + }, + "additionalProperties": false + }, { "description": "This calls into the native bank module for one denomination Return value is BalanceResponse", "type": "object", From 787cef005a0d7177f1010fdad2340eee75c652db Mon Sep 17 00:00:00 2001 From: larry <26318510+larry0x@users.noreply.github.com> Date: Tue, 12 Jul 2022 19:37:44 +0100 Subject: [PATCH 05/25] update import --- packages/std/src/traits.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/std/src/traits.rs b/packages/std/src/traits.rs index 057b968943..b0656f6eb1 100644 --- a/packages/std/src/traits.rs +++ b/packages/std/src/traits.rs @@ -14,11 +14,10 @@ use crate::query::{ #[cfg(feature = "staking")] use crate::query::{ AllDelegationsResponse, AllValidatorsResponse, BondedDenomResponse, Delegation, - DelegationResponse, FullDelegation, StakingQuery, Validator, ValidatorResponse, + DelegationResponse, FullDelegation, StakingQuery, SupplyResponse, Validator, ValidatorResponse, }; use crate::results::{ContractResult, Empty, SystemResult}; use crate::serde::{from_binary, to_binary, to_vec}; -use crate::SupplyResponse; /// Storage provides read and write access to a persistent storage. /// If you only want to provide read access, provide `&Storage` From ce2577b6b9d07d3f951da5b0f87d220447e523ba Mon Sep 17 00:00:00 2001 From: larry <26318510+larry0x@users.noreply.github.com> Date: Tue, 12 Jul 2022 19:45:36 +0100 Subject: [PATCH 06/25] fix import --- packages/std/src/traits.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/std/src/traits.rs b/packages/std/src/traits.rs index b0656f6eb1..e87fc2ef6f 100644 --- a/packages/std/src/traits.rs +++ b/packages/std/src/traits.rs @@ -9,12 +9,13 @@ use crate::errors::{RecoverPubkeyError, StdError, StdResult, VerificationError}; #[cfg(feature = "iterator")] use crate::iterator::{Order, Record}; use crate::query::{ - AllBalanceResponse, BalanceResponse, BankQuery, CustomQuery, QueryRequest, WasmQuery, + AllBalanceResponse, BalanceResponse, BankQuery, CustomQuery, QueryRequest, SupplyResponse, + WasmQuery, }; #[cfg(feature = "staking")] use crate::query::{ AllDelegationsResponse, AllValidatorsResponse, BondedDenomResponse, Delegation, - DelegationResponse, FullDelegation, StakingQuery, SupplyResponse, Validator, ValidatorResponse, + DelegationResponse, FullDelegation, StakingQuery, Validator, ValidatorResponse, }; use crate::results::{ContractResult, Empty, SystemResult}; use crate::serde::{from_binary, to_binary, to_vec}; From c1e0b78f1c277593a43ad558fc4a87e6c7c2e0db Mon Sep 17 00:00:00 2001 From: larry <26318510+larry0x@users.noreply.github.com> Date: Thu, 14 Jul 2022 00:29:19 +0100 Subject: [PATCH 07/25] add unit test for bank query helpers --- Cargo.lock | 196 ++++++++++++++++++++++++++- Cargo.toml | 4 +- contracts/bank-query/Cargo.toml | 17 +++ contracts/bank-query/src/contract.rs | 48 +++++++ contracts/bank-query/src/lib.rs | 2 + contracts/bank-query/src/msg.rs | 17 +++ packages/std/src/traits.rs | 20 ++- 7 files changed, 297 insertions(+), 7 deletions(-) create mode 100644 contracts/bank-query/Cargo.toml create mode 100644 contracts/bank-query/src/contract.rs create mode 100644 contracts/bank-query/src/lib.rs create mode 100644 contracts/bank-query/src/msg.rs diff --git a/Cargo.lock b/Cargo.lock index ef3d6b1290..2d356f8c9d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -87,6 +87,15 @@ dependencies = [ "rustc-demangle", ] +[[package]] +name = "bank-query" +version = "0.0.0" +dependencies = [ + "cosmwasm-std", + "schemars", + "serde", +] + [[package]] name = "base16ct" version = "0.1.1" @@ -128,9 +137,16 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4" dependencies = [ + "block-padding", "generic-array", ] +[[package]] +name = "block-padding" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d696c370c750c948ada61c69a0ee2cbbb9c50b1019ddb86d9317157a99c2cae" + [[package]] name = "bstr" version = "0.2.17" @@ -149,6 +165,17 @@ version = "3.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a4a45a46ab1f2412e53d3a0ade76ffad2025804294569aae387231a0cd6e0899" +[[package]] +name = "burner" +version = "0.0.0" +dependencies = [ + "cosmwasm-schema", + "cosmwasm-std", + "cosmwasm-vm", + "schemars", + "serde", +] + [[package]] name = "bytecheck" version = "0.6.8" @@ -176,6 +203,12 @@ version = "1.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" +[[package]] +name = "bytes" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4872d67bab6358e59559027aa3b9157c53d9358c51423c17554809a8858e0f8" + [[package]] name = "cast" version = "0.2.7" @@ -545,6 +578,23 @@ dependencies = [ "subtle", ] +[[package]] +name = "crypto-verify" +version = "0.0.0" +dependencies = [ + "cosmwasm-schema", + "cosmwasm-std", + "cosmwasm-storage", + "cosmwasm-vm", + "hex", + "hex-literal", + "rlp", + "schemars", + "serde", + "sha2", + "sha3", +] + [[package]] name = "csv" version = "1.1.6" @@ -632,6 +682,12 @@ dependencies = [ "generic-array", ] +[[package]] +name = "doc-comment" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" + [[package]] name = "dyn-clone" version = "1.0.5" @@ -787,6 +843,20 @@ dependencies = [ "subtle", ] +[[package]] +name = "floaty" +version = "0.0.0" +dependencies = [ + "cosmwasm-schema", + "cosmwasm-std", + "cosmwasm-storage", + "cosmwasm-vm", + "schemars", + "serde", + "sha2", + "thiserror", +] + [[package]] name = "fnv" version = "1.0.7" @@ -865,6 +935,8 @@ version = "0.0.0" dependencies = [ "cosmwasm-schema", "cosmwasm-std", + "cosmwasm-storage", + "cosmwasm-vm", "rust-argon2", "schemars", "serde", @@ -936,6 +1008,30 @@ dependencies = [ "digest", ] +[[package]] +name = "ibc-reflect" +version = "0.0.0" +dependencies = [ + "cosmwasm-schema", + "cosmwasm-std", + "cosmwasm-storage", + "cosmwasm-vm", + "schemars", + "serde", +] + +[[package]] +name = "ibc-reflect-send" +version = "0.0.0" +dependencies = [ + "cosmwasm-schema", + "cosmwasm-std", + "cosmwasm-storage", + "cosmwasm-vm", + "schemars", + "serde", +] + [[package]] name = "id-arena" version = "2.2.1" @@ -1011,6 +1107,12 @@ dependencies = [ "sha2", ] +[[package]] +name = "keccak" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9b7d56ba4a8344d6be9729995e6b06f928af29998cdf79fe390cbf6b1fee838" + [[package]] name = "lazy_static" version = "1.4.0" @@ -1286,6 +1388,17 @@ dependencies = [ "syn", ] +[[package]] +name = "queue" +version = "0.0.0" +dependencies = [ + "cosmwasm-schema", + "cosmwasm-std", + "cosmwasm-vm", + "schemars", + "serde", +] + [[package]] name = "quote" version = "1.0.18" @@ -1367,6 +1480,19 @@ dependencies = [ "bitflags", ] +[[package]] +name = "reflect" +version = "0.0.0" +dependencies = [ + "cosmwasm-schema", + "cosmwasm-std", + "cosmwasm-storage", + "cosmwasm-vm", + "schemars", + "serde", + "thiserror", +] + [[package]] name = "regalloc" version = "0.0.31" @@ -1465,6 +1591,16 @@ dependencies = [ "syn", ] +[[package]] +name = "rlp" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "999508abb0ae792aabed2460c45b89106d97fe4adac593bdaef433c2605847b5" +dependencies = [ + "bytes", + "rustc-hex", +] + [[package]] name = "rust-argon2" version = "0.8.3" @@ -1489,6 +1625,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" +[[package]] +name = "rustc-hex" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e75f6a532d0fd9f7f13144f392b6ad56a32696bfcd9c78f797f16bbb6f072d6" + [[package]] name = "rustc_version" version = "0.4.0" @@ -1576,9 +1718,9 @@ checksum = "8cb243bdfdb5936c8dc3c45762a19d12ab4550cdc753bc247637d4ec35a040fd" [[package]] name = "serde" -version = "1.0.137" +version = "1.0.103" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61ea8d54c77f8315140a05f4c7237403bf38b72704d031543aa1d16abbf517d1" +checksum = "1217f97ab8e8904b57dd22eb61cde455fa7446a9c1cf43966066da047c1f3702" dependencies = [ "serde_derive", ] @@ -1613,9 +1755,9 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.137" +version = "1.0.103" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1f26faba0c3959972377d3b2d306ee9f71faee9714294e41bb777f83f88578be" +checksum = "a8c6faef9a2e64b0064f48570289b4bf8823b7581f1d6157c1b52152306651d0" dependencies = [ "proc-macro2", "quote", @@ -1657,6 +1799,18 @@ dependencies = [ "opaque-debug", ] +[[package]] +name = "sha3" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f81199417d4e5de3f04b1e871023acea7389672c4135918f05aa9cbf2f2fa809" +dependencies = [ + "block-buffer", + "digest", + "keccak", + "opaque-debug", +] + [[package]] name = "signature" version = "1.4.0" @@ -1673,6 +1827,27 @@ version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f2dd574626839106c320a323308629dcb1acfc96e32a8cba364ddc61ac23ee83" +[[package]] +name = "snafu" +version = "0.6.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eab12d3c261b2308b0d80c26fffb58d17eba81a4be97890101f416b478c79ca7" +dependencies = [ + "doc-comment", + "snafu-derive", +] + +[[package]] +name = "snafu-derive" +version = "0.6.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1508efa03c362e23817f96cde18abed596a25219a8b2c66e8db33c03543d315b" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "spki" version = "0.5.4" @@ -1689,6 +1864,19 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" +[[package]] +name = "staking" +version = "0.0.0" +dependencies = [ + "cosmwasm-schema", + "cosmwasm-std", + "cosmwasm-storage", + "cosmwasm-vm", + "schemars", + "serde", + "snafu", +] + [[package]] name = "static_assertions" version = "1.1.0" diff --git a/Cargo.toml b/Cargo.toml index 50b55c542c..4fe03c247b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,3 +1,3 @@ [workspace] -members = ["packages/*"] -exclude = ["contracts"] +members = ["packages/*", "contracts/*"] +# exclude = ["contracts"] diff --git a/contracts/bank-query/Cargo.toml b/contracts/bank-query/Cargo.toml new file mode 100644 index 0000000000..fdad06119a --- /dev/null +++ b/contracts/bank-query/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "bank-query" +version = "0.0.0" +authors = ["larry "] +edition = "2021" +publish = false +license = "Apache-2.0" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[lib] +crate-type = ["cdylib", "rlib"] + +[dependencies] +cosmwasm-std = { path = "../../packages/std", features = ["iterator"] } +schemars = "0.8.1" +serde = { version = "1.0.103", default-features = false, features = ["derive"] } diff --git a/contracts/bank-query/src/contract.rs b/contracts/bank-query/src/contract.rs new file mode 100644 index 0000000000..f4d350f1e6 --- /dev/null +++ b/contracts/bank-query/src/contract.rs @@ -0,0 +1,48 @@ +use cosmwasm_std::{ + entry_point, to_binary, AllBalanceResponse, BalanceResponse, BankQuery, Binary, Deps, DepsMut, + Empty, Env, MessageInfo, QueryRequest, Response, StdError, StdResult, SupplyResponse, +}; + +use crate::msg::QueryMsg; + +#[entry_point] +pub fn instantiate( + _deps: DepsMut, + _env: Env, + _info: MessageInfo, + _msg: Empty, +) -> StdResult { + Ok(Response::new()) +} + +#[entry_point] +pub fn execute(_deps: DepsMut, _env: Env, _info: MessageInfo, _msg: Empty) -> StdResult { + Err(StdError::generic_err("unimplemented")) +} + +#[entry_point] +pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult { + match msg { + QueryMsg::Supply { denom } => { + let res: SupplyResponse = deps + .querier + .query(&QueryRequest::Bank(BankQuery::Supply { denom }))?; + + to_binary(&res) + } + QueryMsg::Balance { address, denom } => { + let res: BalanceResponse = deps + .querier + .query(&QueryRequest::Bank(BankQuery::Balance { address, denom }))?; + + to_binary(&res) + } + QueryMsg::AllBalances { address } => { + let res: AllBalanceResponse = deps + .querier + .query(&QueryRequest::Bank(BankQuery::AllBalances { address }))?; + + to_binary(&res) + } + } +} diff --git a/contracts/bank-query/src/lib.rs b/contracts/bank-query/src/lib.rs new file mode 100644 index 0000000000..112ecadc84 --- /dev/null +++ b/contracts/bank-query/src/lib.rs @@ -0,0 +1,2 @@ +pub mod contract; +pub mod msg; diff --git a/contracts/bank-query/src/msg.rs b/contracts/bank-query/src/msg.rs new file mode 100644 index 0000000000..3c8e05a0d2 --- /dev/null +++ b/contracts/bank-query/src/msg.rs @@ -0,0 +1,17 @@ +use schemars::JsonSchema; +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] +#[serde(rename_all = "snake_case")] +pub enum QueryMsg { + Supply { + denom: String + }, + Balance { + address: String, + denom: String, + }, + AllBalances { + address: String, + }, +} diff --git a/packages/std/src/traits.rs b/packages/std/src/traits.rs index e87fc2ef6f..a870683547 100644 --- a/packages/std/src/traits.rs +++ b/packages/std/src/traits.rs @@ -342,7 +342,7 @@ impl<'a, C: CustomQuery> QuerierWrapper<'a, C> { mod tests { use super::*; use crate::mock::MockQuerier; - use crate::{coins, from_slice, Uint128}; + use crate::{coin, coins, from_slice, Uint128}; // this is a simple demo helper to prove we can use it fn demo_helper(_querier: &dyn Querier) -> u64 { @@ -381,4 +381,22 @@ mod tests { let balance: BalanceResponse = from_slice(&raw).unwrap(); assert_eq!(balance.amount.amount, Uint128::new(5)); } + + #[test] + fn bank_query_helpers_work() { + let querier: MockQuerier = MockQuerier::new(&[ + ("foo", &[coin(123, "ELF"), coin(777, "FLY")]), + ("bar", &[coin(321, "ELF")]), + ]); + let wrapper = QuerierWrapper::::new(&querier); + + let supply = wrapper.query_supply("ELF").unwrap(); + assert_eq!(supply, coin(444, "ELF")); + + let balance = wrapper.query_balance("foo", "ELF").unwrap(); + assert_eq!(balance, coin(123, "ELF")); + + let all_balances = wrapper.query_all_balances("foo").unwrap(); + assert_eq!(all_balances, vec![coin(123, "ELF"), coin(777, "FLY")]); + } } From 02933970674d8abc3cd7835a4469c52d07457e10 Mon Sep 17 00:00:00 2001 From: larry <26318510+larry0x@users.noreply.github.com> Date: Thu, 14 Jul 2022 00:31:14 +0100 Subject: [PATCH 08/25] add an entry to changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 70c570d7cc..32921e40af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,8 @@ and this project adheres to - cosmwasm-std: Implement `ceil`/`floor` for `Decimal`/`Decimal256`. - cosmwasm-std: Implement `saturating_add`/`sub`/`mul` for `Decimal`/`Decimal256`. +- cosmwasm-std: Implement `BankQuery::Supply` to allow querying the total supply of + a native token [#1334]: https://github.com/CosmWasm/cosmwasm/pull/1334 From ecb68ea07dce7340819f79ba2bb16fb8fd6e43e5 Mon Sep 17 00:00:00 2001 From: larry <26318510+larry0x@users.noreply.github.com> Date: Thu, 14 Jul 2022 00:31:59 +0100 Subject: [PATCH 09/25] Update packages/std/src/query/bank.rs Co-authored-by: Simon Warta <2603011+webmaster128@users.noreply.github.com> --- packages/std/src/query/bank.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/std/src/query/bank.rs b/packages/std/src/query/bank.rs index 0315571248..07ab9b8d7e 100644 --- a/packages/std/src/query/bank.rs +++ b/packages/std/src/query/bank.rs @@ -7,8 +7,9 @@ use crate::Coin; #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] #[serde(rename_all = "snake_case")] pub enum BankQuery { - /// This calls into the native bank module for querying the total supply on one denomination. - /// Return value is SupplyResponse + /// This calls into the native bank module for querying the total supply of one denomination. + /// It does the same as the SupplyOf call in Cosmos SDK's RPC API. + /// Return value is of type SupplyResponse. Supply { denom: String }, /// This calls into the native bank module for one denomination /// Return value is BalanceResponse From 11cd9a56ca723be78858dac2b65c420e1c1e6a13 Mon Sep 17 00:00:00 2001 From: larry <26318510+larry0x@users.noreply.github.com> Date: Thu, 14 Jul 2022 00:35:04 +0100 Subject: [PATCH 10/25] clarify in comment the case if a denom doesn not exist --- packages/std/src/query/bank.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/std/src/query/bank.rs b/packages/std/src/query/bank.rs index 07ab9b8d7e..bb612ceb2c 100644 --- a/packages/std/src/query/bank.rs +++ b/packages/std/src/query/bank.rs @@ -24,7 +24,7 @@ pub enum BankQuery { #[serde(rename_all = "snake_case")] pub struct SupplyResponse { /// Always returns a Coin with the requested denom. - /// This may be of 0 amount if no such funds. + /// This will be of zero amount if the denom does not exist. pub amount: Coin, } From 7d4785345657160b634590ffd18004176215c13b Mon Sep 17 00:00:00 2001 From: larry <26318510+larry0x@users.noreply.github.com> Date: Thu, 14 Jul 2022 00:37:33 +0100 Subject: [PATCH 11/25] add a unit test for querying the supply of a non-existing denom --- packages/std/src/mock.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/std/src/mock.rs b/packages/std/src/mock.rs index 0ab1235c45..c9c8ff6956 100644 --- a/packages/std/src/mock.rs +++ b/packages/std/src/mock.rs @@ -1113,6 +1113,16 @@ mod tests { .unwrap(); let res: SupplyResponse = from_binary(&fly).unwrap(); assert_eq!(res.amount, coin(777, "FLY")); + + // if a denom does not exist, should return zero amount, instead of throwing an error + let atom = bank + .query(&BankQuery::Supply { + denom: "ATOM".to_string(), + }) + .unwrap() + .unwrap(); + let res: SupplyResponse = from_binary(&atom).unwrap(); + assert_eq!(res.amount, coin(0, "ATOM")); } #[test] From d97d68b6f6ef846f8c88ec4ee1dede8174880af3 Mon Sep 17 00:00:00 2001 From: larry <26318510+larry0x@users.noreply.github.com> Date: Thu, 14 Jul 2022 00:40:36 +0100 Subject: [PATCH 12/25] revert changes made to `Cargo.toml` (not supposed to be pushed) --- Cargo.lock | 188 ----------------------------------------------------- Cargo.toml | 4 +- 2 files changed, 2 insertions(+), 190 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2d356f8c9d..6d6316fa41 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -87,15 +87,6 @@ dependencies = [ "rustc-demangle", ] -[[package]] -name = "bank-query" -version = "0.0.0" -dependencies = [ - "cosmwasm-std", - "schemars", - "serde", -] - [[package]] name = "base16ct" version = "0.1.1" @@ -137,16 +128,9 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4" dependencies = [ - "block-padding", "generic-array", ] -[[package]] -name = "block-padding" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d696c370c750c948ada61c69a0ee2cbbb9c50b1019ddb86d9317157a99c2cae" - [[package]] name = "bstr" version = "0.2.17" @@ -165,17 +149,6 @@ version = "3.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a4a45a46ab1f2412e53d3a0ade76ffad2025804294569aae387231a0cd6e0899" -[[package]] -name = "burner" -version = "0.0.0" -dependencies = [ - "cosmwasm-schema", - "cosmwasm-std", - "cosmwasm-vm", - "schemars", - "serde", -] - [[package]] name = "bytecheck" version = "0.6.8" @@ -203,12 +176,6 @@ version = "1.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" -[[package]] -name = "bytes" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c4872d67bab6358e59559027aa3b9157c53d9358c51423c17554809a8858e0f8" - [[package]] name = "cast" version = "0.2.7" @@ -578,23 +545,6 @@ dependencies = [ "subtle", ] -[[package]] -name = "crypto-verify" -version = "0.0.0" -dependencies = [ - "cosmwasm-schema", - "cosmwasm-std", - "cosmwasm-storage", - "cosmwasm-vm", - "hex", - "hex-literal", - "rlp", - "schemars", - "serde", - "sha2", - "sha3", -] - [[package]] name = "csv" version = "1.1.6" @@ -682,12 +632,6 @@ dependencies = [ "generic-array", ] -[[package]] -name = "doc-comment" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" - [[package]] name = "dyn-clone" version = "1.0.5" @@ -843,20 +787,6 @@ dependencies = [ "subtle", ] -[[package]] -name = "floaty" -version = "0.0.0" -dependencies = [ - "cosmwasm-schema", - "cosmwasm-std", - "cosmwasm-storage", - "cosmwasm-vm", - "schemars", - "serde", - "sha2", - "thiserror", -] - [[package]] name = "fnv" version = "1.0.7" @@ -935,8 +865,6 @@ version = "0.0.0" dependencies = [ "cosmwasm-schema", "cosmwasm-std", - "cosmwasm-storage", - "cosmwasm-vm", "rust-argon2", "schemars", "serde", @@ -1008,30 +936,6 @@ dependencies = [ "digest", ] -[[package]] -name = "ibc-reflect" -version = "0.0.0" -dependencies = [ - "cosmwasm-schema", - "cosmwasm-std", - "cosmwasm-storage", - "cosmwasm-vm", - "schemars", - "serde", -] - -[[package]] -name = "ibc-reflect-send" -version = "0.0.0" -dependencies = [ - "cosmwasm-schema", - "cosmwasm-std", - "cosmwasm-storage", - "cosmwasm-vm", - "schemars", - "serde", -] - [[package]] name = "id-arena" version = "2.2.1" @@ -1107,12 +1011,6 @@ dependencies = [ "sha2", ] -[[package]] -name = "keccak" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f9b7d56ba4a8344d6be9729995e6b06f928af29998cdf79fe390cbf6b1fee838" - [[package]] name = "lazy_static" version = "1.4.0" @@ -1388,17 +1286,6 @@ dependencies = [ "syn", ] -[[package]] -name = "queue" -version = "0.0.0" -dependencies = [ - "cosmwasm-schema", - "cosmwasm-std", - "cosmwasm-vm", - "schemars", - "serde", -] - [[package]] name = "quote" version = "1.0.18" @@ -1480,19 +1367,6 @@ dependencies = [ "bitflags", ] -[[package]] -name = "reflect" -version = "0.0.0" -dependencies = [ - "cosmwasm-schema", - "cosmwasm-std", - "cosmwasm-storage", - "cosmwasm-vm", - "schemars", - "serde", - "thiserror", -] - [[package]] name = "regalloc" version = "0.0.31" @@ -1591,16 +1465,6 @@ dependencies = [ "syn", ] -[[package]] -name = "rlp" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "999508abb0ae792aabed2460c45b89106d97fe4adac593bdaef433c2605847b5" -dependencies = [ - "bytes", - "rustc-hex", -] - [[package]] name = "rust-argon2" version = "0.8.3" @@ -1625,12 +1489,6 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" -[[package]] -name = "rustc-hex" -version = "2.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e75f6a532d0fd9f7f13144f392b6ad56a32696bfcd9c78f797f16bbb6f072d6" - [[package]] name = "rustc_version" version = "0.4.0" @@ -1799,18 +1657,6 @@ dependencies = [ "opaque-debug", ] -[[package]] -name = "sha3" -version = "0.9.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f81199417d4e5de3f04b1e871023acea7389672c4135918f05aa9cbf2f2fa809" -dependencies = [ - "block-buffer", - "digest", - "keccak", - "opaque-debug", -] - [[package]] name = "signature" version = "1.4.0" @@ -1827,27 +1673,6 @@ version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f2dd574626839106c320a323308629dcb1acfc96e32a8cba364ddc61ac23ee83" -[[package]] -name = "snafu" -version = "0.6.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eab12d3c261b2308b0d80c26fffb58d17eba81a4be97890101f416b478c79ca7" -dependencies = [ - "doc-comment", - "snafu-derive", -] - -[[package]] -name = "snafu-derive" -version = "0.6.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1508efa03c362e23817f96cde18abed596a25219a8b2c66e8db33c03543d315b" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "spki" version = "0.5.4" @@ -1864,19 +1689,6 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" -[[package]] -name = "staking" -version = "0.0.0" -dependencies = [ - "cosmwasm-schema", - "cosmwasm-std", - "cosmwasm-storage", - "cosmwasm-vm", - "schemars", - "serde", - "snafu", -] - [[package]] name = "static_assertions" version = "1.1.0" diff --git a/Cargo.toml b/Cargo.toml index 4fe03c247b..50b55c542c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,3 +1,3 @@ [workspace] -members = ["packages/*", "contracts/*"] -# exclude = ["contracts"] +members = ["packages/*"] +exclude = ["contracts"] From f15a25f72ba6a18364e254ddf09e0800834418f5 Mon Sep 17 00:00:00 2001 From: larry <26318510+larry0x@users.noreply.github.com> Date: Thu, 14 Jul 2022 00:43:12 +0100 Subject: [PATCH 13/25] remove a contract unintentionally pushed (only used for local testing) --- contracts/bank-query/Cargo.toml | 17 ---------- contracts/bank-query/src/contract.rs | 48 ---------------------------- contracts/bank-query/src/lib.rs | 2 -- contracts/bank-query/src/msg.rs | 17 ---------- 4 files changed, 84 deletions(-) delete mode 100644 contracts/bank-query/Cargo.toml delete mode 100644 contracts/bank-query/src/contract.rs delete mode 100644 contracts/bank-query/src/lib.rs delete mode 100644 contracts/bank-query/src/msg.rs diff --git a/contracts/bank-query/Cargo.toml b/contracts/bank-query/Cargo.toml deleted file mode 100644 index fdad06119a..0000000000 --- a/contracts/bank-query/Cargo.toml +++ /dev/null @@ -1,17 +0,0 @@ -[package] -name = "bank-query" -version = "0.0.0" -authors = ["larry "] -edition = "2021" -publish = false -license = "Apache-2.0" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[lib] -crate-type = ["cdylib", "rlib"] - -[dependencies] -cosmwasm-std = { path = "../../packages/std", features = ["iterator"] } -schemars = "0.8.1" -serde = { version = "1.0.103", default-features = false, features = ["derive"] } diff --git a/contracts/bank-query/src/contract.rs b/contracts/bank-query/src/contract.rs deleted file mode 100644 index f4d350f1e6..0000000000 --- a/contracts/bank-query/src/contract.rs +++ /dev/null @@ -1,48 +0,0 @@ -use cosmwasm_std::{ - entry_point, to_binary, AllBalanceResponse, BalanceResponse, BankQuery, Binary, Deps, DepsMut, - Empty, Env, MessageInfo, QueryRequest, Response, StdError, StdResult, SupplyResponse, -}; - -use crate::msg::QueryMsg; - -#[entry_point] -pub fn instantiate( - _deps: DepsMut, - _env: Env, - _info: MessageInfo, - _msg: Empty, -) -> StdResult { - Ok(Response::new()) -} - -#[entry_point] -pub fn execute(_deps: DepsMut, _env: Env, _info: MessageInfo, _msg: Empty) -> StdResult { - Err(StdError::generic_err("unimplemented")) -} - -#[entry_point] -pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult { - match msg { - QueryMsg::Supply { denom } => { - let res: SupplyResponse = deps - .querier - .query(&QueryRequest::Bank(BankQuery::Supply { denom }))?; - - to_binary(&res) - } - QueryMsg::Balance { address, denom } => { - let res: BalanceResponse = deps - .querier - .query(&QueryRequest::Bank(BankQuery::Balance { address, denom }))?; - - to_binary(&res) - } - QueryMsg::AllBalances { address } => { - let res: AllBalanceResponse = deps - .querier - .query(&QueryRequest::Bank(BankQuery::AllBalances { address }))?; - - to_binary(&res) - } - } -} diff --git a/contracts/bank-query/src/lib.rs b/contracts/bank-query/src/lib.rs deleted file mode 100644 index 112ecadc84..0000000000 --- a/contracts/bank-query/src/lib.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub mod contract; -pub mod msg; diff --git a/contracts/bank-query/src/msg.rs b/contracts/bank-query/src/msg.rs deleted file mode 100644 index 3c8e05a0d2..0000000000 --- a/contracts/bank-query/src/msg.rs +++ /dev/null @@ -1,17 +0,0 @@ -use schemars::JsonSchema; -use serde::{Deserialize, Serialize}; - -#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] -#[serde(rename_all = "snake_case")] -pub enum QueryMsg { - Supply { - denom: String - }, - Balance { - address: String, - denom: String, - }, - AllBalances { - address: String, - }, -} From 21e505c65d2127eac8826fcbbdad2662b2661dc0 Mon Sep 17 00:00:00 2001 From: larry <26318510+larry0x@users.noreply.github.com> Date: Thu, 14 Jul 2022 00:45:49 +0100 Subject: [PATCH 14/25] format changelog --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 32921e40af..500c206bc9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,8 +19,8 @@ and this project adheres to - cosmwasm-std: Implement `ceil`/`floor` for `Decimal`/`Decimal256`. - cosmwasm-std: Implement `saturating_add`/`sub`/`mul` for `Decimal`/`Decimal256`. -- cosmwasm-std: Implement `BankQuery::Supply` to allow querying the total supply of - a native token +- cosmwasm-std: Implement `BankQuery::Supply` to allow querying the total supply + of a native token [#1334]: https://github.com/CosmWasm/cosmwasm/pull/1334 From 032c34cfd9741751e30b141163e14a7eb2f0c8c6 Mon Sep 17 00:00:00 2001 From: larry <26318510+larry0x@users.noreply.github.com> Date: Thu, 14 Jul 2022 00:48:57 +0100 Subject: [PATCH 15/25] revert unintential changes made to `Cargo.toml` --- Cargo.lock | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6d6316fa41..ef3d6b1290 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1576,9 +1576,9 @@ checksum = "8cb243bdfdb5936c8dc3c45762a19d12ab4550cdc753bc247637d4ec35a040fd" [[package]] name = "serde" -version = "1.0.103" +version = "1.0.137" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1217f97ab8e8904b57dd22eb61cde455fa7446a9c1cf43966066da047c1f3702" +checksum = "61ea8d54c77f8315140a05f4c7237403bf38b72704d031543aa1d16abbf517d1" dependencies = [ "serde_derive", ] @@ -1613,9 +1613,9 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.103" +version = "1.0.137" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a8c6faef9a2e64b0064f48570289b4bf8823b7581f1d6157c1b52152306651d0" +checksum = "1f26faba0c3959972377d3b2d306ee9f71faee9714294e41bb777f83f88578be" dependencies = [ "proc-macro2", "quote", From 2aeca6074f149d721a769779728c6f8578122036 Mon Sep 17 00:00:00 2001 From: larry <26318510+larry0x@users.noreply.github.com> Date: Thu, 14 Jul 2022 00:58:59 +0100 Subject: [PATCH 16/25] update schema --- contracts/reflect/schema/query_msg.json | 2 +- packages/std/schema/query_request.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/contracts/reflect/schema/query_msg.json b/contracts/reflect/schema/query_msg.json index 63eb7d4058..d542b568c3 100644 --- a/contracts/reflect/schema/query_msg.json +++ b/contracts/reflect/schema/query_msg.json @@ -109,7 +109,7 @@ "BankQuery": { "oneOf": [ { - "description": "This calls into the native bank module for querying the total supply on one denomination. Return value is SupplyResponse", + "description": "This calls into the native bank module for querying the total supply of one denomination. It does the same as the SupplyOf call in Cosmos SDK's RPC API. Return value is of type SupplyResponse.", "type": "object", "required": [ "supply" diff --git a/packages/std/schema/query_request.json b/packages/std/schema/query_request.json index c1500291b0..fd674b44cf 100644 --- a/packages/std/schema/query_request.json +++ b/packages/std/schema/query_request.json @@ -43,7 +43,7 @@ "BankQuery": { "oneOf": [ { - "description": "This calls into the native bank module for querying the total supply on one denomination. Return value is SupplyResponse", + "description": "This calls into the native bank module for querying the total supply of one denomination. It does the same as the SupplyOf call in Cosmos SDK's RPC API. Return value is of type SupplyResponse.", "type": "object", "required": [ "supply" From 6f125e415dc36026e377f0372ba96cd94e62afa1 Mon Sep 17 00:00:00 2001 From: larry <26318510+larry0x@users.noreply.github.com> Date: Thu, 14 Jul 2022 01:31:09 +0100 Subject: [PATCH 17/25] updat schema --- contracts/reflect/schema/reflect.json | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/contracts/reflect/schema/reflect.json b/contracts/reflect/schema/reflect.json index a2b6c8ebd8..6ade7f08c8 100644 --- a/contracts/reflect/schema/reflect.json +++ b/contracts/reflect/schema/reflect.json @@ -976,6 +976,27 @@ "definitions": { "BankQuery": { "oneOf": [ + { + "description": "This calls into the native bank module for querying the total supply of one denomination. It does the same as the SupplyOf call in Cosmos SDK's RPC API. Return value is of type SupplyResponse.", + "type": "object", + "required": [ + "supply" + ], + "properties": { + "supply": { + "type": "object", + "required": [ + "denom" + ], + "properties": { + "denom": { + "type": "string" + } + } + } + }, + "additionalProperties": false + }, { "description": "This calls into the native bank module for one denomination Return value is BalanceResponse", "type": "object", From abe4a4f865c2a6302f4206bb9110d0a2039a5d1f Mon Sep 17 00:00:00 2001 From: Simon Warta <2603011+webmaster128@users.noreply.github.com> Date: Thu, 14 Jul 2022 08:54:14 +0200 Subject: [PATCH 18/25] Update packages/std/src/query/bank.rs --- packages/std/src/query/bank.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/std/src/query/bank.rs b/packages/std/src/query/bank.rs index bb612ceb2c..7008f97df8 100644 --- a/packages/std/src/query/bank.rs +++ b/packages/std/src/query/bank.rs @@ -22,6 +22,7 @@ pub enum BankQuery { #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] #[serde(rename_all = "snake_case")] +#[non_exhaustive] pub struct SupplyResponse { /// Always returns a Coin with the requested denom. /// This will be of zero amount if the denom does not exist. From 162bcfd69061ca9212926ae18dc80a7220274251 Mon Sep 17 00:00:00 2001 From: Tomasz Kurcz Date: Tue, 23 Aug 2022 14:55:36 +0200 Subject: [PATCH 19/25] fix poorly resolved merge conflict --- packages/std/src/traits.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/std/src/traits.rs b/packages/std/src/traits.rs index 0f6aff2bad..228dd4a540 100644 --- a/packages/std/src/traits.rs +++ b/packages/std/src/traits.rs @@ -411,7 +411,9 @@ mod tests { let all_balances = wrapper.query_all_balances("foo").unwrap(); assert_eq!(all_balances, vec![coin(123, "ELF"), coin(777, "FLY")]); + } + #[test] fn contract_info() { const ACCT: &str = "foobar"; fn mock_resp() -> ContractInfoResponse { From 61bcb49e60fb94dde7c0e4847819e6cafb394903 Mon Sep 17 00:00:00 2001 From: Tomasz Kurcz Date: Tue, 23 Aug 2022 15:33:17 +0200 Subject: [PATCH 20/25] hide BankQuery::Supply behind the `cosmwasm_1_1` feature --- packages/std/Cargo.toml | 3 +++ packages/std/src/exports.rs | 4 ++++ packages/std/src/lib.rs | 4 +++- packages/std/src/mock.rs | 8 ++++++-- packages/std/src/query/bank.rs | 2 ++ packages/std/src/query/mod.rs | 4 +++- packages/std/src/traits.rs | 11 ++++++++--- 7 files changed, 29 insertions(+), 7 deletions(-) diff --git a/packages/std/Cargo.toml b/packages/std/Cargo.toml index d3ed9ccd8d..e2b4478dd5 100644 --- a/packages/std/Cargo.toml +++ b/packages/std/Cargo.toml @@ -33,6 +33,9 @@ stargate = [] # ibc3 extends ibc messages with ibc-v3 only features. This should only be enabled on contracts # that require these types. Without this, they get the smaller ibc-v1 API. ibc3 = ["stargate"] +# This feature makes `BankQuery::Supply` available for the contract to call, but requires +# the host blockchain to run CosmWasm `1.1.0` or higher. +cosmwasm_1_1 = [] [dependencies] base64 = "0.13.0" diff --git a/packages/std/src/exports.rs b/packages/std/src/exports.rs index 90f1880da4..a2c986806f 100644 --- a/packages/std/src/exports.rs +++ b/packages/std/src/exports.rs @@ -41,6 +41,10 @@ extern "C" fn requires_staking() -> () {} #[no_mangle] extern "C" fn requires_stargate() -> () {} +#[cfg(feature = "cosmwasm_1_1")] +#[no_mangle] +extern "C" fn requires_cosmwasm_1_1() -> () {} + /// interface_version_* exports mark which Wasm VM interface level this contract is compiled for. /// They can be checked by cosmwasm_vm. /// Update this whenever the Wasm VM interface breaks. diff --git a/packages/std/src/lib.rs b/packages/std/src/lib.rs index cd08395450..54e2a4bce3 100644 --- a/packages/std/src/lib.rs +++ b/packages/std/src/lib.rs @@ -46,9 +46,11 @@ pub use crate::math::{ Decimal, Decimal256, Decimal256RangeExceeded, DecimalRangeExceeded, Fraction, Isqrt, Uint128, Uint256, Uint512, Uint64, }; +#[cfg(feature = "cosmwasm_1_1")] +pub use crate::query::SupplyResponse; pub use crate::query::{ AllBalanceResponse, BalanceResponse, BankQuery, ContractInfoResponse, CustomQuery, - QueryRequest, SupplyResponse, WasmQuery, + QueryRequest, WasmQuery, }; #[cfg(feature = "staking")] pub use crate::query::{ diff --git a/packages/std/src/mock.rs b/packages/std/src/mock.rs index c9c8ff6956..14e844502f 100644 --- a/packages/std/src/mock.rs +++ b/packages/std/src/mock.rs @@ -16,9 +16,10 @@ use crate::ibc::{ IbcTimeoutBlock, }; use crate::math::Uint128; +#[cfg(feature = "cosmwasm_1_1")] +use crate::query::SupplyResponse; use crate::query::{ - AllBalanceResponse, BalanceResponse, BankQuery, CustomQuery, QueryRequest, SupplyResponse, - WasmQuery, + AllBalanceResponse, BalanceResponse, BankQuery, CustomQuery, QueryRequest, WasmQuery, }; #[cfg(feature = "staking")] use crate::query::{ @@ -566,6 +567,7 @@ impl Default for WasmQuerier { #[derive(Clone, Default)] pub struct BankQuerier { + #[allow(dead_code)] supplies: HashMap, balances: HashMap>, } @@ -590,6 +592,7 @@ impl BankQuerier { pub fn query(&self, request: &BankQuery) -> QuerierResult { let contract_result: ContractResult = match request { + #[cfg(feature = "cosmwasm_1_1")] BankQuery::Supply { denom } => { let amount = self .supplies @@ -1086,6 +1089,7 @@ mod tests { assert_eq!(res.unwrap_err(), VerificationError::InvalidPubkeyFormat); } + #[cfg(feature = "cosmwasm_1_1")] #[test] fn bank_querier_supply() { let addr1 = String::from("foo"); diff --git a/packages/std/src/query/bank.rs b/packages/std/src/query/bank.rs index 1850a0b484..9e5d8148ff 100644 --- a/packages/std/src/query/bank.rs +++ b/packages/std/src/query/bank.rs @@ -10,6 +10,7 @@ pub enum BankQuery { /// This calls into the native bank module for querying the total supply of one denomination. /// It does the same as the SupplyOf call in Cosmos SDK's RPC API. /// Return value is of type SupplyResponse. + #[cfg(feature = "cosmwasm_1_1")] Supply { denom: String }, /// This calls into the native bank module for one denomination /// Return value is BalanceResponse @@ -20,6 +21,7 @@ pub enum BankQuery { AllBalances { address: String }, } +#[cfg(feature = "cosmwasm_1_1")] #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] #[serde(rename_all = "snake_case")] #[non_exhaustive] diff --git a/packages/std/src/query/mod.rs b/packages/std/src/query/mod.rs index 38a302a16c..67a2fcdb0d 100644 --- a/packages/std/src/query/mod.rs +++ b/packages/std/src/query/mod.rs @@ -10,7 +10,9 @@ mod ibc; mod staking; mod wasm; -pub use bank::{AllBalanceResponse, BalanceResponse, BankQuery, SupplyResponse}; +#[cfg(feature = "cosmwasm_1_1")] +pub use bank::SupplyResponse; +pub use bank::{AllBalanceResponse, BalanceResponse, BankQuery}; #[cfg(feature = "stargate")] pub use ibc::{ChannelResponse, IbcQuery, ListChannelsResponse, PortIdResponse}; #[cfg(feature = "staking")] diff --git a/packages/std/src/traits.rs b/packages/std/src/traits.rs index 228dd4a540..6536d75bde 100644 --- a/packages/std/src/traits.rs +++ b/packages/std/src/traits.rs @@ -8,9 +8,10 @@ use crate::coins::Coin; use crate::errors::{RecoverPubkeyError, StdError, StdResult, VerificationError}; #[cfg(feature = "iterator")] use crate::iterator::{Order, Record}; +#[cfg(feature = "cosmwasm_1_1")] +use crate::query::SupplyResponse; use crate::query::{ - AllBalanceResponse, BalanceResponse, BankQuery, CustomQuery, QueryRequest, SupplyResponse, - WasmQuery, + AllBalanceResponse, BalanceResponse, BankQuery, CustomQuery, QueryRequest, WasmQuery, }; #[cfg(feature = "staking")] use crate::query::{ @@ -199,6 +200,7 @@ impl<'a, C: CustomQuery> QuerierWrapper<'a, C> { } } + #[cfg(feature = "cosmwasm_1_1")] pub fn query_supply(&self, denom: impl Into) -> StdResult { let request = BankQuery::Supply { denom: denom.into(), @@ -355,7 +357,7 @@ impl<'a, C: CustomQuery> QuerierWrapper<'a, C> { mod tests { use super::*; use crate::mock::MockQuerier; - use crate::{coin, coins, from_slice, Uint128}; + use crate::{coins, from_slice, Uint128}; // this is a simple demo helper to prove we can use it fn demo_helper(_querier: &dyn Querier) -> u64 { @@ -395,8 +397,11 @@ mod tests { assert_eq!(balance.amount.amount, Uint128::new(5)); } + #[cfg(feature = "cosmwasm_1_1")] #[test] fn bank_query_helpers_work() { + use crate::coin; + let querier: MockQuerier = MockQuerier::new(&[ ("foo", &[coin(123, "ELF"), coin(777, "FLY")]), ("bar", &[coin(321, "ELF")]), From 914f87c7d1b1c76d06e4f6e95b857c3257f6840b Mon Sep 17 00:00:00 2001 From: Tomasz Kurcz Date: Tue, 23 Aug 2022 15:43:49 +0200 Subject: [PATCH 21/25] fix Rust 1.63 lints --- packages/std/src/query/bank.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/std/src/query/bank.rs b/packages/std/src/query/bank.rs index 9e5d8148ff..9656ea6138 100644 --- a/packages/std/src/query/bank.rs +++ b/packages/std/src/query/bank.rs @@ -31,7 +31,7 @@ pub struct SupplyResponse { pub amount: Coin, } -#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] #[serde(rename_all = "snake_case")] pub struct BalanceResponse { /// Always returns a Coin with the requested denom. From b440266191bd86af113bab2dc65962ff6a9451d9 Mon Sep 17 00:00:00 2001 From: Tomasz Kurcz Date: Tue, 23 Aug 2022 15:45:36 +0200 Subject: [PATCH 22/25] generate std schemas including cosmwasm_1_1 feature --- devtools/check_workspace.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/devtools/check_workspace.sh b/devtools/check_workspace.sh index 1c0d0bcaac..7e9c04d064 100755 --- a/devtools/check_workspace.sh +++ b/devtools/check_workspace.sh @@ -13,7 +13,7 @@ cargo fmt cargo wasm-debug cargo wasm-debug --features iterator,staking,stargate cargo clippy --all-targets --features iterator,staking,stargate -- -D warnings - cargo schema + cargo schema --features cosmwasm_1_1 ) (cd packages/storage && cargo build && cargo clippy --all-targets --features iterator -- -D warnings) (cd packages/schema && cargo build && cargo clippy --all-targets -- -D warnings) From 3cdc9ef93ffdd18c421b49b5f7b57b9abf62465c Mon Sep 17 00:00:00 2001 From: Tomasz Kurcz Date: Tue, 23 Aug 2022 19:22:06 +0200 Subject: [PATCH 23/25] test BankQuery::Supply using the reflect contract --- contracts/reflect/Cargo.toml | 2 +- contracts/reflect/tests/integration.rs | 50 +++++++++++++++++++++++--- packages/check/src/main.rs | 2 +- packages/vm/examples/check_contract.rs | 2 +- packages/vm/src/testing/instance.rs | 2 +- 5 files changed, 50 insertions(+), 8 deletions(-) diff --git a/contracts/reflect/Cargo.toml b/contracts/reflect/Cargo.toml index 6e66eded1c..6ec3c92e4a 100644 --- a/contracts/reflect/Cargo.toml +++ b/contracts/reflect/Cargo.toml @@ -34,7 +34,7 @@ backtraces = ["cosmwasm-std/backtraces", "cosmwasm-vm/backtraces"] [dependencies] cosmwasm-schema = { path = "../../packages/schema" } -cosmwasm-std = { path = "../../packages/std", default-features = false, features = ["staking", "stargate"] } +cosmwasm-std = { path = "../../packages/std", default-features = false, features = ["staking", "stargate", "cosmwasm_1_1"] } cosmwasm-storage = { path = "../../packages/storage", default-features = false } schemars = "0.8.1" serde = { version = "=1.0.103", default-features = false, features = ["derive"] } diff --git a/contracts/reflect/tests/integration.rs b/contracts/reflect/tests/integration.rs index 25bb93fe3c..4df7685dc8 100644 --- a/contracts/reflect/tests/integration.rs +++ b/contracts/reflect/tests/integration.rs @@ -18,8 +18,9 @@ //! 4. Anywhere you see query(&deps, ...) you must replace it with query(&mut deps, ...) use cosmwasm_std::{ - coin, coins, from_binary, BankMsg, Binary, Coin, ContractResult, Event, Reply, Response, - StakingMsg, SubMsg, SubMsgResponse, SubMsgResult, SystemResult, + coin, coins, from_binary, BankMsg, BankQuery, Binary, Coin, ContractResult, Event, + QueryRequest, Reply, Response, StakingMsg, SubMsg, SubMsgResponse, SubMsgResult, + SupplyResponse, SystemResult, }; use cosmwasm_vm::{ testing::{ @@ -30,8 +31,8 @@ use cosmwasm_vm::{ }; use reflect::msg::{ - CapitalizedResponse, CustomMsg, ExecuteMsg, InstantiateMsg, OwnerResponse, QueryMsg, - SpecialQuery, + CapitalizedResponse, ChainResponse, CustomMsg, ExecuteMsg, InstantiateMsg, OwnerResponse, + QueryMsg, SpecialQuery, }; use reflect::testing::custom_query_execute; @@ -56,6 +57,19 @@ pub fn mock_dependencies_with_custom_querier( } } +pub fn mock_dependencies_with_custom_querier_and_balances( + balances: &[(&str, &[Coin])], +) -> Backend> { + let custom_querier: MockQuerier = MockQuerier::new(balances) + .with_custom_handler(|query| SystemResult::Ok(custom_query_execute(query))); + + Backend { + api: MockApi::default(), + storage: MockStorage::default(), + querier: custom_querier, + } +} + #[test] fn proper_initialization() { let mut deps = mock_instance(WASM, &[]); @@ -166,6 +180,34 @@ fn transfer_requires_owner() { assert!(msg.contains("Permission denied: the sender is not the current owner")); } +#[test] +fn supply_query() { + // stub gives us defaults. Consume it and override... + let custom = mock_dependencies_with_custom_querier_and_balances(&[ + ("ryan_reynolds", &[coin(5, "ATOM"), coin(10, "OSMO")]), + ("huge_ackman", &[coin(15, "OSMO"), coin(5, "BTC")]), + ]); + // we cannot use mock_instance, so we just copy and modify code from cosmwasm_vm::testing + let (instance_options, memory_limit) = mock_instance_options(); + let mut deps = Instance::from_code(WASM, custom, instance_options, memory_limit).unwrap(); + + // we don't even initialize, just trigger a query + let res = query( + &mut deps, + mock_env(), + QueryMsg::Chain { + request: QueryRequest::Bank(BankQuery::Supply { + denom: "OSMO".to_string(), + }), + }, + ) + .unwrap(); + + let res: ChainResponse = from_binary(&res).unwrap(); + let res: SupplyResponse = from_binary(&res.data).unwrap(); + assert_eq!(res.amount, coin(25, "OSMO")); +} + #[test] fn dispatch_custom_query() { // stub gives us defaults. Consume it and override... diff --git a/packages/check/src/main.rs b/packages/check/src/main.rs index 98541f5f8f..1d4e6eb67d 100644 --- a/packages/check/src/main.rs +++ b/packages/check/src/main.rs @@ -10,7 +10,7 @@ use colored::Colorize; use cosmwasm_vm::capabilities_from_csv; use cosmwasm_vm::internals::{check_wasm, compile}; -const DEFAULT_AVAILABLE_CAPABILITIES: &str = "iterator,staking,stargate"; +const DEFAULT_AVAILABLE_CAPABILITIES: &str = "iterator,staking,stargate,cosmwasm_1_1"; pub fn main() { let matches = App::new("Contract checking") diff --git a/packages/vm/examples/check_contract.rs b/packages/vm/examples/check_contract.rs index 401421cd7b..1d53b164ee 100644 --- a/packages/vm/examples/check_contract.rs +++ b/packages/vm/examples/check_contract.rs @@ -6,7 +6,7 @@ use clap::{App, Arg}; use cosmwasm_vm::capabilities_from_csv; use cosmwasm_vm::internals::{check_wasm, compile}; -const DEFAULT_AVAILABLE_CAPABILITIES: &str = "iterator,staking,stargate"; +const DEFAULT_AVAILABLE_CAPABILITIES: &str = "iterator,staking,stargate,cosmwasm_1_1"; pub fn main() { eprintln!("`check_contract` will be removed from the next version of `cosmwasm-vm` - please use `cosmwasm-check` instead."); diff --git a/packages/vm/src/testing/instance.rs b/packages/vm/src/testing/instance.rs index a30030a44b..a5aeeb6ac6 100644 --- a/packages/vm/src/testing/instance.rs +++ b/packages/vm/src/testing/instance.rs @@ -95,7 +95,7 @@ pub struct MockInstanceOptions<'a> { impl MockInstanceOptions<'_> { fn default_capabilities() -> HashSet { #[allow(unused_mut)] - let mut out = capabilities_from_csv("iterator,staking"); + let mut out = capabilities_from_csv("iterator,staking,cosmwasm_1_1"); #[cfg(feature = "stargate")] out.insert("stargate".to_string()); out From 74a68bc82cae19e67857b74682265027b1836af2 Mon Sep 17 00:00:00 2001 From: Tomasz Kurcz Date: Tue, 23 Aug 2022 19:27:38 +0200 Subject: [PATCH 24/25] update CI after introducing the `cosmwasm_1_1` feature --- .circleci/config.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 9c08e76b05..764ec11f53 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -274,19 +274,19 @@ jobs: - run: name: Build library for native target (all features) working_directory: ~/project/packages/std - command: cargo build --locked --features abort,iterator,staking,stargate + command: cargo build --locked --features abort,iterator,staking,stargate,cosmwasm_1_1 - run: name: Build library for wasm target (all features) working_directory: ~/project/packages/std - command: cargo wasm --locked --features abort,iterator,staking,stargate + command: cargo wasm --locked --features abort,iterator,staking,stargate,cosmwasm_1_1 - run: name: Run unit tests (all features) working_directory: ~/project/packages/std - command: cargo test --locked --features abort,iterator,staking,stargate + command: cargo test --locked --features abort,iterator,staking,stargate,cosmwasm_1_1 - run: name: Build and run schema generator working_directory: ~/project/packages/std - command: cargo schema --locked + command: cargo schema --features cosmwasm_1_1 --locked - run: name: Ensure schemas are up-to-date command: | From 492e6d70bae02ba87d21efde00aff810b30c187b Mon Sep 17 00:00:00 2001 From: Tomasz Kurcz Date: Wed, 24 Aug 2022 13:35:12 +0200 Subject: [PATCH 25/25] avoid BankQuerier.supplies ending up with invalid state --- packages/std/src/mock.rs | 48 +++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/packages/std/src/mock.rs b/packages/std/src/mock.rs index 14e844502f..1feb040228 100644 --- a/packages/std/src/mock.rs +++ b/packages/std/src/mock.rs @@ -455,7 +455,7 @@ impl MockQuerier { addr: impl Into, balance: Vec, ) -> Option> { - self.bank.balances.insert(addr.into(), balance) + self.bank.update_balance(addr, balance) } #[cfg(feature = "staking")] @@ -568,28 +568,50 @@ impl Default for WasmQuerier { #[derive(Clone, Default)] pub struct BankQuerier { #[allow(dead_code)] + /// HashMap supplies: HashMap, + /// HashMap balances: HashMap>, } impl BankQuerier { pub fn new(balances: &[(&str, &[Coin])]) -> Self { - let mut supplies_map = HashMap::new(); - let mut balances_map = HashMap::new(); - for (addr, coins) in balances.iter() { - balances_map.insert(addr.to_string(), coins.to_vec()); - for coin in coins.iter() { - *supplies_map - .entry(coin.denom.clone()) - .or_insert_with(Uint128::zero) += coin.amount; - } - } + let balances: HashMap<_, _> = balances + .iter() + .map(|(s, c)| (s.to_string(), c.to_vec())) + .collect(); + BankQuerier { - supplies: supplies_map, - balances: balances_map, + supplies: Self::calculate_supplies(&balances), + balances, } } + pub fn update_balance( + &mut self, + addr: impl Into, + balance: Vec, + ) -> Option> { + let result = self.balances.insert(addr.into(), balance); + self.supplies = Self::calculate_supplies(&self.balances); + + result + } + + fn calculate_supplies(balances: &HashMap>) -> HashMap { + let mut supplies = HashMap::new(); + + let all_coins = balances.iter().flat_map(|(_, coins)| coins); + + for coin in all_coins { + *supplies + .entry(coin.denom.clone()) + .or_insert_with(Uint128::zero) += coin.amount; + } + + supplies + } + pub fn query(&self, request: &BankQuery) -> QuerierResult { let contract_result: ContractResult = match request { #[cfg(feature = "cosmwasm_1_1")]