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, fee payer addresses and private keys
Define contract address and abi
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 wallet with the private key using web3.eth.accounts.privateKeyToAccount
Create a fee payer's wallet with the private key using web3.eth.accounts.privateKeyToAccount
Define contractAddr and contractAbi, you can get the contract address and Abi from compiled solidity code or blockchain explorer
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, encodeABI
convert the data into bytecode
Create the transaction object to interact with the contract
Set type: TxType.FeeDelegatedSmartContractExecution
for transaction to change contract state with a fee payer
Set data: data
for earlier encoded function data
Sign the transaction with sender account using signTransaction
Sign the transaction with fee payer account using signTransactionAsFeePayer
Send the transaction to blockchain. It will return the transaction receipt