stackswap-front-api-test-02 1.0.83 → 1.0.86

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