Skip to content

Commit

Permalink
extract translate_amount function
Browse files Browse the repository at this point in the history
  • Loading branch information
fish-sammy committed Nov 1, 2024
1 parent 08c2475 commit 3714ab1
Showing 1 changed file with 79 additions and 60 deletions.
139 changes: 79 additions & 60 deletions contracts/interchain-token-service/src/contract/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,75 @@ pub fn set_chain_config(
}
}

fn translate_amount(
amount: nonempty::Uint256,
src_chain: &ChainNameRaw,
dest_chain: &ChainNameRaw,
src_chain_decimals: u8,
dest_chain_decimals: u8,
dest_chain_max_uint: nonempty::Uint256,
) -> Result<nonempty::Uint256, Error> {

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#L208-L215

Added lines #L208 - L215 were not covered by tests
// dest_chain_amount = amount * 10 ^ (dest_chain_decimals - src_chain_decimals)
// It's intentionally written in this way since the end result may still be fine even if
// 1) amount * (10 ^ (dest_chain_decimals)) overflows
// 2) amount / (10 ^ (src_chain_decimals)) is zero
let dest_chain_amount = if src_chain_decimals > dest_chain_decimals {
amount
.checked_div(
Uint256::from_u128(10)
.checked_pow(
src_chain_decimals
.checked_sub(dest_chain_decimals)
.expect("decimals subtraction overflow")
.into(),
)
.change_context_lazy(|| Error::InvalidTransferAmount {
source_chain: src_chain.to_owned(),
destination_chain: dest_chain.to_owned(),
amount,
})?,

Check warning on line 234 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#L220-L234

Added lines #L220 - L234 were not covered by tests
)
.expect("(10 ^ (src_chain_decimals-dest_chain_decimals)) must be non-zero")

Check warning on line 236 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#L236

Added line #L236 was not covered by tests
} else {
amount
.checked_mul(
Uint256::from_u128(10)
.checked_pow(
dest_chain_decimals
.checked_sub(src_chain_decimals)
.expect("decimals subtraction overflow")
.into(),
)
.change_context_lazy(|| Error::InvalidTransferAmount {
source_chain: src_chain.to_owned(),
destination_chain: dest_chain.to_owned(),
amount,
})?,

Check warning on line 251 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#L238-L251

Added lines #L238 - L251 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 257 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#L253-L257

Added lines #L253 - L257 were not covered by tests
};

if dest_chain_amount.gt(&dest_chain_max_uint) {
bail!(Error::InvalidTransferAmount {
source_chain: src_chain.to_owned(),
destination_chain: dest_chain.to_owned(),
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,
}
})
}

Check warning on line 275 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#L260-L275

Added lines #L260 - L275 were not covered by tests

#[allow(unused)]
fn translate_amount_on_token_transfer(
storage: &dyn Storage,
Expand All @@ -224,71 +293,21 @@ fn translate_amount_on_token_transfer(
.change_context(Error::State)?;
let dest_chain_decimals = state::load_token_decimals(storage, dest_chain, token_id)
.change_context(Error::State)?;
let dest_chain_config =
state::load_chain_config(storage, dest_chain).change_context(Error::State)?;

// dest_chain_amount = amount * 10 ^ (dest_chain_decimals - src_chain_decimals)
// It's intentionally written in this way since the end result may still be fine even if
// 1) amount * (10 ^ (dest_chain_decimals)) overflows
// 2) amount / (10 ^ (src_chain_decimals)) is zero
let dest_chain_amount = if src_chain_decimals > dest_chain_decimals {
amount
.checked_div(
Uint256::from_u128(10)
.checked_pow(
src_chain_decimals
.checked_sub(dest_chain_decimals)
.expect("decimals subtraction overflow")
.into(),
)
.change_context_lazy(|| Error::InvalidTransferAmount {
source_chain: src_chain.to_owned(),
destination_chain: dest_chain.to_owned(),
amount,
})?,
)
.expect("(10 ^ (src_chain_decimals-dest_chain_decimals)) must be non-zero")
} else {
amount
.checked_mul(
Uint256::from_u128(10)
.checked_pow(
dest_chain_decimals
.checked_sub(src_chain_decimals)
.expect("decimals subtraction overflow")
.into(),
)
.change_context_lazy(|| Error::InvalidTransferAmount {
source_chain: src_chain.to_owned(),
destination_chain: dest_chain.to_owned(),
amount,
})?,
)
.change_context_lazy(|| Error::InvalidTransferAmount {
source_chain: src_chain.to_owned(),
destination_chain: dest_chain.to_owned(),
amount,
})?
};

if dest_chain_amount.gt(&dest_chain_config.max_uint) {
bail!(Error::InvalidTransferAmount {
source_chain: src_chain.to_owned(),
destination_chain: dest_chain.to_owned(),
amount,
})
}
let dest_chain_max_uint = state::load_chain_config(storage, dest_chain)
.change_context(Error::State)?

Check warning on line 297 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#L292-L297

Added lines #L292 - L297 were not covered by tests
.max_uint;

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,
},
amount: translate_amount(
amount,
src_chain,
dest_chain,
src_chain_decimals,
src_chain_decimals,
dest_chain_max_uint,
)?,
data,

Check warning on line 312 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#L301-L312

Added lines #L301 - L312 were not covered by tests
})
Expand Down

0 comments on commit 3714ab1

Please sign in to comment.