Nhảy tới nội dung
This page uses machine translation from English, which may contain errors or unclear language. For the most accurate information, please see the original English version. Some content may be in the original English due to frequent updates. Help us improve this page's translation by joining our effort on Crowdin. (Crowdin translation page, Contributing guide)

Smart Contract Execution

TxTypeSmartContractExecution executes a smart contract with the given data. TxTypeSmartContractExecution is accepted only if "to" is a smart contract account.

Import the ethers and @kaiachain/ethers-ext modules to add kaia features on ethers.js.

Define sender address and sender private key

Set up the provider with the specified kairos testnet URL. A provider in ethers is a read-only abstraction to access the blockchain data.

Also, you can change the provider URL from kairos to quicknode

Create a sender's wallet using the sender's private key and the provider

Set the contract address you want to execute into the to field and set ABI

Create a contract instance with ethers.Contract, fill in params contractAddr, abi, provider. You can read and write the contract through this instance

Encode the function name and parameter with the encodeFunctionData function

Declare a transaction object

Send the tx to the blockchain. Function sendTransaction internally signs with the private key of the account and then transmits it to the blockchain network.

The wait function returns the tx receipt if it is completed in the blockchain.

smartContractExecution.js

const ethers = require("ethers");
const { Wallet, TxType } = require("@kaiachain/ethers-ext/v6");
const senderAddr = "0xa2a8854b1802d8cd5de631e690817c253d6a9153";
const senderPriv = "0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8";
const provider = new ethers.JsonRpcProvider("https://public-en-kairos.node.kaia.io");
const wallet = new Wallet(senderPriv, provider);
const contractAddr = "0x95Be48607498109030592C08aDC9577c7C2dD505";
const abi = ["function setNumber(uint256 newNumber)"];
async function main() {
const contract = new ethers.Contract(contractAddr, abi, provider);
const data = contract.interface.encodeFunctionData("setNumber", ["0x123"]);
const tx = {
type: TxType.SmartContractExecution,
from: senderAddr,
to: contractAddr,
value: 0,
data: data,
};
const sentTx = await wallet.sendTransaction(tx);
console.log("sentTx", sentTx.hash);
const receipt = await sentTx.wait();
console.log("receipt", receipt);
}
main();

output

❯ node smartContractExecution.js
sentTx 0x6ee58de9d1fd46da6f595112cc6ce060ef560796f78650e8a18fb32f20ec5343
receipt {
to: '0x95Be48607498109030592C08aDC9577c7C2dD505',
from: '0xA2a8854b1802D8Cd5De631E690817c253d6a9153',
contractAddress: null,
transactionIndex: 3,
gasUsed: BigNumber { _hex: '0x6f49', _isBigNumber: true },
logsBloom: '0x00000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000001200000002',
blockHash: '0xb71bcb74a6772501913302fb30d754bdf82cf2996ba9418b509bfd48b275bb0c',
transactionHash: '0x6ee58de9d1fd46da6f595112cc6ce060ef560796f78650e8a18fb32f20ec5343',
logs: [
{
transactionIndex: 3,
blockNumber: 148721006,
transactionHash: '0x6ee58de9d1fd46da6f595112cc6ce060ef560796f78650e8a18fb32f20ec5343',
address: '0x95Be48607498109030592C08aDC9577c7C2dD505',
topics: [Array],
data: '0x0000000000000000000000000000000000000000000000000000000000000123',
logIndex: 9,
blockHash: '0xb71bcb74a6772501913302fb30d754bdf82cf2996ba9418b509bfd48b275bb0c'
}
],
blockNumber: 148721006,
confirmations: 2,
cumulativeGasUsed: BigNumber { _hex: '0x094000', _isBigNumber: true },
effectiveGasPrice: BigNumber { _hex: '0x05d21dba00', _isBigNumber: true },
status: 1,
type: 0,
byzantium: true
}

Cải thiện trang này