Skip to content

Commit

Permalink
feat(its)!: fix ITS discovery and verify it in js tests (#187)
Browse files Browse the repository at this point in the history
  • Loading branch information
Foivos authored Oct 17, 2024
1 parent 4296e36 commit 86cde3b
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 60 deletions.
5 changes: 5 additions & 0 deletions .changeset/five-houses-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@axelar-network/axelar-cgp-sui': minor
---

Fix ITS discovery and verify it in tests.
52 changes: 49 additions & 3 deletions move/example/sources/its/its.move
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ use its::coin_info;
use its::coin_management;
use its::its::{Self, ITS};
use its::token_id::TokenId;
use its::discovery as its_discovery;
use relayer_discovery::discovery::RelayerDiscovery;
use relayer_discovery::transaction;
use relayer_discovery::transaction::{Self, Transaction};
use std::ascii::{Self, String};
use std::type_name;
use sui::address;
Expand Down Expand Up @@ -59,13 +60,58 @@ public fun register_transaction(
its: &ITS,
clock: &Clock,
) {
let arguments = vector[
concat(vector[0u8], object::id_address(singleton).to_bytes()),
concat(vector[0u8], object::id_address(its).to_bytes()),
vector[3u8],
concat(vector[0u8], object::id_address(clock).to_bytes()),
];

let transaction = transaction::new_transaction(
false,
vector[
transaction::new_move_call(
transaction::new_function(
address::from_bytes(
hex::decode(
*ascii::as_bytes(
&type_name::get_address(
&type_name::get<Singleton>(),
),
),
),
),
ascii::string(b"its"),
ascii::string(b"get_final_transaction"),
),
arguments,
vector[],
),
],
);

discovery.register_transaction(&singleton.channel, transaction);
}

public fun get_final_transaction(
singleton: &Singleton,
its: &ITS,
payload: vector<u8>,
clock: &Clock,
): Transaction {
let arguments = vector[
vector[2u8],
concat(vector[0u8], object::id_address(singleton).to_bytes()),
concat(vector[0u8], object::id_address(its).to_bytes()),
concat(vector[0u8], object::id_address(clock).to_bytes()),
];

// Get the coin type from its
let (token_id, _, _, _) = its_discovery::interchain_transfer_info(
payload,
);
let coin_type = (*its.registered_coin_type(token_id)).into_string();

let transaction = transaction::new_transaction(
true,
vector[
Expand All @@ -84,12 +130,12 @@ public fun register_transaction(
ascii::string(b"receive_interchain_transfer"),
),
arguments,
vector[],
vector[coin_type],
),
],
);

discovery.register_transaction(&singleton.channel, transaction);
transaction
}

