Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholaspai committed Dec 25, 2024
1 parent 33e36a4 commit de2a339
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 38 deletions.
12 changes: 6 additions & 6 deletions src/gasPriceOracle/adapters/linea-viem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import { PopulatedTransaction } from "ethers";
* in that the recommended fee per gas is hardcoded to 7 gwei while the priority fee is dynamic based on the
* compressed transaction size, layer 1 verification costs and capacity, gas price ratio between layer 1 and layer 2,
* the transaction's gas usage, the minimum gas price on layer 2,
* and a minimum margin (for error) for gas price estimation.
* and a minimum margin (for error) for gas price estimation.
* @dev Because the Linea priority fee is more volatile than the base fee, the base fee multiplier will be applied
* to the priority fee.
* @param provider
* @param _chainId
* @param baseFeeMultiplier
* @param _unsignedTx
* @returns
* @param provider
* @param _chainId
* @param baseFeeMultiplier
* @param _unsignedTx
* @returns
*/
export async function eip1559(
provider: PublicClient,
Expand Down
62 changes: 31 additions & 31 deletions test/GasPriceOracle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import dotenv from "dotenv";
import winston from "winston";
import { providers } from "ethers";
import { encodeFunctionData } from 'viem';
import { encodeFunctionData } from "viem";
import { getGasPriceEstimate } from "../src/gasPriceOracle";
import { BigNumber, bnZero, parseUnits } from "../src/utils";
import { expect, makeCustomTransport, randomAddress } from "../test/utils";
Expand All @@ -18,7 +18,7 @@ const dummyLogger = winston.createLogger({

const stdLastBaseFeePerGas = parseUnits("12", 9);
const stdMaxPriorityFeePerGas = parseUnits("1", 9); // EIP-1559 chains only
const expectedLineaMaxFeePerGas = parseUnits("7", 9)
const expectedLineaMaxFeePerGas = parseUnits("7", 9);
const ethersProviderChainIds = [1, 10, 137, 324, 8453, 42161, 534352, 59144];
const viemProviderChainIds = [59144];

Expand All @@ -28,7 +28,10 @@ const provider = new providers.StaticJsonRpcProvider("https://eth.llamarpc.com")

const ERC20ABI = [
{
inputs: [{ name: "to", type: "address" }, { name: "amount", type: "uint256" }],
inputs: [
{ name: "to", type: "address" },
{ name: "amount", type: "uint256" },
],
name: "transfer",
outputs: [],
stateMutability: "nonpayable",
Expand All @@ -38,8 +41,8 @@ const ERC20ABI = [
const erc20TransferTransactionObject = encodeFunctionData({
abi: ERC20ABI,
functionName: "transfer",
args: [randomAddress(), 1n]
})
args: [randomAddress(), 1n],
});

describe("Gas Price Oracle", function () {
it("Viem gas price retrieval", async function () {
Expand All @@ -53,37 +56,36 @@ describe("Gas Price Oracle", function () {
to: randomAddress(),
from: randomAddress(),
value: bnZero,
data: erc20TransferTransactionObject
data: erc20TransferTransactionObject,
},
undefined
]
undefined,
];
const baseFeeMultiplier = 2.0;
for (const unsignedTx of unsignedTxns) {
const { maxFeePerGas, maxPriorityFeePerGas } = await getGasPriceEstimate(provider, {
chainId,
transport: customTransport,
unsignedTx,
baseFeeMultiplier
baseFeeMultiplier,
});

dummyLogger.debug({
at: "Viem: Gas Price Oracle",
message: `Retrieved gas price estimate for chain ID ${chainId}`,
maxFeePerGas: maxFeePerGas.toString(),
maxPriorityFeePerGas: maxPriorityFeePerGas.toString(),
unsignedTx
unsignedTx,
});

expect(BigNumber.isBigNumber(maxFeePerGas)).to.be.true;
expect(BigNumber.isBigNumber(maxPriorityFeePerGas)).to.be.true;

// For Linea, base fee is expected to be hardcoded and unaffected by the base fee multiplier while
// the priority fee gets scaled.
const expectedPriorityFee = stdMaxPriorityFeePerGas.mul(2.0);
expect(maxFeePerGas).to.equal(expectedLineaMaxFeePerGas.add(expectedPriorityFee));
expect(maxPriorityFeePerGas).to.equal(expectedPriorityFee)
expect(maxPriorityFeePerGas).to.equal(expectedPriorityFee);
}

}
delete process.env[chainKey];
}
Expand All @@ -93,55 +95,53 @@ describe("Gas Price Oracle", function () {
// as a fake Polygon gas station API, so it doesn't send real RPC requests.

const baseFeeMultiplier = 2.0;
// For this test, we only use the raw gas price feed for ethereum just so we can
// For this test, we only use the raw gas price feed for ethereum just so we can
// test both the bad and raw variants, since other chains will ultimately call the Etheruem
// adapter.
const eip1559RawGasPriceFeedChainIds = [1];
for (const chainId of ethersProviderChainIds) {
if (eip1559RawGasPriceFeedChainIds.includes(chainId)) {
const chainKey = `GAS_PRICE_EIP1559_RAW_${chainId}`;
process.env[chainKey] = "true";
process.env[chainKey] = "true";
}
const [
{ maxFeePerGas: markedUpMaxFeePerGas, maxPriorityFeePerGas: markedUpMaxPriorityFeePerGas },
{ maxFeePerGas, maxPriorityFeePerGas }
] =
await Promise.all([
getGasPriceEstimate(provider, { chainId, baseFeeMultiplier, transport: customTransport }),
getGasPriceEstimate(provider, { chainId, baseFeeMultiplier: 1.0, transport: customTransport }),
]
);
{ maxFeePerGas, maxPriorityFeePerGas },
] = await Promise.all([
getGasPriceEstimate(provider, { chainId, baseFeeMultiplier, transport: customTransport }),
getGasPriceEstimate(provider, { chainId, baseFeeMultiplier: 1.0, transport: customTransport }),
]);

dummyLogger.debug({
at: "Ethers: Gas Price Oracle",
message: `Retrieved gas price estimate for chain ID ${chainId}`,
maxFeePerGas: maxFeePerGas.toString(),
maxPriorityFeePerGas: maxPriorityFeePerGas.toString(),
markedUpMaxFeePerGas: markedUpMaxFeePerGas.toString(),
markedUpMaxPriorityFeePerGas: markedUpMaxPriorityFeePerGas.toString()
markedUpMaxPriorityFeePerGas: markedUpMaxPriorityFeePerGas.toString(),
});

expect(BigNumber.isBigNumber(maxFeePerGas)).to.be.true;
expect(BigNumber.isBigNumber(maxPriorityFeePerGas)).to.be.true;

// @dev: The following tests *might* be flaky because the above two getGasPriceEstimate
// calls are technically two separate API calls and the suggested base and priority fees
// might be different. In practice, the fees rarely change when called in rapid succession.

// Base fee should be multiplied by multiplier. Returned max fee includes priority fee
// Base fee should be multiplied by multiplier. Returned max fee includes priority fee
// so back it ou.
const expectedMarkedUpMaxFeePerGas = (maxFeePerGas.sub(maxPriorityFeePerGas)).mul(2)
const expectedMarkedUpMaxFeePerGas = maxFeePerGas.sub(maxPriorityFeePerGas).mul(2);
expect(markedUpMaxFeePerGas.sub(markedUpMaxPriorityFeePerGas)).to.equal(expectedMarkedUpMaxFeePerGas);
expect(markedUpMaxFeePerGas.gt(maxFeePerGas)).to.be.true;

// Priority fees should be the same
expect(markedUpMaxPriorityFeePerGas).to.equal(maxPriorityFeePerGas)
expect(markedUpMaxPriorityFeePerGas).to.equal(maxPriorityFeePerGas);

if (chainId === 42161) {
// Arbitrum priority fee should be 1 wei.
expect(markedUpMaxPriorityFeePerGas).to.equal(1);
expect(maxPriorityFeePerGas).to.equal(1);
}
}
if (eip1559RawGasPriceFeedChainIds.includes(chainId)) {
const chainKey = `GAS_PRICE_EIP1559_RAW_${chainId}`;
delete process.env[chainKey];
Expand Down
2 changes: 1 addition & 1 deletion test/utils/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const makeCustomTransport = (
// Linea base fee is always 7 gwei
baseFeePerGas: BigInt(parseUnits("7", 9).toString()),
priorityFeePerGas: BigInt(stdMaxPriorityFeePerGas.toString()),
gasLimit: BigInt("0")
gasLimit: BigInt("0"),
};
default:
throw new Error(`Unsupported method: ${method}.`);
Expand Down

0 comments on commit de2a339

Please sign in to comment.