From 2122bd506587fef0306b43240473ba6b3a5672e6 Mon Sep 17 00:00:00 2001 From: Otniel Nicola Date: Mon, 29 Dec 2025 16:03:57 +0200 Subject: [PATCH] feat: Add N3 examples of MessageBridge usage in JS --- bridge/messaging-bridge/evm-to-n3.md | 48 +++++++++++++++++----------- bridge/messaging-bridge/n3-to-evm.md | 15 ++++++--- 2 files changed, 40 insertions(+), 23 deletions(-) diff --git a/bridge/messaging-bridge/evm-to-n3.md b/bridge/messaging-bridge/evm-to-n3.md index 3992307..1e4b222 100644 --- a/bridge/messaging-bridge/evm-to-n3.md +++ b/bridge/messaging-bridge/evm-to-n3.md @@ -122,29 +122,41 @@ Once the result is transferred, it can be read on the EVM chain. ## Example Implementation (JavaScript) ```javascript -const { ethers } = require('ethers'); +const {ethers} = require('ethers'); // Define the target contract address and the account address to check the balance for. const neoTokenContractAddress = "0xef4073a0f2b305a38ec4050e4d3d28bc40ea63f5"; // Neo token address on N3 const targetAddress = "0xabcdefabcdefabcdefabcdefabcdefabcdefabcd"; // Address to check the balance for -function exampleFunc() { - // TBD: Serialize an N3 method call using Javascript - // Invoke the N3 MessageBridge contract's function serializeN3MethodCall() to get the bytes needed. - // const rawMessage = n3MessageBridge.serializeN3MethodCall(target, method, callFlags, args); - const rawMessage = "0x400428141418e358c565207768eae8d237241e85d3e9f1cb280573746f72652101024002210101210440a87c68"; - - const storeResult = true; - const sendingFee = ethers.parseEther("0.1"); - await evmMessageBridge.connect(sender).sendExecutableMessage(rawMessage, storeResult, {value: sendingFee, maxFeePerGas, maxPriorityFeePerGas}); - - // Wait until the message arrives on N3... - - // TBD: Execute message on N3 using Javascript - // Invoke the method executeMessage() on the MessageBridge contract on N3 using the assigned message nonce when the message was sent. - - // TBD: Send the result back from N3 to EVM - // Invoke the method sendResultMessage() on the MessageBridge contract on N3 using the assigned message nonce when the message was sent initially. +async function exampleFunc() { + // Assume evmMessageBridge and n3MessageBridge are already instantiated contract objects + + // Serialize an N3 method call using the N3 MessageBridge + const rawMessage = await n3MessageBridge.serializeCall( + neoTokenContractAddress, // Target contract (NEO token) + 'balanceOf', // Method to call + 15, // CallFlags.ALL - can be adjusted as needed + [targetAddress] // Arguments + ); + // const rawMessage = "0x400428141418e358c565207768eae8d237241e85d3e9f1cb280573746f72652101024002210101210440a87c68"; // Example serialized message + + const storeResult = true; + let sendingFee = ethers.parseEther("0.1"); + const nonce = await evmMessageBridge.connect(evmSender) + .sendExecutableMessage(rawMessage, storeResult, {value: sendingFee, maxFeePerGas, maxPriorityFeePerGas}); + + // Wait until the message arrives on N3... + + // Execute message on N3 using Javascript + await n3MessageBridge.executeMessage(nonce); + + // Send the result back from N3 to EVM + sendingFee = await n3MessageBridge.sendingFee(); + const params: SendResultMessageParams = { + nonce: nonce, + maxFee: sendingFee + }; + await n3MessageBridge.sendResultMessage(params); } ``` diff --git a/bridge/messaging-bridge/n3-to-evm.md b/bridge/messaging-bridge/n3-to-evm.md index 74f6de5..3c9a1c9 100644 --- a/bridge/messaging-bridge/n3-to-evm.md +++ b/bridge/messaging-bridge/n3-to-evm.md @@ -137,7 +137,7 @@ Once the result is transferred, it can be read on-chain on N3. ## Example Implementation (JavaScript) ```javascript -const { ethers } = require('ethers'); +const {ethers} = require('ethers'); // Define the Neo token contract address on EVM and target address const neoTokenContractAddress = "0xc28736dc83f4fd43d6fb832Fd93c3eE7bB26828f"; // Neo token address on Neo X Testnet @@ -148,12 +148,17 @@ const erc20Interface = new ethers.Interface([ "function balanceOf(address account) view returns (uint256)" ]); -function exampleFunc() { +async function exampleFunc() { const encodedMessage = getEncodedMessage("0xc28736dc83f4fd43d6fb832Fd93c3eE7bB26828f", "0xabcdefabcdefabcdefabcdefabcdefabcdefabcd"); - // TBD: Send message on N3 using Javascript - // Invoke the method sendExecutableMessage on the MessageBridge contract on N3 using the encodedMessage bytes. - // const nonce = n3MessageBridge.sendExecutableMessage(messageBytes, true, sender, messageFee); + // Send message on N3 using Javascript + const sendingFee = await messageBridge.sendingFee(); + const params: SendExecutableMessageParams = { + encodedMessage, + true, // storeResult + maxFee: sendingFee + }; + await messageBridge.sendExecutableMessage(params) // Wait until the message arrives on the EVM chain...