add binance

This commit is contained in:
Danil Kovtonyuk 2021-06-02 19:48:50 +03:00 committed by Alexey Pertsev
parent ca24732a9b
commit c30c7aed63
7 changed files with 84 additions and 9 deletions

@ -1,6 +1,6 @@
{ {
"name": "gas-price-oracle", "name": "gas-price-oracle",
"version": "0.2.2", "version": "0.2.3",
"description": "Gas Price Oracle library for Ethereum dApps.", "description": "Gas Price Oracle library for Ethereum dApps.",
"main": "lib/index.js", "main": "lib/index.js",
"homepage": "https://github.com/peppersec/gas-price-oracle", "homepage": "https://github.com/peppersec/gas-price-oracle",

23
src/config/binance.ts Normal file

@ -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,
};

25
src/config/index.ts Normal file

@ -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,
},
};

@ -1,4 +1,4 @@
import { OffChainOracle, OnChainOracle } from './types'; import { OffChainOracle, OnChainOracle, OffChainOracles, OnChainOracles } from '../types';
const ethgasstation: OffChainOracle = { const ethgasstation: OffChainOracle = {
name: 'ethgasstation', name: 'ethgasstation',
@ -73,7 +73,7 @@ const chainlink: OnChainOracle = {
denominator: '1000000000', denominator: '1000000000',
}; };
export const offChainOracles: { [key: string]: OffChainOracle } = { export const offChainOracles: OffChainOracles = {
ethgasstation, ethgasstation,
anyblock, anyblock,
gasNow, gasNow,
@ -82,7 +82,7 @@ export const offChainOracles: { [key: string]: OffChainOracle } = {
zoltu, zoltu,
}; };
export const onChainOracles: { [key: string]: OnChainOracle } = { export const onChainOracles: OnChainOracles = {
chainlink, chainlink,
}; };

@ -1,13 +1,23 @@
import axios from 'axios'; import axios from 'axios';
import config from './config'; import { ChainId, networks } from './config';
import { GasPrice, OffChainOracle, OnChainOracle, Config, GasPriceKey, Options } from './types'; import {
Config,
Options,
GasPrice,
GasPriceKey,
OffChainOracle,
OnChainOracle,
OnChainOracles,
OffChainOracles,
} from './types';
import BigNumber from 'bignumber.js'; import BigNumber from 'bignumber.js';
export class GasPriceOracle { export class GasPriceOracle {
lastGasPrice: GasPrice; lastGasPrice: GasPrice;
offChainOracles = { ...config.offChainOracles }; offChainOracles: OffChainOracles;
onChainOracles = { ...config.onChainOracles }; onChainOracles: OnChainOracles;
configuration: Config = { configuration: Config = {
chainId: ChainId.MAINNET,
defaultRpc: 'https://api.mycryptoapi.com/eth', defaultRpc: 'https://api.mycryptoapi.com/eth',
timeout: 10000, timeout: 10000,
}; };
@ -16,6 +26,10 @@ export class GasPriceOracle {
if (options) { if (options) {
Object.assign(this.configuration, options); Object.assign(this.configuration, options);
} }
const { offChainOracles, onChainOracles } = networks[this.configuration.chainId];
this.offChainOracles = { ...offChainOracles };
this.onChainOracles = { ...onChainOracles };
} }
async askOracle(oracle: OffChainOracle): Promise<GasPrice> { async askOracle(oracle: OffChainOracle): Promise<GasPrice> {

@ -9,6 +9,8 @@ export type OffChainOracle = {
additionalDataProperty: string | null; additionalDataProperty: string | null;
}; };
export type OffChainOracles = { [key: string]: OffChainOracle };
export type OnChainOracle = { export type OnChainOracle = {
name: string; name: string;
rpc?: string; rpc?: string;
@ -17,6 +19,8 @@ export type OnChainOracle = {
denominator: string; denominator: string;
}; };
export type OnChainOracles = { [key: string]: OnChainOracle };
export type GasPrice = { export type GasPrice = {
[key in GasPriceKey]: number; [key in GasPriceKey]: number;
}; };
@ -24,8 +28,16 @@ export type GasPrice = {
export type GasPriceKey = 'instant' | 'fast' | 'standard' | 'low'; export type GasPriceKey = 'instant' | 'fast' | 'standard' | 'low';
export type Options = { export type Options = {
chainId?: number;
defaultRpc?: string; defaultRpc?: string;
timeout?: number; timeout?: number;
}; };
export type Config = Required<Options>; export type Config = Required<Options>;
export type NetworkConfig = {
[key in number]: {
offChainOracles: OffChainOracles;
onChainOracles: OnChainOracles;
};
};

@ -3,12 +3,12 @@
import { GasPrice, OffChainOracle } from '../src/types'; import { GasPrice, OffChainOracle } from '../src/types';
import mockery from 'mockery'; import mockery from 'mockery';
import chai from 'chai'; import chai from 'chai';
import { onChainOracles, offChainOracles } from '../src/config';
import { GasPriceOracle } from '../src/index'; import { GasPriceOracle } from '../src/index';
chai.use(require('chai-as-promised')); chai.use(require('chai-as-promised'));
chai.should(); chai.should();
let oracle = new GasPriceOracle(); let oracle = new GasPriceOracle();
let { onChainOracles, offChainOracles } = oracle;
before('before', function () { before('before', function () {
const axiosMock = { const axiosMock = {
@ -24,6 +24,7 @@ before('before', function () {
beforeEach('beforeEach', function () { beforeEach('beforeEach', function () {
oracle = new GasPriceOracle(); oracle = new GasPriceOracle();
({ onChainOracles, offChainOracles } = oracle);
}); });
describe('constructor', function () { describe('constructor', function () {