-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from etherspot/polygon-gas-fees
use-oracle-for-matic
- Loading branch information
Showing
10 changed files
with
70 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"name": "root", | ||
"private": true, | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"engines": { | ||
"node": ">=12.9.0" | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "cli", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "> TODO: description", | ||
"author": "zincoshine <[email protected]>", | ||
"homepage": "https://https://github.com/etherspot/skandha#readme", | ||
|
@@ -31,13 +31,13 @@ | |
"url": "https://https://github.com/etherspot/skandha/issues" | ||
}, | ||
"dependencies": { | ||
"api": "^0.0.5", | ||
"db": "^0.0.5", | ||
"executor": "^0.0.5", | ||
"api": "^0.0.6", | ||
"db": "^0.0.6", | ||
"executor": "^0.0.6", | ||
"find-up": "5.0.0", | ||
"got": "12.5.3", | ||
"js-yaml": "4.1.0", | ||
"types": "^0.0.5", | ||
"types": "^0.0.6", | ||
"yargs": "17.6.2" | ||
}, | ||
"devDependencies": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { BigNumber, BigNumberish, ethers, providers } from "ethers"; | ||
import { NetworkName } from "types/lib"; | ||
|
||
export type IGetGasFeeResult = { | ||
maxPriorityFeePerGas: BigNumberish | undefined; | ||
maxFeePerGas: BigNumberish | undefined; | ||
}; | ||
|
||
export const getGasFee = async ( | ||
network: NetworkName, | ||
provider: providers.JsonRpcProvider | ||
): Promise<IGetGasFeeResult> => { | ||
if (network === "matic") { | ||
try { | ||
return getMaticGasFee(); | ||
} catch (err) { | ||
// eslint-disable-next-line no-console | ||
console.error(`Couldn't fetch fee data for matic: ${err}`); | ||
} | ||
} | ||
try { | ||
const feeData = await provider.getFeeData(); | ||
return { | ||
maxPriorityFeePerGas: feeData.maxPriorityFeePerGas ?? 0, | ||
maxFeePerGas: feeData.maxFeePerGas ?? 0, | ||
}; | ||
} catch (err) { | ||
// eslint-disable-next-line no-console | ||
console.error(`Couldn't fetch fee data: ${err}`); | ||
} | ||
return { | ||
maxPriorityFeePerGas: undefined, | ||
maxFeePerGas: undefined, | ||
}; | ||
}; | ||
|
||
function parseGwei(num: number): BigNumber { | ||
return ethers.utils.parseUnits(num.toFixed(9), "gwei"); | ||
} | ||
|
||
export const getMaticGasFee = async (): Promise<IGetGasFeeResult> => { | ||
const oracle = "https://gasstation-mainnet.matic.network/v2"; | ||
const data = await (await fetch(oracle)).json(); | ||
return { | ||
maxPriorityFeePerGas: parseGwei(data.fast.maxPriorityFee), | ||
maxFeePerGas: parseGwei(data.fast.maxFee), | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters