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

refactor: remove redundent code #1643

Merged
merged 4 commits into from
Oct 20, 2022
Merged
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
2 changes: 1 addition & 1 deletion src/app/components/AllowanceMenu/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as SettingsContext from "~/app/context/SettingsContext";
import type { Props } from "./index";
import AllowanceMenu from "./index";

const mockGetFiatValue = jest.fn(() => Promise.resolve("$1,22"));
const mockGetFiatValue = jest.fn(() => "$1,22");

jest.spyOn(SettingsContext, "useSettings").mockReturnValue({
settings: mockSettings,
Expand Down
8 changes: 2 additions & 6 deletions src/app/components/AllowanceMenu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,8 @@ function AllowanceMenu({ allowance, onEdit, onDelete }: Props) {

useEffect(() => {
if (budget !== "" && showFiat) {
const getFiat = async () => {
const res = await getFiatValue(budget);
setFiatAmount(res);
};

getFiat();
const res = getFiatValue(budget);
setFiatAmount(res);
}
}, [budget, showFiat, getFiatValue]);

Expand Down
4 changes: 2 additions & 2 deletions src/app/context/AccountContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ export function AccountProvider({ children }: { children: React.ReactNode }) {
const setAccountId = (id: string) => setAccount({ id });

const updateFiatValue = useCallback(
async (balance: string | number) => {
const fiat = await getFiatValue(balance);
(balance: string | number) => {
const fiat = getFiatValue(balance);
setFiatBalance(fiat);
},
[getFiatValue]
Expand Down
6 changes: 3 additions & 3 deletions src/app/context/SettingsContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface SettingsContextType {
settings: SettingsStorage;
updateSetting: (setting: Setting) => void;
isLoading: boolean;
getFiatValue: (amount: number | string) => Promise<string>;
getFiatValue: (amount: number | string) => string;
}

type Setting = Partial<SettingsStorage>;
Expand Down Expand Up @@ -62,8 +62,8 @@ export const SettingsProvider = ({
});
}, [settings.currency]);

const getFiatValue = async (amount: number | string) =>
await getFiatValueFunc({
const getFiatValue = (amount: number | string) =>
getFiatValueFunc({
amount,
rate: currencyRate,
currency: settings.currency,
Expand Down
8 changes: 4 additions & 4 deletions src/app/screens/ConfirmKeysend/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ jest.spyOn(SettingsContext, "useSettings").mockReturnValue({
updateSetting: jest.fn(),
getFiatValue: jest
.fn()
.mockImplementationOnce(() => Promise.resolve("$0.00"))
.mockImplementationOnce(() => Promise.resolve("$0.00"))
.mockImplementationOnce(() => Promise.resolve("$0.01"))
.mockImplementationOnce(() => Promise.resolve("$0.05")),
.mockImplementationOnce(() => "$0.00")
.mockImplementationOnce(() => "$0.00")
.mockImplementationOnce(() => "$0.01")
.mockImplementationOnce(() => "$0.05"),
});

const mockOrigin: OriginData = {
Expand Down
16 changes: 6 additions & 10 deletions src/app/screens/ConfirmKeysend/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,16 @@ function ConfirmKeysend() {
const [successMessage, setSuccessMessage] = useState("");

useEffect(() => {
(async () => {
if (showFiat && amount) {
const res = await getFiatValue(amount);
setFiatAmount(res);
}
})();
if (showFiat && amount) {
const res = getFiatValue(amount);
setFiatAmount(res);
}
}, [amount, showFiat, getFiatValue]);

useEffect(() => {
if (showFiat) {
(async () => {
const res = await getFiatValue(budget);
setFiatBudgetAmount(res);
})();
const res = getFiatValue(budget);
setFiatBudgetAmount(res);
}
}, [budget, showFiat, getFiatValue]);

Expand Down
6 changes: 3 additions & 3 deletions src/app/screens/ConfirmPayment/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe("ConfirmPayment", () => {
settings: { ...mockSettings },
isLoading: false,
updateSetting: jest.fn(),
getFiatValue: jest.fn(() => Promise.resolve("$0.01")),
getFiatValue: jest.fn(() => "$0.01"),
});

await act(async () => {
Expand Down Expand Up @@ -86,7 +86,7 @@ describe("ConfirmPayment", () => {
settings: { ...mockSettings, showFiat: false },
isLoading: false,
updateSetting: jest.fn(),
getFiatValue: jest.fn(() => Promise.resolve("$0.01")),
getFiatValue: jest.fn(() => "$0.01"),
});

const user = userEvent.setup();
Expand Down Expand Up @@ -121,7 +121,7 @@ describe("ConfirmPayment", () => {
settings: { ...mockSettings },
isLoading: false,
updateSetting: jest.fn(),
getFiatValue: jest.fn(() => Promise.resolve("$0.01")),
getFiatValue: jest.fn(() => "$0.01"),
});

await act(async () => {
Expand Down
20 changes: 8 additions & 12 deletions src/app/screens/ConfirmPayment/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,17 @@ function ConfirmPayment() {
const [fiatBudgetAmount, setFiatBudgetAmount] = useState("");

useEffect(() => {
(async () => {
if (showFiat && invoice.satoshis) {
const res = await getFiatValue(invoice.satoshis);
setFiatAmount(res);
}
})();
if (showFiat && invoice.satoshis) {
const res = getFiatValue(invoice.satoshis);
setFiatAmount(res);
}
}, [invoice.satoshis, showFiat, getFiatValue]);

useEffect(() => {
(async () => {
if (showFiat && budget) {
const res = await getFiatValue(budget);
setFiatBudgetAmount(res);
}
})();
if (showFiat && budget) {
const res = getFiatValue(budget);
setFiatBudgetAmount(res);
}
}, [budget, showFiat, getFiatValue]);

const [rememberMe, setRememberMe] = useState(false);
Expand Down
6 changes: 3 additions & 3 deletions src/app/screens/Home/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ function Home() {
),
}));

for await (const payment of payments) {
for (const payment of payments) {
const totalAmountFiat = settings.showFiat
? await getFiatValue(payment.totalAmount)
? getFiatValue(payment.totalAmount)
: "";
payment.totalAmountFiat = totalAmountFiat;
}
Expand Down Expand Up @@ -172,7 +172,7 @@ function Home() {

for (const invoice of invoices) {
const totalAmountFiat = settings.showFiat
? await getFiatValue(invoice.totalAmount)
? getFiatValue(invoice.totalAmount)
: "";
invoice.totalAmountFiat = totalAmountFiat;
}
Expand Down
2 changes: 1 addition & 1 deletion src/app/screens/LNURLPay/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { LNURLDetails, OriginData } from "~/types";

import LNURLPay from "./index";

const mockGetFiatValue = jest.fn(() => Promise.resolve("$1,22"));
const mockGetFiatValue = jest.fn(() => "$1,22");

jest.spyOn(SettingsContext, "useSettings").mockReturnValue({
settings: mockSettings,
Expand Down
8 changes: 2 additions & 6 deletions src/app/screens/LNURLPay/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,8 @@ function LNURLPay() {

useEffect(() => {
if (valueSat !== "" && showFiat) {
const getFiat = async () => {
const res = await getFiatValue(valueSat);
setFiatValue(res);
};

getFiat();
const res = getFiatValue(valueSat);
setFiatValue(res);
}
}, [valueSat, showFiat, getFiatValue]);

Expand Down
4 changes: 2 additions & 2 deletions src/app/screens/LNURLWithdraw/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,14 @@ describe("LNURLWithdraw", () => {
});

test("show error-reason on error-status", async () => {
(useNavigationState as jest.Mock).mockReturnValueOnce({
(useNavigationState as jest.Mock).mockReturnValue({
origin: mockOrigin,
args: {
lnurlDetails: mockDetailsLnBits,
},
});

(makeInvoice as jest.Mock).mockReturnValueOnce({
(makeInvoice as jest.Mock).mockReturnValue({
invoice: {
paymentRequest:
"lnbc100n1p3s975dpp508vpywcj857rxc78mrwpurhulzxe7slkdqdxsjzyrs3wv9jvsaksdqdwehh2cmgv4e8xcqzpgxqyz5vqsp5vpdqeutqqrwn4eq62a6agmnp3t7rru0asfgy23kcsr6k0tftfxfs9qyyssqf8zxtm0hm5veepjk4kz2ejegkg9449k4e9g5jz25mne096x6k4ajav0afdyx4uw883nv5jdy95w4ltfrfs4hes83j7zh50ygl8w3xxcqf0nhz8",
Expand Down
6 changes: 2 additions & 4 deletions src/app/screens/LNURLWithdraw/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,8 @@ function LNURLWithdraw() {

useEffect(() => {
if (valueSat !== "" && showFiat) {
(async () => {
const res = await getFiatValue(valueSat);
setFiatValue(res);
})();
const res = getFiatValue(valueSat);
setFiatValue(res);
}
}, [valueSat, showFiat, getFiatValue]);

Expand Down
4 changes: 2 additions & 2 deletions src/app/screens/Publisher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ function Publisher() {
),
};
});
for await (const payment of payments) {
for (const payment of payments) {
const totalAmountFiat = settings.showFiat
? await getFiatValue(payment.totalAmount)
? getFiatValue(payment.totalAmount)
: "";
payment.totalAmountFiat = totalAmountFiat;
}
Expand Down
6 changes: 2 additions & 4 deletions src/app/screens/Receive/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,8 @@ function Receive() {

useEffect(() => {
if (formData.amount !== "" && showFiat) {
(async () => {
const res = await getFiatValue(formData.amount);
setFiatAmount(res);
})();
const res = getFiatValue(formData.amount);
setFiatAmount(res);
}
}, [formData, showFiat, getFiatValue]);

Expand Down
4 changes: 2 additions & 2 deletions src/common/utils/__tests__/currencyConvert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { CURRENCIES } from "~/common/constants";
import { getFiatValue, getSatValue } from "../currencyConvert";

describe("Currency coversion utils", () => {
test("getFiatValue", async () => {
const result = await getFiatValue({
test("getFiatValue", () => {
const result = getFiatValue({
amount: 123456789,
rate: 0.00029991836,
currency: CURRENCIES["USD"],
Expand Down
22 changes: 2 additions & 20 deletions src/common/utils/currencyConvert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,7 @@
*/
import type { CURRENCIES } from "../constants";

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

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

export const getFiatValue = async ({
export const getFiatValue = ({
amount,
rate,
currency,
Expand All @@ -27,10 +12,7 @@ export const getFiatValue = async ({
rate: number;
currency: CURRENCIES;
}) => {
const fiatValue = await satoshisToFiat({
amountInSats: amount,
rate,
});
const fiatValue = Number(amount) * rate;

return fiatValue.toLocaleString("en", {
style: "currency",
Expand Down
2 changes: 1 addition & 1 deletion src/extension/background-script/events/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const paymentSuccessNotification = async (
if (showFiat) {
const rate = await getCurrencyRateWithCache(currency);

paymentAmountFiatLocale = await getFiatValue({
paymentAmountFiatLocale = getFiatValue({
amount: paymentAmount,
rate,
currency,
Expand Down