Skip to content

Commit

Permalink
fix(bridge-ui): handle scientific notation (#14105)
Browse files Browse the repository at this point in the history
  • Loading branch information
jscriptcoder authored Jul 5, 2023
1 parent fabefb2 commit fcc154e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/bridge-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"type": "module",
"scripts": {
"start": "pnpm run dev",
"start:a3": "pnpm run dev --mode a3",
"dev": "vite",
"build": "vite build",
"preview": "vite preview",
Expand Down
11 changes: 5 additions & 6 deletions packages/bridge-ui/src/relayer-api/RelayerAPIService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
} from '../domain/relayerApi';
import type { BridgeTransaction } from '../domain/transaction';
import { getLogger } from '../utils/logger';
import { toBigNumber } from '../utils/toBigNumber';
import { tokenVaults } from '../vault/tokenVaults';

const log = getLogger('RelayerAPIService');
Expand Down Expand Up @@ -212,14 +213,12 @@ export class RelayerAPIService implements RelayerAPI {
memo: tx.data.Message.Memo,
owner: tx.data.Message.Owner,
sender: tx.data.Message.Sender,
gasLimit: BigNumber.from(tx.data.Message.GasLimit.toString()),
callValue: BigNumber.from(tx.data.Message.CallValue.toString()),
gasLimit: toBigNumber(tx.data.Message.GasLimit),
callValue: toBigNumber(tx.data.Message.CallValue),
srcChainId: tx.data.Message.SrcChainId,
destChainId: tx.data.Message.DestChainId,
depositValue: BigNumber.from(tx.data.Message.DepositValue.toString()),
processingFee: BigNumber.from(
tx.data.Message.ProcessingFee.toString(),
),
depositValue: toBigNumber(tx.data.Message.DepositValue),
processingFee: toBigNumber(tx.data.Message.ProcessingFee),
refundAddress: tx.data.Message.RefundAddress,
},
};
Expand Down
15 changes: 15 additions & 0 deletions packages/bridge-ui/src/utils/toBigNumber.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { BigNumber } from "ethers";

import { toBigNumber } from "./toBigNumber";

describe('toBigNumber', () => {
it('should handle different notation for big ints', () => {
expect(toBigNumber('1000000000000000000000').toString()).toEqual('1000000000000000000000');
expect(toBigNumber(2e+21).toString()).toEqual('2000000000000000000000');
expect(toBigNumber(3e21).toString()).toEqual('3000000000000000000000');
expect(toBigNumber(1000).toString()).toEqual('1000');

// Number.MAX_SAFE_INTEGER = 9007199254740991. Maximum safe integer (2^53 – 1)
expect(toBigNumber(BigInt(Number.MAX_SAFE_INTEGER) * 3n).toString()).toEqual('27021597764222973');
});
});
6 changes: 6 additions & 0 deletions packages/bridge-ui/src/utils/toBigNumber.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { BigNumber } from "ethers";

// This will also handle scientific notation (or e notation: 2e+21)
export function toBigNumber(value: string | number | bigint | boolean): BigNumber {
return BigNumber.from(BigInt(value).toString());
}

0 comments on commit fcc154e

Please sign in to comment.