Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sambacha/txprice-web
txprice.com gas pricing for ethereum done correctly
https://github.com/sambacha/txprice-web
eth ethereum gas gwei solidity
Last synced: 9 days ago
JSON representation
txprice.com gas pricing for ethereum done correctly
- Host: GitHub
- URL: https://github.com/sambacha/txprice-web
- Owner: sambacha
- Created: 2021-07-10T23:29:55.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2021-08-08T17:53:33.000Z (about 3 years ago)
- Last Synced: 2024-10-12T09:05:14.054Z (24 days ago)
- Topics: eth, ethereum, gas, gwei, solidity
- Language: Vue
- Homepage:
- Size: 1.57 MB
- Stars: 0
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# txprice-web
[![Better Uptime Badge](https://betteruptime.com/status-badges/v1/monitor/898s.svg)](https://betteruptime.com/?utm_source=status_badge)
```js
async function estimateGasPrice(provider: JsonRpcProvider): Promise {
try {
const network = await provider.getNetwork()
if (network.name === 'homestead') {
try {
const response = await fetch('https://www.txprice.com/api')
if (response.ok) {
const { data } = await response.json()
console.log(`gas price estimate for ${network.name}: ${utils.formatUnits(data.fast, 'gwei')}`)
return data.fast
}
} catch (error) {
console.error(`txprice api call failed`, error)
}
}
const block = await provider.getBlockWithTransactions('latest')
const block1 = await provider.getBlockWithTransactions(-1)
const block2 = await provider.getBlockWithTransactions(-2)
const transactions = [...block.transactions, ...block1.transactions, ...block2.transactions]
const filteredTxList = transactions.filter((tx) => tx.gasPrice.gt(0)) // filter out miner stuff
const gasPrices = filteredTxList.map((tx) => tx.gasPrice)
const gasSum = gasPrices.reduce((acc, cur) => acc.add(cur), BigNumber.from(0))
const divisor = gasPrices.length || 1
const average = gasSum.div(divisor).mul(102).div(100) // 2% gas price buffer over average rate
console.log(`gas price estimate for ${network.name}: ${utils.formatUnits(average, 'gwei')}`)
return average || BigNumber.from(1)
} catch (error) {
console.error(`failed gas estimation: ${error}`)
return BigNumber.from(1)
}
}