1.11.0 • Published 5 months ago

caver-js v1.11.0

Weekly downloads
828
License
LGPL-3.0
Repository
github
Last release
5 months ago

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 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 caver-js@X.X.X

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.

NameUnit
peb1
kpeb1,000
Mpeb1,000,000
Gpeb1,000,000,000
ston1,000,000,000
uKLAY1,000,000,000,000
mKLAY1,000,000,000,000,000
KLAY1,000,000,000,000,000,000
kKLAY1,000,000,000,000,000,000,000
MKLAY1,000,000,000,000,000,000,000,000
GKLAY1,000,000,000,000,000,000,000,000,000
TKLAY1,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 CodeDescription
0x02VM error occurs while running smart contract
0x03max call depth exceeded
0x04contract address collision
0x05contract creation code storage out of gas
0x06evm: max code size exceeded
0x07out of gas
0x08evm: write protection
0x09evm: execution reverted
0x0areached the opcode computation cost limit (100000000) for tx
0x0baccount already exists
0x0cnot a program account (e.g., an account having code and storage)
0x0dHuman-readable address is not supported now
0x0efee ratio is out of range 1, 99
0x0fAccountKeyFail is not updatable
0x10different account key type
0x11AccountKeyNil cannot be initialized to an account
0x12public key is not on curve
0x13key weight is zero
0x14key is not serializable
0x15duplicated key
0x16weighted sum overflow
0x17unsatisfiable threshold. Weighted sum of keys is less than the threshold.
0x18length is zero
0x19length too long
0x1anested composite type
0x1ba legacy transaction must be with a legacy account key
0x1cdeprecated feature
0x1dnot supported
0x1esmart 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

1.11.0-rc.2

5 months ago

1.11.0-rc.1

5 months ago

1.11.0

5 months ago

1.10.2

1 year ago

1.10.2-rc.1

1 year ago

1.10.2-rc.2

1 year ago

1.10.1

1 year ago

1.10.0

1 year ago

1.10.1-rc.7

1 year ago

1.10.1-rc.1

1 year ago

1.10.1-rc.2

1 year ago

1.10.1-rc.5

1 year ago

1.10.1-rc.6

1 year ago

1.10.1-rc.3

1 year ago

1.10.1-rc.4

1 year ago

1.10.0-rc.1

1 year ago

1.9.1-rc.4

1 year ago

1.9.1-rc.3

2 years ago

1.9.1-rc.1

2 years ago

1.9.1-rc.2

2 years ago

1.8.2-rc.6

2 years ago

1.9.0-rc.7

2 years ago

1.9.0-rc.6

2 years ago

1.9.0-rc.1

2 years ago

1.9.0-rc.5

2 years ago

1.9.0-rc.4

2 years ago

1.9.0-rc.3

2 years ago

1.9.0-rc.2

2 years ago

1.9.0

2 years ago

1.8.2

2 years ago

1.8.2-rc.5

2 years ago

1.8.2-rc.4

2 years ago

1.8.2-rc.3

2 years ago

1.8.2-rc.2

2 years ago

1.8.0-rc.2

2 years ago

1.8.0-rc.3

2 years ago

1.8.2-rc.1

2 years ago

1.8.1

2 years ago

1.8.0

2 years ago

1.8.1-rc.6

2 years ago

1.8.1-rc.7

2 years ago

1.8.1-rc.1

2 years ago

1.8.1-rc.2

2 years ago

1.8.1-rc.3

2 years ago

1.8.1-rc.4

2 years ago

1.8.1-rc.5

2 years ago

1.8.0-rc.1

2 years ago

1.6.8

2 years ago

1.6.7

2 years ago

1.6.6

2 years ago

1.6.6-rc.1

2 years ago

1.6.7-rc.2

2 years ago

1.6.7-rc.1

2 years ago

1.6.8-rc.1

2 years ago

1.6.8-rc.2

2 years ago

1.6.5

2 years ago

1.6.5-rc.3

2 years ago

1.6.5-rc.2

2 years ago

1.6.5-rc.1

3 years ago

1.6.4

3 years ago

1.6.4-rc.5

3 years ago

1.6.4-rc.4

3 years ago

1.6.4-rc.3

3 years ago

1.6.4-rc.2

3 years ago

1.6.4-rc.1

3 years ago

1.6.3

3 years ago

1.6.3-rc.4

3 years ago

1.6.3-rc.1

3 years ago

1.6.3-rc.2

3 years ago

1.6.3-rc.3

3 years ago

1.6.2

3 years ago

1.6.2-rc.2

3 years ago

1.6.2-rc.1

3 years ago

1.6.1

3 years ago

1.6.1-rc.6

3 years ago

1.6.1-rc.5

3 years ago

1.6.1-rc.4

3 years ago

1.6.1-rc.2

3 years ago

1.6.1-rc.3

3 years ago

1.6.1-rc.1

3 years ago

1.6.0

3 years ago

1.6.0-rc.2

3 years ago

1.6.0-rc.1

3 years ago

1.5.7

3 years ago

1.5.7-rc.6

3 years ago

1.5.7-rc.5

3 years ago

1.5.7-rc.3

3 years ago

1.5.7-rc.4

3 years ago

1.5.7-rc.2

3 years ago

1.5.7-rc.1

3 years ago

1.5.6

3 years ago

