Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(interchain-token-service): add test for 3 chain cycle #697

Merged
merged 3 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
306 changes: 306 additions & 0 deletions contracts/interchain-token-service/tests/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,312 @@ fn execute_message_interchain_transfer_should_scale_the_amount_when_source_decim
);
}

#[test]
fn execute_message_interchain_transfer_should_scale_correctly_in_3_chain_cycle() {
let TestMessage {
source_its_contract,
..
} = TestMessage::dummy();
let configs = vec![
(
"ethereum".parse().unwrap(),
source_its_contract.clone(),
Uint256::MAX.try_into().unwrap(),
18,
cjcobb23 marked this conversation as resolved.
Show resolved Hide resolved
),
(
"stellar".parse().unwrap(),
source_its_contract.clone(),
Uint256::from(u128::MAX).try_into().unwrap(),
12,
),
(
"sui".parse().unwrap(),
source_its_contract.clone(),
Uint256::from(u64::MAX).try_into().unwrap(),
6,
cjcobb23 marked this conversation as resolved.
Show resolved Hide resolved
),
];

let (mut deps, TestMessage { router_message, .. }) =
utils::setup_multiple_chains(configs.clone());
let token_id = TokenId::new([1; 32]);
let deploy_token = DeployInterchainToken {
token_id,
name: "Test".try_into().unwrap(),
symbol: "TST".try_into().unwrap(),
decimals: 18,
minter: None,
};
let hub_message = HubMessage::SendToHub {
destination_chain: configs[1].0.clone(),
message: deploy_token.clone().into(),
};
assert_ok!(utils::execute_hub_message(
deps.as_mut(),
CrossChainId {
source_chain: configs[0].0.clone(),
message_id: router_message.cc_id.message_id.clone()
},
source_its_contract.clone(),
hub_message,
));

let hub_message = HubMessage::SendToHub {
destination_chain: configs[2].0.clone(),
message: deploy_token.clone().into(),
};
assert_ok!(utils::execute_hub_message(
deps.as_mut(),
CrossChainId {
source_chain: configs[0].0.clone(),
message_id: router_message.cc_id.message_id.clone()
},
source_its_contract.clone(),
hub_message,
));

let amount: nonempty::Uint256 = Uint256::from_u128(1000000000000000000u128)
.try_into()
.unwrap();

// send from chain 0 to chain 1
let transfer = InterchainTransfer {
token_id,
source_address: HexBinary::from([1; 32]).try_into().unwrap(),
destination_address: HexBinary::from([2; 32]).try_into().unwrap(),
amount,
data: None,
};
let hub_message = HubMessage::SendToHub {
destination_chain: configs[1].0.clone(),
message: transfer.into(),
};

assert_ok!(utils::execute_hub_message(
deps.as_mut(),
CrossChainId {
source_chain: configs[0].0.clone(),
message_id: router_message.cc_id.message_id.clone()
},
source_its_contract.clone(),
hub_message,
));

let scaling_factor = Uint256::from_u128(10)
.checked_pow(configs[0].3.abs_diff(configs[1].3).into())
.unwrap();
let scaled_amount = amount.clone().checked_div(scaling_factor).unwrap();

// send back from destination to source
let transfer = InterchainTransfer {
token_id,
source_address: HexBinary::from([2; 32]).try_into().unwrap(),
destination_address: HexBinary::from([1; 32]).try_into().unwrap(),
amount: scaled_amount.try_into().unwrap(),
data: None,
};

let hub_message = HubMessage::SendToHub {
destination_chain: configs[2].0.clone(),
message: transfer.into(),
};

assert_ok!(utils::execute_hub_message(
deps.as_mut(),
CrossChainId {
source_chain: configs[1].0.clone(),
message_id: router_message.cc_id.message_id.clone()
},
source_its_contract.clone(),
hub_message,
));

let scaling_factor = Uint256::from_u128(10)
.checked_pow(configs[0].3.abs_diff(configs[2].3).into())
.unwrap();
let scaled_amount = amount.clone().checked_div(scaling_factor).unwrap();

// send back from destination to source
let transfer = InterchainTransfer {
token_id,
source_address: HexBinary::from([2; 32]).try_into().unwrap(),
destination_address: HexBinary::from([1; 32]).try_into().unwrap(),
amount: scaled_amount.try_into().unwrap(),
data: None,
};

let hub_message = HubMessage::SendToHub {
destination_chain: configs[0].0.clone(),
message: transfer.into(),
};

let response_to_destination = assert_ok!(utils::execute_hub_message(
deps.as_mut(),
CrossChainId {
source_chain: configs[2].0.clone(),
message_id: router_message.cc_id.message_id.clone()
},
source_its_contract,
hub_message,
));

goldie::assert_json!(response_to_destination);
}

