Skip to content

Commit

Permalink
chore: use structs from axelar-cgp-sui
Browse files Browse the repository at this point in the history
  • Loading branch information
npty committed Sep 2, 2024
1 parent e80a522 commit 1b0a2ec
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 205 deletions.
14 changes: 5 additions & 9 deletions sui/deploy-contract.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { Command, Option } = require('commander');
const { updateMoveToml, TxBuilder } = require('@axelar-network/axelar-cgp-sui');
const { updateMoveToml, TxBuilder, bcsStructs } = require('@axelar-network/axelar-cgp-sui');
const { ethers } = require('hardhat');
const { toB64 } = require('@mysten/sui/utils');
const { bcs } = require('@mysten/sui/bcs');
Expand All @@ -14,8 +14,6 @@ const {
getWallet,
printWalletInfo,
broadcast,
bytes32Struct,
signersStruct,
upgradePackage,
UPGRADE_POLICIES,
getSigners,
Expand Down Expand Up @@ -153,12 +151,10 @@ async function postDeployAxelarGateway(published, keypair, client, config, chain
`${suiPackageAddress}::package::UpgradeCap`,
]);

const encodedSigners = signersStruct
.serialize({
...signers,
nonce: bytes32Struct.serialize(signers.nonce).toBytes(),
})
.toBytes();
const encodedSigners = bcsStructs.gateway.WeightedSigner.serialize({
...signers,
nonce: bcsStructs.common.Bytes32.serialize(signers.nonce).toBytes(),
}).toBytes();

const tx = new Transaction();