1.5.6-rc.6

3 years ago

1.5.6-rc.5

3 years ago

1.5.6-rc.4

3 years ago

1.5.6-rc.3

3 years ago

1.5.6-rc.2

3 years ago

1.5.6-rc.1

3 years ago

1.5.5

3 years ago

1.5.5-rc.1

3 years ago

1.5.4

3 years ago

1.5.4-rc.2

3 years ago

1.5.4-rc.1

3 years ago

1.5.2

3 years ago

1.5.2-rc.6

3 years ago

1.5.2-rc.5

4 years ago

1.5.2-rc.4

4 years ago

1.5.2-rc.3

4 years ago

1.5.2-rc.2

4 years ago

1.5.2-rc.1

4 years ago

1.5.1

4 years ago

1.5.1-rc.4

4 years ago

1.5.1-rc.3

4 years ago

1.5.1-rc.2

4 years ago

1.5.1-rc.1

4 years ago

1.5.0

4 years ago

1.5.0-rc.4

4 years ago

1.5.0-rc.3

4 years ago

1.5.0-rc.2

4 years ago

1.5.0-rc.1

4 years ago

1.4.1

4 years ago

1.4.1-rc.1

4 years ago

1.4.0

4 years ago

1.4.0-rc.1

4 years ago

1.3.2

4 years ago

1.3.2-rc.5

4 years ago

1.3.2-rc.4

4 years ago

1.3.2-rc.3

4 years ago

1.3.2-rc.2

4 years ago

1.3.2-rc.1

4 years ago

1.3.1

4 years ago

1.3.1-rc.1

4 years ago

1.3.0

4 years ago

1.3.0-rc.3

4 years ago

1.3.0-rc

4 years ago

1.3.0-rc.2

4 years ago

1.3.0-rc.1

4 years ago

1.2.2-rc.2

4 years ago

1.2.1

4 years ago

1.2.1-rc.1

4 years ago

1.2.0

4 years ago

1.2.0-rc.5

4 years ago

1.2.0-rc.4

4 years ago

1.2.0-rc.3

5 years ago

1.2.0-rc.2

5 years ago

1.2.0-rc.1

5 years ago

1.1.2

5 years ago

1.1.2-rc.1

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.1

5 years ago

1.0.1-rc.11

5 years ago

1.0.1-rc.10

5 years ago

1.0.1-rc.9-2

5 years ago

1.0.1-rc.9-1

5 years ago

1.0.0-rc.9-1

5 years ago

1.0.1-rc.9

5 years ago

1.0.1-rc.8

5 years ago

1.0.1-rc.7

5 years ago

1.0.1-rc.6

5 years ago

1.0.1-rc.5

5 years ago

1.0.1-rc.4

5 years ago

1.0.1-rc.3

5 years ago

0.10.3-rc.9

5 years ago

1.0.0

5 years ago

0.10.2-rc

5 years ago

0.8.3-2

5 years ago

0.10.1

5 years ago

0.10.0

5 years ago

0.9.5

5 years ago

0.9.4

5 years ago

0.9.3

5 years ago

0.9.2

5 years ago

0.9.1

5 years ago

0.8.8-6

5 years ago

0.8.7-5

5 years ago

0.8.6-4

5 years ago

0.8.5-3

5 years ago

0.8.4-2

5 years ago

0.8.3-1

5 years ago

0.8.3

5 years ago

0.8.2

5 years ago

0.8.1

5 years ago

0.8.0

5 years ago

0.0.3-d

5 years ago

0.0.3-c

5 years ago

0.0.3-b

5 years ago

0.0.3-a

5 years ago

0.0.2-z

5 years ago

0.0.2-y

5 years ago

0.0.2-x

5 years ago

0.0.2-w

5 years ago

0.0.2-v

5 years ago

0.0.2-u

5 years ago

0.0.2-t

5 years ago

0.0.2-s

5 years ago

0.0.2-r

5 years ago

0.0.2-q

5 years ago

0.0.2-p

5 years ago

0.0.2-o

5 years ago

0.0.2-n

5 years ago

0.0.2-m

5 years ago

0.0.2-l

5 years ago

0.0.2-k

5 years ago

0.0.2-j

5 years ago

0.0.2-i

5 years ago

0.0.2-h

5 years ago

0.0.2-f

5 years ago

0.0.2-e

5 years ago

0.0.2-d

5 years ago

0.0.2-c

5 years ago

0.0.2-b

5 years ago

0.0.2-a

5 years ago

0.0.1-y

5 years ago

0.0.1-x

5 years ago

0.0.1-w

5 years ago

0.0.1-v

5 years ago

0.0.1-u

5 years ago

0.0.1-t

5 years ago

0.0.1-s

5 years ago

0.0.1-r

5 years ago

0.0.1-q

5 years ago

0.0.1-p

5 years ago

0.0.1-o

5 years ago

0.0.1-n

5 years ago

0.0.1-m

5 years ago

0.0.1-l

5 years ago

0.0.1-k

5 years ago

0.0.1-i

5 years ago

0.0.1-h

5 years ago

0.0.1-g

5 years ago

0.0.1-f

5 years ago

0.0.1-e

5 years ago

0.0.1-d

5 years ago

0.0.1-c

5 years ago

0.0.1-b

5 years ago

0.0.1-a

5 years ago

0.0.1

5 years ago