From 9a6951c995dd31bac99b270c6056bce6715332c4 Mon Sep 17 00:00:00 2001 From: KOSASIH Date: Sat, 27 Jul 2024 07:26:09 +0700 Subject: [PATCH] Create NexusCrossChainMessageContract.js --- .../NexusCrossChainMessageContract.js | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 blockchain_integration/pi_network/pi-network-interoperability/bridge-contracts/nexus-smart-chain/NexusCrossChainMessageContract.js diff --git a/blockchain_integration/pi_network/pi-network-interoperability/bridge-contracts/nexus-smart-chain/NexusCrossChainMessageContract.js b/blockchain_integration/pi_network/pi-network-interoperability/bridge-contracts/nexus-smart-chain/NexusCrossChainMessageContract.js new file mode 100644 index 000000000..0deafd500 --- /dev/null +++ b/blockchain_integration/pi_network/pi-network-interoperability/bridge-contracts/nexus-smart-chain/NexusCrossChainMessageContract.js @@ -0,0 +1,34 @@ +const Web3 = require('web3'); +const Ethers = require('ethers'); + +class NexusCrossChainMessageContract { + constructor(nexusCrossChainMessageContractAddress, providerUrl) { + this.nexusCrossChainMessageContractAddress = nexusCrossChainMessageContractAddress; + this.web3 = new Web3(new Web3.providers.HttpProvider(providerUrl)); + this.ethersProvider = new Ethers.providers.JsonRpcProvider(providerUrl); + } + + async sendMessage(messageId, message) { + const txCount = await this.web3.eth.getTransactionCount(this.nexusCrossChainMessageContractAddress); + const tx = { + from: this.nexusCrossChainMessageContractAddress, + to: this.nexusCrossChainMessageContractAddress, + value: '0', + gas: '20000', + gasPrice: '20', + nonce: txCount, + data: `0x${messageId.toString(16)}${message}` + }; + const signedTx = await this.ethersProvider.signTransaction(tx, '0x1234567890abcdef'); + await this.ethersProvider.sendTransaction(signedTx); + } + + async getMessage(messageId) { + return this.web3.eth.call({ + to: this.nexusCrossChainMessageContractAddress, + data: `0x${messageId.toString(16)}` + }); + } +} + +module.exports = NexusCrossChainMessageContract;