• Stars
    star
    110
  • Rank 316,770 (Top 7 %)
  • Language
    JavaScript
  • License
    GNU Lesser Genera...
  • Created over 5 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Official caver-js repository

Branch name will be changed

We will change the master branch to main on Dec 15, 2022. After the branch policy change, please check your local or forked repository settings.

caver-js

Gitter

caver-js

caver-js is a JavaScript API library that allows developers to interact with a Klaytn node using a HTTP or Websocket connection.

NOTE Kaikas Web Extension Wallet is recommended to be used with features prior to common architecture. New features provided in caver-js v1.5.0 (introduced common architecrue) or later are currently not compatible with Kaikas. Kaikas web extension wallet works fine with functions prior to common architecture, so please use functions prior to common architecture. Even in the latest version of caver-js, functions prior to common architecture are provided for backward compatibility. See Trouble shooting and known issues for more details.

Table of contents

Requirements

The following packages are required to use the caver-js library.

Testing in caver-js is implemented using the mocha testing framework. If you want to run unit tests in caver-js, you need to install mocha first.

Note caver-js can run on Node.js versions 12 and 14, and the recommended versions are:

If you are already using a different version of the node(for example, node v15), use the Node Version Manager(NVM) to install and use the version supported by caver-js.

Installation

Node

To try it out, install caver-js with npm like following command:

$ npm install caver-js

Note package.json file should exist on the same install path. If it does not exist, package.json should be generated via npm init.

To install a specific version of caver-js, try the following command:

$ npm install [email protected]

In the Browser

Build using the caver-js repository:

$ npm run build

Then include dist/caver.min.js in your html file. This will expose Caver on the window object.

Or via CDN:

<script src="https://cdnjs.cloudflare.com/ajax/libs/caver-js/x.x.x/caver.min.js"></script>

The caver-js provided by cdnjs can be found at this link.

Getting Started

If you want to run your own EN (Endpoint Node), see EN Operation Guide to set up. You can connect EN like below:

$ node
> const Caver = require('caver-js')
> const caver = new Caver('http://localhost:8551/')

Note The above example should be executed from the location where caver-js is installed, and the example is explained using Node.js REPL.

Check the Connection

You can now use caver-js. You can send a basic request to the node as shown below and check the results.

> caver.rpc.klay.getClientVersion().then(console.log)
Klaytn/vX.X.X/linux-amd64/goX.X.X

Using caver-js keyring/wallet

You can easily use your Klaytn account when signing a transaction or message by using the Keyring / wallet.

The keyring is a new feature that contains an address and one or more private keys based on the key types (SingleKeyring, MultipleKeyring or RoleBasedKeyring). Refer to caver.wallet.keyring for details. caver.wallet, the in-memory wallet, is provided to easily manage multiple keyrings.

Note Functions associated with wallet and keyring have no effect on the Klaytn blockchain platform. It just manipulates keyrings in the in-memory wallet.

Let's create a random keyring as shown in the example below:

> const keyring = caver.wallet.keyring.generate()

> keyring
SingleKeyring {
  _address: '0x64d221893cc628605314026f4c4e0879af5b75b1',
  _key: PrivateKey { _privateKey: '0x{private key}' }
}

You can add the keyring object created in the above example to the caver.wallet, or you can add a keyring using an address and private key(s).

// Add a keyring instance to caver.wallet
> caver.wallet.add(keyring)

// Add a keyring to caver.wallet with an address and a private key
> caver.wallet.newKeyring('0x{address in hex}', '0x{private key}')

// Add a keyring to caver.wallet with an address and private keys
> caver.wallet.newKeyring('0x{address in hex}', ['0x{private key1}', '0x{private key2}', ...])

// Add a keyring to caver.wallet with an address and private keys by roles
> caver.wallet.newKeyring('0x{address in hex}', [ ['0x{private key1}', ...], ['0x{private key2}', ...], ['0x{private key3}', ...] ])

Submitting a Transaction

You can use caver-js to submit various types of transactions to a node. Please refer to the caver.transaction to see how to create a transaction of each type.

Note you should create a transaction instance via create function. From caver-js v1.8.2, constructors per transaction type are not supported.

