diff --git a/package.json b/package.json index 2b44550..05181be 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gas-price-oracle", - "version": "0.2.2", + "version": "0.2.3", "description": "Gas Price Oracle library for Ethereum dApps.", "main": "lib/index.js", "homepage": "https://github.com/peppersec/gas-price-oracle", diff --git a/src/config/binance.ts b/src/config/binance.ts new file mode 100644 index 0000000..7ea50ad --- /dev/null +++ b/src/config/binance.ts @@ -0,0 +1,23 @@ +import { OffChainOracle, OffChainOracles, OnChainOracles } from '../types'; + +const bscgas: OffChainOracle = { + name: 'bscgas', + url: 'https://bscgas.info/gas', + instantPropertyName: 'imediate', + fastPropertyName: 'fast', + standardPropertyName: 'standard', + lowPropertyName: 'slow', + denominator: 1, + additionalDataProperty: null, +}; + +export const offChainOracles: OffChainOracles = { + bscgas, +}; + +export const onChainOracles: OnChainOracles = {}; + +export default { + offChainOracles, + onChainOracles, +}; diff --git a/src/config/index.ts b/src/config/index.ts new file mode 100644 index 0000000..1163c87 --- /dev/null +++ b/src/config/index.ts @@ -0,0 +1,25 @@ +import { NetworkConfig } from '../types'; +import { + onChainOracles as mainnetOnchainOracles, + offChainOracles as mainnetOffChainOracles, +} from './mainnet'; +import { + onChainOracles as binanceOnchainOracles, + offChainOracles as binanceOffchainOracles, +} from './binance'; + +export enum ChainId { + MAINNET = 1, + BINANCE = 56, +} + +export const networks: NetworkConfig = { + [ChainId.MAINNET]: { + onChainOracles: mainnetOnchainOracles, + offChainOracles: mainnetOffChainOracles, + }, + [ChainId.BINANCE]: { + onChainOracles: binanceOnchainOracles, + offChainOracles: binanceOffchainOracles, + }, +}; diff --git a/src/config.ts b/src/config/mainnet.ts similarity index 91% rename from src/config.ts rename to src/config/mainnet.ts index 6b8c6f7..f13c031 100644 --- a/src/config.ts +++ b/src/config/mainnet.ts @@ -1,4 +1,4 @@ -import { OffChainOracle, OnChainOracle } from './types'; +import { OffChainOracle, OnChainOracle, OffChainOracles, OnChainOracles } from '../types'; const ethgasstation: OffChainOracle = { name: 'ethgasstation', @@ -73,7 +73,7 @@ const chainlink: OnChainOracle = { denominator: '1000000000', }; -export const offChainOracles: { [key: string]: OffChainOracle } = { +export const offChainOracles: OffChainOracles = { ethgasstation, anyblock, gasNow, @@ -82,7 +82,7 @@ export const offChainOracles: { [key: string]: OffChainOracle } = { zoltu, }; -export const onChainOracles: { [key: string]: OnChainOracle } = { +export const onChainOracles: OnChainOracles = { chainlink, }; diff --git a/src/index.ts b/src/index.ts index b6b4d41..8c69bac 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,13 +1,23 @@ import axios from 'axios'; -import config from './config'; -import { GasPrice, OffChainOracle, OnChainOracle, Config, GasPriceKey, Options } from './types'; +import { ChainId, networks } from './config'; +import { + Config, + Options, + GasPrice, + GasPriceKey, + OffChainOracle, + OnChainOracle, + OnChainOracles, + OffChainOracles, +} from './types'; import BigNumber from 'bignumber.js'; export class GasPriceOracle { lastGasPrice: GasPrice; - offChainOracles = { ...config.offChainOracles }; - onChainOracles = { ...config.onChainOracles }; + offChainOracles: OffChainOracles; + onChainOracles: OnChainOracles; configuration: Config = { + chainId: ChainId.MAINNET, defaultRpc: 'https://api.mycryptoapi.com/eth', timeout: 10000, }; @@ -16,6 +26,10 @@ export class GasPriceOracle { if (options) { Object.assign(this.configuration, options); } + + const { offChainOracles, onChainOracles } = networks[this.configuration.chainId]; + this.offChainOracles = { ...offChainOracles }; + this.onChainOracles = { ...onChainOracles }; } async askOracle(oracle: OffChainOracle): Promise { diff --git a/src/types.ts b/src/types.ts index b3442f3..83ea57a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -9,6 +9,8 @@ export type OffChainOracle = { additionalDataProperty: string | null; }; +export type OffChainOracles = { [key: string]: OffChainOracle }; + export type OnChainOracle = { name: string; rpc?: string; @@ -17,6 +19,8 @@ export type OnChainOracle = { denominator: string; }; +export type OnChainOracles = { [key: string]: OnChainOracle }; + export type GasPrice = { [key in GasPriceKey]: number; }; @@ -24,8 +28,16 @@ export type GasPrice = { export type GasPriceKey = 'instant' | 'fast' | 'standard' | 'low'; export type Options = { + chainId?: number; defaultRpc?: string; timeout?: number; }; export type Config = Required; + +export type NetworkConfig = { + [key in number]: { + offChainOracles: OffChainOracles; + onChainOracles: OnChainOracles; + }; +}; diff --git a/tests/index.test.ts b/tests/index.test.ts index 9b68fa6..567217a 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -3,12 +3,12 @@ import { GasPrice, OffChainOracle } from '../src/types'; import mockery from 'mockery'; import chai from 'chai'; -import { onChainOracles, offChainOracles } from '../src/config'; import { GasPriceOracle } from '../src/index'; chai.use(require('chai-as-promised')); chai.should(); let oracle = new GasPriceOracle(); +let { onChainOracles, offChainOracles } = oracle; before('before', function () { const axiosMock = { @@ -24,6 +24,7 @@ before('before', function () { beforeEach('beforeEach', function () { oracle = new GasPriceOracle(); + ({ onChainOracles, offChainOracles } = oracle); }); describe('constructor', function () {