From d35c4a4b05b94f35542cf65cc34164af75e90b0f Mon Sep 17 00:00:00 2001 From: Paul <108695806+pxrl@users.noreply.github.com> Date: Mon, 14 Aug 2023 23:30:58 +0200 Subject: [PATCH] improve(TransactionUtils): Use sdk-v2 gasPriceOracle (#742) The gasPriceOracle was recently upgraded to support Polygon's gas station. As a nice side-effect, we can remove node-fetch from our list of dependencies. Fixes ACX-1434. --- package.json | 1 - src/utils/TransactionUtils.ts | 57 ++++++++++++----------------------- src/utils/index.ts | 3 +- 3 files changed, 20 insertions(+), 41 deletions(-) diff --git a/package.json b/package.json index a5a51b4d6..3afad0fab 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,6 @@ "lodash": "^4.17.21", "lodash.get": "^4.4.2", "minimist": "^1.2.8", - "node-fetch": "2.6.7", "redis4": "npm:redis@^4.1.0", "ts-node": "^10.9.1", "winston": "^3.10.0", diff --git a/src/utils/TransactionUtils.ts b/src/utils/TransactionUtils.ts index 79c530863..ea0da36f2 100644 --- a/src/utils/TransactionUtils.ts +++ b/src/utils/TransactionUtils.ts @@ -1,8 +1,8 @@ -import { typeguards } from "@across-protocol/sdk-v2"; +import { gasPriceOracle, typeguards } from "@across-protocol/sdk-v2"; import { AugmentedTransaction } from "../clients"; -import { winston, Contract, getContractInfoFromAddress, fetch, ethers, Wallet } from "../utils"; +import { winston, Contract, getContractInfoFromAddress, ethers, Wallet } from "../utils"; import { DEFAULT_GAS_FEE_SCALERS, multicall3Addresses } from "../common"; -import { toBNWei, BigNumber, toBN, toGWei, TransactionResponse } from "../utils"; +import { toBNWei, BigNumber, toBN, TransactionResponse } from "../utils"; import { getAbi } from "@uma/contracts-node"; import dotenv from "dotenv"; import { FeeData } from "@ethersproject/abstract-provider"; @@ -134,23 +134,23 @@ export async function getGasPrice( priorityScaler = 1.2, maxFeePerGasScaler = 3 ): Promise> { - const [feeData, chainInfo] = await Promise.all([provider.getFeeData(), provider.getNetwork()]); - if (feeData.maxFeePerGas && feeData.maxPriorityFeePerGas) { - // Polygon, for some or other reason, does not correctly return an appropriate maxPriorityFeePerGas. Set the - // maxPriorityFeePerGas to the maxFeePerGas * 5 for now as a temp workaround. - if (chainInfo.chainId === 137) { - feeData.maxPriorityFeePerGas = toGWei((await getPolygonPriorityFee()).fastest.toString()); - } - if (feeData.maxPriorityFeePerGas.gt(feeData.maxFeePerGas)) { - feeData.maxFeePerGas = scaleByNumber(feeData.maxPriorityFeePerGas, 1.5); - } - return { - maxFeePerGas: scaleByNumber(feeData.maxFeePerGas, priorityScaler * maxFeePerGasScaler), // scale up the maxFeePerGas. Any extra paid on this is refunded. - maxPriorityFeePerGas: scaleByNumber(feeData.maxPriorityFeePerGas, priorityScaler), - }; - } else { - return { gasPrice: scaleByNumber(feeData.gasPrice, priorityScaler) }; + const { chainId } = await provider.getNetwork(); + const feeData = await gasPriceOracle.getGasPriceEstimate(provider, chainId); + + if (feeData.maxPriorityFeePerGas.gt(feeData.maxFeePerGas)) { + feeData.maxFeePerGas = scaleByNumber(feeData.maxPriorityFeePerGas, 1.5); + } + + // Handle chains with legacy pricing. + if (feeData.maxPriorityFeePerGas.eq(0)) { + return { gasPrice: scaleByNumber(feeData.maxFeePerGas, priorityScaler) }; } + + // Default to EIP-1559 (type 2) pricing. + return { + maxFeePerGas: scaleByNumber(feeData.maxFeePerGas, priorityScaler * maxFeePerGasScaler), + maxPriorityFeePerGas: scaleByNumber(feeData.maxPriorityFeePerGas, priorityScaler), + }; } export async function willSucceed(transaction: AugmentedTransaction): Promise { @@ -184,25 +184,6 @@ export function getTarget(targetAddress: string): } } -async function getPolygonPriorityFee(): Promise<{ - safeLow: number; - standard: number; - fast: number; - fastest: number; - blockTime: number; - blockNumber: number; -}> { - const res = await fetch("https://gasstation.polygon.technology"); - return (await res.json()) as { - safeLow: number; - standard: number; - fast: number; - fastest: number; - blockTime: number; - blockNumber: number; - }; -} - function scaleByNumber(amount: ethers.BigNumber, scaling: number) { return amount.mul(toBNWei(scaling)).div(toBNWei("1")); } diff --git a/src/utils/index.ts b/src/utils/index.ts index 1ddcf6af6..ffa45d36b 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,8 +1,7 @@ // Utils from other packages. import winston from "winston"; import assert from "assert"; -import fetch from "node-fetch"; -export { winston, assert, fetch }; +export { winston, assert }; export { Logger } from "@uma/financial-templates-lib"; export { BigNumber, Signer, Contract, ContractFactory, Transaction, BigNumberish } from "ethers";