From 7803c382ed84c9d907f08934f2d87c3ca961bd8d Mon Sep 17 00:00:00 2001 From: npty Date: Mon, 15 Jul 2024 19:57:04 +0700 Subject: [PATCH] feat: add execute command --- sui/gmp.js | 52 +++++++++++++++++++++++++++++++++++++++++++--- sui/types-utils.js | 9 ++++++++ 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/sui/gmp.js b/sui/gmp.js index 9d20faffd..ecee34799 100644 --- a/sui/gmp.js +++ b/sui/gmp.js @@ -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'); @@ -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}`); } @@ -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 '); + const executeCommand = new Command() + .name('execute') + .description('Execute gmp contract call') + .command('execute '); + 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(); } diff --git a/sui/types-utils.js b/sui/types-utils.js index b5a7cb1a1..545a6df50 100644 --- a/sui/types-utils.js +++ b/sui/types-utils.js @@ -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())), @@ -64,6 +72,7 @@ module.exports = { signersStruct, messageToSignStruct, messageStruct, + approvedMessageStruct, proofStruct, gasServiceStruct, };