Kaikas Docs
Search…
Caver Methods

Basic Setup

caver-js is a JavaScript API library that allows developers to interact with a Klaytn node. It is available on npm.
There are two ways of using caver. The first way is to install Caver yourself and wrap it with Klaytn provider. Use the following command to install.
npm install caver-js
In this way, you can use the latest version of Caver. To see the full explanation and APIs regarding caver-js, please refer to this documentation.
1
import Caver from 'caver-js'
2
3
const caver = new Caver(klaytn)
Copied!
Alternatively, you can use Caver provided by Kaikas. In this case, you will be using Caver version 1.4.0.
1
window.caver.sendTransaction(...) // or simply, caver.sendTransaction(...)
Copied!
Make sure to send connect request via klaytn.enable() before signing any transactions.

Common APIs

caver.klay.getBalance

Returns selected account's current balance.
1
const account = provider.selectedAddress
2
const balance = await this.caver.klay.getBalance(account)
Copied!

caver.klay.sign

Generates signed data specific to the Klaytn network. Refer to Klaytn Platform API - klay_sign to know how the signature is generated.
1
const account = provider.selectedAddress
2
const message = 'Message to sign'
3
const signedMessage = await caver.klay.sign(message, from)
Copied!

caver.klay.sendTransaction

Create, sign, and send transaction to Klaytn blockchain network.
When executed, Kaikas asks the user for permission. If the user agrees, Kaikas signs the transaction with user's account key and send it to Klaytn blockchain network. There are 19 transaction types, including legacy type. Legacy type is transaction type that is compatible with Ethereum network, and the others are newly introduced types only available in Klaytn. When using the new types, you have to specify the type. To see the full list of types and their roles, please refer to send transaction.
The following are some common examples.
1
caver.klay.sendTransaction({
2
from: klaytn.selectedAddress,
3
to: '0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe',
4
value: '1000000000000000'
5
})
6
.then(function(receipt){
7
...
8
});
Copied!
1
caver.klay
2
.sendTransaction({
3
type: 'VALUE_TRANSFER',
4
from: klaytn.selectedAddress,
5
to: '0x0000000000000000000000000000000000000000',
6
value: caver.utils.toPeb('1', 'KLAY'),
7
gas: 8000000
8
})
9
.once('transactionHash', transactionHash => {
10
console.log('txHash', transactionHash)
11
})
12
.once('receipt', receipt => {
13
console.log('receipt', receipt)
14
})
15
.once('error', error => {
16
console.log('error', error)
17
})
Copied!
1
caver.klay.sendTransaction({
2
type: 'SMART_CONTRACT_EXECUTION',
3
from: klaytn.selectedAddress,
4
to: '0x1d389d91886fd0af55f44c56e1240eb6162ddff8',
5
data: '0x6353586b0000000000000000000000001d389d91886fd0af55f44c56e1240eb6162ddff8',
6
gas: '300000',
7
value: '0x174876e800',
8
})
9
.then(function(receipt){
10
...
11
});
Copied!

caver.klay.signTransaction

Unlike caver.klay.sendTransaction, this does not send transaction to blockchain. Instead it returns RLP encoded signed transaction. This is useful for transactions that require multiple signatures(eg. fee delegated tx) where you have to get the signed raw transaction first then sign it with another key before sending it.
1
const signedTransaction = await caver.klay.signTransaction({
2
type: 'FEE_DELEGATED_SMART_CONTRACT_EXECUTION',
3
from: klaytn.selectedAddress,
4
to: '0x1d389d91886fd0af55f44c56e1240eb6162ddff8',
5
data:
6
'0x6353586b0000000000000000000000001d389d91886fd0af55f44c56e1240eb6162ddff8',
7
gas: '300000'
8
})
9
const senderRawTransaction = signedTransaction.rawTransaction
10
11
//register address for fee payment
12
caver.klay.accounts.wallet.add(feePayerPrivateKey, feePayerAddress)
13
14
caver.klay
15
.sendTransaction({
16
senderRawTransaction,
17
feePayer: feePayerAddress
18
})
19
.once('transactionHash', transactionHash => {
20
console.log('txHash', transactionHash)
21
})
22
.once('receipt', receipt => {
23
console.log('receipt', receipt)
24
})
25
.once('error', error => {
26
console.log('error', error)
27
})
Copied!

caver.klay.sendSignedTransaction

Sends an already-signed transaction.
1
caver.klay.sendSignedTransaction(signedTransactionData).then(console.log)
Copied!

caver.klay.Contract

caver.klay.Contract is used to easily deploy and interact with contracts on blockchain. Before deployment, you need to prepare contract's bytecode and ABI. If you are not familiar with them, please refer to interacting with contracts.
Contract deployment example
1
const myContract = new caver.klay.Contract(contractABI)
2
3
myContract.deploy({
4
data: '0x12345...', //compiled bytecode
5
arguments: [123, 'My String'] //arguments for constructor
6
})
7
.send({
8
from: klaytn.selectedAddress,
9
gas: 1500000,
10
value: 0,
11
}, function(error, transactionHash) { ... })
12
.on('error', function(error) { ... })
13
.on('transactionHash', function(transactionHash) { ... })
14
.on('receipt', function(receipt) {
15
console.log(receipt.contractAddress) // contains the new contract address
16
})
17
.then(function(newContractInstance) {
18
console.log(newContractInstance.options.address) // instance with the new contract address
19
});
Copied!
Calling transfer method from deployed contract
1
const myContract = new caver.klay.Contract(contractABI, contractAddress)
2
3
myContract.methods.transfer(
4
to, caver.utils.toPeb(amount, 'KLAY')
5
).send({from: klaytn.selectedAddress},
6
function(error, transactionHash) {
7
...
8
});
Copied!
Full list of methods are available at caver documentation.
Last modified 1yr ago