The JSON-RPC 2.0 specification support batch request natively. Which enable user send several request at the same time. The Client MAY send an Array filled with Request objects, The Server should respond with an Array containing the corresponding Response objects, after all of the batch Request objects have been processed.
Conflux's RPC method also support batch request, both HTTP and Websocket.
js-conflux-sdk will enable user easily call RPC batched from v2.0.
Quick start
From js-conflux-sdk v2.0 batch RPC is supported.
const { Conflux } =require('js-conflux-sdk');constconflux=newConflux({ url:"https://test.confluxrpc.com", networkId:1,});constaccount=conflux.wallet.addPrivateKey(process.env.PRIVATE_KEY);asyncfunctionmain() {// create a batch requester through method BatchRequest()constbatcher=conflux.BatchRequest();// add method requestbatcher.add(conflux.cfx.getEpochNumber.request());batcher.add(conflux.cfx.getBalance.request(account.address));constresults=awaitbatcher.execute();/* [ 100, // the epoch number 203134n, // balance ] */}main();
One thing to note is execute method will not clear previous added request, so if you add some request later and then call execute method again, all request will send one time. BatchRequest provide one method clear to remove all request previous added.
Batch Send Transaction
To batch send transaction, now developer could use SDK's helper to build a rawTransaction and add it to bacher's request array.
asyncfunctionmain() {// create a batch requester through method BatchRequest()constbatcher=conflux.BatchRequest();// add method requestconstrawTx1= { from: addressA, to: addressB, value:100// Drip };batcher.addTransaction(rawTx1);constrawTx2= { from: addressA, to: addressC, value:200// Drip };batcher.addTransaction(rawTx2);constresults=awaitbatcher.execute();/* [ "0xb8336d2fb53f1f19503d80bf23f8e14370be7e27df6f28e38fa95b8efd8d5c93", "0x6f90c40feb8a2a621e51b0a7cf48b25b677b07294e34eebda5318d7cf23ca9e5", ] */}main();
Note: when sending transation there is a max amount limit 2000 for one account.