Skip to content

Commit

Permalink
improve(TransactionUtils): Use sdk-v2 gasPriceOracle (#742)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pxrl committed Aug 14, 2023
1 parent c91bc25 commit d35c4a4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 41 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
57 changes: 19 additions & 38 deletions src/utils/TransactionUtils.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -134,23 +134,23 @@ export async function getGasPrice(
priorityScaler = 1.2,
maxFeePerGasScaler = 3
): Promise<Partial<FeeData>> {
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<TransactionSimulationResult> {
Expand Down Expand Up @@ -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"));
}
3 changes: 1 addition & 2 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down

0 comments on commit d35c4a4

Please sign in to comment.