Skip to content

Commit

Permalink
Refactor cw3-fixed-multisig and cw1 contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
uint committed Jul 5, 2021
1 parent 490baa6 commit c03a7a4
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 146 deletions.
43 changes: 1 addition & 42 deletions contracts/cw1-subkeys/schema/execute_msg.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"msgs": {
"type": "array",
"items": {
"$ref": "#/definitions/SubMsg_for_Empty"
"$ref": "#/definitions/CosmosMsg_for_Empty"
}
}
}
Expand Down Expand Up @@ -421,16 +421,6 @@
}
}
},
"ReplyOn": {
"description": "Use this to define when the contract gets a response callback. If you only need it for errors or success you can select just those in order to save gas.",
"type": "string",
"enum": [
"always",
"error",
"success",
"never"
]
},
"StakingMsg": {
"description": "The message types of the staking module.\n\nSee https://github.com/cosmos/cosmos-sdk/blob/v0.40.0/proto/cosmos/staking/v1beta1/tx.proto",
"anyOf": [
Expand Down Expand Up @@ -515,37 +505,6 @@
}
]
},
"SubMsg_for_Empty": {
"description": "A submessage that will guarantee a `reply` call on success or error, depending on the `reply_on` setting. If you do not need to process the result, use regular messages instead.\n\nNote: On error the submessage execution will revert any partial state changes due to this message, but not revert any state changes in the calling contract. If this is required, it must be done manually in the `reply` entry point.",
"type": "object",
"required": [
"id",
"msg",
"reply_on"
],
"properties": {
"gas_limit": {
"type": [
"integer",
"null"
],
"format": "uint64",
"minimum": 0.0
},
"id": {
"description": "An arbitrary ID chosen by the contract. This is typically used to match `Reply`s in the `reply` entry point to the submessage.",
"type": "integer",
"format": "uint64",
"minimum": 0.0
},
"msg": {
"$ref": "#/definitions/CosmosMsg_for_Empty"
},
"reply_on": {
"$ref": "#/definitions/ReplyOn"
}
}
},
"Timestamp": {
"description": "A point in time in nanosecond precision.\n\nThis type can represent times from 1970-01-01T00:00:00Z to 2554-07-21T23:34:33Z.\n\n## Examples\n\n``` # use cosmwasm_std::Timestamp; let ts = Timestamp::from_nanos(1_000_000_202); assert_eq!(ts.nanos(), 1_000_000_202); assert_eq!(ts.seconds(), 1); assert_eq!(ts.subsec_nanos(), 202);\n\nlet ts = ts.plus_seconds(2); assert_eq!(ts.nanos(), 3_000_000_202); assert_eq!(ts.seconds(), 3); assert_eq!(ts.subsec_nanos(), 202); ```",
"allOf": [
Expand Down
1 change: 1 addition & 0 deletions contracts/cw1-subkeys/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ pub fn execute_execute<T>(
where
T: Clone + fmt::Debug + PartialEq + JsonSchema,
{
// Wrap `msgs` in SubMsg.
let msgs = msgs.into_iter().map(SubMsg::new).collect();
let cfg = ADMIN_LIST.load(deps.storage)?;
// this is the admin behavior (same as cw1-whitelist)
Expand Down
43 changes: 1 addition & 42 deletions contracts/cw1-whitelist/schema/execute_msg.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"msgs": {
"type": "array",
"items": {
"$ref": "#/definitions/SubMsg_for_Empty"
"$ref": "#/definitions/CosmosMsg_for_Empty"
}
}
}
Expand Down Expand Up @@ -258,16 +258,6 @@
"description": "An empty struct that serves as a placeholder in different places, such as contracts that don't set a custom message.\n\nIt is designed to be expressable in correct JSON and JSON Schema but contains no meaningful data. Previously we used enums without cases, but those cannot represented as valid JSON Schema (https://github.com/CosmWasm/cosmwasm/issues/451)",
"type": "object"
},
"ReplyOn": {
"description": "Use this to define when the contract gets a response callback. If you only need it for errors or success you can select just those in order to save gas.",
"type": "string",
"enum": [
"always",
"error",
"success",
"never"
]
},
"StakingMsg": {
"description": "The message types of the staking module.\n\nSee https://github.com/cosmos/cosmos-sdk/blob/v0.40.0/proto/cosmos/staking/v1beta1/tx.proto",
"anyOf": [
Expand Down Expand Up @@ -352,37 +342,6 @@
}
]
},
"SubMsg_for_Empty": {
"description": "A submessage that will guarantee a `reply` call on success or error, depending on the `reply_on` setting. If you do not need to process the result, use regular messages instead.\n\nNote: On error the submessage execution will revert any partial state changes due to this message, but not revert any state changes in the calling contract. If this is required, it must be done manually in the `reply` entry point.",
"type": "object",
"required": [
"id",
"msg",
"reply_on"
],
"properties": {
"gas_limit": {
"type": [
"integer",
"null"
],
"format": "uint64",
"minimum": 0.0
},
"id": {
"description": "An arbitrary ID chosen by the contract. This is typically used to match `Reply`s in the `reply` entry point to the submessage.",
"type": "integer",
"format": "uint64",
"minimum": 0.0
},
"msg": {
"$ref": "#/definitions/CosmosMsg_for_Empty"
},
"reply_on": {
"$ref": "#/definitions/ReplyOn"
}
}
},
"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"
Expand Down
19 changes: 13 additions & 6 deletions contracts/cw1-whitelist/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ pub fn execute_execute<T>(
deps: DepsMut,
_env: Env,
info: MessageInfo,
msgs: Vec<SubMsg<T>>,
msgs: Vec<CosmosMsg<T>>,
) -> Result<Response<T>, ContractError>
where
T: Clone + fmt::Debug + PartialEq + JsonSchema,
{
// Wrap `msgs` in SubMsg.
let msgs = msgs.into_iter().map(SubMsg::new).collect();
if !can_execute(deps.as_ref(), info.sender.as_ref())? {
Err(ContractError::Unauthorized {})
} else {
Expand Down Expand Up @@ -243,15 +245,17 @@ mod tests {

let freeze: ExecuteMsg<Empty> = ExecuteMsg::Freeze {};
let msgs = vec![
SubMsg::new(BankMsg::Send {
BankMsg::Send {
to_address: bob.to_string(),
amount: coins(10000, "DAI"),
}),
SubMsg::new(WasmMsg::Execute {
}
.into(),
WasmMsg::Execute {
contract_addr: "some contract".into(),
msg: to_binary(&freeze).unwrap(),
funds: vec![],
}),
}
.into(),
];

// make some nice message
Expand All @@ -265,7 +269,10 @@ mod tests {
// but carl can
let info = mock_info(&carl, &[]);
let res = execute(deps.as_mut(), mock_env(), info, execute_msg).unwrap();
assert_eq!(res.messages, msgs);
assert_eq!(
res.messages,
msgs.into_iter().map(SubMsg::new).collect::<Vec<_>>()
);
assert_eq!(res.attributes, vec![attr("action", "execute")]);
}

Expand Down
4 changes: 2 additions & 2 deletions contracts/cw1-whitelist/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::fmt;

use cosmwasm_std::{CosmosMsg, Empty, SubMsg};
use cosmwasm_std::{CosmosMsg, Empty};

#[derive(Serialize, Deserialize, JsonSchema)]
pub struct InstantiateMsg {
Expand All @@ -19,7 +19,7 @@ where
/// Execute requests the contract to re-dispatch all these messages with the
/// contract's address as sender. Every implementation has it's own logic to
/// determine in
Execute { msgs: Vec<SubMsg<T>> },
Execute { msgs: Vec<CosmosMsg<T>> },
/// Freeze will make a mutable contract immutable, must be called by an admin
Freeze {},
/// UpdateAdmins will change the admin set of the contract, must be called by an existing admin,
Expand Down
43 changes: 1 addition & 42 deletions contracts/cw3-fixed-multisig/schema/execute_msg.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"msgs": {
"type": "array",
"items": {
"$ref": "#/definitions/SubMsg_for_Empty"
"$ref": "#/definitions/CosmosMsg_for_Empty"
}
},
"title": {
Expand Down Expand Up @@ -354,16 +354,6 @@
}
]
},
"ReplyOn": {
"description": "Use this to define when the contract gets a response callback. If you only need it for errors or success you can select just those in order to save gas.",
"type": "string",
"enum": [
"always",
"error",
"success",
"never"
]
},
"StakingMsg": {
"description": "The message types of the staking module.\n\nSee https://github.com/cosmos/cosmos-sdk/blob/v0.40.0/proto/cosmos/staking/v1beta1/tx.proto",
"anyOf": [
Expand Down Expand Up @@ -448,37 +438,6 @@
}
]
},
"SubMsg_for_Empty": {
"description": "A submessage that will guarantee a `reply` call on success or error, depending on the `reply_on` setting. If you do not need to process the result, use regular messages instead.\n\nNote: On error the submessage execution will revert any partial state changes due to this message, but not revert any state changes in the calling contract. If this is required, it must be done manually in the `reply` entry point.",
"type": "object",
"required": [
"id",
"msg",
"reply_on"
],
"properties": {
"gas_limit": {
"type": [
"integer",
"null"
],
"format": "uint64",
"minimum": 0.0
},
"id": {
"description": "An arbitrary ID chosen by the contract. This is typically used to match `Reply`s in the `reply` entry point to the submessage.",
"type": "integer",
"format": "uint64",
"minimum": 0.0
},
"msg": {
"$ref": "#/definitions/CosmosMsg_for_Empty"
},
"reply_on": {
"$ref": "#/definitions/ReplyOn"
}
}
},
"Timestamp": {
"description": "A point in time in nanosecond precision.\n\nThis type can represent times from 1970-01-01T00:00:00Z to 2554-07-21T23:34:33Z.\n\n## Examples\n\n``` # use cosmwasm_std::Timestamp; let ts = Timestamp::from_nanos(1_000_000_202); assert_eq!(ts.nanos(), 1_000_000_202); assert_eq!(ts.seconds(), 1); assert_eq!(ts.subsec_nanos(), 202);\n\nlet ts = ts.plus_seconds(2); assert_eq!(ts.nanos(), 3_000_000_202); assert_eq!(ts.seconds(), 3); assert_eq!(ts.subsec_nanos(), 202); ```",
"allOf": [
Expand Down
18 changes: 10 additions & 8 deletions contracts/cw3-fixed-multisig/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::cmp::Ordering;
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
attr, to_binary, Binary, BlockInfo, Deps, DepsMut, Empty, Env, MessageInfo, Order, Response,
StdResult, SubMsg,
attr, to_binary, Binary, BlockInfo, CosmosMsg, Deps, DepsMut, Empty, Env, MessageInfo, Order,
Response, StdResult, SubMsg,
};

use cw0::Expiration;
Expand Down Expand Up @@ -87,10 +87,11 @@ pub fn execute_propose(
info: MessageInfo,
title: String,
description: String,
msgs: Vec<SubMsg>,
msgs: Vec<CosmosMsg>,
// we ignore earliest
latest: Option<Expiration>,
) -> Result<Response<Empty>, ContractError> {
let msgs = msgs.into_iter().map(SubMsg::new).collect();
// only members of the multisig can create a proposal
let vote_power = VOTERS
.may_load(deps.storage, &info.sender)?
Expand Down Expand Up @@ -608,7 +609,7 @@ mod tests {
to_address: SOMEBODY.into(),
amount: vec![coin(1, "BTC")],
};
let msgs = vec![SubMsg::new(bank_msg)];
let msgs = vec![CosmosMsg::Bank(bank_msg)];

// Only voters can propose
let info = mock_info(SOMEBODY, &[]);
Expand Down Expand Up @@ -688,7 +689,7 @@ mod tests {
to_address: SOMEBODY.into(),
amount: vec![coin(1, "BTC")],
};
let msgs = vec![SubMsg::new(bank_msg)];
let msgs = vec![CosmosMsg::Bank(bank_msg)];
let proposal = ExecuteMsg::Propose {
title: "Pay somebody".to_string(),
description: "Do I pay her?".to_string(),
Expand Down Expand Up @@ -813,7 +814,7 @@ mod tests {
to_address: SOMEBODY.into(),
amount: vec![coin(1, "BTC")],
};
let msgs = vec![SubMsg::new(bank_msg)];
let msgs = vec![CosmosMsg::Bank(bank_msg)];
let proposal = ExecuteMsg::Propose {
title: "Pay somebody".to_string(),
description: "Do I pay her?".to_string(),
Expand Down Expand Up @@ -863,11 +864,12 @@ mod tests {
let info = mock_info(SOMEBODY, &[]);
let res = execute(deps.as_mut(), mock_env(), info.clone(), execution).unwrap();

let msgs: Vec<_> = msgs.into_iter().map(SubMsg::new).collect();
// Verify
assert_eq!(
res,
Response {
messages: msgs,
messages: msgs.clone(),
attributes: vec![
attr("action", "execute"),
attr("sender", SOMEBODY),
Expand Down Expand Up @@ -899,7 +901,7 @@ mod tests {
to_address: SOMEBODY.into(),
amount: vec![coin(1, "BTC")],
};
let msgs = vec![SubMsg::new(bank_msg)];
let msgs = vec![CosmosMsg::Bank(bank_msg)];
let proposal = ExecuteMsg::Propose {
title: "Pay somebody".to_string(),
description: "Do I pay her?".to_string(),
Expand Down
4 changes: 2 additions & 2 deletions contracts/cw3-fixed-multisig/src/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::contract::{execute, instantiate, query};
use crate::msg::{ExecuteMsg, InstantiateMsg, Voter};
use cosmwasm_std::testing::{mock_env, MockApi, MockStorage};
use cosmwasm_std::{from_binary, to_binary, Addr, Empty, SubMsg, Uint128, WasmMsg, WasmQuery};
use cosmwasm_std::{from_binary, to_binary, Addr, CosmosMsg, Empty, Uint128, WasmMsg, WasmQuery};
use cw0::Duration;
use cw20::{BalanceResponse, MinterResponse};
use cw20_base::msg::QueryMsg;
Expand Down Expand Up @@ -111,7 +111,7 @@ fn cw3_controls_cw20() {
let propose_msg = ExecuteMsg::Propose {
title: "Mint tokens".to_string(),
description: "Need to mint tokens".to_string(),
msgs: vec![SubMsg::new(execute_mint_msg)],
msgs: vec![CosmosMsg::Wasm(execute_mint_msg)],
latest: None,
};
// propose mint
Expand Down
4 changes: 2 additions & 2 deletions contracts/cw3-fixed-multisig/src/msg.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use cosmwasm_std::{Empty, SubMsg};
use cosmwasm_std::{CosmosMsg, Empty};
use cw0::{Duration, Expiration};
use cw3::Vote;

Expand All @@ -25,7 +25,7 @@ pub enum ExecuteMsg {
Propose {
title: String,
description: String,
msgs: Vec<SubMsg<Empty>>,
msgs: Vec<CosmosMsg<Empty>>,
// note: we ignore API-spec'd earliest if passed, always opens immediately
latest: Option<Expiration>,
},
Expand Down

0 comments on commit c03a7a4

Please sign in to comment.