From ffca6eb83c1fdbeef9efff1a58a211f9b47ebd1f Mon Sep 17 00:00:00 2001 From: Andrea Scartabelli Date: Mon, 25 Nov 2024 12:04:04 +0100 Subject: [PATCH] web-wallet: use Lux for all calculations, display only in Dusk - removed misleading case in `luxToDusk` tests Resolves #2860 --- .../src/lib/components/Send/Send.svelte | 4 ++-- .../src/lib/components/__tests__/Send.spec.js | 9 ++++----- .../lib/components/__tests__/Stake.spec.js | 20 +++++++++---------- .../__tests__/deductLuxFeeFrom.spec.js | 16 --------------- .../src/lib/contracts/deductLuxFeeFrom.js | 16 --------------- web-wallet/src/lib/contracts/index.js | 1 - .../dusk/currency/__tests__/luxToDusk.spec.js | 6 ------ 7 files changed, 15 insertions(+), 57 deletions(-) delete mode 100644 web-wallet/src/lib/contracts/__tests__/deductLuxFeeFrom.spec.js delete mode 100644 web-wallet/src/lib/contracts/deductLuxFeeFrom.js diff --git a/web-wallet/src/lib/components/Send/Send.svelte b/web-wallet/src/lib/components/Send/Send.svelte index 9d69a141d2..137b9160eb 100644 --- a/web-wallet/src/lib/components/Send/Send.svelte +++ b/web-wallet/src/lib/components/Send/Send.svelte @@ -8,7 +8,7 @@ mdiArrowUpBoldBoxOutline, mdiWalletOutline, } from "@mdi/js"; - import { areValidGasSettings, deductLuxFeeFrom } from "$lib/contracts"; + import { areValidGasSettings } from "$lib/contracts"; import { duskToLux, luxToDusk } from "$lib/dusk/currency"; import { validateAddress } from "$lib/dusk/string"; import { logo } from "$lib/dusk/icons"; @@ -96,7 +96,7 @@ }); $: fee = gasLimit * gasPrice; - $: maxSpendable = deductLuxFeeFrom(luxToDusk(spendable), fee); + $: maxSpendable = luxToDusk(spendable - fee); $: isAmountValid = amount >= minAmount && amount <= maxSpendable; $: totalLuxFee = fee + (amount ? duskToLux(amount) : 0n); $: isFeeWithinLimit = totalLuxFee <= spendable; diff --git a/web-wallet/src/lib/components/__tests__/Send.spec.js b/web-wallet/src/lib/components/__tests__/Send.spec.js index 63c954899a..237c47c6f3 100644 --- a/web-wallet/src/lib/components/__tests__/Send.spec.js +++ b/web-wallet/src/lib/components/__tests__/Send.spec.js @@ -1,6 +1,5 @@ import { afterAll, afterEach, describe, expect, it, vi } from "vitest"; import { cleanup, fireEvent, render } from "@testing-library/svelte"; -import { deductLuxFeeFrom } from "$lib/contracts"; import { createCurrencyFormatter, luxToDusk } from "$lib/dusk/currency"; import { getAsHTMLElement } from "$lib/dusk/test-helpers"; @@ -137,9 +136,9 @@ describe("Send", () => { }); it("should set the max amount in the textbox if the user clicks the related button", async () => { - const maxSpendable = deductLuxFeeFrom( - luxToDusk(baseProps.spendable), - baseProps.gasSettings.gasPrice * baseProps.gasSettings.gasLimit + const maxSpendableDusk = luxToDusk( + baseProps.spendable - + baseProps.gasSettings.gasPrice * baseProps.gasSettings.gasLimit ); const { getByRole } = render(Send, baseProps); @@ -151,7 +150,7 @@ describe("Send", () => { await fireEvent.click(useMaxButton); - expect(amountInput).toHaveValue(maxSpendable); + expect(amountInput).toHaveValue(maxSpendableDusk); expect(nextButton).toBeEnabled(); }); diff --git a/web-wallet/src/lib/components/__tests__/Stake.spec.js b/web-wallet/src/lib/components/__tests__/Stake.spec.js index 6acf2a2e17..a4e0226bd0 100644 --- a/web-wallet/src/lib/components/__tests__/Stake.spec.js +++ b/web-wallet/src/lib/components/__tests__/Stake.spec.js @@ -8,7 +8,6 @@ import { vi, } from "vitest"; import { cleanup, fireEvent, render } from "@testing-library/svelte"; -import { deductLuxFeeFrom } from "$lib/contracts"; import { createCurrencyFormatter, duskToLux, @@ -79,9 +78,9 @@ describe("Stake", () => { target: document.body, }; - const maxSpendable = deductLuxFeeFrom( - luxToDusk(baseProps.spendable), - baseProps.gasSettings.gasPrice * baseProps.gasSettings.gasLimit + const maxSpendableDusk = luxToDusk( + baseProps.spendable - + baseProps.gasSettings.gasPrice * baseProps.gasSettings.gasLimit ); afterEach(() => { @@ -114,7 +113,7 @@ describe("Stake", () => { expect(amountInput.getAttribute("min")).toBe( luxToDusk(baseProps.minAllowedStake).toString() ); - expect(amountInput.getAttribute("max")).toBe(maxSpendable.toString()); + expect(amountInput.getAttribute("max")).toBe(maxSpendableDusk.toString()); expect(container.firstChild).toMatchSnapshot(); }); @@ -127,9 +126,8 @@ describe("Stake", () => { gasPrice: 40000000n, }, }; - const currentMaxSpendable = deductLuxFeeFrom( - luxToDusk(props.spendable), - props.gasSettings.gasPrice * props.gasSettings.gasLimit + const currentMaxSpendableDusk = luxToDusk( + props.spendable - props.gasSettings.gasPrice * props.gasSettings.gasLimit ); const { getByRole } = render(Stake, { ...baseOptions, props }); const nextButton = getByRole("button", { name: "Next" }); @@ -141,7 +139,7 @@ describe("Stake", () => { luxToDusk(baseProps.minAllowedStake).toString() ); expect(amountInput.getAttribute("max")).toBe( - currentMaxSpendable.toString() + currentMaxSpendableDusk.toString() ); }); @@ -153,7 +151,7 @@ describe("Stake", () => { const amountInput = getByRole("spinbutton"); - expect(amountInput).toHaveValue(maxSpendable); + expect(amountInput).toHaveValue(maxSpendableDusk); }); it("should not change the default amount (min stake amount) in the textbox if the user clicks the related button and the balance is zero", async () => { @@ -290,7 +288,7 @@ describe("Stake", () => { expect(baseProps.execute).toHaveBeenCalledTimes(1); expect(baseProps.execute).toHaveBeenCalledWith( - duskToLux(maxSpendable), + duskToLux(maxSpendableDusk), baseProps.gasSettings.gasPrice, baseProps.gasSettings.gasLimit ); diff --git a/web-wallet/src/lib/contracts/__tests__/deductLuxFeeFrom.spec.js b/web-wallet/src/lib/contracts/__tests__/deductLuxFeeFrom.spec.js deleted file mode 100644 index 4de2df0d3c..0000000000 --- a/web-wallet/src/lib/contracts/__tests__/deductLuxFeeFrom.spec.js +++ /dev/null @@ -1,16 +0,0 @@ -import { describe, expect, it } from "vitest"; - -import { deductLuxFeeFrom } from ".."; - -describe("deductLuxFeeFrom", () => { - it("should deduct a fee in Lux from a Dusk amount and return a value in Dusk", () => { - expect(deductLuxFeeFrom(1000, 20000000n)).toBe(999.98); - expect(deductLuxFeeFrom(1000.456, 40000000n)).toBe(1000.416); - expect(deductLuxFeeFrom(1000.456, BigInt(4e15))).toBe(-3998999.544); - - // the simple subtraction would return 664144.7698459921 which has an extra decimal digit - expect(deductLuxFeeFrom(664_144.809845992, 40000000n)).toBe( - 664_144.769845992 - ); - }); -}); diff --git a/web-wallet/src/lib/contracts/deductLuxFeeFrom.js b/web-wallet/src/lib/contracts/deductLuxFeeFrom.js deleted file mode 100644 index 7a4058fb8c..0000000000 --- a/web-wallet/src/lib/contracts/deductLuxFeeFrom.js +++ /dev/null @@ -1,16 +0,0 @@ -import { duskToLux, luxToDusk } from "$lib/dusk/currency"; - -/** - * Deducts a fee in Lux from an amount in Dusk - * and returns a value in Dusk. - * If the returned value is negative, the fee exceeds - * the given amount. - * - * @param {number} duskAmount - * @param {bigint} luxFee - * @returns {number} - */ -const deductLuxFeeFrom = (duskAmount, luxFee) => - +luxToDusk(duskToLux(duskAmount) - luxFee).toFixed(9); - -export default deductLuxFeeFrom; diff --git a/web-wallet/src/lib/contracts/index.js b/web-wallet/src/lib/contracts/index.js index bb1d1ea387..5090e6c193 100644 --- a/web-wallet/src/lib/contracts/index.js +++ b/web-wallet/src/lib/contracts/index.js @@ -1,5 +1,4 @@ export { default as contractDescriptors } from "./contract-descriptors"; -export { default as deductLuxFeeFrom } from "./deductLuxFeeFrom"; export { default as executeSend } from "./executeSend"; export { default as areValidGasSettings } from "./areValidGasSettings"; export { default as updateOperation } from "./updateOperation"; diff --git a/web-wallet/src/lib/dusk/currency/__tests__/luxToDusk.spec.js b/web-wallet/src/lib/dusk/currency/__tests__/luxToDusk.spec.js index 60fb3d2a29..5e9a3c9aa4 100644 --- a/web-wallet/src/lib/dusk/currency/__tests__/luxToDusk.spec.js +++ b/web-wallet/src/lib/dusk/currency/__tests__/luxToDusk.spec.js @@ -12,11 +12,5 @@ describe("luxToDusk", () => { expect(luxToDusk(9_007_199_254_740_993n)).toBe(9_007_199.254740993); expect(luxToDusk(10_000_000_001n)).toBe(10.000000001); expect(luxToDusk(3_141_592_653_589_793n)).toBe(3_141_592.653589793); - - // result with integer part bigger than Number.MAX_SAFE_INTEGER - expect(luxToDusk(123_456_789_012_345_678_901_234_567_890n)).toBe( - // eslint-disable-next-line no-loss-of-precision - 123_456_789_012_345_678_901.23456789 - ); }); });