@kittycorn-labs/v2-sdk 1.0.12 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -0
- package/dist/{constants.d.ts → cjs/src/constants.d.ts} +19 -19
- package/dist/cjs/src/constants.js +23 -0
- package/dist/cjs/src/constants.js.map +1 -0
- package/dist/{entities → cjs/src/entities}/index.d.ts +3 -3
- package/dist/cjs/src/entities/index.js +7 -0
- package/dist/cjs/src/entities/index.js.map +1 -0
- package/dist/{entities → cjs/src/entities}/pair.d.ts +147 -147
- package/dist/cjs/src/entities/pair.js +318 -0
- package/dist/cjs/src/entities/pair.js.map +1 -0
- package/dist/{entities → cjs/src/entities}/route.d.ts +12 -12
- package/dist/cjs/src/entities/route.js +45 -0
- package/dist/cjs/src/entities/route.js.map +1 -0
- package/dist/{entities → cjs/src/entities}/trade.d.ts +103 -103
- package/dist/cjs/src/entities/trade.js +263 -0
- package/dist/cjs/src/entities/trade.js.map +1 -0
- package/dist/{errors.d.ts → cjs/src/errors.d.ts} +16 -16
- package/dist/cjs/src/errors.js +34 -0
- package/dist/cjs/src/errors.js.map +1 -0
- package/dist/{index.d.ts → cjs/src/index.d.ts} +4 -4
- package/dist/cjs/src/index.js +12 -0
- package/dist/cjs/src/index.js.map +1 -0
- package/dist/{router.d.ts → cjs/src/router.d.ts} +64 -64
- package/dist/cjs/src/router.js +94 -0
- package/dist/cjs/src/router.js.map +1 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/src/constants.d.ts +19 -0
- package/dist/esm/src/constants.js +19 -0
- package/dist/esm/src/constants.js.map +1 -0
- package/dist/esm/src/entities/index.d.ts +3 -0
- package/dist/esm/src/entities/index.js +4 -0
- package/dist/esm/src/entities/index.js.map +1 -0
- package/dist/esm/src/entities/pair.d.ts +147 -0
- package/dist/esm/src/entities/pair.js +312 -0
- package/dist/esm/src/entities/pair.js.map +1 -0
- package/dist/esm/src/entities/route.d.ts +12 -0
- package/dist/esm/src/entities/route.js +40 -0
- package/dist/esm/src/entities/route.js.map +1 -0
- package/dist/esm/src/entities/trade.d.ts +103 -0
- package/dist/esm/src/entities/trade.js +256 -0
- package/dist/esm/src/entities/trade.js.map +1 -0
- package/dist/esm/src/errors.d.ts +16 -0
- package/dist/esm/src/errors.js +29 -0
- package/dist/esm/src/errors.js.map +1 -0
- package/dist/esm/src/index.d.ts +4 -0
- package/dist/esm/src/index.js +5 -0
- package/dist/esm/src/index.js.map +1 -0
- package/dist/esm/src/router.d.ts +64 -0
- package/dist/esm/src/router.js +89 -0
- package/dist/esm/src/router.js.map +1 -0
- package/dist/types/src/constants.d.ts +20 -0
- package/dist/types/src/constants.d.ts.map +1 -0
- package/dist/types/src/entities/index.d.ts +4 -0
- package/dist/types/src/entities/index.d.ts.map +1 -0
- package/dist/types/src/entities/pair.d.ts +148 -0
- package/dist/types/src/entities/pair.d.ts.map +1 -0
- package/dist/types/src/entities/route.d.ts +13 -0
- package/dist/types/src/entities/route.d.ts.map +1 -0
- package/dist/types/src/entities/trade.d.ts +104 -0
- package/dist/types/src/entities/trade.d.ts.map +1 -0
- package/dist/types/src/errors.d.ts +17 -0
- package/dist/types/src/errors.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +5 -0
- package/dist/types/src/index.d.ts.map +1 -0
- package/dist/types/src/router.d.ts +65 -0
- package/dist/types/src/router.d.ts.map +1 -0
- package/package.json +23 -34
- package/dist/index.js +0 -8
- package/dist/v2-sdk.cjs.development.js +0 -897
- package/dist/v2-sdk.cjs.development.js.map +0 -1
- package/dist/v2-sdk.cjs.production.min.js +0 -2
- package/dist/v2-sdk.cjs.production.min.js.map +0 -1
- package/dist/v2-sdk.esm.js +0 -880
- package/dist/v2-sdk.esm.js.map +0 -1
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Percent, V2_FACTORY_ADDRESSES } from '@kittycorn-labs/sdk-core';
|
|
2
|
+
import JSBI from 'jsbi';
|
|
3
|
+
/**
|
|
4
|
+
* @deprecated use FACTORY_ADDRESS_MAP instead
|
|
5
|
+
*/
|
|
6
|
+
export const FACTORY_ADDRESS = '0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f';
|
|
7
|
+
export const FACTORY_ADDRESS_MAP = V2_FACTORY_ADDRESSES;
|
|
8
|
+
export const INIT_CODE_HASH = '0x96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f';
|
|
9
|
+
export const MINIMUM_LIQUIDITY = JSBI.BigInt(1000);
|
|
10
|
+
// exports for internal consumption
|
|
11
|
+
export const ZERO = JSBI.BigInt(0);
|
|
12
|
+
export const ONE = JSBI.BigInt(1);
|
|
13
|
+
export const FIVE = JSBI.BigInt(5);
|
|
14
|
+
export const _997 = JSBI.BigInt(997);
|
|
15
|
+
export const _1000 = JSBI.BigInt(1000);
|
|
16
|
+
export const BASIS_POINTS = JSBI.BigInt(10000);
|
|
17
|
+
export const ZERO_PERCENT = new Percent(ZERO);
|
|
18
|
+
export const ONE_HUNDRED_PERCENT = new Percent(ONE);
|
|
19
|
+
//# sourceMappingURL=constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../src/constants.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,oBAAoB,EAAE,MAAM,0BAA0B,CAAA;AACxE,OAAO,IAAI,MAAM,MAAM,CAAA;AAEvB;;GAEG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,4CAA4C,CAAA;AAE3E,MAAM,CAAC,MAAM,mBAAmB,GAAkC,oBAAoB,CAAA;AAEtF,MAAM,CAAC,MAAM,cAAc,GAAG,oEAAoE,CAAA;AAElG,MAAM,CAAC,MAAM,iBAAiB,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;AAElD,mCAAmC;AACnC,MAAM,CAAC,MAAM,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAA;AAClC,MAAM,CAAC,MAAM,GAAG,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAA;AACjC,MAAM,CAAC,MAAM,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAA;AAClC,MAAM,CAAC,MAAM,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;AACpC,MAAM,CAAC,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;AACtC,MAAM,CAAC,MAAM,YAAY,GAAG,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;AAE9C,MAAM,CAAC,MAAM,YAAY,GAAG,IAAI,OAAO,CAAC,IAAI,CAAC,CAAA;AAC7C,MAAM,CAAC,MAAM,mBAAmB,GAAG,IAAI,OAAO,CAAC,GAAG,CAAC,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/entities/index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAA;AACtB,cAAc,SAAS,CAAA;AACvB,cAAc,SAAS,CAAA"}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { BigintIsh, CurrencyAmount, Price, Token } from '@kittycorn-labs/sdk-core';
|
|
2
|
+
export declare const computePairAddress: ({ factoryAddress, tokenA, tokenB, }: {
|
|
3
|
+
factoryAddress: string;
|
|
4
|
+
tokenA: Token;
|
|
5
|
+
tokenB: Token;
|
|
6
|
+
}) => string;
|
|
7
|
+
export declare class Pair {
|
|
8
|
+
readonly liquidityToken: Token;
|
|
9
|
+
private readonly tokenAmounts;
|
|
10
|
+
static getAddress(tokenA: Token, tokenB: Token): string;
|
|
11
|
+
constructor(currencyAmountA: CurrencyAmount<Token>, tokenAmountB: CurrencyAmount<Token>);
|
|
12
|
+
/**
|
|
13
|
+
* Returns true if the token is either token0 or token1
|
|
14
|
+
* @param token to check
|
|
15
|
+
*/
|
|
16
|
+
involvesToken(token: Token): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Returns the current mid price of the pair in terms of token0, i.e. the ratio of reserve1 to reserve0
|
|
19
|
+
*/
|
|
20
|
+
get token0Price(): Price<Token, Token>;
|
|
21
|
+
/**
|
|
22
|
+
* Returns the current mid price of the pair in terms of token1, i.e. the ratio of reserve0 to reserve1
|
|
23
|
+
*/
|
|
24
|
+
get token1Price(): Price<Token, Token>;
|
|
25
|
+
/**
|
|
26
|
+
* Return the price of the given token in terms of the other token in the pair.
|
|
27
|
+
* @param token token to return price of
|
|
28
|
+
*/
|
|
29
|
+
priceOf(token: Token): Price<Token, Token>;
|
|
30
|
+
/**
|
|
31
|
+
* Returns the chain ID of the tokens in the pair.
|
|
32
|
+
*/
|
|
33
|
+
get chainId(): number;
|
|
34
|
+
get token0(): Token;
|
|
35
|
+
get token1(): Token;
|
|
36
|
+
get reserve0(): CurrencyAmount<Token>;
|
|
37
|
+
get reserve1(): CurrencyAmount<Token>;
|
|
38
|
+
reserveOf(token: Token): CurrencyAmount<Token>;
|
|
39
|
+
/**
|
|
40
|
+
* getAmountOut is the linear algebra of reserve ratio against amountIn:amountOut.
|
|
41
|
+
* https://ethereum.stackexchange.com/questions/101629/what-is-math-for-uniswap-calculates-the-amountout-and-amountin-why-997-and-1000
|
|
42
|
+
* has the math deduction for the reserve calculation without fee-on-transfer fees.
|
|
43
|
+
*
|
|
44
|
+
* With fee-on-transfer tax, intuitively it's just:
|
|
45
|
+
* inputAmountWithFeeAndTax = 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn
|
|
46
|
+
* = (1 - amountIn.sellFeesBips / 10000) * amountInWithFee
|
|
47
|
+
* where amountInWithFee is the amountIn after taking out the LP fees
|
|
48
|
+
* outputAmountWithTax = amountOut * (1 - amountOut.buyFeesBips / 10000)
|
|
49
|
+
*
|
|
50
|
+
* But we are illustrating the math deduction below to ensure that's the case.
|
|
51
|
+
*
|
|
52
|
+
* before swap A * B = K where A = reserveIn B = reserveOut
|
|
53
|
+
*
|
|
54
|
+
* after swap A' * B' = K where only k is a constant value
|
|
55
|
+
*
|
|
56
|
+
* getAmountOut
|
|
57
|
+
*
|
|
58
|
+
* A' = A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn # here 0.3% is deducted
|
|
59
|
+
* B' = B - amountOut * (1 - amountOut.buyFeesBips / 10000)
|
|
60
|
+
* amountOut = (B - B') / (1 - amountOut.buyFeesBips / 10000) # where A' * B' still is k
|
|
61
|
+
* = (B - K/(A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn))
|
|
62
|
+
* /
|
|
63
|
+
* (1 - amountOut.buyFeesBips / 10000)
|
|
64
|
+
* = (B - AB/(A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn))
|
|
65
|
+
* /
|
|
66
|
+
* (1 - amountOut.buyFeesBips / 10000)
|
|
67
|
+
* = ((BA + B * 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn - AB)/(A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn))
|
|
68
|
+
* /
|
|
69
|
+
* (1 - amountOut.buyFeesBips / 10000)
|
|
70
|
+
* = (B * 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn / (A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn)
|
|
71
|
+
* /
|
|
72
|
+
* (1 - amountOut.buyFeesBips / 10000)
|
|
73
|
+
* amountOut * (1 - amountOut.buyFeesBips / 10000) = (B * 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn
|
|
74
|
+
* /
|
|
75
|
+
* (A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn)
|
|
76
|
+
*
|
|
77
|
+
* outputAmountWithTax = (B * 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn
|
|
78
|
+
* /
|
|
79
|
+
* (A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn)
|
|
80
|
+
* = (B * 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn * 1000
|
|
81
|
+
* /
|
|
82
|
+
* ((A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn) * 1000)
|
|
83
|
+
* = (B * (1 - amountIn.sellFeesBips / 10000) 997 * * amountIn
|
|
84
|
+
* /
|
|
85
|
+
* (1000 * A + (1 - amountIn.sellFeesBips / 10000) * 997 * amountIn)
|
|
86
|
+
* = (B * (1 - amountIn.sellFeesBips / 10000) * inputAmountWithFee)
|
|
87
|
+
* /
|
|
88
|
+
* (1000 * A + (1 - amountIn.sellFeesBips / 10000) * inputAmountWithFee)
|
|
89
|
+
* = (B * inputAmountWithFeeAndTax)
|
|
90
|
+
* /
|
|
91
|
+
* (1000 * A + inputAmountWithFeeAndTax)
|
|
92
|
+
*
|
|
93
|
+
* inputAmountWithFeeAndTax = (1 - amountIn.sellFeesBips / 10000) * inputAmountWithFee
|
|
94
|
+
* outputAmountWithTax = amountOut * (1 - amountOut.buyFeesBips / 10000)
|
|
95
|
+
*
|
|
96
|
+
* @param inputAmount
|
|
97
|
+
* @param calculateFotFees
|
|
98
|
+
*/
|
|
99
|
+
getOutputAmount(inputAmount: CurrencyAmount<Token>, calculateFotFees?: boolean): [CurrencyAmount<Token>, Pair];
|
|
100
|
+
/**
|
|
101
|
+
* getAmountIn is the linear algebra of reserve ratio against amountIn:amountOut.
|
|
102
|
+
* https://ethereum.stackexchange.com/questions/101629/what-is-math-for-uniswap-calculates-the-amountout-and-amountin-why-997-and-1000
|
|
103
|
+
* has the math deduction for the reserve calculation without fee-on-transfer fees.
|
|
104
|
+
*
|
|
105
|
+
* With fee-on-transfer fees, intuitively it's just:
|
|
106
|
+
* outputAmountWithTax = amountOut / (1 - amountOut.buyFeesBips / 10000)
|
|
107
|
+
* inputAmountWithTax = amountIn / (1 - amountIn.sellFeesBips / 10000) / 0.997
|
|
108
|
+
*
|
|
109
|
+
* But we are illustrating the math deduction below to ensure that's the case.
|
|
110
|
+
*
|
|
111
|
+
* before swap A * B = K where A = reserveIn B = reserveOut
|
|
112
|
+
*
|
|
113
|
+
* after swap A' * B' = K where only k is a constant value
|
|
114
|
+
*
|
|
115
|
+
* getAmountIn
|
|
116
|
+
*
|
|
117
|
+
* B' = B - amountOut * (1 - amountOut.buyFeesBips / 10000)
|
|
118
|
+
* A' = A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn # here 0.3% is deducted
|
|
119
|
+
* amountIn = (A' - A) / (0.997 * (1 - amountIn.sellFeesBips / 10000))
|
|
120
|
+
* = (K / (B - amountOut / (1 - amountOut.buyFeesBips / 10000)) - A)
|
|
121
|
+
* /
|
|
122
|
+
* (0.997 * (1 - amountIn.sellFeesBips / 10000))
|
|
123
|
+
* = (AB / (B - amountOut / (1 - amountOut.buyFeesBips / 10000)) - A)
|
|
124
|
+
* /
|
|
125
|
+
* (0.997 * (1 - amountIn.sellFeesBips / 10000))
|
|
126
|
+
* = ((AB - AB + A * amountOut / (1 - amountOut.buyFeesBips / 10000)) / (B - amountOut / (1 - amountOut.buyFeesBips / 10000)))
|
|
127
|
+
* /
|
|
128
|
+
* (0.997 * (1 - amountIn.sellFeesBips / 10000))
|
|
129
|
+
* = ((A * amountOut / (1 - amountOut.buyFeesBips / 10000)) / (B - amountOut / (1 - amountOut.buyFeesBips / 10000)))
|
|
130
|
+
* /
|
|
131
|
+
* (0.997 * (1 - amountIn.sellFeesBips / 10000))
|
|
132
|
+
* = ((A * 1000 * amountOut / (1 - amountOut.buyFeesBips / 10000)) / (B - amountOut / (1 - amountOut.buyFeesBips / 10000)))
|
|
133
|
+
* /
|
|
134
|
+
* (997 * (1 - amountIn.sellFeesBips / 10000))
|
|
135
|
+
*
|
|
136
|
+
* outputAmountWithTax = amountOut / (1 - amountOut.buyFeesBips / 10000)
|
|
137
|
+
* inputAmountWithTax = amountIn / (997 * (1 - amountIn.sellFeesBips / 10000))
|
|
138
|
+
* = (A * outputAmountWithTax * 1000) / ((B - outputAmountWithTax) * 997)
|
|
139
|
+
*
|
|
140
|
+
* @param outputAmount
|
|
141
|
+
*/
|
|
142
|
+
getInputAmount(outputAmount: CurrencyAmount<Token>, calculateFotFees?: boolean): [CurrencyAmount<Token>, Pair];
|
|
143
|
+
getLiquidityMinted(totalSupply: CurrencyAmount<Token>, tokenAmountA: CurrencyAmount<Token>, tokenAmountB: CurrencyAmount<Token>): CurrencyAmount<Token>;
|
|
144
|
+
getLiquidityValue(token: Token, totalSupply: CurrencyAmount<Token>, liquidity: CurrencyAmount<Token>, feeOn?: boolean, kLast?: BigintIsh): CurrencyAmount<Token>;
|
|
145
|
+
private derivePercentAfterSellFees;
|
|
146
|
+
private derivePercentAfterBuyFees;
|
|
147
|
+
}
|
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
import { getCreate2Address } from '@ethersproject/address';
|
|
2
|
+
import { BigNumber } from '@ethersproject/bignumber';
|
|
3
|
+
import { keccak256, pack } from '@ethersproject/solidity';
|
|
4
|
+
import { CurrencyAmount, Percent, Price, sqrt, Token } from '@kittycorn-labs/sdk-core';
|
|
5
|
+
import JSBI from 'jsbi';
|
|
6
|
+
import invariant from 'tiny-invariant';
|
|
7
|
+
import { _1000, _997, BASIS_POINTS, FACTORY_ADDRESS, FACTORY_ADDRESS_MAP, FIVE, INIT_CODE_HASH, MINIMUM_LIQUIDITY, ONE, ONE_HUNDRED_PERCENT, ZERO, ZERO_PERCENT, } from '../constants.js';
|
|
8
|
+
import { InsufficientInputAmountError, InsufficientReservesError } from '../errors.js';
|
|
9
|
+
export const computePairAddress = ({ factoryAddress, tokenA, tokenB, }) => {
|
|
10
|
+
const [token0, token1] = tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA]; // does safety checks
|
|
11
|
+
return getCreate2Address(factoryAddress, keccak256(['bytes'], [pack(['address', 'address'], [token0.address, token1.address])]), INIT_CODE_HASH);
|
|
12
|
+
};
|
|
13
|
+
export class Pair {
|
|
14
|
+
static getAddress(tokenA, tokenB) {
|
|
15
|
+
var _a;
|
|
16
|
+
const factoryAddress = (_a = FACTORY_ADDRESS_MAP[tokenA.chainId]) !== null && _a !== void 0 ? _a : FACTORY_ADDRESS;
|
|
17
|
+
return computePairAddress({ factoryAddress, tokenA, tokenB });
|
|
18
|
+
}
|
|
19
|
+
constructor(currencyAmountA, tokenAmountB) {
|
|
20
|
+
const tokenAmounts = currencyAmountA.currency.sortsBefore(tokenAmountB.currency) // does safety checks
|
|
21
|
+
? [currencyAmountA, tokenAmountB]
|
|
22
|
+
: [tokenAmountB, currencyAmountA];
|
|
23
|
+
this.liquidityToken = new Token(tokenAmounts[0].currency.chainId, Pair.getAddress(tokenAmounts[0].currency, tokenAmounts[1].currency), 18, 'UNI-V2', 'Uniswap V2');
|
|
24
|
+
this.tokenAmounts = tokenAmounts;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Returns true if the token is either token0 or token1
|
|
28
|
+
* @param token to check
|
|
29
|
+
*/
|
|
30
|
+
involvesToken(token) {
|
|
31
|
+
return token.equals(this.token0) || token.equals(this.token1);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Returns the current mid price of the pair in terms of token0, i.e. the ratio of reserve1 to reserve0
|
|
35
|
+
*/
|
|
36
|
+
get token0Price() {
|
|
37
|
+
const result = this.tokenAmounts[1].divide(this.tokenAmounts[0]);
|
|
38
|
+
return new Price(this.token0, this.token1, result.denominator, result.numerator);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Returns the current mid price of the pair in terms of token1, i.e. the ratio of reserve0 to reserve1
|
|
42
|
+
*/
|
|
43
|
+
get token1Price() {
|
|
44
|
+
const result = this.tokenAmounts[0].divide(this.tokenAmounts[1]);
|
|
45
|
+
return new Price(this.token1, this.token0, result.denominator, result.numerator);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Return the price of the given token in terms of the other token in the pair.
|
|
49
|
+
* @param token token to return price of
|
|
50
|
+
*/
|
|
51
|
+
priceOf(token) {
|
|
52
|
+
invariant(this.involvesToken(token), 'TOKEN');
|
|
53
|
+
return token.equals(this.token0) ? this.token0Price : this.token1Price;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Returns the chain ID of the tokens in the pair.
|
|
57
|
+
*/
|
|
58
|
+
get chainId() {
|
|
59
|
+
return this.token0.chainId;
|
|
60
|
+
}
|
|
61
|
+
get token0() {
|
|
62
|
+
return this.tokenAmounts[0].currency;
|
|
63
|
+
}
|
|
64
|
+
get token1() {
|
|
65
|
+
return this.tokenAmounts[1].currency;
|
|
66
|
+
}
|
|
67
|
+
get reserve0() {
|
|
68
|
+
return this.tokenAmounts[0];
|
|
69
|
+
}
|
|
70
|
+
get reserve1() {
|
|
71
|
+
return this.tokenAmounts[1];
|
|
72
|
+
}
|
|
73
|
+
reserveOf(token) {
|
|
74
|
+
invariant(this.involvesToken(token), 'TOKEN');
|
|
75
|
+
return token.equals(this.token0) ? this.reserve0 : this.reserve1;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* getAmountOut is the linear algebra of reserve ratio against amountIn:amountOut.
|
|
79
|
+
* https://ethereum.stackexchange.com/questions/101629/what-is-math-for-uniswap-calculates-the-amountout-and-amountin-why-997-and-1000
|
|
80
|
+
* has the math deduction for the reserve calculation without fee-on-transfer fees.
|
|
81
|
+
*
|
|
82
|
+
* With fee-on-transfer tax, intuitively it's just:
|
|
83
|
+
* inputAmountWithFeeAndTax = 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn
|
|
84
|
+
* = (1 - amountIn.sellFeesBips / 10000) * amountInWithFee
|
|
85
|
+
* where amountInWithFee is the amountIn after taking out the LP fees
|
|
86
|
+
* outputAmountWithTax = amountOut * (1 - amountOut.buyFeesBips / 10000)
|
|
87
|
+
*
|
|
88
|
+
* But we are illustrating the math deduction below to ensure that's the case.
|
|
89
|
+
*
|
|
90
|
+
* before swap A * B = K where A = reserveIn B = reserveOut
|
|
91
|
+
*
|
|
92
|
+
* after swap A' * B' = K where only k is a constant value
|
|
93
|
+
*
|
|
94
|
+
* getAmountOut
|
|
95
|
+
*
|
|
96
|
+
* A' = A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn # here 0.3% is deducted
|
|
97
|
+
* B' = B - amountOut * (1 - amountOut.buyFeesBips / 10000)
|
|
98
|
+
* amountOut = (B - B') / (1 - amountOut.buyFeesBips / 10000) # where A' * B' still is k
|
|
99
|
+
* = (B - K/(A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn))
|
|
100
|
+
* /
|
|
101
|
+
* (1 - amountOut.buyFeesBips / 10000)
|
|
102
|
+
* = (B - AB/(A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn))
|
|
103
|
+
* /
|
|
104
|
+
* (1 - amountOut.buyFeesBips / 10000)
|
|
105
|
+
* = ((BA + B * 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn - AB)/(A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn))
|
|
106
|
+
* /
|
|
107
|
+
* (1 - amountOut.buyFeesBips / 10000)
|
|
108
|
+
* = (B * 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn / (A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn)
|
|
109
|
+
* /
|
|
110
|
+
* (1 - amountOut.buyFeesBips / 10000)
|
|
111
|
+
* amountOut * (1 - amountOut.buyFeesBips / 10000) = (B * 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn
|
|
112
|
+
* /
|
|
113
|
+
* (A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn)
|
|
114
|
+
*
|
|
115
|
+
* outputAmountWithTax = (B * 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn
|
|
116
|
+
* /
|
|
117
|
+
* (A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn)
|
|
118
|
+
* = (B * 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn * 1000
|
|
119
|
+
* /
|
|
120
|
+
* ((A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn) * 1000)
|
|
121
|
+
* = (B * (1 - amountIn.sellFeesBips / 10000) 997 * * amountIn
|
|
122
|
+
* /
|
|
123
|
+
* (1000 * A + (1 - amountIn.sellFeesBips / 10000) * 997 * amountIn)
|
|
124
|
+
* = (B * (1 - amountIn.sellFeesBips / 10000) * inputAmountWithFee)
|
|
125
|
+
* /
|
|
126
|
+
* (1000 * A + (1 - amountIn.sellFeesBips / 10000) * inputAmountWithFee)
|
|
127
|
+
* = (B * inputAmountWithFeeAndTax)
|
|
128
|
+
* /
|
|
129
|
+
* (1000 * A + inputAmountWithFeeAndTax)
|
|
130
|
+
*
|
|
131
|
+
* inputAmountWithFeeAndTax = (1 - amountIn.sellFeesBips / 10000) * inputAmountWithFee
|
|
132
|
+
* outputAmountWithTax = amountOut * (1 - amountOut.buyFeesBips / 10000)
|
|
133
|
+
*
|
|
134
|
+
* @param inputAmount
|
|
135
|
+
* @param calculateFotFees
|
|
136
|
+
*/
|
|
137
|
+
getOutputAmount(inputAmount, calculateFotFees = true) {
|
|
138
|
+
invariant(this.involvesToken(inputAmount.currency), 'TOKEN');
|
|
139
|
+
if (JSBI.equal(this.reserve0.quotient, ZERO) || JSBI.equal(this.reserve1.quotient, ZERO)) {
|
|
140
|
+
throw new InsufficientReservesError();
|
|
141
|
+
}
|
|
142
|
+
const inputReserve = this.reserveOf(inputAmount.currency);
|
|
143
|
+
const outputReserve = this.reserveOf(inputAmount.currency.equals(this.token0) ? this.token1 : this.token0);
|
|
144
|
+
const percentAfterSellFees = calculateFotFees ? this.derivePercentAfterSellFees(inputAmount) : ZERO_PERCENT;
|
|
145
|
+
const inputAmountAfterTax = percentAfterSellFees.greaterThan(ZERO_PERCENT)
|
|
146
|
+
? CurrencyAmount.fromRawAmount(inputAmount.currency, percentAfterSellFees.multiply(inputAmount).quotient // fraction.quotient will round down by itself, which is desired
|
|
147
|
+
)
|
|
148
|
+
: inputAmount;
|
|
149
|
+
const inputAmountWithFeeAndAfterTax = JSBI.multiply(inputAmountAfterTax.quotient, _997);
|
|
150
|
+
const numerator = JSBI.multiply(inputAmountWithFeeAndAfterTax, outputReserve.quotient);
|
|
151
|
+
const denominator = JSBI.add(JSBI.multiply(inputReserve.quotient, _1000), inputAmountWithFeeAndAfterTax);
|
|
152
|
+
const outputAmount = CurrencyAmount.fromRawAmount(inputAmount.currency.equals(this.token0) ? this.token1 : this.token0, JSBI.divide(numerator, denominator) // JSBI.divide will round down by itself, which is desired
|
|
153
|
+
);
|
|
154
|
+
if (JSBI.equal(outputAmount.quotient, ZERO)) {
|
|
155
|
+
throw new InsufficientInputAmountError();
|
|
156
|
+
}
|
|
157
|
+
const percentAfterBuyFees = calculateFotFees ? this.derivePercentAfterBuyFees(outputAmount) : ZERO_PERCENT;
|
|
158
|
+
const outputAmountAfterTax = percentAfterBuyFees.greaterThan(ZERO_PERCENT)
|
|
159
|
+
? CurrencyAmount.fromRawAmount(outputAmount.currency, outputAmount.multiply(percentAfterBuyFees).quotient // fraction.quotient will round down by itself, which is desired
|
|
160
|
+
)
|
|
161
|
+
: outputAmount;
|
|
162
|
+
if (JSBI.equal(outputAmountAfterTax.quotient, ZERO)) {
|
|
163
|
+
throw new InsufficientInputAmountError();
|
|
164
|
+
}
|
|
165
|
+
return [
|
|
166
|
+
outputAmountAfterTax,
|
|
167
|
+
new Pair(inputReserve.add(inputAmountAfterTax), outputReserve.subtract(outputAmountAfterTax)),
|
|
168
|
+
];
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* getAmountIn is the linear algebra of reserve ratio against amountIn:amountOut.
|
|
172
|
+
* https://ethereum.stackexchange.com/questions/101629/what-is-math-for-uniswap-calculates-the-amountout-and-amountin-why-997-and-1000
|
|
173
|
+
* has the math deduction for the reserve calculation without fee-on-transfer fees.
|
|
174
|
+
*
|
|
175
|
+
* With fee-on-transfer fees, intuitively it's just:
|
|
176
|
+
* outputAmountWithTax = amountOut / (1 - amountOut.buyFeesBips / 10000)
|
|
177
|
+
* inputAmountWithTax = amountIn / (1 - amountIn.sellFeesBips / 10000) / 0.997
|
|
178
|
+
*
|
|
179
|
+
* But we are illustrating the math deduction below to ensure that's the case.
|
|
180
|
+
*
|
|
181
|
+
* before swap A * B = K where A = reserveIn B = reserveOut
|
|
182
|
+
*
|
|
183
|
+
* after swap A' * B' = K where only k is a constant value
|
|
184
|
+
*
|
|
185
|
+
* getAmountIn
|
|
186
|
+
*
|
|
187
|
+
* B' = B - amountOut * (1 - amountOut.buyFeesBips / 10000)
|
|
188
|
+
* A' = A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn # here 0.3% is deducted
|
|
189
|
+
* amountIn = (A' - A) / (0.997 * (1 - amountIn.sellFeesBips / 10000))
|
|
190
|
+
* = (K / (B - amountOut / (1 - amountOut.buyFeesBips / 10000)) - A)
|
|
191
|
+
* /
|
|
192
|
+
* (0.997 * (1 - amountIn.sellFeesBips / 10000))
|
|
193
|
+
* = (AB / (B - amountOut / (1 - amountOut.buyFeesBips / 10000)) - A)
|
|
194
|
+
* /
|
|
195
|
+
* (0.997 * (1 - amountIn.sellFeesBips / 10000))
|
|
196
|
+
* = ((AB - AB + A * amountOut / (1 - amountOut.buyFeesBips / 10000)) / (B - amountOut / (1 - amountOut.buyFeesBips / 10000)))
|
|
197
|
+
* /
|
|
198
|
+
* (0.997 * (1 - amountIn.sellFeesBips / 10000))
|
|
199
|
+
* = ((A * amountOut / (1 - amountOut.buyFeesBips / 10000)) / (B - amountOut / (1 - amountOut.buyFeesBips / 10000)))
|
|
200
|
+
* /
|
|
201
|
+
* (0.997 * (1 - amountIn.sellFeesBips / 10000))
|
|
202
|
+
* = ((A * 1000 * amountOut / (1 - amountOut.buyFeesBips / 10000)) / (B - amountOut / (1 - amountOut.buyFeesBips / 10000)))
|
|
203
|
+
* /
|
|
204
|
+
* (997 * (1 - amountIn.sellFeesBips / 10000))
|
|
205
|
+
*
|
|
206
|
+
* outputAmountWithTax = amountOut / (1 - amountOut.buyFeesBips / 10000)
|
|
207
|
+
* inputAmountWithTax = amountIn / (997 * (1 - amountIn.sellFeesBips / 10000))
|
|
208
|
+
* = (A * outputAmountWithTax * 1000) / ((B - outputAmountWithTax) * 997)
|
|
209
|
+
*
|
|
210
|
+
* @param outputAmount
|
|
211
|
+
*/
|
|
212
|
+
getInputAmount(outputAmount, calculateFotFees = true) {
|
|
213
|
+
invariant(this.involvesToken(outputAmount.currency), 'TOKEN');
|
|
214
|
+
const percentAfterBuyFees = calculateFotFees ? this.derivePercentAfterBuyFees(outputAmount) : ZERO_PERCENT;
|
|
215
|
+
const outputAmountBeforeTax = percentAfterBuyFees.greaterThan(ZERO_PERCENT)
|
|
216
|
+
? CurrencyAmount.fromRawAmount(outputAmount.currency, JSBI.add(outputAmount.divide(percentAfterBuyFees).quotient, ONE) // add 1 for rounding up
|
|
217
|
+
)
|
|
218
|
+
: outputAmount;
|
|
219
|
+
if (JSBI.equal(this.reserve0.quotient, ZERO) ||
|
|
220
|
+
JSBI.equal(this.reserve1.quotient, ZERO) ||
|
|
221
|
+
JSBI.greaterThanOrEqual(outputAmount.quotient, this.reserveOf(outputAmount.currency).quotient) ||
|
|
222
|
+
JSBI.greaterThanOrEqual(outputAmountBeforeTax.quotient, this.reserveOf(outputAmount.currency).quotient)) {
|
|
223
|
+
throw new InsufficientReservesError();
|
|
224
|
+
}
|
|
225
|
+
const outputReserve = this.reserveOf(outputAmount.currency);
|
|
226
|
+
const inputReserve = this.reserveOf(outputAmount.currency.equals(this.token0) ? this.token1 : this.token0);
|
|
227
|
+
const numerator = JSBI.multiply(JSBI.multiply(inputReserve.quotient, outputAmountBeforeTax.quotient), _1000);
|
|
228
|
+
const denominator = JSBI.multiply(JSBI.subtract(outputReserve.quotient, outputAmountBeforeTax.quotient), _997);
|
|
229
|
+
const inputAmount = CurrencyAmount.fromRawAmount(outputAmount.currency.equals(this.token0) ? this.token1 : this.token0, JSBI.add(JSBI.divide(numerator, denominator), ONE) // add 1 here is part of the formula, no rounding needed here, since there will not be decimal at this point
|
|
230
|
+
);
|
|
231
|
+
const percentAfterSellFees = calculateFotFees ? this.derivePercentAfterSellFees(inputAmount) : ZERO_PERCENT;
|
|
232
|
+
const inputAmountBeforeTax = percentAfterSellFees.greaterThan(ZERO_PERCENT)
|
|
233
|
+
? CurrencyAmount.fromRawAmount(inputAmount.currency, JSBI.add(inputAmount.divide(percentAfterSellFees).quotient, ONE) // add 1 for rounding up
|
|
234
|
+
)
|
|
235
|
+
: inputAmount;
|
|
236
|
+
return [inputAmountBeforeTax, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))];
|
|
237
|
+
}
|
|
238
|
+
getLiquidityMinted(totalSupply, tokenAmountA, tokenAmountB) {
|
|
239
|
+
invariant(totalSupply.currency.equals(this.liquidityToken), 'LIQUIDITY');
|
|
240
|
+
const tokenAmounts = tokenAmountA.currency.sortsBefore(tokenAmountB.currency) // does safety checks
|
|
241
|
+
? [tokenAmountA, tokenAmountB]
|
|
242
|
+
: [tokenAmountB, tokenAmountA];
|
|
243
|
+
invariant(tokenAmounts[0].currency.equals(this.token0) && tokenAmounts[1].currency.equals(this.token1), 'TOKEN');
|
|
244
|
+
let liquidity;
|
|
245
|
+
if (JSBI.equal(totalSupply.quotient, ZERO)) {
|
|
246
|
+
liquidity = JSBI.subtract(sqrt(JSBI.multiply(tokenAmounts[0].quotient, tokenAmounts[1].quotient)), MINIMUM_LIQUIDITY);
|
|
247
|
+
}
|
|
248
|
+
else {
|
|
249
|
+
const amount0 = JSBI.divide(JSBI.multiply(tokenAmounts[0].quotient, totalSupply.quotient), this.reserve0.quotient);
|
|
250
|
+
const amount1 = JSBI.divide(JSBI.multiply(tokenAmounts[1].quotient, totalSupply.quotient), this.reserve1.quotient);
|
|
251
|
+
liquidity = JSBI.lessThanOrEqual(amount0, amount1) ? amount0 : amount1;
|
|
252
|
+
}
|
|
253
|
+
if (!JSBI.greaterThan(liquidity, ZERO)) {
|
|
254
|
+
throw new InsufficientInputAmountError();
|
|
255
|
+
}
|
|
256
|
+
return CurrencyAmount.fromRawAmount(this.liquidityToken, liquidity);
|
|
257
|
+
}
|
|
258
|
+
getLiquidityValue(token, totalSupply, liquidity, feeOn = false, kLast) {
|
|
259
|
+
invariant(this.involvesToken(token), 'TOKEN');
|
|
260
|
+
invariant(totalSupply.currency.equals(this.liquidityToken), 'TOTAL_SUPPLY');
|
|
261
|
+
invariant(liquidity.currency.equals(this.liquidityToken), 'LIQUIDITY');
|
|
262
|
+
invariant(JSBI.lessThanOrEqual(liquidity.quotient, totalSupply.quotient), 'LIQUIDITY');
|
|
263
|
+
let totalSupplyAdjusted;
|
|
264
|
+
if (!feeOn) {
|
|
265
|
+
totalSupplyAdjusted = totalSupply;
|
|
266
|
+
}
|
|
267
|
+
else {
|
|
268
|
+
invariant(!!kLast, 'K_LAST');
|
|
269
|
+
const kLastParsed = JSBI.BigInt(kLast);
|
|
270
|
+
if (!JSBI.equal(kLastParsed, ZERO)) {
|
|
271
|
+
const rootK = sqrt(JSBI.multiply(this.reserve0.quotient, this.reserve1.quotient));
|
|
272
|
+
const rootKLast = sqrt(kLastParsed);
|
|
273
|
+
if (JSBI.greaterThan(rootK, rootKLast)) {
|
|
274
|
+
const numerator = JSBI.multiply(totalSupply.quotient, JSBI.subtract(rootK, rootKLast));
|
|
275
|
+
const denominator = JSBI.add(JSBI.multiply(rootK, FIVE), rootKLast);
|
|
276
|
+
const feeLiquidity = JSBI.divide(numerator, denominator);
|
|
277
|
+
totalSupplyAdjusted = totalSupply.add(CurrencyAmount.fromRawAmount(this.liquidityToken, feeLiquidity));
|
|
278
|
+
}
|
|
279
|
+
else {
|
|
280
|
+
totalSupplyAdjusted = totalSupply;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
else {
|
|
284
|
+
totalSupplyAdjusted = totalSupply;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
return CurrencyAmount.fromRawAmount(token, JSBI.divide(JSBI.multiply(liquidity.quotient, this.reserveOf(token).quotient), totalSupplyAdjusted.quotient));
|
|
288
|
+
}
|
|
289
|
+
derivePercentAfterSellFees(inputAmount) {
|
|
290
|
+
const sellFeeBips = this.token0.wrapped.equals(inputAmount.wrapped.currency)
|
|
291
|
+
? this.token0.wrapped.sellFeeBps
|
|
292
|
+
: this.token1.wrapped.sellFeeBps;
|
|
293
|
+
if (sellFeeBips === null || sellFeeBips === void 0 ? void 0 : sellFeeBips.gt(BigNumber.from(0))) {
|
|
294
|
+
return ONE_HUNDRED_PERCENT.subtract(new Percent(JSBI.BigInt(sellFeeBips)).divide(BASIS_POINTS));
|
|
295
|
+
}
|
|
296
|
+
else {
|
|
297
|
+
return ZERO_PERCENT;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
derivePercentAfterBuyFees(outputAmount) {
|
|
301
|
+
const buyFeeBps = this.token0.wrapped.equals(outputAmount.wrapped.currency)
|
|
302
|
+
? this.token0.wrapped.buyFeeBps
|
|
303
|
+
: this.token1.wrapped.buyFeeBps;
|
|
304
|
+
if (buyFeeBps === null || buyFeeBps === void 0 ? void 0 : buyFeeBps.gt(BigNumber.from(0))) {
|
|
305
|
+
return ONE_HUNDRED_PERCENT.subtract(new Percent(JSBI.BigInt(buyFeeBps)).divide(BASIS_POINTS));
|
|
306
|
+
}
|
|
307
|
+
else {
|
|
308
|
+
return ZERO_PERCENT;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
//# sourceMappingURL=pair.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pair.js","sourceRoot":"","sources":["../../../../src/entities/pair.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAA;AAC1D,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAA;AACpD,OAAO,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,yBAAyB,CAAA;AACzD,OAAO,EAAa,cAAc,EAAE,OAAO,EAAE,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AAI,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC,CAAA;SAChG;aAAM;YACL,OAAO,YAAY,CAAA;SACpB;IACH,CAAC;IAEO,yBAAyB,CAAC,YAAmC;QACnE,MAAM,SAAS,GAAG,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,YAAY,CAAC,OAAO,CAAC,QAAQ,CAAC;YACzE,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,SAAS;YAC/B,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,SAAS,CAAA;QACjC,IAAI,SAAS,aAAT,SAAS,uBAAT,SAAS,CAAE,EAAE,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE;YACpC,OAAO,mBAAmB,CAAC,QAAQ,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC,CAAA;SAC9F;aAAM;YACL,OAAO,YAAY,CAAA;SACpB;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Currency, Price, Token } from '@kittycorn-labs/sdk-core';
|
|
2
|
+
import { Pair } from './pair';
|
|
3
|
+
export declare class Route<TInput extends Currency, TOutput extends Currency> {
|
|
4
|
+
readonly pairs: Pair[];
|
|
5
|
+
readonly path: Token[];
|
|
6
|
+
readonly input: TInput;
|
|
7
|
+
readonly output: TOutput;
|
|
8
|
+
constructor(pairs: Pair[], input: TInput, output: TOutput);
|
|
9
|
+
private _midPrice;
|
|
10
|
+
get midPrice(): Price<TInput, TOutput>;
|
|
11
|
+
get chainId(): number;
|
|
12
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import invariant from 'tiny-invariant';
|
|
2
|
+
import { Price } from '@kittycorn-labs/sdk-core';
|
|
3
|
+
export class Route {
|
|
4
|
+
constructor(pairs, input, output) {
|
|
5
|
+
this._midPrice = null;
|
|
6
|
+
invariant(pairs.length > 0, 'PAIRS');
|
|
7
|
+
const chainId = pairs[0].chainId;
|
|
8
|
+
invariant(pairs.every((pair) => pair.chainId === chainId), 'CHAIN_IDS');
|
|
9
|
+
const wrappedInput = input.wrapped;
|
|
10
|
+
invariant(pairs[0].involvesToken(wrappedInput), 'INPUT');
|
|
11
|
+
invariant(typeof output === 'undefined' || pairs[pairs.length - 1].involvesToken(output.wrapped), 'OUTPUT');
|
|
12
|
+
const path = [wrappedInput];
|
|
13
|
+
for (const [i, pair] of pairs.entries()) {
|
|
14
|
+
const currentInput = path[i];
|
|
15
|
+
invariant(currentInput.equals(pair.token0) || currentInput.equals(pair.token1), 'PATH');
|
|
16
|
+
const output = currentInput.equals(pair.token0) ? pair.token1 : pair.token0;
|
|
17
|
+
path.push(output);
|
|
18
|
+
}
|
|
19
|
+
this.pairs = pairs;
|
|
20
|
+
this.path = path;
|
|
21
|
+
this.input = input;
|
|
22
|
+
this.output = output;
|
|
23
|
+
}
|
|
24
|
+
get midPrice() {
|
|
25
|
+
if (this._midPrice !== null)
|
|
26
|
+
return this._midPrice;
|
|
27
|
+
const prices = [];
|
|
28
|
+
for (const [i, pair] of this.pairs.entries()) {
|
|
29
|
+
prices.push(this.path[i].equals(pair.token0)
|
|
30
|
+
? new Price(pair.reserve0.currency, pair.reserve1.currency, pair.reserve0.quotient, pair.reserve1.quotient)
|
|
31
|
+
: new Price(pair.reserve1.currency, pair.reserve0.currency, pair.reserve1.quotient, pair.reserve0.quotient));
|
|
32
|
+
}
|
|
33
|
+
const reduced = prices.slice(1).reduce((accumulator, currentValue) => accumulator.multiply(currentValue), prices[0]);
|
|
34
|
+
return (this._midPrice = new Price(this.input, this.output, reduced.denominator, reduced.numerator));
|
|
35
|
+
}
|
|
36
|
+
get chainId() {
|
|
37
|
+
return this.pairs[0].chainId;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
//# sourceMapp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