Skip to content

Commit

Permalink
web-wallet: Update duskToLux and luxToDusk
Browse files Browse the repository at this point in the history
Resolves #2736, Resolves #2737
  • Loading branch information
nortonandreev committed Oct 25, 2024
1 parent f16ff30 commit 05e786e
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 55 deletions.
6 changes: 3 additions & 3 deletions web-wallet/src/lib/components/Allocate/Allocate.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
});
$: luxFee = gasLimit * gasPrice;
$: fee = formatter(luxToDusk(luxFee));
$: fee = formatter(luxToDusk(BigInt(luxFee)));
$: isFromUnshielded = shieldedAmount > shieldedBalance;
$: isFromShielded = unshieldedAmount > unshieldedBalance;
$: isNextButtonDisabled = !(isFromUnshielded || isFromShielded);
Expand Down Expand Up @@ -229,8 +229,8 @@
<dd class="review-transaction__value operation__review-amount">
<span>
{isFromUnshielded
? `${formatter(luxToDusk(unshieldedBalance - BigInt(duskToLux(unshieldedAmount))))} DUSK`
: `${formatter(luxToDusk(shieldedBalance - BigInt(duskToLux(shieldedAmount))))} DUSK`}
? `${formatter(luxToDusk(unshieldedBalance - duskToLux(unshieldedAmount)))} DUSK`
: `${formatter(luxToDusk(shieldedBalance - duskToLux(shieldedAmount)))} DUSK`}
</span>
<Icon
className="dusk-amount__icon"
Expand Down
6 changes: 3 additions & 3 deletions web-wallet/src/lib/components/Send/Send.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@
});
$: luxFee = gasLimit * gasPrice;
$: fee = formatter(luxToDusk(luxFee));
$: fee = formatter(luxToDusk(BigInt(luxFee)));
$: maxSpendable = deductLuxFeeFrom(luxToDusk(spendable), luxFee);
$: isAmountValid = amount >= minAmount && amount <= maxSpendable;
$: totalLuxFee = luxFee + (amount ? duskToLux(amount) : 0);
$: isFeeWithinLimit = BigInt(totalLuxFee) <= spendable;
$: totalLuxFee = BigInt(luxFee) + (amount ? duskToLux(amount) : 0n);
$: isFeeWithinLimit = totalLuxFee <= spendable;
$: isNextButtonDisabled = !(isAmountValid && isGasValid && isFeeWithinLimit);
$: addressValidationResult = validateAddress(address);
Expand Down
6 changes: 3 additions & 3 deletions web-wallet/src/lib/components/Stake/Stake.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,16 @@
});
$: luxFee = gasLimit * gasPrice;
$: fee = formatter(luxToDusk(luxFee));
$: fee = formatter(luxToDusk(BigInt(luxFee)));
$: maxSpendable = deductLuxFeeFrom(luxToDusk(spendable), luxFee);
$: minStake =
maxSpendable > 0
? Math.min(minAllowedStake, maxSpendable)
: minAllowedStake;
$: isStakeAmountValid =
stakeAmount >= minStake && stakeAmount <= maxSpendable;
$: totalLuxFee = luxFee + duskToLux(stakeAmount);
$: isFeeWithinLimit = BigInt(totalLuxFee) <= spendable;
$: totalLuxFee = BigInt(luxFee) + duskToLux(stakeAmount);
$: isFeeWithinLimit = totalLuxFee <= spendable;
$: isNextButtonDisabled =
flow === "stake"
? !(isStakeAmountValid && isGasValid && isFeeWithinLimit)
Expand Down
2 changes: 1 addition & 1 deletion web-wallet/src/lib/contracts/deductLuxFeeFrom.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ import { duskToLux, luxToDusk } from "$lib/dusk/currency";
* @returns {number}
*/
const deductLuxFeeFrom = (duskAmount, luxFee) =>
+luxToDusk(duskToLux(duskAmount) - luxFee).toFixed(9);
+luxToDusk(duskToLux(duskAmount) - BigInt(luxFee)).toFixed(9);

