Skip to main content

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 web3 and @kaiachain/web3js-ext packages to add kaia features on web3

Define sender address and sender private key

Define contract address and Abi, you can retreive it from block explorer and compiled solidity code

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

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

Create a KlaytnWeb3 instance using the provider

Convert the sender's private key to an account using web3.eth.accounts.privateKeyToAccount

Create a contract instance using Web3.eth.Contract with defined contractAbi and address. You can read and write the contract through this instance

Encode the function data for the "setNumber" function to convert the data to bytes

Define a transaction object for the contract execution, params type: TxType.SmartContractExecution, from: senderAddr, to: contractAddr, data: data are required

Sign the transaction with the sender's private key

Send the signed transaction to blockchain. It will return the transaction receipt

SmartContractExecution.js

const { KlaytnWeb3, TxType } = require("@kaiachain/web3js-ext");
const { Web3 } = require("web3");
const senderAddr = "0xa2a8854b1802d8cd5de631e690817c253d6a9153";
const senderPriv = "0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8";
const contractAddr = "0xD7fA6634bDDe0B2A9d491388e2fdeD0fa25D2067";
const contractAbi = [
{
"inputs": [
{
"internalType": "uint256",
"name": "newNumber",
"type": "uint256"
}
],
"name": "setNumber",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
];
async function main() {
const provider = new Web3.providers.HttpProvider("https://public-en-kairos.node.kaia.io");
const web3 = new KlaytnWeb3(provider);
const senderAccount = web3.eth.accounts.privateKeyToAccount(senderPriv);
// https://web3js.readthedocs.io/en/v1.2.11/web3-eth-contract.html#methods-mymethod-encodeabi
const contract = new web3.eth.Contract(contractAbi, contractAddr);
const data = contract.methods.setNumber(0x123).encodeABI();
const tx = {
type: TxType.SmartContractExecution,
from: senderAddr,
to: contractAddr,
data: data,
};
const signResult = await senderAccount.signTransaction(tx);
console.log("rawTx", signResult.rawTransaction);
const receipt = await web3.eth.sendSignedTransaction(signResult.rawTransaction);
console.log("receipt", receipt);
}
main();

output

❯ node SmartContractExecution.js
signedTx 0x6e3843d8fe5ef53b4642d82357d423dfb2741a96a9a74307df2bd7caee5659f0
receipt {
blockHash: '0x4670feb1301e6b726568e81ea084b2b1c7d281c8d989faec8522d67af0cc8870',
blockNumber: 148742541n,
cumulativeGasUsed: 26990n,
effectiveGasPrice: 25000000000n,
from: '0xa2a8854b1802d8cd5de631e690817c253d6a9153',
gasUsed: 26990n,
logs: [],
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
status: 1n,
to: '0xd7fa6634bdde0b2a9d491388e2fded0fa25d2067',
transactionHash: '0x6e3843d8fe5ef53b4642d82357d423dfb2741a96a9a74307df2bd7caee5659f0',
transactionIndex: 0n,
type: 0n
}

Make this page better