You can sign the transaction using a keyring and send a signed transaction through caver.rpc.klay.sendRawTransaction as shown below, and the receipt is returned as a result.

// Add a keyring to caver.wallet
> const keyring = caver.wallet.newKeyring('0x{address in hex}', '0x{private key}')
> const vt = caver.transaction.valueTransfer.create({
		from: keyring.address,
		to: '0x176ff0344de49c04be577a3512b6991507647f72',
		value: caver.utils.convertToPeb(1, 'KLAY'),
		gas: 25000,
	})
> caver.wallet.sign(keyring.address, vt).then(signed => {
  caver.rpc.klay.sendRawTransaction(signed).then(console.log)
})
{ 
  blockHash: '0x0a78b5c5b95456b2d6b6a9ba25fd2afd0000d16bcf03a8ae58a6557a59319a67',
  blockNumber: 8021,
  contractAddress: null,
  from: '0x09a08f2289d3eb3499868908f1c84fd9523fe11b',
  ...
  type: 'TxTypeValueTransfer',
  typeInt: 8,
  value: '0xde0b6b3a7640000' 
}

The above example uses Promise when sending a signed transaction to the Klaytn. You can also use event emitter like below.

caver.rpc.klay.sendRawTransaction(signed).on('transactionHash', function(hash){
    ...
  }).on('receipt', function(receipt){
    ...
  })

Units for KLAY

Units of KLAY is shown as below, and peb is the smallest currency unit. peb is the default unit unless the unit conversion is used.

Name Unit
peb 1
kpeb 1,000
Mpeb 1,000,000
Gpeb 1,000,000,000
ston 1,000,000,000
uKLAY 1,000,000,000,000
mKLAY 1,000,000,000,000,000
KLAY 1,000,000,000,000,000,000
kKLAY 1,000,000,000,000,000,000,000
MKLAY 1,000,000,000,000,000,000,000,000
GKLAY 1,000,000,000,000,000,000,000,000,000
TKLAY 1,000,000,000,000,000,000,000,000,000,000

caver-js provides the caver.utils.convertToPeb function for unit conversion. Please refer to the usage below.

> caver.utils.convertToPeb(1, 'peb')
'1'

> caver.utils.convertToPeb(1, 'Gpeb')
'1000000000'

> caver.utils.convertToPeb(1, 'KLAY')
'1000000000000000000'

Documentation

Documentation can be found at Klaytn Docs-caver-js.

API Specification

The API lists of caver-js are described in folloinwg links:

Web3.js Similarity

Since caver-js has been evolved from web3.js, usage pattern of caver-js is very similar to that of web3.js. This means a software developed using web3.js can be easily converted to caver-js. The following examples are code patterns used in web3.js and caver-js, respectively.

const Web3 = require('web3');	
const web3 = new Web3(new web3.providers.HttpProvider('http://localhost:8545'));	
web3.eth.getBalance('0x407d73d8a49eeb85d32cf465507dd71d507100c1').then(console.log)	
const Caver = require('caver-js');	
const caver = new Caver(new Caver.providers.HttpProvider('http://localhost:8545'));	
caver.rpc.klay.getBalance('0x407d73d8a49eeb85d32cf465507dd71d507100c1').then(console.log)	

Error Code Improvement

Klaytn improves reporting transaction failure via txError in the receipt. caver-js further improves the report by presenting the error string that corresponds to txError.

The below is an example of a receipt containing txError.

Error: VM error occurs while running smart contract
 {
  "blockHash": "0xe7ec35c9fff1178d52cee1d46d40627d19f828c4b06ad1a5c3807698b99acb20",
  ...
  "txError": "0x2",
  ...
}

The meaning of error code can be found below:

