Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

Commit

Permalink
refactor: Refactor features
Browse files Browse the repository at this point in the history
  • Loading branch information
code0xff committed Jul 7, 2024
1 parent d20f3e0 commit 70aaf02
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 28 deletions.
24 changes: 6 additions & 18 deletions primitives/cosmos/src/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,47 +15,37 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#[cfg(feature = "with-serde")]
use crate::error::DecodeTxError;
#[cfg(feature = "with-serde")]
use crate::SequenceNumber;
#[cfg(feature = "with-serde")]
use crate::{error::DecodeTxError, SequenceNumber};
use serde::{Deserialize, Serialize};
use serde_json::Value;
#[cfg(feature = "with-serde")]
use sp_core::hashing::sha2_256;

#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Coin {
pub amount: String,
pub denom: String,
}

#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct AminoSignFee {
pub amount: Vec<Coin>,
pub gas: String,
}

#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct MsgSend {
pub amount: Vec<Coin>,
pub from_address: String,
pub to_address: String,
}

#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Any {
pub r#type: String,
pub value: Value,
}

#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct AminoSignDoc {
pub account_number: String,
pub chain_id: String,
Expand All @@ -65,7 +55,6 @@ pub struct AminoSignDoc {
pub sequence: String,
}

#[cfg(all(feature = "std", feature = "with-serde"))]
impl AminoSignDoc {
pub fn new(
tx: &cosmrs::Tx,
Expand Down Expand Up @@ -131,7 +120,6 @@ impl AminoSignDoc {
}

#[cfg(test)]
#[cfg(feature = "with-serde")]
mod tests {
use super::AminoSignDoc;
use base64ct::{Base64, Encoding};
Expand Down
8 changes: 4 additions & 4 deletions primitives/cosmos/src/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ impl TryFrom<Any> for MsgSend {

#[cfg(all(feature = "std", feature = "with-codec"))]
pub fn to_scale(type_url: &[u8], value: &[u8]) -> Result<(Vec<u8>, Vec<u8>), DecodeMsgError> {
match core::str::from_utf8(type_url).map_err(|_| DecodeMsgError::InvalidTypeUrl)? {
"/cosmos.bank.v1beta1.MsgSend" => {
match type_url {
b"/cosmos.bank.v1beta1.MsgSend" => {
let any = Any { type_url: type_url.to_vec(), value: value.to_vec() };
let msg_send: MsgSend = any.try_into()?;
Ok((type_url.to_vec(), msg_send.encode()))
Expand All @@ -84,8 +84,8 @@ pub fn get_msg_any_signers(
type_url: &[u8],
value: &[u8],
) -> Result<Vec<AccountId>, DecodeMsgError> {
match core::str::from_utf8(type_url).map_err(|_| DecodeMsgError::InvalidTypeUrl)? {
"/cosmos.bank.v1beta1.MsgSend" => {
match type_url {
b"/cosmos.bank.v1beta1.MsgSend" => {
let any = Any { type_url: type_url.to_vec(), value: value.to_vec() };
let msg_send: MsgSend = any.try_into()?;
Ok(msg_send.get_signers())
Expand Down
5 changes: 1 addition & 4 deletions primitives/cosmos/src/sign_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::error::DecodeTxError;
#[cfg(feature = "with-serde")]
use crate::{legacy::AminoSignDoc, SequenceNumber};
use crate::{error::DecodeTxError, legacy::AminoSignDoc, SequenceNumber};
use core::str::FromStr;
use cosmrs::tendermint::chain;
use sp_core::sha2_256;
Expand All @@ -37,7 +35,6 @@ pub fn get_signer_doc_bytes(
Ok(sha2_256(&sign_doc_bytes))
}

#[cfg(feature = "with-serde")]
pub fn get_amino_signer_doc_bytes(
tx_bytes: &[u8],
chain_id: &[u8],
Expand Down
4 changes: 2 additions & 2 deletions template/runtime/src/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ where
T: frame_system::Config + pallet_cosmos::Config,
{
fn route(type_url: &[u8]) -> Option<sp_std::boxed::Box<dyn MsgHandler>> {
match core::str::from_utf8(type_url).unwrap() {
"/cosmos.bank.v1beta1.MsgSend" =>
match type_url {
b"/cosmos.bank.v1beta1.MsgSend" =>
Some(sp_std::boxed::Box::<MsgSendHandler<T>>::default()),
_ => None,
}
Expand Down

0 comments on commit 70aaf02

Please sign in to comment.