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

cleanup some pair files #52

Merged
merged 1 commit into from
Jul 18, 2023
Merged
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
58 changes: 0 additions & 58 deletions src/pair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,61 +63,3 @@ export abstract class Pair {
public abstract snapshot(): Snapshot;
public abstract restore(snapshot: Snapshot): void;
}

interface PairXYeqKSnapshot extends Snapshot {
fee: BigNumberString
bucketA: BigNumberString
bucketB: BigNumberString
}

export abstract class PairXYeqK extends Pair {
private fee: BigNumber = new BigNumber(0)
private bucketA: BigNumber = new BigNumber(0)
private bucketB: BigNumber = new BigNumber(0)

public refreshBuckets(fee: BigNumber, bucketA: BigNumber, bucketB: BigNumber) {
this.fee = fee
this.bucketA = bucketA
this.bucketB = bucketB
}

public outputAmount(inputToken: Address, inputAmount: BigNumber): BigNumber {
const buckets =
inputToken === this.tokenA ? [this.bucketA, this.bucketB] :
inputToken === this.tokenB ? [this.bucketB, this.bucketA] : null
if (buckets === null) {
throw new Error(`unsupported input: ${inputToken}, pair: ${this.tokenA}/${this.tokenB}!`)
}
if (this.bucketA.lt(1) || this.bucketB.lt(1)) {
return new BigNumber(0)
}
const amountWithFee = inputAmount.multipliedBy(this.fee)
const outputAmount = buckets[1].multipliedBy(amountWithFee).idiv(buckets[0].plus(amountWithFee))
return !outputAmount.isNaN() ? outputAmount : new BigNumber(0)
}

public inputAmount(outputToken: Address, outputAmount: BigNumber): BigNumber {
const buckets =
outputToken === this.tokenB ? [this.bucketA, this.bucketB] :
outputToken === this.tokenA ? [this.bucketB, this.bucketA] : null
if (buckets === null) {
throw new Error(`unsupported output: ${outputToken}, pair: ${this.tokenA}/${this.tokenB}!`)
}
return buckets[0].multipliedBy(outputAmount).idiv(
buckets[1].minus(outputAmount).multipliedBy(this.fee))
}

public snapshot(): PairXYeqKSnapshot {
return {
fee: this.fee.toFixed(),
bucketA: this.bucketA.toFixed(),
bucketB: this.bucketB.toFixed(),
}
}

public restore(snapshot: PairXYeqKSnapshot): void {
this.fee = new BigNumber(snapshot.fee)
this.bucketA = new BigNumber(snapshot.bucketA)
this.bucketB = new BigNumber(snapshot.bucketB)
}
}
2 changes: 1 addition & 1 deletion src/pairs/mento.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { ExchangeWrapper } from "@celo/contractkit/lib/wrappers/Exchange"
import BigNumber from "bignumber.js"
import Web3 from "web3"

import { PairXYeqK } from "../pair"
import { address as pairMentoAddress } from "../../tools/deployed/mainnet.PairMento.addr.json"
import { ReserveWrapper } from "@celo/contractkit/lib/wrappers/Reserve"
import { SortedOraclesWrapper } from "@celo/contractkit/lib/wrappers/SortedOracles"
import { selectAddress } from "../utils"
import { PairXYeqK } from "./pair-xyeqk"

