stackswap-front-api-test-02 1.0.82 → 1.0.85
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -10
- package/dist/esm/index.d.ts +84 -84
- package/dist/esm/index.js +143 -143
- package/dist/esm/stackswap/config.d.ts +112 -112
- package/dist/esm/stackswap/config.js +185 -185
- package/dist/esm/stackswap/manager/farm.manager.d.ts +41 -41
- package/dist/esm/stackswap/manager/farm.manager.js +293 -293
- package/dist/esm/stackswap/manager/farm2.manager.d.ts +60 -60
- package/dist/esm/stackswap/manager/farm2.manager.js +403 -403
- package/dist/esm/stackswap/manager/farm2.manager.js.map +1 -1
- package/dist/esm/stackswap/manager/governance.manager.d.ts +9 -9
- package/dist/esm/stackswap/manager/governance.manager.js +53 -53
- package/dist/esm/stackswap/manager/launchpad.manager.d.ts +13 -13
- package/dist/esm/stackswap/manager/launchpad.manager.js +147 -147
- package/dist/esm/stackswap/manager/lbtc.manager.d.ts +31 -31
- package/dist/esm/stackswap/manager/lbtc.manager.js +326 -326
- package/dist/esm/stackswap/manager/multisig.manager.d.ts +11 -11
- package/dist/esm/stackswap/manager/multisig.manager.js +51 -51
- package/dist/esm/stackswap/manager/nft.manager.d.ts +52 -52
- package/dist/esm/stackswap/manager/nft.manager.js +243 -243
- package/dist/esm/stackswap/manager/operator.manager.d.ts +17 -17
- package/dist/esm/stackswap/manager/operator.manager.js +85 -85
- package/dist/esm/stackswap/manager/other.manager.d.ts +9 -9
- package/dist/esm/stackswap/manager/other.manager.js +47 -47
- package/dist/esm/stackswap/manager/pool.manager.d.ts +34 -34
- package/dist/esm/stackswap/manager/pool.manager.js +171 -175
- package/dist/esm/stackswap/manager/pool.manager.js.map +1 -1
- package/dist/esm/stackswap/manager/poxl.manager.d.ts +16 -16
- package/dist/esm/stackswap/manager/poxl.manager.js +71 -71
- package/dist/esm/stackswap/manager/staking.manager.d.ts +25 -25
- package/dist/esm/stackswap/manager/staking.manager.js +233 -233
- package/dist/esm/stackswap/manager/swap.manager.d.ts +35 -35
- package/dist/esm/stackswap/manager/swap.manager.js +290 -290
- package/dist/esm/stackswap/manager/token.manager.d.ts +31 -31
- package/dist/esm/stackswap/manager/token.manager.js +133 -135
- package/dist/esm/stackswap/manager/token.manager.js.map +1 -1
- package/dist/esm/stackswap/util.d.ts +35 -35
- package/dist/esm/stackswap/util.js +201 -201
- package/dist/index.d.ts +84 -84
- package/dist/index.js +152 -152
- package/dist/index.umd.js +1 -1
- package/dist/stackswap/config.d.ts +112 -112
- package/dist/stackswap/config.js +190 -190
- package/dist/stackswap/manager/farm.manager.d.ts +41 -41
- package/dist/stackswap/manager/farm.manager.js +301 -301
- package/dist/stackswap/manager/farm2.manager.d.ts +60 -60
- package/dist/stackswap/manager/farm2.manager.js +410 -410
- package/dist/stackswap/manager/farm2.manager.js.map +1 -1
- package/dist/stackswap/manager/governance.manager.d.ts +9 -9
- package/dist/stackswap/manager/governance.manager.js +60 -60
- package/dist/stackswap/manager/launchpad.manager.d.ts +13 -13
- package/dist/stackswap/manager/launchpad.manager.js +151 -151
- package/dist/stackswap/manager/lbtc.manager.d.ts +31 -31
- package/dist/stackswap/manager/lbtc.manager.js +333 -333
- package/dist/stackswap/manager/multisig.manager.d.ts +11 -11
- package/dist/stackswap/manager/multisig.manager.js +55 -55
- package/dist/stackswap/manager/nft.manager.d.ts +52 -52
- package/dist/stackswap/manager/nft.manager.js +250 -250
- package/dist/stackswap/manager/operator.manager.d.ts +17 -17
- package/dist/stackswap/manager/operator.manager.js +92 -92
- package/dist/stackswap/manager/other.manager.d.ts +9 -9
- package/dist/stackswap/manager/other.manager.js +54 -54
- package/dist/stackswap/manager/pool.manager.d.ts +34 -34
- package/dist/stackswap/manager/pool.manager.js +179 -183
- package/dist/stackswap/manager/pool.manager.js.map +1 -1
- package/dist/stackswap/manager/poxl.manager.d.ts +16 -16
- package/dist/stackswap/manager/poxl.manager.js +78 -78
- package/dist/stackswap/manager/staking.manager.d.ts +25 -25
- package/dist/stackswap/manager/staking.manager.js +240 -240
- package/dist/stackswap/manager/swap.manager.d.ts +35 -35
- package/dist/stackswap/manager/swap.manager.js +297 -297
- package/dist/stackswap/manager/token.manager.d.ts +31 -31
- package/dist/stackswap/manager/token.manager.js +141 -143
- package/dist/stackswap/manager/token.manager.js.map +1 -1
- package/dist/stackswap/util.d.ts +35 -35
- package/dist/stackswap/util.js +218 -218
- package/package.json +43 -43
- package/src/index.ts +204 -204
- package/src/stackswap/config.ts +292 -292
- package/src/stackswap/manager/farm.manager.ts +399 -399
- package/src/stackswap/manager/farm2.manager.ts +463 -463
- package/src/stackswap/manager/governance.manager.ts +95 -95
- package/src/stackswap/manager/launchpad.manager.ts +190 -190
- package/src/stackswap/manager/multisig.manager.ts +87 -87
- package/src/stackswap/manager/nft.manager.ts +311 -311
- package/src/stackswap/manager/operator.manager.ts +123 -123
- package/src/stackswap/manager/other.manager.ts +71 -71
- package/src/stackswap/manager/pool.manager.ts +202 -202
- package/src/stackswap/manager/poxl.manager.ts +99 -99
- package/src/stackswap/manager/staking.manager.ts +321 -321
- package/src/stackswap/manager/swap.manager.ts +351 -351
- package/src/stackswap/manager/token.manager.ts +167 -167
- package/src/stackswap/util.ts +237 -237
|
@@ -1,298 +1,298 @@
|
|
|
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/
|
|
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 findRouter(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 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;
|
|
298
298
|
//# sourceMappingURL=swap.manager.js.map
|