add binance
This commit is contained in:
parent
ca24732a9b
commit
c30c7aed63
@ -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",
|
||||
|
23
src/config/binance.ts
Normal file
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
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 = {
|
||||
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,
|
||||
};
|
||||
|
22
src/index.ts
22
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<GasPrice> {
|
||||
|
12
src/types.ts
12
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<Options>;
|
||||
|
||||
export type NetworkConfig = {
|
||||
[key in number]: {
|
||||
offChainOracles: OffChainOracles;
|
||||
onChainOracles: OnChainOracles;
|
||||
};
|
||||
};
|
||||
|
@ -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 () {
|
||||
|
Loading…
Reference in New Issue
Block a user