Transactions are a formal action on a blockchain. They are always initiated in Kaikas with a call to the klay_sendTransaction
method. They can involve a simple sending of KLAY, may result in sending tokens, creating a new smart contract, or changing state on the blockchain in any number of ways. They are always initiated by a signature from an external account, or a simple key pair.
In Kaikas, there are several ways to make transactions. First one is to use klaytn.sendAsync
method to directly invoke klay_sendTransaction
. This method wraps RPC API and sends it to browser's Caver instance.
const transactionParameters = {gas: '0x2710',to: '0x0000000000000000000000000000000000000000',from: klaytn.selectedAddress,value: '0xff'}​klaytn.sendAsync({method: 'klay_sendTransaction',params: [transactionParameters],from: klaytn.selectedAddress},callback)
Another way (more recommanded) is to use Caver. Pass window.klaytn
, or just klaytn
, when creating Caver instance. After that, you can use that instance when interacting with Klaytn network. Below is an example of sending 1 KLAY. Install Caver via npm install caver-js
. You can choose whatever version of Caver you want to use.
import Caver from 'caver-js' // or const Caver = require('caver-js')const caver = new Caver(klaytn)caver.klay.sendTransaction({type: 'VALUE_TRANSFER',from: klaytn.selectedAddress,to: '0x0000000000000000000000000000000000000000',value: caver.utils.toPeb('1', 'KLAY'),gas: 8000000}).once('transactionHash', transactionHash => {console.log('txHash', transactionHash)}).once('receipt', receipt => {console.log('receipt', receipt)}).once('error', error => {console.log('error', error)})
Even without installing caver-js, you can use injected caver. If user has Kaikas installed, caver will be available as a global variable. This caver's version is 1.4.0.
window.caver.sendTransaction({type: 'VALUE_TRANSFER',from: klaytn.selectedAddress,to: '0x0000000000000000000000000000000000000000',value: caver.utils.toPeb('1', 'KLAY'),gas: 8000000})
From the user's perspective, the following pop-up will be shown. If the user confirms, Kaikas will sign the transaction and send it to Klaytn blockchain network.