Skip to content
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

test: reset 8578b74 #26

Open
wants to merge 1 commit 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 lib/util/misc.ts
Original file line number Diff line number Diff line change
@@ -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,
};
107 changes: 107 additions & 0 deletions lib/util/pair.ts
Original file line number Diff line number Diff line change
@@ -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<T> = {
-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<Token>, tokenAmountB: CurrencyAmount<Token>) {
super(currencyAmountA, tokenAmountB);
const mutableThis = this as Mutable<Pair>;
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<Token>,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_calculateFotFees?: boolean,
): [CurrencyAmount<Token>, 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<Token>,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_calculateFotFees?: boolean,
): [CurrencyAmount<Token>, 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))];
}
}
Loading