Contrast with web3.js
js-conflux-sdk is Conflux network's javascript SDK, if you are familiar with Ethereum's web3.js, this guide can help you quickly know how to use js-conflux-sdk.

Install

web3:
  • npm: npm install web3
  • pure js: link the dist/web3.min.js
js-conflux-sdk:
  • npm: npm install js-conflux-sdk
  • pure js: link the dist/js-conflux-sdk.umd.min.js

Import & initialize

web3:
1
// In Node.js use: const Web3 = require('web3');
2
3
const web3 = new Web3(Web3.givenProvider || "ws://localhost:8545");
Copied!
js-conflux-sdk:
1
const { Conflux } = require('js-conflux-sdk');
2
// In browser: const Conflux = window.TreeGraph.Conflux;
3
4
const conflux = new Conflux({
5
url: "https://test.confluxrpc.com",
6
networkId: 1
7
});
Copied!

Invoke client's RPC method

web3:
1
// get balance
2
await web3.eth.getBalance("0x407d73d8a49eeb85d32cf465507dd71d507100c1");
3
// get nonce
4
await web3.eth.getTransactionCount("0x407d73d8a49eeb85d32cf465507dd71d507100c1");
5
// get block
6
await web3.eth.getBlock('0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b');
7
// get transaction
8
await web3.eth.getTransaction('0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b');
9
// get tx receipt
10
await web3.eth.getTransactionReceipt('0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b');
Copied!
js-conflux-sdk:
1
// get balance
2
await conflux.cfx.getBalance('cfxtest:aaawgvnhveawgvnhveawgvnhveawgvnhvey1umfzwp');
3
// get nonce
4
await conflux.cfx.getNextNonce('cfxtest:aaawgvnhveawgvnhveawgvnhveawgvnhvey1umfzwp');
5
// get block
6
await conflux.cfx.getBlockByHash('0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b')
7
// get transaction
8
await conflux.cfx.getTransactionByHash('0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b');
9
// get tx receipt
10
await conflux.cfx.getTransactionReceipt('0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b');
Copied!

Account manage

web3:
1
// create
2
const account = web3.eth.accounts.create();
3
/*
4
{
5
address: "0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01",
6
privateKey: "0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709",
7
signTransaction: function(tx){...},
8
sign: function(data){...},
9
encrypt: function(password){...}
10
}
11
*/
12
// import private key
13
web3.eth.accounts.privateKeyToAccount(privateKey);
14
// signTransaction
15
const txInfo = {
16
to: '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55',
17
value: '1000000000',
18
gas: 2000000
19
};
20
account.signTransaction(txInfo)
21
web3.eth.accounts.signTransaction(txInfo, account.privateKey);
Copied!
js-conflux-sdk:
1
// create
2
const account = conflux.wallet.addRandom(privateKey);
3
// import private key
4
const account2 = conflux.wallet.addPrivateKey();
5
// signTransaction
6
const txInfo = {
7
to: '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55',
8
value: '1000000000',
9
gas: 2000000
10
};
Copied!

Sending Transactions

web3:
1
const account = web3.eth.accounts.privateKeyToAccount(privateKey);
2
// signTransaction
3
const txInfo = {
4
to: '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55',
5
value: '1000000000',
6
gas: 2000000
7
};
8
const tx = await account.signTransaction(txInfo);
9
const hash = await web3.eth.sendRawTransaction(tx.rawTransaction);
Copied!
js-conflux-sdk:
1
const account = conflux.wallet.addPrivateKey(privateKey);
2
const hash = await conflux.cfx.sendTransaction({
3
from: account.address,
4
to: 'cfxtest:aaawgvnhveawgvnhveawgvnhveawgvnhvey1umfzwp',
5
value: '1000000000',
6
gas: 2000000
7
});
Copied!

Tx state checker

