Skip to content

Commit

Permalink
Fix RPC switching, try to avoid BigInt issue
Browse files Browse the repository at this point in the history
  • Loading branch information
EvgenKor committed Jul 2, 2024
1 parent fb80143 commit 1d12a58
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zkbob-client-js",
"version": "5.5.0",
"version": "5.5.1",
"description": "zkBob integration library",
"repository": "git@github.com:zkBob/libzkbob-client-js.git",
"author": "Dmitry Vdovin <voidxnull@gmail.com>",
Expand Down Expand Up @@ -37,8 +37,7 @@
"tronweb": "^5.3.0",
"wasm-feature-detect": "^1.2.11",
"web3": "1.8.0",
"web3-utils": "1.8.0",
"promise-retry": "^2.0.1"
"web3-utils": "1.8.0"
},
"devDependencies": {
"@types/ethereum-protocol": "^1.0.1",
Expand Down
8 changes: 4 additions & 4 deletions src/client-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,14 +379,14 @@ export class ZkBobProvider {

// Convert native pool amount to the base units
public async shieldedAmountToWei(amountShielded: bigint): Promise<bigint> {
const denominator = await this.denominator();
return denominator > 0 ? amountShielded * denominator : amountShielded / (-denominator);
const denominator = BigInt(await this.denominator());
return denominator > 0 ? BigInt(amountShielded) * denominator : BigInt(amountShielded) / (-denominator);
}

// Convert base units to the native pool amount
public async weiToShieldedAmount(amountWei: bigint): Promise<bigint> {
const denominator = await this.denominator();
return denominator > 0 ? amountWei / denominator : amountWei * (-denominator);
const denominator = BigInt(await this.denominator());
return denominator > 0 ? BigInt(amountWei) / denominator : BigInt(amountWei) * (-denominator);
}

// Round up the fee if needed with fixed fee decimal places (after point)
Expand Down
37 changes: 20 additions & 17 deletions src/networks/rpcman.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { InternalError } from "../errors";
import promiseRetry from 'promise-retry';

const RPC_ISSUES_THRESHOLD = 20; // number of errors needed to switch RPC

Expand All @@ -26,23 +25,27 @@ export class MultiRpcManager {
}

// Performs RPC interaction within several attempts. The errors will registered automatically
protected commonRpcRetry(closure: () => any, errorPattern: string, retriesCnt: number): Promise<any> {
return promiseRetry(
async (retry, attempt) => {
try {
return await closure();
} catch (e) {
console.error(`${errorPattern ?? 'Error occured'} [attempt #${attempt}]: ${e.message}`);
this.registerRpcIssue();
retry(e)
}
},
{
retries: retriesCnt,
minTimeout: 500,
maxTimeout: 500,
protected async commonRpcRetry(closure: () => any, errorPattern: string, retriesCnt: number): Promise<any> {
let cnt = 0;
const attemptMinDelayMs = 500;
let lastErr;
do {
const startTs = Date.now();
try {
return await closure();
} catch (e) {
lastErr = e;
console.error(`${errorPattern ?? 'Error occured'} [attempt #${cnt + 1}]: ${e.message}`);
this.registerRpcIssue();

const delay = Date.now() - startTs;
if (delay < attemptMinDelayMs) {
await new Promise(f => setTimeout(f, attemptMinDelayMs - delay));
}
}
);
} while (++cnt < retriesCnt);

throw new InternalError(`MultRpcManager: ${lastErr.message}`)
}

// ----------------------=========< RPC switching >=========----------------------
Expand Down

0 comments on commit 1d12a58

Please sign in to comment.