From dbc1bf6955c1a0758f1ddb5d69f92f8079e5e770 Mon Sep 17 00:00:00 2001 From: Evan Gray Date: Wed, 6 Mar 2024 10:10:16 -0500 Subject: [PATCH] sdk/rust: shared accountant ModificationKind --- .../global-accountant/src/contract.rs | 10 ++-- .../global-accountant/tests/helpers/mod.rs | 3 +- .../ntt-global-accountant/src/contract.rs | 7 +-- .../tests/helpers/mod.rs | 4 +- sdk/rust/core/src/accountant.rs | 50 +------------------ sdk/rust/core/src/accountant_modification.rs | 47 +++++++++++++++++ sdk/rust/core/src/lib.rs | 1 + sdk/rust/core/src/ntt_accountant.rs | 50 +------------------ 8 files changed, 66 insertions(+), 106 deletions(-) create mode 100644 sdk/rust/core/src/accountant_modification.rs diff --git a/cosmwasm/contracts/global-accountant/src/contract.rs b/cosmwasm/contracts/global-accountant/src/contract.rs index b44d9413c..63b35352b 100644 --- a/cosmwasm/contracts/global-accountant/src/contract.rs +++ b/cosmwasm/contracts/global-accountant/src/contract.rs @@ -18,7 +18,9 @@ use serde_wormhole::RawMessage; use tinyvec::{Array, TinyVec}; use wormhole_bindings::WormholeQuery; use wormhole_sdk::{ - accountant as accountant_module, token, + accountant as accountant_module, + accountant_modification::ModificationKind, + token, vaa::{self, Body, Header, Signature}, Chain, }; @@ -416,9 +418,9 @@ fn handle_accountant_governance_vaa( } => { let token_address = TokenAddress::new(token_address.0); let kind = match kind { - accountant_module::ModificationKind::Add => Kind::Add, - accountant_module::ModificationKind::Subtract => Kind::Sub, - accountant_module::ModificationKind::Unknown => { + ModificationKind::Add => Kind::Add, + ModificationKind::Subtract => Kind::Sub, + ModificationKind::Unknown => { bail!("unsupported governance action") } }; diff --git a/cosmwasm/contracts/global-accountant/tests/helpers/mod.rs b/cosmwasm/contracts/global-accountant/tests/helpers/mod.rs index 6c5e582ce..b1e18d25c 100644 --- a/cosmwasm/contracts/global-accountant/tests/helpers/mod.rs +++ b/cosmwasm/contracts/global-accountant/tests/helpers/mod.rs @@ -20,7 +20,8 @@ use global_accountant::{ use serde::Serialize; use wormhole_bindings::{fake, WormholeQuery}; use wormhole_sdk::{ - accountant::{self as accountant_module, ModificationKind}, + accountant as accountant_module, + accountant_modification::ModificationKind, token, vaa::{Body, Header, Signature}, Address, Amount, Chain, Vaa, diff --git a/cosmwasm/contracts/ntt-global-accountant/src/contract.rs b/cosmwasm/contracts/ntt-global-accountant/src/contract.rs index 99e17ce26..1c39484a3 100644 --- a/cosmwasm/contracts/ntt-global-accountant/src/contract.rs +++ b/cosmwasm/contracts/ntt-global-accountant/src/contract.rs @@ -28,6 +28,7 @@ use tinyvec::{Array, TinyVec}; use wormhole_bindings::WormholeQuery; use wormhole_io::TypePrefixedPayload; use wormhole_sdk::{ + accountant_modification::ModificationKind, ntt_accountant as ntt_accountant_module, relayer, vaa::{self, Body, Header, Signature}, Chain, @@ -465,9 +466,9 @@ fn handle_accountant_governance_vaa( } => { let token_address = TokenAddress::new(token_address.0); let kind = match kind { - ntt_accountant_module::ModificationKind::Add => Kind::Add, - ntt_accountant_module::ModificationKind::Subtract => Kind::Sub, - ntt_accountant_module::ModificationKind::Unknown => { + ModificationKind::Add => Kind::Add, + ModificationKind::Subtract => Kind::Sub, + ModificationKind::Unknown => { bail!("unsupported governance action") } }; diff --git a/cosmwasm/contracts/ntt-global-accountant/tests/helpers/mod.rs b/cosmwasm/contracts/ntt-global-accountant/tests/helpers/mod.rs index 04f3c9678..9a4a209f6 100644 --- a/cosmwasm/contracts/ntt-global-accountant/tests/helpers/mod.rs +++ b/cosmwasm/contracts/ntt-global-accountant/tests/helpers/mod.rs @@ -20,8 +20,8 @@ use ntt_global_accountant::{ use serde::Serialize; use wormhole_bindings::{fake, WormholeQuery}; use wormhole_sdk::{ - ntt_accountant::{self as ntt_accountant_module, ModificationKind}, - relayer, + accountant_modification::ModificationKind, + ntt_accountant as ntt_accountant_module, relayer, vaa::{Body, Header, Signature}, Address, Amount, Chain, Vaa, }; diff --git a/sdk/rust/core/src/accountant.rs b/sdk/rust/core/src/accountant.rs index 3f7267ef5..db3031f4b 100644 --- a/sdk/rust/core/src/accountant.rs +++ b/sdk/rust/core/src/accountant.rs @@ -4,55 +4,9 @@ //! It needs a modify_balance message to be able to correct for unforeseen events. use bstr::BString; -use serde::{Deserialize, Deserializer, Serialize, Serializer}; +use serde::{Deserialize, Serialize}; -use crate::{Address, Amount, Chain}; - -#[repr(u8)] -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)] -pub enum ModificationKind { - Unknown = 0, - Add = 1, - Subtract = 2, -} - -impl From for ModificationKind { - fn from(other: u8) -> ModificationKind { - match other { - 1 => ModificationKind::Add, - 2 => ModificationKind::Subtract, - _ => ModificationKind::Unknown, - } - } -} - -impl From for u8 { - fn from(other: ModificationKind) -> u8 { - match other { - ModificationKind::Unknown => 0, - ModificationKind::Add => 1, - ModificationKind::Subtract => 2, - } - } -} - -impl Serialize for ModificationKind { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_u8((*self).into()) - } -} - -impl<'de> Deserialize<'de> for ModificationKind { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - ::deserialize(deserializer).map(Self::from) - } -} +use crate::{accountant_modification::ModificationKind, Address, Amount, Chain}; /// Represents a governance action targeted at the Accountant. #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] diff --git a/sdk/rust/core/src/accountant_modification.rs b/sdk/rust/core/src/accountant_modification.rs new file mode 100644 index 000000000..8d090c1b6 --- /dev/null +++ b/sdk/rust/core/src/accountant_modification.rs @@ -0,0 +1,47 @@ +use serde::{Deserialize, Deserializer, Serialize, Serializer}; + +#[repr(u8)] +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)] +pub enum ModificationKind { + Unknown = 0, + Add = 1, + Subtract = 2, +} + +impl From for ModificationKind { + fn from(other: u8) -> ModificationKind { + match other { + 1 => ModificationKind::Add, + 2 => ModificationKind::Subtract, + _ => ModificationKind::Unknown, + } + } +} + +impl From for u8 { + fn from(other: ModificationKind) -> u8 { + match other { + ModificationKind::Unknown => 0, + ModificationKind::Add => 1, + ModificationKind::Subtract => 2, + } + } +} + +impl Serialize for ModificationKind { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + serializer.serialize_u8((*self).into()) + } +} + +impl<'de> Deserialize<'de> for ModificationKind { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + ::deserialize(deserializer).map(Self::from) + } +} diff --git a/sdk/rust/core/src/lib.rs b/sdk/rust/core/src/lib.rs index b3652718f..be2840e25 100644 --- a/sdk/rust/core/src/lib.rs +++ b/sdk/rust/core/src/lib.rs @@ -16,6 +16,7 @@ use std::fmt; use serde::{Deserialize, Serialize}; pub mod accountant; +pub mod accountant_modification; mod arraystring; mod chain; pub mod core; diff --git a/sdk/rust/core/src/ntt_accountant.rs b/sdk/rust/core/src/ntt_accountant.rs index d9f277066..6c86b3644 100644 --- a/sdk/rust/core/src/ntt_accountant.rs +++ b/sdk/rust/core/src/ntt_accountant.rs @@ -4,55 +4,9 @@ //! It needs a modify_balance message to be able to correct for unforeseen events. use bstr::BString; -use serde::{Deserialize, Deserializer, Serialize, Serializer}; +use serde::{Deserialize, Serialize}; -use crate::{Address, Amount, Chain}; - -#[repr(u8)] -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)] -pub enum ModificationKind { - Unknown = 0, - Add = 1, - Subtract = 2, -} - -impl From for ModificationKind { - fn from(other: u8) -> ModificationKind { - match other { - 1 => ModificationKind::Add, - 2 => ModificationKind::Subtract, - _ => ModificationKind::Unknown, - } - } -} - -impl From for u8 { - fn from(other: ModificationKind) -> u8 { - match other { - ModificationKind::Unknown => 0, - ModificationKind::Add => 1, - ModificationKind::Subtract => 2, - } - } -} - -impl Serialize for ModificationKind { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_u8((*self).into()) - } -} - -impl<'de> Deserialize<'de> for ModificationKind { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - ::deserialize(deserializer).map(Self::from) - } -} +use crate::{accountant_modification::ModificationKind, Address, Amount, Chain}; /// Represents a governance action targeted at the Accountant. #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]