-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'decentralized-stable' into main
- Loading branch information
Showing
8 changed files
with
200 additions
and
62 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
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,29 @@ | ||
import axios from "axios"; | ||
import { | ||
GAS_LIMIT, | ||
TGas, | ||
convertToGwei, | ||
getConstantGas, | ||
getGas, | ||
} from "./utils"; | ||
|
||
const getConstantGasForPolygon = (): TGas => getConstantGas(800); | ||
|
||
const getGasFromGasStation = async (): Promise<TGas> => { | ||
const response = await axios.get("https://gasstation.polygon.technology/v2"); | ||
console.log("polygon gas response", response.data); | ||
|
||
const gas = convertToGwei(String(response?.data?.fast.maxFee)); | ||
|
||
return { | ||
maxPriorityFeePerGas: gas, | ||
maxFeePerGas: gas, | ||
gasLimit: GAS_LIMIT, | ||
}; | ||
}; | ||
|
||
export const getPolygonGas = async (): Promise<TGas> => { | ||
const gas = await getGas(getGasFromGasStation, getConstantGasForPolygon); | ||
console.log({ gas }, "gas in internal polygon"); | ||
return gas; | ||
}; |
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,60 @@ | ||
import { BigNumberish as EthersBigNumber, ethers } from "ethers"; | ||
|
||
export type TGas = { | ||
maxPriorityFeePerGas: EthersBigNumber; | ||
maxFeePerGas: EthersBigNumber; | ||
gasLimit: number; | ||
}; | ||
|
||
type TArgs = [ | ||
getGasStationGas: () => Promise<TGas>, | ||
getConstantGas: () => TGas, | ||
]; | ||
|
||
type TGetGas = (...args: TArgs) => Promise<TGas>; | ||
|
||
export const GAS_LIMIT = 5_000_000; | ||
|
||
export type TCreateGasConfig = { | ||
chainNonce: number; | ||
gasToAdd: number; | ||
constantGas: number; | ||
gasStationEndpoint: string; | ||
}; | ||
|
||
export const convertToGwei = (value: string): EthersBigNumber => { | ||
return ethers.parseUnits(value, "gwei"); | ||
}; | ||
|
||
export const getConstantGas = (fee: number): TGas => { | ||
const maxPriorityFeePerGas = convertToGwei(`${fee}`); | ||
const maxFeePerGas = convertToGwei(`${fee}`); | ||
return { | ||
maxPriorityFeePerGas, | ||
maxFeePerGas, | ||
gasLimit: GAS_LIMIT, | ||
}; | ||
}; | ||
|
||
export const getGas: TGetGas = async (getGasStationGas, getConstantGas) => { | ||
let retriesForGettingGas = 0; | ||
while (retriesForGettingGas < 5) { | ||
try { | ||
return await getGasStationGas(); | ||
} catch (error) { | ||
console.log("error when getting gas for", error); | ||
if (retriesForGettingGas === 4) { | ||
return getConstantGas(); | ||
} | ||
await sleep(5_000); | ||
retriesForGettingGas++; | ||
} | ||
} | ||
return getConstantGas(); | ||
}; | ||
|
||
export function sleep(ms: number) { | ||
return new Promise((resolve) => { | ||
setTimeout(resolve, ms); | ||
}); | ||
} |
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