export default deductLuxFeeFrom;
20 changes: 1 addition & 19 deletions web-wallet/src/lib/contracts/executeSend.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,7 @@ import { getKey } from "lamb";
import { Gas } from "$lib/vendor/w3sper.js/src/mod";

import { walletStore } from "$lib/stores";

const scaleFactor = BigInt(1e9);

/**
* This `duskToLux` function will replace the one
* in `$lib/dusk/currency` when the migration to
* `w3sper.js` advances.
*
* @param {number} n
* @returns {bigint}
*/
function duskToLux(n) {
const [integerPart, decimalPart] = n.toString().split(".");

return (
BigInt(integerPart) * scaleFactor +
(decimalPart ? BigInt(decimalPart.padEnd(9, "0")) : 0n)
);
}
import { duskToLux } from "$lib/dusk/currency";

/** @type {(to: string, amount: number, gasPrice:number, gasLimit:number) => Promise<string>} */
const executeSend = (to, amount, gasPrice, gasLimit) => {
Expand Down
4 changes: 2 additions & 2 deletions web-wallet/src/lib/dusk/currency/__tests__/duskToLux.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { duskToLux } from "..";

describe("duskToLux", () => {
it("should convert an amount in Dusk to Lux", () => {
expect(duskToLux(1)).toBe(1e9);
expect(duskToLux(3_456_789.012)).toBe(3_456_789_012_000_000);
expect(duskToLux(1)).toBe(BigInt(1e9));
expect(duskToLux(3_456_789.012)).toBe(BigInt(3_456_789_012_000_000));
});
});
7 changes: 1 addition & 6 deletions web-wallet/src/lib/dusk/currency/__tests__/luxToDusk.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@ import { describe, expect, it } from "vitest";
import { luxToDusk } from "..";

describe("luxToDusk", () => {
it("should convert a number amount in Lux to Dusk", () => {
expect(luxToDusk(1e9)).toBe(1);
expect(luxToDusk(123_456_789_012)).toBe(123.456789012);
});

it("should accept `BigInt`s as input", () => {
it("should take a lux value, as a bigInt, and return Dusk (as a number)", () => {
expect(luxToDusk(BigInt(1e9))).toBe(1);
expect(luxToDusk(123_456_789_012n)).toBe(123.456789012);
});
Expand Down
16 changes: 11 additions & 5 deletions web-wallet/src/lib/dusk/currency/duskToLux.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
const scaleFactor = BigInt(1e9);

/**
* Temporary conversion function until
* `dusk-wallet-js` exposes its own.
*
* @param {number} n
* @returns {number}
* @returns {bigint}
*/
const duskToLux = (n) => n * 1e9;
function duskToLux(n) {
const [integerPart, decimalPart] = n.toString().split(".");

return (
BigInt(integerPart) * scaleFactor +
(decimalPart ? BigInt(decimalPart.padEnd(9, "0")) : 0n)
);
}

export default duskToLux;
17 changes: 4 additions & 13 deletions web-wallet/src/lib/dusk/currency/luxToDusk.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
import { isType } from "lamb";

/** @type {(n: any) => n is number} */
const isNumber = isType("Number");

const scaleFactorAsNumber = 1e9;
const scaleFactorAsBigInt = BigInt(scaleFactorAsNumber);

/**
* Temporary conversion: in the near future
* we will accept only BigInt as input.
*
* @param {bigint | number} n
* @param {bigint} n
* @returns {number}
*/
function luxToDusk(n) {
if (isNumber(n)) {
return n / scaleFactorAsNumber;
} else {
const integerPart = Number(n / scaleFactorAsBigInt);
const decimalPart = Number(n % scaleFactorAsBigInt);
const integerPart = Number(n / scaleFactorAsBigInt);
const decimalPart = Number(n % scaleFactorAsBigInt);

return integerPart + decimalPart / scaleFactorAsNumber;
}
return integerPart + decimalPart / scaleFactorAsNumber;
}

export default luxToDusk;

0 comments on commit 05e786e

Please sign in to comment.