Skip to content

Commit

Permalink
Fix(Gas estimation): gas price from oracles can be a float
Browse files Browse the repository at this point in the history
  • Loading branch information
katspaugh committed Jan 22, 2025
1 parent 0935bb7 commit 60b4b4f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
16 changes: 8 additions & 8 deletions apps/web/src/hooks/__tests__/useGasPrice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ describe('useGasPrice', () => {
json: () =>
Promise.resolve({
data: {
FastGasPrice: '47',
suggestBaseFee: '44',
FastGasPrice: '47.543',
suggestBaseFee: '44.435',
},
}),
}),
Expand Down Expand Up @@ -104,8 +104,8 @@ describe('useGasPrice', () => {
json: () =>
Promise.resolve({
data: {
FastGasPrice: '30',
suggestBaseFee: '10',
FastGasPrice: '30.45435',
suggestBaseFee: '10.5435435',
},
}),
}),
Expand Down Expand Up @@ -270,8 +270,8 @@ describe('useGasPrice', () => {
json: () =>
Promise.resolve({
data: {
FastGasPrice: '21',
suggestBaseFee: '19',
FastGasPrice: '21.5435435',
suggestBaseFee: '19.5435',
},
}),
}),
Expand All @@ -282,8 +282,8 @@ describe('useGasPrice', () => {
json: () =>
Promise.resolve({
data: {
FastGasPrice: '22',
suggestBaseFee: '19',
FastGasPrice: '22.89879',
suggestBaseFee: '19.1243',
},
}),
}),
Expand Down
8 changes: 6 additions & 2 deletions apps/web/src/hooks/useGasPrice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ const isEtherscanResult = (data: any): data is EtherscanResult => {
return 'FastGasPrice' in data && 'suggestBaseFee' in data
}

const stringToBigInt = (value: string): bigint => {
return BigInt(value.split('.')[0])
}

/**
* Parses result from etherscan oracle.
* Since EIP 1559 it returns the `maxFeePerGas` as gas price and the current network baseFee as `suggestedBaseFee`.
Expand All @@ -55,8 +59,8 @@ const isEtherscanResult = (data: any): data is EtherscanResult => {
* @see https://docs.etherscan.io/api-endpoints/gas-tracker
*/
const parseEtherscanOracleResult = (result: EtherscanResult, gweiFactor: string): EstimatedGasPrice => {
const maxFeePerGas = BigInt(Number(result.FastGasPrice) * Number(gweiFactor))
const baseFee = BigInt(Number(result.suggestBaseFee) * Number(gweiFactor))
const maxFeePerGas = stringToBigInt(result.FastGasPrice) * stringToBigInt(gweiFactor)
const baseFee = stringToBigInt(result.suggestBaseFee) * stringToBigInt(gweiFactor)

return {
maxFeePerGas,
Expand Down

0 comments on commit 60b4b4f

Please sign in to comment.