Skip to main content

Write

You can make a "transaction" interacting with a Smart Contract when you **need to update some state ** in the contract.

Import the web3 and @kaiachain/web3js-ext packages to add kaia features on web3

Define sender's address and private key

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

Also, you can change the provider URL from baobab to allthatnode

Create a sender's account using web3.eth.accounts.privateKeyToAccount

Define contract Abi and address to interact with

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

Get the number before update

Create a transaction object that increment the number

Sign the transaction with sender account

Send the transaction to network and wait for the receipt

Get the number after update

smartContractWrite.js

const { Web3 } = require("@kaiachain/web3js-ext");
const senderAddr = "0x24e8efd18d65bcb6b3ba15a4698c0b0d69d13ff7";
const senderPriv = "0x4a72b3d09c3d5e28e8652e0111f9c4ce252e8299aad95bb219a38eb0a3f4da49";
const provider = new Web3.providers.HttpProvider("https://public-en-kairos.node.kaia.io");
const web3 = new Web3(provider);
const senderAccount = web3.eth.accounts.privateKeyToAccount(senderPriv);
/* compiled in remix.ethereum.org (compiler: 0.8.18, optimizer: false)
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
contract Counter {
uint256 public number;
event SetNumber(uint256 number);
constructor(uint256 initNumber) {
number = initNumber;
}
function setNumber(uint256 newNumber) public {
number = newNumber;
emit SetNumber(number);
}
function increment() public {
number++;
emit SetNumber(number);
}
}
*/
const contractAbi = JSON.parse('[{"inputs":[{"internalType":"uint256","name":"initNumber","type":"uint256"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"number","type":"uint256"}],"name":"SetNumber","type":"event"},{"inputs":[],"name":"increment","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"number","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"newNumber","type":"uint256"}],"name":"setNumber","outputs":[],"stateMutability":"nonpayable","type":"function"}]');
const contractAddr = "0x95Be48607498109030592C08aDC9577c7C2dD505";
async function main() {
const contract = new web3.eth.Contract(contractAbi, contractAddr);
console.log("number before", (await contract.methods.number().call()).toString());
const data = contract.methods.increment().encodeABI();
const tx = {
from: senderAddr,
to: contractAddr,
data: data,
};
const signResult = await senderAccount.signTransaction(tx);
console.log("signedTx", signResult.transactionHash);
const receipt = await web3.eth.sendSignedTransaction(signResult.rawTransaction);
console.log("receipt", receipt);
console.log("number after", (await contract.methods.number().call()).toString());
}
main();

output

❯ node smartContractWrite.js
number before 294
signedTx 0x842b7043b185e1e9506529f4562f27e70d6a4f1bd8fd0971e844f70a2d103eb6
receipt {
blockHash: '0xa6e2b651bfd1f04ca7711f43eb650363a33fbd626314164bceb9e71ac6c709fa',
blockNumber: 148743520n,
cumulativeGasUsed: 28014n,
effectiveGasPrice: 25000000000n,
from: '0x24e8efd18d65bcb6b3ba15a4698c0b0d69d13ff7',
gasUsed: 28014n,
logs: [
{
address: '0x95be48607498109030592c08adc9577c7c2dd505',
topics: [Array],
data: '0x0000000000000000000000000000000000000000000000000000000000000127',
blockNumber: 148743520n,
transactionHash: '0x842b7043b185e1e9506529f4562f27e70d6a4f1bd8fd0971e844f70a2d103eb6',
transactionIndex: 0n,
blockHash: '0xa6e2b651bfd1f04ca7711f43eb650363a33fbd626314164bceb9e71ac6c709fa',
logIndex: 0n,
removed: false
}
],
logsBloom: '0x00000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000001200000002',
status: 1n,
to: '0x95be48607498109030592c08adc9577c7c2dd505',
transactionHash: '0x842b7043b185e1e9506529f4562f27e70d6a4f1bd8fd0971e844f70a2d103eb6',
transactionIndex: 0n,
type: 2n
}
number after 295