-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: rewrite sudtValueToAmount with formatUnit in neuron-wallet (#…
- Loading branch information
Showing
2 changed files
with
18 additions
and
29 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,32 +1,11 @@ | ||
const sudtValueToAmount = (value: string | null | undefined = '0', decimal: string | null | undefined = '') => { | ||
if (value === null || value === '0') { | ||
return '+0' | ||
} | ||
|
||
if (decimal === null || decimal === undefined || Number.isNaN(+value)) { | ||
return '--' | ||
} | ||
|
||
let sign = '+' | ||
if (value.startsWith('-')) { | ||
sign = '-' | ||
} | ||
|
||
const unsignedValue = value.replace(/^-?0*/, '') | ||
|
||
const dec = +decimal | ||
if (dec === 0) { | ||
return +unsignedValue ? `${sign}${unsignedValue}` : '+0' | ||
} | ||
let unsignedSUDTValue = '' | ||
if (unsignedValue.length <= dec) { | ||
unsignedSUDTValue = `0.${unsignedValue.padStart(dec, '0')}`.replace(/\.?0+$/, '') | ||
} else { | ||
const decimalFraction = `.${unsignedValue.slice(-dec)}`.replace(/\.?0+$/, '') | ||
const int = unsignedValue.slice(0, -dec).replace(/\^0+/, '') | ||
unsignedSUDTValue = `${int}${decimalFraction}` | ||
} | ||
return `${sign}${unsignedSUDTValue}` | ||
import { formatUnit } from '@ckb-lumos/bi' | ||
|
||
const sudtValueToAmount = (value: string | null = '0', decimal: string | null = '') => { | ||
return value === null || value === '0' | ||
? '+0' | ||
: decimal === null || Number.isNaN(+value) || Number.isNaN(+decimal) | ||
? '--' | ||
: `${+value >= 0 ? '+' : ''}${formatUnit(BigInt(value), +decimal)}` | ||
} | ||
|
||
export default sudtValueToAmount |
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
de1df5d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Packaging for test is done in 8660067732
de1df5d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Packaging for test is done in 8660069418