Skip to content

Commit

Permalink
Merge pull request #302 from curvefi/feat/add-getGasInfoForL2
Browse files Browse the repository at this point in the history
Feat/add get gas info for l2
  • Loading branch information
fedorovdg authored Apr 2, 2024
2 parents 269003a + 946d058 commit 49aafcf
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 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.56.12",
"version": "2.56.13",
"description": "JavaScript library for curve.fi",
"main": "lib/index.js",
"author": "Macket",
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import {
getUsdRate,
getGasPriceFromL1,
getGasPriceFromL2,
getGasInfoForL2,
getTVL,
getCoinsData,
getVolume,
Expand Down Expand Up @@ -165,6 +166,7 @@ const curve = {
getUsdRate,
getGasPriceFromL1,
getGasPriceFromL2,
getGasInfoForL2,
getTVL,
getBalances,
getAllowance,
Expand Down
30 changes: 29 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,21 @@ export const getUsdRate = async (coin: string): Promise<number> => {
return await _getUsdRate(coinAddress);
}

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

try {
const block = await provider.getBlock('latest');
if(!block) {
return 0.01
}

return Number(block.baseFeePerGas) / (10**9);
} catch (error: any) {
throw new Error(error)
}
}

export const getGasPriceFromL1 = async (): Promise<number> => {
if(L2Networks.includes(curve.chainId) && curve.L1WeightedGasPrice) {
return curve.L1WeightedGasPrice + 1e9; // + 1 gwei
Expand All @@ -525,7 +540,7 @@ export const getGasPriceFromL1 = async (): Promise<number> => {

export const getGasPriceFromL2 = async (): Promise<number> => {
if(curve.chainId === 42161) {
return 0.1 * 1e9; // constant 0.1 gwei
return await getBaseFeeByLastBlock()
}
if(L2Networks.includes(curve.chainId)) {
const gasPrice = await curve.contracts[curve.constants.ALIASES.gas_oracle_blob].contract.gasPrice({"gasPrice":"0x2000000"});
Expand All @@ -535,6 +550,19 @@ export const getGasPriceFromL2 = async (): Promise<number> => {
}
}

export const getGasInfoForL2 = async (): Promise<Record<string, number>> => {
if(curve.chainId === 42161) {
const baseFee = await getBaseFeeByLastBlock()

return {
maxFeePerGas: (baseFee * 1.1) + 0.01,
maxPriorityFeePerGas: 0.01,
}
} else {
throw Error("This method exists only for L2 networks");
}
}

export const getTxCostsUsd = (ethUsdRate: number, gasPrice: number, gas: number | number[], gasPriceL1 = 0): number => {
if(Array.isArray(gas)) {
return ethUsdRate * ((gas[0] * gasPrice / 1e18) + (gas[1] * gasPriceL1 / 1e18));
Expand Down

0 comments on commit 49aafcf

Please sign in to comment.