Kaikas Docs
Search…
Sending Transactions
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.
1
const transactionParameters = {
2
gas: '0x2710',
3
to: '0x0000000000000000000000000000000000000000',
4
from: klaytn.selectedAddress,
5
value: '0xff'
6
}
7
8
klaytn.sendAsync(
9
{
10
method: 'klay_sendTransaction',
11
params: [transactionParameters],
12
from: klaytn.selectedAddress
13
},
14
callback
15
)
Copied!
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.
1
import Caver from 'caver-js' // or const Caver = require('caver-js')
2
const caver = new Caver(klaytn)
3
caver.klay
4
.sendTransaction({
5
type: 'VALUE_TRANSFER',
6
from: klaytn.selectedAddress,
7
to: '0x0000000000000000000000000000000000000000',
8
value: caver.utils.toPeb('1', 'KLAY'),
9
gas: 8000000
10
})
11
.once('transactionHash', transactionHash => {
12
console.log('txHash', transactionHash)
13
})
14
.once('receipt', receipt => {
15
console.log('receipt', receipt)
16
})
17
.once('error', error => {
18
console.log('error', error)
19
})
Copied!
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.
1
window.caver.sendTransaction({
2
type: 'VALUE_TRANSFER',
3
from: klaytn.selectedAddress,
4
to: '0x0000000000000000000000000000000000000000',
5
value: caver.utils.toPeb('1', 'KLAY'),
6
gas: 8000000
7
})
Copied!
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.
Sending Transactions
Last modified 1yr ago
Copy link