Error Code Description
0x02 VM error occurs while running smart contract
0x03 max call depth exceeded
0x04 contract address collision
0x05 contract creation code storage out of gas
0x06 evm: max code size exceeded
0x07 out of gas
0x08 evm: write protection
0x09 evm: execution reverted
0x0a reached the opcode computation cost limit (100000000) for tx
0x0b account already exists
0x0c not a program account (e.g., an account having code and storage)
0x0d Human-readable address is not supported now
0x0e fee ratio is out of range [1, 99]
0x0f AccountKeyFail is not updatable
0x10 different account key type
0x11 AccountKeyNil cannot be initialized to an account
0x12 public key is not on curve
0x13 key weight is zero
0x14 key is not serializable
0x15 duplicated key
0x16 weighted sum overflow
0x17 unsatisfiable threshold. Weighted sum of keys is less than the threshold.
0x18 length is zero
0x19 length too long
0x1a nested composite type
0x1b a legacy transaction must be with a legacy account key
0x1c deprecated feature
0x1d not supported
0x1e smart contract code format is invalid

Sample Projects

The BApp (Blockchain Application) Development sample projects using caver-js are the following:

Trouble shooting and known issues

Connect with Kaikas Web Extension

Kaikas Web Extension Wallet works normally with functions prior to common architecture. Features provided in later versions (caver-js v1.5.0~) may not work with Kaikas Web Extension Wallet.

If the following error occurs when using with Kaikas, it must be modified to use the functions supported by caver-js v1.4.1. For compatibility with the latest version, the same functions provided by caver-js v1.4.0 are provided.

Kaikas only processes one transaction at a time. Open Kaikas and refresh the pending transaction. If the service doesn’t process your transaction for a while, cancel the pending transaction.
Kaikas는 한 번에 하나의 트랜잭션만 처리합니다. Kaikas를 열어 대기 중인 트랜잭션을 새로고침 해주세요. 만약 대기 상태가 계속된다면 이용 중인 서비스가 트랜잭션을 처리하지 않는 것이니 트랜잭션을 취소바랍니다.

Although the above error is mostly the case, other errors may occur besides the above error, so if you are using Kaikas Web Extension Wallet, please use the functions supported by caver-js v1.4.1. Keep in mind that you cannot mix features before and after common architecture.

For documents for the functions supported by caver-js v1.4.1, refer to caver-js ~v1.4.1 Documentation.

Using webpack >= 5

Node.js module polyfills are not provided by default in webpack v5 and later. Therefore, you need to install the missing modules and add them to the resolve.fallback property of the webpack.config.js file in the form below.

module.exports = {
    ...
    resolve: {
        fallback: {
            fs: false,
            net: false,
            stream: require.resolve('stream-browserify'),
            crypto: require.resolve('crypto-browserify'),
            http: require.resolve('stream-http'),
            https: require.resolve('https-browserify'),
            os: require.resolve('os-browserify/browser'),
            ...
        },
    },
}

More information on migrating to webpack v5 can be found here.

If you are implementing an app using create-react-app, you can use react-app-rewired to add the above polyfills to the webpack.config.js file used by CRA. More information on using react-app-rewired with create-react-app can be found here.

Github Repository

Related Projects

caver-java for Java

More Repositories

1

klaytn

Official Go implementation of the Klaytn protocol
Go
376
star
2

klaytn-contracts-old

JavaScript
71
star
3

caver-java

Official caver-java repository
Java
61
star
4

awesome-klaytn

This page introduces all links related to Klaytn’s core development and ecosystem
37
star
5

kaikas-tutorial

Kaikas tutorial repository to send Klaytn's various types of transactions!
JavaScript
34
star
6

klaytn-etl

Python scripts for ETL (extract, transform and load) jobs for Klaytn blocks, transactions, ERC20 / ERC721 tokens, transfers, receipts, logs, contracts, internal transactions.
Python
20
star
7

klaytn-nft-minter-frontend

JavaScript
19
star
8

klaytn-docs

Klaytn Docs
JavaScript
19
star
9

klaytnwallet

JavaScript
18
star
10

kips

Website
HTML
18
star
11

klaytn-nft-minter-backend

JavaScript
18
star
12

caver-js-examples

JavaScript
12
star
13

klaystagram

JavaScript
12
star
14

klaytn-dex-frontend

TypeScript
10
star
15

klaytn-online-toolkit

klaytn-online-toolkit provides code samples and github-page to help you utilize the Klaytn SDK(caver-js)
TypeScript
10
star
16

klaytn-dev-sandbox

TypeScript
9
star
17

truffle-hdwallet-provider-klaytn

TypeScript
8
star
18

web3klaytn

