@@ -237,8 +237,17 @@ export function toHex(value?: CurrencyAmount<Currency> | CurrencyAmount<Token>):
237237 return value ? `0x${ value . quotient . toString ( 16 ) } ` : '0x0' ;
238238}
239239
240+ /** bignumber.js `toFraction()` returns a single BigNumber for NaN/±Infinity (`!xc`), not `[n, d]`. */
241+ function bignumberNumeratorDenominator ( amount : BigNumber ) : [ BigNumber , BigNumber ] {
242+ if ( ! amount . isFinite ( ) ) {
243+ return [ new BigNumber ( 0 ) , new BigNumber ( 1 ) ] ;
244+ }
245+ const frac = amount . toFraction ( ) ;
246+ return Array . isArray ( frac ) ? frac : [ frac , new BigNumber ( 1 ) ] ;
247+ }
248+
240249export function toCurrencyAmount ( token : Token , amount : BigNumber ) : CurrencyAmount < Token > {
241- const [ amountNum , amountDeno ] = amount . toFraction ( ) ;
250+ const [ amountNum , amountDeno ] = bignumberNumeratorDenominator ( amount ) ;
242251 return CurrencyAmount . fromFractionalAmount (
243252 token ,
244253 amountNum . times ( TEN . pow ( token . decimals ) ) . toFixed ( ) ,
@@ -247,12 +256,12 @@ export function toCurrencyAmount(token: Token, amount: BigNumber): CurrencyAmoun
247256}
248257
249258export function toCurrencyAmountFromRawBN ( token : Token , amount : BigNumber ) : CurrencyAmount < Token > {
250- const [ amountNum , amountDeno ] = amount . toFraction ( ) ;
259+ const [ amountNum , amountDeno ] = bignumberNumeratorDenominator ( amount ) ;
251260 return CurrencyAmount . fromFractionalAmount ( token , amountNum . toFixed ( ) , amountDeno . toFixed ( ) ) ;
252261}
253262
254263export function toFraction ( amount : BigNumber | undefined ) : Fraction {
255- const [ amountNum , amountDeno ] = amount ? amount . toFraction ( ) : [ 0 , 1 ] ;
264+ const [ amountNum , amountDeno ] = amount ? bignumberNumeratorDenominator ( amount ) : [ new BigNumber ( 0 ) , new BigNumber ( 1 ) ] ;
256265 return new Fraction ( amountNum . toFixed ( ) , amountDeno . toFixed ( ) ) ;
257266}
258267
0 commit comments