Every action on blockchain can be considered as a transaction: sending native token, sending utility tokens, calling a smart contract, creating an NFT...
To estimate gas limit for your transaction, use estimateGas method from transaction module
const transactionModule = kardiaClient.transaction
const txPayload = {
to: 'RECEIVER_WALLET_ADDRESS',
from: 'YOUR_WALLET_ADDRESS',
gasPrice: oracleGasPrice, // Gas price from above step
value: 231095, // Amount of KAI to send
};
const txData = '0x12'; // Transaction data, this will usually be used when you want to execute a smart contract call
const estimatedGas = await transactionModule.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
// };
const parsedPayload = transactionModule.generateTransaction(txPayload);
Then you can sign your transaction and send to KardiaChain network
const signedTx = transactionModule.signTransaction(parsedPayload, 'YOUR_PRIVATE_KEY');
// Send transaction to network and get transaction hash immediately
const txHash = await transactionModule.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 {
const receipt = await transactionModule.getTransactionReceipt(txHash);
if (receipt) {
return receipt; // Now your transaction is mined into a new block
} else {
await sleep(1000); //
}
} catch (err) {
// Handle error here
}
}
All the above steps can be combined by using sendTransaction method in transaction module
const txPayload = {
to: 'RECEIVER_WALLET_ADDRESS',
from: 'YOUR_WALLET_ADDRESS',
value: 231095, // Amount of KAI to send
}
const txResult = await transactionModule.sendTransaction(
txPayload,
'YOUR_WALLET_PRIVATEKEY',
true, // Flag to indicate if you want to wait for the transaction to complete
50000 // Time (in ms) you want to wait for transaction to complete, default will be 300000 (300s)
);