stackswap-front-api-test-02 1.0.145 → 1.0.148
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/stackswap/config.d.ts +2 -0
- package/dist/esm/stackswap/config.js +3 -0
- package/dist/esm/stackswap/config.js.map +1 -1
- package/dist/esm/stackswap/manager/groupfarm.manager.d.ts +12 -0
- package/dist/esm/stackswap/manager/groupfarm.manager.js +20 -3
- package/dist/esm/stackswap/manager/groupfarm.manager.js.map +1 -1
- package/dist/esm/stackswap/manager/swap.manager.d.ts +8 -1
- package/dist/esm/stackswap/manager/swap.manager.js +122 -1
- package/dist/esm/stackswap/manager/swap.manager.js.map +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/stackswap/config.d.ts +2 -0
- package/dist/stackswap/config.js +3 -0
- package/dist/stackswap/config.js.map +1 -1
- package/dist/stackswap/manager/groupfarm.manager.d.ts +12 -0
- package/dist/stackswap/manager/groupfarm.manager.js +20 -3
- package/dist/stackswap/manager/groupfarm.manager.js.map +1 -1
- package/dist/stackswap/manager/swap.manager.d.ts +8 -1
- package/dist/stackswap/manager/swap.manager.js +122 -1
- package/dist/stackswap/manager/swap.manager.js.map +1 -1
- package/package.json +1 -1
- package/src/stackswap/config.ts +5 -0
- package/src/stackswap/manager/groupfarm.manager.ts +21 -3
- package/src/stackswap/manager/swap.manager.ts +131 -2
|
@@ -13,7 +13,9 @@ export enum SwapType {
|
|
|
13
13
|
ON_LOADING,
|
|
14
14
|
SINGLE = 2,
|
|
15
15
|
ROUTER_STX = 10,
|
|
16
|
-
ROUTER_STSW = 11
|
|
16
|
+
ROUTER_STSW = 11,
|
|
17
|
+
ROUTER_STX_STSW = 20,
|
|
18
|
+
ROUTER_STSW_STX = 21,
|
|
17
19
|
}
|
|
18
20
|
|
|
19
21
|
export class SwapManager {
|
|
@@ -23,6 +25,73 @@ export class SwapManager {
|
|
|
23
25
|
this.stackswap = stackswap;
|
|
24
26
|
}
|
|
25
27
|
|
|
28
|
+
|
|
29
|
+
async findRouter3(token_x : Token, token_y : Token, current_block: number) : Promise<{ valid: SwapType, [key: string]: any}> {
|
|
30
|
+
const url = this.stackswap.config.STACKS_BACKEND_URL() + '/api/v1/swap_v3/' + token_x.addr +'/'+token_y.addr + '/' + current_block;
|
|
31
|
+
const result = await axios.get ( url,
|
|
32
|
+
{timeout: 50000,});
|
|
33
|
+
// console.log('swap router : ', value)
|
|
34
|
+
const res = result.data;
|
|
35
|
+
if(Object.keys(res).length === 0){
|
|
36
|
+
return {valid : SwapType.NO_ROUTE}
|
|
37
|
+
}else if(res.type === 'SWAP'){
|
|
38
|
+
return{
|
|
39
|
+
valid : SwapType.SINGLE,
|
|
40
|
+
x_to_y : res.direction,
|
|
41
|
+
pair : LiquidityPool.parsePoolsFromServerData2(res.pair, token_x, token_y, res.direction)
|
|
42
|
+
}
|
|
43
|
+
}else if (res.type === 'RouterSWAP'){
|
|
44
|
+
if(res.bridge_token === 'STX'){
|
|
45
|
+
return {
|
|
46
|
+
valid : SwapType.ROUTER_STX,
|
|
47
|
+
router_from_pair : LiquidityPool.parsePoolsFromServerData2(res.from_pair, token_x, Token.getBaseTokens(this.stackswap, BaseToken.STX), res.from_direction),
|
|
48
|
+
router_to_pair : LiquidityPool.parsePoolsFromServerData2(res.to_pair, Token.getBaseTokens(this.stackswap, BaseToken.STX), token_y, res.to_direction),
|
|
49
|
+
router_from_mode : res.from_direction,
|
|
50
|
+
router_to_mode : res.to_direction
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
}else if(res.bridge_token === 'STSW') {
|
|
54
|
+
return{
|
|
55
|
+
valid : SwapType.ROUTER_STSW,
|
|
56
|
+
router_from_pair : LiquidityPool.parsePoolsFromServerData2(res.from_pair, token_x, Token.getBaseTokens(this.stackswap, BaseToken.STSW), res.from_direction),
|
|
57
|
+
router_to_pair : LiquidityPool.parsePoolsFromServerData2(res.to_pair, Token.getBaseTokens(this.stackswap, BaseToken.STSW), token_y, res.to_direction),
|
|
58
|
+
router_from_mode : res.from_direction,
|
|
59
|
+
router_to_mode : res.to_direction
|
|
60
|
+
}
|
|
61
|
+
} else{
|
|
62
|
+
return {valid : SwapType.NO_ROUTE}
|
|
63
|
+
}
|
|
64
|
+
} else if(res.type === 'RouterSWAP2') {
|
|
65
|
+
if(res.bridge_token === 'STSW|STX'){
|
|
66
|
+
return {
|
|
67
|
+
valid : SwapType.ROUTER_STSW_STX,
|
|
68
|
+
router_from_pair : LiquidityPool.parsePoolsFromServerData2(res.from_pair, token_x, Token.getBaseTokens(this.stackswap, BaseToken.STSW), res.from_direction),
|
|
69
|
+
router_bridge_pair : LiquidityPool.parsePoolsFromServerData2(res.bridge_pair, Token.getBaseTokens(this.stackswap, BaseToken.STSW), Token.getBaseTokens(this.stackswap, BaseToken.STX), res.bridge_direction),
|
|
70
|
+
router_to_pair : LiquidityPool.parsePoolsFromServerData2(res.to_pair, Token.getBaseTokens(this.stackswap, BaseToken.STX), token_y, res.to_direction),
|
|
71
|
+
router_from_mode : res.from_direction,
|
|
72
|
+
router_bridge_mode : res.bridge_direction,
|
|
73
|
+
router_to_mode : res.to_direction
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
}else if(res.bridge_token === 'STX|STSW') {
|
|
77
|
+
return{
|
|
78
|
+
valid : SwapType.ROUTER_STX_STSW,
|
|
79
|
+
router_from_pair : LiquidityPool.parsePoolsFromServerData2(res.from_pair, token_x, Token.getBaseTokens(this.stackswap, BaseToken.STX), res.from_direction),
|
|
80
|
+
router_bridge_pair : LiquidityPool.parsePoolsFromServerData2(res.bridge_pair, Token.getBaseTokens(this.stackswap, BaseToken.STX), Token.getBaseTokens(this.stackswap, BaseToken.STSW), res.bridge_direction),
|
|
81
|
+
router_to_pair : LiquidityPool.parsePoolsFromServerData2(res.to_pair, Token.getBaseTokens(this.stackswap, BaseToken.STSW), token_y, res.to_direction),
|
|
82
|
+
router_from_mode : res.from_direction,
|
|
83
|
+
router_bridge_mode : res.bridge_direction,
|
|
84
|
+
router_to_mode : res.to_direction
|
|
85
|
+
}
|
|
86
|
+
} else{
|
|
87
|
+
return {valid : SwapType.NO_ROUTE}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
return {valid : SwapType.NO_ROUTE}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
26
95
|
async findRouter2(token_x : Token, token_y : Token, current_block: number) : Promise<{ valid: SwapType, [key: string]: any}> {
|
|
27
96
|
const url = this.stackswap.config.STACKS_BACKEND_URL() + '/api/v1/swap_v2/' + token_x.addr +'/'+token_y.addr + '/' + current_block;
|
|
28
97
|
const result = await axios.get ( url,
|
|
@@ -103,6 +172,65 @@ export class SwapManager {
|
|
|
103
172
|
}
|
|
104
173
|
}
|
|
105
174
|
|
|
175
|
+
async routerSwap2ContractCall(token_from_amount: string, token_to_amount: string, token_bridge1_amount: string, token_bridge2_amount: string, pair_from: LiquidityPool, pair_to: LiquidityPool, pair_bridge: LiquidityPool, router_from_mode: boolean, router_to_mode: boolean, router_bridge_mode: boolean, tolerance: number, callback: any = null) {
|
|
176
|
+
|
|
177
|
+
const token_from = router_from_mode ? pair_from.token_x : pair_from.token_y;
|
|
178
|
+
const token_bridge1 = router_from_mode ? pair_from.token_y : pair_from.token_x;
|
|
179
|
+
const token_bridge2 = router_from_mode ? pair_to.token_x : pair_to.token_y;
|
|
180
|
+
const token_to = router_to_mode ? pair_to.token_y: pair_to.token_x;
|
|
181
|
+
|
|
182
|
+
const from_amt = new BigNumber(10 ** token_from.decimal).multipliedBy(token_from_amount).integerValue();
|
|
183
|
+
const bridge1_amt = new BigNumber(10 ** token_bridge1.decimal).multipliedBy(token_bridge1_amount).integerValue();
|
|
184
|
+
const bridge2_amt = new BigNumber(10 ** token_bridge2.decimal).multipliedBy(token_bridge2_amount).integerValue();
|
|
185
|
+
const to_amt = new BigNumber(10 ** token_to.decimal).multipliedBy(token_to_amount).integerValue();
|
|
186
|
+
|
|
187
|
+
const bridge1_min = bridge1_amt.multipliedBy((100 - tolerance)).dividedBy(100).integerValue();
|
|
188
|
+
const bridge1_max = bridge1_amt.multipliedBy((100 + tolerance)).dividedBy(100).integerValue();
|
|
189
|
+
const bridge2_min = bridge2_amt.multipliedBy((100 - tolerance)).dividedBy(100).multipliedBy((100 - tolerance)).dividedBy(100).integerValue();
|
|
190
|
+
const bridge2_max = bridge2_amt.multipliedBy((100 + tolerance)).dividedBy(100).multipliedBy((100 + tolerance)).dividedBy(100).integerValue();
|
|
191
|
+
const to_min = to_amt.multipliedBy((100 - tolerance)).dividedBy(100).multipliedBy((100 - tolerance)).dividedBy(100).multipliedBy((100 - tolerance)).dividedBy(100).integerValue();
|
|
192
|
+
|
|
193
|
+
const token_from_addr = (token_from.addr);
|
|
194
|
+
const token_bridge1_addr = (token_bridge1.addr);
|
|
195
|
+
const token_bridge2_addr = (token_bridge2.addr);
|
|
196
|
+
const token_to_addr = (token_to.addr);
|
|
197
|
+
|
|
198
|
+
const pair_from_addr = pair_from.token_lp.addr;
|
|
199
|
+
const pair_bridge_addr = pair_bridge.token_lp.addr;
|
|
200
|
+
const pair_to_addr = pair_to.token_lp.addr;
|
|
201
|
+
|
|
202
|
+
const from_mode = router_from_mode ? trueCV() : falseCV();
|
|
203
|
+
const bridge_mode = router_bridge_mode ? trueCV() : falseCV();
|
|
204
|
+
const to_mode = router_to_mode ? trueCV() : falseCV();
|
|
205
|
+
|
|
206
|
+
const post_condition = [];
|
|
207
|
+
post_condition.push(await getPostConditionFromAsset(this.stackswap, this.stackswap.getSenderAddress(), token_from.addr, from_amt.toString(), FungibleConditionCode.Equal));
|
|
208
|
+
post_condition.push(await getPostConditionFromAsset(this.stackswap, pair_from_addr, token_bridge1.addr, bridge1_min.toString(), FungibleConditionCode.GreaterEqual));
|
|
209
|
+
post_condition.push(await getPostConditionFromAsset(this.stackswap, this.stackswap.getSenderAddress(), token_bridge1.addr, bridge1_max.toString(), FungibleConditionCode.LessEqual));
|
|
210
|
+
post_condition.push(await getPostConditionFromAsset(this.stackswap, pair_bridge_addr, token_bridge2.addr, bridge2_min.toString(), FungibleConditionCode.GreaterEqual));
|
|
211
|
+
post_condition.push(await getPostConditionFromAsset(this.stackswap, this.stackswap.getSenderAddress(), token_bridge2.addr, bridge2_max.toString(), FungibleConditionCode.LessEqual));
|
|
212
|
+
post_condition.push(await getPostConditionFromAsset(this.stackswap, pair_to_addr, token_to.addr, to_min.toString(), FungibleConditionCode.GreaterEqual));
|
|
213
|
+
|
|
214
|
+
const options = getWriteOptions(this.stackswap, this.stackswap.config.CONTRACT_NAME_STACKSWAP_ROUTER_SWAP2(), 'router-swap', [
|
|
215
|
+
parseAddressToCV(token_from_addr),
|
|
216
|
+
parseAddressToCV(token_bridge1_addr),
|
|
217
|
+
parseAddressToCV(token_bridge2_addr),
|
|
218
|
+
parseAddressToCV(token_to_addr),
|
|
219
|
+
parseAddressToCV(pair_from_addr),
|
|
220
|
+
parseAddressToCV(pair_bridge_addr),
|
|
221
|
+
parseAddressToCV(pair_to_addr),
|
|
222
|
+
from_mode,
|
|
223
|
+
bridge_mode,
|
|
224
|
+
to_mode,
|
|
225
|
+
uintCV(from_amt.toString()),
|
|
226
|
+
uintCV(bridge1_min.toString()),
|
|
227
|
+
uintCV(bridge2_min.toString()),
|
|
228
|
+
uintCV(to_min.toString()),
|
|
229
|
+
this.stackswap.getSenderAddress(),
|
|
230
|
+
], post_condition, callback);
|
|
231
|
+
openContractCall(options);
|
|
232
|
+
}
|
|
233
|
+
|
|
106
234
|
async routerSwapContractCall(token_from_amount: string, token_to_amount: string, token_bridge_amount: string, pair_from: LiquidityPool, pair_to: LiquidityPool, router_from_mode: boolean, router_to_mode: boolean, tolerance: number, callback: any = null) {
|
|
107
235
|
|
|
108
236
|
const token_from = router_from_mode ? pair_from.token_x : pair_from.token_y;
|
|
@@ -114,6 +242,7 @@ export class SwapManager {
|
|
|
114
242
|
const to_amt = new BigNumber(10 ** token_to.decimal).multipliedBy(token_to_amount).integerValue();
|
|
115
243
|
|
|
116
244
|
const bridge_min = bridge_amt.multipliedBy((100 - tolerance)).dividedBy(100).integerValue();
|
|
245
|
+
const bridge_max = bridge_amt.multipliedBy((100 + tolerance)).dividedBy(100).integerValue();
|
|
117
246
|
const to_min = to_amt.multipliedBy((100 - tolerance)).dividedBy(100).multipliedBy((100 - tolerance)).dividedBy(100).integerValue();
|
|
118
247
|
|
|
119
248
|
const token_from_addr = (token_from.addr);
|
|
@@ -129,7 +258,7 @@ export class SwapManager {
|
|
|
129
258
|
const post_condition = [];
|
|
130
259
|
post_condition.push(await getPostConditionFromAsset(this.stackswap, this.stackswap.getSenderAddress(), token_from.addr, from_amt.toString(), FungibleConditionCode.Equal));
|
|
131
260
|
post_condition.push(await getPostConditionFromAsset(this.stackswap, pair_from_addr, token_bridge.addr, bridge_min.toString(), FungibleConditionCode.GreaterEqual));
|
|
132
|
-
post_condition.push(await getPostConditionFromAsset(this.stackswap, this.stackswap.getSenderAddress(), token_bridge.addr,
|
|
261
|
+
post_condition.push(await getPostConditionFromAsset(this.stackswap, this.stackswap.getSenderAddress(), token_bridge.addr, bridge_max.toString(), FungibleConditionCode.LessEqual));
|
|
133
262
|
post_condition.push(await getPostConditionFromAsset(this.stackswap, pair_to_addr, token_to.addr, to_min.toString(), FungibleConditionCode.GreaterEqual));
|
|
134
263
|
|
|
135
264
|
const options = getWriteOptions(this.stackswap, this.stackswap.config.CONTRACT_NAME_STACKSWAP_ROUTER_SWAP(), 'router-swap', [
|