stackswap-front-api-test-02 1.0.82 → 1.0.85

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 (93) hide show
  1. package/README.md +10 -10
  2. package/dist/esm/index.d.ts +84 -84
  3. package/dist/esm/index.js +143 -143
  4. package/dist/esm/stackswap/config.d.ts +112 -112
  5. package/dist/esm/stackswap/config.js +185 -185
  6. package/dist/esm/stackswap/manager/farm.manager.d.ts +41 -41
  7. package/dist/esm/stackswap/manager/farm.manager.js +293 -293
  8. package/dist/esm/stackswap/manager/farm2.manager.d.ts +60 -60
  9. package/dist/esm/stackswap/manager/farm2.manager.js +403 -403
  10. package/dist/esm/stackswap/manager/farm2.manager.js.map +1 -1
  11. package/dist/esm/stackswap/manager/governance.manager.d.ts +9 -9
  12. package/dist/esm/stackswap/manager/governance.manager.js +53 -53
  13. package/dist/esm/stackswap/manager/launchpad.manager.d.ts +13 -13
  14. package/dist/esm/stackswap/manager/launchpad.manager.js +147 -147
  15. package/dist/esm/stackswap/manager/lbtc.manager.d.ts +31 -31
  16. package/dist/esm/stackswap/manager/lbtc.manager.js +326 -326
  17. package/dist/esm/stackswap/manager/multisig.manager.d.ts +11 -11
  18. package/dist/esm/stackswap/manager/multisig.manager.js +51 -51
  19. package/dist/esm/stackswap/manager/nft.manager.d.ts +52 -52
  20. package/dist/esm/stackswap/manager/nft.manager.js +243 -243
  21. package/dist/esm/stackswap/manager/operator.manager.d.ts +17 -17
  22. package/dist/esm/stackswap/manager/operator.manager.js +85 -85
  23. package/dist/esm/stackswap/manager/other.manager.d.ts +9 -9
  24. package/dist/esm/stackswap/manager/other.manager.js +47 -47
  25. package/dist/esm/stackswap/manager/pool.manager.d.ts +34 -34
  26. package/dist/esm/stackswap/manager/pool.manager.js +171 -175
  27. package/dist/esm/stackswap/manager/pool.manager.js.map +1 -1
  28. package/dist/esm/stackswap/manager/poxl.manager.d.ts +16 -16
  29. package/dist/esm/stackswap/manager/poxl.manager.js +71 -71
  30. package/dist/esm/stackswap/manager/staking.manager.d.ts +25 -25
  31. package/dist/esm/stackswap/manager/staking.manager.js +233 -233
  32. package/dist/esm/stackswap/manager/swap.manager.d.ts +35 -35
  33. package/dist/esm/stackswap/manager/swap.manager.js +290 -290
  34. package/dist/esm/stackswap/manager/token.manager.d.ts +31 -31
  35. package/dist/esm/stackswap/manager/token.manager.js +133 -135
  36. package/dist/esm/stackswap/manager/token.manager.js.map +1 -1
  37. package/dist/esm/stackswap/util.d.ts +35 -35
  38. package/dist/esm/stackswap/util.js +201 -201
  39. package/dist/index.d.ts +84 -84
  40. package/dist/index.js +152 -152
  41. package/dist/index.umd.js +1 -1
  42. package/dist/stackswap/config.d.ts +112 -112
  43. package/dist/stackswap/config.js +190 -190
  44. package/dist/stackswap/manager/farm.manager.d.ts +41 -41
  45. package/dist/stackswap/manager/farm.manager.js +301 -301
  46. package/dist/stackswap/manager/farm2.manager.d.ts +60 -60
  47. package/dist/stackswap/manager/farm2.manager.js +410 -410
  48. package/dist/stackswap/manager/farm2.manager.js.map +1 -1
  49. package/dist/stackswap/manager/governance.manager.d.ts +9 -9
  50. package/dist/stackswap/manager/governance.manager.js +60 -60
  51. package/dist/stackswap/manager/launchpad.manager.d.ts +13 -13
  52. package/dist/stackswap/manager/launchpad.manager.js +151 -151
  53. package/dist/stackswap/manager/lbtc.manager.d.ts +31 -31
  54. package/dist/stackswap/manager/lbtc.manager.js +333 -333
  55. package/dist/stackswap/manager/multisig.manager.d.ts +11 -11
  56. package/dist/stackswap/manager/multisig.manager.js +55 -55
  57. package/dist/stackswap/manager/nft.manager.d.ts +52 -52
  58. package/dist/stackswap/manager/nft.manager.js +250 -250
  59. package/dist/stackswap/manager/operator.manager.d.ts +17 -17
  60. package/dist/stackswap/manager/operator.manager.js +92 -92
  61. package/dist/stackswap/manager/other.manager.d.ts +9 -9
  62. package/dist/stackswap/manager/other.manager.js +54 -54
  63. package/dist/stackswap/manager/pool.manager.d.ts +34 -34
  64. package/dist/stackswap/manager/pool.manager.js +179 -183
  65. package/dist/stackswap/manager/pool.manager.js.map +1 -1
  66. package/dist/stackswap/manager/poxl.manager.d.ts +16 -16
  67. package/dist/stackswap/manager/poxl.manager.js +78 -78
  68. package/dist/stackswap/manager/staking.manager.d.ts +25 -25
  69. package/dist/stackswap/manager/staking.manager.js +240 -240
  70. package/dist/stackswap/manager/swap.manager.d.ts +35 -35
  71. package/dist/stackswap/manager/swap.manager.js +297 -297
  72. package/dist/stackswap/manager/token.manager.d.ts +31 -31
  73. package/dist/stackswap/manager/token.manager.js +141 -143
  74. package/dist/stackswap/manager/token.manager.js.map +1 -1
  75. package/dist/stackswap/util.d.ts +35 -35
  76. package/dist/stackswap/util.js +218 -218
  77. package/package.json +43 -43
  78. package/src/index.ts +204 -204
  79. package/src/stackswap/config.ts +292 -292
  80. package/src/stackswap/manager/farm.manager.ts +399 -399
  81. package/src/stackswap/manager/farm2.manager.ts +463 -463
  82. package/src/stackswap/manager/governance.manager.ts +95 -95
  83. package/src/stackswap/manager/launchpad.manager.ts +190 -190
  84. package/src/stackswap/manager/multisig.manager.ts +87 -87
  85. package/src/stackswap/manager/nft.manager.ts +311 -311
  86. package/src/stackswap/manager/operator.manager.ts +123 -123
  87. package/src/stackswap/manager/other.manager.ts +71 -71
  88. package/src/stackswap/manager/pool.manager.ts +202 -202
  89. package/src/stackswap/manager/poxl.manager.ts +99 -99
  90. package/src/stackswap/manager/staking.manager.ts +321 -321
  91. package/src/stackswap/manager/swap.manager.ts +351 -351
  92. package/src/stackswap/manager/token.manager.ts +167 -167
  93. package/src/stackswap/util.ts +237 -237
