Skip to content

Remove BankQuery::AllBalances and query_all_balances #2433

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

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .github/workflows/typo-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v4
- id: files
uses: tj-actions/changed-files@v45
uses: tj-actions/changed-files@v46
with:
files_ignore: |
contracts/**/schema/**
packages/crypto/**/*.json
packages/vm/**/*.wasm
- name: Run spell-check
uses: crate-ci/typos@master
with:
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ and this project adheres to
([#2195])
- cosmwasm-std: Make `GovMsg` `#[non_exhaustive]` for consistency. ([#2347])
- cosmwasm-crypto: Upgrade ark-\* dependencies to 0.5.0. ([#2432])
- cosmwasm-std: Remove support for `BankQuery::AllBalances` and
`query_all_balances`. ([#2433])

## Fixed

Expand Down Expand Up @@ -94,6 +96,7 @@ and this project adheres to
[#2399]: https://github.com/CosmWasm/cosmwasm/pull/2399
[#2403]: https://github.com/CosmWasm/cosmwasm/pull/2403
[#2432]: https://github.com/CosmWasm/cosmwasm/pull/2432
[#2433]: https://github.com/CosmWasm/cosmwasm/pull/2433
[#2438]: https://github.com/CosmWasm/cosmwasm/pull/2438

## [2.2.0] - 2024-12-17
Expand Down
10 changes: 9 additions & 1 deletion contracts/burner/schema/burner.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"title": "MigrateMsg",
"type": "object",
"required": [
"denoms",
"payout"
],
"properties": {
Expand All @@ -26,8 +27,15 @@
"format": "uint32",
"minimum": 0.0
},
"denoms": {
"description": "The denoms of the final payout. Balances of tokens not listed here will remain in the account untouched.",
"type": "array",
"items": {
"type": "string"
}
},
"payout": {
"description": "The address we send all remaining balance to",
"description": "The address we send all remaining balance to. See denoms below for the denoms to consider.",
"type": "string"
}
},
Expand Down
10 changes: 9 additions & 1 deletion contracts/burner/schema/raw/migrate.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"title": "MigrateMsg",
"type": "object",
"required": [
"denoms",
"payout"
],
"properties": {
Expand All @@ -13,8 +14,15 @@
"format": "uint32",
"minimum": 0.0
},
"denoms": {
"description": "The denoms of the final payout. Balances of tokens not listed here will remain in the account untouched.",
"type": "array",
"items": {
"type": "string"
}
},
"payout": {
"description": "The address we send all remaining balance to",
"description": "The address we send all remaining balance to. See denoms below for the denoms to consider.",
"type": "string"
}
},
Expand Down
79 changes: 68 additions & 11 deletions contracts/burner/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use cosmwasm_std::{
entry_point, BankMsg, DepsMut, Env, MessageInfo, Order, Response, StdError, StdResult, Storage,
entry_point, BankMsg, Coin, DepsMut, Env, MessageInfo, Order, Response, StdError, StdResult,
Storage,
};
use std::collections::BTreeSet;

use crate::msg::{ExecuteMsg, InstantiateMsg, MigrateMsg};

Expand All @@ -18,12 +20,22 @@ pub fn instantiate(

#[entry_point]
pub fn migrate(deps: DepsMut, env: Env, msg: MigrateMsg) -> StdResult<Response> {
// get balance and send all to recipient
#[allow(deprecated)]
let balance = deps.querier.query_all_balances(env.contract.address)?;
let denom_len = msg.denoms.len();
let denoms = BTreeSet::<String>::from_iter(msg.denoms); // Ensure uniqueness
if denoms.len() != denom_len {
return Err(StdError::generic_err("Denoms not unique"));
}

// get balance and send to recipient
let mut balances = Vec::<Coin>::with_capacity(denoms.len());
for denom in denoms {
let balance = deps.querier.query_balance(&env.contract.address, denom)?;
balances.push(balance);
}

let send = BankMsg::Send {
to_address: msg.payout.clone(),
amount: balance,
amount: balances,
};

let deleted = cleanup(deps.storage, msg.delete as usize);
Expand Down Expand Up @@ -86,7 +98,7 @@ mod tests {
use cosmwasm_std::testing::{
message_info, mock_dependencies, mock_dependencies_with_balance, mock_env,
};
use cosmwasm_std::{coins, Attribute, StdError, Storage, SubMsg};
use cosmwasm_std::{coin, coins, Attribute, StdError, Storage, SubMsg};

/// Gets the value of the first attribute with the given key
fn first_attr(data: impl AsRef<[Attribute]>, search_key: &str) -> Option<String> {
Expand Down Expand Up @@ -118,24 +130,64 @@ mod tests {
}

#[test]
fn migrate_sends_funds() {
let mut deps = mock_dependencies_with_balance(&coins(123456, "gold"));
fn migrate_sends_one_balance() {
let initial_balance = vec![coin(123456, "gold"), coin(77, "silver")];
let mut deps = mock_dependencies_with_balance(&initial_balance);
let payout = String::from("someone else");

// malformed denoms
let msg = MigrateMsg {
payout: payout.clone(),
denoms: vec!["gold".to_string(), "silver".to_string(), "gold".to_string()],
delete: 0,
};
let err = migrate(deps.as_mut(), mock_env(), msg).unwrap_err();
match err {
StdError::GenericErr { msg, .. } => assert_eq!(msg, "Denoms not unique"),
err => panic!("Unexpected error: {err:?}"),
}

// One denom
let msg = MigrateMsg {
payout: payout.clone(),
denoms: vec!["gold".to_string()],
delete: 0,
};
let res = migrate(deps.as_mut(), mock_env(), msg).unwrap();
// check payout
assert_eq!(res.messages.len(), 1);
let msg = res.messages.first().expect("no message");
assert_eq!(
msg,
&SubMsg::new(BankMsg::Send {
to_address: payout,
amount: coins(123456, "gold"),
})
);
}

// as above but this time we want all gold and silver
#[test]
fn migrate_sends_two_balances() {
let initial_balance = vec![coin(123456, "gold"), coin(77, "silver")];
let mut deps = mock_dependencies_with_balance(&initial_balance);

// change the verifier via migrate
let payout = String::from("someone else");
let msg = MigrateMsg {
payout: payout.clone(),
denoms: vec!["silver".to_string(), "gold".to_string()],
delete: 0,
};
let res = migrate(deps.as_mut(), mock_env(), msg).unwrap();
// check payout
assert_eq!(1, res.messages.len());
assert_eq!(res.messages.len(), 1);
let msg = res.messages.first().expect("no message");
assert_eq!(
msg,
&SubMsg::new(BankMsg::Send {
to_address: payout,
amount: coins(123456, "gold"),
amount: vec![coin(123456, "gold"), coin(77, "silver")],
})
);
}
Expand All @@ -154,6 +206,7 @@ mod tests {
// migrate all of the data in one go
let msg = MigrateMsg {
payout: "user".to_string(),
denoms: vec![],
delete: 100,
};
migrate(deps.as_mut(), mock_env(), msg).unwrap();
Expand All @@ -178,7 +231,11 @@ mod tests {

// change the verifier via migrate
let payout = String::from("someone else");
let msg = MigrateMsg { payout, delete: 0 };
let msg = MigrateMsg {
payout,
denoms: vec![],
delete: 0,
};
let _res = migrate(deps.as_mut(), mock_env(), msg).unwrap();

let res = execute(
Expand Down
6 changes: 5 additions & 1 deletion contracts/burner/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ use cosmwasm_schema::cw_serde;

#[cw_serde]
pub struct MigrateMsg {
/// The address we send all remaining balance to
/// The address we send all remaining balance to. See denoms
/// below for the denoms to consider.
pub payout: String,
/// The denoms of the final payout. Balances of tokens not listed here
/// will remain in the account untouched.
pub denoms: Vec<String>,
/// Optional amount of items to delete in this call.
/// If it is not provided, nothing will be deleted.
/// You can delete further items in a subsequent execute call.
Expand Down
8 changes: 7 additions & 1 deletion contracts/burner/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ fn migrate_sends_funds() {

// change the verifier via migrate
let payout = String::from("someone else");

let msg = MigrateMsg {
payout: payout.clone(),
denoms: vec!["gold".to_string()],
delete: 0,
};
let res: Response = migrate(&mut deps, mock_env(), msg).unwrap();
Expand Down Expand Up @@ -95,7 +97,11 @@ fn execute_cleans_up_data() {

// change the verifier via migrate
let payout = String::from("someone else");
let msg = MigrateMsg { payout, delete: 0 };
let msg = MigrateMsg {
payout,
denoms: vec![],
delete: 0,
};
let _res: Response = migrate(&mut deps, mock_env(), msg).unwrap();

let res: Response = execute(
Expand Down
72 changes: 9 additions & 63 deletions contracts/hackatom/schema/hackatom.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,22 @@
"title": "ExecuteMsg",
"oneOf": [
{
"description": "Releasing all funds in the contract to the beneficiary. This is the only \"proper\" action of this demo contract.",
"description": "Releasing all funds of the given denom in the contract to the beneficiary. This is the only \"proper\" action of this demo contract.",
"type": "object",
"required": [
"release"
],
"properties": {
"release": {
"type": "object",
"required": [
"denom"
],
"properties": {
"denom": {
"type": "string"
}
},
"additionalProperties": false
}
},
Expand Down Expand Up @@ -166,28 +174,6 @@
},
"additionalProperties": false
},
{
"description": "This returns cosmwasm_std::AllBalanceResponse to demo use of the querier",
"type": "object",
"required": [
"other_balance"
],
"properties": {
"other_balance": {
"type": "object",
"required": [
"address"
],
"properties": {
"address": {
"type": "string"
}
},
"additionalProperties": false
}
},
"additionalProperties": false
},
{
"description": "Recurse will execute a query into itself up to depth-times and return Each step of the recursion may perform some extra work to test gas metering (`work` rounds of sha256 on contract). Now that we have Env, we can auto-calculate the address to recurse into",
"type": "object",
Expand Down Expand Up @@ -323,46 +309,6 @@
},
"additionalProperties": false
},
"other_balance": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "AllBalanceResponse",
"type": "object",
"required": [
"amount"
],
"properties": {
"amount": {
"description": "Returns all non-zero coins held by this account.",
"type": "array",
"items": {
"$ref": "#/definitions/Coin"
}
}
},
"additionalProperties": false,
"definitions": {
"Coin": {
"type": "object",
"required": [
"amount",
"denom"
],
"properties": {
"amount": {
"$ref": "#/definitions/Uint128"
},
"denom": {
"type": "string"
}
},
"additionalProperties": false
},
"Uint128": {
"description": "A thin wrapper around u128 that is using strings for JSON encoding/decoding, such that the full u128 range can be used for clients that convert JSON numbers to floats, like JavaScript and jq.\n\n# Examples\n\nUse `from` to create instances of this and `u128` to get the value out:\n\n``` # use cosmwasm_std::Uint128; let a = Uint128::from(123u128); assert_eq!(a.u128(), 123);\n\nlet b = Uint128::from(42u64); assert_eq!(b.u128(), 42);\n\nlet c = Uint128::from(70u32); assert_eq!(c.u128(), 70); ```",
"type": "string"
}
}
},
"recurse": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "RecurseResponse",
Expand Down
10 changes: 9 additions & 1 deletion contracts/hackatom/schema/raw/execute.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@
"title": "ExecuteMsg",
"oneOf": [
{
"description": "Releasing all funds in the contract to the beneficiary. This is the only \"proper\" action of this demo contract.",
"description": "Releasing all funds of the given denom in the contract to the beneficiary. This is the only \"proper\" action of this demo contract.",
"type": "object",
"required": [
"release"
],
"properties": {
"release": {
"type": "object",
"required": [
"denom"
],
"properties": {
"denom": {
"type": "string"
}
},
"additionalProperties": false
}
},
Expand Down
Loading
Loading