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

refactor: Centralise BigNumber imports #725

Merged
merged 11 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 10 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
11 changes: 11 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ module.exports = {
semi: ["error", "always"],
"spaced-comment": ["error", "always", { exceptions: ["-", "+"] }],
"no-console": 0,
"no-restricted-imports": [
"error",
{
"patterns": [
{ group: ["@ethersproject/bignumber"], message: "Use 'src/utils/BigNumberUtils' instead" },
],
"paths": [
{ name: "ethers", importNames: ["BigNumber"], message: "Use 'src/utils/BigNumberUtils' instead" }
]
}
],
"@typescript-eslint/no-unused-vars": ["error", { ignoreRestSiblings: true }],
"chai-expect/missing-assertion": 2,
"no-duplicate-imports": "error",
Expand Down
3 changes: 2 additions & 1 deletion e2e/oracle.e2e.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// @note: This test is _not_ run automatically as part of git hooks or CI.
import dotenv from "dotenv";
import winston from "winston";
import { BigNumber, providers, utils as ethersUtils } from "ethers";
import { providers, utils as ethersUtils } from "ethers";
import { getGasPriceEstimate } from "../src/gasPriceOracle";
import { BigNumber } from "../src/utils";
import { assertPromiseError, expect } from "../test/utils";
dotenv.config({ path: ".env" });

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
"@across-protocol/constants": "^3.1.14",
"@across-protocol/contracts": "^3.0.10",
"@eth-optimism/sdk": "^3.3.1",
"@ethersproject/bignumber": "^5.7.0",
"@pinata/sdk": "^2.1.0",
"@types/mocha": "^10.0.1",
"@uma/sdk": "^0.34.1",
Expand Down
4 changes: 2 additions & 2 deletions src/apiClient/abstractClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ethers } from "ethers";
import { BigNumber } from "../utils";
import {
CoingeckoDataReturnType,
SuggestedFeeReturnType,
Expand Down Expand Up @@ -64,7 +64,7 @@ export default abstract class AbstractApiClient {
* @throws Throws an error if the API call fails.
*/
public abstract getSuggestedFees(
amount: ethers.BigNumber,
amount: BigNumber,
originToken: string,
toChainid: number,
fromChainid: number
Expand Down
27 changes: 14 additions & 13 deletions src/apiClient/mockedClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { ethers } from "ethers";
import { deepCopy } from "ethers/lib/utils";
import { BigNumber, bnOne } from "../utils";
import AbstractApiClient from "./abstractClient";
import {
AcrossBridgeStatisticsType,
Expand Down Expand Up @@ -39,28 +40,28 @@ export default class MockedApiClient extends AbstractApiClient {
);
}
public getSuggestedFees(
_amount: ethers.BigNumber,
_amount: BigNumber,
_originToken: string,
_toChainid: number,
_fromChainid: number
): Promise<SuggestedFeeReturnType> {
return Promise.resolve(
this.mockedData.SuggestedFees ?? {
relayerFee: {
pct: ethers.constants.One,
total: ethers.constants.One,
pct: bnOne,
total: bnOne,
},
relayerCapitalFee: {
pct: ethers.constants.One,
total: ethers.constants.One,
pct: bnOne,
total: bnOne,
},
relayerGasFee: {
pct: ethers.constants.One,
total: ethers.constants.One,
pct: bnOne,
total: bnOne,
},
isAmountTooLow: false,
quoteBlock: ethers.constants.One,
quoteTimestamp: ethers.constants.One,
quoteBlock: bnOne,
quoteTimestamp: bnOne,
}
);
}
Expand All @@ -71,10 +72,10 @@ export default class MockedApiClient extends AbstractApiClient {
): Promise<BridgeLimitsReturnType> {
return Promise.resolve(
this.mockedData.BridgeLimits ?? {
minDeposit: ethers.BigNumber.from("317845960607070"),
maxDeposit: ethers.BigNumber.from("1625976243310274613043"),
maxDepositInstant: ethers.BigNumber.from("148518401181482545509"),
maxDepositShortDelay: ethers.BigNumber.from("1625976243310274613043"),
minDeposit: BigNumber.from("317845960607070"),
maxDeposit: BigNumber.from("1625976243310274613043"),
maxDepositInstant: BigNumber.from("148518401181482545509"),
maxDepositShortDelay: BigNumber.from("1625976243310274613043"),
}
);
}
Expand Down
23 changes: 11 additions & 12 deletions src/apiClient/productionClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from "axios";
import { ethers } from "ethers";
import AbstractApiClient from "./abstractClient";
import { BigNumber, parseEther } from "../utils";
import {
CoingeckoDataReturnType,
SuggestedFeeReturnType,
Expand All @@ -27,14 +27,13 @@ export default class ProductionApiClient extends AbstractApiClient {
},
});
const result = response.data;
const price =
baseCurrency === "usd" ? ethers.utils.parseEther(String(result.price)) : ethers.BigNumber.from(result.price);
const price = baseCurrency === "usd" ? parseEther(String(result.price)) : BigNumber.from(result.price);
return {
price,
};
}
public async getSuggestedFees(
amount: ethers.BigNumber,
amount: BigNumber,
originToken: string,
toChainid: number,
fromChainid: number
Expand All @@ -49,19 +48,19 @@ export default class ProductionApiClient extends AbstractApiClient {
},
});
const result = response.data;
const relayFeePct = ethers.BigNumber.from(result["relayFeePct"]);
const relayFeeTotal = ethers.BigNumber.from(result["relayFeeTotal"]);
const relayFeePct = BigNumber.from(result["relayFeePct"]);
const relayFeeTotal = BigNumber.from(result["relayFeeTotal"]);

