-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(interchain-token-service): register canonical interchain token (#…
…119)
- Loading branch information
Showing
7 changed files
with
190 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
contracts/interchain-token-service/tests/canonical_token.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
mod utils; | ||
|
||
use axelar_soroban_std::{address::AddressExt, assert_contract_err, events}; | ||
use interchain_token_service::{ | ||
error::ContractError, event::InterchainTokenIdClaimedEvent, types::TokenManagerType, | ||
}; | ||
use soroban_sdk::{testutils::Address as _, xdr::ToXdr, Address, BytesN}; | ||
use utils::setup_env; | ||
|
||
const PREFIX_CANONICAL_TOKEN_SALT: &str = "canonical-token-salt"; | ||
|
||
#[test] | ||
fn register_canonical_token_succeeds() { | ||
let (env, client, _, _) = setup_env(); | ||
let token_address = Address::generate(&env); | ||
|
||
let chain_name = client.chain_name(); | ||
let chain_name_hash: BytesN<32> = env.crypto().keccak256(&(chain_name).to_xdr(&env)).into(); | ||
let expected_deploy_salt = env | ||
.crypto() | ||
.keccak256( | ||
&( | ||
PREFIX_CANONICAL_TOKEN_SALT, | ||
chain_name_hash, | ||
token_address.clone(), | ||
) | ||
.to_xdr(&env), | ||
) | ||
.into(); | ||
let expected_id = client.interchain_token_id(&Address::zero(&env), &expected_deploy_salt); | ||
|
||
assert_eq!(client.register_canonical_token(&token_address), expected_id); | ||
|
||
assert_eq!(client.token_address(&expected_id), token_address); | ||
|
||
assert_eq!( | ||
client.token_manager_type(&expected_id), | ||
TokenManagerType::LockUnlock | ||
); | ||
|
||
goldie::assert!(events::fmt_last_emitted_event::< | ||
InterchainTokenIdClaimedEvent, | ||
>(&env)); | ||
} | ||
|
||
#[test] | ||
fn register_canonical_token_fails_if_already_registered() { | ||
let (env, client, _, _) = setup_env(); | ||
let token_address = Address::generate(&env); | ||
|
||
client.register_canonical_token(&token_address); | ||
|
||
assert_contract_err!( | ||
client.try_register_canonical_token(&token_address), | ||
ContractError::TokenAlreadyRegistered | ||
); | ||
} | ||
|
||
#[test] | ||
fn canonical_token_id_derivation() { | ||
let (env, client, _, _) = setup_env(); | ||
let token_address = Address::generate(&env); | ||
|
||
let chain_name = client.chain_name(); | ||
let chain_name_hash: BytesN<32> = env.crypto().keccak256(&(chain_name).to_xdr(&env)).into(); | ||
|
||
let deploy_salt = env | ||
.crypto() | ||
.keccak256( | ||
&( | ||
PREFIX_CANONICAL_TOKEN_SALT, | ||
chain_name_hash.clone(), | ||
token_address, | ||
) | ||
.to_xdr(&env), | ||
) | ||
.into(); | ||
|
||
let token_id = client.interchain_token_id(&Address::zero(&env), &deploy_salt); | ||
|
||
goldie::assert_json!(vec![ | ||
hex::encode(chain_name_hash.to_array()), | ||
hex::encode(deploy_salt.to_array()), | ||
hex::encode(token_id.to_array()) | ||
]); | ||
} |
5 changes: 5 additions & 0 deletions
5
contracts/interchain-token-service/tests/testdata/canonical_token_id_derivation.golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[ | ||
"bdfb629dd56a9581bfba5ac25009cef0ee7646acbbd1880e99d7d26ea68f0885", | ||
"007a506f6c21cb0e84f844a86f9a8a86be868f99f35015c78ba868a7b66afc6d", | ||
"25c162306962b7a6d3ca7e65974ca39afa329e1cea9ed3d3cdaa70e38475a73a" | ||
] |
3 changes: 3 additions & 0 deletions
3
contracts/interchain-token-service/tests/testdata/register_canonical_token_succeeds.golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
contract: Contract(CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAARQG5) | ||
topics: (Symbol(interchain_token_id_claimed), BytesN<32>(37, 193, 98, 48, 105, 98, 183, 166, 211, 202, 126, 101, 151, 76, 163, 154, 250, 50, 158, 28, 234, 158, 211, 211, 205, 170, 112, 227, 132, 117, 167, 58), AccountId(GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWHF), BytesN<32>(0, 122, 80, 111, 108, 33, 203, 14, 132, 248, 68, 168, 111, 154, 138, 134, 190, 134, 143, 153, 243, 80, 21, 199, 139, 168, 104, 167, 182, 106, 252, 109)) | ||
data: () |