From 1783b02428db9eea5596b1db97693417fc38fb28 Mon Sep 17 00:00:00 2001 From: Sang Tran Date: Thu, 24 Oct 2024 19:47:04 +0700 Subject: [PATCH] test: reset 8578b74 --- lib/util/misc.ts | 26 ++++++++++++ lib/util/pair.ts | 107 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 lib/util/misc.ts create mode 100644 lib/util/pair.ts diff --git a/lib/util/misc.ts b/lib/util/misc.ts new file mode 100644 index 0000000..7896cd1 --- /dev/null +++ b/lib/util/misc.ts @@ -0,0 +1,26 @@ +import { Percent } from '@uniswap/sdk-core'; +import JSBI from 'jsbi'; + +const BigInt_0 = JSBI.BigInt(0); +const BigInt_1 = JSBI.BigInt(1); +const BigInt_5 = JSBI.BigInt(5); +const BigInt_997 = JSBI.BigInt(997); +const BigInt_1000 = JSBI.BigInt(1000); + +const BASIS_POINTS = JSBI.BigInt(10000); + +const ZERO_PERCENT = new Percent(BigInt_0); +const ONE_HUNDRED_PERCENT = new Percent(BigInt_1); +const MINIMUM_LIQUIDITY = JSBI.BigInt(1000); + +export { + BASIS_POINTS, + BigInt_0, + BigInt_1, + BigInt_5, + BigInt_997, + BigInt_1000, + MINIMUM_LIQUIDITY, + ONE_HUNDRED_PERCENT, + ZERO_PERCENT, +}; diff --git a/lib/util/pair.ts b/lib/util/pair.ts new file mode 100644 index 0000000..0d0aca7 --- /dev/null +++ b/lib/util/pair.ts @@ -0,0 +1,107 @@ +// @ts-nocheck +import { computePairAddress } from '@axieinfinity/sdk-core'; +import { CurrencyAmount, Token } from '@uniswap/sdk-core'; +import { InsufficientInputAmountError, InsufficientReservesError, Pair } from '@uniswap/v2-sdk'; +import JSBI from 'jsbi'; +import invariant from 'tiny-invariant'; + +import { BigInt_0, BigInt_1, BigInt_997, BigInt_1000 } from './misc'; + +type Mutable = { + -readonly [k in keyof T]: T[k]; +}; + +/** + * This is a clone Pair class from "@uniswap/v2-sdk" (https://github.com/Uniswap/sdks/blob/main/sdks/v2-sdk/src/entities/pair.ts) + * with custom factoryAddress & initCodeHash for Ronin chain + */ +export class CustomPair extends Pair { + public static getAddress(tokenA: Token, tokenB: Token): string { + return computePairAddress({ chainId: tokenA.chainId, tokenA, tokenB }); + } + + public constructor(currencyAmountA: CurrencyAmount, tokenAmountB: CurrencyAmount) { + super(currencyAmountA, tokenAmountB); + const mutableThis = this as Mutable; + const tokenAmounts = currencyAmountA.currency.sortsBefore(tokenAmountB.currency) // does safety checks + ? [currencyAmountA, tokenAmountB] + : [tokenAmountB, currencyAmountA]; + + const name = ['ERC20', ' / ', 'ERC20']; + const symbol = ['ERC20', '/', 'ERC20']; + + tokenAmounts[0].currency.name; + tokenAmounts[1].currency.name; + if (tokenAmounts[0].currency.name) { + name[0] = tokenAmounts[0].currency.name; + } + + if (tokenAmounts[1].currency.name) { + name[2] = tokenAmounts[1].currency.name; + } + + if (tokenAmounts[0].currency.symbol) { + symbol[0] = tokenAmounts[0].currency.symbol; + } + + if (tokenAmounts[1].currency.symbol) { + symbol[2] = tokenAmounts[1].currency.symbol; + } + mutableThis.liquidityToken = new Token( + tokenAmounts[0].currency.chainId, + CustomPair.getAddress(tokenAmounts[0].currency, tokenAmounts[1].currency), + 18, + symbol.join(''), + name.join(''), + ); + } + + public getOutputAmount( + inputAmount: CurrencyAmount, + // eslint-disable-next-line @typescript-eslint/no-unused-vars + _calculateFotFees?: boolean, + ): [CurrencyAmount, Pair] { + invariant(this.involvesToken(inputAmount.currency), 'TOKEN'); + if (JSBI.equal(this.reserve0.quotient, BigInt_0) || JSBI.equal(this.reserve1.quotient, BigInt_0)) { + throw new InsufficientReservesError(); + } + const inputReserve = this.reserveOf(inputAmount.currency); + const outputReserve = this.reserveOf(inputAmount.currency.equals(this.token0) ? this.token1 : this.token0); + const inputAmountWithFee = JSBI.multiply(inputAmount.quotient, BigInt_997); + const numerator = JSBI.multiply(inputAmountWithFee, outputReserve.quotient); + const denominator = JSBI.add(JSBI.multiply(inputReserve.quotient, BigInt_1000), inputAmountWithFee); + const outputAmount = CurrencyAmount.fromRawAmount( + inputAmount.currency.equals(this.token0) ? this.token1 : this.token0, + JSBI.divide(numerator, denominator), + ); + if (JSBI.equal(outputAmount.quotient, BigInt_0)) { + throw new InsufficientInputAmountError(); + } + return [outputAmount, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))]; + } + + public getInputAmount( + outputAmount: CurrencyAmount, + // eslint-disable-next-line @typescript-eslint/no-unused-vars + _calculateFotFees?: boolean, + ): [CurrencyAmount, Pair] { + invariant(this.involvesToken(outputAmount.currency), 'TOKEN'); + if ( + JSBI.equal(this.reserve0.quotient, BigInt_0) || + JSBI.equal(this.reserve1.quotient, BigInt_0) || + JSBI.greaterThanOrEqual(outputAmount.quotient, this.reserveOf(outputAmount.currency).quotient) + ) { + throw new InsufficientReservesError(); + } + + const outputReserve = this.reserveOf(outputAmount.currency); + const inputReserve = this.reserveOf(outputAmount.currency.equals(this.token0) ? this.token1 : this.token0); + const numerator = JSBI.multiply(JSBI.multiply(inputReserve.quotient, outputAmount.quotient), BigInt_1000); + const denominator = JSBI.multiply(JSBI.subtract(outputReserve.quotient, outputAmount.quotient), BigInt_997); + const inputAmount = CurrencyAmount.fromRawAmount( + outputAmount.currency.equals(this.token0) ? this.token1 : this.token0, + JSBI.add(JSBI.divide(numerator, denominator), BigInt_1), + ); + return [inputAmount, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))]; + } +}