@@ -1,291 +1,291 @@
1
- import BigNumber from "bignumber.js";
2
- import { falseCV, FungibleConditionCode, stringAsciiCV, trueCV, uintCV } from "@stacks/transactions";
3
- import { openContractCall } from "@stacks/connect";
4
- import { decimal2integer, getPostConditionFromAsset, getWriteOptions, parseAddressToCV } from "../util";
5
- import { LiquidityPool } from "./pool.manager";
6
- import { BaseToken, Token } from "./token.manager";
7
- import axios from "axios";
8
- export var SwapType;
9
- (function (SwapType) {
10
- SwapType[SwapType["NO_ROUTE"] = 0] = "NO_ROUTE";
11
- SwapType[SwapType["ON_LOADING"] = 1] = "ON_LOADING";
12
- SwapType[SwapType["SINGLE"] = 2] = "SINGLE";
13
- SwapType[SwapType["ROUTER_STX"] = 10] = "ROUTER_STX";
14
- SwapType[SwapType["ROUTER_STSW"] = 11] = "ROUTER_STSW";
15
- })(SwapType || (SwapType = {}));
16
- export class SwapManager {
17
- constructor(stackswap) {
18
- this.stackswap = stackswap;
19
- }
20
- async findRouter(token_x, token_y, current_block) {
21
- const url = this.stackswap.config.STACKS_BACKEND_URL() + '/api/v1/swap/' + token_x.addr + '/' + token_y.addr + '/' + current_block;
22
- const result = await axios.get(url, { timeout: 50000, });
23
- const res = result.data;
24
- if (Object.keys(res).length === 0) {
25
- return { valid: SwapType.NO_ROUTE };
26
- }
27
- else if (res.type === 'SWAP') {
28
- return {
29
- valid: SwapType.SINGLE,
30
- x_to_y: res.direction,
31
- pair: LiquidityPool.parsePoolsFromServerData2(res.pair, token_x, token_y, res.direction)
32
- };
33
- }
34
- else if (res.type === 'RouterSWAP') {
35
- if (res.bridge_token === 'STX') {
36
- return {
37
- valid: SwapType.ROUTER_STX,
38
- router_from_pair: LiquidityPool.parsePoolsFromServerData2(res.from_pair, token_x, Token.getBaseTokens(this.stackswap, BaseToken.STX), res.from_direction),
39
- router_to_pair: LiquidityPool.parsePoolsFromServerData2(res.to_pair, Token.getBaseTokens(this.stackswap, BaseToken.STX), token_y, res.to_direction),
40
- router_from_mode: res.from_direction,
41
- router_to_mode: res.to_direction
42
- };
43
- }
44
- else if (res.bridge_token === 'STSW') {
45
- return {
46
- valid: SwapType.ROUTER_STSW,
47
- router_from_pair: LiquidityPool.parsePoolsFromServerData2(res.from_pair, token_x, Token.getBaseTokens(this.stackswap, BaseToken.STSW), res.from_direction),
48
- router_to_pair: LiquidityPool.parsePoolsFromServerData2(res.to_pair, Token.getBaseTokens(this.stackswap, BaseToken.STSW), token_y, res.to_direction),
49
- router_from_mode: res.from_direction,
50
- router_to_mode: res.to_direction
51
- };
52
- }
53
- else {
54
- return { valid: SwapType.NO_ROUTE };
55
- }
56
- }
57
- else {
58
- return { valid: SwapType.NO_ROUTE };
59
- }
60
- }
61
- async routerSwapContractCall(token_from_amount, token_to_amount, token_bridge_amount, pair_from, pair_to, router_from_mode, router_to_mode, tolerance, callback = null) {
62
- const token_from = router_from_mode ? pair_from.token_x : pair_from.token_y;
63
- const token_bridge = router_from_mode ? pair_from.token_y : pair_from.token_x;
64
- const token_to = router_to_mode ? pair_to.token_y : pair_to.token_x;
65
- const from_amt = new BigNumber(10 ** token_from.decimal).multipliedBy(token_from_amount).integerValue();
66
- const bridge_amt = new BigNumber(10 ** token_bridge.decimal).multipliedBy(token_bridge_amount).integerValue();
67
- const to_amt = new BigNumber(10 ** token_to.decimal).multipliedBy(token_to_amount).integerValue();
68
- const bridge_min = bridge_amt.multipliedBy((100 - tolerance)).dividedBy(100).integerValue();
69
- const to_min = to_amt.multipliedBy((100 - tolerance)).dividedBy(100).multipliedBy((100 - tolerance)).dividedBy(100).integerValue();
70
- const token_from_addr = (token_from.addr);
71
- const token_bridge_addr = (token_bridge.addr);
72
- const token_to_addr = (token_to.addr);
73
- const pair_from_addr = pair_from.token_lp.addr;
74
- const pair_to_addr = pair_to.token_lp.addr;
75
- const from_mode = router_from_mode ? trueCV() : falseCV();
76
- const to_mode = router_to_mode ? trueCV() : falseCV();
77
- const post_condition = [];
78
- post_condition.push(await getPostConditionFromAsset(this.stackswap, this.stackswap.getSenderAddress(), token_from.addr, from_amt.toString(), FungibleConditionCode.Equal));
79
- post_condition.push(await getPostConditionFromAsset(this.stackswap, pair_from_addr, token_bridge.addr, bridge_min.toString(), FungibleConditionCode.GreaterEqual));
80
- post_condition.push(await getPostConditionFromAsset(this.stackswap, this.stackswap.getSenderAddress(), token_bridge.addr, bridge_min.toString(), FungibleConditionCode.GreaterEqual));
81
- post_condition.push(await getPostConditionFromAsset(this.stackswap, pair_to_addr, token_to.addr, to_min.toString(), FungibleConditionCode.GreaterEqual));
82
- const options = getWriteOptions(this.stackswap, this.stackswap.config.CONTRACT_NAME_STACKSWAP_ROUTER_SWAP(), 'router-swap', [
83
- parseAddressToCV(token_from_addr),
84
- parseAddressToCV(token_bridge_addr),
85
- parseAddressToCV(token_to_addr),
86
- parseAddressToCV(pair_from_addr),
87
- parseAddressToCV(pair_to_addr),
88
- from_mode,
89
- to_mode,
90
- uintCV(from_amt.toString()),
91
- uintCV(bridge_min.toString()),
92
- uintCV(to_min.toString())
93
- ], post_condition, callback);
94
- openContractCall(options);
95
- }
96
- async swapContractCall(token_x_amount, token_y_amount, pair, x_to_y, tolerance, callback = null) {
97
- const token_x = x_to_y ? pair.token_x : pair.token_y;
98
- const token_y = x_to_y ? pair.token_y : pair.token_x;
99
- const dx_bn = new BigNumber(10 ** token_x.decimal).multipliedBy(token_x_amount);
100
- const dy_min_bn = new BigNumber(10 ** token_y.decimal).multipliedBy(token_y_amount).multipliedBy(100 - tolerance).dividedBy(100);
101
- const dx = dx_bn.toFixed(0).toString();
102
- const dy_min = dy_min_bn.toFixed(0).toString();
103
- const functionName = x_to_y ? 'swap-x-for-y' : 'swap-y-for-x';
104
- const token_x_addr = x_to_y ? (token_x.addr) : (token_y.addr);
105
- const token_y_addr = x_to_y ? (token_y.addr) : (token_x.addr);
106
- const post_condition = [];
107
- post_condition.push(await getPostConditionFromAsset(this.stackswap, this.stackswap.getSenderAddress(), token_x.addr, dx, FungibleConditionCode.Equal));
108
- post_condition.push(await getPostConditionFromAsset(this.stackswap, pair.token_lp.addr, token_y.addr, dy_min, FungibleConditionCode.GreaterEqual));
109
- const options = getWriteOptions(this.stackswap, this.stackswap.config.CONTRACT_NAME_STACKSWAP_SWAP(), functionName, [
110
- parseAddressToCV(token_x_addr), parseAddressToCV(token_y_addr),
111
- parseAddressToCV(pair.token_lp.addr), uintCV(dx), uintCV(dy_min)
112
- ], post_condition, callback);
113
- openContractCall(options);
114
- }
115
- async importContractCall(pair, x_to_y, amount_x, amount_y, callback = null) {
116
- const token_x = x_to_y ? pair.token_x : pair.token_y;
117
- const token_y = x_to_y ? pair.token_y : pair.token_x;
118
- const dx_temp = new BigNumber(10 ** token_x.decimal).multipliedBy(amount_x);
119
- const dy_temp = new BigNumber(10 ** token_y.decimal).multipliedBy(amount_y);
120
- const dx = x_to_y ? dx_temp : dy_temp;
121
- const dy = x_to_y ? dy_temp : dx_temp;
122
- const dx_out = dx_temp.multipliedBy(1.2).toFixed(0).toString();
123
- const dy_out = dy_temp.multipliedBy(1.2).toFixed(0).toString();
124
- const token_x_addr = x_to_y ? (token_x.addr) : (token_y.addr);
125
- const token_y_addr = x_to_y ? (token_y.addr) : (token_x.addr);
126
- const post_condition = [];
127
- post_condition.push(await getPostConditionFromAsset(this.stackswap, this.stackswap.getSenderAddress(), token_x.addr, dx_out, FungibleConditionCode.LessEqual));
128
- post_condition.push(await getPostConditionFromAsset(this.stackswap, this.stackswap.getSenderAddress(), token_y.addr, dy_out, FungibleConditionCode.LessEqual));
129
- const options = getWriteOptions(this.stackswap, this.stackswap.config.CONTRACT_NAME_STACKSWAP_SWAP(), 'add-to-position', [
130
- parseAddressToCV(token_x_addr), parseAddressToCV(token_y_addr),
131
- parseAddressToCV(pair.token_lp.addr), uintCV(dx.toFixed(0).toString()), uintCV(dy.toFixed(0).toString())
132
- ], post_condition, callback);
133
- openContractCall(options);
134
- }
135
- async removeContractCall(pair, percent, user_lp_total, dx, dy, swap_contract = this.stackswap.config.CONTRACT_NAME_STACKSWAP_SWAP(), callback = null) {
136
- const token_x_addr = pair.token_x.addr;
137
- const token_y_addr = pair.token_y.addr;
138
- const pair_addr = pair.token_lp.addr;
139
- const post_condition = [];
140
- const burn_amount = new BigNumber(user_lp_total).multipliedBy(percent * 1.01).div(100).toFixed(0).toString();
141
- post_condition.push(await getPostConditionFromAsset(this.stackswap, pair.token_lp.addr, pair.token_x.addr, dx, FungibleConditionCode.GreaterEqual));
142
- post_condition.push(await getPostConditionFromAsset(this.stackswap, pair.token_lp.addr, pair.token_y.addr, dy, FungibleConditionCode.GreaterEqual));
143
- post_condition.push(await getPostConditionFromAsset(this.stackswap, this.stackswap.getSenderAddress(), pair.token_lp.addr, burn_amount, FungibleConditionCode.LessEqual));
144
- const options = getWriteOptions(this.stackswap, swap_contract, 'reduce-position', [
145
- parseAddressToCV(token_x_addr), parseAddressToCV(token_y_addr),
146
- parseAddressToCV(pair_addr), uintCV(percent),
147
- ], post_condition, callback);
148
- openContractCall(options);
149
- }
150
- async createContractCall(token_x, token_y, token_x_amount, token_y_amount, liquidity_token_addr, callback = null) {
151
- const token_x_addr = token_x.addr;
152
- const token_y_addr = token_y.addr;
153
- const pair_addr = liquidity_token_addr;
154
- const post_condition = [];
155
- const amount_X = decimal2integer(token_x_amount, token_x.decimal);
156
- const amount_Y = decimal2integer(token_y_amount, token_y.decimal);
157
- post_condition.push(await getPostConditionFromAsset(this.stackswap, this.stackswap.getSenderAddress(), token_x.addr, amount_X, FungibleConditionCode.Equal));
158
- post_condition.push(await getPostConditionFromAsset(this.stackswap, this.stackswap.getSenderAddress(), token_y.addr, amount_Y, FungibleConditionCode.Equal));
159
- const options = getWriteOptions(this.stackswap, this.stackswap.config.CONTRACT_NAME_STACKSWAP_ONE_STEP_MINT(), 'create-pair-new-liquidity-token', [
160
- parseAddressToCV(token_x_addr), parseAddressToCV(token_y_addr),
161
- parseAddressToCV(pair_addr), stringAsciiCV(token_x.symbol + '-' + token_y.symbol),
162
- uintCV(amount_X), uintCV(amount_Y), parseAddressToCV(pair_addr),
163
- parseAddressToCV(this.stackswap.getQualifiedAddress(this.stackswap.config.CONTRACT_NAME_STACKSWAP_SWAP())),
164
- ], post_condition, callback);
165
- openContractCall(options);
166
- }
167
- price_impact(x_to_y, pair, token_x_amount) {
168
- try {
169
- const balance_x = x_to_y ? pair.balance_x : pair.balance_y;
170
- const token_x_decimal = x_to_y ? pair.token_x.decimal : pair.token_y.decimal;
171
- const balance_y = x_to_y ? pair.balance_y : pair.balance_y;
172
- const dx = new BigNumber(token_x_amount).multipliedBy(10 ** token_x_decimal);
173
- if (dx.toNumber() > new BigNumber(balance_x).toNumber()) {
174
- return 100;
175
- }
176
- const dy_1 = new BigNumber(balance_y).multipliedBy(dx);
177
- const dy_2 = new BigNumber(balance_x).plus(dx);
178
- const dy = (dy_1.toNumber()) / (dy_2.toNumber());
179
- const input_price = new BigNumber(10 ** 10).multipliedBy(new BigNumber(balance_x)).dividedBy(new BigNumber(balance_y));
180
- const output_price = new BigNumber(10 ** 10).multipliedBy(new BigNumber(balance_x)).plus(dx).dividedBy(new BigNumber(balance_y).minus(dy));
181
- const price_impact2 = output_price.minus(input_price).multipliedBy(10000).dividedBy(input_price);
182
- let price_impact = price_impact2.toNumber();
183
- if (price_impact >= 10000) {
184
- price_impact = 9999;
185
- }
186
- return (price_impact / 100).toFixed(5);
187
- }
188
- catch (e) {
189
- return 0;
190
- }
191
- }
192
- price_impact_router(router_from_mode, router_to_mode, pair_from, pair_to, token_from_amount) {
193
- try {
194
- const balance_x = router_from_mode ? pair_from.balance_x : pair_from.balance_y;
195
- const token_from_decimal = router_from_mode ? pair_from.token_x.decimal : pair_from.token_y.decimal;
196
- const balance_y = router_to_mode ? pair_to.balance_y : pair_to.balance_x;
197
- const dx = new BigNumber(token_from_amount).multipliedBy(10 ** token_from_decimal);
198
- if (dx.toNumber() > new BigNumber(balance_x).toNumber()) {
199
- return 100;
200
- }
201
- const dy_1 = new BigNumber(balance_y).multipliedBy(dx);
202
- const dy_2 = new BigNumber(balance_x).plus(dx);
203
- const dy = (dy_1.toNumber()) / (dy_2.toNumber());
204
- const input_price = new BigNumber(10 ** 10).multipliedBy(new BigNumber(balance_x)).dividedBy(new BigNumber(balance_y));
205
- const output_price = new BigNumber(10 ** 10).multipliedBy(new BigNumber(balance_x)).plus(dx).dividedBy(new BigNumber(balance_y).minus(dy));
206
- const price_impact2 = output_price.minus(input_price).multipliedBy(10000).dividedBy(input_price);
207
- let price_impact = price_impact2.toNumber();
208
- if (price_impact >= 10000) {
209
- price_impact = 9999;
210
- }
211
- return (price_impact / 100).toFixed(5);
212
- }
213
- catch (e) {
214
- return 0;
215
- }
216
- }
217
- getPriceString(pair, token_x, token_y, price, price_invert) {
218
- try {
219
- if (pair != null && price < 0) {
220
- return 'dx exceed pool balance';
221
- }
222
- else if (token_x != null && token_y != null) {
223
- if (price_invert) {
224
- return ` 1 ${token_y.symbol} = ${new BigNumber((1 / price)).toFixed(token_x.decimal)} ${token_x.symbol}`;
225
- }
226
- else {
227
- return ` 1 ${token_x.symbol} = ${new BigNumber(price).toFixed(token_x.decimal)} ${token_y.symbol}`;
228
- }
229
- }
230
- return '';
231
- }
232
- catch (e) {
233
- return '';
234
- }
235
- }
236
- getPricesWithSwapData(swap_data, token_x_amount, price_invert) {
237
- let price = -1;
238
- switch (swap_data.valid) {
239
- case SwapType.SINGLE:
240
- try {
241
- const temp = swap_data.pair.getPoolPrice(!swap_data.x_to_y, Number(token_x_amount) <= 0 || token_x_amount == '' ? '-1' : token_x_amount);
242
- if (temp.output_price !== '0') {
243
- price = (1 / parseFloat(temp.output_price));
244
- }
245
- const price_strings = this.getPriceString(swap_data.pair, swap_data.pair.getTokenX(swap_data.x_to_y), swap_data.pair.getTokenY(swap_data.x_to_y), price, price_invert);
246
- const token_y_amount = new BigNumber(temp.dy).toFixed(Number(swap_data.pair.getTokenY(swap_data.x_to_y).decimal)).toString();
247
- return { price: price, dy: token_y_amount, price_strings: price_strings };
248
- }
249
- catch (e) {
250
- return { price: price, dy: '0', price_strings: '' };
251
- }
252
- case SwapType.ROUTER_STX:
253
- const bridge_token = Token.getBaseTokens(this.stackswap, BaseToken.STX);
254
- const from_res = swap_data.router_from_pair.getPoolPrice(!swap_data.router_from_mode, Number(token_x_amount) <= 0 || token_x_amount == '' ? '1' : token_x_amount);
255
- let router_from_price = -1;
256
- if (from_res.output_price !== '0') {
257
- router_from_price = (1 / parseFloat(from_res.output_price));
258
- }
259
- const router_bridge_amt = new BigNumber(new BigNumber(from_res.dy).toFixed(Number(bridge_token.decimal))).toString();
260
- const to_res = swap_data.router_to_pair.getPoolPrice(!swap_data.router_to_mode, Number(router_bridge_amt) <= 0 ? '-1' : router_bridge_amt);
261
- let router_to_price = -1;
262
- if (to_res.output_price !== '0') {
263
- router_to_price = (1 / parseFloat(to_res.output_price));
264
- }
265
- const token_y_amount2 = new BigNumber(to_res.dy).toFixed(Number(Number(token_x_amount) <= 0 || token_x_amount == '' ? 0 : swap_data.router_to_pair.getTokenY(swap_data.router_to_mode).decimal)).toString();
266
- price = router_from_price * router_to_price;
267
- const price_strings2 = this.getPriceString(swap_data.pair, swap_data.router_from_pair.getTokenX(swap_data.router_from_mode), swap_data.router_to_pair.getTokenY(swap_data.router_to_mode), price, price_invert);
268
- return { price: price, dy: token_y_amount2, price_strings: price_strings2, bridge_amount: router_bridge_amt };
269
- case SwapType.ROUTER_STSW:
270
- const bridge_token2 = Token.getBaseTokens(this.stackswap, BaseToken.STX);
271
- const from_res2 = swap_data.router_from_pair.getPoolPrice(!swap_data.router_from_mode, Number(token_x_amount) <= 0 || token_x_amount == '' ? '1' : token_x_amount);
272
- let router_from_price2 = -1;
273
- if (from_res2.output_price !== '0') {
274
- router_from_price2 = (1 / parseFloat(from_res2.output_price));
275
- }
276
- const router_bridge_amt2 = new BigNumber(new BigNumber(from_res2.dy).toFixed(Number(bridge_token2.decimal))).toString();
277
- const to_res2 = swap_data.router_to_pair.getPoolPrice(!swap_data.router_to_mode, Number(router_bridge_amt2) <= 0 ? '-1' : router_bridge_amt2);
278
- let router_to_price2 = -1;
279
- if (to_res2.output_price !== '0') {
280
- router_to_price2 = (1 / parseFloat(to_res2.output_price));
281
- }
282
- const token_y_amount3 = new BigNumber(to_res2.dy).toFixed(Number(Number(token_x_amount) <= 0 || token_x_amount == '' ? 0 : swap_data.router_to_pair.getTokenY(swap_data.router_to_mode).decimal)).toString();
283
- price = router_from_price2 * router_to_price2;
284
- const price_strings3 = this.getPriceString(swap_data.pair, swap_data.router_from_pair.getTokenX(swap_data.router_from_mode), swap_data.router_to_pair.getTokenY(swap_data.router_to_mode), price, price_invert);
285
- return { price: price, dy: token_y_amount3, price_strings: price_strings3, bridge_amount: router_bridge_amt2 };
286
- default:
287
- return { price: price, dy: '0', price_strings: '' };
288
- }
289
- }
290
- }
1
+ import BigNumber from "bignumber.js";
2
+ import { falseCV, FungibleConditionCode, stringAsciiCV, trueCV, uintCV } from "@stacks/transactions";
3
+ import { openContractCall } from "@stacks/connect";
4
+ import { decimal2integer, getPostConditionFromAsset, getWriteOptions, parseAddressToCV } from "../util";
5
+ import { LiquidityPool } from "./pool.manager";
6
+ import { BaseToken, Token } from "./token.manager";
7
+ import axios from "axios";
8
+ export var SwapType;
9
+ (function (SwapType) {
10
+ SwapType[SwapType["NO_ROUTE"] = 0] = "NO_ROUTE";
11
+ SwapType[SwapType["ON_LOADING"] = 1] = "ON_LOADING";
12
+ SwapType[SwapType["SINGLE"] = 2] = "SINGLE";
13
+ SwapType[SwapType["ROUTER_STX"] = 10] = "ROUTER_STX";
14
+ SwapType[SwapType["ROUTER_STSW"] = 11] = "ROUTER_STSW";
15
+ })(SwapType || (SwapType = {}));
16
+ export class SwapManager {
17
+ constructor(stackswap) {
18
+ this.stackswap = stackswap;
19
+ }
20
+ async findRouter(token_x, token_y, current_block) {
21
+ const url = this.stackswap.config.STACKS_BACKEND_URL() + '/api/v2/swap/' + token_x.addr + '/' + token_y.addr + '/' + current_block;
22
+ const result = await axios.get(url, { timeout: 50000, });
23
+ const res = result.data;
24
+ if (Object.keys(res).length === 0) {
25
+ return { valid: SwapType.NO_ROUTE };
26
+ }
27
+ else if (res.type === 'SWAP') {
28
+ return {
29
+ valid: SwapType.SINGLE,
30
+ x_to_y: res.direction,
31
+ pair: LiquidityPool.parsePoolsFromServerData2(res.pair, token_x, token_y, res.direction)
32
+ };
33
+ }
34
+ else if (res.type === 'RouterSWAP') {
35
+ if (res.bridge_token === 'STX') {
36
+ return {
37
+ valid: SwapType.ROUTER_STX,
38
+ router_from_pair: LiquidityPool.parsePoolsFromServerData2(res.from_pair, token_x, Token.getBaseTokens(this.stackswap, BaseToken.STX), res.from_direction),
39
+ router_to_pair: LiquidityPool.parsePoolsFromServerData2(res.to_pair, Token.getBaseTokens(this.stackswap, BaseToken.STX), token_y, res.to_direction),
40
+ router_from_mode: res.from_direction,
41
+ router_to_mode: res.to_direction
42
+ };
43
+ }
44
+ else if (res.bridge_token === 'STSW') {
45
+ return {
46
+ valid: SwapType.ROUTER_STSW,
47
+ router_from_pair: LiquidityPool.parsePoolsFromServerData2(res.from_pair, token_x, Token.getBaseTokens(this.stackswap, BaseToken.STSW), res.from_direction),
48
+ router_to_pair: LiquidityPool.parsePoolsFromServerData2(res.to_pair, Token.getBaseTokens(this.stackswap, BaseToken.STSW), token_y, res.to_direction),
49
+ router_from_mode: res.from_direction,
50
+ router_to_mode: res.to_direction
51
+ };
52
+ }
53
+ else {
54
+ return { valid: SwapType.NO_ROUTE };
55
+ }
56
+ }
57
+ else {
58
+ return { valid: SwapType.NO_ROUTE };
59
+ }
60
+ }
61
+ async routerSwapContractCall(token_from_amount, token_to_amount, token_bridge_amount, pair_from, pair_to, router_from_mode, router_to_mode, tolerance, callback = null) {
62
+ const token_from = router_from_mode ? pair_from.token_x : pair_from.token_y;
63
+ const token_bridge = router_from_mode ? pair_from.token_y : pair_from.token_x;
64
+ const token_to = router_to_mode ? pair_to.token_y : pair_to.token_x;
65
+ const from_amt = new BigNumber(10 ** token_from.decimal).multipliedBy(token_from_amount).integerValue();
66
+ const bridge_amt = new BigNumber(10 ** token_bridge.decimal).multipliedBy(token_bridge_amount).integerValue();
67
+ const to_amt = new BigNumber(10 ** token_to.decimal).multipliedBy(token_to_amount).integerValue();
68
+ const bridge_min = bridge_amt.multipliedBy((100 - tolerance)).dividedBy(100).integerValue();
69
+ const to_min = to_amt.multipliedBy((100 - tolerance)).dividedBy(100).multipliedBy((100 - tolerance)).dividedBy(100).integerValue();
70
+ const token_from_addr = (token_from.addr);
71
+ const token_bridge_addr = (token_bridge.addr);
72
+ const token_to_addr = (token_to.addr);
73
+ const pair_from_addr = pair_from.token_lp.addr;
74
+ const pair_to_addr = pair_to.token_lp.addr;
75
+ const from_mode = router_from_mode ? trueCV() : falseCV();
76
+ const to_mode = router_to_mode ? trueCV() : falseCV();
77
+ const post_condition = [];
78
+ post_condition.push(await getPostConditionFromAsset(this.stackswap, this.stackswap.getSenderAddress(), token_from.addr, from_amt.toString(), FungibleConditionCode.Equal));
79
+ post_condition.push(await getPostConditionFromAsset(this.stackswap, pair_from_addr, token_bridge.addr, bridge_min.toString(), FungibleConditionCode.GreaterEqual));
80
+ post_condition.push(await getPostConditionFromAsset(this.stackswap, this.stackswap.getSenderAddress(), token_bridge.addr, bridge_min.toString(), FungibleConditionCode.GreaterEqual));
81
+ post_condition.push(await getPostConditionFromAsset(this.stackswap, pair_to_addr, token_to.addr, to_min.toString(), FungibleConditionCode.GreaterEqual));
82
+ const options = getWriteOptions(this.stackswap, this.stackswap.config.CONTRACT_NAME_STACKSWAP_ROUTER_SWAP(), 'router-swap', [
83
+ parseAddressToCV(token_from_addr),
84
+ parseAddressToCV(token_bridge_addr),
85
+ parseAddressToCV(token_to_addr),
86
+ parseAddressToCV(pair_from_addr),
87
+ parseAddressToCV(pair_to_addr),
88
+ from_mode,
89
+ to_mode,
90
+ uintCV(from_amt.toString()),
91
+ uintCV(bridge_min.toString()),
92
+ uintCV(to_min.toString())
93
+ ], post_condition, callback);
94
+ openContractCall(options);
95
+ }
96
+ async swapContractCall(token_x_amount, token_y_amount, pair, x_to_y, tolerance, callback = null) {
97
+ const token_x = x_to_y ? pair.token_x : pair.token_y;
98
+ const token_y = x_to_y ? pair.token_y : pair.token_x;
99
+ const dx_bn = new BigNumber(10 ** token_x.decimal).multipliedBy(token_x_amount);
100
+ const dy_min_bn = new BigNumber(10 ** token_y.decimal).multipliedBy(token_y_amount).multipliedBy(100 - tolerance).dividedBy(100);
101
+ const dx = dx_bn.toFixed(0).toString();
102
+ const dy_min = dy_min_bn.toFixed(0).toString();
103
+ const functionName = x_to_y ? 'swap-x-for-y' : 'swap-y-for-x';
104
+ const token_x_addr = x_to_y ? (token_x.addr) : (token_y.addr);
105
+ const token_y_addr = x_to_y ? (token_y.addr) : (token_x.addr);
106
+ const post_condition = [];
107
+ post_condition.push(await getPostConditionFromAsset(this.stackswap, this.stackswap.getSenderAddress(), token_x.addr, dx, FungibleConditionCode.Equal));
108
+ post_condition.push(await getPostConditionFromAsset(this.stackswap, pair.token_lp.addr, token_y.addr, dy_min, FungibleConditionCode.GreaterEqual));
109
+ const options = getWriteOptions(this.stackswap, this.stackswap.config.CONTRACT_NAME_STACKSWAP_SWAP(), functionName, [
110
+ parseAddressToCV(token_x_addr), parseAddressToCV(token_y_addr),
111
+ parseAddressToCV(pair.token_lp.addr), uintCV(dx), uintCV(dy_min)
112
+ ], post_condition, callback);
113
+ openContractCall(options);
114
+ }
115
+ async importContractCall(pair, x_to_y, amount_x, amount_y, callback = null) {
116
+ const token_x = x_to_y ? pair.token_x : pair.token_y;
117
+ const token_y = x_to_y ? pair.token_y : pair.token_x;
118
+ const dx_temp = new BigNumber(10 ** token_x.decimal).multipliedBy(amount_x);
119
+ const dy_temp = new BigNumber(10 ** token_y.decimal).multipliedBy(amount_y);
120
+ const dx = x_to_y ? dx_temp : dy_temp;
121
+ const dy = x_to_y ? dy_temp : dx_temp;
122
+ const dx_out = dx_temp.multipliedBy(1.2).toFixed(0).toString();
123
+ const dy_out = dy_temp.multipliedBy(1.2).toFixed(0).toString();
124
+ const token_x_addr = x_to_y ? (token_x.addr) : (token_y.addr);
125
+ const token_y_addr = x_to_y ? (token_y.addr) : (token_x.addr);
126
+ const post_condition = [];
127
+ post_condition.push(await getPostConditionFromAsset(this.stackswap, this.stackswap.getSenderAddress(), token_x.addr, dx_out, FungibleConditionCode.LessEqual));
128
+ post_condition.push(await getPostConditionFromAsset(this.stackswap, this.stackswap.getSenderAddress(), token_y.addr, dy_out, FungibleConditionCode.LessEqual));
129
+ const options = getWriteOptions(this.stackswap, this.stackswap.config.CONTRACT_NAME_STACKSWAP_SWAP(), 'add-to-position', [
130
+ parseAddressToCV(token_x_addr), parseAddressToCV(token_y_addr),
131
+ parseAddressToCV(pair.token_lp.addr), uintCV(dx.toFixed(0).toString()), uintCV(dy.toFixed(0).toString())
132
+ ], post_condition, callback);
133
+ openContractCall(options);
134
+ }
135
+ async removeContractCall(pair, percent, user_lp_total, dx, dy, swap_contract = this.stackswap.config.CONTRACT_NAME_STACKSWAP_SWAP(), callback = null) {
136
+ const token_x_addr = pair.token_x.addr;
137
+ const token_y_addr = pair.token_y.addr;
138
+ const pair_addr = pair.token_lp.addr;
139
+ const post_condition = [];
140
+ const burn_amount = new BigNumber(user_lp_total).multipliedBy(percent * 1.01).div(100).toFixed(0).toString();
141
+ post_condition.push(await getPostConditionFromAsset(this.stackswap, pair.token_lp.addr, pair.token_x.addr, dx, FungibleConditionCode.GreaterEqual));
142
+ post_condition.push(await getPostConditionFromAsset(this.stackswap, pair.token_lp.addr, pair.token_y.addr, dy, FungibleConditionCode.GreaterEqual));
143
+ post_condition.push(await getPostConditionFromAsset(this.stackswap, this.stackswap.getSenderAddress(), pair.token_lp.addr, burn_amount, FungibleConditionCode.LessEqual));
144
+ const options = getWriteOptions(this.stackswap, swap_contract, 'reduce-position', [
145
+ parseAddressToCV(token_x_addr), parseAddressToCV(token_y_addr),
146
+ parseAddressToCV(pair_addr), uintCV(percent),
147
+ ], post_condition, callback);
148
+ openContractCall(options);
149
+ }
150
+ async createContractCall(token_x, token_y, token_x_amount, token_y_amount, liquidity_token_addr, callback = null) {
151
+ const token_x_addr = token_x.addr;
152
+ const token_y_addr = token_y.addr;
153
+ const pair_addr = liquidity_token_addr;
154
+ const post_condition = [];
155
+ const amount_X = decimal2integer(token_x_amount, token_x.decimal);
156
+ const amount_Y = decimal2integer(token_y_amount, token_y.decimal);
157
+ post_condition.push(await getPostConditionFromAsset(this.stackswap, this.stackswap.getSenderAddress(), token_x.addr, amount_X, FungibleConditionCode.Equal));
158
+ post_condition.push(await getPostConditionFromAsset(this.stackswap, this.stackswap.getSenderAddress(), token_y.addr, amount_Y, FungibleConditionCode.Equal));
159
+ const options = getWriteOptions(this.stackswap, this.stackswap.config.CONTRACT_NAME_STACKSWAP_ONE_STEP_MINT(), 'create-pair-new-liquidity-token', [
160
+ parseAddressToCV(token_x_addr), parseAddressToCV(token_y_addr),
161
+ parseAddressToCV(pair_addr), stringAsciiCV(token_x.symbol + '-' + token_y.symbol),
162
+ uintCV(amount_X), uintCV(amount_Y), parseAddressToCV(pair_addr),
163
+ parseAddressToCV(this.stackswap.getQualifiedAddress(this.stackswap.config.CONTRACT_NAME_STACKSWAP_SWAP())),
164
+ ], post_condition, callback);
165
+ openContractCall(options);
166
+ }
167
+ price_impact(x_to_y, pair, token_x_amount) {
168
+ try {
169
+ const balance_x = x_to_y ? pair.balance_x : pair.balance_y;
170
+ const token_x_decimal = x_to_y ? pair.token_x.decimal : pair.token_y.decimal;
171
+ const balance_y = x_to_y ? pair.balance_y : pair.balance_y;
172
+ const dx = new BigNumber(token_x_amount).multipliedBy(10 ** token_x_decimal);
173
+ if (dx.toNumber() > new BigNumber(balance_x).toNumber()) {
174
+ return 100;
175
+ }
176
+ const dy_1 = new BigNumber(balance_y).multipliedBy(dx);
177
+ const dy_2 = new BigNumber(balance_x).plus(dx);
178
+ const dy = (dy_1.toNumber()) / (dy_2.toNumber());
179
+ const input_price = new BigNumber(10 ** 10).multipliedBy(new BigNumber(balance_x)).dividedBy(new BigNumber(balance_y));
180
+ const output_price = new BigNumber(10 ** 10).multipliedBy(new BigNumber(balance_x)).plus(dx).dividedBy(new BigNumber(balance_y).minus(dy));
181
+ const price_impact2 = output_price.minus(input_price).multipliedBy(10000).dividedBy(input_price);
182
+ let price_impact = price_impact2.toNumber();
183
+ if (price_impact >= 10000) {
184
+ price_impact = 9999;
185
+ }
186
+ return (price_impact / 100).toFixed(5);
187
+ }
188
+ catch (e) {
189
+ return 0;
190
+ }
191
+ }
192
+ price_impact_router(router_from_mode, router_to_mode, pair_from, pair_to, token_from_amount) {
193
+ try {
194
+ const balance_x = router_from_mode ? pair_from.balance_x : pair_from.balance_y;
195
+ const token_from_decimal = router_from_mode ? pair_from.token_x.decimal : pair_from.token_y.decimal;
196
+ const balance_y = router_to_mode ? pair_to.balance_y : pair_to.balance_x;
197
+ const dx = new BigNumber(token_from_amount).multipliedBy(10 ** token_from_decimal);
198
+ if (dx.toNumber() > new BigNumber(balance_x).toNumber()) {
199
+ return 100;
200
+ }
201
+ const dy_1 = new BigNumber(balance_y).multipliedBy(dx);
202
+ const dy_2 = new BigNumber(balance_x).plus(dx);
203
+ const dy = (dy_1.toNumber()) / (dy_2.toNumber());
204
+ const input_price = new BigNumber(10 ** 10).multipliedBy(new BigNumber(balance_x)).dividedBy(new BigNumber(balance_y));
205
+ const output_price = new BigNumber(10 ** 10).multipliedBy(new BigNumber(balance_x)).plus(dx).dividedBy(new BigNumber(balance_y).minus(dy));
206
+ const price_impact2 = output_price.minus(input_price).multipliedBy(10000).dividedBy(input_price);
207
+ let price_impact = price_impact2.toNumber();
208
+ if (price_impact >= 10000) {
209
+ price_impact = 9999;
210
+ }
211
+ return (price_impact / 100).toFixed(5);
212
+ }
213
+ catch (e) {
214
+ return 0;
215
+ }
216
+ }
217
+ getPriceString(pair, token_x, token_y, price, price_invert) {
218
+ try {
219
+ if (pair != null && price < 0) {
220
+ return 'dx exceed pool balance';
221
+ }
222
+ else if (token_x != null && token_y != null) {
223
+ if (price_invert) {
224
+ return ` 1 ${token_y.symbol} = ${new BigNumber((1 / price)).toFixed(token_x.decimal)} ${token_x.symbol}`;
225
+ }
226
+ else {
227
+ return ` 1 ${token_x.symbol} = ${new BigNumber(price).toFixed(token_x.decimal)} ${token_y.symbol}`;
228
+ }
229
+ }
230
+ return '';
231
+ }
232
+ catch (e) {
233
+ return '';
234
+ }
235
+ }
236
+ getPricesWithSwapData(swap_data, token_x_amount, price_invert) {
237
+ let price = -1;
238
+ switch (swap_data.valid) {
239
+ case SwapType.SINGLE:
240
+ try {
241
+ const temp = swap_data.pair.getPoolPrice(!swap_data.x_to_y, Number(token_x_amount) <= 0 || token_x_amount == '' ? '-1' : token_x_amount);
242
+ if (temp.output_price !== '0') {
243
+ price = (1 / parseFloat(temp.output_price));
244
+ }
245
+ const price_strings = this.getPriceString(swap_data.pair, swap_data.pair.getTokenX(swap_data.x_to_y), swap_data.pair.getTokenY(swap_data.x_to_y), price, price_invert);
246
+ const token_y_amount = new BigNumber(temp.dy).toFixed(Number(swap_data.pair.getTokenY(swap_data.x_to_y).decimal)).toString();
247
+ return { price: price, dy: token_y_amount, price_strings: price_strings };
248
+ }
249
+ catch (e) {
250
+ return { price: price, dy: '0', price_strings: '' };
251
+ }
252
+ case SwapType.ROUTER_STX:
253
+ const bridge_token = Token.getBaseTokens(this.stackswap, BaseToken.STX);
254
+ const from_res = swap_data.router_from_pair.getPoolPrice(!swap_data.router_from_mode, Number(token_x_amount) <= 0 || token_x_amount == '' ? '1' : token_x_amount);
255
+ let router_from_price = -1;
256
+ if (from_res.output_price !== '0') {
257
+ router_from_price = (1 / parseFloat(from_res.output_price));
258
+ }
259
+ const router_bridge_amt = new BigNumber(new BigNumber(from_res.dy).toFixed(Number(bridge_token.decimal))).toString();
260
+ const to_res = swap_data.router_to_pair.getPoolPrice(!swap_data.router_to_mode, Number(router_bridge_amt) <= 0 ? '-1' : router_bridge_amt);
261
+ let router_to_price = -1;
262
+ if (to_res.output_price !== '0') {
263
+ router_to_price = (1 / parseFloat(to_res.output_price));
264
+ }
265
+ const token_y_amount2 = new BigNumber(to_res.dy).toFixed(Number(Number(token_x_amount) <= 0 || token_x_amount == '' ? 0 : swap_data.router_to_pair.getTokenY(swap_data.router_to_mode).decimal)).toString();
266
+ price = router_from_price * router_to_price;
267
+ const price_strings2 = this.getPriceString(swap_data.pair, swap_data.router_from_pair.getTokenX(swap_data.router_from_mode), swap_data.router_to_pair.getTokenY(swap_data.router_to_mode), price, price_invert);
268
+ return { price: price, dy: token_y_amount2, price_strings: price_strings2, bridge_amount: router_bridge_amt };
269
+ case SwapType.ROUTER_STSW:
270
+ const bridge_token2 = Token.getBaseTokens(this.stackswap, BaseToken.STX);
271
+ const from_res2 = swap_data.router_from_pair.getPoolPrice(!swap_data.router_from_mode, Number(token_x_amount) <= 0 || token_x_amount == '' ? '1' : token_x_amount);
272
+ let router_from_price2 = -1;
273
+ if (from_res2.output_price !== '0') {
274
+ router_from_price2 = (1 / parseFloat(from_res2.output_price));
275
+ }
276
+ const router_bridge_amt2 = new BigNumber(new BigNumber(from_res2.dy).toFixed(Number(bridge_token2.decimal))).toString();
277
+ const to_res2 = swap_data.router_to_pair.getPoolPrice(!swap_data.router_to_mode, Number(router_bridge_amt2) <= 0 ? '-1' : router_bridge_amt2);
278
+ let router_to_price2 = -1;
279
+ if (to_res2.output_price !== '0') {
280
+ router_to_price2 = (1 / parseFloat(to_res2.output_price));
281
+ }
282
+ const token_y_amount3 = new BigNumber(to_res2.dy).toFixed(Number(Number(token_x_amount) <= 0 || token_x_amount == '' ? 0 : swap_data.router_to_pair.getTokenY(swap_data.router_to_mode).decimal)).toString();
283
+ price = router_from_price2 * router_to_price2;
284
+ const price_strings3 = this.getPriceString(swap_data.pair, swap_data.router_from_pair.getTokenX(swap_data.router_from_mode), swap_data.router_to_pair.getTokenY(swap_data.router_to_mode), price, price_invert);
285
+ return { price: price, dy: token_y_amount3, price_strings: price_strings3, bridge_amount: router_bridge_amt2 };
286
+ default:
287
+ return { price: price, dy: '0', price_strings: '' };
288
+ }
289
+ }
290
+ }
291
291
  //# sourceMappingURL=swap.manager.js.map