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';
const RPC_ENDPOINT = 'YOUR_RPC_ENDPOINT';
const kardiaClient = new KardiaClient({ endpoint: RPC_ENDPOINT });
// Get your wallet's latest nonce
const nonce = await kardiaClient.account.getNonce('YOUR_WALLET_ADDRESS');
const txPayload = {
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
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)
);