@alcorexchange/alcor-swap-sdk 1.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.
Files changed (110) hide show
  1. package/.eslintignore +2 -0
  2. package/.eslintrc +12 -0
  3. package/LICENSE +21 -0
  4. package/build/entities/baseCurrency.js +27 -0
  5. package/build/entities/currency.js +2 -0
  6. package/build/entities/fractions/currencyAmount.js +80 -0
  7. package/build/entities/fractions/fraction.js +117 -0
  8. package/build/entities/fractions/index.js +11 -0
  9. package/build/entities/fractions/percent.js +46 -0
  10. package/build/entities/fractions/price.js +73 -0
  11. package/build/entities/index.js +26 -0
  12. package/build/entities/pool.js +248 -0
  13. package/build/entities/position.js +364 -0
  14. package/build/entities/route.js +70 -0
  15. package/build/entities/tick.js +23 -0
  16. package/build/entities/tickDataProvider.js +30 -0
  17. package/build/entities/tickListDataProvider.js +35 -0
  18. package/build/entities/token.js +53 -0
  19. package/build/entities/trade.js +464 -0
  20. package/build/index.js +19 -0
  21. package/build/internalConstants.js +54 -0
  22. package/build/utils/computeAllRoutes.js +39 -0
  23. package/build/utils/encodeSqrtRatioX64.js +21 -0
  24. package/build/utils/fullMath.js +22 -0
  25. package/build/utils/index.js +30 -0
  26. package/build/utils/isSorted.js +18 -0
  27. package/build/utils/liquidityMath.js +23 -0
  28. package/build/utils/maxLiquidityForAmounts.js +86 -0
  29. package/build/utils/mostSignificantBit.js +27 -0
  30. package/build/utils/nearestUsableTick.js +26 -0
  31. package/build/utils/positionLibrary.js +22 -0
  32. package/build/utils/priceTickConversions.js +51 -0
  33. package/build/utils/sortedInsert.js +39 -0
  34. package/build/utils/sqrt.js +33 -0
  35. package/build/utils/sqrtPriceMath.js +92 -0
  36. package/build/utils/swapMath.js +86 -0
  37. package/build/utils/tickLibrary.js +61 -0
  38. package/build/utils/tickList.js +110 -0
  39. package/build/utils/tickMath.js +122 -0
  40. package/jest.config.js +40 -0
  41. package/nodemon.json +6 -0
  42. package/package.json +51 -0
  43. package/src/entities/baseCurrency.ts +53 -0
  44. package/src/entities/currency.ts +3 -0
  45. package/src/entities/fractions/currencyAmount.ts +129 -0
  46. package/src/entities/fractions/fraction.ts +190 -0
  47. package/src/entities/fractions/index.ts +4 -0
  48. package/src/entities/fractions/percent.ts +54 -0
  49. package/src/entities/fractions/price.ts +127 -0
  50. package/src/entities/index.ts +11 -0
  51. package/src/entities/pool.ts +399 -0
  52. package/src/entities/position.ts +591 -0
  53. package/src/entities/route.ts +84 -0
  54. package/src/entities/tick.ts +48 -0
  55. package/src/entities/tickDataProvider.ts +43 -0
  56. package/src/entities/tickListDataProvider.ts +37 -0
  57. package/src/entities/token.ts +56 -0
  58. package/src/entities/trade.ts +650 -0
  59. package/src/index.ts +3 -0
  60. package/src/internalConstants.ts +58 -0
  61. package/src/utils/computeAllRoutes.ts +64 -0
  62. package/src/utils/encodeSqrtRatioX64.ts +20 -0
  63. package/src/utils/fullMath.ts +17 -0
  64. package/src/utils/index.ts +14 -0
  65. package/src/utils/isSorted.ts +17 -0
  66. package/src/utils/liquidityMath.ts +17 -0
  67. package/src/utils/maxLiquidityForAmounts.ts +127 -0
  68. package/src/utils/mostSignificantBit.ts +25 -0
  69. package/src/utils/nearestUsableTick.ts +23 -0
  70. package/src/utils/positionLibrary.ts +37 -0
  71. package/src/utils/priceTickConversions.ts +57 -0
  72. package/src/utils/sortedInsert.ts +35 -0
  73. package/src/utils/sqrt.ts +31 -0
  74. package/src/utils/sqrtPriceMath.ts +169 -0
  75. package/src/utils/swapMath.ts +175 -0
  76. package/src/utils/tickLibrary.ts +88 -0
  77. package/src/utils/tickList.ts +147 -0
  78. package/src/utils/tickMath.ts +166 -0
  79. package/test/bestTradeExactOut.test.ts +266 -0
  80. package/test/currencyAmount.test.js +92 -0
  81. package/test/currencyAmount.test.ts +114 -0
  82. package/test/encodeSqrtRatioX64.test.ts +33 -0
  83. package/test/fixtures/pools.json +276 -0
  84. package/test/fixtures/ticks.json +608 -0
  85. package/test/fraction.test.js +87 -0
  86. package/test/fraction.test.ts +176 -0
  87. package/test/isSorted.test.ts +52 -0
  88. package/test/maxLiquidityForAmounts.test copy.ts +256 -0
  89. package/test/mostSignificantBit.test.ts +32 -0
  90. package/test/nearestUsableTick.test.ts +54 -0
  91. package/test/percent.test.js +52 -0
  92. package/test/percent.test.ts +68 -0
  93. package/test/pool.test.ts +377 -0
  94. package/test/position.test.ts +579 -0
  95. package/test/positionLibrary.test.ts +31 -0
  96. package/test/price.test.js +57 -0
  97. package/test/price.test.ts +62 -0
  98. package/test/priceTickConversions.test.ts +137 -0
  99. package/test/sqrtPriceMath.test.ts +113 -0
  100. package/test/tick.test.js +22 -0
  101. package/test/tick.test.ts +28 -0
  102. package/test/tickDataProvider.test.ts +17 -0
  103. package/test/tickLibrary.test.ts +111 -0
  104. package/test/tickList.test.ts +215 -0
  105. package/test/tickListDataProvider.test.ts +64 -0
  106. package/test/tickMath.test.ts +66 -0
  107. package/test/token.test.ts +58 -0
  108. package/test/trade.test.ts +210 -0
  109. package/test2.ts +73 -0
  110. package/tsconfig.json +24 -0
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Route = void 0;
7
+ const tiny_invariant_1 = __importDefault(require("tiny-invariant"));
8
+ const fractions_1 = require("./fractions");
9
+ /**
10
+ * Represents a list of pools through which a swap can occur
11
+ * @template TInput The input token
12
+ * @template TOutput The output token
13
+ */
14
+ class Route {
15
+ /**
16
+ * Creates an instance of route.
17
+ * @param pools An array of `Pool` objects, ordered by the route the swap will take
18
+ * @param input The input token
19
+ * @param output The output token
20
+ */
21
+ constructor(pools, input, output) {
22
+ this._midPrice = null;
23
+ (0, tiny_invariant_1.default)(pools.length > 0, 'POOLS');
24
+ const wrappedInput = input;
25
+ (0, tiny_invariant_1.default)(pools[0].involvesToken(wrappedInput), 'INPUT');
26
+ (0, tiny_invariant_1.default)(pools[pools.length - 1].involvesToken(output), 'OUTPUT');
27
+ /**
28
+ * Normalizes tokenA-tokenB order and selects the next token/fee step to add to the path
29
+ * */
30
+ const tokenPath = [wrappedInput];
31
+ for (const [i, pool] of pools.entries()) {
32
+ const currentInputToken = tokenPath[i];
33
+ (0, tiny_invariant_1.default)(currentInputToken.equals(pool.tokenA) || currentInputToken.equals(pool.tokenB), 'PATH');
34
+ const nextToken = currentInputToken.equals(pool.tokenA) ? pool.tokenB : pool.tokenA;
35
+ tokenPath.push(nextToken);
36
+ }
37
+ this.pools = pools;
38
+ this.tokenPath = tokenPath;
39
+ this.input = input;
40
+ this.output = output !== null && output !== void 0 ? output : tokenPath[tokenPath.length - 1];
41
+ }
42
+ /**
43
+ * Returns the mid price of the route
44
+ */
45
+ get midPrice() {
46
+ if (this._midPrice !== null)
47
+ return this._midPrice;
48
+ const price = this.pools.slice(1).reduce(({ nextInput, price }, pool) => {
49
+ return nextInput.equals(pool.tokenA)
50
+ ? {
51
+ nextInput: pool.tokenB,
52
+ price: price.multiply(pool.tokenAPrice)
53
+ }
54
+ : {
55
+ nextInput: pool.tokenA,
56
+ price: price.multiply(pool.tokenBPrice)
57
+ };
58
+ }, this.pools[0].tokenA.equals(this.input)
59
+ ? {
60
+ nextInput: this.pools[0].tokenB,
61
+ price: this.pools[0].tokenAPrice
62
+ }
63
+ : {
64
+ nextInput: this.pools[0].tokenA,
65
+ price: this.pools[0].tokenBPrice
66
+ }).price;
67
+ return (this._midPrice = new fractions_1.Price(this.input, this.output, price.denominator, price.numerator));
68
+ }
69
+ }
70
+ exports.Route = Route;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Tick = void 0;
7
+ const jsbi_1 = __importDefault(require("jsbi"));
8
+ const tiny_invariant_1 = __importDefault(require("tiny-invariant"));
9
+ const utils_1 = require("../utils");
10
+ class Tick {
11
+ constructor({ id, liquidityGross, liquidityNet, feeGrowthOutsideAX64 = 0, feeGrowthOutsideBX64 = 0, tickCumulativeOutside = 0, secondsOutside = 0, secondsPerLiquidityOutsideX64 = 0, }) {
12
+ (0, tiny_invariant_1.default)(id >= utils_1.TickMath.MIN_TICK && id <= utils_1.TickMath.MAX_TICK, "TICK");
13
+ this.id = id;
14
+ this.liquidityGross = jsbi_1.default.BigInt(liquidityGross);
15
+ this.liquidityNet = jsbi_1.default.BigInt(liquidityNet);
16
+ this.feeGrowthOutsideAX64 = jsbi_1.default.BigInt(feeGrowthOutsideAX64);
17
+ this.feeGrowthOutsideBX64 = jsbi_1.default.BigInt(feeGrowthOutsideBX64);
18
+ this.tickCumulativeOutside = jsbi_1.default.BigInt(tickCumulativeOutside);
19
+ this.secondsOutside = jsbi_1.default.BigInt(secondsOutside);
20
+ this.secondsPerLiquidityOutsideX64 = jsbi_1.default.BigInt(secondsPerLiquidityOutsideX64);
21
+ }
22
+ }
23
+ exports.Tick = Tick;
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.NoTickDataProvider = void 0;
13
+ /**
14
+ * This tick data provider does not know how to fetch any tick data. It throws whenever it is required. Useful if you
15
+ * do not need to load tick data for your use case.
16
+ */
17
+ class NoTickDataProvider {
18
+ getTick(_tick) {
19
+ return __awaiter(this, void 0, void 0, function* () {
20
+ throw new Error(NoTickDataProvider.ERROR_MESSAGE);
21
+ });
22
+ }
23
+ nextInitializedTickWithinOneWord(_tick, _lte, _tickSpacing) {
24
+ return __awaiter(this, void 0, void 0, function* () {
25
+ throw new Error(NoTickDataProvider.ERROR_MESSAGE);
26
+ });
27
+ }
28
+ }
29
+ exports.NoTickDataProvider = NoTickDataProvider;
30
+ NoTickDataProvider.ERROR_MESSAGE = "No tick data provider was given";
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.TickListDataProvider = void 0;
13
+ const tickList_1 = require("../utils/tickList");
14
+ const tick_1 = require("./tick");
15
+ /**
16
+ * A data provider for ticks that is backed by an in-memory array of ticks.
17
+ */
18
+ class TickListDataProvider {
19
+ constructor(ticks, tickSpacing) {
20
+ const ticksMapped = ticks.map((t) => t instanceof tick_1.Tick ? t : new tick_1.Tick(t));
21
+ tickList_1.TickList.validateList(ticksMapped, tickSpacing);
22
+ this.ticks = ticksMapped;
23
+ }
24
+ getTick(tick) {
25
+ return __awaiter(this, void 0, void 0, function* () {
26
+ return tickList_1.TickList.getTick(this.ticks, tick);
27
+ });
28
+ }
29
+ nextInitializedTickWithinOneWord(tick, lte, tickSpacing) {
30
+ return __awaiter(this, void 0, void 0, function* () {
31
+ return tickList_1.TickList.nextInitializedTickWithinOneWord(this.ticks, tick, lte, tickSpacing);
32
+ });
33
+ }
34
+ }
35
+ exports.TickListDataProvider = TickListDataProvider;
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Token = void 0;
7
+ const tiny_invariant_1 = __importDefault(require("tiny-invariant"));
8
+ const baseCurrency_1 = require("./baseCurrency");
9
+ const eos_common_1 = require("eos-common");
10
+ /**
11
+ * Represents an ERC20 token with a unique address and some metadata.
12
+ */
13
+ class Token extends baseCurrency_1.BaseCurrency {
14
+ /**
15
+ * @param contract {@link BaseCurrency#contract}
16
+ * @param decimals {@link BaseCurrency#decimals}
17
+ * @param symbol {@link BaseCurrency#symbol}
18
+ * @param id {@link BaseCurrency#id}
19
+ */
20
+ constructor(contract, decimals, symbol, id) {
21
+ super(contract, decimals, symbol, id);
22
+ }
23
+ get name() {
24
+ console.warn('Token.name is deprecated, use token.id');
25
+ return this.symbol.toLowerCase() + '-' + this.contract;
26
+ }
27
+ /**
28
+ * Returns true if the two tokens are equivalent, i.e. have the same contract and symbol.
29
+ * @param other other token to compare
30
+ */
31
+ equals(other) {
32
+ return (this.contract === other.contract &&
33
+ this.symbol === other.symbol &&
34
+ this.decimals === other.decimals);
35
+ }
36
+ /**
37
+ * Returns true if the address of this token sorts before the address of the other token
38
+ * @param other other token to compare
39
+ * @throws if the tokens have the same contract and symbol
40
+ */
41
+ sortsBefore(other) {
42
+ if (this.contract === other.contract) {
43
+ (0, tiny_invariant_1.default)(this.symbol !== other.symbol, "SYMBOLS");
44
+ const token0Symbol = (0, eos_common_1.symbol)(this.symbol, this.decimals);
45
+ const token1Symbol = (0, eos_common_1.symbol)(other.symbol, other.decimals);
46
+ return token0Symbol.raw().lt(token1Symbol.raw());
47
+ }
48
+ else {
49
+ return (0, eos_common_1.name)(this.contract).raw().lt((0, eos_common_1.name)(other.contract).raw());
50
+ }
51
+ }
52
+ }
53
+ exports.Token = Token;