Skip to content

Commit

Permalink
feat(interchain-token-service): apply scaling factor on token transfe…
Browse files Browse the repository at this point in the history
…r based on token decimals
  • Loading branch information
fish-sammy committed Nov 1, 2024
1 parent d631343 commit 905d6c9
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
82 changes: 81 additions & 1 deletion contracts/interchain-token-service/src/contract/execute.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use axelar_wasm_std::{killswitch, nonempty, FnExt, IntoContractError};
use cosmwasm_std::{DepsMut, HexBinary, QuerierWrapper, Response, Storage};
use cosmwasm_std::{DepsMut, HexBinary, QuerierWrapper, Response, Storage, Uint256};
use error_stack::{bail, ensure, report, Result, ResultExt};
use router_api::{Address, ChainName, ChainNameRaw, CrossChainId};

Expand Down Expand Up @@ -34,6 +34,14 @@ pub enum Error {
SaveChainConfig(ChainNameRaw),
#[error("chain {0} is frozen")]
ChainFrozen(ChainNameRaw),
#[error(
"invalid transfer amount {amount} from chain {source_chain} to chain {destination_chain}"
)]
InvalidTransferAmount {
source_chain: ChainNameRaw,
destination_chain: ChainNameRaw,
amount: nonempty::Uint256,
},
#[error("state error")]
State,
}
Expand Down Expand Up @@ -197,6 +205,78 @@ pub fn set_chain_config(
}
}

#[allow(unused)]
fn translate_amount_on_token_transfer(
storage: &dyn Storage,
src_chain: &ChainNameRaw,
dest_chain: &ChainNameRaw,
message: Message,
) -> Result<Message, Error> {
match message {

Check warning on line 215 in contracts/interchain-token-service/src/contract/execute.rs

View check run for this annotation

Codecov / codecov/patch

contracts/interchain-token-service/src/contract/execute.rs#L209-L215

Added lines #L209 - L215 were not covered by tests
Message::InterchainTransfer {
token_id,
source_address,
destination_address,
amount,
data,

Check warning on line 221 in contracts/interchain-token-service/src/contract/execute.rs

View check run for this annotation

Codecov / codecov/patch

contracts/interchain-token-service/src/contract/execute.rs#L217-L221

Added lines #L217 - L221 were not covered by tests
} => {
let src_chain_decimals = state::load_token_decimals(storage, src_chain, token_id)
.change_context(Error::State)?;
let dest_chain_decimals = state::load_token_decimals(storage, dest_chain, token_id)
.change_context(Error::State)?;

Check warning on line 226 in contracts/interchain-token-service/src/contract/execute.rs

View check run for this annotation

Codecov / codecov/patch

contracts/interchain-token-service/src/contract/execute.rs#L223-L226

Added lines #L223 - L226 were not covered by tests

let dest_chain_amount = if src_chain_decimals > dest_chain_decimals {
amount
.checked_div(
Uint256::from_u128(10)
.checked_pow((src_chain_decimals - dest_chain_decimals).into())

Check failure on line 232 in contracts/interchain-token-service/src/contract/execute.rs

View workflow job for this annotation

GitHub Actions / Lints

arithmetic operation that can potentially result in unexpected side-effects
.change_context_lazy(|| Error::InvalidTransferAmount {
source_chain: src_chain.to_owned(),
destination_chain: dest_chain.to_owned(),
amount,
})?,

Check warning on line 237 in contracts/interchain-token-service/src/contract/execute.rs

View check run for this annotation

Codecov / codecov/patch

contracts/interchain-token-service/src/contract/execute.rs#L228-L237

Added lines #L228 - L237 were not covered by tests
)
.change_context_lazy(|| Error::InvalidTransferAmount {
source_chain: src_chain.to_owned(),
destination_chain: dest_chain.to_owned(),
amount,
})?

Check warning on line 243 in contracts/interchain-token-service/src/contract/execute.rs

View check run for this annotation

Codecov / codecov/patch

contracts/interchain-token-service/src/contract/execute.rs#L239-L243

Added lines #L239 - L243 were not covered by tests
} else {
amount
.checked_mul(
Uint256::from_u128(10)
.checked_pow((dest_chain_decimals - src_chain_decimals).into())

Check failure on line 248 in contracts/interchain-token-service/src/contract/execute.rs

View workflow job for this annotation

GitHub Actions / Lints

arithmetic operation that can potentially result in unexpected side-effects
.change_context_lazy(|| Error::InvalidTransferAmount {
source_chain: src_chain.to_owned(),
destination_chain: dest_chain.to_owned(),
amount,
})?,

Check warning on line 253 in contracts/interchain-token-service/src/contract/execute.rs

View check run for this annotation

Codecov / codecov/patch

contracts/interchain-token-service/src/contract/execute.rs#L245-L253

Added lines #L245 - L253 were not covered by tests
)
.change_context_lazy(|| Error::InvalidTransferAmount {
source_chain: src_chain.to_owned(),
destination_chain: dest_chain.to_owned(),
amount,
})?

Check warning on line 259 in contracts/interchain-token-service/src/contract/execute.rs

View check run for this annotation

Codecov / codecov/patch

contracts/interchain-token-service/src/contract/execute.rs#L255-L259

Added lines #L255 - L259 were not covered by tests
};

Ok(Message::InterchainTransfer {
token_id,
source_address,
destination_address,
amount: nonempty::Uint256::try_from(dest_chain_amount).change_context_lazy(
|| Error::InvalidTransferAmount {
source_chain: src_chain.to_owned(),
destination_chain: dest_chain.to_owned(),
amount,
},
)?,
data,

Check warning on line 273 in contracts/interchain-token-service/src/contract/execute.rs

View check run for this annotation

Codecov / codecov/patch

contracts/interchain-token-service/src/contract/execute.rs#L263-L273

Added lines #L263 - L273 were not covered by tests
})
}
_ => Ok(message),

Check warning on line 276 in contracts/interchain-token-service/src/contract/execute.rs

View check run for this annotation

Codecov / codecov/patch

contracts/interchain-token-service/src/contract/execute.rs#L276

Added line #L276 was not covered by tests
}
}

Check warning on line 278 in contracts/interchain-token-service/src/contract/execute.rs

View check run for this annotation

Codecov / codecov/patch

contracts/interchain-token-service/src/contract/execute.rs#L278

Added line #L278 was not covered by tests

#[allow(unused)]
fn translate_and_save_token_decimals_on_token_deployment(
storage: &mut dyn Storage,
Expand Down
8 changes: 8 additions & 0 deletions contracts/interchain-token-service/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ pub fn save_token_decimals(
todo!()
}

pub fn load_token_decimals(
_storage: &dyn Storage,
_chain: &ChainNameRaw,
_token_id: TokenId,
) -> Result<u8, Error> {
todo!()

Check warning on line 107 in contracts/interchain-token-service/src/state.rs

View check run for this annotation

Codecov / codecov/patch

contracts/interchain-token-service/src/state.rs#L102-L107

Added lines #L102 - L107 were not covered by tests
}

pub fn may_load_its_contract(
storage: &dyn Storage,
chain: &ChainNameRaw,
Expand Down
6 changes: 6 additions & 0 deletions packages/axelar-wasm-std/src/nonempty/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ impl fmt::Display for Uint64 {
#[serde(try_from = "cosmwasm_std::Uint256")]
pub struct Uint256(cosmwasm_std::Uint256);

impl fmt::Display for Uint256 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}

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

Expand Down

0 comments on commit 905d6c9

Please sign in to comment.