Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Diff of Ashwin's fork #1

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions web3.js/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ module.exports = {
sourceType: 'module',
ecmaVersion: 8,
},
globals: {
NodeJS: true,
},
plugins: ['@typescript-eslint'],
rules: {
'@typescript-eslint/no-unused-vars': ['error'],
Expand Down
5 changes: 5 additions & 0 deletions web3.js/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions web3.js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@solana/web3.js",
"version": "0.0.0-development",
"name": "@epicfaace/web3.js",
"version": "1.21.0",
"description": "Solana Javascript API",
"keywords": [
"api",
Expand Down Expand Up @@ -37,7 +37,7 @@
"/src"
],
"scripts": {
"build": "npm run clean; cross-env NODE_ENV=production rollup -c; npm run type:gen; npm run flow:gen; npm run flow:check",
"build": "cross-env NODE_ENV=production rollup -c; npm run type:gen; npm run flow:gen; npm run flow:check",
"build:fixtures": "set -ex; ./test/fixtures/noop-program/build.sh",
"clean": "rimraf ./coverage ./lib",
"codecov": "set -ex; npm run test:cover; cat ./coverage/lcov.info | codecov",
Expand Down
65 changes: 56 additions & 9 deletions web3.js/src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -860,17 +860,54 @@ function createRpcClient(
return clientBrowser;
}

function createRpcRequest(client: RpcClient): RpcRequest {
function createRpcRequest(
client: RpcClient,
connection: Connection,
): RpcRequest {
return (method, args) => {
return new Promise((resolve, reject) => {
client.request(method, args, (err: any, response: any) => {
if (err) {
reject(err);
return;
if (connection._autoBatch) {
return new Promise((resolve, reject) => {
// Automatically batch requests every 50 ms.
const BATCH_INTERVAL_MS = 50;

connection._batchedRequests.push({
params: {methodName: method, args},
resolve,
reject,
});

if (!connection._pendingBatchTimer) {
connection._pendingBatchTimer = setTimeout(async () => {
try {
// Call the RPC batch request.
const unsafeRes = await connection._rpcBatchRequest(
connection._batchedRequests.map(e => e.params),
);
// Call resolve handler of each promise
connection._batchedRequests.forEach(({resolve}, i) =>
resolve(unsafeRes[i]),
);
} catch (err) {
// Call reject handler of each promise
connection._batchedRequests.forEach(({reject}) => reject(err));
} finally {
connection._pendingBatchTimer = undefined;
connection._batchedRequests = [];
}
}, BATCH_INTERVAL_MS);
}
resolve(response);
});
});
} else {
return new Promise((resolve, reject) => {
client.request(method, args, (err: any, response: any) => {
if (err) {
reject(err);
return;
}
resolve(response);
});
});
}
};
}

Expand Down Expand Up @@ -2073,6 +2110,8 @@ export type ConnectionConfig = {
disableRetryOnRateLimit?: boolean;
/** time to allow for the server to initially process a transaction (in milliseconds) */
confirmTransactionInitialTimeout?: number;
/** automatically batch JSON RPC operations */
autoBatch?: boolean;
};

/**
Expand All @@ -2083,6 +2122,13 @@ export class Connection {
/** @internal */ _confirmTransactionInitialTimeout?: number;
/** @internal */ _rpcEndpoint: string;
/** @internal */ _rpcWsEndpoint: string;
/** @internal */ _autoBatch?: boolean;
/** @internal */ _batchedRequests: {
params: RpcParams;
resolve: (value?: unknown) => void;
reject: (reason?: any) => void;
}[] = [];
/** @internal */ _pendingBatchTimer?: NodeJS.Timeout;
/** @internal */ _rpcClient: RpcClient;
/** @internal */ _rpcRequest: RpcRequest;
/** @internal */ _rpcBatchRequest: RpcBatchRequest;
Expand Down Expand Up @@ -2171,6 +2217,7 @@ export class Connection {
httpHeaders = commitmentOrConfig.httpHeaders;
fetchMiddleware = commitmentOrConfig.fetchMiddleware;
disableRetryOnRateLimit = commitmentOrConfig.disableRetryOnRateLimit;
this._autoBatch = commitmentOrConfig.autoBatch || true;
}

this._rpcEndpoint = endpoint;
Expand All @@ -2183,7 +2230,7 @@ export class Connection {
fetchMiddleware,
disableRetryOnRateLimit,
);
this._rpcRequest = createRpcRequest(this._rpcClient);
this._rpcRequest = createRpcRequest(this._rpcClient, this);
this._rpcBatchRequest = createRpcBatchRequest(this._rpcClient);

this._rpcWebSocket = new RpcWebSocketClient(this._rpcWsEndpoint, {
Expand Down
77 changes: 77 additions & 0 deletions web3.js/test/connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,83 @@ describe('Connection', () => {
expect(balance).to.be.at.least(0);
});

it('get balance - batch a single request', async () => {
let connection_ = new Connection(url, {autoBatch: true});
const account = Keypair.generate();
await mockRpcBatchResponse({
batch: [
{
methodName: 'getBalance',
args: [account.publicKey.toBase58()],
},
],
result: [
{
context: {
slot: 11,
},
value: 5,
},
],
});

const balance = await connection_.getBalance(account.publicKey);
expect(balance).to.equal(5);
});

it('get balance - batch multiple requests', async () => {
let connection_ = new Connection(url, {autoBatch: true});
const account1 = Keypair.generate();
const account2 = Keypair.generate();
const account3 = Keypair.generate();

await mockRpcBatchResponse({
batch: [
{
methodName: 'getBalance',
args: [account1.publicKey.toBase58()],
},
{
methodName: 'getBalance',
args: [account2.publicKey.toBase58()],
},
{
methodName: 'getBalance',
args: [account3.publicKey.toBase58()],
},
],
result: [
{
context: {
slot: 11,
},
value: 5,
},
{
context: {
slot: 11,
},
value: 10,
},
{
context: {
slot: 11,
},
value: 15,
},
],
});

const [balance1, balance2, balance3] = await Promise.all([
connection_.getBalance(account1.publicKey),
connection_.getBalance(account2.publicKey),
connection_.getBalance(account3.publicKey),
]);
expect(balance1).to.equal(5);
expect(balance2).to.equal(10);
expect(balance3).to.equal(15);
});

it('get inflation', async () => {
await mockRpcResponse({
method: 'getInflationGovernor',
Expand Down