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.

import Caver from 'caver-js'

const caver = new Caver(klaytn)

Alternatively, you can use Caver provided by Kaikas. In this case, you will be using Caver version 1.4.0.

window.caver.sendTransaction(...) // or simply, caver.sendTransaction(...)

Make sure to send connect request via klaytn.enable() before signing any transactions.

Common APIs

caver.klay.getBalance

Returns selected account's current balance.

const account = provider.selectedAddress
const balance = await this.caver.klay.getBalance(account)

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.

const account = provider.selectedAddress
const message = 'Message to sign'
const signedMessage = await caver.klay.sign(message, from)

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.

caver.klay.sendTransaction({
    from: klaytn.selectedAddress,
    to: '0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe',
    value: '1000000000000000'
})
.then(function(receipt){
    ...
});
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)
  })
caver.klay.sendTransaction({
    type: 'SMART_CONTRACT_EXECUTION',
    from: klaytn.selectedAddress,
    to: '0x1d389d91886fd0af55f44c56e1240eb6162ddff8',
    data: '0x6353586b0000000000000000000000001d389d91886fd0af55f44c56e1240eb6162ddff8',
    gas: '300000',
    value: '0x174876e800',
})
.then(function(receipt){
    ...
});

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.

const signedTransaction = await caver.klay.signTransaction({
  type: 'FEE_DELEGATED_SMART_CONTRACT_EXECUTION',
  from: klaytn.selectedAddress,
  to: '0x1d389d91886fd0af55f44c56e1240eb6162ddff8',
  data:
    '0x6353586b0000000000000000000000001d389d91886fd0af55f44c56e1240eb6162ddff8',
  gas: '300000'
})
const senderRawTransaction = signedTransaction.rawTransaction

//register address for fee payment
caver.klay.accounts.wallet.add(feePayerPrivateKey, feePayerAddress)

caver.klay
  .sendTransaction({
    senderRawTransaction,
    feePayer: feePayerAddress
  })
  .once('transactionHash', transactionHash => {
    console.log('txHash', transactionHash)
  })
  .once('receipt', receipt => {
    console.log('receipt', receipt)
  })
  .once('error', error => {
    console.log('error', error)
  })

caver.klay.sendSignedTransaction

Sends an already-signed transaction.

caver.klay.sendSignedTransaction(signedTransactionData).then(console.log)

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

const myContract = new caver.klay.Contract(contractABI)

myContract.deploy({
    data: '0x12345...', //compiled bytecode
    arguments: [123, 'My String'] //arguments for constructor
  })
  .send({
    from: klaytn.selectedAddress,
    gas: 1500000,
    value: 0,
  }, function(error, transactionHash) { ... })
  .on('error', function(error) { ... })
  .on('transactionHash', function(transactionHash) { ... })
  .on('receipt', function(receipt) {
    console.log(receipt.contractAddress) // contains the new contract address
   })
  .then(function(newContractInstance) {
    console.log(newContractInstance.options.address) // instance with the new contract address
  });

Calling transfer method from deployed contract

const myContract = new caver.klay.Contract(contractABI, contractAddress)

myContract.methods.transfer(
  to, caver.utils.toPeb(amount, 'KLAY')
).send({from: klaytn.selectedAddress},
  function(error, transactionHash) {
    ...
  });

Full list of methods are available at caver documentation.

Last updated