Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: currency data type #4741

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ describe('RatesController', () => {
const publishActionSpy = jest.spyOn(messenger, 'publish');

jest.spyOn(global.Date, 'now').mockImplementation(() => getStubbedDate());
const mockRateValue = '57715.42';
const mockRateValue = 57715.42;
const fetchExchangeRateStub = jest.fn(() => {
return Promise.resolve({
btc: {
Expand All @@ -142,7 +142,7 @@ describe('RatesController', () => {
expect(ratesPreUpdate).toStrictEqual({
btc: {
conversionDate: 0,
conversionRate: '0',
conversionRate: 0,
},
});

Expand Down Expand Up @@ -177,12 +177,12 @@ describe('RatesController', () => {

it('starts the polling process with custom values', async () => {
jest.spyOn(global.Date, 'now').mockImplementation(() => getStubbedDate());
const mockBtcUsdRateValue = '62235.48';
const mockSolUsdRateValue = '148.41';
const mockStrkUsdRateValue = '1.248';
const mockBtcEurRateValue = '57715.42';
const mockSolEurRateValue = '137.68';
const mockStrkEurRateValue = '1.157';
const mockBtcUsdRateValue = 62235.48;
const mockSolUsdRateValue = 148.41;
const mockStrkUsdRateValue = 1.248;
const mockBtcEurRateValue = 57715.42;
const mockSolEurRateValue = 137.68;
const mockStrkEurRateValue = 1.157;
const fetchExchangeRateStub = jest.fn(() => {
return Promise.resolve({
btc: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const defaultState = {
rates: {
[Cryptocurrency.Btc]: {
conversionDate: 0,
conversionRate: '0',
conversionRate: 0,
},
},
cryptocurrencies: [Cryptocurrency.Btc],
Expand Down Expand Up @@ -119,7 +119,7 @@ export class RatesController extends BaseController<
const { fiatCurrency, cryptocurrencies } = this.state;
const response: Record<
Cryptocurrency,
Record<string, string>
Record<string, number>
> = await this.#fetchMultiExchangeRate(
fiatCurrency,
cryptocurrencies,
Expand Down
16 changes: 8 additions & 8 deletions packages/assets-controllers/src/RatesController/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ import type {

/**
* Represents the conversion rates from one currency to others, including the conversion date.
* The `conversionRate` field is a string that maps a cryptocurrency code (e.g., "BTC") to its
* conversion rate. For this field we use string as the data type to avoid potential rounding
* errors and precision loss.
* The `usdConversionRate` provides the conversion rate to USD as a string, or `null` if the
* conversion rate to USD is not available. We also use string for the same reason as stated before.
* The `conversionDate` is a Unix timestamp (number) indicating when the conversion rate was last updated.
* The `conversionRate` field is a `number` that maps a cryptocurrency code (e.g., "BTC") to its
* conversion rate. Precision lost is not relevant in this case since the value is only used for UI
* purposes, once we start to use it for operations, there should be the proper checks.
* The `usdConversionRate` provides the conversion rate to USD as a `number`, or `null` if the
* conversion rate to USD is not available.
* The `conversionDate` is a Unix timestamp (`number`) indicating when the conversion rate was last updated.
*/
export type Rate = {
conversionRate: string;
conversionRate: number;
conversionDate: number;
usdConversionRate?: string;
usdConversionRate?: number;
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export async function fetchMultiExchangeRate(
fiatCurrency: string,
cryptocurrencies: string[],
includeUSDRate: boolean,
): Promise<Record<string, Record<string, string>>> {
): Promise<Record<string, Record<string, number>>> {
const url = getMultiPricingURL(
Object.values(cryptocurrencies).join(','),
fiatCurrency,
Expand All @@ -148,10 +148,10 @@ export async function fetchMultiExchangeRate(
const response = await handleFetch(url);
handleErrorResponse(response);

const rates: Record<string, Record<string, string>> = {};
const rates: Record<string, Record<string, number>> = {};
for (const [cryptocurrency, values] of Object.entries(response) as [
string,
Record<string, string>,
Record<string, number>,
][]) {
rates[cryptocurrency.toLowerCase()] = {
[fiatCurrency.toLowerCase()]: values[fiatCurrency.toUpperCase()],
Expand Down
Loading