Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

txprice.com gas pricing for ethereum done correctly

NotificationsYou must be signed in to change notification settings

sambacha/txprice-web

Repository files navigation

Better Uptime Badge

asyncfunctionestimateGasPrice(provider:JsonRpcProvider):Promise<BigNumber|void>{try{constnetwork=awaitprovider.getNetwork()if(network.name==='homestead'){try{const response=awaitfetch('https://www.txprice.com/api')if(response.ok){const{ data}=awaitresponse.json()console.log(`gas price estimate for${network.name}:${utils.formatUnits(data.fast,'gwei')}`)returndata.fast}}catch(error){console.error(`txprice api call failed`,error)}}constblock=awaitprovider.getBlockWithTransactions('latest')constblock1=awaitprovider.getBlockWithTransactions(-1)constblock2=awaitprovider.getBlockWithTransactions(-2)consttransactions=[...block.transactions, ...block1.transactions, ...block2.transactions]constfilteredTxList=transactions.filter((tx)=>tx.gasPrice.gt(0))// filter out miner stuffconstgasPrices=filteredTxList.map((tx)=>tx.gasPrice)constgasSum=gasPrices.reduce((acc,cur)=>acc.add(cur),BigNumber.from(0))constdivisor=gasPrices.length||1constaverage=gasSum.div(divisor).mul(102).div(100)// 2% gas price buffer over average rateconsole.log(`gas price estimate for${network.name}:${utils.formatUnits(average,'gwei')}`)returnaverage||BigNumber.from(1)}catch(error){console.error(`failedgasestimation: ${error}`)returnBigNumber.from(1)}}

About

txprice.com gas pricing for ethereum done correctly

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp