@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,64 @@
1
+ import JSBI from "jsbi";
2
+ import { TickListDataProvider } from "entities/tickListDataProvider";
3
+
4
+ describe("TickListDataProvider", () => {
5
+ describe("constructor", () => {
6
+ it("can take an empty list of ticks", () => {
7
+ new TickListDataProvider([], 1);
8
+ });
9
+ it("throws for 0 tick spacing", () => {
10
+ expect(() => new TickListDataProvider([], 0)).toThrow(
11
+ "TICK_SPACING_NONZERO"
12
+ );
13
+ });
14
+ it("throws for uneven tick list", async () => {
15
+ await expect(
16
+ () =>
17
+ new TickListDataProvider(
18
+ [
19
+ { id: -1, liquidityNet: -1, liquidityGross: 1 },
20
+ { id: 1, liquidityNet: 2, liquidityGross: 1 },
21
+ ],
22
+ 1
23
+ )
24
+ ).toThrow("ZERO_NET");
25
+ });
26
+ });
27
+
28
+ describe("#getTick", () => {
29
+ it("throws if tick not in list", async () => {
30
+ const provider = new TickListDataProvider(
31
+ [
32
+ { id: -1, liquidityNet: -1, liquidityGross: 1 },
33
+ { id: 1, liquidityNet: 1, liquidityGross: 1 },
34
+ ],
35
+ 1
36
+ );
37
+ await expect(provider.getTick(0)).rejects.toThrow("NOT_CONTAINED");
38
+ });
39
+ it("gets the smallest tick from the list", async () => {
40
+ const provider = new TickListDataProvider(
41
+ [
42
+ { id: -1, liquidityNet: -1, liquidityGross: 1 },
43
+ { id: 1, liquidityNet: 1, liquidityGross: 1 },
44
+ ],
45
+ 1
46
+ );
47
+ const { liquidityNet, liquidityGross } = await provider.getTick(-1);
48
+ expect(liquidityNet).toEqual(JSBI.BigInt(-1));
49
+ expect(liquidityGross).toEqual(JSBI.BigInt(1));
50
+ });
51
+ it("gets the largest tick from the list", async () => {
52
+ const provider = new TickListDataProvider(
53
+ [
54
+ { id: -1, liquidityNet: -1, liquidityGross: 1 },
55
+ { id: 1, liquidityNet: 1, liquidityGross: 1 },
56
+ ],
57
+ 1
58
+ );
59
+ const { liquidityNet, liquidityGross } = await provider.getTick(1);
60
+ expect(liquidityNet).toEqual(JSBI.BigInt(1));
61
+ expect(liquidityGross).toEqual(JSBI.BigInt(1));
62
+ });
63
+ });
64
+ });
@@ -0,0 +1,66 @@
1
+ import JSBI from "jsbi";
2
+ import { ONE } from "internalConstants";
3
+ import { TickMath } from "utils/tickMath";
4
+
5
+ describe("TickMath", () => {
6
+ describe("#MIN_TICK", () => {
7
+ it("equals correct value", () => {
8
+ expect(TickMath.MIN_TICK).toEqual(-443636);
9
+ });
10
+ });
11
+
12
+ describe("#MAX_TICK", () => {
13
+ it("equals correct value", () => {
14
+ expect(TickMath.MAX_TICK).toEqual(443636);
15
+ });
16
+ });
17
+
18
+ describe("#getSqrtRatioAtTick", () => {
19
+ it("throws for non integer", () => {
20
+ expect(() => TickMath.getSqrtRatioAtTick(1.5)).toThrow("TICK");
21
+ });
22
+
23
+ it("throws for tick too small", () => {
24
+ expect(() => TickMath.getSqrtRatioAtTick(TickMath.MIN_TICK - 1)).toThrow(
25
+ "TICK"
26
+ );
27
+ });
28
+
29
+ it("throws for tick too large", () => {
30
+ expect(() => TickMath.getSqrtRatioAtTick(TickMath.MAX_TICK + 1)).toThrow(
31
+ "TICK"
32
+ );
33
+ });
34
+
35
+ it("returns the correct value for min tick", () => {
36
+ expect(TickMath.getSqrtRatioAtTick(TickMath.MIN_TICK)).toEqual(
37
+ TickMath.MIN_SQRT_RATIO
38
+ );
39
+ });
40
+
41
+ it("returns the correct value for tick 0", () => {
42
+ expect(TickMath.getSqrtRatioAtTick(0)).toEqual(
43
+ JSBI.leftShift(JSBI.BigInt(1), JSBI.BigInt(64))
44
+ );
45
+ });
46
+
47
+ it("returns the correct value for max tick", () => {
48
+ expect(TickMath.getSqrtRatioAtTick(TickMath.MAX_TICK)).toEqual(
49
+ TickMath.MAX_SQRT_RATIO
50
+ );
51
+ });
52
+ });
53
+
54
+ describe("#getTickAtSqrtRatio", () => {
55
+ it("returns the correct value for sqrt ratio at min tick", () => {
56
+ expect(TickMath.getTickAtSqrtRatio(TickMath.MIN_SQRT_RATIO)).toEqual(
57
+ TickMath.MIN_TICK
58
+ );
59
+ });
60
+ it("returns the correct value for sqrt ratio at max tick", () => {
61
+ expect(
62
+ TickMath.getTickAtSqrtRatio(JSBI.subtract(TickMath.MAX_SQRT_RATIO, ONE))
63
+ ).toEqual(TickMath.MAX_TICK - 1);
64
+ });
65
+ });
66
+ });
@@ -0,0 +1,58 @@
1
+ import { Token } from "entities/token";
2
+
3
+ describe("Token", () => {
4
+ const CONTRACT_ONE = "contract1";
5
+ const CONTRACT_TWO = "contract2";
6
+
7
+ describe("#constructor", () => {
8
+ it("fails with negative decimals", () => {
9
+ expect(() => new Token(CONTRACT_ONE, -1, "ABC")).toThrow("DECIMALS");
10
+ });
11
+ it("fails with 256 decimals", () => {
12
+ expect(() => new Token(CONTRACT_ONE, 256, "ABC")).toThrow("DECIMALS");
13
+ });
14
+ it("fails with non-integer decimals", () => {
15
+ expect(() => new Token(CONTRACT_ONE, 1.5, "ABC")).toThrow("DECIMALS");
16
+ });
17
+ });
18
+
19
+ describe("#equals", () => {
20
+ it("fails if contract differs", () => {
21
+ expect(
22
+ new Token(CONTRACT_ONE, 8, "ABC").equals(
23
+ new Token(CONTRACT_TWO, 8, "ABC")
24
+ )
25
+ ).toBe(false);
26
+ });
27
+
28
+ it("true if contract and symbol are the same", () => {
29
+ expect(
30
+ new Token(CONTRACT_ONE, 8, "ABC").equals(
31
+ new Token(CONTRACT_ONE, 8, "ABC")
32
+ )
33
+ ).toBe(true);
34
+ });
35
+
36
+ it("true even if name differ", () => {
37
+ const tokenA = new Token(CONTRACT_ONE, 9, "abc", "def");
38
+ const tokenB = new Token(CONTRACT_ONE, 9, "abc", "jkl");
39
+ expect(tokenA.equals(tokenB)).toBe(true);
40
+ });
41
+ });
42
+
43
+ describe("#sortsBefore", () => {
44
+ it("correct cases", () => {
45
+ expect(
46
+ new Token('eosio.token', 4, "TLOS").sortsBefore(
47
+ new Token('vapaeetokens', 6, "GUX")
48
+ )
49
+ ).toBe(true);
50
+
51
+ expect(
52
+ new Token('vapaeetokens', 0, "SEXCOIN").sortsBefore(
53
+ new Token('vapaeetokens', 0, "PRTYFUN")
54
+ )
55
+ ).toBe(true);
56
+ });
57
+ });
58
+ });
@@ -0,0 +1,210 @@
1
+ import fs from "fs";
2
+ import { Pool } from "entities/pool";
3
+ import { Token } from "entities/token";
4
+ import { Trade } from "entities/trade";
5
+ import { asset, symbol } from "eos-common";
6
+ import { CurrencyAmount } from "../src/entities/fractions/currencyAmount";
7
+ import JSBI from "jsbi";
8
+ import { FeeAmount, TICK_SPACINGS } from "internalConstants";
9
+ import { encodeSqrtRatioX64 } from "utils/encodeSqrtRatioX64";
10
+ import { TickMath } from "utils/tickMath";
11
+ import { nearestUsableTick } from "utils/nearestUsableTick";
12
+ import { sqrt } from "utils/sqrt";
13
+
14
+ function parseToken(token) {
15
+ return new Token(
16
+ token.contract,
17
+ asset(token.quantity).symbol.precision(),
18
+ asset(token.quantity).symbol.code().to_string(),
19
+ (
20
+ asset(token.quantity).symbol.code().to_string() +
21
+ "-" +
22
+ token.contract
23
+ ).toLowerCase()
24
+ );
25
+ }
26
+
27
+ describe("Trade", () => {
28
+ const token0 = new Token(
29
+ "0x0000000000000000000000000000000000000001",
30
+ 8,
31
+ "t0",
32
+ "token0"
33
+ );
34
+ const token1 = new Token(
35
+ "0x0000000000000000000000000000000000000002",
36
+ 8,
37
+ "t1",
38
+ "token1"
39
+ );
40
+ const token2 = new Token(
41
+ "0x0000000000000000000000000000000000000003",
42
+ 8,
43
+ "t2",
44
+ "token2"
45
+ );
46
+ const token3 = new Token(
47
+ "0x0000000000000000000000000000000000000004",
48
+ 8,
49
+ "t3",
50
+ "token3"
51
+ );
52
+
53
+ function v2StylePool(
54
+ id: number,
55
+ reserve0: CurrencyAmount<Token>,
56
+ reserve1: CurrencyAmount<Token>,
57
+ feeAmount: FeeAmount = FeeAmount.MEDIUM
58
+ ) {
59
+ console.log("feeAmount", feeAmount);
60
+ const sqrtRatioX64 = encodeSqrtRatioX64(
61
+ reserve1.quotient,
62
+ reserve0.quotient
63
+ );
64
+ const liquidity = sqrt(
65
+ JSBI.multiply(reserve0.quotient, reserve1.quotient)
66
+ );
67
+ return new Pool({
68
+ id,
69
+ tokenA: reserve0.currency,
70
+ tokenB: reserve1.currency,
71
+ fee: feeAmount,
72
+ sqrtPriceX64: sqrtRatioX64,
73
+ liquidity,
74
+ tickCurrent: TickMath.getTickAtSqrtRatio(sqrtRatioX64),
75
+ ticks: [
76
+ {
77
+ id: nearestUsableTick(TickMath.MIN_TICK, TICK_SPACINGS[feeAmount]),
78
+ liquidityNet: liquidity,
79
+ liquidityGross: liquidity,
80
+ },
81
+ {
82
+ id: nearestUsableTick(TickMath.MAX_TICK, TICK_SPACINGS[feeAmount]),
83
+ liquidityNet: JSBI.multiply(liquidity, JSBI.BigInt(-1)),
84
+ liquidityGross: liquidity,
85
+ },
86
+ ],
87
+ });
88
+ }
89
+ const pool_0_1 = v2StylePool(
90
+ 0,
91
+ CurrencyAmount.fromRawAmount(token0, 100000),
92
+ CurrencyAmount.fromRawAmount(token1, 100000)
93
+ );
94
+ const pool_0_2 = v2StylePool(
95
+ 1,
96
+ CurrencyAmount.fromRawAmount(token0, 100000),
97
+ CurrencyAmount.fromRawAmount(token2, 110000)
98
+ );
99
+ const pool_0_3 = v2StylePool(
100
+ 2,
101
+ CurrencyAmount.fromRawAmount(token0, 100000),
102
+ CurrencyAmount.fromRawAmount(token3, 90000)
103
+ );
104
+ const pool_1_2 = v2StylePool(
105
+ 3,
106
+ CurrencyAmount.fromRawAmount(token1, 120000),
107
+ CurrencyAmount.fromRawAmount(token2, 100000)
108
+ );
109
+ const pool_1_3 = v2StylePool(
110
+ 4,
111
+ CurrencyAmount.fromRawAmount(token1, 120000),
112
+ CurrencyAmount.fromRawAmount(token3, 130000)
113
+ );
114
+ describe('#bestTradeExactIn', () => {
115
+ it('throws with empty pools', async () => {
116
+ await expect(
117
+ Trade.bestTradeExactIn([], CurrencyAmount.fromRawAmount(token0, JSBI.BigInt(10000)), token2)
118
+ ).rejects.toThrow('POOLS')
119
+ })
120
+ it('throws with max hops of 0', async () => {
121
+ await expect(
122
+ Trade.bestTradeExactIn([pool_0_2], CurrencyAmount.fromRawAmount(token0, JSBI.BigInt(10000)), token2, {
123
+ maxHops: 0
124
+ })
125
+ ).rejects.toThrow('MAX_HOPS')
126
+ })
127
+
128
+ it.only('provides best route', async () => {
129
+ const result = await Trade.bestTradeExactIn(
130
+ [pool_0_1, pool_0_2, pool_1_2],
131
+ CurrencyAmount.fromRawAmount(token0, 10000),
132
+ token2
133
+ )
134
+ console.log("result", result)
135
+ expect(result).toHaveLength(2)
136
+ console.log("result[0].swaps[0].route.tokenPath", result[0].swaps[0].route.tokenPath)
137
+ console.log("result[0].swaps[0].route.pools", result[0].swaps[0].route.pools)
138
+ expect(result[0].swaps[0].route.pools).toHaveLength(1) // 0 -> 2 at 10:11
139
+ expect(result[0].swaps[0].route.tokenPath).toEqual([token0, token2])
140
+ expect(result[0].inputAmount.equalTo(CurrencyAmount.fromRawAmount(token0, JSBI.BigInt(10000)))).toBeTruthy()
141
+ expect(result[0].outputAmount.equalTo(CurrencyAmount.fromRawAmount(token2, JSBI.BigInt(9971)))).toBeTruthy()
142
+ expect(result[1].swaps[0].route.pools).toHaveLength(2) // 0 -> 1 -> 2 at 12:12:10
143
+ expect(result[1].swaps[0].route.tokenPath).toEqual([token0, token1, token2])
144
+ expect(result[1].inputAmount.equalTo(CurrencyAmount.fromRawAmount(token0, JSBI.BigInt(10000)))).toBeTruthy()
145
+ expect(result[1].outputAmount.equalTo(CurrencyAmount.fromRawAmount(token2, JSBI.BigInt(7004)))).toBeTruthy()
146
+ })
147
+ })
148
+
149
+ describe("#bestTradeExactOut", () => {
150
+ it("throws with empty pools", async () => {
151
+ await expect(
152
+ Trade.bestTradeExactOut(
153
+ [],
154
+ token0,
155
+ CurrencyAmount.fromRawAmount(token2, JSBI.BigInt(100))
156
+ )
157
+ ).rejects.toThrow("POOLS");
158
+ });
159
+
160
+ it("throws with max hops of 0", async () => {
161
+ await expect(
162
+ Trade.bestTradeExactOut(
163
+ [pool_0_2],
164
+ token0,
165
+ CurrencyAmount.fromRawAmount(token2, JSBI.BigInt(100)),
166
+ {
167
+ maxHops: 0,
168
+ }
169
+ )
170
+ ).rejects.toThrow("MAX_HOPS");
171
+ });
172
+
173
+ it("provides best route", async () => {
174
+ const result = await Trade.bestTradeExactOut(
175
+ [pool_0_1, pool_0_2, pool_1_2],
176
+ token0,
177
+ CurrencyAmount.fromRawAmount(token2, 10000)
178
+ );
179
+ expect(result).toHaveLength(2);
180
+ expect(result[0].swaps[0].route.pools).toHaveLength(1); // 0 -> 2 at 10:11
181
+ expect(result[0].swaps[0].route.tokenPath).toEqual([token0, token2]);
182
+ expect(
183
+ result[0].inputAmount.equalTo(
184
+ CurrencyAmount.fromRawAmount(token0, 10032)
185
+ )
186
+ ).toBeTruthy();
187
+ expect(
188
+ result[0].outputAmount.equalTo(
189
+ CurrencyAmount.fromRawAmount(token2, 10000)
190
+ )
191
+ ).toBeTruthy();
192
+ expect(result[1].swaps[0].route.pools).toHaveLength(2); // 0 -> 1 -> 2 at 12:12:10
193
+ expect(result[1].swaps[0].route.tokenPath).toEqual([
194
+ token0,
195
+ token1,
196
+ token2,
197
+ ]);
198
+ expect(
199
+ result[1].inputAmount.equalTo(
200
+ CurrencyAmount.fromRawAmount(token0, 15488)
201
+ )
202
+ ).toBeTruthy();
203
+ expect(
204
+ result[1].outputAmount.equalTo(
205
+ CurrencyAmount.fromRawAmount(token2, 10000)
206
+ )
207
+ ).toBeTruthy();
208
+ });
209
+ });
210
+ });
package/test2.ts ADDED
@@ -0,0 +1,73 @@
1
+ import fetch from 'node-fetch'
2
+
3
+ // Alcor v2 sdk: https://github.com/alcorexchange/alcor-v2-sdk
4
+ import { Token, Position, Pool } from './src'
5
+
6
+ import { asset } from 'eos-common'
7
+ import { JsonRpc } from 'eosjs'
8
+ import { Serialize } from 'eosjs'
9
+
10
+
11
+ export function parseToken(token) {
12
+ return new Token(
13
+ token.contract,
14
+ asset(token.quantity).symbol.precision(),
15
+ asset(token.quantity).symbol.code().to_string(),
16
+ (asset(token.quantity).symbol.code().to_string() + '-' + token.contract).toLowerCase()
17
+ )
18
+ }
19
+
20
+ const rpc = new JsonRpc('https://waxnode02.alcor.exchange', { fetch });
21
+
22
+ const types: any = Serialize.createInitialTypes()
23
+
24
+ export const nameToUint64 = (name) => {
25
+ const ser = new Serialize.SerialBuffer()
26
+ ser.pushName(name)
27
+ return types.get('uint64').deserialize(ser)
28
+ }
29
+
30
+ async function main() {
31
+ const account = '3mob2.wam'
32
+
33
+ const { rows: pools } = await rpc.get_table_rows({
34
+ scope: 'swap.alcor',
35
+ table: 'pools',
36
+ code: 'swap.alcor',
37
+ })
38
+
39
+ // First pool for example (TLM / WAX)
40
+ const { tokenA, tokenB, currSlot: { sqrtPriceX64, tick } } = pools[0]
41
+
42
+ const pool = new Pool({
43
+ ...pools[0],
44
+ tokenA: parseToken(tokenA),
45
+ tokenB: parseToken(tokenB),
46
+ sqrtPriceX64,
47
+ tickCurrent: tick
48
+ })
49
+
50
+ const { rows: positions } = await rpc.get_table_rows({
51
+ scope: pool.id,
52
+ table: 'positions',
53
+ code: 'swap.alcor',
54
+ key_type: 'i64',
55
+ index_position: 3,
56
+ lower_bound: nameToUint64(account),
57
+ upper_bound: nameToUint64(account)
58
+ })
59
+
60
+
61
+ const position = new Position({
62
+ ...positions[0], // Only first of account position
63
+ pool
64
+ })
65
+
66
+ console.log('amountA:', position.amountA.toAsset())
67
+ console.log('amountB:', position.amountB.toAsset())
68
+ }
69
+
70
+ main()
71
+
72
+ // amountA: 103.4332 TLM
73
+ // amountB: 29.16056021 WAX
package/tsconfig.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
4
+ "lib": ["es6"], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
5
+ "module": "commonjs", /* Specify what module code is generated. */
6
+ "rootDir": "src", /* Specify the root folder within your source files. */
7
+ "resolveJsonModule": true, /* Enable importing .json files. */
8
+ "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
9
+ "outDir": "build", /* Specify an output folder for all emitted files. */
10
+ "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
11
+ "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
12
+ "strict": true, /* Enable all strict type-checking options. */
13
+ "noImplicitAny": false, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
14
+ "types": [
15
+ "node"
16
+ ],
17
+ "skipLibCheck": true /* Skip type checking all .d.ts files. */
18
+ },
19
+ "include": [
20
+ "src/**/*.ts"],
21
+ "exclude": [
22
+ "node_modules"
23
+ ],
24
+ }