Skip to content

Commit

Permalink
feat(its)!: remove remote decimals (#195)
Browse files Browse the repository at this point in the history
  • Loading branch information
Foivos authored Nov 7, 2024
1 parent 9f656e2 commit 7dd615b
Show file tree
Hide file tree
Showing 14 changed files with 46 additions and 137 deletions.
5 changes: 5 additions & 0 deletions .changeset/eight-candles-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@axelar-network/axelar-cgp-sui': minor
---

Removed remote decimals tracking from ITS as it will be handled at the hub.
1 change: 0 additions & 1 deletion move/example/sources/its/its.move
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ public fun register_coin<TOKEN>(
coin_metadata.get_name(),
coin_metadata.get_symbol(),
coin_metadata.get_decimals(),
coin_metadata.get_decimals(),
);
let coin_management = coin_management::new_locked();

Expand Down
8 changes: 4 additions & 4 deletions move/its/sources/events.move
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public struct InterchainTransfer<phantom T> has copy, drop {
source_address: address,
destination_chain: String,
destination_address: vector<u8>,
amount: u256,
amount: u64,
data_hash: Bytes32,
}

Expand All @@ -38,7 +38,7 @@ public struct InterchainTransferReceived<phantom T> has copy, drop {
source_chain: String,
source_address: vector<u8>,
destination_address: address,
amount: u256,
amount: u64,
data_hash: Bytes32,
}

Expand All @@ -62,7 +62,7 @@ public(package) fun interchain_transfer<T>(
source_address: address,
destination_chain: String,
destination_address: vector<u8>,
amount: u256,
amount: u64,
data: &vector<u8>,
) {
let data_hash = bytes32::new(address::from_bytes(keccak256(data)));
Expand Down Expand Up @@ -98,7 +98,7 @@ public(package) fun interchain_transfer_received<T>(
source_chain: String,
source_address: vector<u8>,
destination_address: address,
amount: u256,
amount: u64,
data: &vector<u8>,
) {
let data_hash = bytes32::new(address::from_bytes(keccak256(data)));
Expand Down
28 changes: 4 additions & 24 deletions move/its/sources/its.move
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,6 @@ const MESSAGE_TYPE_DEPLOY_INTERCHAIN_TOKEN: u256 = 1;
#[test_only]
const ITS_HUB_ROUTING_IDENTIFIER: vector<u8> = b"hub";

// === The maximum number of decimals allowed ===
#[test_only]
const DECIMALS_CAP: u8 = 9;

#[test_only]
public fun create_for_testing(ctx: &mut TxContext): ITS {
let mut version_control = version_control();
Expand Down Expand Up @@ -416,7 +412,6 @@ fun test_register_coin() {
string::utf8(b"Name"),
ascii::string(b"Symbol"),
10,
12,
);
let coin_management = its::coin_management::new_locked();

Expand All @@ -432,13 +427,11 @@ fun test_deploy_remote_interchain_token() {
let token_name = string::utf8(b"Name");
let token_symbol = ascii::string(b"Symbol");
let token_decimals = 10;
let remote_decimals = 12;

let coin_info = its::coin_info::from_info<COIN>(
token_name,
token_symbol,
token_decimals,
remote_decimals,
);
let coin_management = its::coin_management::new_locked();

Expand Down Expand Up @@ -483,9 +476,7 @@ fun test_deploy_interchain_token() {
string::utf8(b"Name"),
ascii::string(b"Symbol"),
10,
12,
);
let scaling = coin_info.scaling();
let coin_management = its::coin_management::new_locked();

let token_id = register_coin(&mut its, coin_info, coin_management);
Expand Down Expand Up @@ -517,7 +508,7 @@ fun test_deploy_interchain_token() {
.write_u256(token_id.to_u256())
.write_bytes(source_channel.to_address().to_bytes())
.write_bytes(destination_address)
.write_u256((amount as u256) * scaling)
.write_u256((amount as u256))
.write_bytes(b"");

assert!(
Expand Down Expand Up @@ -546,7 +537,6 @@ fun test_receive_interchain_transfer() {
string::utf8(b"Name"),
ascii::string(b"Symbol"),
10,
12,
);

let amount = 1234;
Expand Down Expand Up @@ -595,9 +585,7 @@ fun test_receive_interchain_transfer_with_data() {
string::utf8(b"Name"),
ascii::string(b"Symbol"),
10,
12,
);
let scaling = coin_info.scaling();

let amount = 1234;
let data = b"some_data";
Expand Down Expand Up @@ -646,7 +634,7 @@ fun test_receive_interchain_transfer_with_data() {
assert!(received_source_chain == source_chain);
assert!(received_source_address == its_source_address);
assert!(received_data == data);
assert!(received_coin.value() == amount / (scaling as u64));
assert!(received_coin.value() == amount);

clock.destroy_for_testing();
channel.destroy();
Expand All @@ -665,9 +653,7 @@ fun test_receive_deploy_interchain_token() {
let source_address = ascii::string(b"Address");
let name = b"Token Name";
let symbol = b"Symbol";
let remote_decimals = 12;
let decimals = if (remote_decimals > DECIMALS_CAP) DECIMALS_CAP
else remote_decimals;
let decimals = 9;
let token_id: u256 = 1234;

its.value_mut!(b"").create_unregistered_coin(symbol, decimals, ctx);
Expand All @@ -678,7 +664,7 @@ fun test_receive_deploy_interchain_token() {
.write_u256(token_id)
.write_bytes(name)
.write_bytes(symbol)
.write_u256((remote_decimals as u256))
.write_u256((decimals as u256))
.write_bytes(vector::empty());
let payload = writer.into_bytes();

Expand Down Expand Up @@ -720,7 +706,6 @@ fun test_mint_as_distributor() {
let mut its = create_for_testing(ctx);
let symbol = b"COIN";
let decimals = 9;
let remote_decimals = 18;

let (treasury_cap, coin_metadata) = its::coin::create_treasury_and_metadata(
symbol,
Expand All @@ -729,7 +714,6 @@ fun test_mint_as_distributor() {
);
let coin_info = its::coin_info::from_metadata<COIN>(
coin_metadata,
remote_decimals,
);
let mut coin_management = its::coin_management::new_with_cap(treasury_cap);

Expand Down Expand Up @@ -759,7 +743,6 @@ fun test_mint_to_as_distributor() {
let mut its = create_for_testing(ctx);
let symbol = b"COIN";
let decimals = 9;
let remote_decimals = 18;

let (treasury_cap, coin_metadata) = its::coin::create_treasury_and_metadata(
symbol,
Expand All @@ -768,7 +751,6 @@ fun test_mint_to_as_distributor() {
);
let coin_info = its::coin_info::from_metadata<COIN>(
coin_metadata,
remote_decimals,
);
let mut coin_management = its::coin_management::new_with_cap(treasury_cap);

Expand Down Expand Up @@ -796,7 +778,6 @@ fun test_burn_as_distributor() {
let mut its = create_for_testing(ctx);
let symbol = b"COIN";
let decimals = 9;
let remote_decimals = 18;
let amount = 1234;

let (
Expand All @@ -806,7 +787,6 @@ fun test_burn_as_distributor() {
let coin = treasury_cap.mint(amount, ctx);
let coin_info = its::coin_info::from_metadata<COIN>(
coin_metadata,
remote_decimals,
);
let mut coin_management = its::coin_management::new_with_cap(treasury_cap);

Expand Down
4 changes: 0 additions & 4 deletions move/its/sources/types/coin_data.move
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,3 @@ public(package) fun coin_management_mut<T>(
): &mut CoinManagement<T> {
&mut self.coin_management
}

public(package) fun coin_scaling<T>(self: &CoinData<T>): u256 {
self.coin_info.scaling()
}
24 changes: 2 additions & 22 deletions move/its/sources/types/coin_info.move
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
/// either derived from `CoinMetadata` or manually provided.
module its::coin_info;

use its::utils;
use std::ascii;
use std::string::String;
use sui::coin::CoinMetadata;
Expand All @@ -11,7 +10,6 @@ public struct CoinInfo<phantom T> has store {
name: String,
symbol: ascii::String,
decimals: u8,
remote_decimals: u8,
metadata: Option<CoinMetadata<T>>,
}

Expand All @@ -20,27 +18,21 @@ public fun from_info<T>(
name: String,
symbol: ascii::String,
decimals: u8,
remote_decimals: u8,
): CoinInfo<T> {
CoinInfo {
name,
symbol,
decimals,
remote_decimals,
metadata: option::none(),
}
}

/// Create a new coin info from the given `CoinMetadata` object.
public fun from_metadata<T>(
metadata: CoinMetadata<T>,
remote_decimals: u8,
): CoinInfo<T> {
public fun from_metadata<T>(metadata: CoinMetadata<T>): CoinInfo<T> {
CoinInfo {
name: metadata.get_name(),
symbol: metadata.get_symbol(),
decimals: metadata.get_decimals(),
remote_decimals,
metadata: option::some(metadata),
}
}
Expand All @@ -59,14 +51,6 @@ public fun decimals<T>(self: &CoinInfo<T>): u8 {
self.decimals
}

public fun remote_decimals<T>(self: &CoinInfo<T>): u8 {
self.remote_decimals
}

public fun scaling<T>(self: &CoinInfo<T>): u256 {
utils::pow(10, self.remote_decimals - self.decimals)
}

public fun metadata<T>(self: &CoinInfo<T>): &Option<CoinMetadata<T>> {
&self.metadata
}
Expand All @@ -82,7 +66,6 @@ public fun drop<T>(coin_info: CoinInfo<T>) {
name: _,
symbol: _,
decimals: _,
remote_decimals: _,
metadata,
} = coin_info;
if (metadata.is_some()) {
Expand All @@ -101,18 +84,15 @@ fun test_from_metadata() {
let name = metadata.get_name();
let symbol = metadata.get_symbol();
let decimals = metadata.get_decimals();
let remote_decimals = 31;

let coin_info = from_metadata(metadata, remote_decimals);
let coin_info = from_metadata(metadata);

assert!(coin_info.name() == name);
assert!(coin_info.symbol() == symbol);
assert!(coin_info.decimals() == decimals);
assert!(coin_info.remote_decimals() == remote_decimals);
assert!(
sui::bcs::to_bytes(coin_info.metadata().borrow()) == metadata_bytes,
);
assert!(coin_info.scaling() == utils::pow(10, remote_decimals - decimals));

sui::test_utils::destroy(coin_info);
}
31 changes: 8 additions & 23 deletions move/its/sources/types/coin_management.move
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public struct CoinManagement<phantom T> has store {
distributor: Option<address>,
operator: Option<address>,
flow_limit: FlowLimit,
scaling: u256,
dust: u256,
}

Expand All @@ -38,7 +37,6 @@ public fun new_with_cap<T>(treasury_cap: TreasuryCap<T>): CoinManagement<T> {
distributor: option::none(),
operator: option::none(),
flow_limit: flow_limit::new(),
scaling: 0, // placeholder, this gets edited when a coin is registered.
dust: 0,
}
}
Expand All @@ -52,7 +50,6 @@ public fun new_locked<T>(): CoinManagement<T> {
distributor: option::none(),
operator: option::none(),
flow_limit: flow_limit::new(),
scaling: 0, // placeholder, this gets edited when a coin is registered.
dust: 0,
}
}
Expand Down Expand Up @@ -97,9 +94,9 @@ public(package) fun take_balance<T>(
self: &mut CoinManagement<T>,
to_take: Balance<T>,
clock: &Clock,
): u256 {
): u64 {
self.flow_limit.add_flow_out(to_take.value(), clock);
let amount = (to_take.value() as u256) * self.scaling;
let amount = to_take.value();
if (has_capability(self)) {
self.burn(to_take);
} else {
Expand All @@ -112,28 +109,18 @@ public(package) fun take_balance<T>(
/// previous transfers is added to the coin here.
public(package) fun give_coin<T>(
self: &mut CoinManagement<T>,
mut amount: u256,
amount: u64,
clock: &Clock,
ctx: &mut TxContext,
): Coin<T> {
amount = amount + self.dust;
self.dust = amount % self.scaling;
let sui_amount = (amount / self.scaling as u64);
self.flow_limit.add_flow_in(sui_amount, clock);
self.flow_limit.add_flow_in(amount, clock);
if (has_capability(self)) {
self.mint(sui_amount, ctx)
self.mint(amount, ctx)
} else {
coin::take(self.balance.borrow_mut(), sui_amount, ctx)
coin::take(self.balance.borrow_mut(), amount, ctx)
}
}

public(package) fun set_scaling<T>(
self: &mut CoinManagement<T>,
scaling: u256,
) {
self.scaling = scaling;
}

// helper function to mint as a distributor.
public(package) fun mint<T>(
self: &mut CoinManagement<T>,
Expand Down Expand Up @@ -214,19 +201,17 @@ fun test_give_coin() {

let mut coin = cap.mint(amount1, ctx);
let mut management1 = new_locked<COIN_MANAGEMENT>();
management1.scaling = 1;
let clock = sui::clock::create_for_testing(ctx);
management1.take_balance(coin.into_balance(), &clock);
coin = management1.give_coin((amount1 as u256), &clock, ctx);
coin = management1.give_coin(amount1, &clock, ctx);

assert!(management1.balance.borrow().value() == 0);
assert!(coin.value() == amount1);

sui::test_utils::destroy(coin);

let mut management2 = new_with_cap<COIN_MANAGEMENT>(cap);
management2.scaling = 1;
coin = management2.give_coin((amount2 as u256), &clock, ctx);
coin = management2.give_coin(amount2, &clock, ctx);

assert!(coin.value() == amount2);

Expand Down
Loading

0 comments on commit 7dd615b

Please sign in to comment.