js-conflux-sdk v2.0
has imported several big features.
From Conflux-rust v2.0.0
, the pos
RPC group has been added, which can be used to get PoS chain info. js-conflux-sdk
will add support to PoS related RPC in v2.0. These methods are located at Conflux's sub object pos conflux.pos
, for example:
async function main() {
const conflux = new Conflux({
url: 'https://test.confluxrpc.com',
networkId: 1
});
const status = await conflux.pos.getStatus();
console.log('Current PoS status: ', status);
/*
{
epoch: 29,
latestCommitted: 1725,
latestTxNumber: '0x1e36',
latestVoted: 1728,
pivotDecision: {
blockHash: '0xc8c2c58ef952f48adf00d6204d6e930f23aee26c6b9a903820bea1c012f72f3e',
height: 120480
}
}
*/
}
Check PoS RPC documentation for the complete method list.
And previous cfx
RPC methods are also been moved to their own namespace, for example:
const balance = await conflux.cfx.getBalance('cfxtest:aaawgvnhveawgvnhveawgvnhveawgvnhvey1umfzwp');
Besides txpool
and trace
namespace is also added, so js-sdk v2.0 have four RPC namespace:
cfx
pos
txpool
trace
There also is an advanced
namespace, which will provide some useful combined RPC utilities, for example:
getNextUsableNonce
getPoSInterestRate
Two more internal contracts are added:
ConfluxContext
PoSRegister
v2.0 has add support for RPC batch invoke, for example:
const batcher = conflux.BatchRequest();
batcher.add(conflux.cfx.getStatus.request());
batcher.add(conflux.cfx.getBalance.request('cfxtest:aaawgvnhveawgvnhveawgvnhveawgvnhvey1umfzwp'));
const [status, balance] = await batcher.execute();
Check here for detail introduction.
Note this is a breaking change
From v2.0 the browser exported SDK class name has change from Conflux
to TreeGraph
.
Previous
<script type="text/javascript">
const conflux = new window.Conflux.Conflux({
url: 'https://test.confluxrpc.com',
logger: console,
networkId: 1,
});
</script>
Currently:
<script type="text/javascript">
const conflux = new window.TreeGraph.Conflux({
url: 'https://test.confluxrpc.com',
logger: console,
networkId: 1,
});
</script>
If your RPC endpoint support txpool
RPC methods, now you can fast send transaction sequencely. For example:
for(let i = 0; i < 100; i++) {
let hash = await conflux.cfx.sendTransaction({
from: account,
to: 'cfxtest:aaawgvnhveawgvnhveawgvnhveawgvnhvey1umfzwp',
value: 1
});
console.log(`TX hash: ${hash}`);
}
Note the max sequence transaction send count can not surpass 2000
const tokenAbi = [
// Some details about the token
"function name() view returns (string)",
"function symbol() view returns (string)",
// Get the account balance
"function balanceOf(address) view returns (uint)",
// Send some of your tokens to someone else
"function transfer(address to, uint amount)",
// An event triggered whenever anyone transfers to someone else
"event Transfer(address indexed from, address indexed to, uint amount)"
];
const contract = conflux.Contract({
abi: tokenAbi,
address: 'cfxtest:aaawgvnhveawgvnhveawgvnhveawgvnhvey1umfzwp' // use a valid token address here
});
const balance = await contract.balanceOf('cfxtest:aaawgvnhveawgvnhveawgvnhveawgvnhvey1umfzwp');