Skip to content

Commit

Permalink
feat: add execute command
Browse files Browse the repository at this point in the history
  • Loading branch information
npty committed Jul 15, 2024
1 parent 1510825 commit 7803c38
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
52 changes: 49 additions & 3 deletions sui/gmp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { saveConfig, printInfo } = require('../evm/utils');
const { Command } = require('commander');
const { TransactionBlock } = require('@mysten/sui.js/transactions');
const { approvedMessageStruct } = require('./types-utils');
const { bcs } = require('@mysten/sui.js/bcs');
const { loadSuiConfig } = require('./utils');
const { ethers } = require('hardhat');
Expand Down Expand Up @@ -37,11 +38,45 @@ async function sendCommand(chain, args, options) {
printInfo('Call sent', receipt.digest);
}

async function execute(chain, args, options) {
const [keypair, client] = getWallet(chain, options);

await printWalletInfo(keypair, client, chain, options);

const [sourceChain, messageId, sourceAddress, destinationId, payload] = args;

const encodedMessage = approvedMessageStruct
.serialize({
source_chain: sourceChain,
message_id: messageId,
source_address: sourceAddress,
destination_id: destinationId,
payload,
})
.toBytes();

const testConfig = chain.contracts.test;
const singletonObjectId = testConfig.objects.singleton;

const tx = new TransactionBlock();
tx.moveCall({
target: `${chain.contracts.test.address}::test::execute`,
arguments: [tx.pure(bcs.vector(bcs.u8()).serialize(encodedMessage).toBytes()), tx.object(singletonObjectId)],
});

const receipt = await broadcast(client, keypair, tx);

printInfo('Call executed', receipt.digest);
}

async function processCommand(command, chain, args, options) {
switch (command) {
case 'send-call':
printInfo('Action', 'Sending call');
printInfo('Action', 'Send Call');
return sendCommand(chain, args, options);
case 'execute':
printInfo('Action', 'Execute');
return execute(chain, args, options);
default:
throw new Error(`Unknown command: ${command}`);
}
Expand All @@ -57,17 +92,28 @@ if (require.main === module) {
const program = new Command();
program.name('gmp').description('Example of SUI gmp commands');

const sendCallProgram = program
const sendCallProgram = new Command()
.name('send-call')
.description('Example of SUI contract call')
.description('Send gmp contract call')
.command('send-call <destChain> <destContractAddress> <payload>');

const executeCommand = new Command()
.name('execute')
.description('Execute gmp contract call')
.command('execute <sourceChain> <messageId> <sourceAddress> <destinationId> <payload>');

addBaseOptions(sendCallProgram);
addBaseOptions(executeCommand);

sendCallProgram.action((destChain, destContractAddress, payload, options) => {
mainProcessor('send-call', options, [destChain, destContractAddress, payload], processCommand);
});

executeCommand.action((sourceChain, messageId, sourceAddress, destinationId, payload, options) => {
mainProcessor('execute', options, [sourceChain, messageId, sourceAddress, destinationId, payload], processCommand);
});

program.addCommand(sendCallProgram);
program.addCommand(executeCommand);
program.parse();
}
9 changes: 9 additions & 0 deletions sui/types-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ const messageStruct = bcs.struct('Message', {
payload_hash: bytes32Struct,
});

const approvedMessageStruct = bcs.struct('ApprovedMessage', {
source_chain: bcs.string(),
message_id: bcs.string(),
source_address: bcs.string(),
destination_id: addressStruct,
payload: bcs.vector(bcs.u8()),
});

const proofStruct = bcs.struct('Proof', {
signers: signersStruct,
signatures: bcs.vector(bcs.vector(bcs.u8())),
Expand All @@ -64,6 +72,7 @@ module.exports = {
signersStruct,
messageToSignStruct,
messageStruct,
approvedMessageStruct,
proofStruct,
gasServiceStruct,
};

0 comments on commit 7803c38

Please sign in to comment.