@@ -4,6 +4,12 @@ import { CapacityUnit } from './const'
44
55const base = 10e9
66const numberParser = ( value : string , exchange : string ) => {
7+ if ( Number . isNaN ( + value ) ) {
8+ throw new TypeError ( 'Value is not a valid number' )
9+ }
10+ if ( Number . isNaN ( + exchange ) ) {
11+ throw new TypeError ( 'Exchange is not a valid number' )
12+ }
713 const res = ( BigInt ( value ) * BigInt ( + exchange * base ) ) . toString ( )
814 const integer = res . slice ( 0 , res . length - 10 )
915 const decimal = res . slice ( res . length - 10 ) . replace ( / 0 + $ / , '' )
@@ -41,10 +47,18 @@ export type currencyCode = 'CKB' | 'CNY' | 'USD'
4147 * @returns
4248 */
4349export const currencyFormatter = (
44- shannons : string ,
50+ shannons : string = '0' ,
4551 unit : currencyCode = 'CKB' ,
4652 exchange : string = '0.000000001'
4753) : string => {
54+ if ( Number . isNaN ( + shannons ) ) {
55+ throw new TypeError ( `Shannons is not a valid number` )
56+ }
57+
58+ if ( Number . isNaN ( + exchange ) ) {
59+ throw new TypeError ( `Exchange is not a valid number` )
60+ }
61+
4862 const [ integer , decimal ] = numberParser ( shannons , exchange )
4963 const dot = '.'
5064 const delimiter = ','
@@ -60,12 +74,16 @@ export const currencyFormatter = (
6074 return `${ integer . replace ( / \B (? = ( \d { 3 } ) + (? ! \d ) ) / g, delimiter ) } ${ dot } ${ decimal } ${ unit } `
6175}
6276
63- export const CKBToShannonFormatter = ( amount : string , uint : CapacityUnit ) => {
77+ export const CKBToShannonFormatter = ( amount : string = '0' , unit : CapacityUnit ) => {
78+ if ( Number . isNaN ( + amount ) ) {
79+ console . warn ( `Amount is not a valid number` )
80+ return `${ amount } ${ unit } `
81+ }
6482 const [ integer = '0' , decimal = '' ] = amount . split ( '.' )
6583 const decimalLength = 10 ** decimal . length
6684 const num = integer + decimal
6785
68- switch ( uint ) {
86+ switch ( unit ) {
6987 case CapacityUnit . CKB : {
7088 return ( BigInt ( num ) * BigInt ( 1e8 / decimalLength ) ) . toString ( )
7189 }
@@ -81,7 +99,11 @@ export const CKBToShannonFormatter = (amount: string, uint: CapacityUnit) => {
8199 }
82100}
83101
84- export const shannonToCKBFormatter = ( shannon : string ) => {
102+ export const shannonToCKBFormatter = ( shannon : string = '0' ) => {
103+ if ( Number . isNaN ( + shannon ) ) {
104+ console . warn ( `Shannon is not a valid number` )
105+ return shannon
106+ }
85107 const sign = shannon . startsWith ( '-' ) ? '-' : ''
86108 const unsignedShannon = shannon . replace ( / ^ - ? 0 * / , '' )
87109 let unsignedCKB = ''
@@ -106,6 +128,10 @@ export const shannonToCKBFormatter = (shannon: string) => {
106128}
107129
108130export const localNumberFormatter = ( num : string | number = 0 ) => {
131+ if ( Number . isNaN ( + num ) ) {
132+ console . warn ( `Nuumber is not a valid number` )
133+ return num
134+ }
109135 return numberFormatter . format ( + num )
110136}
111137
@@ -114,9 +140,34 @@ export const uniformTimeFormatter = (time: string | number | Date) => {
114140}
115141
116142export const priceToFee = ( price : string , cycles : string ) => {
143+ if ( Number . isNaN ( + price ) ) {
144+ console . warn ( `Price is not a valid number` )
145+ return `0`
146+ }
117147 return ( BigInt ( price ) * BigInt ( cycles ) ) . toString ( )
118148}
119149
150+ export const addressesToBalance = ( addresses : State . Address [ ] = [ ] ) => {
151+ return addresses
152+ . reduce ( ( total , addr ) => {
153+ if ( Number . isNaN ( + addr . balance ) ) {
154+ return total
155+ }
156+ return total + BigInt ( addr . balance || 0 )
157+ } , BigInt ( 0 ) )
158+ . toString ( )
159+ }
160+
161+ export const outputsToTotalCapacity = ( outputs : { amount : string ; unit : CapacityUnit } [ ] ) => {
162+ const totalCapacity = outputs . reduce ( ( total , cur ) => {
163+ if ( Number . isNaN ( + cur . amount ) ) {
164+ return total
165+ }
166+ return total + BigInt ( CKBToShannonFormatter ( cur . amount , cur . unit ) )
167+ } , BigInt ( 0 ) )
168+ return totalCapacity . toString ( )
169+ }
170+
120171export default {
121172 queryFormatter,
122173 currencyFormatter,
@@ -125,4 +176,6 @@ export default {
125176 localNumberFormatter,
126177 uniformTimeFormatter,
127178 priceToFee,
179+ addressesToBalance,
180+ outputsToTotalCapacity,
128181}
0 commit comments