web3klaytn
Java
7
star
19

klayspray

Deploy a Klaytn network
HCL
6
star
20

klaytn-tests

Python
6
star
21

tx-latency-measurement

JavaScript
6
star
22

local-klaytn-deploy

Shell
5
star
23

homebrew-klaytn

Official Klaytn Homebrew formula repository
Ruby
5
star
24

3d-nft-gallery

JavaScript
5
star
25

caver-java-examples

Java
5
star
26

klaytn-rpc-tester

Python
4
star
27

countbapp

JavaScript
4
star
28

servicechain-value-transfer-examples

Example tutorials for value transfer in service chain
JavaScript
4
star
29

rosetta-klaytn

Go
4
star
30

metaverse-knowledge-kit

Documentation for metaverse components
JavaScript
3
star
31

klaytn-load-tester

Go
3
star
32

klaytn-integration-tests

Solidity
3
star
33

canonical-wklay

Standard for wrapped KLAY for dapps
JavaScript
3
star
34

klaytn-service-sdk

A MonoRepo for all the ecosystem tools to build in Klaytn.
JavaScript
3
star
35

examples

This repository will be used for storing any examples related to Klaytn dapp development.
Solidity
2
star
36

governance-contracts-audit

Klaytn governance contracts
Solidity
2
star
37

servicechain-deploy

Shell
2
star
38

klaytn-faucet

Faucet to obtain KLAY for Klaytn Baobab Testnet
TypeScript
2
star
39

treasury-rebalance

Smart contract Implementation of KGP-6 to record the process of rebalancing funds in a transparent manner
JavaScript
2
star
40

kaikas-web3-provider

TypeScript
2
star
41

klaytn-terraform

Terraform modules set to create various resources regarding Klaytn.
HCL
2
star
42

cn-staking-contract-tests

Shell
2
star
43

klaytn-dex-contracts

TypeScript
2
star
44

bridgeregistry

BridgeRegistry is a smart contract that manages information to facilitate value transfer of the ServiceChain.
TypeScript
2
star
45

servicechain-contracts

Solidity contracts for bridge and tokens(KLAY, ERC20 / ERC721, KIPs) of service chain. Generate go binding codes from solidity files.
Solidity
2
star
46

klaytn-ansible

Ansible modules for installing and using Klaytn.
Jinja
2
star
47

ecosystem-integration-scripts

A repository which contains all sctipts to integrate with diff ecosystem tools.
JavaScript
2
star
48

project-management

Shell
1
star
49

klaytn-docs-ko

Korean translations of klaytn-docs
1
star
50

hardhat-utils

TypeScript
1
star
51

klaytn-reward-tester

Python
1
star
52

config

1
star
53

klaytn-snap

An integration of Caver and Metamask Snap.
TypeScript
1
star
54

klip-web3-provider

Web3 Provider of Klip Wallet that allows interacting with the Klaytn network when using the web3.js library
TypeScript
1
star
55

klaytn-pre-cypress-setup-workshop

Online workshop for joining Pre-cypress network
CSS
1
star
56

servicechain-remote-tests

It is for remote testing using caver-js. And it is for when the test doesn't work on the servicechain simulator node.
1
star
57

helm-charts

Official helm chart repository for Klaytn
1
star
58

klaytn-dapp-kit

Boilerplate code for klaytn ecosystem tools
JavaScript
1
star
59

klaytn-deploy

Python
1
star
60

klaytn-baobab-setup-workshop

Online workshop for joining BaoBab network
CSS
1
star
61

klaytn-docs-vn

Vietnam translations of klaytn-docs
1
star
62

klaytn-net-intelligence-api

Klaytn Network Intelligence API
JavaScript
1
star
63

klaytn-dex-dashboard

Vue
1
star
64

rosetta-sdk-go-klaytn

Go
1
star
65

datadog-agent-install.

Shell
1
star
66

contract-template

TypeScript
1
star
67

klaytn-cypress-setup-workshop

Online workshop for joining Cypress network
HTML
1
star
68

enterprise_proxy_lite

Java
1
star
69

enterprise_proxy

Klaytn enterprise proxy
Java
1
star
70

klaytn-dex-subgraphs

TypeScript
1
star