From 551136ab0f1d357edfa6467e9467a6fd56299f09 Mon Sep 17 00:00:00 2001 From: Tomasz Kurcz Date: Mon, 10 Jan 2022 10:57:45 +0100 Subject: [PATCH] voting-contract: remove unnecessary trait object --- .../src/multitest/contracts.rs | 129 +-------------- .../src/multitest/contracts/voting.rs | 152 ++++++++++++++++++ .../voting-contract/src/multitest/suite.rs | 4 +- 3 files changed, 159 insertions(+), 126 deletions(-) create mode 100644 packages/voting-contract/src/multitest/contracts/voting.rs diff --git a/packages/voting-contract/src/multitest/contracts.rs b/packages/voting-contract/src/multitest/contracts.rs index 2b30969c..0b54c71f 100644 --- a/packages/voting-contract/src/multitest/contracts.rs +++ b/packages/voting-contract/src/multitest/contracts.rs @@ -1,4 +1,8 @@ -use cosmwasm_std::{Binary, Deps, DepsMut, Env, MessageInfo, StdError}; +pub mod voting; + +pub use voting::VotingContract; + +use cosmwasm_std::{Binary, Deps, DepsMut, Env, MessageInfo}; use cw_multi_test::{Contract, ContractWrapper}; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; @@ -13,126 +17,3 @@ pub fn engagement_contract() -> Box> { Box::new(contract) } - -pub fn voting_contract() -> Box> { - let contract = ContractWrapper::new(voting::execute, voting::instantiate, voting::query); - Box::new(contract) -} - -pub mod voting { - use cosmwasm_std::to_binary; - use cw3::Vote; - - use crate::{list_voters, propose, query_rules, state::VotingRules, ContractError}; - - use super::*; - - type Response = cosmwasm_std::Response; - - #[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)] - #[serde(rename_all = "snake_case")] - pub struct InstantiateMsg { - pub rules: VotingRules, - pub group_addr: String, - } - - #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] - #[serde(rename_all = "snake_case")] - pub enum ExecuteMsg { - Propose { - title: String, - description: String, - proposal: String, - }, - Vote { - proposal_id: u64, - vote: Vote, - }, - Execute { - proposal_id: u64, - }, - Close { - proposal_id: u64, - }, - /// The Community Pool may be a participant in engagement and end up - /// receiving engagement rewards. This endpoint can be used to withdraw - /// those. Anyone can call it. - WithdrawEngagementRewards {}, - /// Message comming from valset on funds distribution, just takes funds - /// send with message and does nothing - DistributeFunds {}, - } - - #[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)] - #[serde(rename_all = "snake_case")] - pub enum QueryMsg { - /// Return VotingRules - Rules {}, - /// Returns ProposalResponse - Proposal { proposal_id: u64 }, - /// Returns ProposalListResponse - ListProposals { - start_after: Option, - limit: Option, - }, - /// Returns ProposalListResponse - ReverseProposals { - start_before: Option, - limit: Option, - }, - /// Returns VoteResponse - Vote { proposal_id: u64, voter: String }, - /// Returns VoteListResponse - ListVotes { - proposal_id: u64, - start_after: Option, - limit: Option, - }, - /// Returns VoterResponse - Voter { address: String }, - /// Returns VoterListResponse - ListVoters { - start_after: Option, - limit: Option, - }, - /// Returns address of current's group contract - GroupContract {}, - } - - pub fn instantiate( - deps: DepsMut, - _env: Env, - _info: MessageInfo, - msg: InstantiateMsg, - ) -> Result { - crate::instantiate(deps, msg.rules, &msg.group_addr) - } - - pub fn execute( - deps: DepsMut, - env: Env, - info: MessageInfo, - msg: ExecuteMsg, - ) -> Result { - use ExecuteMsg::*; - - match msg { - Propose { - title, - description, - proposal, - } => propose(deps, env, info, title, description, proposal), - _ => todo!(), - } - } - - pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> Result { - use QueryMsg::*; - - match msg { - Rules {} => to_binary(&query_rules(deps)?), - ListVoters { start_after, limit } => to_binary(&list_voters(deps, start_after, limit)?), - _ => todo!(), - } - } -} diff --git a/packages/voting-contract/src/multitest/contracts/voting.rs b/packages/voting-contract/src/multitest/contracts/voting.rs new file mode 100644 index 00000000..1217e8e9 --- /dev/null +++ b/packages/voting-contract/src/multitest/contracts/voting.rs @@ -0,0 +1,152 @@ +use cosmwasm_std::{from_slice, to_binary}; +use cw3::Vote; + +use crate::{list_voters, propose, query_rules, state::VotingRules}; + +use super::*; + +#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)] +#[serde(rename_all = "snake_case")] +pub struct InstantiateMsg { + pub rules: VotingRules, + pub group_addr: String, +} + +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] +#[serde(rename_all = "snake_case")] +pub enum ExecuteMsg { + Propose { + title: String, + description: String, + proposal: String, + }, + Vote { + proposal_id: u64, + vote: Vote, + }, + Execute { + proposal_id: u64, + }, + Close { + proposal_id: u64, + }, + /// The Community Pool may be a participant in engagement and end up + /// receiving engagement rewards. This endpoint can be used to withdraw + /// those. Anyone can call it. + WithdrawEngagementRewards {}, + /// Message comming from valset on funds distribution, just takes funds + /// send with message and does nothing + DistributeFunds {}, +} + +#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)] +#[serde(rename_all = "snake_case")] +pub enum QueryMsg { + /// Return VotingRules + Rules {}, + /// Returns ProposalResponse + Proposal { proposal_id: u64 }, + /// Returns ProposalListResponse + ListProposals { + start_after: Option, + limit: Option, + }, + /// Returns ProposalListResponse + ReverseProposals { + start_before: Option, + limit: Option, + }, + /// Returns VoteResponse + Vote { proposal_id: u64, voter: String }, + /// Returns VoteListResponse + ListVotes { + proposal_id: u64, + start_after: Option, + limit: Option, + }, + /// Returns VoterResponse + Voter { address: String }, + /// Returns VoterListResponse + ListVoters { + start_after: Option, + limit: Option, + }, + /// Returns address of current's group contract + GroupContract {}, +} + +pub struct VotingContract; + +impl Contract for VotingContract { + fn instantiate( + &self, + deps: DepsMut, + _env: Env, + _info: MessageInfo, + msg: Vec, + ) -> anyhow::Result> { + let msg: InstantiateMsg = from_slice(&msg)?; + + crate::instantiate(deps, msg.rules, &msg.group_addr).map_err(anyhow::Error::from) + } + + fn execute( + &self, + deps: DepsMut, + env: Env, + info: MessageInfo, + msg: Vec, + ) -> anyhow::Result> { + let msg: ExecuteMsg = from_slice(&msg)?; + + use ExecuteMsg::*; + match msg { + Propose { + title, + description, + proposal, + } => propose(deps, env, info, title, description, proposal), + _ => todo!(), + } + .map_err(anyhow::Error::from) + } + + fn query(&self, deps: Deps, _env: Env, msg: Vec) -> anyhow::Result { + let msg: QueryMsg = from_slice(&msg)?; + + use QueryMsg::*; + match msg { + Rules {} => to_binary(&query_rules(deps)?), + ListVoters { start_after, limit } => to_binary(&list_voters(deps, start_after, limit)?), + _ => todo!(), + } + .map_err(anyhow::Error::from) + } + + fn sudo( + &self, + _deps: DepsMut, + _env: Env, + _msg: Vec, + ) -> anyhow::Result> { + unimplemented!() + } + + fn reply( + &self, + _deps: DepsMut, + _env: Env, + _msg: cosmwasm_std::Reply, + ) -> anyhow::Result> { + unimplemented!() + } + + fn migrate( + &self, + _deps: DepsMut, + _env: Env, + _msg: Vec, + ) -> anyhow::Result> { + unimplemented!() + } +} diff --git a/packages/voting-contract/src/multitest/suite.rs b/packages/voting-contract/src/multitest/suite.rs index 358640d4..476f1003 100644 --- a/packages/voting-contract/src/multitest/suite.rs +++ b/packages/voting-contract/src/multitest/suite.rs @@ -1,4 +1,4 @@ -use super::contracts::{self, engagement_contract, voting, voting_contract}; +use super::contracts::{self, engagement_contract, voting, VotingContract}; use anyhow::Result as AnyResult; use cosmwasm_std::{Addr, StdResult}; use cw3::{VoterDetail, VoterListResponse}; @@ -61,7 +61,7 @@ impl SuiteBuilder { ) .unwrap(); - let voting_id = app.store_code(voting_contract()); + let voting_id = app.store_code(Box::new(VotingContract)); let voting = app .instantiate_contract( voting_id,