Skip to content

Commit

Permalink
Merge pull request #3054 from dusk-network/feature-2860
Browse files Browse the repository at this point in the history
web-wallet: use Lux for all calculations, display only in Dusk
  • Loading branch information
ascartabelli authored Nov 25, 2024
2 parents d9eaae0 + ffca6eb commit 245bedb
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 57 deletions.
4 changes: 2 additions & 2 deletions web-wallet/src/lib/components/Send/Send.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 4 additions & 5 deletions web-wallet/src/lib/components/__tests__/Send.spec.js
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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);

Expand All @@ -151,7 +150,7 @@ describe("Send", () => {

await fireEvent.click(useMaxButton);

expect(amountInput).toHaveValue(maxSpendable);
expect(amountInput).toHaveValue(maxSpendableDusk);
expect(nextButton).toBeEnabled();
});

Expand Down
20 changes: 9 additions & 11 deletions web-wallet/src/lib/components/__tests__/Stake.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
vi,
} from "vitest";
import { cleanup, fireEvent, render } from "@testing-library/svelte";
import { deductLuxFeeFrom } from "$lib/contracts";
import {
createCurrencyFormatter,
duskToLux,
Expand Down Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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();
});

Expand All @@ -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" });
Expand All @@ -141,7 +139,7 @@ describe("Stake", () => {
luxToDusk(baseProps.minAllowedStake).toString()
);
expect(amountInput.getAttribute("max")).toBe(
currentMaxSpendable.toString()
currentMaxSpendableDusk.toString()
);
});

Expand All @@ -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 () => {
Expand Down Expand Up @@ -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
);
Expand Down
16 changes: 0 additions & 16 deletions web-wallet/src/lib/contracts/__tests__/deductLuxFeeFrom.spec.js

This file was deleted.

16 changes: 0 additions & 16 deletions web-wallet/src/lib/contracts/deductLuxFeeFrom.js

This file was deleted.

1 change: 0 additions & 1 deletion web-wallet/src/lib/contracts/index.js
Original file line number Diff line number Diff line change
@@ -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";
6 changes: 0 additions & 6 deletions web-wallet/src/lib/dusk/currency/__tests__/luxToDusk.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
});
});

0 comments on commit 245bedb

Please sign in to comment.