#[test]
fn execute_message_interchain_transfer_should_scale_correctly_in_3_chain_cycle_with_dust() {
cjcobb23 marked this conversation as resolved.
Show resolved Hide resolved
let TestMessage {
source_its_contract,
..
} = TestMessage::dummy();
let configs = vec![
(
"ethereum".parse().unwrap(),
source_its_contract.clone(),
Uint256::MAX.try_into().unwrap(),
18,
),
(
"stellar".parse().unwrap(),
source_its_contract.clone(),
Uint256::from(u128::MAX).try_into().unwrap(),
12,
),
(
"sui".parse().unwrap(),
source_its_contract.clone(),
Uint256::from(u64::MAX).try_into().unwrap(),
6,
),
];

let (mut deps, TestMessage { router_message, .. }) =
utils::setup_multiple_chains(configs.clone());
let token_id = TokenId::new([1; 32]);
let deploy_token = DeployInterchainToken {
token_id,
name: "Test".try_into().unwrap(),
symbol: "TST".try_into().unwrap(),
decimals: 18,
minter: None,
};
let hub_message = HubMessage::SendToHub {
destination_chain: configs[1].0.clone(),
message: deploy_token.clone().into(),
};
assert_ok!(utils::execute_hub_message(
deps.as_mut(),
CrossChainId {
source_chain: configs[0].0.clone(),
message_id: router_message.cc_id.message_id.clone()
},
source_its_contract.clone(),
hub_message,
));

let hub_message = HubMessage::SendToHub {
destination_chain: configs[2].0.clone(),
message: deploy_token.clone().into(),
};
assert_ok!(utils::execute_hub_message(
deps.as_mut(),
CrossChainId {
source_chain: configs[0].0.clone(),
message_id: router_message.cc_id.message_id.clone()
},
source_its_contract.clone(),
hub_message,
));

let amount: nonempty::Uint256 = Uint256::from_u128(1000000000010000001u128)
.try_into()
.unwrap();

// send from chain 0 to chain 1
let transfer = InterchainTransfer {
token_id,
source_address: HexBinary::from([1; 32]).try_into().unwrap(),
destination_address: HexBinary::from([2; 32]).try_into().unwrap(),
amount,
data: None,
};
let hub_message = HubMessage::SendToHub {
destination_chain: configs[1].0.clone(),
message: transfer.into(),
};

assert_ok!(utils::execute_hub_message(
deps.as_mut(),
CrossChainId {
source_chain: configs[0].0.clone(),
message_id: router_message.cc_id.message_id.clone()
},
source_its_contract.clone(),
hub_message,
));

let scaling_factor = Uint256::from_u128(10)
.checked_pow(configs[0].3.abs_diff(configs[1].3).into())
.unwrap();
let scaled_amount = amount.clone().checked_div(scaling_factor).unwrap();

// send back from destination to source
let transfer = InterchainTransfer {
token_id,
source_address: HexBinary::from([2; 32]).try_into().unwrap(),
destination_address: HexBinary::from([1; 32]).try_into().unwrap(),
amount: scaled_amount.try_into().unwrap(),
data: None,
};

let hub_message = HubMessage::SendToHub {
destination_chain: configs[2].0.clone(),
message: transfer.into(),
};

assert_ok!(utils::execute_hub_message(
deps.as_mut(),
CrossChainId {
source_chain: configs[1].0.clone(),
message_id: router_message.cc_id.message_id.clone()
},
source_its_contract.clone(),
hub_message,
));

let scaling_factor = Uint256::from_u128(10)
.checked_pow(configs[0].3.abs_diff(configs[2].3).into())
.unwrap();
let scaled_amount = amount.clone().checked_div(scaling_factor).unwrap();

// send back from destination to source
let transfer = InterchainTransfer {
token_id,
source_address: HexBinary::from([2; 32]).try_into().unwrap(),
destination_address: HexBinary::from([1; 32]).try_into().unwrap(),
amount: scaled_amount.try_into().unwrap(),
data: None,
};

let hub_message = HubMessage::SendToHub {
destination_chain: configs[0].0.clone(),
message: transfer.into(),
};

let response_to_destination = assert_ok!(utils::execute_hub_message(
deps.as_mut(),
CrossChainId {
source_chain: configs[2].0.clone(),
message_id: router_message.cc_id.message_id.clone()
},
source_its_contract,
hub_message,
));

goldie::assert_json!(response_to_destination);
}

#[test]
fn execute_message_deploy_interchain_token_should_translate_decimals_when_max_uints_are_different()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"messages": [
{
"id": 0,
"msg": {
"wasm": {
"execute": {
"contract_addr": "gateway",
"msg": "eyJjYWxsX2NvbnRyYWN0Ijp7ImRlc3RpbmF0aW9uX2NoYWlu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MDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAxNDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDIwMDIwMjAyMDIwMjAyMDIwMjAyMDIwMjAyMDIwMjAyMDIwMjAyMDIwMjAyMDIwMjAyMDIwMjAyMDIwMjAyMDIwMjAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMjAwMTAxMDEwMTAxMDEwMTAxMDEwMTAxMDEwMTAxMDEwMTAxMDEwMTAxMDEwMTAxMDEwMTAxMDEwMTAxMDEwMTAxMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMCJ9fQ==",
"funds": []
}
}
},
"gas_limit": null,
"reply_on": "never"
}
],
"attributes": [],
"events": [
{
"type": "message_received",
"attributes": [
{
"key": "cc_id",
"value": "sui_message-id"
},
{
"key": "destination_chain",
"value": "ethereum"
},
{
"key": "message_type",
"value": "InterchainTransfer"
},
{
"key": "token_id",
"value": "0101010101010101010101010101010101010101010101010101010101010101"
},
{
"key": "source_address",
"value": "0202020202020202020202020202020202020202020202020202020202020202"
},
{
"key": "destination_address",
"value": "0101010101010101010101010101010101010101010101010101010101010101"
},
{
"key": "amount",
"value": "1000000000000000000"
}
]
}
],
"data": null
}
Loading
Loading