Skip to content

Commit

Permalink
Merge pull request #338 from curvefi/fix/fixed-gas-for-x-layer
Browse files Browse the repository at this point in the history
fix: fixed getGasPriceFromL2 for x-layer
  • Loading branch information
fedorovdg authored May 17, 2024
2 parents 2f1c909 + e91f67f commit 8534f7a
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 30 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@curvefi/api",
"version": "2.58.8",
"version": "2.58.9",
"description": "JavaScript library for curve.fi",
"main": "lib/index.js",
"author": "Macket",
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ import {
hasDepositAndStake,
hasRouter,
getBasePools,
getGasPriceByLastTransactions,
getGasPrice,
} from "./utils.js";
import {
deployStablePlainPool,
Expand Down Expand Up @@ -168,7 +168,7 @@ const curve = {
getGasPriceFromL1,
getGasPriceFromL2,
getGasInfoForL2,
getGasPriceByLastTransactions,
getGasPrice,
getTVL,
getBalances,
getAllowance,
Expand Down
31 changes: 4 additions & 27 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,32 +532,9 @@ export const getBaseFeeByLastBlock = async () => {
}
}

export const getGasPriceByLastTransactions = async () => {
export const getGasPrice = async () => {
const provider = curve.provider;

const latestBlockNumber = await provider.getBlockNumber();

let totalGasPrice = 0;
let transactionsCount = 0;

const txAmount = 15;

for (let i = latestBlockNumber; i > latestBlockNumber - txAmount && i >= 0; i--) {
const block = await provider.getBlock(i);
if (!block) continue;

for (const txHash of block.transactions) {
const tx = await provider.getTransaction(txHash);
if (!tx) continue;

totalGasPrice = totalGasPrice + Number(tx.gasPrice);
transactionsCount++;

if (transactionsCount >= txAmount) break; // Limit to txAmount transactions
}
}

return Number((totalGasPrice / transactionsCount / 1e9).toFixed(2));
return Number((Number((await provider.getFeeData()).gasPrice) / 1e9).toFixed(2));
}

export const getGasPriceFromL1 = async (): Promise<number> => {
Expand All @@ -573,7 +550,7 @@ export const getGasPriceFromL2 = async (): Promise<number> => {
return await getBaseFeeByLastBlock()
}
if(curve.chainId === 196) {
return await getGasPriceByLastTransactions() // gwei
return await getGasPrice() // gwei
}
if(L2Networks.includes(curve.chainId)) {
const gasPrice = await curve.contracts[curve.constants.ALIASES.gas_oracle_blob].contract.gasPrice({"gasPrice":"0x2000000"});
Expand All @@ -592,7 +569,7 @@ export const getGasInfoForL2 = async (): Promise<Record<string, number | null>>
maxPriorityFeePerGas: 0.01,
}
} else if(curve.chainId === 196) {
const gasPrice = await getGasPriceByLastTransactions()
const gasPrice = await getGasPrice()

return {
gasPrice,
Expand Down

0 comments on commit 8534f7a

Please sign in to comment.