export class PairMento extends PairXYeqK {
allowRepeats = false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { Address, BigNumberString, Pair, Snapshot } from "../../pair";
import { Address, BigNumberString, Pair, Snapshot } from "../pair";

import { TickMath } from "../../utils/concentrated-liquidity/tickMath";
import { SwapMath, UniV3FeeAmount } from "../../utils/concentrated-liquidity/swapMath";
import { LiquidityMath } from "../../utils/concentrated-liquidity/liquidityMath";
import { TickMath } from "../utils/concentrated-liquidity/tickMath";
import { SwapMath, UniV3FeeAmount } from "../utils/concentrated-liquidity/swapMath";
import { LiquidityMath } from "../utils/concentrated-liquidity/liquidityMath";

import { NEGATIVE_ONE, ONE, ZERO } from "../../constants";
import { JSBI_BN } from "../../utils/JSBI_BN";
import { ONE, ZERO } from "../constants";
import BigNumber from "bignumber.js";
import { SwappaMathError } from "../../errors";

export interface SpotTicksPayload {
sqrtPriceX96: string;
Expand Down Expand Up @@ -232,32 +230,26 @@ export abstract class PairContentratedLiquidity extends Pair {
state.amountSpecifiedRemaining - (step.amountIn + step.feeAmount);
state.amountCalculated = state.amountCalculated + step.amountOut;

if (JSBI_BN.equal(state.sqrtPriceX96, step.sqrtPriceNextX96 ?? 0)) {
if (state.sqrtPriceX96 === step.sqrtPriceNextX96!) {
// if the tick is initialized, run the tick transition
if (step.initialized) {
let liquidityNet = BigInt(
this.ticks[step.tickIndexNext].liquidityNet
);
let liquidityNet = this.ticks[step.tickIndexNext].liquidityNet

// if we're moving leftward, we interpret liquidityNet as the opposite sign
// safe because liquidityNet cannot be type(int128).min
if (zeroForOne)
liquidityNet = JSBI_BN.multiply(liquidityNet, NEGATIVE_ONE);
if (zeroForOne) {
liquidityNet = -liquidityNet
}

state.liquidity = LiquidityMath.addDelta(
state.liquidity ?? BigInt(0),
state.liquidity!,
liquidityNet
);
}

state.tick = zeroForOne ? step.tickNext - 1 : step.tickNext;
state.tickIndex = this.tickToIndex[step.tickNext];
} else if (
JSBI_BN.notEqual(
state.sqrtPriceX96,
step.sqrtPriceStartX96 ?? BigInt(0)
)
) {
} else if (state.sqrtPriceX96 !== step.sqrtPriceStartX96!) {
// updated comparison function
// recompute unless we're on a lower tick boundary (i.e. already transitioned ticks), and haven't moved
state.tick = TickMath.getTickAtSqrtRatio(state.sqrtPriceX96);
Expand Down
60 changes: 60 additions & 0 deletions src/pairs/pair-xyeqk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import BigNumber from "bignumber.js"
import { Address, BigNumberString, Pair, Snapshot } from "../pair"

interface PairXYeqKSnapshot extends Snapshot {
fee: BigNumberString
bucketA: BigNumberString
bucketB: BigNumberString
}

export abstract class PairXYeqK extends Pair {
private fee: BigNumber = new BigNumber(0)
private bucketA: BigNumber = new BigNumber(0)
private bucketB: BigNumber = new BigNumber(0)

public refreshBuckets(fee: BigNumber, bucketA: BigNumber, bucketB: BigNumber) {
this.fee = fee
this.bucketA = bucketA
this.bucketB = bucketB
}

public outputAmount(inputToken: Address, inputAmount: BigNumber): BigNumber {
const buckets =
inputToken === this.tokenA ? [this.bucketA, this.bucketB] :
inputToken === this.tokenB ? [this.bucketB, this.bucketA] : null
if (buckets === null) {
throw new Error(`unsupported input: ${inputToken}, pair: ${this.tokenA}/${this.tokenB}!`)
}
if (this.bucketA.lt(1) || this.bucketB.lt(1)) {
return new BigNumber(0)
}
const amountWithFee = inputAmount.multipliedBy(this.fee)
const outputAmount = buckets[1].multipliedBy(amountWithFee).idiv(buckets[0].plus(amountWithFee))
return !outputAmount.isNaN() ? outputAmount : new BigNumber(0)
}

public inputAmount(outputToken: Address, outputAmount: BigNumber): BigNumber {
const buckets =
outputToken === this.tokenB ? [this.bucketA, this.bucketB] :
outputToken === this.tokenA ? [this.bucketB, this.bucketA] : null
if (buckets === null) {
throw new Error(`unsupported output: ${outputToken}, pair: ${this.tokenA}/${this.tokenB}!`)
}
return buckets[0].multipliedBy(outputAmount).idiv(
buckets[1].minus(outputAmount).multipliedBy(this.fee))
}

public snapshot(): PairXYeqKSnapshot {
return {
fee: this.fee.toFixed(),
bucketA: this.bucketA.toFixed(),
bucketB: this.bucketB.toFixed(),
}
}

public restore(snapshot: PairXYeqKSnapshot): void {
this.fee = new BigNumber(snapshot.fee)
this.bucketA = new BigNumber(snapshot.bucketA)
this.bucketB = new BigNumber(snapshot.bucketB)
}
}
3 changes: 2 additions & 1 deletion src/pairs/uniswapv2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import Web3 from "web3"
import BigNumber from "bignumber.js"

import { IUniswapV2Pair, ABI as PairABI } from "../../types/web3-v1-contracts/IUniswapV2Pair"
import { Address, PairXYeqK } from "../pair"
import { Address } from "../pair"
import { address as pairUniswapV2Address } from "../../tools/deployed/mainnet.PairUniswapV2.addr.json"
import { selectAddress } from "../utils"
import { PairXYeqK } from "./pair-xyeqk"

export class PairUniswapV2 extends PairXYeqK {
allowRepeats = false
Expand Down
2 changes: 1 addition & 1 deletion src/pairs/uniswapv3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
IUniswapV3Pool,
ABI as V3PoolABI,
} from "../../types/web3-v1-contracts/IUniswapV3Pool";
import { PairContentratedLiquidity } from "./concentrated-liquidity/pair-concentrated-liquidity";
import { PairContentratedLiquidity } from "./pair-concentrated-liquidity";
import { UniV3FeeAmount } from "../utils/concentrated-liquidity/swapMath";
import { selectAddress } from "../utils";
import { address as pairUniV3Address } from "../../tools/deployed/mainnet.PairUniswapV3.addr.json";
Expand Down
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -853,12 +853,12 @@
resolved "https://registry.yarnpkg.com/@ledgerhq/logs/-/logs-5.50.0.tgz#29c6419e8379d496ab6d0426eadf3c4d100cd186"
integrity sha512-swKHYCOZUGyVt4ge0u8a7AwNcA//h4nx5wIi0sruGye1IJ5Cva0GyK9L2/WdX+kWVTKp92ZiEo1df31lrWGPgA==

"@mento-protocol/mento-core-ts@^0.1.0", "@mento-protocol/mento-core-ts@^0.1.1":
"@mento-protocol/mento-core-ts@^0.1.1":
version "0.1.1"
resolved "https://registry.yarnpkg.com/@mento-protocol/mento-core-ts/-/mento-core-ts-0.1.1.tgz#a775db1eadd8ccf895b9b33f5a7e490628566193"
integrity sha512-3kQUqwi19sZlCFInROqSNqwaTDN5K3ZZ2K1W1BVuoNpTqhPEs26n5CWYQQPMZaEA8hAybQyQsGlQbGXpYNeinw==

"@mento-protocol/mento-sdk@^0.1.4":
"@mento-protocol/mento-sdk@^0.1.5":
version "0.1.5"
resolved "https://registry.yarnpkg.com/@mento-protocol/mento-sdk/-/mento-sdk-0.1.5.tgz#8704ada987a8f3e98601a613f81f5f9090560ab2"
integrity sha512-YEYHNMMp0wtCE1UN+83nGYoyELfdgA2URlG+CFOtHPfPS8yc/zAgnNkCI4R7ehRnX1RDRQgf9s/w0kGKsi7M9w==
Expand Down