diff --git a/packages/dai/src/eth/GasService.js b/packages/dai/src/eth/GasService.js index c0ebb89db..48f6af448 100644 --- a/packages/dai/src/eth/GasService.js +++ b/packages/dai/src/eth/GasService.js @@ -2,6 +2,9 @@ import { PublicService } from '@makerdao/services-core'; import map from 'lodash/map'; import fetch from 'isomorphic-fetch'; +export const API_URL = + 'https://ethgasstation.info/json/ethgasAPI.json?api-key='; + export default class GasService extends PublicService { constructor(name = 'gas') { super(name, ['web3', 'log']); @@ -16,6 +19,8 @@ export default class GasService extends PublicService { this._parseConfig(settings.price, 'price'); } + this._settings = settings; + this._gasStationDataPromise = this.disablePrice ? Promise.resolve({}) : this.fetchGasStationData(); @@ -43,9 +48,7 @@ export default class GasService extends PublicService { async fetchGasStationData() { try { - const response = await fetch( - 'https://ethgasstation.info/json/ethgasAPI.json' - ); + const response = await fetch(API_URL + this._settings.apiKey); return response.json(); } catch (err) { console.error('Error fetching gas data; disabling preset gas price'); diff --git a/packages/dai/test/eth/GasService2.spec.js b/packages/dai/test/eth/GasService2.spec.js new file mode 100644 index 000000000..516be3dd1 --- /dev/null +++ b/packages/dai/test/eth/GasService2.spec.js @@ -0,0 +1,24 @@ +// this is split out from the other GasService tests in order to mock fetching + +import { buildTestService } from '../helpers/serviceBuilders'; +import fetch from 'isomorphic-fetch'; +import { API_URL } from '../../src/eth/GasService'; + +jest.mock('isomorphic-fetch', () => jest.fn()); + +test('apiKey setting', async () => { + fetch.mockReturnValue( + new Promise(res => + res({ + json: () => ({ mock: true }) + }) + ) + ); + const service = buildTestService('gas', { + gas: { + apiKey: 'foo' + } + }); + await service.manager().authenticate(); + expect(fetch).toBeCalledWith(API_URL + 'foo'); +});