Skip to content

Commit

Permalink
Merge branch 'main' into feat/pay-gas-from-ticket
Browse files Browse the repository at this point in the history
  • Loading branch information
Foivos authored Nov 20, 2024
2 parents edf3343 + 2967055 commit dd8f4fe
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 6 deletions.
74 changes: 73 additions & 1 deletion move/its/sources/events.move
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ public(package) fun interchain_transfer<T>(
amount: u64,
data: &vector<u8>,
) {
let data_hash = bytes32::new(address::from_bytes(keccak256(data)));
let data_hash = if (data.length() == 0) {
bytes32::new(@0x0)
} else {
bytes32::new(address::from_bytes(keccak256(data)))
};
event::emit(InterchainTransfer<T> {
token_id,
source_address,
Expand Down Expand Up @@ -166,3 +170,71 @@ public(package) fun flow_limit_set<T>(
flow_limit
});
}

// ---------
// Test Only
// ---------
#[test_only]
use its::coin::COIN;
#[test_only]
use its::token_id;
#[test_only]
use utils::utils;

// -----
// Tests
// -----
#[test]
fun test_interchain_transfer_empty_data() {
let token_id = token_id::from_address(@0x1);
let source_address = @0x2;
let destination_chain = b"destination chain".to_ascii_string();
let destination_address = b"destination address";
let amount = 123;
let data = b"";
let data_hash = bytes32::new(@0x0);

interchain_transfer<COIN>(
token_id,
source_address,
destination_chain,
destination_address,
amount,
&data,
);
let event = utils::assert_single_event<InterchainTransfer<COIN>>();

assert!(event.data_hash == data_hash);
assert!(event.source_address == source_address);
assert!(event.destination_chain == destination_chain);
assert!(event.destination_address == destination_address);
assert!(event.amount == amount);
}


#[test]
fun test_interchain_transfer_nonempty_data() {
let token_id = token_id::from_address(@0x1);
let source_address = @0x2;
let destination_chain = b"destination chain".to_ascii_string();
let destination_address = b"destination address";
let amount = 123;
let data = b"data";
let data_hash = bytes32::new(address::from_bytes(keccak256(&data)));

interchain_transfer<COIN>(
token_id,
source_address,
destination_chain,
destination_address,
amount,
&data,
);
let event = utils::assert_single_event<InterchainTransfer<COIN>>();

assert!(event.data_hash == data_hash);
assert!(event.source_address == source_address);
assert!(event.destination_chain == destination_chain);
assert!(event.destination_address == destination_address);
assert!(event.amount == amount);
}
19 changes: 18 additions & 1 deletion move/its/sources/its.move
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,8 @@ use axelar_gateway::channel;
use std::string;
#[test_only]
use abi::abi;
#[test_only]
use utils::utils;

// === MESSAGE TYPES ===
#[test_only]
Expand Down Expand Up @@ -433,6 +435,7 @@ fun test_register_coin() {
let coin_management = its::coin_management::new_locked();

register_coin(&mut its, coin_info, coin_management);
utils::assert_single_event<its::events::CoinRegistered<COIN>>();

sui::test_utils::destroy(its);
}
Expand Down Expand Up @@ -460,6 +463,8 @@ fun test_deploy_remote_interchain_token() {
destination_chain,
);

utils::assert_single_event<its::events::InterchainTokenDeploymentStarted<COIN>>();

let mut writer = abi::new_writer(6);

writer
Expand Down Expand Up @@ -497,6 +502,9 @@ fun test_deploy_interchain_token() {
let coin_management = its::coin_management::new_locked();

let token_id = register_coin(&mut its, coin_info, coin_management);

utils::assert_single_event<its::events::CoinRegistered<COIN>>();

let amount = 1234;
let coin = sui::coin::mint_for_testing<COIN>(amount, ctx);
let destination_chain = ascii::string(b"Chain Name");
Expand All @@ -518,6 +526,9 @@ fun test_deploy_interchain_token() {
interchain_transfer_ticket,
&clock,
);

utils::assert_single_event<its::events::InterchainTransfer<COIN>>();

let mut writer = abi::new_writer(6);

writer
Expand Down Expand Up @@ -588,6 +599,8 @@ fun test_receive_interchain_transfer() {

receive_interchain_transfer<COIN>(&mut its, approved_message, &clock, ctx);

utils::assert_single_event<its::events::InterchainTransferReceived<COIN>>();

clock.destroy_for_testing();
sui::test_utils::destroy(its);
}
Expand Down Expand Up @@ -648,6 +661,8 @@ fun test_receive_interchain_transfer_with_data() {
ctx,
);

utils::assert_single_event<its::events::InterchainTransferReceived<COIN>>();

assert!(received_source_chain == source_chain);
assert!(received_source_address == its_source_address);
assert!(received_data == data);
Expand Down Expand Up @@ -694,7 +709,9 @@ fun test_receive_deploy_interchain_token() {
);

receive_deploy_interchain_token<COIN>(&mut its, approved_message);


utils::assert_single_event<its::events::CoinRegistered<COIN>>();

clock.destroy_for_testing();
sui::test_utils::destroy(its);
}
Expand Down
8 changes: 4 additions & 4 deletions move/its/sources/versioned/its_v0.move
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,6 @@ public(package) fun register_coin<T>(

self.add_registered_coin(token_id, coin_management, coin_info);

events::coin_registered<T>(
token_id,
);

token_id
}

Expand Down Expand Up @@ -599,6 +595,10 @@ fun add_registered_coin<T>(

let type_name = type_name::get<T>();
add_registered_coin_type(self, token_id, type_name);

events::coin_registered<T>(
token_id,
);
}

/// Send a payload to a destination chain. The destination chain needs to have a
Expand Down
16 changes: 16 additions & 0 deletions move/utils/sources/utils/utils.move
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ public macro fun peel<$T>($data: vector<u8>, $peel_fn: |&mut BCS| -> $T): $T {
result
}

#[test_only]
use sui::event;

#[test_only]
public fun assert_events<T: copy + drop>(n: u64): vector<T> {
let events = event::events_by_type<T>();
assert!(events.length() == n);
events
}

#[test_only]
public fun assert_single_event<T: copy + drop>(): T {
let events = assert_events<T>(1);
events[0]
}

#[test]
fun peel_bcs_data_succeeds() {
let test_bytes = b"test";
Expand Down

0 comments on commit dd8f4fe

Please sign in to comment.