-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use satcomma to format notification amounts
- Loading branch information
1 parent
0c38daf
commit 06c9f6d
Showing
11 changed files
with
232 additions
and
205 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,41 @@ | ||
const decimals = 100_000_000; | ||
const decimals = 8; | ||
const satFactor = 100_000_000; | ||
|
||
/** | ||
* Round a number to a specific amount of decimals | ||
*/ | ||
const roundToDecimals = (number: number, decimals: number): number => { | ||
return Number(number.toFixed(decimals)); | ||
}; | ||
|
||
/** | ||
* Convert whole coins to satoshis | ||
*/ | ||
export const coinsToSatoshis = (coins: number): number => { | ||
return coins * decimals; | ||
return coins * satFactor; | ||
}; | ||
|
||
/** | ||
* Convert satoshis to whole coins and remove trailing zeros | ||
*/ | ||
export const satoshisToCoins = (satoshis: number): number => { | ||
return roundToDecimals(satoshis / decimals, 8); | ||
return roundToDecimals(satoshis / satFactor, decimals); | ||
}; | ||
|
||
/** | ||
* Round a number to a specific amount of decimals | ||
* Convert satoshis to whole coins with trailing zeros | ||
*/ | ||
const roundToDecimals = (number: number, decimals: number): number => { | ||
return Number(number.toFixed(decimals)); | ||
export const satoshisToPaddedCoins = (satoshis: number): string => { | ||
return (satoshis / satFactor).toFixed(decimals); | ||
}; | ||
|
||
export const satoshisToSatcomma = (satoshis: number): string => { | ||
let coins = (satoshis / satFactor).toFixed(decimals); | ||
for (const [num, index] of [3, 6].entries()) { | ||
coins = `${coins.substring( | ||
0, | ||
coins.length - index - num, | ||
)},${coins.substring(coins.length - index - num)}`; | ||
} | ||
return coins; | ||
}; |
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
Oops, something went wrong.