Uniswap / sdk-core

⚙️ Code shared across TypeScript Uniswap SDK versions
MIT License
121 stars 355 forks source link

Upgrade jsbi to 3.2.4 #96

Closed mrosendin closed 11 months ago

mrosendin commented 11 months ago

Resolves https://github.com/Uniswap/v3-sdk/issues/94

This SO solution recommends jsbi@3.2.5, however, in running v3-sdk tests the error Could not parse fraction is still occurring from Fraction.tryParseFunction, where it doesn't recognize the passed in variable fractionish as instanceof JSB. I suspect this is happening because the instance's prototype is lost between the different libraries due to transpilation to CommonJS, hence why fractionish instanceof JSBI works in sdk-core but not in v3-sdk.

I did check the transpiled sdk-core code and you can see that the passed in var other loses its type signatures, so when compared in tryParseFraction the check returns false.

  _proto.lessThan = function lessThan(other) {
    var otherParsed = Fraction.tryParseFraction(other);
    return JSBI.lessThan(JSBI.multiply(this.numerator, otherParsed.denominator), JSBI.multiply(otherParsed.numerator, this.denominator));
  };

...

  Fraction.tryParseFraction = function tryParseFraction(fractionish) {
    if (fractionish instanceof JSBI || typeof fractionish === 'number' || typeof fractionish === 'string') {
      return new Fraction(fractionish);
    }
    if ('numerator' in fractionish && 'denominator' in fractionish) return fractionish;
    throw new Error('Could not parse fraction');
  }

Checking typeof fractionish === 'object' is not robust enough since a Fraction will also be of type object.

JSBI is a subclass of Array, hence the recommended solution to check for fractionish instanceof Array.

socket-security[bot] commented 11 months ago

Updated dependencies detected. Learn more about Socket for GitHub ↗︎

Packages Version New capabilities Transitives Size Publisher
jsbi 3.1.4...3.2.5 None +0/-0 322 kB google-wombot