Skip to content

Commit

Permalink
adding comments, tweaking function signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
adonesky1 committed May 18, 2021
1 parent badae43 commit 10c5b47
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 46 deletions.
24 changes: 0 additions & 24 deletions src/apis/crypto-compare.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,6 @@ describe('CryptoCompare', () => {
expect(conversionRate).toStrictEqual(2000.42);
});

it('should return conversion date', async () => {
nock(cryptoCompareHost)
.get('/data/price?fsym=ETH&tsyms=CAD')
.reply(200, { CAD: 2000.42 });

const before = Date.now() / 1000;
const { conversionDate } = await fetchExchangeRate('CAD', 'ETH');
const after = Date.now() / 1000;

expect(conversionDate).toBeGreaterThanOrEqual(before);
expect(conversionDate).toBeLessThanOrEqual(after);
});

it('should return CAD conversion rate given lower-cased currency', async () => {
nock(cryptoCompareHost)
.get('/data/price?fsym=ETH&tsyms=CAD')
Expand Down Expand Up @@ -153,17 +140,6 @@ describe('CryptoCompare', () => {
);
});

it('should return null if either currency inputs are empty', async () => {
const { conversionRate, usdConversionRate } = await fetchExchangeRate(
'',
'ETH',
true,
);

expect(conversionRate).toBeNull();
expect(usdConversionRate).toBeNull();
});

it('should throw an error if either currency is invalid', async () => {
nock(cryptoCompareHost)
.get('/data/price?fsym=ETH&tsyms=EUABRT')
Expand Down
22 changes: 10 additions & 12 deletions src/apis/crypto-compare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,21 @@ export async function fetchExchangeRate(
nativeCurrency: string,
includeUSDRate?: boolean,
): Promise<{
conversionDate: number;
conversionRate: number | null;
usdConversionRate: number | null;
conversionRate: number;
usdConversionRate: number;
}> {
if (!currency || !nativeCurrency) {
return {
conversionDate: Date.now() / 1000,
conversionRate: null,
usdConversionRate: null,
};
}

const json = await handleFetch(
getPricingURL(currency, nativeCurrency, includeUSDRate),
);

/*
Example expected error response (if pair is not found)
{
Response: "Error",
Message: "cccagg_or_exchange market does not exist for this coin pair (ETH-<NON_EXISTENT_TOKEN>)",
HasWarning: false,
}
*/
if (json.Response === 'Error') {
throw new Error(json.Message);
}
Expand All @@ -60,7 +59,6 @@ export async function fetchExchangeRate(
}

return {
conversionDate: Date.now() / 1000,
conversionRate,
usdConversionRate,
};
Expand Down
31 changes: 31 additions & 0 deletions src/assets/CurrencyRateController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,4 +288,35 @@ describe('CurrencyRateController', () => {
expect(controller.state.conversionRate).toBeNull();
controller.destroy();
});

it('should update conversionRates in state to null if either currentCurrency or nativeCurrency is null', async () => {
const realDate = Date.now
global.Date.now = jest.fn(() => new Date('2019-04-07T10:20:30Z').getTime())

const cryptoCompareHost = 'https://min-api.cryptocompare.com';
nock(cryptoCompareHost)
.get('/data/price?fsym=ETH&tsyms=XYZ')
.reply(200, { XYZ: 2000.42 })
.persist();

const messenger = getRestrictedMessenger();
const existingState = { currentCurrency: '', nativeCurrency: 'BNB' };
const controller = new CurrencyRateController({
messenger,
state: existingState,
});

await controller.updateExchangeRate();

expect(controller.state).toEqual({
conversionDate: new Date('2019-04-07T10:20:30Z').getTime() / 1000,
conversionRate: null,
currentCurrency: '',
nativeCurrency: 'BNB',
pendingCurrentCurrency: null,
pendingNativeCurrency: null,
usdConversionRate: null,
});
global.Date.now = realDate;
})
});
21 changes: 11 additions & 10 deletions src/assets/CurrencyRateController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,19 +196,20 @@ export class CurrencyRateController extends BaseController<
pendingNativeCurrency,
} = this.state;

let conversionDate: number = Date.now() / 1000;
const conversionDate: number = Date.now() / 1000;
let conversionRate: number | null = null;
let usdConversionRate: number | null = null;
try {
({
conversionDate,
conversionRate,
usdConversionRate,
} = await this.fetchExchangeRate(
pendingCurrentCurrency || currentCurrency,
pendingNativeCurrency || nativeCurrency,
this.includeUsdRate,
));
if (
(pendingCurrentCurrency || currentCurrency) &&
(pendingNativeCurrency || nativeCurrency)
) {
({ conversionRate, usdConversionRate } = await this.fetchExchangeRate(
pendingCurrentCurrency || currentCurrency,
pendingNativeCurrency || nativeCurrency,
this.includeUsdRate,
));
}
} catch (error) {
if (!error.message.includes('market does not exist for this coin pair')) {
throw error;
Expand Down

0 comments on commit 10c5b47

Please sign in to comment.