Skip to content

Commit

Permalink
voting-contract: remove unnecessary trait object
Browse files Browse the repository at this point in the history
  • Loading branch information
uint committed Jan 10, 2022
1 parent 685b4ba commit 551136a
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 126 deletions.
129 changes: 5 additions & 124 deletions packages/voting-contract/src/multitest/contracts.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -13,126 +17,3 @@ pub fn engagement_contract() -> Box<dyn Contract<TgradeMsg>> {

Box::new(contract)
}

pub fn voting_contract() -> Box<dyn Contract<TgradeMsg>> {
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<TgradeMsg>;

#[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<u64>,
limit: Option<u32>,
},
/// Returns ProposalListResponse
ReverseProposals {
start_before: Option<u64>,
limit: Option<u32>,
},
/// Returns VoteResponse
Vote { proposal_id: u64, voter: String },
/// Returns VoteListResponse
ListVotes {
proposal_id: u64,
start_after: Option<String>,
limit: Option<u32>,
},
/// Returns VoterResponse
Voter { address: String },
/// Returns VoterListResponse
ListVoters {
start_after: Option<String>,
limit: Option<u32>,
},
/// Returns address of current's group contract
GroupContract {},
}

pub fn instantiate(
deps: DepsMut,
_env: Env,
_info: MessageInfo,
msg: InstantiateMsg,
) -> Result<Response, ContractError> {
crate::instantiate(deps, msg.rules, &msg.group_addr)
}

pub fn execute(
deps: DepsMut,
env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> Result<Response, ContractError> {
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<Binary, StdError> {
use QueryMsg::*;

match msg {
Rules {} => to_binary(&query_rules(deps)?),
ListVoters { start_after, limit } => to_binary(&list_voters(deps, start_after, limit)?),
_ => todo!(),
}
}
}
152 changes: 152 additions & 0 deletions packages/voting-contract/src/multitest/contracts/voting.rs
Original file line number Diff line number Diff line change
@@ -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<u64>,
limit: Option<u32>,
},
/// Returns ProposalListResponse
ReverseProposals {
start_before: Option<u64>,
limit: Option<u32>,
},
/// Returns VoteResponse
Vote { proposal_id: u64, voter: String },
/// Returns VoteListResponse
ListVotes {
proposal_id: u64,
start_after: Option<String>,
limit: Option<u32>,
},
/// Returns VoterResponse
Voter { address: String },
/// Returns VoterListResponse
ListVoters {
start_after: Option<String>,
limit: Option<u32>,
},
/// Returns address of current's group contract
GroupContract {},
}

pub struct VotingContract;

impl Contract<TgradeMsg> for VotingContract {
fn instantiate(
&self,
deps: DepsMut,
_env: Env,
_info: MessageInfo,
msg: Vec<u8>,
) -> anyhow::Result<cosmwasm_std::Response<TgradeMsg>> {
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<u8>,
) -> anyhow::Result<cosmwasm_std::Response<TgradeMsg>> {
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<u8>) -> anyhow::Result<Binary> {
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<u8>,
) -> anyhow::Result<cosmwasm_std::Response<TgradeMsg>> {
unimplemented!()
}

fn reply(
&self,
_deps: DepsMut,
_env: Env,
_msg: cosmwasm_std::Reply,
) -> anyhow::Result<cosmwasm_std::Response<TgradeMsg>> {
unimplemented!()
}

fn migrate(
&self,
_deps: DepsMut,
_env: Env,
_msg: Vec<u8>,
) -> anyhow::Result<cosmwasm_std::Response<TgradeMsg>> {
unimplemented!()
}
}
4 changes: 2 additions & 2 deletions packages/voting-contract/src/multitest/suite.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 551136a

Please sign in to comment.