Skip to content

Commit

Permalink
refactor(getcurrencyrate): switch btc-rate directly to sats-rate #1381
Browse files Browse the repository at this point in the history
  • Loading branch information
escapedcat committed Sep 22, 2022
1 parent 0b91796 commit bd25d6f
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 25 deletions.
1 change: 0 additions & 1 deletion src/app/components/AllowanceMenu/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ jest.mock("~/common/lib/api", () => {
currency: "USD",
exchange: "coindesk",
})),
getCurrencyRate: jest.fn(() => ({ rate: 29991.836 })),
};
});

Expand Down
1 change: 0 additions & 1 deletion src/app/screens/LNURLWithdraw/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ jest.mock("~/common/lib/api", () => ({
exchange: "coindesk",
})),
makeInvoice: jest.fn(() => ({})),
getCurrencyRate: jest.fn(() => ({ rate: 29991.836 })),
}));

describe("LNURLWithdraw", () => {
Expand Down
1 change: 0 additions & 1 deletion src/app/screens/Receive/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ jest.mock("~/common/lib/api", () => {
exchange: "coindesk",
})),
makeInvoice: jest.fn(),
getCurrencyRate: jest.fn(() => ({ rate: 29991.836 })),
};
});

Expand Down
2 changes: 1 addition & 1 deletion src/common/utils/__tests__/currencyConvert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe("Currency coversion utils", () => {
test("getFiatValue", async () => {
const result = await getFiatValue({
amount: 123456789,
rate: 29991.836,
rate: 0.00029991836,
currency: CURRENCIES["USD"],
});

Expand Down
19 changes: 6 additions & 13 deletions src/common/utils/currencyConvert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,18 @@
*/
import type { CURRENCIES } from "../constants";

const numSatsInBtc = 100_000_000;

const bitcoinToFiat = async (amountInBtc: number | string, rate: number) => {
return Number(amountInBtc) * Number(rate);
};

const satoshisToBitcoin = (amountInSatoshis: number | string) => {
return Number(amountInSatoshis) / numSatsInBtc;
const satsToFiat = async (amount: number | string, rate: number) => {
return Number(amount) * rate;
};

const satoshisToFiat = async ({
amountInSats,
amount,
rate,
}: {
amountInSats: number | string;
amount: number | string;
rate: number;
}) => {
const btc = satoshisToBitcoin(amountInSats);
const fiat = await bitcoinToFiat(btc, rate);
const fiat = await satsToFiat(amount, rate);
return fiat;
};

Expand All @@ -35,7 +28,7 @@ export const getFiatValue = async ({
currency: CURRENCIES;
}) => {
const fiatValue = await satoshisToFiat({
amountInSats: amount,
amount,
rate,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe("currencyRate", () => {
test("storing rate for the first time", async () => {
(chrome.storage.local.get as jest.Mock).mockResolvedValue({});
const result = await getCurrencyRate(message);
expect(result.data.rate).toBe(29991.836);
expect(result.data.rate).toBe(0.00029991836);
});

test("storing rate if it is outdated", async () => {
Expand All @@ -46,9 +46,9 @@ describe("currencyRate", () => {
const result = await getCurrencyRate(message);
expect(chrome.storage.local.set).toHaveBeenCalledWith({
currencyRate:
'{"currency":"USD","rate":29991.836,"timestamp":1577836800000}',
'{"currency":"USD","rate":0.00029991836,"timestamp":1577836800000}',
});
expect(result.data.rate).toBe(29991.836);
expect(result.data.rate).toBe(0.00029991836);
});

test("returning rate if still valid", async () => {
Expand All @@ -75,7 +75,7 @@ describe("currencyRate", () => {
await getCurrencyRate(message);
expect(chrome.storage.local.set).toHaveBeenCalledWith({
currencyRate:
'{"currency":"USD","rate":29991.836,"timestamp":1577836800000}',
'{"currency":"USD","rate":0.00029991836,"timestamp":1577836800000}',
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ interface CurrencyRate {
timestamp?: number;
}

export const numSatsInBtc = 100_000_000;

const storeCurrencyRate = async ({ rate, currency }: CurrencyRate) => {
const currencyRate: CurrencyRate = {
currency,
Expand All @@ -37,22 +39,23 @@ const getFiatBtcRate = async (currency: CURRENCIES): Promise<number> => {
`https://api.yadio.io/exrates/${currency.toLowerCase()}`
);
const data = await response?.data;
return data.BTC;
return data.BTC / numSatsInBtc;
}

if (exchange === "coindesk") {
response = await axios.get(
`https://api.coindesk.com/v1/bpi/currentprice/${currency.toLowerCase()}.json`
);
const data = await response?.data;
return data.bpi[currency].rate_float;
return data.bpi[currency].rate_float / numSatsInBtc;
}

response = await axios.get(
`https://getalby.com/api/rates/${currency.toLowerCase()}.json`
);
const data = await response?.data;
return data[currency].rate_float;

return data[currency].rate_float / numSatsInBtc;
};

export const getCurrencyRateFromCache = async (currency: CURRENCIES) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {

jest.mock("~/extension/background-script/actions/cache/getCurrencyRate", () => {
return {
getCurrencyRateFromCache: jest.fn(() => Promise.resolve(888.836)),
getCurrencyRateFromCache: jest.fn(() => Promise.resolve(0.00019233)),
};
});

Expand Down

0 comments on commit bd25d6f

Please sign in to comment.