gas-price-oracle/README.md

111 lines
2.6 KiB
Markdown
Raw Normal View History

2020-10-16 14:10:25 +03:00
# Gas Price Oracle library for Ethereum dApps [![GitHub Workflow Status](https://img.shields.io/github/workflow/status/peppersec/gas-price-oracle/Node.js%20CI)](https://github.com/peppersec/gas-price-oracle/actions) [![npm](https://img.shields.io/npm/v/gas-price-oracle)](https://www.npmjs.com/package/gas-price-oracle)
2020-06-04 02:08:09 +03:00
A library that has a collection of onchain and offchain gas price oracle URLs
2020-05-27 18:52:57 +03:00
2021-06-03 13:01:25 +03:00
## Supported networks
### Ethereum Mainnet
2020-10-16 14:10:25 +03:00
Current offchain list:
- https://ethgasstation.info/json/ethgasAPI.json
2021-11-15 09:17:34 +03:00
- https://etherchain.org/api/gasnow
- https://blockscout.com/eth/mainnet/api/v1/gas-price-oracle
2020-10-16 14:10:25 +03:00
Current onchain list:
2020-06-04 02:08:09 +03:00
2020-10-19 16:08:19 +03:00
- [chainlink](https://etherscan.io/address/0x169e633a2d1e6c10dd91238ba11c4a708dfef37c#readContract)
2020-06-04 02:08:09 +03:00
2021-06-03 13:01:25 +03:00
### Binance Smart Chain
Current offchain list:
2021-11-15 09:17:34 +03:00
- https://ztake.org/
2021-06-03 13:01:25 +03:00
2021-08-25 15:31:20 +03:00
### xDAI Chain
Current offchain list:
2021-11-15 09:17:34 +03:00
- https://blockscout.com/xdai/mainnet/api/v1/gas-price-oracle
2021-08-25 15:31:20 +03:00
2021-06-15 13:26:02 +03:00
### Polygon (Matic) Network
Current offchain list:
- https://gasstation-mainnet.matic.network/
2020-06-04 02:08:09 +03:00
## Installation
2020-10-16 14:10:25 +03:00
2020-05-27 18:52:57 +03:00
`npm i gas-price-oracle`
2020-06-02 16:29:12 +03:00
## Import
2020-10-16 14:10:25 +03:00
2020-05-27 18:52:57 +03:00
```js
const { GasPriceOracle } = require('gas-price-oracle');
2020-06-02 16:29:12 +03:00
```
2020-10-16 14:10:25 +03:00
2020-06-02 16:29:12 +03:00
## Usage
2020-10-16 14:10:25 +03:00
2020-06-02 16:29:12 +03:00
### Basic
2020-06-04 02:08:09 +03:00
2020-06-02 16:29:12 +03:00
```js
2020-06-04 02:08:09 +03:00
const options = {
2021-06-03 13:01:25 +03:00
chainId: 1,
2020-10-16 14:10:25 +03:00
defaultRpc: 'https://api.mycryptoapi.com/eth',
2020-10-19 15:15:25 +03:00
timeout: 10000,
2021-06-03 15:16:49 +03:00
defaultFallbackGasPrices: {
instant: 28,
fast: 22,
standard: 17,
low: 11,
},
2020-10-16 14:10:25 +03:00
};
2020-06-04 02:08:09 +03:00
const oracle = new GasPriceOracle(options);
// optional fallbackGasPrices
const fallbackGasPrices = {
2020-10-16 14:10:25 +03:00
instant: 70,
fast: 31,
standard: 20,
low: 7,
};
oracle.gasPrices(fallbackGasPrices).then(gasPrices => {
console.log(gasPrices); // { instant: 50, fast: 21, standard: 10, low: 3 }
2020-06-02 16:29:12 +03:00
});
```
2020-10-19 15:16:34 +03:00
The `gasPrices` method also accepts `median` argument (`true`) by default. For more details see [below](#offchain-oracles-only-get-median-price).
2020-10-19 15:15:25 +03:00
Under the hood it's a combination of `fetchMedianGasPriceOffChain`(`fetchGasPricesOffChain`) and `fetchGasPricesOnChain` methods.
2020-06-02 16:29:12 +03:00
### Offchain oracles only
2020-10-16 14:10:25 +03:00
2020-06-02 16:29:12 +03:00
```js
const oracle = new GasPriceOracle();
2020-10-16 14:10:25 +03:00
oracle.fetchGasPricesOffChain().then(gasPrices => {
console.log(gasPrices); // { instant: 50, fast: 21, standard: 10, low: 3 }
2020-06-02 16:29:12 +03:00
});
```
2020-10-19 15:15:25 +03:00
### Offchain oracles only (get median price)
```js
const oracle = new GasPriceOracle();
oracle.fetchMedianGasPriceOffChain().then(gasPrices => {
console.log(gasPrices); // { instant: 50, fast: 21, standard: 10, low: 3 }
});
```
it returns the median gas price of all the oracles configured.
2020-06-02 16:29:12 +03:00
### Custom RPC URL for onchain oracles
2020-10-16 14:10:25 +03:00
2020-06-02 16:29:12 +03:00
```js
2020-10-16 14:10:25 +03:00
const defaultRpc = 'https://mainnet.infura.io/v3/<API_KEY>';
2020-06-04 02:08:09 +03:00
const oracle = new GasPriceOracle({ defaultRpc });
2020-06-02 16:29:12 +03:00
2020-10-16 14:10:25 +03:00
oracle.fetchGasPricesOnChain().then(gasPrices => {
console.log(gasPrices); // 21
2020-05-27 18:52:57 +03:00
});
```