Every action on blockchain can be considered as a transaction: sending native token, sending utility tokens, calling a smart contract, creating an NFT...
Sending a basic transaction can be done by following these steps:
Get your wallet's latest nonce
Estimate gas price
Estimate gas limit
Parse your payload to KardiaChain RPC protocol format
Sign your transaction
Send signed transaction to network
To get wallet's latest nonce, use account module
import KardiaClient from'kardia-js-sdk';constRPC_ENDPOINT='YOUR_RPC_ENDPOINT';constkardiaClient=newKardiaClient({ endpoint:RPC_ENDPOINT });// Get your wallet's latest nonceconstnonce=awaitkardiaClient.account.getNonce('YOUR_WALLET_ADDRESS');consttxPayload= { to:'RECEIVER_WALLET_ADDRESS', gas:50000,// Gas limit nonce, gasPrice:1000000000,// Gas price value:231095,// Amount of KAI to send};
In most cases, estimating gas price and gas limit is pretty complex. Luckily, this can be done using the SDK.
To get recommended gas price from oracle, use the getGasPrice method from KAIChain module.
To estimate gas limit for your transaction, use estimateGas method from transaction module
consttransactionModule=kardiaClient.transactionconsttxPayload= { to:'RECEIVER_WALLET_ADDRESS', from:'YOUR_WALLET_ADDRESS', gasPrice: oracleGasPrice,// Gas price from above step value:231095,// Amount of KAI to send};consttxData='0x12'; // Transaction data, this will usually be used when you want to execute a smart contract callconstestimatedGas=awaittransactionModule.estimateGas(txPayload, txData);
After this, your transaction payload is complete. Next you need to parse your payload to match KardiaChain RPC protocol. This can be done using generateTransaction utility
txPayload.gas = estimatedGas // Gas from above step// txPayload now should be// {// to: 'RECEIVER_WALLET_ADDRESS',// from: 'YOUR_WALLET_ADDRESS',// gasPrice: oracleGasPrice, // Gas price from above step// value: 231095, // Amount of KAI to send// gas: estimatedGas// };constparsedPayload=transactionModule.generateTransaction(txPayload);
Then you can sign your transaction and send to KardiaChain network
constsignedTx=transactionModule.signTransaction(parsedPayload,'YOUR_PRIVATE_KEY');// Send transaction to network and get transaction hash immediatelyconsttxHash=awaittransactionModule.sendRawTransaction(signedTx);// txHash should be something like '0x0a2db5831c314363a97a79f416061a9daec5230f8b6306cd1c431b467c42f820'
At this moment, your transaction has been submitted but not mined into a new block. To ensure that, you can create a loop to check for transaction receipt
while (true) {try {constreceipt=awaittransactionModule.getTransactionReceipt(txHash);if (receipt) {return receipt; // Now your transaction is mined into a new block } else {awaitsleep(1000); // } } catch (err) {// Handle error here }}
All the above steps can be combined by using sendTransaction method in transaction module
consttxPayload= { to:'RECEIVER_WALLET_ADDRESS', from:'YOUR_WALLET_ADDRESS', value:231095,// Amount of KAI to send}consttxResult=awaittransactionModule.sendTransaction( txPayload,'YOUR_WALLET_PRIVATEKEY',true,// Flag to indicate if you want to wait for the transaction to complete50000// Time (in ms) you want to wait for transaction to complete, default will be 300000 (300s));