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

feat(its)!: add chain name to token id derivation #236

Merged
merged 7 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from 5 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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use interchain_token_service::coin_management::CoinManagement;
use interchain_token_service::interchain_transfer_ticket::{Self, InterchainTransferTicket};
use interchain_token_service::interchain_token_service_v0::{Self, InterchainTokenService_v0};
use interchain_token_service::owner_cap::{Self, OwnerCap};
use interchain_token_service::creator_cap::{Self, CreatorCap};
use interchain_token_service::operator_cap::{Self, OperatorCap};
use interchain_token_service::token_id::TokenId;
use interchain_token_service::trusted_addresses::TrustedAddresses;
Expand Down Expand Up @@ -37,10 +38,28 @@ public struct InterchainTokenService has key {
// Setup
// -----
fun init(ctx: &mut TxContext) {
transfer::public_transfer(
owner_cap::create(ctx),
ctx.sender(),
);

transfer::public_transfer(
operator_cap::create(ctx),
ctx.sender(),
);

transfer::public_transfer(
creator_cap::create(ctx),
ctx.sender(),
);
}

public fun setup(creator_cap: CreatorCap, chain_name: String, ctx: &mut TxContext) {
let inner = versioned::create(
DATA_VERSION,
interchain_token_service_v0::new(
version_control(),
chain_name,
ctx,
),
ctx,
Expand All @@ -52,15 +71,7 @@ fun init(ctx: &mut TxContext) {
inner,
});

transfer::public_transfer(
owner_cap::create(ctx),
ctx.sender(),
);

transfer::public_transfer(
operator_cap::create(ctx),
ctx.sender(),
);
creator_cap.destroy();
}

// ------
Expand Down Expand Up @@ -412,6 +423,7 @@ public fun create_for_testing(ctx: &mut TxContext): InterchainTokenService {

let mut value = interchain_token_service_v0::new(
version_control,
b"chain name".to_ascii_string(),
ctx,
);
value.set_trusted_address(
Expand Down Expand Up @@ -976,8 +988,25 @@ fun test_init() {
ts.next_tx(@0x0);

let owner_cap = ts.take_from_sender<OwnerCap>();
let its = ts.take_shared<InterchainTokenService>();
let operator_cap = ts.take_from_sender<OperatorCap>();

ts.return_to_sender(owner_cap);
ts.return_to_sender(operator_cap);
ts.end();
}

#[test]
fun test_setup() {
let mut ts = sui::test_scenario::begin(@0x0);
let creator_cap = creator_cap::create(ts.ctx());
let chain_name = b"chain name".to_ascii_string();

setup(creator_cap, chain_name, ts.ctx());
ts.next_tx(@0x0);

let its = ts.take_shared<InterchainTokenService>();
assert!(its.value!(b"send_interchain_transfer").chain_name() == chain_name);

sui::test_scenario::return_shared(its);
ts.end();
}
Expand Down
19 changes: 19 additions & 0 deletions move/interchain_token_service/sources/types/creator_cap.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module interchain_token_service::creator_cap;

// -----
// Types
// -----
public struct CreatorCap has key, store {
id: UID,
}

public(package) fun create(ctx: &mut TxContext): CreatorCap {
CreatorCap {
id: object::new(ctx),
}
}

public(package) fun destroy(self: CreatorCap) {
let CreatorCap { id } = self;
id.delete();
}
4 changes: 4 additions & 0 deletions move/interchain_token_service/sources/types/token_id.move
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ public fun to_u256(token_id: &TokenId): u256 {
}

public fun from_info<T>(
chain_name: &ascii::String,
name: &String,
symbol: &ascii::String,
decimals: &u8,
has_metadata: &bool,
has_treasury: &bool,
): TokenId {
let mut vec = address::from_u256(PREFIX_SUI_TOKEN_ID).to_bytes();
vec.append(bcs::to_bytes(chain_name));
Foivos marked this conversation as resolved.
Show resolved Hide resolved
Foivos marked this conversation as resolved.
Show resolved Hide resolved
vec.append(bcs::to_bytes(&type_name::get<T>()));
vec.append(bcs::to_bytes(name));
vec.append(bcs::to_bytes(symbol));
Expand All @@ -51,10 +53,12 @@ public fun from_info<T>(
}

public(package) fun from_coin_data<T>(
chain_name: &ascii::String,
coin_info: &CoinInfo<T>,
coin_management: &CoinManagement<T>,
): TokenId {
from_info<T>(
chain_name,
&coin_info.name(),
&coin_info.symbol(),
&coin_info.decimals(),
Expand Down
Loading
Loading