Skip to content

fix(v2-sdk): Use type-based undefined check for kLast #295

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
26 changes: 26 additions & 0 deletions sdks/v2-sdk/src/entities/pair.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,5 +451,31 @@ describe('Pair', () => {
expect(liquidityValue.currency.equals(tokenA)).toBe(true)
expect(liquidityValue.quotient.toString()).toBe('917') // ceiling(1000 - (500 * (1 / 6)))
})

it('getLiquidityValue:kLast', async () => {
const tokenA = new Token(3, '0x0000000000000000000000000000000000000001', 18)
const tokenB = new Token(3, '0x0000000000000000000000000000000000000002', 18)
const pair = new Pair(CurrencyAmount.fromRawAmount(tokenA, '1000'), CurrencyAmount.fromRawAmount(tokenB, '1000'))

expect(() => {
pair.getLiquidityValue(
tokenA,
CurrencyAmount.fromRawAmount(pair.liquidityToken, '1000'),
CurrencyAmount.fromRawAmount(pair.liquidityToken, '1000'),
true,
0 // kLast is explicitly 0, which is a valid value
)
}).not.toThrow()

expect(() => {
pair.getLiquidityValue(
tokenA,
CurrencyAmount.fromRawAmount(pair.liquidityToken, '1000'),
CurrencyAmount.fromRawAmount(pair.liquidityToken, '1000'),
true,
undefined // kLast is undefined; should trigger the invariant
)
}).toThrow('K_LAST')
})
})
})
2 changes: 1 addition & 1 deletion sdks/v2-sdk/src/entities/pair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ export class Pair {
if (!feeOn) {
totalSupplyAdjusted = totalSupply
} else {
invariant(!!kLast, 'K_LAST')
invariant(typeof kLast !== 'undefined', 'K_LAST')
const kLastParsed = JSBI.BigInt(kLast)
if (!JSBI.equal(kLastParsed, ZERO)) {
const rootK = sqrt(JSBI.multiply(this.reserve0.quotient, this.reserve1.quotient))
Expand Down