npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@subwallet-connect/gas

v1.0.8

Published

Estimate the gas prices needed to get a transaction in to the next block for Ethereum Mainnet and Polygon Matic Mainnet.

Downloads

13

Readme

@subwallet-connect/gas

A module for requesting streams or single requests of gas price estimates from the Blocknative Gas Platform API.

Supports both Eth Mainnet and Polygon gas pricing.

Install

NPM npm i @subwallet-connect/gas

Yarn yarn add @subwallet-connect/gas

Standalone Setup

import gas from '@subwallet-connect/gas'

// subscribe to a single chain for estimates using the default poll rate of 5 secs
// API key is optional and if provided allows for faster poll rates
const ethMainnetGasBlockPrices = gas.stream({
  chains: ['0x1'],
  apiKey: '<OPTIONAL_API_KEY>',
  endpoint: 'blockPrices'
})

const { unsubscribe: ethGasUnsub } = ethMainnetGasBlockPrices.subscribe(
  estimates => console.log(estimates)
)

// .... sometime later, unsubscribe to stop polling
setTimeout(ethGasUnsub, 10000)

// OR you can subscribe to multiple chains at once:
const gasBlockPrices = gas.stream({
  chains: ['0x1', '0x89'],
  apiKey: '<OPTIONAL_API_KEY>',
  endpoint: 'blockPrices',
  // can override default poll rate as well
  poll: 1000
})

const { unsubscribe } = gasBlockPrices.subscribe(estimates =>
  console.log(estimates)
  console.log(estimates[0].blockPrices[0].estimatedPrice)
  // block inclusion confidence options: 70, 80, 90, 95, 99
  console.log(bnGasPrices.find(gas => gas.confidence === 90))
)

// .... sometime later, unsubscribe to stop polling
setTimeout(unsubscribe, 10000)

// Can also just do a one time get rather than a stream
const gasBlockPrices = await gas.get({
  chains: ['0x1', '0x89'],
  apiKey: '<OPTIONAL_API_KEY>',
  endpoint: 'blockPrices'
})

Usage with Web3-Onboard wallet Connect and Ethers.js

This example assumes you have already setup web3-onboard to connect wallets to your dapp. For more information see web3-onboard docs.

import gas from '@subwallet-connect/gas'
import { ethers } from 'ethers'

// Set provider using the Web3-Onboard wallet.provider instance from the connected wallet
let provider = new ethers.providers.Web3Provider(wallet.provider, 'any')
let bnGasPrices

const ethMainnetGasBlockPrices = gas.stream({
  chains: ['0x1'], // '0x89' can also be added/replaced here for Polygon gas data 
  apiKey: '<OPTIONAL_API_KEY>', // for faster refresh rates
  endpoint: 'blockPrices'
})

ethMainnetGasBlockPrices.subscribe(estimates => {
  console.log(estimates)
  bnGasPrices = estimates[0].blockPrices[0].estimatedPrices
})


const gweiToWeiHex = gwei => {
  return `0x${(gwei * 1e9).toString(16)}`
}

const sendTransaction = async () => {
  if (!toAddress) {
    alert('An Ethereum address to send Eth to is required.')
    return
  }

  const signer = provider.getUncheckedSigner()
  
  // define desired confidence for transaction inclusion in block and set in transaction
  // block inclusion confidence options: 70, 80, 90, 95, 99
  const bnGasForTransaction = bnGasPrices.find(gas => gas.confidence === 90)

  const rc = await signer.sendTransaction({
    to: toAddress,
    value: 1000000000000000

    // This will set the transaction gas based on desired confidence
    maxPriorityFeePerGas: gweiToWeiHex(
      bnGasForTransaction.maxPriorityFeePerGas
    ),
    maxFeePerGas: gweiToWeiHex(bnGasForTransaction.maxFeePerGas)
  })
  console.log(rc)
}