Jerrycan is an ethereum gas api service aggregator that allows you to have a single interface for multiple gas estimation api services.
Blocknative EthGasStation Polygon
Head to your target project and type in:
npm install jerrycan
Getting started is easy.
- You'll need to import the GasPriceService, setting up a new GasPriceService class. If you'd like to use a provider that requires an API (Blocknative or EthGasStation) you'll need to pass in a
blocknativeApiKey
and/orethGasStationApiKey
(no api is required when instantiating a new GasPriceService for Polygon):
// Blocknative
const blocknativeApiKey = 'example';
let gasPrice = new gasPriceService({ blocknativeApiKey });
or
// EthGasStation
const ethGasStationApiKey = 'example';
gasPrice = new gasPriceService({ ethGasStationApiKey });
or
// Both
gasPrice = new gasPriceService({ blocknativeApiKey, ethGasStationApiKey });
- Afterwards you will need to pass in two arguments to the
getLatest()
method. First pass in the Ethereum chain id as a string (e.g.'1'
for the Ethereum Mainnet) and then either'POLYGON'
,'ETHGASSTATION'
or'BLOCKNATIVE'
depending on which provider you want to fetch data from. This will then return the latest price in GWei.
❓ Note - If you mix and match chain id and provider incorrectly the service will return null
.
// Sample use with Blocknative
import { GasPriceService } from 'jerrycan';
const blocknativeApiKey = 'example';
const gasPrice = new GasPriceService({blocknativeApiKey});
const result = gasPrice.getLatest('1','BLOCKNATIVE');
console.log(result); // { price: 41000000000 }