@centurion_project/v2-sdk 4.20.0-ctn.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.
Files changed (59) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +6 -0
  3. package/dist/cjs/src/constants.d.ts +19 -0
  4. package/dist/cjs/src/constants.js +23 -0
  5. package/dist/cjs/src/constants.js.map +1 -0
  6. package/dist/cjs/src/entities/index.d.ts +3 -0
  7. package/dist/cjs/src/entities/index.js +7 -0
  8. package/dist/cjs/src/entities/index.js.map +1 -0
  9. package/dist/cjs/src/entities/pair.d.ts +147 -0
  10. package/dist/cjs/src/entities/pair.js +318 -0
  11. package/dist/cjs/src/entities/pair.js.map +1 -0
  12. package/dist/cjs/src/entities/route.d.ts +12 -0
  13. package/dist/cjs/src/entities/route.js +45 -0
  14. package/dist/cjs/src/entities/route.js.map +1 -0
  15. package/dist/cjs/src/entities/trade.d.ts +103 -0
  16. package/dist/cjs/src/entities/trade.js +260 -0
  17. package/dist/cjs/src/entities/trade.js.map +1 -0
  18. package/dist/cjs/src/errors.d.ts +16 -0
  19. package/dist/cjs/src/errors.js +34 -0
  20. package/dist/cjs/src/errors.js.map +1 -0
  21. package/dist/cjs/src/index.d.ts +4 -0
  22. package/dist/cjs/src/index.js +12 -0
  23. package/dist/cjs/src/index.js.map +1 -0
  24. package/dist/cjs/src/router.d.ts +64 -0
  25. package/dist/cjs/src/router.js +94 -0
  26. package/dist/cjs/src/router.js.map +1 -0
  27. package/dist/esm/src/constants.d.ts +19 -0
  28. package/dist/esm/src/constants.js +19 -0
  29. package/dist/esm/src/constants.js.map +1 -0
  30. package/dist/esm/src/entities/index.d.ts +3 -0
  31. package/dist/esm/src/entities/index.js +4 -0
  32. package/dist/esm/src/entities/index.js.map +1 -0
  33. package/dist/esm/src/entities/pair.d.ts +147 -0
  34. package/dist/esm/src/entities/pair.js +312 -0
  35. package/dist/esm/src/entities/pair.js.map +1 -0
  36. package/dist/esm/src/entities/route.d.ts +12 -0
  37. package/dist/esm/src/entities/route.js +40 -0
  38. package/dist/esm/src/entities/route.js.map +1 -0
  39. package/dist/esm/src/entities/trade.d.ts +103 -0
  40. package/dist/esm/src/entities/trade.js +253 -0
  41. package/dist/esm/src/entities/trade.js.map +1 -0
  42. package/dist/esm/src/errors.d.ts +16 -0
  43. package/dist/esm/src/errors.js +29 -0
  44. package/dist/esm/src/errors.js.map +1 -0
  45. package/dist/esm/src/index.d.ts +4 -0
  46. package/dist/esm/src/index.js +5 -0
  47. package/dist/esm/src/index.js.map +1 -0
  48. package/dist/esm/src/router.d.ts +64 -0
  49. package/dist/esm/src/router.js +89 -0
  50. package/dist/esm/src/router.js.map +1 -0
  51. package/dist/types/src/constants.d.ts +19 -0
  52. package/dist/types/src/entities/index.d.ts +3 -0
  53. package/dist/types/src/entities/pair.d.ts +147 -0
  54. package/dist/types/src/entities/route.d.ts +12 -0
  55. package/dist/types/src/entities/trade.d.ts +103 -0
  56. package/dist/types/src/errors.d.ts +16 -0
  57. package/dist/types/src/index.d.ts +4 -0
  58. package/dist/types/src/router.d.ts +64 -0
  59. package/package.json +68 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Uniswap Labs
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,6 @@
1
+ # Uniswap V2 SDK
2
+
3
+ [![npm version](https://img.shields.io/npm/v/@uniswap/v2-sdk/latest.svg)](https://www.npmjs.com/package/@uniswap/v2-sdk/v/latest)
4
+ [![npm bundle size (scoped version)](https://img.shields.io/bundlephobia/minzip/@uniswap/v2-sdk/latest.svg)](https://bundlephobia.com/result?p=@uniswap/v2-sdk@latest)
5
+
6
+ In-depth documentation on this SDK is available at [uniswap.org](https://uniswap.org/docs/v2/SDK/getting-started/).
@@ -0,0 +1,19 @@
1
+ import { Percent } from '@centurion_project/sdk-core';
2
+ import JSBI from 'jsbi';
3
+ /**
4
+ * @deprecated use FACTORY_ADDRESS_MAP instead
5
+ */
6
+ export declare const FACTORY_ADDRESS = "0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f";
7
+ export declare const FACTORY_ADDRESS_MAP: {
8
+ [chainId: number]: string;
9
+ };
10
+ export declare const INIT_CODE_HASH = "0x96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f";
11
+ export declare const MINIMUM_LIQUIDITY: JSBI;
12
+ export declare const ZERO: JSBI;
13
+ export declare const ONE: JSBI;
14
+ export declare const FIVE: JSBI;
15
+ export declare const _997: JSBI;
16
+ export declare const _1000: JSBI;
17
+ export declare const BASIS_POINTS: JSBI;
18
+ export declare const ZERO_PERCENT: Percent;
19
+ export declare const ONE_HUNDRED_PERCENT: Percent;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ONE_HUNDRED_PERCENT = exports.ZERO_PERCENT = exports.BASIS_POINTS = exports._1000 = exports._997 = exports.FIVE = exports.ONE = exports.ZERO = exports.MINIMUM_LIQUIDITY = exports.INIT_CODE_HASH = exports.FACTORY_ADDRESS_MAP = exports.FACTORY_ADDRESS = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const sdk_core_1 = require("@centurion_project/sdk-core");
6
+ const jsbi_1 = tslib_1.__importDefault(require("jsbi"));
7
+ /**
8
+ * @deprecated use FACTORY_ADDRESS_MAP instead
9
+ */
10
+ exports.FACTORY_ADDRESS = '0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f';
11
+ exports.FACTORY_ADDRESS_MAP = sdk_core_1.V2_FACTORY_ADDRESSES;
12
+ exports.INIT_CODE_HASH = '0x96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f';
13
+ exports.MINIMUM_LIQUIDITY = jsbi_1.default.BigInt(1000);
14
+ // exports for internal consumption
15
+ exports.ZERO = jsbi_1.default.BigInt(0);
16
+ exports.ONE = jsbi_1.default.BigInt(1);
17
+ exports.FIVE = jsbi_1.default.BigInt(5);
18
+ exports._997 = jsbi_1.default.BigInt(997);
19
+ exports._1000 = jsbi_1.default.BigInt(1000);
20
+ exports.BASIS_POINTS = jsbi_1.default.BigInt(10000);
21
+ exports.ZERO_PERCENT = new sdk_core_1.Percent(exports.ZERO);
22
+ exports.ONE_HUNDRED_PERCENT = new sdk_core_1.Percent(exports.ONE);
23
+ //# sourceMappingURL=constants.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../src/constants.ts"],"names":[],"mappings":";;;;AAAA,0DAA2E;AAC3E,wDAAuB;AAEvB;;GAEG;AACU,QAAA,eAAe,GAAG,4CAA4C,CAAA;AAE9D,QAAA,mBAAmB,GAAkC,+BAAoB,CAAA;AAEzE,QAAA,cAAc,GAAG,oEAAoE,CAAA;AAErF,QAAA,iBAAiB,GAAG,cAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;AAElD,mCAAmC;AACtB,QAAA,IAAI,GAAG,cAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAA;AACrB,QAAA,GAAG,GAAG,cAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAA;AACpB,QAAA,IAAI,GAAG,cAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAA;AACrB,QAAA,IAAI,GAAG,cAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;AACvB,QAAA,KAAK,GAAG,cAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;AACzB,QAAA,YAAY,GAAG,cAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;AAEjC,QAAA,YAAY,GAAG,IAAI,kBAAO,CAAC,YAAI,CAAC,CAAA;AAChC,QAAA,mBAAmB,GAAG,IAAI,kBAAO,CAAC,WAAG,CAAC,CAAA"}
@@ -0,0 +1,3 @@
1
+ export * from './pair';
2
+ export * from './route';
3
+ export * from './trade';
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./pair"), exports);
5
+ tslib_1.__exportStar(require("./route"), exports);
6
+ tslib_1.__exportStar(require("./trade"), exports);
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/entities/index.ts"],"names":[],"mappings":";;;AAAA,iDAAsB;AACtB,kDAAuB;AACvB,kDAAuB"}
@@ -0,0 +1,147 @@
1
+ import { BigintIsh, CurrencyAmount, Price, Token } from '@centurion_project/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,318 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Pair = exports.computePairAddress = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const address_1 = require("@ethersproject/address");
6
+ const bignumber_1 = require("@ethersproject/bignumber");
7
+ const solidity_1 = require("@ethersproject/solidity");
8
+ const sdk_core_1 = require("@centurion_project/sdk-core");
9
+ const jsbi_1 = tslib_1.__importDefault(require("jsbi"));
10
+ const tiny_invariant_1 = tslib_1.__importDefault(require("tiny-invariant"));
11
+ const constants_1 = require("../constants");
12
+ const errors_1 = require("../errors");
13
+ const computePairAddress = ({ factoryAddress, tokenA, tokenB, }) => {
14
+ const [token0, token1] = tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA]; // does safety checks
15
+ return (0, address_1.getCreate2Address)(factoryAddress, (0, solidity_1.keccak256)(['bytes'], [(0, solidity_1.pack)(['address', 'address'], [token0.address, token1.address])]), constants_1.INIT_CODE_HASH);
16
+ };
17
+ exports.computePairAddress = computePairAddress;
18
+ class Pair {
19
+ static getAddress(tokenA, tokenB) {
20
+ var _a;
21
+ const factoryAddress = (_a = constants_1.FACTORY_ADDRESS_MAP[tokenA.chainId]) !== null && _a !== void 0 ? _a : constants_1.FACTORY_ADDRESS;
22
+ return (0, exports.computePairAddress)({ factoryAddress, tokenA, tokenB });
23
+ }
24
+ constructor(currencyAmountA, tokenAmountB) {
25
+ const tokenAmounts = currencyAmountA.currency.sortsBefore(tokenAmountB.currency) // does safety checks
26
+ ? [currencyAmountA, tokenAmountB]
27
+ : [tokenAmountB, currencyAmountA];
28
+ this.liquidityToken = new sdk_core_1.Token(tokenAmounts[0].currency.chainId, Pair.getAddress(tokenAmounts[0].currency, tokenAmounts[1].currency), 18, 'UNI-V2', 'Uniswap V2');
29
+ this.tokenAmounts = tokenAmounts;
30
+ }
31
+ /**
32
+ * Returns true if the token is either token0 or token1
33
+ * @param token to check
34
+ */
35
+ involvesToken(token) {
36
+ return token.equals(this.token0) || token.equals(this.token1);
37
+ }
38
+ /**
39
+ * Returns the current mid price of the pair in terms of token0, i.e. the ratio of reserve1 to reserve0
40
+ */
41
+ get token0Price() {
42
+ const result = this.tokenAmounts[1].divide(this.tokenAmounts[0]);
43
+ return new sdk_core_1.Price(this.token0, this.token1, result.denominator, result.numerator);
44
+ }
45
+ /**
46
+ * Returns the current mid price of the pair in terms of token1, i.e. the ratio of reserve0 to reserve1
47
+ */
48
+ get token1Price() {
49
+ const result = this.tokenAmounts[0].divide(this.tokenAmounts[1]);
50
+ return new sdk_core_1.Price(this.token1, this.token0, result.denominator, result.numerator);
51
+ }
52
+ /**
53
+ * Return the price of the given token in terms of the other token in the pair.
54
+ * @param token token to return price of
55
+ */
56
+ priceOf(token) {
57
+ (0, tiny_invariant_1.default)(this.involvesToken(token), 'TOKEN');
58
+ return token.equals(this.token0) ? this.token0Price : this.token1Price;
59
+ }
60
+ /**
61
+ * Returns the chain ID of the tokens in the pair.
62
+ */
63
+ get chainId() {
64
+ return this.token0.chainId;
65
+ }
66
+ get token0() {
67
+ return this.tokenAmounts[0].currency;
68
+ }
69
+ get token1() {
70
+ return this.tokenAmounts[1].currency;
71
+ }
72
+ get reserve0() {
73
+ return this.tokenAmounts[0];
74
+ }
75
+ get reserve1() {
76
+ return this.tokenAmounts[1];
77
+ }
78
+ reserveOf(token) {
79
+ (0, tiny_invariant_1.default)(this.involvesToken(token), 'TOKEN');
80
+ return token.equals(this.token0) ? this.reserve0 : this.reserve1;
81
+ }
82
+ /**
83
+ * getAmountOut is the linear algebra of reserve ratio against amountIn:amountOut.
84
+ * https://ethereum.stackexchange.com/questions/101629/what-is-math-for-uniswap-calculates-the-amountout-and-amountin-why-997-and-1000
85
+ * has the math deduction for the reserve calculation without fee-on-transfer fees.
86
+ *
87
+ * With fee-on-transfer tax, intuitively it's just:
88
+ * inputAmountWithFeeAndTax = 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn
89
+ * = (1 - amountIn.sellFeesBips / 10000) * amountInWithFee
90
+ * where amountInWithFee is the amountIn after taking out the LP fees
91
+ * outputAmountWithTax = amountOut * (1 - amountOut.buyFeesBips / 10000)
92
+ *
93
+ * But we are illustrating the math deduction below to ensure that's the case.
94
+ *
95
+ * before swap A * B = K where A = reserveIn B = reserveOut
96
+ *
97
+ * after swap A' * B' = K where only k is a constant value
98
+ *
99
+ * getAmountOut
100
+ *
101
+ * A' = A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn # here 0.3% is deducted
102
+ * B' = B - amountOut * (1 - amountOut.buyFeesBips / 10000)
103
+ * amountOut = (B - B') / (1 - amountOut.buyFeesBips / 10000) # where A' * B' still is k
104
+ * = (B - K/(A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn))
105
+ * /
106
+ * (1 - amountOut.buyFeesBips / 10000)
107
+ * = (B - AB/(A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn))
108
+ * /
109
+ * (1 - amountOut.buyFeesBips / 10000)
110
+ * = ((BA + B * 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn - AB)/(A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn))
111
+ * /
112
+ * (1 - amountOut.buyFeesBips / 10000)
113
+ * = (B * 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn / (A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn)
114
+ * /
115
+ * (1 - amountOut.buyFeesBips / 10000)
116
+ * amountOut * (1 - amountOut.buyFeesBips / 10000) = (B * 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn
117
+ * /
118
+ * (A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn)
119
+ *
120
+ * outputAmountWithTax = (B * 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn
121
+ * /
122
+ * (A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn)
123
+ * = (B * 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn * 1000
124
+ * /
125
+ * ((A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn) * 1000)
126
+ * = (B * (1 - amountIn.sellFeesBips / 10000) 997 * * amountIn
127
+ * /
128
+ * (1000 * A + (1 - amountIn.sellFeesBips / 10000) * 997 * amountIn)
129
+ * = (B * (1 - amountIn.sellFeesBips / 10000) * inputAmountWithFee)
130
+ * /
131
+ * (1000 * A + (1 - amountIn.sellFeesBips / 10000) * inputAmountWithFee)
132
+ * = (B * inputAmountWithFeeAndTax)
133
+ * /
134
+ * (1000 * A + inputAmountWithFeeAndTax)
135
+ *
136
+ * inputAmountWithFeeAndTax = (1 - amountIn.sellFeesBips / 10000) * inputAmountWithFee
137
+ * outputAmountWithTax = amountOut * (1 - amountOut.buyFeesBips / 10000)
138
+ *
139
+ * @param inputAmount
140
+ * @param calculateFotFees
141
+ */
142
+ getOutputAmount(inputAmount, calculateFotFees = true) {
143
+ (0, tiny_invariant_1.default)(this.involvesToken(inputAmount.currency), 'TOKEN');
144
+ if (jsbi_1.default.equal(this.reserve0.quotient, constants_1.ZERO) || jsbi_1.default.equal(this.reserve1.quotient, constants_1.ZERO)) {
145
+ throw new errors_1.InsufficientReservesError();
146
+ }
147
+ const inputReserve = this.reserveOf(inputAmount.currency);
148
+ const outputReserve = this.reserveOf(inputAmount.currency.equals(this.token0) ? this.token1 : this.token0);
149
+ const percentAfterSellFees = calculateFotFees ? this.derivePercentAfterSellFees(inputAmount) : constants_1.ZERO_PERCENT;
150
+ const inputAmountAfterTax = percentAfterSellFees.greaterThan(constants_1.ZERO_PERCENT)
151
+ ? sdk_core_1.CurrencyAmount.fromRawAmount(inputAmount.currency, percentAfterSellFees.multiply(inputAmount).quotient // fraction.quotient will round down by itself, which is desired
152
+ )
153
+ : inputAmount;
154
+ const inputAmountWithFeeAndAfterTax = jsbi_1.default.multiply(inputAmountAfterTax.quotient, constants_1._997);
155
+ const numerator = jsbi_1.default.multiply(inputAmountWithFeeAndAfterTax, outputReserve.quotient);
156
+ const denominator = jsbi_1.default.add(jsbi_1.default.multiply(inputReserve.quotient, constants_1._1000), inputAmountWithFeeAndAfterTax);
157
+ const outputAmount = sdk_core_1.CurrencyAmount.fromRawAmount(inputAmount.currency.equals(this.token0) ? this.token1 : this.token0, jsbi_1.default.divide(numerator, denominator) // JSBI.divide will round down by itself, which is desired
158
+ );
159
+ if (jsbi_1.default.equal(outputAmount.quotient, constants_1.ZERO)) {
160
+ throw new errors_1.InsufficientInputAmountError();
161
+ }
162
+ const percentAfterBuyFees = calculateFotFees ? this.derivePercentAfterBuyFees(outputAmount) : constants_1.ZERO_PERCENT;
163
+ const outputAmountAfterTax = percentAfterBuyFees.greaterThan(constants_1.ZERO_PERCENT)
164
+ ? sdk_core_1.CurrencyAmount.fromRawAmount(outputAmount.currency, outputAmount.multiply(percentAfterBuyFees).quotient // fraction.quotient will round down by itself, which is desired
165
+ )
166
+ : outputAmount;
167
+ if (jsbi_1.default.equal(outputAmountAfterTax.quotient, constants_1.ZERO)) {
168
+ throw new errors_1.InsufficientInputAmountError();
169
+ }
170
+ return [
171
+ outputAmountAfterTax,
172
+ new Pair(inputReserve.add(inputAmountAfterTax), outputReserve.subtract(outputAmountAfterTax)),
173
+ ];
174
+ }
175
+ /**
176
+ * getAmountIn is the linear algebra of reserve ratio against amountIn:amountOut.
177
+ * https://ethereum.stackexchange.com/questions/101629/what-is-math-for-uniswap-calculates-the-amountout-and-amountin-why-997-and-1000
178
+ * has the math deduction for the reserve calculation without fee-on-transfer fees.
179
+ *
180
+ * With fee-on-transfer fees, intuitively it's just:
181
+ * outputAmountWithTax = amountOut / (1 - amountOut.buyFeesBips / 10000)
182
+ * inputAmountWithTax = amountIn / (1 - amountIn.sellFeesBips / 10000) / 0.997
183
+ *
184
+ * But we are illustrating the math deduction below to ensure that's the case.
185
+ *
186
+ * before swap A * B = K where A = reserveIn B = reserveOut
187
+ *
188
+ * after swap A' * B' = K where only k is a constant value
189
+ *
190
+ * getAmountIn
191
+ *
192
+ * B' = B - amountOut * (1 - amountOut.buyFeesBips / 10000)
193
+ * A' = A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn # here 0.3% is deducted
194
+ * amountIn = (A' - A) / (0.997 * (1 - amountIn.sellFeesBips / 10000))
195
+ * = (K / (B - amountOut / (1 - amountOut.buyFeesBips / 10000)) - A)
196
+ * /
197
+ * (0.997 * (1 - amountIn.sellFeesBips / 10000))
198
+ * = (AB / (B - amountOut / (1 - amountOut.buyFeesBips / 10000)) - A)
199
+ * /
200
+ * (0.997 * (1 - amountIn.sellFeesBips / 10000))
201
+ * = ((AB - AB + A * amountOut / (1 - amountOut.buyFeesBips / 10000)) / (B - amountOut / (1 - amountOut.buyFeesBips / 10000)))
202
+ * /
203
+ * (0.997 * (1 - amountIn.sellFeesBips / 10000))
204
+ * = ((A * amountOut / (1 - amountOut.buyFeesBips / 10000)) / (B - amountOut / (1 - amountOut.buyFeesBips / 10000)))
205
+ * /
206
+ * (0.997 * (1 - amountIn.sellFeesBips / 10000))
207
+ * = ((A * 1000 * amountOut / (1 - amountOut.buyFeesBips / 10000)) / (B - amountOut / (1 - amountOut.buyFeesBips / 10000)))
208
+ * /
209
+ * (997 * (1 - amountIn.sellFeesBips / 10000))
210
+ *
211
+ * outputAmountWithTax = amountOut / (1 - amountOut.buyFeesBips / 10000)
212
+ * inputAmountWithTax = amountIn / (997 * (1 - amountIn.sellFeesBips / 10000))
213
+ * = (A * outputAmountWithTax * 1000) / ((B - outputAmountWithTax) * 997)
214
+ *
215
+ * @param outputAmount
216
+ */
217
+ getInputAmount(outputAmount, calculateFotFees = true) {
218
+ (0, tiny_invariant_1.default)(this.involvesToken(outputAmount.currency), 'TOKEN');
219
+ const percentAfterBuyFees = calculateFotFees ? this.derivePercentAfterBuyFees(outputAmount) : constants_1.ZERO_PERCENT;
220
+ const outputAmountBeforeTax = percentAfterBuyFees.greaterThan(constants_1.ZERO_PERCENT)
221
+ ? sdk_core_1.CurrencyAmount.fromRawAmount(outputAmount.currency, jsbi_1.default.add(outputAmount.divide(percentAfterBuyFees).quotient, constants_1.ONE) // add 1 for rounding up
222
+ )
223
+ : outputAmount;
224
+ if (jsbi_1.default.equal(this.reserve0.quotient, constants_1.ZERO) ||
225
+ jsbi_1.default.equal(this.reserve1.quotient, constants_1.ZERO) ||
226
+ jsbi_1.default.greaterThanOrEqual(outputAmount.quotient, this.reserveOf(outputAmount.currency).quotient) ||
227
+ jsbi_1.default.greaterThanOrEqual(outputAmountBeforeTax.quotient, this.reserveOf(outputAmount.currency).quotient)) {
228
+ throw new errors_1.InsufficientReservesError();
229
+ }
230
+ const outputReserve = this.reserveOf(outputAmount.currency);
231
+ const inputReserve = this.reserveOf(outputAmount.currency.equals(this.token0) ? this.token1 : this.token0);
232
+ const numerator = jsbi_1.default.multiply(jsbi_1.default.multiply(inputReserve.quotient, outputAmountBeforeTax.quotient), constants_1._1000);
233
+ const denominator = jsbi_1.default.multiply(jsbi_1.default.subtract(outputReserve.quotient, outputAmountBeforeTax.quotient), constants_1._997);
234
+ const inputAmount = sdk_core_1.CurrencyAmount.fromRawAmount(outputAmount.currency.equals(this.token0) ? this.token1 : this.token0, jsbi_1.default.add(jsbi_1.default.divide(numerator, denominator), constants_1.ONE) // add 1 here is part of the formula, no rounding needed here, since there will not be decimal at this point
235
+ );
236
+ const percentAfterSellFees = calculateFotFees ? this.derivePercentAfterSellFees(inputAmount) : constants_1.ZERO_PERCENT;
237
+ const inputAmountBeforeTax = percentAfterSellFees.greaterThan(constants_1.ZERO_PERCENT)
238
+ ? sdk_core_1.CurrencyAmount.fromRawAmount(inputAmount.currency, jsbi_1.default.add(inputAmount.divide(percentAfterSellFees).quotient, constants_1.ONE) // add 1 for rounding up
239
+ )
240
+ : inputAmount;
241
+ return [inputAmountBeforeTax, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))];
242
+ }
243
+ getLiquidityMinted(totalSupply, tokenAmountA, tokenAmountB) {
244
+ (0, tiny_invariant_1.default)(totalSupply.currency.equals(this.liquidityToken), 'LIQUIDITY');
245
+ const tokenAmounts = tokenAmountA.currency.sortsBefore(tokenAmountB.currency) // does safety checks
246
+ ? [tokenAmountA, tokenAmountB]
247
+ : [tokenAmountB, tokenAmountA];
248
+ (0, tiny_invariant_1.default)(tokenAmounts[0].currency.equals(this.token0) && tokenAmounts[1].currency.equals(this.token1), 'TOKEN');
249
+ let liquidity;
250
+ if (jsbi_1.default.equal(totalSupply.quotient, constants_1.ZERO)) {
251
+ liquidity = jsbi_1.default.subtract((0, sdk_core_1.sqrt)(jsbi_1.default.multiply(tokenAmounts[0].quotient, tokenAmounts[1].quotient)), constants_1.MINIMUM_LIQUIDITY);
252
+ }
253
+ else {
254
+ const amount0 = jsbi_1.default.divide(jsbi_1.default.multiply(tokenAmounts[0].quotient, totalSupply.quotient), this.reserve0.quotient);
255
+ const amount1 = jsbi_1.default.divide(jsbi_1.default.multiply(tokenAmounts[1].quotient, totalSupply.quotient), this.reserve1.quotient);
256
+ liquidity = jsbi_1.default.lessThanOrEqual(amount0, amount1) ? amount0 : amount1;
257
+ }
258
+ if (!jsbi_1.default.greaterThan(liquidity, constants_1.ZERO)) {
259
+ throw new errors_1.InsufficientInputAmountError();
260
+ }
261
+ return sdk_core_1.CurrencyAmount.fromRawAmount(this.liquidityToken, liquidity);
262
+ }
263
+ getLiquidityValue(token, totalSupply, liquidity, feeOn = false, kLast) {
264
+ (0, tiny_invariant_1.default)(this.involvesToken(token), 'TOKEN');
265
+ (0, tiny_invariant_1.default)(totalSupply.currency.equals(this.liquidityToken), 'TOTAL_SUPPLY');
266
+ (0, tiny_invariant_1.default)(liquidity.currency.equals(this.liquidityToken), 'LIQUIDITY');
267
+ (0, tiny_invariant_1.default)(jsbi_1.default.lessThanOrEqual(liquidity.quotient, totalSupply.quotient), 'LIQUIDITY');
268
+ let totalSupplyAdjusted;
269
+ if (!feeOn) {
270
+ totalSupplyAdjusted = totalSupply;
271
+ }
272
+ else {
273
+ (0, tiny_invariant_1.default)(!!kLast, 'K_LAST');
274
+ const kLastParsed = jsbi_1.default.BigInt(kLast);
275
+ if (!jsbi_1.default.equal(kLastParsed, constants_1.ZERO)) {
276
+ const rootK = (0, sdk_core_1.sqrt)(jsbi_1.default.multiply(this.reserve0.quotient, this.reserve1.quotient));
277
+ const rootKLast = (0, sdk_core_1.sqrt)(kLastParsed);
278
+ if (jsbi_1.default.greaterThan(rootK, rootKLast)) {
279
+ const numerator = jsbi_1.default.multiply(totalSupply.quotient, jsbi_1.default.subtract(rootK, rootKLast));
280
+ const denominator = jsbi_1.default.add(jsbi_1.default.multiply(rootK, constants_1.FIVE), rootKLast);
281
+ const feeLiquidity = jsbi_1.default.divide(numerator, denominator);
282
+ totalSupplyAdjusted = totalSupply.add(sdk_core_1.CurrencyAmount.fromRawAmount(this.liquidityToken, feeLiquidity));
283
+ }
284
+ else {
285
+ totalSupplyAdjusted = totalSupply;
286
+ }
287
+ }
288
+ else {
289
+ totalSupplyAdjusted = totalSupply;
290
+ }
291
+ }
292
+ return sdk_core_1.CurrencyAmount.fromRawAmount(token, jsbi_1.default.divide(jsbi_1.default.multiply(liquidity.quotient, this.reserveOf(token).quotient), totalSupplyAdjusted.quotient));
293
+ }
294
+ derivePercentAfterSellFees(inputAmount) {
295
+ const sellFeeBips = this.token0.wrapped.equals(inputAmount.wrapped.currency)
296
+ ? this.token0.wrapped.sellFeeBps
297
+ : this.token1.wrapped.sellFeeBps;
298
+ if (sellFeeBips === null || sellFeeBips === void 0 ? void 0 : sellFeeBips.gt(bignumber_1.BigNumber.from(0))) {
299
+ return constants_1.ONE_HUNDRED_PERCENT.subtract(new sdk_core_1.Percent(jsbi_1.default.BigInt(sellFeeBips)).divide(constants_1.BASIS_POINTS));
300
+ }
301
+ else {
302
+ return constants_1.ZERO_PERCENT;
303
+ }
304
+ }
305
+ derivePercentAfterBuyFees(outputAmount) {
306
+ const buyFeeBps = this.token0.wrapped.equals(outputAmount.wrapped.currency)
307
+ ? this.token0.wrapped.buyFeeBps
308
+ : this.token1.wrapped.buyFeeBps;
309
+ if (buyFeeBps === null || buyFeeBps === void 0 ? void 0 : buyFeeBps.gt(bignumber_1.BigNumber.from(0))) {
310
+ return constants_1.ONE_HUNDRED_PERCENT.subtract(new sdk_core_1.Percent(jsbi_1.default.BigInt(buyFeeBps)).divide(constants_1.BASIS_POINTS));
311
+ }
312
+ else {
313
+ return constants_1.ZERO_PERCENT;
314
+ }
315
+ }
316
+ }
317
+ exports.Pair = Pair;
318
+ //# sourceMappingURL=pair.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pair.js","sourceRoot":"","sources":["../../../../src/entities/pair.ts"],"names":[],"mappings":";;;;AAAA,oDAA0D;AAC1D,w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AO,wBAAY,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,qBAAS,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE;YACpC,OAAO,+BAAmB,CAAC,QAAQ,CAAC,IAAI,kBAAO,CAAC,cAAI,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,wBAAY,CAAC,CAAC,CAAA;SAC9F;aAAM;YACL,OAAO,wBAAY,CAAA;SACpB;IACH,CAAC;CACF;AA7WD,oBA6WC"}
@@ -0,0 +1,12 @@
1
+ import { Currency, Price, Token } from '@centurion_project/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,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Route = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const tiny_invariant_1 = tslib_1.__importDefault(require("tiny-invariant"));
6
+ const sdk_core_1 = require("@centurion_project/sdk-core");
7
+ class Route {
8
+ constructor(pairs, input, output) {
9
+ this._midPrice = null;
10
+ (0, tiny_invariant_1.default)(pairs.length > 0, 'PAIRS');
11
+ const chainId = pairs[0].chainId;
12
+ (0, tiny_invariant_1.default)(pairs.every((pair) => pair.chainId === chainId), 'CHAIN_IDS');
13
+ const wrappedInput = input.wrapped;
14
+ (0, tiny_invariant_1.default)(pairs[0].involvesToken(wrappedInput), 'INPUT');
15
+ (0, tiny_invariant_1.default)(typeof output === 'undefined' || pairs[pairs.length - 1].involvesToken(output.wrapped), 'OUTPUT');
16
+ const path = [wrappedInput];
17
+ for (const [i, pair] of pairs.entries()) {
18
+ const currentInput = path[i];
19
+ (0, tiny_invariant_1.default)(currentInput.equals(pair.token0) || currentInput.equals(pair.token1), 'PATH');
20
+ const output = currentInput.equals(pair.token0) ? pair.token1 : pair.token0;
21
+ path.push(output);
22
+ }
23
+ this.pairs = pairs;
24
+ this.path = path;
25
+ this.input = input;
26
+ this.output = output;
27
+ }
28
+ get midPrice() {
29
+ if (this._midPrice !== null)
30
+ return this._midPrice;
31
+ const prices = [];
32
+ for (const [i, pair] of this.pairs.entries()) {
33
+ prices.push(this.path[i].equals(pair.token0)
34
+ ? new sdk_core_1.Price(pair.reserve0.currency, pair.reserve1.currency, pair.reserve0.quotient, pair.reserve1.quotient)
35
+ : new sdk_core_1.Price(pair.reserve1.currency, pair.reserve0.currency, pair.reserve1.quotient, pair.reserve0.quotient));
36
+ }
37
+ const reduced = prices.slice(1).reduce((accumulator, currentValue) => accumulator.multiply(currentValue), prices[0]);
38
+ return (this._midPrice = new sdk_core_1.Price(this.input, this.output, reduced.denominator, reduced.numerator));
39
+ }
40
+ get chainId() {
41
+ return this.pairs[0].chainId;
42
+ }
43
+ }
44
+ exports.Route = Route;
45
+ //# sourceMappingURL=route.js.map