/// This function needs to be called first to register the coin for either of
Expand Down
6 changes: 3 additions & 3 deletions move/its/sources/discovery.move
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public fun register_transaction(
let function = transaction::new_function(
package_id<ITS>(),
ascii::string(b"discovery"),
ascii::string(b"get_call_info"),
ascii::string(b"call_info"),
);

let move_call = transaction::new_move_call(
Expand Down Expand Up @@ -137,7 +137,7 @@ fun interchain_transfer_tx(its: &ITS, reader: &mut AbiReader): Transaction {
ascii::string(b"discovery"),
ascii::string(b"get_transaction"),
),
vector[discovery_arg, channel_id_arg, vector[0, 6]],
vector[discovery_arg, channel_id_arg ],
vector[],
),
],
Expand Down Expand Up @@ -187,7 +187,7 @@ fun initial_tx(its: &ITS): Transaction {
let function = transaction::new_function(
package_id<ITS>(),
ascii::string(b"discovery"),
ascii::string(b"get_call_info"),
ascii::string(b"call_info"),
);

let move_call = transaction::new_move_call(
Expand Down
84 changes: 30 additions & 54 deletions test/its.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ const {
findObjectId,
getRandomBytes32,
calculateNextSigners,
approveMessage,
getSingletonChannelId,
getITSChannelId,
setupTrustedAddresses,
approveAndExecuteMessage,
} = require('./testutils');
const { expect } = require('chai');
const { CLOCK_PACKAGE_ID } = require('../dist/types');
Expand Down Expand Up @@ -81,6 +81,20 @@ describe('ITS', () => {
gatewayInfo.gateway = objectIds.gateway;
gatewayInfo.domainSeparator = domainSeparator;
gatewayInfo.packageId = deployments.axelar_gateway.packageId;
gatewayInfo.discoveryPackageId;
gatewayInfo.discoveryPackageId = deployments.relayer_discovery.packageId;
gatewayInfo.discovery = objectIds.relayerDiscovery;
}

async function registerItsTransaction() {
const registerTransactionBuilder = new TxBuilder(client);

await registerTransactionBuilder.moveCall({
target: `${deployments.its.packageId}::discovery::register_transaction`,
arguments: [objectIds.its, objectIds.relayerDiscovery],
});

await registerTransactionBuilder.signAndExecute(deployer);
}

before(async () => {
Expand Down Expand Up @@ -165,6 +179,7 @@ describe('ITS', () => {
describe('Two-way Calls', () => {
before(async () => {
await setupGateway();
await registerItsTransaction();
await setupTrustedAddresses(client, deployer, objectIds, deployments, [trustedSourceAddress], [trustedSourceChain]);
});

Expand Down Expand Up @@ -218,7 +233,6 @@ describe('ITS', () => {

// Channel ID for the ITS example. This will be encoded in the payload
const itsExampleChannelId = await getSingletonChannelId(client, objectIds.singleton);

// ITS transfer payload from Ethereum to Sui
const payload = defaultAbiCoder.encode(
['uint256', 'uint256', 'bytes', 'bytes', 'uint256', 'bytes'],
Expand All @@ -230,32 +244,11 @@ describe('ITS', () => {
message_id: hexlify(randomBytes(32)),
source_address: trustedSourceAddress,
destination_id: destinationAddress,
payload,
payload_hash: keccak256(payload),
};

await approveMessage(client, keypair, gatewayInfo, message);

const txBuilder = new TxBuilder(client);

const approvedMessage = await txBuilder.moveCall({
target: `${deployments.axelar_gateway.packageId}::gateway::take_approved_message`,
arguments: [
objectIds.gateway,
message.source_chain,
message.message_id,
message.source_address,
message.destination_id,
payload,
],
});

await txBuilder.moveCall({
target: `${deployments.example.packageId}::its::receive_interchain_transfer`,
arguments: [approvedMessage, objectIds.singleton, objectIds.its, CLOCK_PACKAGE_ID],
typeArguments: [`${deployments.example.packageId}::token::TOKEN`],
});

await txBuilder.signAndExecute(deployer);
await approveAndExecuteMessage(client, keypair, gatewayInfo, message);
});
});

Expand Down Expand Up @@ -307,6 +300,16 @@ describe('ITS', () => {
const treasuryCap = findObjectId(publishTxn, `TreasuryCap<${typeArg}>`);
const metadata = findObjectId(publishTxn, `CoinMetadata<${typeArg}>`);

const txBuilder = new TxBuilder(client);

await txBuilder.moveCall({
target: `${deployments.its.packageId}::its::give_unregistered_coin`,
arguments: [objectIds.its, treasuryCap, metadata],
typeArguments: [typeArg],
});

await txBuilder.signAndExecute(deployer);

// Approve ITS transfer message
const messageType = ITSMessageType.InterchainTokenDeployment;
const tokenId = hexlify(randomBytes(32));
Expand All @@ -326,38 +329,11 @@ describe('ITS', () => {
message_id: hexlify(randomBytes(32)),
source_address: trustedSourceAddress,
destination_id: objectIds.itsChannel,
payload,
payload_hash: keccak256(payload),
};

await approveMessage(client, keypair, gatewayInfo, message);

const txBuilder = new TxBuilder(client);

const approvedMessage = await txBuilder.moveCall({
target: `${deployments.axelar_gateway.packageId}::gateway::take_approved_message`,
arguments: [
objectIds.gateway,
message.source_chain,
message.message_id,
message.source_address,
message.destination_id,
payload,
],
});

await txBuilder.moveCall({
target: `${deployments.its.packageId}::its::give_unregistered_coin`,
arguments: [objectIds.its, treasuryCap, metadata],
typeArguments: [typeArg],
});

await txBuilder.moveCall({
target: `${deployments.its.packageId}::its::receive_deploy_interchain_token`,
arguments: [objectIds.its, approvedMessage],
typeArguments: [typeArg],
});

await txBuilder.signAndExecute(deployer);
await approveAndExecuteMessage(client, keypair, gatewayInfo, message);
});
});
});
Expand Down
11 changes: 11 additions & 0 deletions test/testdata/interface_example_its.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@
}
},
"publicFunctions": {
"get_final_transaction": {
"name": "get_final_transaction",
"visibility": "public",
"params": {
"singleton#0#0": "&Singleton",
"its#0#0": "&ITS",
"payload#0#0": "vector<u8>",
"clock#0#0": "&Clock"
},
"returnType": "Transaction"
},
"register_coin<TOKEN>": {
"name": "register_coin<TOKEN>",
"visibility": "public",
Expand Down

0 comments on commit 86cde3b

Please sign in to comment.