Overview
The purpose of this page is to give you a sense of everything js-conflux-sdk can do and serve as a quick reference guide
Pre-requisites
Node.js environment to install this SDK
Conflux account with some CFX (Use FluentWallet to create account and get testnet CFX from faucet)
Conflux RPC endpoint, for example
https://test.confluxrpc.com
is a testnet RPC endpoint
Initialize
After installing js-conflux-sdk
(via npm), you’ll need to specify the provider url. You can use the mainnet(https://main.confluxrpc.com), or testnet(https://test.confluxrpc.com), or run your own Conflux node.
Testnet
With a RPC endpoint we can initialize a Conflux object, which can be used to send JSON RPC request.
Besides url
and logger
you can pass other options to initialize a Conflux object
Add account
Private keys are required to approve any transaction made on your behalf, conflux.wallet
provide utility help you manage your accounts
Only after you add your account to wallet, then you can use them send transactions.
Send JSON-RPC request
There are a lot methods on cfx object which are one-to-one with Conflux node RPC methods. All conflux methods will return a promise, so you can use it with async/await
syntax.
Besides balance you can get a lot blockchain information through it, for example: nonce, block, transaction, receipt and so on. You can check API and RPC
Conflux hex address
Note: from Conflux-rust v1.1.1 we have import a new base32 encoded address, the hex address can not be used then.
Conflux base32 checksum address
From conflux-rust 1.1.1
Conflux has switch to base32Checksum address for example cfxtest:aak2rra2njvd77ezwjvx04kkds9fzagfe6d5r8e957
. It was introduced by CIP37. js-conflux-sdk
add support for it from version 1.5.10
, check here for details.
Send Transaction
Check here for details
chainId
chainId
is used to distinguish different network and prevent replay attack, currently:
mainnet:
1029
testnet:
1
RPC endpoint
mainnet
: https://main.confluxrpc.comtestnet
: https://test.confluxrpc.com
Hex value and epochNumber and tags
You can find the epochNumber doc at official developer RPC doc
JSBI
Because in blockchain world there are a lot big numbers (uint256), only modern JS VM and Node.js support BigInt
, so we use JSBI in browser environment to handle these big number. For how to use JSBI check it's documentation
Note: jsbi currently cann't pretty log, you need convert it to string before log.
Note: When a js integer is bigger than Number.MAX_SAFE_INTEGER
(2^53 - 1 or 9,007,199,254,740,991) you should use BigInt
to contain it.
Drip
In Conflux network there are there unit: CFX, Drip, Gdrip. Drip is the minimum unit in Conflux 1 CFX=10^18Drip, 1Gdrip=10^9Drip. When getting account's balance, send transaction, specify gasPrice, all unit will be Drip. The SDK has provide a class Drip, which can use to easily convert between different unit.
Websocket provider
The SDK also provide a websocket provider, and the default Conflux node ws port is 12535
.
Pub/Sub
Conflux node support pub/sub makes it possible to query certain items on an ongoing basis, without polling through the JSON-RPC HTTP interface, currently three topics are supported: newHeads
, epochs
, logs
, for detail explain of Pub/Sub check the official doc
Use JS SDK it will be very easy to use Pub/Sub.
Interact with contract
Quick demo of interact with contract: query balance of one CRC20 token
You can use this SDK get and update contract state, we have a complete documentation for you.
Type conversion
If you want convert a hex to number, or uint, you can use format
, for example:
Sign
The SDK also provide most common used crypto utilities in sign
Last updated