@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,48 @@
1
+ import JSBI from "jsbi";
2
+ import invariant from "tiny-invariant";
3
+ import { BigintIsh } from "../internalConstants";
4
+ import { TickMath } from "../utils";
5
+
6
+ export interface TickConstructorArgs {
7
+ id: number;
8
+ liquidityGross: BigintIsh;
9
+ liquidityNet: BigintIsh;
10
+ feeGrowthOutsideAX64: BigintIsh;
11
+ feeGrowthOutsideBX64: BigintIsh;
12
+ tickCumulativeOutside: BigintIsh;
13
+ secondsPerLiquidityOutsideX64: BigintIsh;
14
+ secondsOutside: BigintIsh;
15
+ }
16
+
17
+ export class Tick {
18
+ public readonly id: number;
19
+ public readonly liquidityGross: JSBI;
20
+ public readonly liquidityNet: JSBI;
21
+ public readonly feeGrowthOutsideAX64: JSBI;
22
+ public readonly feeGrowthOutsideBX64: JSBI;
23
+ public readonly tickCumulativeOutside: JSBI;
24
+ public readonly secondsOutside: JSBI;
25
+ public readonly secondsPerLiquidityOutsideX64: JSBI;
26
+
27
+ constructor({
28
+ id,
29
+ liquidityGross,
30
+ liquidityNet,
31
+ feeGrowthOutsideAX64 = 0,
32
+ feeGrowthOutsideBX64 = 0,
33
+ tickCumulativeOutside = 0,
34
+ secondsOutside = 0,
35
+ secondsPerLiquidityOutsideX64 = 0,
36
+ }: TickConstructorArgs) {
37
+ invariant(id >= TickMath.MIN_TICK && id <= TickMath.MAX_TICK, "TICK");
38
+
39
+ this.id = id;
40
+ this.liquidityGross = JSBI.BigInt(liquidityGross);
41
+ this.liquidityNet = JSBI.BigInt(liquidityNet);
42
+ this.feeGrowthOutsideAX64 = JSBI.BigInt(feeGrowthOutsideAX64);
43
+ this.feeGrowthOutsideBX64 = JSBI.BigInt(feeGrowthOutsideBX64);
44
+ this.tickCumulativeOutside = JSBI.BigInt(tickCumulativeOutside)
45
+ this.secondsOutside = JSBI.BigInt(secondsOutside);
46
+ this.secondsPerLiquidityOutsideX64 = JSBI.BigInt(secondsPerLiquidityOutsideX64);
47
+ }
48
+ }
@@ -0,0 +1,43 @@
1
+ import { Tick } from "./tick";
2
+
3
+ /**
4
+ * Provides information about ticks
5
+ */
6
+ export interface TickDataProvider {
7
+ /**
8
+ * Return information corresponding to a specific tick
9
+ * @param tick the tick to load
10
+ */
11
+ getTick(tick: number): Promise<Tick>;
12
+
13
+ /**
14
+ * Return the next tick that is initialized within a single word
15
+ * @param tick The current tick
16
+ * @param lte Whether the next tick should be lte the current tick
17
+ * @param tickSpacing The tick spacing of the pool
18
+ */
19
+ nextInitializedTickWithinOneWord(
20
+ tick: number,
21
+ lte: boolean,
22
+ tickSpacing: number
23
+ ): Promise<[number, boolean]>;
24
+ }
25
+
26
+ /**
27
+ * This tick data provider does not know how to fetch any tick data. It throws whenever it is required. Useful if you
28
+ * do not need to load tick data for your use case.
29
+ */
30
+ export class NoTickDataProvider implements TickDataProvider {
31
+ private static ERROR_MESSAGE = "No tick data provider was given";
32
+ async getTick(_tick: number): Promise<Tick> {
33
+ throw new Error(NoTickDataProvider.ERROR_MESSAGE);
34
+ }
35
+
36
+ async nextInitializedTickWithinOneWord(
37
+ _tick: number,
38
+ _lte: boolean,
39
+ _tickSpacing: number
40
+ ): Promise<[number, boolean]> {
41
+ throw new Error(NoTickDataProvider.ERROR_MESSAGE);
42
+ }
43
+ }
@@ -0,0 +1,37 @@
1
+ import { TickList } from "../utils/tickList";
2
+ import { Tick, TickConstructorArgs } from "./tick";
3
+ import { TickDataProvider } from "./tickDataProvider";
4
+
5
+ /**
6
+ * A data provider for ticks that is backed by an in-memory array of ticks.
7
+ */
8
+ export class TickListDataProvider implements TickDataProvider {
9
+ private ticks: readonly Tick[];
10
+
11
+ constructor(ticks: (Tick | TickConstructorArgs)[], tickSpacing: number) {
12
+ const ticksMapped: Tick[] = ticks.map((t) =>
13
+ t instanceof Tick ? t : new Tick(t)
14
+ );
15
+ TickList.validateList(ticksMapped, tickSpacing);
16
+ this.ticks = ticksMapped;
17
+ }
18
+
19
+ async getTick(
20
+ tick: number
21
+ ): Promise<Tick> {
22
+ return TickList.getTick(this.ticks, tick);
23
+ }
24
+
25
+ async nextInitializedTickWithinOneWord(
26
+ tick: number,
27
+ lte: boolean,
28
+ tickSpacing: number
29
+ ): Promise<[number, boolean]> {
30
+ return TickList.nextInitializedTickWithinOneWord(
31
+ this.ticks,
32
+ tick,
33
+ lte,
34
+ tickSpacing
35
+ );
36
+ }
37
+ }
@@ -0,0 +1,56 @@
1
+ import invariant from "tiny-invariant";
2
+ import { BaseCurrency } from "./baseCurrency";
3
+ import { symbol, name } from "eos-common"
4
+
5
+ /**
6
+ * Represents an ERC20 token with a unique address and some metadata.
7
+ */
8
+ export class Token extends BaseCurrency {
9
+ /**
10
+ * @param contract {@link BaseCurrency#contract}
11
+ * @param decimals {@link BaseCurrency#decimals}
12
+ * @param symbol {@link BaseCurrency#symbol}
13
+ * @param id {@link BaseCurrency#id}
14
+ */
15
+ public constructor(
16
+ contract: string,
17
+ decimals: number,
18
+ symbol: string,
19
+ id?: string
20
+ ) {
21
+ super(contract, decimals, symbol, id);
22
+ }
23
+
24
+ public get name(): string {
25
+ console.warn('Token.name is deprecated, use token.id')
26
+ return this.symbol.toLowerCase() + '-' + this.contract
27
+ }
28
+
29
+ /**
30
+ * Returns true if the two tokens are equivalent, i.e. have the same contract and symbol.
31
+ * @param other other token to compare
32
+ */
33
+ public equals(other: Token): boolean {
34
+ return (
35
+ this.contract === other.contract &&
36
+ this.symbol === other.symbol &&
37
+ this.decimals === other.decimals
38
+ );
39
+ }
40
+
41
+ /**
42
+ * Returns true if the address of this token sorts before the address of the other token
43
+ * @param other other token to compare
44
+ * @throws if the tokens have the same contract and symbol
45
+ */
46
+ public sortsBefore(other: Token): boolean {
47
+ if (this.contract === other.contract) {
48
+ invariant(this.symbol !== other.symbol, "SYMBOLS");
49
+ const token0Symbol = symbol(this.symbol, this.decimals);
50
+ const token1Symbol = symbol(other.symbol, other.decimals);
51
+ return token0Symbol.raw().lt(token1Symbol.raw());
52
+ } else {
53
+ return name(this.contract).raw().lt(name(other.contract).raw());
54
+ }
55
+ }
56
+ }