Skip to content

Commit

Permalink
use nonempty uint256
Browse files Browse the repository at this point in the history
  • Loading branch information
milapsheth committed Oct 11, 2024
1 parent bbacc16 commit 09e1fef
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 38 deletions.
32 changes: 26 additions & 6 deletions contracts/interchain-token-service/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ impl Message {
destination_address: Vec::<u8>::from(decoded.destinationAddress)
.try_into()
.map_err(Error::NonEmpty)?,
amount: Uint256::from_le_bytes(decoded.amount.to_le_bytes()),
amount: Uint256::from_le_bytes(decoded.amount.to_le_bytes())
.try_into()
.map_err(Error::NonEmpty)?,
data: from_vec(decoded.data.into())?,
}
}
Expand Down Expand Up @@ -301,7 +303,7 @@ mod tests {
token_id: [0u8; 32].into(),
source_address: from_hex("00"),
destination_address: from_hex("00"),
amount: Uint256::zero(),
amount: 1u64.try_into().unwrap(),
data: None,
},
},
Expand All @@ -311,7 +313,7 @@ mod tests {
token_id: [255u8; 32].into(),
source_address: from_hex("4F4495243837681061C4743b74B3eEdf548D56A5"),
destination_address: from_hex("4F4495243837681061C4743b74B3eEdf548D56A5"),
amount: Uint256::MAX,
amount: Uint256::MAX.try_into().unwrap(),
data: Some(from_hex("abcd")),
},
},
Expand All @@ -321,7 +323,7 @@ mod tests {
token_id: [0u8; 32].into(),
source_address: from_hex("00"),
destination_address: from_hex("00"),
amount: Uint256::zero(),
amount: 1u64.try_into().unwrap(),
data: None,
},
},
Expand All @@ -331,7 +333,7 @@ mod tests {
token_id: [255u8; 32].into(),
source_address: from_hex("4F4495243837681061C4743b74B3eEdf548D56A5"),
destination_address: from_hex("4F4495243837681061C4743b74B3eEdf548D56A5"),
amount: Uint256::MAX,
amount: Uint256::MAX.try_into().unwrap(),
data: Some(from_hex("abcd")),
},
},
Expand Down Expand Up @@ -359,6 +361,24 @@ mod tests {
tokenId: FixedBytes::<32>::new([1u8; 32]),
sourceAddress: vec![1, 2].into(),
destinationAddress: vec![].into(),
amount: U256::from(1),
data: vec![].into(),
}
.abi_encode_params(),
InterchainTransfer {
messageType: MessageType::InterchainTransfer.into(),
tokenId: FixedBytes::<32>::new([1u8; 32]),
sourceAddress: vec![].into(),
destinationAddress: vec![1, 2].into(),
amount: U256::from(1),
data: vec![].into(),
}
.abi_encode_params(),
InterchainTransfer {
messageType: MessageType::InterchainTransfer.into(),
tokenId: FixedBytes::<32>::new([1u8; 32]),
sourceAddress: vec![1, 2].into(),
destinationAddress: vec![1, 2].into(),
amount: U256::from(0),
data: vec![].into(),
}
Expand Down Expand Up @@ -632,7 +652,7 @@ mod tests {
token_id: [0u8; 32].into(),
source_address: from_hex("1234"),
destination_address: from_hex("5678"),
amount: Uint256::from(1u128),
amount: Uint256::from(1u128).try_into().unwrap(),
data: Some(large_data.try_into().unwrap()),
},
};
Expand Down
4 changes: 2 additions & 2 deletions contracts/interchain-token-service/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ mod tests {
.unwrap()
.try_into()
.unwrap(),
amount: coin.amount.into(),
amount: coin.amount.try_into().unwrap(),
data: None,
},
};
Expand Down Expand Up @@ -284,7 +284,7 @@ mod tests {
.unwrap()
.try_into()
.unwrap(),
amount: coin.amount.into(),
amount: coin.amount.try_into().unwrap(),
data: None,
},
};
Expand Down
24 changes: 12 additions & 12 deletions contracts/interchain-token-service/src/contract/execute.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use axelar_core_std::nexus;
use axelar_wasm_std::{nonempty, FnExt, IntoContractError};
use cosmwasm_std::{Coin, DepsMut, HexBinary, QuerierWrapper, Response, Storage, Uint128};
use cosmwasm_std::{Coin, DepsMut, HexBinary, QuerierWrapper, Response, Storage, Uint128, Uint256};
use error_stack::{bail, ensure, report, Result, ResultExt};
use router_api::{Address, ChainName, ChainNameRaw, CrossChainId};
use sha3::{Digest, Keccak256};
Expand Down Expand Up @@ -158,7 +158,7 @@ fn gateway_token_transfer(
match (gateway_denom, message) {
(Some(denom), Message::InterchainTransfer { amount, .. }) => Ok(Some(Coin {
denom: denom.to_string(),
amount: Uint128::try_from(*amount).change_context(Error::TransferAmountOverflow)?,
amount: Uint128::try_from(Uint256::from(*amount)).change_context(Error::TransferAmountOverflow)?,
})),
_ => Ok(None),
}
Expand Down Expand Up @@ -333,7 +333,7 @@ mod tests {
.unwrap()
.try_into()
.unwrap(),
amount: coin.amount.into(),
amount: coin.amount.try_into().unwrap(),
data: None,
},
};
Expand All @@ -360,7 +360,7 @@ mod tests {
.unwrap()
.try_into()
.unwrap(),
amount: coin.amount.into(),
amount: coin.amount.try_into().unwrap(),
data: None,
},
};
Expand Down Expand Up @@ -412,7 +412,7 @@ mod tests {
.unwrap()
.try_into()
.unwrap(),
amount: coin.amount.into(),
amount: coin.amount.try_into().unwrap(),
data: None,
},
};
Expand Down Expand Up @@ -468,7 +468,7 @@ mod tests {
.unwrap()
.try_into()
.unwrap(),
amount: amount_in_msg.into(),
amount: amount_in_msg.try_into().unwrap(),
data: None,
},
};
Expand Down Expand Up @@ -525,7 +525,7 @@ mod tests {
.unwrap()
.try_into()
.unwrap(),
amount: coin.amount.into(),
amount: coin.amount.try_into().unwrap(),
data: None,
},
};
Expand Down Expand Up @@ -580,7 +580,7 @@ mod tests {
.unwrap()
.try_into()
.unwrap(),
amount: coin.amount.into(),
amount: coin.amount.try_into().unwrap(),
data: None,
},
};
Expand Down Expand Up @@ -635,7 +635,7 @@ mod tests {
.unwrap()
.try_into()
.unwrap(),
amount: coin.amount.into(),
amount: coin.amount.try_into().unwrap(),
data: None,
},
};
Expand Down Expand Up @@ -677,7 +677,7 @@ mod tests {
.unwrap()
.try_into()
.unwrap(),
amount: coin.amount.into(),
amount: coin.amount.try_into().unwrap(),
data: None,
},
};
Expand Down Expand Up @@ -728,7 +728,7 @@ mod tests {
.unwrap()
.try_into()
.unwrap(),
amount: Uint256::one(),
amount: Uint256::one().try_into().unwrap(),
data: None,
},
};
Expand Down Expand Up @@ -774,7 +774,7 @@ mod tests {
.unwrap()
.try_into()
.unwrap(),
amount: coin.amount.into(),
amount: coin.amount.try_into().unwrap(),
data: None,
},
};
Expand Down
6 changes: 3 additions & 3 deletions contracts/interchain-token-service/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ mod test {
token_id: TokenId::new([1; 32]),
source_address: HexBinary::from([1; 32]).try_into().unwrap(),
destination_address: HexBinary::from([1, 2, 3, 4]).try_into().unwrap(),
amount: 1u64.into(),
amount: 1u64.try_into().unwrap(),
data: Some(HexBinary::from([1, 2, 3, 4]).try_into().unwrap()),
},
Message::DeployInterchainToken {
Expand Down Expand Up @@ -144,14 +144,14 @@ mod test {
token_id: TokenId::new([1; 32]),
source_address: HexBinary::from([1; 32]).try_into().unwrap(),
destination_address: HexBinary::from([1, 2, 3, 4]).try_into().unwrap(),
amount: 1u64.into(),
amount: 1u64.try_into().unwrap(),
data: None,
},
Message::InterchainTransfer {
token_id: TokenId::new([1; 32]),
source_address: HexBinary::from([0u8]).try_into().unwrap(),
destination_address: HexBinary::from([0u8]).try_into().unwrap(),
amount: 1u64.into(),
amount: 1u64.try_into().unwrap(),
data: None,
},
Message::DeployInterchainToken {
Expand Down
10 changes: 1 addition & 9 deletions contracts/interchain-token-service/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::fmt::Display;

use axelar_wasm_std::nonempty;
use cosmwasm_schema::cw_serde;
use cosmwasm_std::Uint256;
use cw_storage_plus::{Key, KeyDeserialize, Prefixer, PrimaryKey};
use router_api::ChainNameRaw;
use strum::FromRepr;
Expand Down Expand Up @@ -50,7 +49,7 @@ pub enum Message {
/// If data is not empty, this address will given the token and executed as a contract on the destination chain
destination_address: nonempty::HexBinary,
/// The amount of tokens to transfer
amount: Uint256,
amount: nonempty::Uint256,
/// An optional payload to be provided to the destination address, if `data` is not empty
data: Option<nonempty::HexBinary>,
},
Expand Down Expand Up @@ -114,13 +113,6 @@ impl Message {
| Message::DeployTokenManager { token_id, .. } => token_id.clone(),
}
}

pub fn transfer_amount(&self) -> Option<Uint256> {
match self {
Message::InterchainTransfer { amount, .. } => Some(amount.to_owned()),
_ => None,
}
}
}

