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.

This type of transaction can create an account, transfer tokens, deploy a smart contract, execute a smart contract, or perform a mix of aforementioned.

Import @kaiachain/viem-ext packages to add kaia features on web3

Create a publicClient using createPublicClient, configured to interact with the Kaia Kairos testnet (kairos) via HTTP transport (http()). The public client is used for read-only operations, such as querying the blockchain or reading smart contract state, without requiring a private key or signing capabilities.

Set up a wallet client using createWalletClient, configured with the Kairos chain, an HTTP transport, and the sender’s private key converted to an account.

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

Encode the function name and parameter with the encodeFunctionData function

Create a transaction request for execute a smart contract using prepareTransactionRequest. Specify the transaction type (TxType.smartContractExecution)

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 publicClient calls the number function on the smart contract, which retrieves the current value stored in the contract’s state.

smartContractExecution.js

import {
http,
encodeFunctionData,
createWalletClient, createPublicClient, kairos,
TxType,
privateKeyToAccount
} from "@kaiachain/viem-ext";
const publicClient = createPublicClient({
chain: kairos,
transport: http(),
});
const senderWallet = createWalletClient({
chain: kairos,
transport: http(),
account: privateKeyToAccount(
"0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8"
),
});
// Example usage
(async () => {
const contractAddr = "0x95Be48607498109030592C08aDC9577c7C2dD505";
const abi = [{ "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 data = encodeFunctionData({
abi,
args: [Date.now()],
functionName: "setNumber",
});
const tx = await senderWallet.prepareTransactionRequest({
type: TxType.SmartContractExecution,
account: senderWallet.account,
to: contractAddr,
value: 0,
data,
});
console.log("preparedTx", tx);
const sentTx = await senderWallet.sendTransaction(tx);
console.log("contract interaction tx", sentTx);
const result = await publicClient.readContract({
address: contractAddr,
abi,
functionName: 'number'
})
console.log('Current contract value', result);
})();

output

❯ node smartContractExecution.js
preparedTx {
type: 48,
account: {
address: '0xA2a8854b1802D8Cd5De631E690817c253d6a9153',
nonceManager: undefined,
sign: [AsyncFunction: sign],
signAuthorization: [AsyncFunction: signAuthorization],
signMessage: [AsyncFunction: signMessage],
signTransaction: [AsyncFunction: signTransaction],
signTypedData: [AsyncFunction: signTypedData],
source: 'privateKey',
type: 'local',
publicKey: '0x04dc9dccbd788c00fa98f7f4082f2f714e799bc0c29d63f04d48b54fe6250453cdaf06ca34ae8714cf3dae06bacdb78c7c2d4054bd38961d40853cd5f15955da79'
},
to: '0x95Be48607498109030592C08aDC9577c7C2dD505',
value: 0,
data: '0x3fb5c1cb000000000000000000000000000000000000000000000000000001977b9b017d',
from: '0xA2a8854b1802D8Cd5De631E690817c253d6a9153',
nonce: 2384,
chainId: 1001,
gas: 27953n,
gasPrice: '0x66720b300',
gasLimit: 69882
}
contract interaction tx 0x50d708b08ae4de72e9e52212eb48ec317032c2d7d06620b615d819ecdf004e8f
Current contract value 1750125336184n

Make this page better