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