Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/currency-safe-comparisons.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@uniswap/sdk-core": minor
---

`CurrencyAmount.greaterThan/equalTo/lessThan` now throw a `CURRENCY` invariant when compared against a `CurrencyAmount` of a different currency, matching the existing behavior of `add`/`subtract`. Previously these fell through to `Fraction` and silently compared raw numerators across currencies. Comparisons against a raw amount (number, `JSBI`, `Fraction`) are unchanged.
33 changes: 33 additions & 0 deletions sdks/sdk-core/src/entities/fractions/currencyAmount.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,37 @@ describe('CurrencyAmount', () => {
expect(amount.toExact()).toEqual('0.00123')
})
})

describe('#comparisons', () => {
const ADDRESS_TWO = '0x0000000000000000000000000000000000000002'
const tokenA = new Token(1, ADDRESS_ONE, 18)
const tokenB = new Token(1, ADDRESS_TWO, 18)

it('compares amounts of the same currency', () => {
const two = CurrencyAmount.fromRawAmount(tokenA, 2)
const one = CurrencyAmount.fromRawAmount(tokenA, 1)
expect(two.greaterThan(one)).toBe(true)
expect(one.lessThan(two)).toBe(true)
expect(one.equalTo(CurrencyAmount.fromRawAmount(tokenA, 1))).toBe(true)
})

it('still compares against a raw amount (number, JSBI) without a currency', () => {
const amount = CurrencyAmount.fromRawAmount(tokenA, 1)
const zero = CurrencyAmount.fromRawAmount(tokenA, 0)
expect(amount.greaterThan(0)).toBe(true)
expect(zero.equalTo(0)).toBe(true)
// raw BigintIsh (e.g. a JSBI ZERO constant) must keep working, not just the 0 literal
expect(zero.equalTo(JSBI.BigInt(0))).toBe(true)
expect(amount.greaterThan(JSBI.BigInt(0))).toBe(true)
expect(zero.lessThan(amount)).toBe(true)
})

it('throws when comparing different currencies', () => {
const a = CurrencyAmount.fromRawAmount(tokenA, 1)
const b = CurrencyAmount.fromRawAmount(tokenB, 1)
expect(() => a.greaterThan(b)).toThrow('CURRENCY')
expect(() => a.lessThan(b)).toThrow('CURRENCY')
expect(() => a.equalTo(b)).toThrow('CURRENCY')
})
})
})
20 changes: 20 additions & 0 deletions sdks/sdk-core/src/entities/fractions/currencyAmount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,26 @@ export class CurrencyAmount<T extends Currency> extends Fraction {
return CurrencyAmount.fromFractionalAmount(this.currency, divided.numerator, divided.denominator)
}

// Comparisons keep the base `Fraction | BigintIsh` signature so existing
// callers (e.g. `amount.equalTo(ZERO)` against a raw zero) keep working, and
// add the same currency-safety invariant that add/subtract enforce whenever
// the other side is itself a CurrencyAmount - so a cross-currency comparison
// throws instead of silently comparing raw numerators.
public lessThan(other: Fraction | BigintIsh): boolean {
if (other instanceof CurrencyAmount) invariant(this.currency.equals(other.currency), 'CURRENCY')
return super.lessThan(other)
}

public equalTo(other: Fraction | BigintIsh): boolean {
if (other instanceof CurrencyAmount) invariant(this.currency.equals(other.currency), 'CURRENCY')
return super.equalTo(other)
}

public greaterThan(other: Fraction | BigintIsh): boolean {
if (other instanceof CurrencyAmount) invariant(this.currency.equals(other.currency), 'CURRENCY')
return super.greaterThan(other)
}

public toSignificant(
significantDigits: number = 6,
format?: object,
Expand Down
Loading