Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
fish-sammy committed Nov 8, 2024
1 parent 4a8c453 commit 4049046
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ pub fn apply_scaling_factor_to_amount(
storage: &dyn Storage,
source_chain: &ChainNameRaw,
destination_chain: &ChainNameRaw,
transfer: &mut InterchainTransfer,
) -> Result<(), Error> {
mut transfer: InterchainTransfer,
) -> Result<InterchainTransfer, Error> {
transfer.amount = destination_amount(
storage,
source_chain,
Expand All @@ -59,7 +59,7 @@ pub fn apply_scaling_factor_to_amount(
transfer.amount,
)?;

Ok(())
Ok(transfer)
}

pub fn save_token_instance_for_source_chain(
Expand Down Expand Up @@ -129,16 +129,16 @@ pub fn calculate_scaling_factor(
storage: &dyn Storage,
source_chain: &ChainNameRaw,
destination_chain: &ChainNameRaw,
deploy_token: &mut DeployInterchainToken,
) -> Result<(), Error> {
mut deploy_token: DeployInterchainToken,
) -> Result<DeployInterchainToken, Error> {
deploy_token.decimals = destination_token_decimals(
storage,
source_chain,
destination_chain,
deploy_token.decimals,
)?;

Ok(())
Ok(deploy_token)
}

fn ensure_matching_original_deployment(
Expand Down Expand Up @@ -327,7 +327,7 @@ mod test {
let mut storage = MockStorage::new();
let source_chain: ChainNameRaw = "sourcechain".try_into().unwrap();
let destination_chain: ChainNameRaw = "destinationchain".try_into().unwrap();
let mut transfer = InterchainTransfer {
let transfer = InterchainTransfer {
token_id: [1u8; 32].into(),
source_address: b"source_address".to_vec().try_into().unwrap(),
destination_address: b"destination_address".to_vec().try_into().unwrap(),
Expand Down Expand Up @@ -361,11 +361,11 @@ mod test {
)
.unwrap();

assert_ok!(interceptors::apply_scaling_factor_to_amount(
let transfer = assert_ok!(interceptors::apply_scaling_factor_to_amount(
&storage,
&source_chain,
&destination_chain,
&mut transfer,
transfer,
));
assert_eq!(
transfer.amount,
Expand All @@ -378,7 +378,7 @@ mod test {
let mut storage = MockStorage::new();
let source_chain: ChainNameRaw = "sourcechain".try_into().unwrap();
let destination_chain: ChainNameRaw = "destinationchain".try_into().unwrap();
let mut transfer = InterchainTransfer {
let transfer = InterchainTransfer {
token_id: [1u8; 32].into(),
source_address: b"source_address".to_vec().try_into().unwrap(),
destination_address: b"destination_address".to_vec().try_into().unwrap(),
Expand Down Expand Up @@ -412,11 +412,11 @@ mod test {
)
.unwrap();

assert_ok!(interceptors::apply_scaling_factor_to_amount(
let transfer = assert_ok!(interceptors::apply_scaling_factor_to_amount(
&storage,
&source_chain,
&destination_chain,
&mut transfer,
transfer,
));
assert_eq!(
transfer.amount,
Expand All @@ -429,7 +429,7 @@ mod test {
let mut storage = MockStorage::new();
let source_chain: ChainNameRaw = "sourcechain".try_into().unwrap();
let destination_chain: ChainNameRaw = "destinationchain".try_into().unwrap();
let mut transfer = InterchainTransfer {
let transfer = InterchainTransfer {
token_id: [1u8; 32].into(),
source_address: b"source_address".to_vec().try_into().unwrap(),
destination_address: b"destination_address".to_vec().try_into().unwrap(),
Expand Down Expand Up @@ -463,11 +463,11 @@ mod test {
)
.unwrap();

assert_ok!(interceptors::apply_scaling_factor_to_amount(
let transfer = assert_ok!(interceptors::apply_scaling_factor_to_amount(
&storage,
&source_chain,
&destination_chain,
&mut transfer,
transfer,
));
assert_eq!(
transfer.amount,
Expand All @@ -480,7 +480,7 @@ mod test {
let mut storage = MockStorage::new();
let source_chain: ChainNameRaw = "sourcechain".try_into().unwrap();
let destination_chain: ChainNameRaw = "destinationchain".try_into().unwrap();
let mut transfer = InterchainTransfer {
let transfer = InterchainTransfer {
token_id: [1u8; 32].into(),
source_address: b"source_address".to_vec().try_into().unwrap(),
destination_address: b"destination_address".to_vec().try_into().unwrap(),
Expand Down Expand Up @@ -519,7 +519,7 @@ mod test {
&storage,
&source_chain,
&destination_chain,
&mut transfer,
transfer,
),
Error,
Error::InvalidTransferAmount { .. }
Expand All @@ -531,7 +531,7 @@ mod test {
let mut storage = MockStorage::new();
let source_chain: ChainNameRaw = "sourcechain".try_into().unwrap();
let destination_chain: ChainNameRaw = "destinationchain".try_into().unwrap();
let mut transfer = InterchainTransfer {
let transfer = InterchainTransfer {
token_id: [1u8; 32].into(),
source_address: b"source_address".to_vec().try_into().unwrap(),
destination_address: b"destination_address".to_vec().try_into().unwrap(),
Expand Down Expand Up @@ -570,7 +570,7 @@ mod test {
&storage,
&source_chain,
&destination_chain,
&mut transfer,
transfer,
),
Error,
Error::InvalidTransferAmount { .. }
Expand All @@ -582,7 +582,7 @@ mod test {
let mut storage = MockStorage::new();
let source_chain: ChainNameRaw = "sourcechain".try_into().unwrap();
let destination_chain: ChainNameRaw = "destinationchain".try_into().unwrap();
let mut deploy_token = DeployInterchainToken {
let deploy_token = DeployInterchainToken {
token_id: [1u8; 32].into(),
name: "token".to_string().try_into().unwrap(),
symbol: "TKN".to_string().try_into().unwrap(),
Expand Down Expand Up @@ -613,26 +613,26 @@ mod test {
)
.unwrap();

assert_ok!(interceptors::calculate_scaling_factor(
let deploy_token = assert_ok!(interceptors::calculate_scaling_factor(
&storage,
&source_chain,
&destination_chain,
&mut deploy_token,
deploy_token,
));
assert_eq!(deploy_token.decimals, 6);

let mut deploy_token = DeployInterchainToken {
let deploy_token = DeployInterchainToken {
token_id: [1u8; 32].into(),
name: "token".to_string().try_into().unwrap(),
symbol: "TKN".to_string().try_into().unwrap(),
decimals: 3,
minter: None,
};
assert_ok!(interceptors::calculate_scaling_factor(
let deploy_token = assert_ok!(interceptors::calculate_scaling_factor(
&storage,
&source_chain,
&destination_chain,
&mut deploy_token,
deploy_token,
));
assert_eq!(deploy_token.decimals, 3);
}
Expand All @@ -642,7 +642,7 @@ mod test {
let mut storage = MockStorage::new();
let source_chain: ChainNameRaw = "sourcechain".try_into().unwrap();
let destination_chain: ChainNameRaw = "destinationchain".try_into().unwrap();
let mut deploy_token = DeployInterchainToken {
let deploy_token = DeployInterchainToken {
token_id: [1u8; 32].into(),
name: "token".to_string().try_into().unwrap(),
symbol: "TKN".to_string().try_into().unwrap(),
Expand Down Expand Up @@ -673,11 +673,11 @@ mod test {
)
.unwrap();

assert_ok!(interceptors::calculate_scaling_factor(
let deploy_token = assert_ok!(interceptors::calculate_scaling_factor(
&storage,
&source_chain,
&destination_chain,
&mut deploy_token,
deploy_token,
));
assert_eq!(deploy_token.decimals, 9);
}
Expand Down
56 changes: 26 additions & 30 deletions contracts/interchain-token-service/src/contract/execute/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,44 +165,40 @@ fn apply_to_transfer(
storage: &mut dyn Storage,
source_chain: ChainNameRaw,
destination_chain: ChainNameRaw,
mut transfer: InterchainTransfer,
transfer: InterchainTransfer,
) -> Result<InterchainTransfer, Error> {
interceptors::subtract_supply_amount(storage, &source_chain, &transfer)
.and_then(|_| {
interceptors::apply_scaling_factor_to_amount(
storage,
&source_chain,
&destination_chain,
&mut transfer,
)
})
.and_then(|_| interceptors::add_supply_amount(storage, &destination_chain, &transfer))
.map(|_| transfer)
interceptors::subtract_supply_amount(storage, &source_chain, &transfer)?;
let transfer = interceptors::apply_scaling_factor_to_amount(
storage,
&source_chain,
&destination_chain,
transfer,
)?;
interceptors::add_supply_amount(storage, &destination_chain, &transfer)?;

Ok(transfer)
}

fn apply_to_token_deployment(
storage: &mut dyn Storage,
source_chain: &ChainNameRaw,
destination_chain: &ChainNameRaw,
mut deploy_token: DeployInterchainToken,
deploy_token: DeployInterchainToken,
) -> Result<DeployInterchainToken, Error> {
interceptors::save_token_instance_for_source_chain(storage, source_chain, &deploy_token)
.and_then(|_| {
interceptors::calculate_scaling_factor(
storage,
source_chain,
destination_chain,
&mut deploy_token,
)
})
.and_then(|_| {
interceptors::save_token_instance_for_destination_chain(
storage,
destination_chain,
&deploy_token,
)
})
.map(|_| deploy_token)
interceptors::save_token_instance_for_source_chain(storage, source_chain, &deploy_token)?;
let deploy_token = interceptors::calculate_scaling_factor(
storage,
source_chain,
destination_chain,
deploy_token,
)?;
interceptors::save_token_instance_for_destination_chain(
storage,
destination_chain,
&deploy_token,
)?;

Ok(deploy_token)
}

fn ensure_chain_not_frozen(storage: &dyn Storage, chain: &ChainNameRaw) -> Result<(), Error> {
Expand Down

0 comments on commit 4049046

Please sign in to comment.