Skip to content

Commit

Permalink
refactor(contract): generate Vote enum variant strings with a macro (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
cgorenflo authored Sep 20, 2024
1 parent a639197 commit 1983493
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 19 deletions.
3 changes: 1 addition & 2 deletions contracts/interchain-token-service/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,10 @@ fn make_message_event(
destination_chain: ChainNameRaw,
msg: Message,
) -> cosmwasm_std::Event {
let message_type: &'static str = (&msg).into();
let mut attrs = vec![
Attribute::new("cc_id", cc_id.to_string()),
Attribute::new("destination_chain", destination_chain.to_string()),
Attribute::new("message_type", message_type.to_string()),
Attribute::new("message_type", msg.as_ref().to_string()),
];

match msg {
Expand Down
2 changes: 1 addition & 1 deletion contracts/interchain-token-service/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub enum TokenManagerType {
/// A message sent between ITS contracts to facilitate interchain transfers, token deployments, or token manager deployments.
/// `Message` routed via the ITS hub get wrapped inside a [`HubMessage`]
#[cw_serde]
#[derive(Eq, strum::IntoStaticStr)]
#[derive(Eq, strum::AsRefStr)]
pub enum Message {
/// Transfer ITS tokens between different chains
InterchainTransfer {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
"SucceededOnChain",
"FailedOnChain",
"NotFound"
]
29 changes: 13 additions & 16 deletions packages/axelar-wasm-std/src/voting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use cosmwasm_schema::cw_serde;
use cosmwasm_std::{Addr, StdError, StdResult, Uint128, Uint64};
use cw_storage_plus::{IntKey, Key, KeyDeserialize, Prefixer, PrimaryKey};
use num_traits::{CheckedAdd, One};
use strum::{EnumIter, EnumString, IntoEnumIterator};
use strum::{AsRefStr, EnumIter, EnumString, IntoEnumIterator};
use thiserror::Error;
use valuable::Valuable;

Expand Down Expand Up @@ -153,23 +153,13 @@ impl fmt::Display for PollId {
}

#[cw_serde]
#[derive(Eq, Hash, Ord, PartialOrd, EnumIter, EnumString, Valuable)]
#[derive(Eq, Hash, Ord, PartialOrd, EnumIter, EnumString, AsRefStr, Valuable)]
pub enum Vote {
SucceededOnChain, // the txn was included on chain, and achieved the intended result
FailedOnChain, // the txn was included on chain, but failed to achieve the intended result
NotFound, // the txn could not be found on chain in any blocks at the time of voting
}

impl fmt::Display for Vote {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Vote::SucceededOnChain => write!(f, "SucceededOnChain"),
Vote::FailedOnChain => write!(f, "FailedOnChain"),
Vote::NotFound => write!(f, "NotFound"),
}
}
}

// Deserialization of enums as map keys is not supported by serde-json-wasm, we use String instead
#[cw_serde]
pub struct Tallies(BTreeMap<String, Uint128>);
Expand All @@ -178,7 +168,7 @@ impl Default for Tallies {
fn default() -> Self {
Self(
Vote::iter()
.map(|vote| (vote.to_string(), Uint128::zero()))
.map(|vote| (vote.as_ref().to_string(), Uint128::zero()))
.collect(),
)
}
Expand All @@ -196,15 +186,15 @@ impl Tallies {
}

pub fn tally(&mut self, vote: &Vote, weight: &Uint128) {
let key = vote.to_string();
let key = vote.as_ref();

let tally = self
.0
.get(&key)
.get(key)
.unwrap_or(&Uint128::zero())
.saturating_add(*weight);

self.0.insert(key, tally);
self.0.insert(key.to_string(), tally);
}
}

Expand Down Expand Up @@ -416,6 +406,13 @@ mod tests {
use super::*;
use crate::{nonempty, Participant, Threshold};

#[test]
fn vote_strings_as_expected() {
goldie::assert_json!(Vote::iter()
.map(|vote| vote.as_ref().to_string())
.collect::<Vec<_>>());
}

#[test]
fn cast_vote() {
let poll = new_poll(2, 2, vec!["addr1", "addr2"]);
Expand Down

0 comments on commit 1983493

Please sign in to comment.