Expand Down
6 changes: 3 additions & 3 deletions sui/gas-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { Command } = require('commander');
const { Transaction } = require('@mysten/sui/transactions');
const { bcs } = require('@mysten/sui/bcs');
const { ethers } = require('hardhat');
const { bcsStructs } = require('@axelar-network/axelar-cgp-sui');
const {
utils: { arrayify },
} = ethers;
Expand All @@ -10,7 +11,6 @@ const {
getWallet,
printWalletInfo,
broadcast,
gasServiceStruct,
getBcsBytesByObjectId,
getFormattedAmount,
addOptionsToCommands,
Expand Down Expand Up @@ -91,7 +91,7 @@ async function collectGas(keypair, client, gasServiceConfig, args, options) {
const receiver = options.receiver || walletAddress;

const bytes = await getBcsBytesByObjectId(client, gasServiceObjectId);
const { balance: gasServiceBalance } = gasServiceStruct.parse(bytes);
const { balance: gasServiceBalance } = bcsStructs.gasService.GasService.parse(bytes);

// Check if the gas service balance is sufficient
if (gasServiceBalance < unitAmount) {
Expand Down Expand Up @@ -127,7 +127,7 @@ async function refund(keypair, client, gasServiceConfig, args, options) {
const receiver = options.receiver || walletAddress;

const bytes = await getBcsBytesByObjectId(client, gasServiceObjectId);
const { balance: gasServiceBalance } = gasServiceStruct.parse(bytes);
const { balance: gasServiceBalance } = bcsStructs.gasService.GasService.parse(bytes);

// Check if the gas service balance is sufficient
if (gasServiceBalance < unitAmount) {
Expand Down
47 changes: 18 additions & 29 deletions sui/gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,14 @@ const { Command, Option } = require('commander');
const { Transaction } = require('@mysten/sui/transactions');
const { bcs } = require('@mysten/sui/bcs');
const { ethers } = require('hardhat');
const { bcsStructs } = require('@axelar-network/axelar-cgp-sui');
const {
utils: { arrayify, keccak256, toUtf8Bytes },
constants: { HashZero },
} = ethers;

const { saveConfig, printInfo, loadConfig, getMultisigProof } = require('../common/utils');
const {
bytes32Struct,
signersStruct,
messageToSignStruct,
messageStruct,
executeDataStruct,
proofStruct,
addBaseOptions,
addOptionsToCommands,
getSigners,
Expand Down Expand Up @@ -88,22 +83,18 @@ function getProof(keypair, commandType, data, contractConfig, options) {

const dataHash = arrayify(hashMessage(commandType, data));

const message = messageToSignStruct
.serialize({
domain_separator: contractConfig.domainSeparator,
signers_hash: keccak256(signersStruct.serialize(signers).toBytes()),
data_hash: dataHash,
})
.toBytes();
const message = bcsStructs.gateway.MessageToSign.serialize({
domain_separator: contractConfig.domainSeparator,
signers_hash: keccak256(bcsStructs.gateway.WeightedSigners.serialize(signers).toBytes()),
data_hash: dataHash,
}).toBytes();

const signatures = getSignatures(keypair, message, options);

const encodedProof = proofStruct
.serialize({
signers,
signatures,
})
.toBytes();
const encodedProof = bcsStructs.gateway.Proof.serialize({
signers,
signatures,
}).toBytes();

return encodedProof;
}
Expand Down Expand Up @@ -160,7 +151,7 @@ async function approveMessages(keypair, client, config, chain, contractConfig, a
throw new Error('Multisig session not completed');
}

const executeData = executeDataStruct.parse(arrayify('0x' + status.completed.execute_data));
const executeData = bcsStructs.gateway.ExecuteData.parse(arrayify('0x' + status.completed.execute_data));

const tx = new Transaction();

Expand All @@ -183,14 +174,14 @@ async function approve(keypair, client, config, chain, contractConfig, args, opt
const [sourceChain, messageId, sourceAddress, destinationId, payloadHash] = args;

const encodedMessages = bcs
.vector(messageStruct)
.vector(bcsStructs.gateway.ApprovedMessageZ)
.serialize([
{
source_chain: sourceChain,
message_id: messageId,
source_address: sourceAddress,
destination_id: destinationId,
payload_hash: bytes32Struct.serialize(arrayify(payloadHash)).toBytes(),
payload_hash: bcsStructs.common.Bytes32.serialize(arrayify(payloadHash)).toBytes(),
},
])
.toBytes();
Expand Down Expand Up @@ -222,7 +213,7 @@ async function submitProof(keypair, client, config, chain, contractConfig, args,
throw new Error('Multisig session not completed');
}

const executeData = executeDataStruct.parse(arrayify('0x' + status.completed.execute_data));
const executeData = bcsStructs.gateway.ExecuteData.parse(arrayify('0x' + status.completed.execute_data));

const tx = new Transaction();

Expand Down Expand Up @@ -263,12 +254,10 @@ async function rotate(keypair, client, config, chain, contractConfig, args, opti
const signers = await getSigners(keypair, config, chain.axelarId, options);

const newNonce = options.newNonce ? keccak256(toUtf8Bytes(options.newNonce)) : signers.nonce;
const encodedSigners = signersStruct
.serialize({
...signers,
nonce: bytes32Struct.serialize(newNonce).toBytes(),
})
.toBytes();
const encodedSigners = bcsStructs.gateway.WeightedSigners.serialize({
...signers,
nonce: bcsStructs.common.Bytes32.serialize(newNonce).toBytes(),
}).toBytes();

const encodedProof = getProof(keypair, COMMAND_TYPE_ROTATE_SIGNERS, encodedSigners, contractConfig, options);

Expand Down
4 changes: 2 additions & 2 deletions sui/gmp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { Command } = require('commander');
const { Transaction } = require('@mysten/sui/transactions');
const { bcs } = require('@mysten/sui/bcs');
const { bcsStructs } = require('@axelar-network/axelar-cgp-sui');
const { loadConfig, saveConfig, printInfo } = require('../common/utils');
const {
getBcsBytesByObjectId,
Expand All @@ -9,7 +10,6 @@ const {
getUnitAmount,
getWallet,
printWalletInfo,
discoveryStruct,
broadcast,
} = require('./utils');
const { ethers } = require('hardhat');
Expand Down Expand Up @@ -68,7 +68,7 @@ async function execute(keypair, client, contracts, args, options) {

// Get Discovery table id from discovery object
const tableBcsBytes = await getBcsBytesByObjectId(client, discoveryObjectId);
const { fields } = discoveryStruct.parse(tableBcsBytes);
const { fields } = bcsStructs.common.Discovery.parse(tableBcsBytes);
const tableId = fields.id;

// Get the transaction list from the discovery table
Expand Down
8 changes: 0 additions & 8 deletions sui/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +0,0 @@
module.exports = {
...require('./amount-utils'),
...require('./sign-utils'),
...require('./types-utils'),
...require('./utils'),
...require('./upgrade-utils'),
...require('./cli-utils'),
};
149 changes: 0 additions & 149 deletions sui/utils/types-utils.js

This file was deleted.

9 changes: 4 additions & 5 deletions sui/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ const {
const fs = require('fs');
const { fromB64 } = require('@mysten/bcs');
const { CosmWasmClient } = require('@cosmjs/cosmwasm-stargate');
const { updateMoveToml, copyMovePackage, TxBuilder } = require('@axelar-network/axelar-cgp-sui');
const { singletonStruct, itsStruct, squidStruct } = require('./types-utils');
const { updateMoveToml, copyMovePackage, TxBuilder, bcsStructs } = require('@axelar-network/axelar-cgp-sui');

const suiPackageAddress = '0x2';
const suiClockAddress = '0x6';
Expand Down Expand Up @@ -131,19 +130,19 @@ const getObjectIdsByObjectTypes = (txn, objectTypes) =>
// Parse bcs bytes from singleton object which is created when the Test contract is deployed
const getSingletonChannelId = async (client, singletonObjectId) => {
const bcsBytes = await getBcsBytesByObjectId(client, singletonObjectId);
const data = singletonStruct.parse(bcsBytes);
const data = bcsStructs.common.Singleton.parse(bcsBytes);
return '0x' + data.channel.id;
};

const getItsChannelId = async (client, itsObjectId) => {
const bcsBytes = await getBcsBytesByObjectId(client, itsObjectId);
const data = itsStruct.parse(bcsBytes);
const data = bcsStructs.its.ITS.parse(bcsBytes);
return '0x' + data.channel.id;
};

const getSquidChannelId = async (client, squidObjectId) => {
const bcsBytes = await getBcsBytesByObjectId(client, squidObjectId);
const data = squidStruct.parse(bcsBytes);
const data = bcsStructs.squid.Squid.parse(bcsBytes);
return '0x' + data.channel.id;
};

Expand Down

0 comments on commit 1b0a2ec

Please sign in to comment.