web3:
1
web3.eth.sendTransaction({from: '0x123...', data: '0x432...'})
2
.once('sending', function(payload){ ... })
3
.once('sent', function(payload){ ... })
4
.once('transactionHash', function(hash){ ... })
5
.once('receipt', function(receipt){ ... })
6
.on('confirmation', function(confNumber, receipt, latestBlockHash){ ... })
7
.on('error', function(error){ ... })
8
.then(function(receipt){
9
// will be fired once the receipt is mined
10
});
Copied!
js-conflux-sdk:
1
const pendingTx = conflux.cfx.sendTransaction({
2
from: 'cfxtest:123...',
3
to: 'cfxtest:456...',
4
value: 1
5
});
6
7
const hash = await pendingTx;
8
9
const tx = await pendingTx.get();
10
11
tx = await pendingTx.mined();
12
13
const receipt = await pendingTx.executed();
14
15
receipt = await pendingTx.confirmed();
Copied!

Interact with contract

To interact with contract, you must know it's abi, bytecode, address

Deploy contract

web3:
1
const contract = new web3.eth.Contract(abi);
2
3
await contract.deploy({
4
data: bytecode,
5
arguments: [arg1, arg2]
6
});
Copied!
js-conflux-sdk:
1
const contract = conflux.Contract({
2
abi,
3
bytecode,
4
});
5
6
const hash = await contract.constructor(arg1, arg2).sendTransaction({
7
from: account
8
});
9
10
// wait tx packed and executed
11
12
const receipt = await conflux.cfx.getTransactionReceipt(hash);
13
14
console.log('Contract address: ', receipt.contractCreated);
Copied!

Call methods

web3:
1
const contract = new web3.eth.Contract(abi, address);
2
let result = await contract.methods.myMethod(123).call({
3
from: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe',
4
});
Copied!
js-conflux-sdk
1
const contract = conflux.Contract({
2
abi,
3
address
4
});
5
let result = await contract.myMethod(123).call({
6
from: "cfxtest:aaawgvnhveawgvnhveawgvnhveawgvnhvey1umfzwp",
7
});
Copied!

Update contract state

web3:
1
let receipt = await contract.methods.updateStateMethod(123).send({
2
from: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe'
3
});
Copied!
js-conflux-sdk:
1
let receipt = await contract.updateStateMethod(123).sendTransaction({
2
from: "cfxtest:aaawgvnhveawgvnhveawgvnhveawgvnhvey1umfzwp"
3
}).executed();
Copied!

Utilities

Unit convert

web3:
1
web3.utils.toWei('1', 'ether');
2
> "1000000000000000000"
3
4
web3.utils.toWei('1', 'Gwei')
5
> "1000000000"
6
7
web3.utils.fromWei('1', 'ether');
8
> "0.000000000000000001"
9
10
web3.utils.fromWei('1', 'Gwei');
11
> "0.000000001"
Copied!
js-conflux-sdk:
1
const { Drip } = require('js-conflux-sdk');
2
3
Drip.fromCFX(1).toString()
4
> "1000000000000000000"
5
6
Drip.fromGDrip(1).toString();
7
> "1000000000"
8
9
new Drip(1).toCFX();
10
> "0.000000000000000001"
11
12
new Drip(1).toGDrip();
13
> "0.000000001"
Copied!

hex encoding

web3:
1
web3.utils.toHex('234');
2
> "0xea"
3
4
web3.utils.toHex(234);
5
> "0xea"
6
7
web3.utils.toHex(new BN('234'));
8
> "0xea"
9
10
web3.utils.toHex(new BigNumber('234'));
11
> "0xea"
12
13
web3.utils.toHex('I have 100€');
14
> "0x49206861766520313030e282ac"
Copied!
js-conflux-sdk:
1
const { format } = require('js-conflux-sdk');
2
3
format.hex(234)
4
> '0xea'
5
6
format.hex(BigInt(123))
7
> '0x7b'
8
9
format.hex(Buffer.from('hello world'))
10
> '0x68656c6c6f20776f726c64'
Copied!

keccak

web3:
1
web3.utils.sha3('234'); // taken as string
2
> "0xc1912fee45d61c87cc5ea59dae311904cd86b84fee17cc96966216f811ce6a79"
Copied!
js-conflux-sdk:
1
format.keccak256(Buffer.from('234'))
2
> "0xc1912fee45d61c87cc5ea59dae311904cd86b84fee17cc96966216f811ce6a79"
Copied!

Last modified 5mo ago