impl TokenId {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
"0000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000005636861696e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"0000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000005636861696e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"0000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000005636861696e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000000000000000000000000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000100ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000144f4495243837681061c4743b74b3eedf548d56a500000000000000000000000000000000000000000000000000000000000000000000000000000000000000144f4495243837681061c4743b74b3eedf548d56a50000000000000000000000000000000000000000000000000000000000000000000000000000000000000002abcd000000000000000000000000000000000000000000000000000000000000",
"0000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000005636861696e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"0000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000005636861696e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"0000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000005636861696e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000000000000000000000000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000100ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000144f4495243837681061c4743b74b3eedf548d56a500000000000000000000000000000000000000000000000000000000000000000000000000000000000000144f4495243837681061c4743b74b3eedf548d56a50000000000000000000000000000000000000000000000000000000000000000000000000000000000000002abcd000000000000000000000000000000000000000000000000000000000000"
]
2 changes: 1 addition & 1 deletion contracts/interchain-token-service/tests/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ fn execute_hub_message_succeeds() {
token_id: token_id.clone(),
source_address: HexBinary::from([1; 32]).try_into().unwrap(),
destination_address: HexBinary::from([2; 32]).try_into().unwrap(),
amount: 1u64.into(),
amount: 1u64.try_into().unwrap(),
data: Some(HexBinary::from([1, 2, 3, 4]).try_into().unwrap()),
},
Message::DeployInterchainToken {
Expand Down
4 changes: 2 additions & 2 deletions contracts/interchain-token-service/tests/utils/messages.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cosmwasm_std::{HexBinary, Uint256};
use cosmwasm_std::HexBinary;
use interchain_token_service::{HubMessage, Message, TokenId};
use router_api::{Address, ChainNameRaw, CrossChainId};

Expand All @@ -7,7 +7,7 @@ pub fn dummy_message() -> Message {
token_id: TokenId::new([2; 32]),
source_address: HexBinary::from_hex("1234").unwrap().try_into().unwrap(),
destination_address: HexBinary::from_hex("5678").unwrap().try_into().unwrap(),
amount: Uint256::from(1000u64),
amount: 1000u64.try_into().unwrap(),
data: Some(HexBinary::from_hex("abcd").unwrap().try_into().unwrap()),
}
}
Expand Down
18 changes: 17 additions & 1 deletion packages/axelar-wasm-std/src/nonempty/uint.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fmt;
use std::{fmt, ops::Deref};

use cosmwasm_schema::cw_serde;
use into_inner_derive::IntoInner;
Expand Down Expand Up @@ -72,6 +72,14 @@ impl From<Uint256> for cosmwasm_std::Uint256 {
}
}

impl TryFrom<u64> for Uint256 {
type Error = Error;

fn try_from(value: u64) -> Result<Self, Self::Error> {
cosmwasm_std::Uint256::from(value).try_into()
}
}

impl TryFrom<cosmwasm_std::Uint128> for Uint256 {
type Error = Error;

Expand All @@ -84,6 +92,14 @@ impl TryFrom<cosmwasm_std::Uint128> for Uint256 {
}
}

impl Deref for Uint256 {
type Target = cosmwasm_std::Uint256;

fn deref(&self) -> &Self::Target {
&self.0
}
}

#[cw_serde]
#[derive(Copy, PartialOrd, Eq, IntoInner)]
pub struct Uint128(cosmwasm_std::Uint128);
Expand Down

0 comments on commit 09e1fef

Please sign in to comment.