Skip to content

Commit

Permalink
fix: update voting-with-uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
loloicci committed Dec 16, 2021
1 parent a3de3ac commit edcc939
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 59 deletions.
4 changes: 2 additions & 2 deletions contracts/voting-with-uuid/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ backtraces = ["cosmwasm-std/backtraces", "cosmwasm-vm/backtraces"]


[dependencies]
cosmwasm-std = { path = "../../packages/std" }
cosmwasm-std = { path = "../../packages/std", features = ["iterator"] }
cosmwasm-storage = { path = "../../packages/storage"}
schemars = "0.8.1"
serde = { version = "1.0.125", default-features = false, features = ["derive"] }
Expand All @@ -41,4 +41,4 @@ thiserror = { version = "1.0.23" }

[dev-dependencies]
cosmwasm-schema = { path = "../../packages/schema" }
cosmwasm-vm = { path = "../../packages/vm", default-features = false }
cosmwasm-vm = { path = "../../packages/vm", default-features = false, features = ["iterator"] }
75 changes: 25 additions & 50 deletions contracts/voting-with-uuid/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,19 +208,14 @@ pub fn create_poll(
};
poll(deps.storage).save(poll_id.as_slice(), &new_poll)?;

let r = Response {
submessages: vec![],
messages: vec![],
attributes: vec![
attr("action", "create_poll"),
attr("creator", new_poll.creator),
attr("poll_id", poll_id.to_string()),
attr("quorum_percentage", quorum_percentage.unwrap_or(0)),
attr("end_height", new_poll.end_height),
attr("start_height", start_height.unwrap_or(0)),
],
data: Some(to_binary(&CreatePollResponse { poll_id })?),
};
let r = Response::new()
.add_attribute("action", "create_poll")
.add_attribute("creator", new_poll.creator)
.add_attribute("poll_id", poll_id.to_string())
.add_attribute("quorum_percentage", quorum_percentage.unwrap_or(0).to_string())
.add_attribute("end_height", new_poll.end_height.to_string().to_string())
.add_attribute("start_height", start_height.unwrap_or(0).to_string())
.set_data(to_binary(&CreatePollResponse { poll_id })?);
Ok(r)
}

Expand Down Expand Up @@ -316,16 +311,10 @@ pub fn end_poll(
attr("action", "end_poll"),
attr("poll_id", poll_id.to_string()),
attr("rejected_reason", rejected_reason),
attr("passed", &passed),
attr("passed", &passed.to_string()),
];

let r = Response {
submessages: vec![],
messages: vec![],
attributes,
data: None,
};
Ok(r)
Ok(Response::new().add_attributes(attributes))
}

// unlock voter's tokens in a given poll
Expand Down Expand Up @@ -401,41 +390,30 @@ pub fn cast_vote(
let attributes = vec![
attr("action", "vote_casted"),
attr("poll_id", poll_id.to_string()),
attr("weight", &weight),
attr("weight", &weight.to_string()),
attr("voter", &info.sender),
];

let r = Response {
submessages: vec![],
messages: vec![],
attributes,
data: None,
};
Ok(r)
Ok(Response::new().add_attributes(attributes))
}

fn send_tokens(to_address: &Addr, amount: Vec<Coin>, action: &str) -> Response {
let attributes = vec![attr("action", action), attr("to", to_address.clone())];

Response {
submessages: vec![],
messages: vec![CosmosMsg::Bank(BankMsg::Send {
to_address: to_address.to_string(),
amount,
})],
attributes,
data: None,
}
Response::new()
.add_message(
CosmosMsg::Bank(BankMsg::Send {
to_address: to_address.to_string(),
amount,
}))
.add_attributes(attributes)
}

pub fn make_uuid(deps: DepsMut, env: Env, _info: MessageInfo) -> Result<Response, ContractError> {
let uuid = new_uuid(&env, deps.storage, deps.api)?;
let r = Response {
submessages: vec![],
messages: vec![],
attributes: vec![attr("action", "make_uuid"), attr("uuid", uuid.to_string())],
data: None,
};
let r = Response::new()
.add_attribute("action", "make_uuid")
.add_attribute("uuid", uuid.to_string());
Ok(r)
}

Expand All @@ -446,12 +424,9 @@ pub fn make_seq_id(
) -> Result<Response, ContractError> {
let seq_id: u64 = 0;

let r = Response {
submessages: vec![],
messages: vec![],
attributes: vec![attr("action", "make_seq_id"), attr("uuid", seq_id)],
data: None,
};
let r = Response::new()
.add_attribute("action", "make_seq_id")
.add_attribute("uuid", seq_id.to_string());
Ok(r)
}

Expand Down
14 changes: 7 additions & 7 deletions contracts/voting-with-uuid/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,8 +692,8 @@ mod tests {
let msg = execute_res.messages.get(0).expect("no message");

assert_eq!(
msg,
&CosmosMsg::Bank(BankMsg::Send {
msg.msg,
CosmosMsg::Bank(BankMsg::Send {
to_address: TEST_VOTER.to_string(),
amount: coins(11, VOTING_TOKEN),
})
Expand Down Expand Up @@ -750,7 +750,7 @@ mod tests {
match res {
Ok(_) => panic!("Must return error"),
Err(ContractError::ExcessiveWithdraw { max_amount }) => {
assert_eq!(max_amount, Uint128(10))
assert_eq!(max_amount, Uint128::from(10u32))
}
Err(e) => panic!("Unexpected error: {:?}", e),
}
Expand Down Expand Up @@ -904,9 +904,9 @@ mod tests {
attr("action", "create_poll"),
attr("creator", creator),
attr("poll_id", poll_id.to_string()),
attr("quorum_percentage", quorum),
attr("end_height", end_height),
attr("start_height", start_height),
attr("quorum_percentage", quorum.to_string()),
attr("end_height", end_height.to_string()),
attr("start_height", start_height.to_string()),
]
);

Expand Down Expand Up @@ -942,7 +942,7 @@ mod tests {
vec![
attr("action", "vote_casted"),
attr("poll_id", poll_id.to_string()),
attr("weight", weight),
attr("weight", weight.to_string()),
attr("voter", voter),
]
);
Expand Down

0 comments on commit edcc939

Please sign in to comment.