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.