Skip to content
This repository has been archived by the owner on Sep 28, 2022. It is now read-only.

Commit

Permalink
SDK-746 add apiKey setting to GasService
Browse files Browse the repository at this point in the history
  • Loading branch information
levity committed Feb 10, 2020
1 parent a376194 commit 5aa6bcc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
9 changes: 6 additions & 3 deletions packages/dai/src/eth/GasService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand All @@ -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();
Expand Down Expand Up @@ -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');
Expand Down
24 changes: 24 additions & 0 deletions packages/dai/test/eth/GasService2.spec.js
Original file line number Diff line number Diff line change
@@ -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');
});

0 comments on commit 5aa6bcc

Please sign in to comment.