const capitalFeePct = ethers.BigNumber.from(result["capitalFeePct"]);
const capitalFeeTotal = ethers.BigNumber.from(result["capitalFeeTotal"]);
const capitalFeePct = BigNumber.from(result["capitalFeePct"]);
const capitalFeeTotal = BigNumber.from(result["capitalFeeTotal"]);

const relayGasFeePct = ethers.BigNumber.from(result["relayGasFeePct"]);
const relayGasFeeTotal = ethers.BigNumber.from(result["relayGasFeeTotal"]);
const relayGasFeePct = BigNumber.from(result["relayGasFeePct"]);
const relayGasFeeTotal = BigNumber.from(result["relayGasFeeTotal"]);

const isAmountTooLow = result["isAmountTooLow"];

const quoteTimestamp = ethers.BigNumber.from(result["timestamp"]);
const quoteBlock = ethers.BigNumber.from(result["quoteBlock"]);
const quoteTimestamp = BigNumber.from(result["timestamp"]);
const quoteBlock = BigNumber.from(result["quoteBlock"]);

return {
relayerFee: {
Expand Down
20 changes: 10 additions & 10 deletions src/apiClient/types.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import { ethers } from "ethers";
import { BigNumber } from "../utils";

export type CoingeckoDataReturnType = { price: ethers.BigNumber };
export type CoingeckoDataReturnType = { price: BigNumber };

export type Fee = {
total: ethers.BigNumber;
pct: ethers.BigNumber;
total: BigNumber;
pct: BigNumber;
};
export type SuggestedFeeReturnType = {
relayerFee: Fee;
relayerGasFee: Fee;
relayerCapitalFee: Fee;
isAmountTooLow: boolean;
quoteTimestamp: ethers.BigNumber;
quoteBlock: ethers.BigNumber;
quoteTimestamp: BigNumber;
quoteBlock: BigNumber;
};

export type BridgeLimitsReturnType = {
minDeposit: ethers.BigNumber;
maxDeposit: ethers.BigNumber;
maxDepositInstant: ethers.BigNumber;
maxDepositShortDelay: ethers.BigNumber;
minDeposit: BigNumber;
maxDeposit: BigNumber;
maxDepositInstant: BigNumber;
maxDepositShortDelay: BigNumber;
};

export type SpecificRewardType = {
Expand Down
7 changes: 3 additions & 4 deletions src/caching/Arweave/ArweaveClient.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import Arweave from "arweave";
import { JWKInterface } from "arweave/node/lib/wallet";
import axios from "axios";
import { BigNumber, ethers } from "ethers";
import { Struct, create } from "superstruct";
import winston from "winston";
import { ARWEAVE_TAG_APP_NAME, ARWEAVE_TAG_APP_VERSION, DEFAULT_ARWEAVE_STORAGE_ADDRESS } from "../../constants";
import { isDefined, jsonReplacerWithBigNumbers, toBN } from "../../utils";
import { BigNumber, isDefined, jsonReplacerWithBigNumbers, toBN } from "../../utils";

export class ArweaveClient {
private client: Arweave;
Expand Down Expand Up @@ -230,14 +229,14 @@ export class ArweaveClient {
* The balance of the signer
* @returns The balance of the signer in winston units
*/
async getBalance(): Promise<ethers.BigNumber> {
async getBalance(): Promise<BigNumber> {
const address = await this.getAddress();
const balanceInFloat = await this.client.wallets.getBalance(address);
// Sometimes the balance is returned in scientific notation, so we need to
// convert it to a BigNumber
if (balanceInFloat.includes("e")) {
const [balance, exponent] = balanceInFloat.split("e");
const resultingBN = ethers.BigNumber.from(balance).mul(toBN(10).pow(exponent.replace("+", "")));
const resultingBN = BigNumber.from(balance).mul(toBN(10).pow(exponent.replace("+", "")));
return BigNumber.from(resultingBN.toString());
} else {
return BigNumber.from(balanceInFloat);
Expand Down
2 changes: 1 addition & 1 deletion src/clients/BundleDataClient/BundleDataClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
} from "../../interfaces";
import { AcrossConfigStoreClient, SpokePoolClient } from "..";
import {
BigNumber,
bnZero,
queryHistoricalDepositForFill,
assign,
Expand All @@ -31,7 +32,6 @@ import {
mapAsync,
bnUint32Max,
} from "../../utils";
import { BigNumber } from "ethers";
import winston from "winston";
import {
_buildPoolRebalanceRoot,
Expand Down
3 changes: 1 addition & 2 deletions src/clients/BundleDataClient/utils/PoolRebalanceUtils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { MerkleTree } from "@across-protocol/contracts/dist/utils/MerkleTree";
import { RunningBalances, PoolRebalanceLeaf, Clients, SpokePoolTargetBalance } from "../../../interfaces";
import { SpokePoolClient } from "../../SpokePoolClient";
import { BigNumber } from "ethers";
import { bnZero, compareAddresses } from "../../../utils";
import { BigNumber, bnZero, compareAddresses } from "../../../utils";
import { HubPoolClient } from "../../HubPoolClient";
import { V3DepositWithBlock } from "./shims";
import { AcrossConfigStoreClient } from "../../AcrossConfigStoreClient";
Expand Down
2 changes: 1 addition & 1 deletion src/clients/BundleDataClient/utils/SuperstructUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
boolean,
defaulted,
} from "superstruct";
import { BigNumber } from "ethers";
import { BigNumber } from "../../../utils";

const PositiveIntegerStringSS = pattern(string(), /\d+/);
const Web3AddressSS = pattern(string(), /^0x[a-fA-F0-9]{40}$/);
Expand Down
3 changes: 2 additions & 1 deletion src/clients/HubPoolClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from "assert";
import { BigNumber, Contract, Event, EventFilter } from "ethers";
import { Contract, Event, EventFilter } from "ethers";
import _ from "lodash";
import winston from "winston";
import { DEFAULT_CACHING_SAFE_LAG, DEFAULT_CACHING_TTL } from "../constants";
Expand All @@ -22,6 +22,7 @@ import {
} from "../interfaces";
import * as lpFeeCalculator from "../lpFeeCalculator";
import {
BigNumber,
BlockFinder,
bnZero,
dedupArray,
Expand Down
3 changes: 2 additions & 1 deletion src/clients/SpokePoolClient.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { BigNumber, Contract, Event, EventFilter } from "ethers";
import { Contract, Event, EventFilter } from "ethers";
import winston from "winston";
import {
AnyObject,
BigNumber,
bnZero,
DefaultLogLevels,
EventSearchConfig,
Expand Down
4 changes: 2 additions & 2 deletions src/clients/mocks/MockHubPoolClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import winston from "winston";
import { BigNumber, Contract, Event } from "ethers";
import { randomAddress, assign, bnZero } from "../../utils";
import { Contract, Event } from "ethers";
import { BigNumber, randomAddress, assign, bnZero } from "../../utils";
import { L1Token, PendingRootBundle, RealizedLpFee } from "../../interfaces";
import { AcrossConfigStoreClient as ConfigStoreClient } from "../AcrossConfigStoreClient";
import { HubPoolClient, HubPoolUpdate, LpFeeRequest } from "../HubPoolClient";
Expand Down
4 changes: 2 additions & 2 deletions src/clients/mocks/MockSpokePoolClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from "assert";
import { BigNumber, Contract, Event, providers } from "ethers";
import { Contract, Event, providers } from "ethers";
import { random } from "lodash";
import winston from "winston";
import { ZERO_ADDRESS } from "../../constants";
Expand All @@ -15,7 +15,7 @@ import {
SlowFillLeaf,
SpeedUp,
} from "../../interfaces";
import { bnZero, toBN, toBNWei, forEachAsync, getCurrentTime, randomAddress } from "../../utils";
import { BigNumber, bnZero, toBN, toBNWei, forEachAsync, getCurrentTime, randomAddress } from "../../utils";
import { SpokePoolClient, SpokePoolUpdate } from "../SpokePoolClient";
import { HubPoolClient } from "../HubPoolClient";
import { EventManager, EventOverrides, getEventManager } from "./MockEvents";
Expand Down
5 changes: 1 addition & 4 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { constants as ethersConstants, BigNumber, utils } from "ethers";
import { constants as ethersConstants } from "ethers";
import { TOKEN_SYMBOLS_MAP } from "@across-protocol/constants";

export {
Expand Down Expand Up @@ -44,9 +44,6 @@ export const PROTOCOL_DEFAULT_CHAIN_ID_INDICES = [1, 10, 137, 288, 42161];
export const DEFAULT_CACHING_TTL = 60 * 60 * 24 * 7 * 2; // 2 Weeks
export const DEFAULT_CACHING_SAFE_LAG = 60 * 60; // 1 hour

export const UBA_BOUNDS_RANGE_MAX = BigNumber.from(String(Number.MAX_SAFE_INTEGER)).mul(utils.parseEther("1.0"));
export const UBA_BOUNDS_RANGE_MIN = UBA_BOUNDS_RANGE_MAX.mul(-1);

export const DEFAULT_SIMULATED_RELAYER_ADDRESS = "0x07aE8551Be970cB1cCa11Dd7a11F47Ae82e70E67";
export const DEFAULT_SIMULATED_RELAYER_ADDRESS_TEST = "0x9A8f92a830A5cB89a3816e3D267CB7791c16b04D"; // Görli, ...

Expand Down
4 changes: 2 additions & 2 deletions src/contracts/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert from "assert";
import { BigNumber, BigNumberish, Event } from "ethers";
import { isDefined } from "../utils";
import { Event } from "ethers";
import { BigNumber, BigNumberish, isDefined } from "../utils";

/**
* @dev Originally imported from @uma/sdk.
Expand Down
7 changes: 4 additions & 3 deletions src/gasPriceOracle/adapters/arbitrum.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { providers, utils as ethersUtils } from "ethers";
import { bnOne } from "../../utils";
import { providers } from "ethers";
import { BigNumber, bnOne, parseUnits } from "../../utils";
import { GasPriceEstimate } from "../types";
import * as ethereum from "./ethereum";

const DEFAULT_PRIORITY_FEE = ethersUtils.parseUnits("1.5", 9);
let DEFAULT_PRIORITY_FEE: BigNumber | undefined = undefined;

// Arbitrum Nitro implements EIP-1559 pricing, but the priority fee is always refunded to the caller. Further,
// ethers typically hardcodes the priority fee to 1.5 Gwei. So, confirm that the priority fee supplied was 1.5
// Gwei, and then drop it to 1 Wei. Reference: https://developer.arbitrum.io/faqs/gas-faqs#q-priority
export async function eip1559(provider: providers.Provider, chainId: number): Promise<GasPriceEstimate> {
DEFAULT_PRIORITY_FEE ??= parseUnits("1.5", 9);
const { maxFeePerGas: _maxFeePerGas, maxPriorityFeePerGas } = await ethereum.eip1559(provider, chainId);

// If this throws, ethers default behaviour has changed, or Arbitrum RPCs are returning something more sensible.
Expand Down
4 changes: 2 additions & 2 deletions src/gasPriceOracle/adapters/ethereum.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BigNumber, providers } from "ethers";
import { bnZero } from "../../utils";
import { providers } from "ethers";
import { BigNumber, bnZero } from "../../utils";
import { GasPriceEstimate } from "../types";
import { gasPriceError } from "../util";

Expand Down
10 changes: 5 additions & 5 deletions src/gasPriceOracle/adapters/polygon.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { providers, utils as ethersUtils } from "ethers";
import { providers } from "ethers";
import { BaseHTTPAdapter, BaseHTTPAdapterArgs } from "../../priceClient/adapters/baseAdapter";
import { bnZero, isDefined } from "../../utils";
import { bnZero, isDefined, parseUnits } from "../../utils";
import { CHAIN_IDs } from "../../constants";
import { GasPriceEstimate } from "../types";
import { gasPriceError } from "../util";
Expand Down Expand Up @@ -50,12 +50,12 @@ class PolygonGasStation extends BaseHTTPAdapter {

[gasPrice.maxFee, gasPrice.maxPriorityFee].forEach((gasPrice) => {
if (Number(gasPrice) < 0) {
gasPriceError("getFeeData()", this.chainId, ethersUtils.parseUnits(gasPrice.toString(), 9));
gasPriceError("getFeeData()", this.chainId, parseUnits(gasPrice.toString(), 9));
}
});

const maxPriorityFeePerGas = ethersUtils.parseUnits(gasPrice.maxPriorityFee.toString(), 9);
const maxFeePerGas = ethersUtils.parseUnits(gasPrice.maxFee.toString(), 9);
const maxPriorityFeePerGas = parseUnits(gasPrice.maxPriorityFee.toString(), 9);
const maxFeePerGas = parseUnits(gasPrice.maxFee.toString(), 9);

return { maxPriorityFeePerGas, maxFeePerGas };
}
Expand Down
Loading
Loading