Smart Contract Execution
TxTypeSmartContractExecution executes a smart contract with the given data. TxTypeSmartContractExecution is accepted only if "to" is a smart contract account.
Fee Delegation
Import the @kaiachain/viem-ext packages to add kaia features on web3
Initializes a public client for read-only interactions with the Kaia blockchain.
Set up sender and fee payer wallets 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, specifying the sender’s account, recipient address, value to transfer (0 KLAY in this example) and transaction type (TxType.FeeDelegatedSmartContractDeploy)
Signing the transaction with the wallet client’s signTransaction method.
The sender submits the transaction to the Kaia blockchain using klay_sendRawTransaction
, and the result (typically a transaction hash) is logged. The sender pays the transaction fees.
Prepares a fee-delegated transaction (FeeDelegatedSmartContractExecution
) to call the same setNumber
function on the contract. The setup is similar to the non-fee-delegated transaction, but the fee payer signs the transaction to cover the fees.
The sender signs the transaction first, the fee payer adds their signature, and the transaction is submitted to the Kaia blockchain.
Uses the public client to read the value of the number variable from the contract by calling the number
function (a view function that doesn’t modify state).