-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
web-wallet: Update duskToLux and luxToDusk
- Loading branch information
1 parent
f16ff30
commit 05e786e
Showing
9 changed files
with
29 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |