Skip to content
This repository has been archived by the owner on May 26, 2023. It is now read-only.

Commit

Permalink
Merge pull request #304 from Zilliqa/fix/batch-request
Browse files Browse the repository at this point in the history
feat/getsmartcontractsubstate-batch-request
  • Loading branch information
bb111189 authored Jun 2, 2021
2 parents 84f931a + 1e11447 commit 0d8b1ba
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 16 deletions.
11 changes: 0 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ language: node_js
sudo: true
jobs:
include:
- os: linux
dist: xenial
node_js:
- 10
- os: linux
dist: xenial
node_js:
Expand All @@ -14,10 +10,6 @@ jobs:
dist: xenial
node_js:
- 14
- os: linux
dist: bionic
node_js:
- 10
- os: linux
dist: bionic
node_js:
Expand All @@ -26,9 +18,6 @@ jobs:
dist: bionic
node_js:
- 14
- os: osx
node_js:
- 10
- os: osx
node_js:
- 12
Expand Down
15 changes: 15 additions & 0 deletions examples/node/queryState.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,21 @@ async function test() {
['0x381f4008505e940ad7681ec3468a719060caf796'],
);
console.log(state4.result);

console.log(
`\n\nGet substate using batch api.`,
);
const req1 = ["5938fc8af82250ad6cf1da3bb92f4aa005cb2717","balances",['0x381f4008505e940ad7681ec3468a719060caf796']]
const req2 = ["5938fc8af82250ad6cf1da3bb92f4aa005cb2717","balances",['0xa476fcedc061797fa2a6f80bd9e020a056904298']]

const state5 = await zilliqa.blockchain.getSmartContractSubStateBatch(
[
req1,req2
]
);

const batchResult = JSON.stringify(state5.batch_result);
console.log(batchResult);
}

test();
15 changes: 15 additions & 0 deletions packages/zilliqa-js-blockchain/src/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,21 @@ export class Blockchain implements ZilliqaModule {
);
}

/**
* getSmartContractSubStateBatch - Quires the contract state using batch rpc.
* @param reqs array of address variableName indices
* e.g ["5938fc8af82250ad6cf1da3bb92f4aa005cb2717","balances",['0x381f4008505e940ad7681ec3468a719060caf796']]
* @returns
*/
getSmartContractSubStateBatch(
reqs: any[]
): Promise<RPCResponse<any, any>>{
return this.provider.sendBatch(
RPCMethod.GetSmartContractSubState,
reqs,
);
}

/**
* getSmartContracts
*
Expand Down
4 changes: 2 additions & 2 deletions packages/zilliqa-js-core/src/net.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export enum RPCErrorCode {
}

export interface RPCRequestPayload<T> {
id: 1;
id: number;
jsonrpc: '2.0';
method: RPCMethod;
params: T;
Expand All @@ -120,7 +120,7 @@ export interface RPCRequest<T> {

interface RPCResponseBase {
jsonrpc: '2.0';
id: '1';
id: number;
}

export interface RPCResponseSuccess<R = any> extends RPCResponseBase {
Expand Down
17 changes: 14 additions & 3 deletions packages/zilliqa-js-core/src/providers/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,21 @@ export class HTTPProvider extends BaseProvider implements Provider {
paramsList: T[],
): RPCRequest<T> {
let payloads: RPCRequestPayload<T>[] = [];
for (let payloadParam of paramsList) {
const params: any = [payloadParam];
for (let i = 0; i < paramsList.length; i++) {
// most of the payloads should be a single param, e.g. GetTransaction
// however, there are special cases e.g. GetSmartContractSubState & GetTransactionsForTxBlockEx
// where the param field is a list
let payloadParams = paramsList[i];
let params: any;
if (Array.isArray(payloadParams)) {
// for those param field that is already a list
params = payloadParams;
} else {
params = [payloadParams];
}
// id start from index 1
payloads.push({
id: 1,
id: i + 1,
jsonrpc: '2.0',
method,
params,
Expand Down

0 comments on commit 0d8b1ba

Please sign in to comment.