stackswap-front-api-test-02 1.0.89 → 1.0.90

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