stackswap-front-api-test-02 0.10.2 → 0.10.4
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/dist/esm/index.d.ts +106 -0
- package/dist/esm/index.js +196 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/stackswap/config.d.ts +172 -0
- package/dist/esm/stackswap/config.js +302 -0
- package/dist/esm/stackswap/config.js.map +1 -0
- package/dist/esm/stackswap/manager/bridge.manager.d.ts +27 -0
- package/dist/esm/stackswap/manager/bridge.manager.js +215 -0
- package/dist/esm/stackswap/manager/bridge.manager.js.map +1 -0
- package/dist/esm/stackswap/manager/farm.manager.d.ts +41 -0
- package/dist/esm/stackswap/manager/farm.manager.js +293 -0
- package/dist/esm/stackswap/manager/farm.manager.js.map +1 -0
- package/dist/esm/stackswap/manager/farm2.manager.d.ts +106 -0
- package/dist/esm/stackswap/manager/farm2.manager.js +625 -0
- package/dist/esm/stackswap/manager/farm2.manager.js.map +1 -0
- package/dist/esm/stackswap/manager/farm3.manager.d.ts +121 -0
- package/dist/esm/stackswap/manager/farm3.manager.js +557 -0
- package/dist/esm/stackswap/manager/farm3.manager.js.map +1 -0
- package/dist/esm/stackswap/manager/governance.manager.d.ts +9 -0
- package/dist/esm/stackswap/manager/governance.manager.js +68 -0
- package/dist/esm/stackswap/manager/governance.manager.js.map +1 -0
- package/dist/esm/stackswap/manager/groupfarm.manager.d.ts +70 -0
- package/dist/esm/stackswap/manager/groupfarm.manager.js +379 -0
- package/dist/esm/stackswap/manager/groupfarm.manager.js.map +1 -0
- package/dist/esm/stackswap/manager/launchpad.manager.d.ts +18 -0
- package/dist/esm/stackswap/manager/launchpad.manager.js +253 -0
- package/dist/esm/stackswap/manager/launchpad.manager.js.map +1 -0
- package/dist/esm/stackswap/manager/lbtc.manager.d.ts +31 -0
- package/dist/esm/stackswap/manager/lbtc.manager.js +327 -0
- package/dist/esm/stackswap/manager/lbtc.manager.js.map +1 -0
- package/dist/esm/stackswap/manager/lbtcstaking.manager.d.ts +61 -0
- package/dist/esm/stackswap/manager/lbtcstaking.manager.js +248 -0
- package/dist/esm/stackswap/manager/lbtcstaking.manager.js.map +1 -0
- package/dist/esm/stackswap/manager/multisig.manager.d.ts +11 -0
- package/dist/esm/stackswap/manager/multisig.manager.js +50 -0
- package/dist/esm/stackswap/manager/multisig.manager.js.map +1 -0
- package/dist/esm/stackswap/manager/nft.manager.d.ts +55 -0
- package/dist/esm/stackswap/manager/nft.manager.js +274 -0
- package/dist/esm/stackswap/manager/nft.manager.js.map +1 -0
- package/dist/esm/stackswap/manager/operator.manager.d.ts +17 -0
- package/dist/esm/stackswap/manager/operator.manager.js +86 -0
- package/dist/esm/stackswap/manager/operator.manager.js.map +1 -0
- package/dist/esm/stackswap/manager/other.manager.d.ts +9 -0
- package/dist/esm/stackswap/manager/other.manager.js +41 -0
- package/dist/esm/stackswap/manager/other.manager.js.map +1 -0
- package/dist/esm/stackswap/manager/pool.manager.d.ts +34 -0
- package/dist/esm/stackswap/manager/pool.manager.js +172 -0
- package/dist/esm/stackswap/manager/pool.manager.js.map +1 -0
- package/dist/esm/stackswap/manager/poxl.manager.d.ts +16 -0
- package/dist/esm/stackswap/manager/poxl.manager.js +72 -0
- package/dist/esm/stackswap/manager/poxl.manager.js.map +1 -0
- package/dist/esm/stackswap/manager/staking.manager.d.ts +40 -0
- package/dist/esm/stackswap/manager/staking.manager.js +295 -0
- package/dist/esm/stackswap/manager/staking.manager.js.map +1 -0
- package/dist/esm/stackswap/manager/swap.manager.d.ts +71 -0
- package/dist/esm/stackswap/manager/swap.manager.js +730 -0
- package/dist/esm/stackswap/manager/swap.manager.js.map +1 -0
- package/dist/esm/stackswap/manager/token.manager.d.ts +32 -0
- package/dist/esm/stackswap/manager/token.manager.js +142 -0
- package/dist/esm/stackswap/manager/token.manager.js.map +1 -0
- package/dist/esm/stackswap/util.d.ts +35 -0
- package/dist/esm/stackswap/util.js +202 -0
- package/dist/esm/stackswap/util.js.map +1 -0
- package/dist/index.d.ts +106 -0
- package/dist/index.js +206 -0
- package/dist/index.js.map +1 -0
- package/dist/stackswap/config.d.ts +172 -0
- package/dist/stackswap/config.js +308 -0
- package/dist/stackswap/config.js.map +1 -0
- package/dist/stackswap/manager/bridge.manager.d.ts +27 -0
- package/dist/stackswap/manager/bridge.manager.js +222 -0
- package/dist/stackswap/manager/bridge.manager.js.map +1 -0
- package/dist/stackswap/manager/farm.manager.d.ts +41 -0
- package/dist/stackswap/manager/farm.manager.js +301 -0
- package/dist/stackswap/manager/farm.manager.js.map +1 -0
- package/dist/stackswap/manager/farm2.manager.d.ts +106 -0
- package/dist/stackswap/manager/farm2.manager.js +632 -0
- package/dist/stackswap/manager/farm2.manager.js.map +1 -0
- package/dist/stackswap/manager/farm3.manager.d.ts +121 -0
- package/dist/stackswap/manager/farm3.manager.js +564 -0
- package/dist/stackswap/manager/farm3.manager.js.map +1 -0
- package/dist/stackswap/manager/governance.manager.d.ts +9 -0
- package/dist/stackswap/manager/governance.manager.js +75 -0
- package/dist/stackswap/manager/governance.manager.js.map +1 -0
- package/dist/stackswap/manager/groupfarm.manager.d.ts +70 -0
- package/dist/stackswap/manager/groupfarm.manager.js +386 -0
- package/dist/stackswap/manager/groupfarm.manager.js.map +1 -0
- package/dist/stackswap/manager/launchpad.manager.d.ts +18 -0
- package/dist/stackswap/manager/launchpad.manager.js +257 -0
- package/dist/stackswap/manager/launchpad.manager.js.map +1 -0
- package/dist/stackswap/manager/lbtc.manager.d.ts +31 -0
- package/dist/stackswap/manager/lbtc.manager.js +334 -0
- package/dist/stackswap/manager/lbtc.manager.js.map +1 -0
- package/dist/stackswap/manager/lbtcstaking.manager.d.ts +61 -0
- package/dist/stackswap/manager/lbtcstaking.manager.js +255 -0
- package/dist/stackswap/manager/lbtcstaking.manager.js.map +1 -0
- package/dist/stackswap/manager/multisig.manager.d.ts +11 -0
- package/dist/stackswap/manager/multisig.manager.js +54 -0
- package/dist/stackswap/manager/multisig.manager.js.map +1 -0
- package/dist/stackswap/manager/nft.manager.d.ts +55 -0
- package/dist/stackswap/manager/nft.manager.js +281 -0
- package/dist/stackswap/manager/nft.manager.js.map +1 -0
- package/dist/stackswap/manager/operator.manager.d.ts +17 -0
- package/dist/stackswap/manager/operator.manager.js +93 -0
- package/dist/stackswap/manager/operator.manager.js.map +1 -0
- package/dist/stackswap/manager/other.manager.d.ts +9 -0
- package/dist/stackswap/manager/other.manager.js +45 -0
- package/dist/stackswap/manager/other.manager.js.map +1 -0
- package/dist/stackswap/manager/pool.manager.d.ts +34 -0
- package/dist/stackswap/manager/pool.manager.js +180 -0
- package/dist/stackswap/manager/pool.manager.js.map +1 -0
- package/dist/stackswap/manager/poxl.manager.d.ts +16 -0
- package/dist/stackswap/manager/poxl.manager.js +79 -0
- package/dist/stackswap/manager/poxl.manager.js.map +1 -0
- package/dist/stackswap/manager/staking.manager.d.ts +40 -0
- package/dist/stackswap/manager/staking.manager.js +302 -0
- package/dist/stackswap/manager/staking.manager.js.map +1 -0
- package/dist/stackswap/manager/swap.manager.d.ts +71 -0
- package/dist/stackswap/manager/swap.manager.js +737 -0
- package/dist/stackswap/manager/swap.manager.js.map +1 -0
- package/dist/stackswap/manager/token.manager.d.ts +32 -0
- package/dist/stackswap/manager/token.manager.js +150 -0
- package/dist/stackswap/manager/token.manager.js.map +1 -0
- package/dist/stackswap/util.d.ts +35 -0
- package/dist/stackswap/util.js +219 -0
- package/dist/stackswap/util.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,737 @@
|
|
|
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[SwapType["ROUTER_STX_STSW"] = 20] = "ROUTER_STX_STSW";
|
|
22
|
+
SwapType[SwapType["ROUTER_STSW_STX"] = 21] = "ROUTER_STSW_STX";
|
|
23
|
+
})(SwapType = exports.SwapType || (exports.SwapType = {}));
|
|
24
|
+
class SwapManager {
|
|
25
|
+
constructor(stackswap) {
|
|
26
|
+
this.stackswap = stackswap;
|
|
27
|
+
}
|
|
28
|
+
async findRouterAll(token_x, token_y, current_block) {
|
|
29
|
+
const url = this.stackswap.config.STACKS_BACKEND_URL() + '/api/v1/swap_all/' + token_x.addr + '/' + token_y.addr + '/' + current_block;
|
|
30
|
+
const result = await axios_1.default.get(url, { timeout: 50000, });
|
|
31
|
+
const res = result.data;
|
|
32
|
+
if (Object.keys(res).length === 0) {
|
|
33
|
+
return { valid: SwapType.NO_ROUTE };
|
|
34
|
+
}
|
|
35
|
+
else if (res.type === 'SWAP') {
|
|
36
|
+
return {
|
|
37
|
+
valid: SwapType.SINGLE,
|
|
38
|
+
x_to_y: res.direction,
|
|
39
|
+
pair: pool_manager_1.LiquidityPool.parsePoolsFromServerData2(res.pair, token_x, token_y, res.direction)
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
else if (res.type === 'RouterSWAP') {
|
|
43
|
+
if (res.bridge_token == token_manager_1.Token.getBaseTokens(this.stackswap, token_manager_1.BaseToken.STX).addr) {
|
|
44
|
+
return {
|
|
45
|
+
valid: SwapType.ROUTER_STX,
|
|
46
|
+
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),
|
|
47
|
+
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),
|
|
48
|
+
router_from_mode: res.from_direction,
|
|
49
|
+
router_to_mode: res.to_direction
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
else if (res.bridge_token == token_manager_1.Token.getBaseTokens(this.stackswap, token_manager_1.BaseToken.STSW).addr) {
|
|
53
|
+
return {
|
|
54
|
+
valid: SwapType.ROUTER_STSW,
|
|
55
|
+
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),
|
|
56
|
+
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),
|
|
57
|
+
router_from_mode: res.from_direction,
|
|
58
|
+
router_to_mode: res.to_direction
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
return { valid: SwapType.NO_ROUTE };
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
else if (res.type === 'RouterSWAP2') {
|
|
66
|
+
if (res.bridge_token[0] == token_manager_1.Token.getBaseTokens(this.stackswap, token_manager_1.BaseToken.STSW).addr && res.bridge_token[1] == token_manager_1.Token.getBaseTokens(this.stackswap, token_manager_1.BaseToken.STX).addr) {
|
|
67
|
+
return {
|
|
68
|
+
valid: SwapType.ROUTER_STSW_STX,
|
|
69
|
+
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),
|
|
70
|
+
router_bridge_pair: pool_manager_1.LiquidityPool.parsePoolsFromServerData2(res.bridge_pair, token_manager_1.Token.getBaseTokens(this.stackswap, token_manager_1.BaseToken.STSW), token_manager_1.Token.getBaseTokens(this.stackswap, token_manager_1.BaseToken.STX), res.bridge_direction),
|
|
71
|
+
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),
|
|
72
|
+
router_from_mode: res.from_direction,
|
|
73
|
+
router_bridge_mode: res.bridge_direction,
|
|
74
|
+
router_to_mode: res.to_direction
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
else if (res.bridge_token[1] == token_manager_1.Token.getBaseTokens(this.stackswap, token_manager_1.BaseToken.STSW).addr && res.bridge_token[0] == token_manager_1.Token.getBaseTokens(this.stackswap, token_manager_1.BaseToken.STX).addr) {
|
|
78
|
+
return {
|
|
79
|
+
valid: SwapType.ROUTER_STX_STSW,
|
|
80
|
+
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),
|
|
81
|
+
router_bridge_pair: pool_manager_1.LiquidityPool.parsePoolsFromServerData2(res.bridge_pair, token_manager_1.Token.getBaseTokens(this.stackswap, token_manager_1.BaseToken.STX), token_manager_1.Token.getBaseTokens(this.stackswap, token_manager_1.BaseToken.STSW), res.bridge_direction),
|
|
82
|
+
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),
|
|
83
|
+
router_from_mode: res.from_direction,
|
|
84
|
+
router_bridge_mode: res.bridge_direction,
|
|
85
|
+
router_to_mode: res.to_direction
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
return { valid: SwapType.NO_ROUTE };
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
return { valid: SwapType.NO_ROUTE };
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
async findRouter4(token_x, token_y, current_block) {
|
|
97
|
+
const url = this.stackswap.config.STACKS_BACKEND_URL() + '/api/v1/swap_v4/' + token_x.addr + '/' + token_y.addr + '/' + current_block;
|
|
98
|
+
const result = await axios_1.default.get(url, { timeout: 50000, });
|
|
99
|
+
const res = result.data;
|
|
100
|
+
if (Object.keys(res).length === 0) {
|
|
101
|
+
return { valid: SwapType.NO_ROUTE };
|
|
102
|
+
}
|
|
103
|
+
else if (res.type === 'SWAP') {
|
|
104
|
+
return {
|
|
105
|
+
valid: SwapType.SINGLE,
|
|
106
|
+
x_to_y: res.direction,
|
|
107
|
+
pair: pool_manager_1.LiquidityPool.parsePoolsFromServerData2(res.pair, token_x, token_y, res.direction)
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
else if (res.type === 'RouterSWAP') {
|
|
111
|
+
if (res.bridge_token == token_manager_1.Token.getBaseTokens(this.stackswap, token_manager_1.BaseToken.STX).addr) {
|
|
112
|
+
return {
|
|
113
|
+
valid: SwapType.ROUTER_STX,
|
|
114
|
+
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),
|
|
115
|
+
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),
|
|
116
|
+
router_from_mode: res.from_direction,
|
|
117
|
+
router_to_mode: res.to_direction
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
else if (res.bridge_token == token_manager_1.Token.getBaseTokens(this.stackswap, token_manager_1.BaseToken.STSW).addr) {
|
|
121
|
+
return {
|
|
122
|
+
valid: SwapType.ROUTER_STSW,
|
|
123
|
+
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),
|
|
124
|
+
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),
|
|
125
|
+
router_from_mode: res.from_direction,
|
|
126
|
+
router_to_mode: res.to_direction
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
return { valid: SwapType.NO_ROUTE };
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
else if (res.type === 'RouterSWAP2') {
|
|
134
|
+
if (res.bridge_token[0] == token_manager_1.Token.getBaseTokens(this.stackswap, token_manager_1.BaseToken.STSW).addr && res.bridge_token[1] == token_manager_1.Token.getBaseTokens(this.stackswap, token_manager_1.BaseToken.STX).addr) {
|
|
135
|
+
return {
|
|
136
|
+
valid: SwapType.ROUTER_STSW_STX,
|
|
137
|
+
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),
|
|
138
|
+
router_bridge_pair: pool_manager_1.LiquidityPool.parsePoolsFromServerData2(res.bridge_pair, token_manager_1.Token.getBaseTokens(this.stackswap, token_manager_1.BaseToken.STSW), token_manager_1.Token.getBaseTokens(this.stackswap, token_manager_1.BaseToken.STX), res.bridge_direction),
|
|
139
|
+
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),
|
|
140
|
+
router_from_mode: res.from_direction,
|
|
141
|
+
router_bridge_mode: res.bridge_direction,
|
|
142
|
+
router_to_mode: res.to_direction
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
else if (res.bridge_token[1] == token_manager_1.Token.getBaseTokens(this.stackswap, token_manager_1.BaseToken.STSW).addr && res.bridge_token[0] == token_manager_1.Token.getBaseTokens(this.stackswap, token_manager_1.BaseToken.STX).addr) {
|
|
146
|
+
return {
|
|
147
|
+
valid: SwapType.ROUTER_STX_STSW,
|
|
148
|
+
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),
|
|
149
|
+
router_bridge_pair: pool_manager_1.LiquidityPool.parsePoolsFromServerData2(res.bridge_pair, token_manager_1.Token.getBaseTokens(this.stackswap, token_manager_1.BaseToken.STX), token_manager_1.Token.getBaseTokens(this.stackswap, token_manager_1.BaseToken.STSW), res.bridge_direction),
|
|
150
|
+
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),
|
|
151
|
+
router_from_mode: res.from_direction,
|
|
152
|
+
router_bridge_mode: res.bridge_direction,
|
|
153
|
+
router_to_mode: res.to_direction
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
return { valid: SwapType.NO_ROUTE };
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
return { valid: SwapType.NO_ROUTE };
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
async findRouter3(token_x, token_y, current_block) {
|
|
165
|
+
const url = this.stackswap.config.STACKS_BACKEND_URL() + '/api/v1/swap_v3/' + token_x.addr + '/' + token_y.addr + '/' + current_block;
|
|
166
|
+
const result = await axios_1.default.get(url, { timeout: 50000, });
|
|
167
|
+
const res = result.data;
|
|
168
|
+
if (Object.keys(res).length === 0) {
|
|
169
|
+
return { valid: SwapType.NO_ROUTE };
|
|
170
|
+
}
|
|
171
|
+
else if (res.type === 'SWAP') {
|
|
172
|
+
return {
|
|
173
|
+
valid: SwapType.SINGLE,
|
|
174
|
+
x_to_y: res.direction,
|
|
175
|
+
pair: pool_manager_1.LiquidityPool.parsePoolsFromServerData2(res.pair, token_x, token_y, res.direction)
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
else if (res.type === 'RouterSWAP') {
|
|
179
|
+
if (res.bridge_token === 'STX') {
|
|
180
|
+
return {
|
|
181
|
+
valid: SwapType.ROUTER_STX,
|
|
182
|
+
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),
|
|
183
|
+
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),
|
|
184
|
+
router_from_mode: res.from_direction,
|
|
185
|
+
router_to_mode: res.to_direction
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
else if (res.bridge_token === 'STSW') {
|
|
189
|
+
return {
|
|
190
|
+
valid: SwapType.ROUTER_STSW,
|
|
191
|
+
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),
|
|
192
|
+
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),
|
|
193
|
+
router_from_mode: res.from_direction,
|
|
194
|
+
router_to_mode: res.to_direction
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
else {
|
|
198
|
+
return { valid: SwapType.NO_ROUTE };
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
else if (res.type === 'RouterSWAP2') {
|
|
202
|
+
if (res.bridge_token === 'STSW|STX') {
|
|
203
|
+
return {
|
|
204
|
+
valid: SwapType.ROUTER_STSW_STX,
|
|
205
|
+
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),
|
|
206
|
+
router_bridge_pair: pool_manager_1.LiquidityPool.parsePoolsFromServerData2(res.bridge_pair, token_manager_1.Token.getBaseTokens(this.stackswap, token_manager_1.BaseToken.STSW), token_manager_1.Token.getBaseTokens(this.stackswap, token_manager_1.BaseToken.STX), res.bridge_direction),
|
|
207
|
+
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),
|
|
208
|
+
router_from_mode: res.from_direction,
|
|
209
|
+
router_bridge_mode: res.bridge_direction,
|
|
210
|
+
router_to_mode: res.to_direction
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
else if (res.bridge_token === 'STX|STSW') {
|
|
214
|
+
return {
|
|
215
|
+
valid: SwapType.ROUTER_STX_STSW,
|
|
216
|
+
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),
|
|
217
|
+
router_bridge_pair: pool_manager_1.LiquidityPool.parsePoolsFromServerData2(res.bridge_pair, token_manager_1.Token.getBaseTokens(this.stackswap, token_manager_1.BaseToken.STX), token_manager_1.Token.getBaseTokens(this.stackswap, token_manager_1.BaseToken.STSW), res.bridge_direction),
|
|
218
|
+
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),
|
|
219
|
+
router_from_mode: res.from_direction,
|
|
220
|
+
router_bridge_mode: res.bridge_direction,
|
|
221
|
+
router_to_mode: res.to_direction
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
return { valid: SwapType.NO_ROUTE };
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
else {
|
|
229
|
+
return { valid: SwapType.NO_ROUTE };
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
async findRouter2(token_x, token_y, current_block) {
|
|
233
|
+
const url = this.stackswap.config.STACKS_BACKEND_URL() + '/api/v1/swap_v2/' + token_x.addr + '/' + token_y.addr + '/' + current_block;
|
|
234
|
+
const result = await axios_1.default.get(url, { timeout: 50000, });
|
|
235
|
+
const res = result.data;
|
|
236
|
+
if (Object.keys(res).length === 0) {
|
|
237
|
+
return { valid: SwapType.NO_ROUTE };
|
|
238
|
+
}
|
|
239
|
+
else if (res.type === 'SWAP') {
|
|
240
|
+
return {
|
|
241
|
+
valid: SwapType.SINGLE,
|
|
242
|
+
x_to_y: res.direction,
|
|
243
|
+
pair: pool_manager_1.LiquidityPool.parsePoolsFromServerData2(res.pair, token_x, token_y, res.direction)
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
else if (res.type === 'RouterSWAP') {
|
|
247
|
+
if (res.bridge_token === 'STX') {
|
|
248
|
+
return {
|
|
249
|
+
valid: SwapType.ROUTER_STX,
|
|
250
|
+
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),
|
|
251
|
+
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),
|
|
252
|
+
router_from_mode: res.from_direction,
|
|
253
|
+
router_to_mode: res.to_direction
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
else if (res.bridge_token === 'STSW') {
|
|
257
|
+
return {
|
|
258
|
+
valid: SwapType.ROUTER_STSW,
|
|
259
|
+
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),
|
|
260
|
+
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),
|
|
261
|
+
router_from_mode: res.from_direction,
|
|
262
|
+
router_to_mode: res.to_direction
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
else {
|
|
266
|
+
return { valid: SwapType.NO_ROUTE };
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
else {
|
|
270
|
+
return { valid: SwapType.NO_ROUTE };
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
async findRouter(token_x, token_y, current_block) {
|
|
274
|
+
const url = this.stackswap.config.STACKS_BACKEND_URL() + '/api/v1/swap/' + token_x.addr + '/' + token_y.addr + '/' + current_block;
|
|
275
|
+
const result = await axios_1.default.get(url, { timeout: 50000, });
|
|
276
|
+
const res = result.data;
|
|
277
|
+
if (Object.keys(res).length === 0) {
|
|
278
|
+
return { valid: SwapType.NO_ROUTE };
|
|
279
|
+
}
|
|
280
|
+
else if (res.type === 'SWAP') {
|
|
281
|
+
return {
|
|
282
|
+
valid: SwapType.SINGLE,
|
|
283
|
+
x_to_y: res.direction,
|
|
284
|
+
pair: pool_manager_1.LiquidityPool.parsePoolsFromServerData2(res.pair, token_x, token_y, res.direction)
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
else if (res.type === 'RouterSWAP') {
|
|
288
|
+
if (res.bridge_token === 'STX') {
|
|
289
|
+
return {
|
|
290
|
+
valid: SwapType.ROUTER_STX,
|
|
291
|
+
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),
|
|
292
|
+
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),
|
|
293
|
+
router_from_mode: res.from_direction,
|
|
294
|
+
router_to_mode: res.to_direction
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
else if (res.bridge_token === 'STSW') {
|
|
298
|
+
return {
|
|
299
|
+
valid: SwapType.ROUTER_STSW,
|
|
300
|
+
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),
|
|
301
|
+
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),
|
|
302
|
+
router_from_mode: res.from_direction,
|
|
303
|
+
router_to_mode: res.to_direction
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
else {
|
|
307
|
+
return { valid: SwapType.NO_ROUTE };
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
else {
|
|
311
|
+
return { valid: SwapType.NO_ROUTE };
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
async routerSwap2ContractCall(token_from_amount, token_to_amount, token_bridge1_amount, token_bridge2_amount, pair_from, pair_to, pair_bridge, router_from_mode, router_to_mode, router_bridge_mode, tolerance, callback = null) {
|
|
315
|
+
const token_from = router_from_mode ? pair_from.token_x : pair_from.token_y;
|
|
316
|
+
const token_bridge1 = router_from_mode ? pair_from.token_y : pair_from.token_x;
|
|
317
|
+
const token_bridge2 = router_to_mode ? pair_to.token_x : pair_to.token_y;
|
|
318
|
+
const token_to = router_to_mode ? pair_to.token_y : pair_to.token_x;
|
|
319
|
+
const from_amt = new bignumber_js_1.default(10 ** token_from.decimal).multipliedBy(token_from_amount).integerValue();
|
|
320
|
+
const bridge1_amt = new bignumber_js_1.default(10 ** token_bridge1.decimal).multipliedBy(token_bridge1_amount).integerValue();
|
|
321
|
+
const bridge2_amt = new bignumber_js_1.default(10 ** token_bridge2.decimal).multipliedBy(token_bridge2_amount).integerValue();
|
|
322
|
+
const to_amt = new bignumber_js_1.default(10 ** token_to.decimal).multipliedBy(token_to_amount).integerValue();
|
|
323
|
+
const bridge1_min = bridge1_amt.multipliedBy((100 - tolerance)).dividedBy(100).integerValue();
|
|
324
|
+
const bridge1_max = bridge1_amt.multipliedBy((100 + tolerance)).dividedBy(100).integerValue();
|
|
325
|
+
const bridge2_min = bridge2_amt.multipliedBy((100 - tolerance)).dividedBy(100).multipliedBy((100 - tolerance)).dividedBy(100).integerValue();
|
|
326
|
+
const bridge2_max = bridge2_amt.multipliedBy((100 + tolerance)).dividedBy(100).multipliedBy((100 + tolerance)).dividedBy(100).integerValue();
|
|
327
|
+
const to_min = to_amt.multipliedBy((100 - tolerance)).dividedBy(100).multipliedBy((100 - tolerance)).dividedBy(100).multipliedBy((100 - tolerance)).dividedBy(100).integerValue();
|
|
328
|
+
const token_from_addr = (token_from.addr);
|
|
329
|
+
const token_bridge1_addr = (token_bridge1.addr);
|
|
330
|
+
const token_bridge2_addr = (token_bridge2.addr);
|
|
331
|
+
const token_to_addr = (token_to.addr);
|
|
332
|
+
const pair_from_addr = pair_from.token_lp.addr;
|
|
333
|
+
const pair_bridge_addr = pair_bridge.token_lp.addr;
|
|
334
|
+
const pair_to_addr = pair_to.token_lp.addr;
|
|
335
|
+
const from_mode = router_from_mode ? (0, transactions_1.trueCV)() : (0, transactions_1.falseCV)();
|
|
336
|
+
const bridge_mode = router_bridge_mode ? (0, transactions_1.trueCV)() : (0, transactions_1.falseCV)();
|
|
337
|
+
const to_mode = router_to_mode ? (0, transactions_1.trueCV)() : (0, transactions_1.falseCV)();
|
|
338
|
+
const post_condition = [];
|
|
339
|
+
post_condition.push(await (0, util_1.getPostConditionFromAsset)(this.stackswap, this.stackswap.getSenderAddress(), token_from.addr, from_amt.toString(), transactions_1.FungibleConditionCode.Equal));
|
|
340
|
+
post_condition.push(await (0, util_1.getPostConditionFromAsset)(this.stackswap, pair_from_addr, token_bridge1.addr, bridge1_min.toString(), transactions_1.FungibleConditionCode.GreaterEqual));
|
|
341
|
+
post_condition.push(await (0, util_1.getPostConditionFromAsset)(this.stackswap, this.stackswap.getSenderAddress(), token_bridge1.addr, bridge1_max.toString(), transactions_1.FungibleConditionCode.LessEqual));
|
|
342
|
+
post_condition.push(await (0, util_1.getPostConditionFromAsset)(this.stackswap, pair_bridge_addr, token_bridge2.addr, bridge2_min.toString(), transactions_1.FungibleConditionCode.GreaterEqual));
|
|
343
|
+
post_condition.push(await (0, util_1.getPostConditionFromAsset)(this.stackswap, this.stackswap.getSenderAddress(), token_bridge2.addr, bridge2_max.toString(), transactions_1.FungibleConditionCode.LessEqual));
|
|
344
|
+
post_condition.push(await (0, util_1.getPostConditionFromAsset)(this.stackswap, pair_to_addr, token_to.addr, to_min.toString(), transactions_1.FungibleConditionCode.GreaterEqual));
|
|
345
|
+
const options = (0, util_1.getWriteOptions)(this.stackswap, this.stackswap.config.CONTRACT_NAME_STACKSWAP_ROUTER_SWAP2(), 'exchange-router2', [
|
|
346
|
+
(0, util_1.parseAddressToCV)(token_from_addr),
|
|
347
|
+
(0, util_1.parseAddressToCV)(token_bridge1_addr),
|
|
348
|
+
(0, util_1.parseAddressToCV)(token_bridge2_addr),
|
|
349
|
+
(0, util_1.parseAddressToCV)(token_to_addr),
|
|
350
|
+
(0, util_1.parseAddressToCV)(pair_from_addr),
|
|
351
|
+
(0, util_1.parseAddressToCV)(pair_bridge_addr),
|
|
352
|
+
(0, util_1.parseAddressToCV)(pair_to_addr),
|
|
353
|
+
from_mode,
|
|
354
|
+
bridge_mode,
|
|
355
|
+
to_mode,
|
|
356
|
+
(0, transactions_1.uintCV)(from_amt.toString()),
|
|
357
|
+
(0, transactions_1.uintCV)(bridge1_min.toString()),
|
|
358
|
+
(0, transactions_1.uintCV)(bridge2_min.toString()),
|
|
359
|
+
(0, transactions_1.uintCV)(to_min.toString()),
|
|
360
|
+
(0, util_1.parseAddressToCV)(this.stackswap.getSenderAddress()),
|
|
361
|
+
], post_condition, callback);
|
|
362
|
+
(0, connect_1.openContractCall)(options);
|
|
363
|
+
}
|
|
364
|
+
async routerSwapContractCall(token_from_amount, token_to_amount, token_bridge_amount, pair_from, pair_to, router_from_mode, router_to_mode, tolerance, callback = null) {
|
|
365
|
+
const token_from = router_from_mode ? pair_from.token_x : pair_from.token_y;
|
|
366
|
+
const token_bridge = router_from_mode ? pair_from.token_y : pair_from.token_x;
|
|
367
|
+
const token_to = router_to_mode ? pair_to.token_y : pair_to.token_x;
|
|
368
|
+
const from_amt = new bignumber_js_1.default(10 ** token_from.decimal).multipliedBy(token_from_amount).integerValue();
|
|
369
|
+
const bridge_amt = new bignumber_js_1.default(10 ** token_bridge.decimal).multipliedBy(token_bridge_amount).integerValue();
|
|
370
|
+
const to_amt = new bignumber_js_1.default(10 ** token_to.decimal).multipliedBy(token_to_amount).integerValue();
|
|
371
|
+
const bridge_min = bridge_amt.multipliedBy((100 - tolerance)).dividedBy(100).integerValue();
|
|
372
|
+
const bridge_max = bridge_amt.multipliedBy((100 + tolerance)).dividedBy(100).integerValue();
|
|
373
|
+
const to_min = to_amt.multipliedBy((100 - tolerance)).dividedBy(100).multipliedBy((100 - tolerance)).dividedBy(100).integerValue();
|
|
374
|
+
const token_from_addr = (token_from.addr);
|
|
375
|
+
const token_bridge_addr = (token_bridge.addr);
|
|
376
|
+
const token_to_addr = (token_to.addr);
|
|
377
|
+
const pair_from_addr = pair_from.token_lp.addr;
|
|
378
|
+
const pair_to_addr = pair_to.token_lp.addr;
|
|
379
|
+
const from_mode = router_from_mode ? (0, transactions_1.trueCV)() : (0, transactions_1.falseCV)();
|
|
380
|
+
const to_mode = router_to_mode ? (0, transactions_1.trueCV)() : (0, transactions_1.falseCV)();
|
|
381
|
+
const post_condition = [];
|
|
382
|
+
post_condition.push(await (0, util_1.getPostConditionFromAsset)(this.stackswap, this.stackswap.getSenderAddress(), token_from.addr, from_amt.toString(), transactions_1.FungibleConditionCode.Equal));
|
|
383
|
+
post_condition.push(await (0, util_1.getPostConditionFromAsset)(this.stackswap, pair_from_addr, token_bridge.addr, bridge_min.toString(), transactions_1.FungibleConditionCode.GreaterEqual));
|
|
384
|
+
post_condition.push(await (0, util_1.getPostConditionFromAsset)(this.stackswap, this.stackswap.getSenderAddress(), token_bridge.addr, bridge_max.toString(), transactions_1.FungibleConditionCode.LessEqual));
|
|
385
|
+
post_condition.push(await (0, util_1.getPostConditionFromAsset)(this.stackswap, pair_to_addr, token_to.addr, to_min.toString(), transactions_1.FungibleConditionCode.GreaterEqual));
|
|
386
|
+
const options = (0, util_1.getWriteOptions)(this.stackswap, this.stackswap.config.CONTRACT_NAME_STACKSWAP_ROUTER_SWAP(), 'router-swap', [
|
|
387
|
+
(0, util_1.parseAddressToCV)(token_from_addr),
|
|
388
|
+
(0, util_1.parseAddressToCV)(token_bridge_addr),
|
|
389
|
+
(0, util_1.parseAddressToCV)(token_to_addr),
|
|
390
|
+
(0, util_1.parseAddressToCV)(pair_from_addr),
|
|
391
|
+
(0, util_1.parseAddressToCV)(pair_to_addr),
|
|
392
|
+
from_mode,
|
|
393
|
+
to_mode,
|
|
394
|
+
(0, transactions_1.uintCV)(from_amt.toString()),
|
|
395
|
+
(0, transactions_1.uintCV)(bridge_min.toString()),
|
|
396
|
+
(0, transactions_1.uintCV)(to_min.toString())
|
|
397
|
+
], post_condition, callback);
|
|
398
|
+
(0, connect_1.openContractCall)(options);
|
|
399
|
+
}
|
|
400
|
+
async swapContractCall(token_x_amount, token_y_amount, pair, x_to_y, tolerance, callback = null) {
|
|
401
|
+
const token_x = x_to_y ? pair.token_x : pair.token_y;
|
|
402
|
+
const token_y = x_to_y ? pair.token_y : pair.token_x;
|
|
403
|
+
const dx_bn = new bignumber_js_1.default(10 ** token_x.decimal).multipliedBy(token_x_amount);
|
|
404
|
+
const dy_min_bn = new bignumber_js_1.default(10 ** token_y.decimal).multipliedBy(token_y_amount).multipliedBy(100 - tolerance).dividedBy(100);
|
|
405
|
+
const dx = dx_bn.toFixed(0).toString();
|
|
406
|
+
const dy_min = dy_min_bn.toFixed(0).toString();
|
|
407
|
+
const functionName = x_to_y ? 'swap-x-for-y' : 'swap-y-for-x';
|
|
408
|
+
const token_x_addr = x_to_y ? (token_x.addr) : (token_y.addr);
|
|
409
|
+
const token_y_addr = x_to_y ? (token_y.addr) : (token_x.addr);
|
|
410
|
+
const post_condition = [];
|
|
411
|
+
post_condition.push(await (0, util_1.getPostConditionFromAsset)(this.stackswap, this.stackswap.getSenderAddress(), token_x.addr, dx, transactions_1.FungibleConditionCode.Equal));
|
|
412
|
+
post_condition.push(await (0, util_1.getPostConditionFromAsset)(this.stackswap, pair.token_lp.addr, token_y.addr, dy_min, transactions_1.FungibleConditionCode.GreaterEqual));
|
|
413
|
+
const options = (0, util_1.getWriteOptions)(this.stackswap, this.stackswap.config.CONTRACT_NAME_STACKSWAP_SWAP(), functionName, [
|
|
414
|
+
(0, util_1.parseAddressToCV)(token_x_addr), (0, util_1.parseAddressToCV)(token_y_addr),
|
|
415
|
+
(0, util_1.parseAddressToCV)(pair.token_lp.addr), (0, transactions_1.uintCV)(dx), (0, transactions_1.uintCV)(dy_min)
|
|
416
|
+
], post_condition, callback);
|
|
417
|
+
(0, connect_1.openContractCall)(options);
|
|
418
|
+
}
|
|
419
|
+
async importContractCall(pair, x_to_y, amount_x, amount_y, callback = null) {
|
|
420
|
+
const token_x = x_to_y ? pair.token_x : pair.token_y;
|
|
421
|
+
const token_y = x_to_y ? pair.token_y : pair.token_x;
|
|
422
|
+
const dx_temp = new bignumber_js_1.default(10 ** token_x.decimal).multipliedBy(amount_x);
|
|
423
|
+
const dy_temp = new bignumber_js_1.default(10 ** token_y.decimal).multipliedBy(amount_y);
|
|
424
|
+
const dx = x_to_y ? dx_temp : dy_temp;
|
|
425
|
+
const dy = x_to_y ? dy_temp : dx_temp;
|
|
426
|
+
const dx_out = dx_temp.multipliedBy(1.2).toFixed(0).toString();
|
|
427
|
+
const dy_out = dy_temp.multipliedBy(1.2).toFixed(0).toString();
|
|
428
|
+
const token_x_addr = x_to_y ? (token_x.addr) : (token_y.addr);
|
|
429
|
+
const token_y_addr = x_to_y ? (token_y.addr) : (token_x.addr);
|
|
430
|
+
const post_condition = [];
|
|
431
|
+
post_condition.push(await (0, util_1.getPostConditionFromAsset)(this.stackswap, this.stackswap.getSenderAddress(), token_x.addr, dx_out, transactions_1.FungibleConditionCode.LessEqual));
|
|
432
|
+
post_condition.push(await (0, util_1.getPostConditionFromAsset)(this.stackswap, this.stackswap.getSenderAddress(), token_y.addr, dy_out, transactions_1.FungibleConditionCode.LessEqual));
|
|
433
|
+
const options = (0, util_1.getWriteOptions)(this.stackswap, this.stackswap.config.CONTRACT_NAME_STACKSWAP_SWAP(), 'add-to-position', [
|
|
434
|
+
(0, util_1.parseAddressToCV)(token_x_addr), (0, util_1.parseAddressToCV)(token_y_addr),
|
|
435
|
+
(0, util_1.parseAddressToCV)(pair.token_lp.addr), (0, transactions_1.uintCV)(dx.toFixed(0).toString()), (0, transactions_1.uintCV)(dy.toFixed(0).toString())
|
|
436
|
+
], post_condition, callback);
|
|
437
|
+
(0, connect_1.openContractCall)(options);
|
|
438
|
+
}
|
|
439
|
+
async removeContractCall(pair, percent, user_lp_total, dx, dy, swap_contract = this.stackswap.config.CONTRACT_NAME_STACKSWAP_SWAP(), callback = null) {
|
|
440
|
+
const token_x_addr = pair.token_x.addr;
|
|
441
|
+
const token_y_addr = pair.token_y.addr;
|
|
442
|
+
const pair_addr = pair.token_lp.addr;
|
|
443
|
+
const post_condition = [];
|
|
444
|
+
const burn_amount = new bignumber_js_1.default(user_lp_total).multipliedBy(percent * 1.01).div(100).toFixed(0).toString();
|
|
445
|
+
post_condition.push(await (0, util_1.getPostConditionFromAsset)(this.stackswap, pair.token_lp.addr, pair.token_x.addr, dx, transactions_1.FungibleConditionCode.GreaterEqual));
|
|
446
|
+
post_condition.push(await (0, util_1.getPostConditionFromAsset)(this.stackswap, pair.token_lp.addr, pair.token_y.addr, dy, transactions_1.FungibleConditionCode.GreaterEqual));
|
|
447
|
+
post_condition.push(await (0, util_1.getPostConditionFromAsset)(this.stackswap, this.stackswap.getSenderAddress(), pair.token_lp.addr, burn_amount, transactions_1.FungibleConditionCode.LessEqual));
|
|
448
|
+
const options = (0, util_1.getWriteOptions)(this.stackswap, swap_contract, 'reduce-position', [
|
|
449
|
+
(0, util_1.parseAddressToCV)(token_x_addr), (0, util_1.parseAddressToCV)(token_y_addr),
|
|
450
|
+
(0, util_1.parseAddressToCV)(pair_addr), (0, transactions_1.uintCV)(percent),
|
|
451
|
+
], post_condition, callback);
|
|
452
|
+
(0, connect_1.openContractCall)(options);
|
|
453
|
+
}
|
|
454
|
+
async createContractCall(token_x, token_y, token_x_amount, token_y_amount, liquidity_token_addr, callback = null) {
|
|
455
|
+
const token_x_addr = token_x.addr;
|
|
456
|
+
const token_y_addr = token_y.addr;
|
|
457
|
+
const pair_addr = liquidity_token_addr;
|
|
458
|
+
const post_condition = [];
|
|
459
|
+
const amount_X = (0, util_1.decimal2integer)(token_x_amount, token_x.decimal);
|
|
460
|
+
const amount_Y = (0, util_1.decimal2integer)(token_y_amount, token_y.decimal);
|
|
461
|
+
post_condition.push(await (0, util_1.getPostConditionFromAsset)(this.stackswap, this.stackswap.getSenderAddress(), token_x.addr, amount_X, transactions_1.FungibleConditionCode.Equal));
|
|
462
|
+
post_condition.push(await (0, util_1.getPostConditionFromAsset)(this.stackswap, this.stackswap.getSenderAddress(), token_y.addr, amount_Y, transactions_1.FungibleConditionCode.Equal));
|
|
463
|
+
const options = (0, util_1.getWriteOptions)(this.stackswap, this.stackswap.config.CONTRACT_NAME_STACKSWAP_ONE_STEP_MINT(), 'create-pair-new-liquidity-token', [
|
|
464
|
+
(0, util_1.parseAddressToCV)(token_x_addr), (0, util_1.parseAddressToCV)(token_y_addr),
|
|
465
|
+
(0, util_1.parseAddressToCV)(pair_addr), (0, transactions_1.stringAsciiCV)(token_x.symbol + '-' + token_y.symbol),
|
|
466
|
+
(0, transactions_1.uintCV)(amount_X), (0, transactions_1.uintCV)(amount_Y), (0, util_1.parseAddressToCV)(pair_addr),
|
|
467
|
+
(0, util_1.parseAddressToCV)(this.stackswap.getQualifiedAddress(this.stackswap.config.CONTRACT_NAME_STACKSWAP_SWAP())),
|
|
468
|
+
], post_condition, callback);
|
|
469
|
+
(0, connect_1.openContractCall)(options);
|
|
470
|
+
}
|
|
471
|
+
price_impact(x_to_y, pair, token_x_amount) {
|
|
472
|
+
try {
|
|
473
|
+
const balance_x = x_to_y ? pair.balance_x : pair.balance_y;
|
|
474
|
+
const token_x_decimal = x_to_y ? pair.token_x.decimal : pair.token_y.decimal;
|
|
475
|
+
const balance_y = x_to_y ? pair.balance_y : pair.balance_y;
|
|
476
|
+
const dx = new bignumber_js_1.default(token_x_amount).multipliedBy(10 ** token_x_decimal);
|
|
477
|
+
if (dx.toNumber() > new bignumber_js_1.default(balance_x).toNumber()) {
|
|
478
|
+
return 100;
|
|
479
|
+
}
|
|
480
|
+
const dy_1 = new bignumber_js_1.default(balance_y).multipliedBy(dx);
|
|
481
|
+
const dy_2 = new bignumber_js_1.default(balance_x).plus(dx);
|
|
482
|
+
const dy = (dy_1.toNumber()) / (dy_2.toNumber());
|
|
483
|
+
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));
|
|
484
|
+
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));
|
|
485
|
+
const price_impact2 = output_price.minus(input_price).multipliedBy(10000).dividedBy(input_price);
|
|
486
|
+
let price_impact = price_impact2.toNumber();
|
|
487
|
+
if (price_impact >= 10000) {
|
|
488
|
+
price_impact = 9999;
|
|
489
|
+
}
|
|
490
|
+
return (price_impact / 100).toFixed(5);
|
|
491
|
+
}
|
|
492
|
+
catch (e) {
|
|
493
|
+
return 0;
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
price_impact_router(router_from_mode, router_to_mode, pair_from, pair_to, token_from_amount) {
|
|
497
|
+
try {
|
|
498
|
+
const from_input_balance = router_from_mode ? pair_from.balance_x : pair_from.balance_y;
|
|
499
|
+
const from_input_decimal = router_from_mode ? pair_from.token_x.decimal : pair_from.token_y.decimal;
|
|
500
|
+
const from_output_balance = router_from_mode ? pair_from.balance_y : pair_from.balance_y;
|
|
501
|
+
const from_input_dx = new bignumber_js_1.default(token_from_amount).multipliedBy(10 ** from_input_decimal);
|
|
502
|
+
if (from_input_dx.toNumber() > new bignumber_js_1.default(from_input_balance).toNumber()) {
|
|
503
|
+
return 100;
|
|
504
|
+
}
|
|
505
|
+
const from_dy_1 = new bignumber_js_1.default(from_output_balance).multipliedBy(from_input_dx);
|
|
506
|
+
const from_dy_2 = new bignumber_js_1.default(from_input_balance).plus(from_input_dx);
|
|
507
|
+
const from_dy = (from_dy_1.toNumber()) / (from_dy_2.toNumber());
|
|
508
|
+
const from_input_price = new bignumber_js_1.default(10 ** 10).multipliedBy(new bignumber_js_1.default(from_input_balance)).dividedBy(new bignumber_js_1.default(from_output_balance));
|
|
509
|
+
const from_output_price = new bignumber_js_1.default(10 ** 10).multipliedBy(new bignumber_js_1.default(from_input_balance)).plus(from_input_dx).dividedBy(new bignumber_js_1.default(from_output_balance).minus(from_dy));
|
|
510
|
+
const from_price_impact = from_output_price.minus(from_input_price).multipliedBy(10000).dividedBy(from_input_price);
|
|
511
|
+
const to_input_balance = router_to_mode ? pair_to.balance_x : pair_to.balance_y;
|
|
512
|
+
const to_input_decimal = router_to_mode ? pair_to.token_x.decimal : pair_to.token_y.decimal;
|
|
513
|
+
const to_output_balance = router_to_mode ? pair_to.balance_y : pair_to.balance_y;
|
|
514
|
+
const to_input_dx = new bignumber_js_1.default(from_dy).multipliedBy(10 ** to_input_decimal);
|
|
515
|
+
if (to_input_dx.toNumber() > new bignumber_js_1.default(to_input_balance).toNumber()) {
|
|
516
|
+
return 100;
|
|
517
|
+
}
|
|
518
|
+
const to_dy_1 = new bignumber_js_1.default(to_output_balance).multipliedBy(to_input_dx);
|
|
519
|
+
const to_dy_2 = new bignumber_js_1.default(to_input_balance).plus(to_input_dx);
|
|
520
|
+
const to_dy = (to_dy_1.toNumber()) / (to_dy_2.toNumber());
|
|
521
|
+
const to_input_price = new bignumber_js_1.default(10 ** 10).multipliedBy(new bignumber_js_1.default(to_input_balance)).dividedBy(new bignumber_js_1.default(to_output_balance));
|
|
522
|
+
const to_output_price = new bignumber_js_1.default(10 ** 10).multipliedBy(new bignumber_js_1.default(to_input_balance)).plus(to_input_dx).dividedBy(new bignumber_js_1.default(to_output_balance).minus(to_dy));
|
|
523
|
+
const to_price_impact = to_output_price.minus(to_input_price).multipliedBy(10000).dividedBy(to_input_price);
|
|
524
|
+
let price_impact = from_price_impact.toNumber() * to_price_impact.toNumber();
|
|
525
|
+
if (price_impact >= 10000) {
|
|
526
|
+
price_impact = 9999;
|
|
527
|
+
}
|
|
528
|
+
return (price_impact / 100).toFixed(5);
|
|
529
|
+
}
|
|
530
|
+
catch (e) {
|
|
531
|
+
return 0;
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
price_impact_router2(router_from_mode, router_bridge_mode, router_to_mode, pair_from, pair_bridge, pair_to, token_from_amount) {
|
|
535
|
+
try {
|
|
536
|
+
const from_input_balance = router_from_mode ? pair_from.balance_x : pair_from.balance_y;
|
|
537
|
+
const from_input_decimal = router_from_mode ? pair_from.token_x.decimal : pair_from.token_y.decimal;
|
|
538
|
+
const from_output_balance = router_from_mode ? pair_from.balance_y : pair_from.balance_y;
|
|
539
|
+
const from_input_dx = new bignumber_js_1.default(token_from_amount).multipliedBy(10 ** from_input_decimal);
|
|
540
|
+
if (from_input_dx.toNumber() > new bignumber_js_1.default(from_input_balance).toNumber()) {
|
|
541
|
+
return 100;
|
|
542
|
+
}
|
|
543
|
+
const from_dy_1 = new bignumber_js_1.default(from_output_balance).multipliedBy(from_input_dx);
|
|
544
|
+
const from_dy_2 = new bignumber_js_1.default(from_input_balance).plus(from_input_dx);
|
|
545
|
+
const from_dy = (from_dy_1.toNumber()) / (from_dy_2.toNumber());
|
|
546
|
+
const from_input_price = new bignumber_js_1.default(10 ** 10).multipliedBy(new bignumber_js_1.default(from_input_balance)).dividedBy(new bignumber_js_1.default(from_output_balance));
|
|
547
|
+
const from_output_price = new bignumber_js_1.default(10 ** 10).multipliedBy(new bignumber_js_1.default(from_input_balance)).plus(from_input_dx).dividedBy(new bignumber_js_1.default(from_output_balance).minus(from_dy));
|
|
548
|
+
const from_price_impact = from_output_price.minus(from_input_price).multipliedBy(10000).dividedBy(from_input_price);
|
|
549
|
+
const bridge_input_balance = router_bridge_mode ? pair_bridge.balance_x : pair_bridge.balance_y;
|
|
550
|
+
const bridge_input_decimal = router_bridge_mode ? pair_bridge.token_x.decimal : pair_bridge.token_y.decimal;
|
|
551
|
+
const bridge_output_balance = router_bridge_mode ? pair_bridge.balance_y : pair_bridge.balance_y;
|
|
552
|
+
const bridge_input_dx = new bignumber_js_1.default(from_dy).multipliedBy(10 ** bridge_input_decimal);
|
|
553
|
+
if (bridge_input_dx.toNumber() > new bignumber_js_1.default(bridge_input_balance).toNumber()) {
|
|
554
|
+
return 100;
|
|
555
|
+
}
|
|
556
|
+
const bridge_dy_1 = new bignumber_js_1.default(bridge_output_balance).multipliedBy(bridge_input_dx);
|
|
557
|
+
const bridge_dy_2 = new bignumber_js_1.default(bridge_input_balance).plus(bridge_input_dx);
|
|
558
|
+
const bridge_dy = (bridge_dy_1.toNumber()) / (bridge_dy_2.toNumber());
|
|
559
|
+
const bridge_input_price = new bignumber_js_1.default(10 ** 10).multipliedBy(new bignumber_js_1.default(bridge_input_balance)).dividedBy(new bignumber_js_1.default(bridge_output_balance));
|
|
560
|
+
const bridge_output_price = new bignumber_js_1.default(10 ** 10).multipliedBy(new bignumber_js_1.default(bridge_input_balance)).plus(bridge_input_dx).dividedBy(new bignumber_js_1.default(bridge_output_balance).minus(bridge_dy));
|
|
561
|
+
const bridge_price_impact = bridge_output_price.minus(bridge_input_price).multipliedBy(10000).dividedBy(bridge_input_price);
|
|
562
|
+
const to_input_balance = router_to_mode ? pair_to.balance_x : pair_to.balance_y;
|
|
563
|
+
const to_input_decimal = router_to_mode ? pair_to.token_x.decimal : pair_to.token_y.decimal;
|
|
564
|
+
const to_output_balance = router_to_mode ? pair_to.balance_y : pair_to.balance_y;
|
|
565
|
+
const to_input_dx = new bignumber_js_1.default(bridge_dy).multipliedBy(10 ** to_input_decimal);
|
|
566
|
+
if (to_input_dx.toNumber() > new bignumber_js_1.default(to_input_balance).toNumber()) {
|
|
567
|
+
return 100;
|
|
568
|
+
}
|
|
569
|
+
const to_dy_1 = new bignumber_js_1.default(to_output_balance).multipliedBy(to_input_dx);
|
|
570
|
+
const to_dy_2 = new bignumber_js_1.default(to_input_balance).plus(to_input_dx);
|
|
571
|
+
const to_dy = (to_dy_1.toNumber()) / (to_dy_2.toNumber());
|
|
572
|
+
const to_input_price = new bignumber_js_1.default(10 ** 10).multipliedBy(new bignumber_js_1.default(to_input_balance)).dividedBy(new bignumber_js_1.default(to_output_balance));
|
|
573
|
+
const to_output_price = new bignumber_js_1.default(10 ** 10).multipliedBy(new bignumber_js_1.default(to_input_balance)).plus(to_input_dx).dividedBy(new bignumber_js_1.default(to_output_balance).minus(to_dy));
|
|
574
|
+
const to_price_impact = to_output_price.minus(to_input_price).multipliedBy(10000).dividedBy(to_input_price);
|
|
575
|
+
let price_impact = from_price_impact.toNumber() * bridge_price_impact.toNumber() * to_price_impact.toNumber();
|
|
576
|
+
if (price_impact >= 10000) {
|
|
577
|
+
price_impact = 9999;
|
|
578
|
+
}
|
|
579
|
+
return (price_impact / 100).toFixed(5);
|
|
580
|
+
}
|
|
581
|
+
catch (e) {
|
|
582
|
+
return 0;
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
getPriceString(pair, token_x, token_y, price, price_invert) {
|
|
586
|
+
try {
|
|
587
|
+
if (price < 0) {
|
|
588
|
+
pair;
|
|
589
|
+
return 'dx exceed pool balance';
|
|
590
|
+
}
|
|
591
|
+
else if (token_x != null && token_y != null) {
|
|
592
|
+
if (price_invert) {
|
|
593
|
+
return ` 1 ${token_y.symbol} = ${new bignumber_js_1.default((1 / price)).toFixed(token_x.decimal)} ${token_x.symbol}`;
|
|
594
|
+
}
|
|
595
|
+
else {
|
|
596
|
+
return ` 1 ${token_x.symbol} = ${new bignumber_js_1.default(price).toFixed(token_y.decimal)} ${token_y.symbol}`;
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
return '';
|
|
600
|
+
}
|
|
601
|
+
catch (e) {
|
|
602
|
+
return '';
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
getPricesWithSwapData(swap_data, token_x_amount, price_invert) {
|
|
606
|
+
let price = -1;
|
|
607
|
+
switch (swap_data.valid) {
|
|
608
|
+
case SwapType.SINGLE:
|
|
609
|
+
try {
|
|
610
|
+
const temp = swap_data.pair.getPoolPrice(!swap_data.x_to_y, Number(token_x_amount) <= 0 || token_x_amount == '' ? '-1' : token_x_amount);
|
|
611
|
+
if (temp.output_price !== '0') {
|
|
612
|
+
price = (1 / parseFloat(temp.output_price));
|
|
613
|
+
}
|
|
614
|
+
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);
|
|
615
|
+
const token_y_amount = new bignumber_js_1.default(temp.dy).toFixed(Number(swap_data.pair.getTokenY(swap_data.x_to_y).decimal)).toString();
|
|
616
|
+
return { price: price, dy: token_y_amount, price_strings: price_strings };
|
|
617
|
+
}
|
|
618
|
+
catch (e) {
|
|
619
|
+
return { price: price, dy: '0', price_strings: '' };
|
|
620
|
+
}
|
|
621
|
+
case SwapType.ROUTER_STX:
|
|
622
|
+
return this.getRouterDY_V1(swap_data, token_x_amount, token_manager_1.Token.getBaseTokens(this.stackswap, token_manager_1.BaseToken.STX), price_invert);
|
|
623
|
+
case SwapType.ROUTER_STSW:
|
|
624
|
+
return this.getRouterDY_V1(swap_data, token_x_amount, token_manager_1.Token.getBaseTokens(this.stackswap, token_manager_1.BaseToken.STSW), price_invert);
|
|
625
|
+
case SwapType.ROUTER_STX_STSW:
|
|
626
|
+
return this.getRouterDY_V2(swap_data, token_x_amount, token_manager_1.Token.getBaseTokens(this.stackswap, token_manager_1.BaseToken.STX), token_manager_1.Token.getBaseTokens(this.stackswap, token_manager_1.BaseToken.STSW), price_invert);
|
|
627
|
+
case SwapType.ROUTER_STSW_STX:
|
|
628
|
+
return this.getRouterDY_V2(swap_data, token_x_amount, token_manager_1.Token.getBaseTokens(this.stackswap, token_manager_1.BaseToken.STSW), token_manager_1.Token.getBaseTokens(this.stackswap, token_manager_1.BaseToken.STX), price_invert);
|
|
629
|
+
default:
|
|
630
|
+
return { price: price, dy: '0', price_strings: '' };
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
calcParamsFromSwapData(swap_data, token_x_amount, price_invert, slippage_tolerance) {
|
|
634
|
+
if (!swap_data)
|
|
635
|
+
return null;
|
|
636
|
+
const dy_and_price = this.stackswap.swapManager.getPricesWithSwapData(swap_data, token_x_amount, price_invert);
|
|
637
|
+
let temp_price_impact = 0;
|
|
638
|
+
if (swap_data.valid === SwapType.ROUTER_STSW ||
|
|
639
|
+
swap_data.valid === SwapType.ROUTER_STX) {
|
|
640
|
+
temp_price_impact = Math.max(Number(this.stackswap.swapManager.price_impact(swap_data.router_from_mode, swap_data.router_from_pair, token_x_amount)), Number(this.stackswap.swapManager.price_impact(swap_data.router_to_mode, swap_data.router_to_pair, dy_and_price.bridge_amount.toString())));
|
|
641
|
+
}
|
|
642
|
+
else if (swap_data.valid === SwapType.ROUTER_STSW_STX ||
|
|
643
|
+
swap_data.valid === SwapType.ROUTER_STX_STSW) {
|
|
644
|
+
temp_price_impact = Math.max(Number(this.stackswap.swapManager.price_impact(swap_data.router_from_mode, swap_data.router_from_pair, token_x_amount)), Number(this.stackswap.swapManager.price_impact(swap_data.router_bridge_mode, swap_data.router_bridge_pair, dy_and_price.bridge_amount1.toString())), Number(this.stackswap.swapManager.price_impact(swap_data.router_to_mode, swap_data.router_to_pair, dy_and_price.bridge_amount2.toString())));
|
|
645
|
+
}
|
|
646
|
+
else if (swap_data.valid === SwapType.SINGLE) {
|
|
647
|
+
temp_price_impact = Number(this.stackswap.swapManager.price_impact(swap_data.x_to_y, swap_data.pair, token_x_amount));
|
|
648
|
+
}
|
|
649
|
+
if (!temp_price_impact) {
|
|
650
|
+
temp_price_impact = 0;
|
|
651
|
+
}
|
|
652
|
+
dy_and_price.price_impact = temp_price_impact;
|
|
653
|
+
dy_and_price.min_dy = new bignumber_js_1.default(dy_and_price.dy).multipliedBy(100 - slippage_tolerance).dividedBy(100).toNumber();
|
|
654
|
+
return dy_and_price;
|
|
655
|
+
}
|
|
656
|
+
getRouterDY_V1(swap_data, token_x_amount, bridge_token, price_invert) {
|
|
657
|
+
let price = -1;
|
|
658
|
+
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);
|
|
659
|
+
let router_from_price = -1;
|
|
660
|
+
if (from_res.output_price !== '0') {
|
|
661
|
+
router_from_price = (1 / parseFloat(from_res.output_price));
|
|
662
|
+
}
|
|
663
|
+
const router_bridge_amt = new bignumber_js_1.default(new bignumber_js_1.default(from_res.dy).toFixed(Number(bridge_token.decimal))).toString();
|
|
664
|
+
const to_res = swap_data.router_to_pair.getPoolPrice(!swap_data.router_to_mode, Number(router_bridge_amt) <= 0 ? '-1' : router_bridge_amt);
|
|
665
|
+
let router_to_price = -1;
|
|
666
|
+
if (to_res.output_price !== '0') {
|
|
667
|
+
router_to_price = (1 / parseFloat(to_res.output_price));
|
|
668
|
+
}
|
|
669
|
+
const token_y_amount = 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();
|
|
670
|
+
price = router_from_price * router_to_price;
|
|
671
|
+
const price_strings = 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);
|
|
672
|
+
return { price: price, dy: token_y_amount, price_strings: price_strings, bridge_amount: router_bridge_amt };
|
|
673
|
+
}
|
|
674
|
+
getRouterDY_V2(swap_data, token_x_amount, bridge_token1, bridge_token2, price_invert) {
|
|
675
|
+
let price = -1;
|
|
676
|
+
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);
|
|
677
|
+
let router_from_price = -1;
|
|
678
|
+
if (from_res.output_price !== '0') {
|
|
679
|
+
router_from_price = (1 / parseFloat(from_res.output_price));
|
|
680
|
+
}
|
|
681
|
+
const router_bridge1_amt = new bignumber_js_1.default(new bignumber_js_1.default(from_res.dy).toFixed(Number(bridge_token1.decimal))).toString();
|
|
682
|
+
let router_bridge_price = -1;
|
|
683
|
+
const bridge_res = swap_data.router_bridge_pair.getPoolPrice(!swap_data.router_bridge_mode, Number(router_bridge1_amt) <= 0 || router_bridge1_amt == '' ? '1' : router_bridge1_amt);
|
|
684
|
+
if (bridge_res.output_price !== '0') {
|
|
685
|
+
router_bridge_price = (1 / parseFloat(bridge_res.output_price));
|
|
686
|
+
}
|
|
687
|
+
const router_bridge2_amt = new bignumber_js_1.default(new bignumber_js_1.default(bridge_res.dy).toFixed(Number(bridge_token2.decimal))).toString();
|
|
688
|
+
const to_res = swap_data.router_to_pair.getPoolPrice(!swap_data.router_to_mode, Number(router_bridge2_amt) <= 0 ? '-1' : router_bridge2_amt);
|
|
689
|
+
let router_to_price = -1;
|
|
690
|
+
if (to_res.output_price !== '0') {
|
|
691
|
+
router_to_price = (1 / parseFloat(to_res.output_price));
|
|
692
|
+
}
|
|
693
|
+
const token_y_amount = 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();
|
|
694
|
+
price = router_from_price * router_to_price * router_bridge_price;
|
|
695
|
+
const price_strings = 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);
|
|
696
|
+
return { price: price, dy: token_y_amount, price_strings: price_strings, bridge_amount1: router_bridge1_amt, bridge_amount2: router_bridge2_amt };
|
|
697
|
+
}
|
|
698
|
+
async STX_transfer(receiver, fromAmount, tx_id, callback = null) {
|
|
699
|
+
const options = (0, util_1.getWriteOptions)(this.stackswap, this.stackswap.config.CONTRACT_NAME_STACKSWAP_MULTICHAIN(), 'STX_transfer', [
|
|
700
|
+
(0, util_1.parseAddressToCV)(receiver),
|
|
701
|
+
(0, transactions_1.uintCV)(fromAmount),
|
|
702
|
+
(0, util_1.parseAddressToCV)(token_manager_1.Token.getBaseTokens(this.stackswap, token_manager_1.BaseToken.STX).addr),
|
|
703
|
+
(0, util_1.parseAddressToCV)(token_manager_1.Token.getBaseTokens(this.stackswap, token_manager_1.BaseToken.STSW).addr),
|
|
704
|
+
(0, util_1.parseAddressToCV)(this.stackswap.config.STX_STSW_LP()),
|
|
705
|
+
(0, transactions_1.stringAsciiCV)(tx_id),
|
|
706
|
+
], [], callback, true);
|
|
707
|
+
(0, connect_1.openContractCall)(options);
|
|
708
|
+
}
|
|
709
|
+
async SingleSwap_Token_STX(receiver, fromAmount, from_token, lp_stx_token, tx_id, callback = null) {
|
|
710
|
+
const options = (0, util_1.getWriteOptions)(this.stackswap, this.stackswap.config.CONTRACT_NAME_STACKSWAP_MULTICHAIN(), 'SingleSwap_Token_STX', [
|
|
711
|
+
(0, util_1.parseAddressToCV)(from_token),
|
|
712
|
+
(0, util_1.parseAddressToCV)(lp_stx_token),
|
|
713
|
+
(0, transactions_1.uintCV)(fromAmount),
|
|
714
|
+
(0, util_1.parseAddressToCV)(receiver),
|
|
715
|
+
(0, util_1.parseAddressToCV)(token_manager_1.Token.getBaseTokens(this.stackswap, token_manager_1.BaseToken.STX).addr),
|
|
716
|
+
(0, util_1.parseAddressToCV)(token_manager_1.Token.getBaseTokens(this.stackswap, token_manager_1.BaseToken.STSW).addr),
|
|
717
|
+
(0, util_1.parseAddressToCV)(this.stackswap.config.STX_STSW_LP()),
|
|
718
|
+
(0, transactions_1.stringAsciiCV)(tx_id),
|
|
719
|
+
], [], callback, true);
|
|
720
|
+
(0, connect_1.openContractCall)(options);
|
|
721
|
+
}
|
|
722
|
+
async Router_Token_STSW_STX(receiver, fromAmount, from_token, lp_stsw_from, tx_id, callback = null) {
|
|
723
|
+
const options = (0, util_1.getWriteOptions)(this.stackswap, this.stackswap.config.CONTRACT_NAME_STACKSWAP_MULTICHAIN(), 'Router_Token_STSW_STX', [
|
|
724
|
+
(0, util_1.parseAddressToCV)(from_token),
|
|
725
|
+
(0, util_1.parseAddressToCV)(lp_stsw_from),
|
|
726
|
+
(0, transactions_1.uintCV)(fromAmount),
|
|
727
|
+
(0, util_1.parseAddressToCV)(receiver),
|
|
728
|
+
(0, util_1.parseAddressToCV)(token_manager_1.Token.getBaseTokens(this.stackswap, token_manager_1.BaseToken.STX).addr),
|
|
729
|
+
(0, util_1.parseAddressToCV)(token_manager_1.Token.getBaseTokens(this.stackswap, token_manager_1.BaseToken.STSW).addr),
|
|
730
|
+
(0, util_1.parseAddressToCV)(this.stackswap.config.STX_STSW_LP()),
|
|
731
|
+
(0, transactions_1.stringAsciiCV)(tx_id),
|
|
732
|
+
], [], callback, true);
|
|
733
|
+
(0, connect_1.openContractCall)(options);
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
exports.SwapManager = SwapManager;
|
|
737
|
+
//# sourceMappingURL=swap.manager.js.map
|