stackswap-front-api-test-02 1.0.104 → 1.0.105
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 +86 -86
- package/dist/esm/index.js +145 -145
- 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 +60 -60
- package/dist/esm/stackswap/manager/farm2.manager.js +403 -403
- 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/esm/stackswap/util.js.map +1 -1
- package/dist/index.d.ts +86 -86
- package/dist/index.js +154 -154
- 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 +60 -60
- package/dist/stackswap/manager/farm2.manager.js +410 -410
- 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/dist/stackswap/util.js.map +1 -1
- package/package.json +43 -43
- package/src/index.ts +207 -207
- package/src/stackswap/config.ts +305 -305
- package/src/stackswap/manager/farm.manager.ts +399 -399
- package/src/stackswap/manager/farm2.manager.ts +463 -463
- package/src/stackswap/manager/governance.manager.ts +95 -95
- package/src/stackswap/manager/launchpad.manager.ts +190 -190
- package/src/stackswap/manager/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,234 +1,234 @@
|
|
|
1
|
-
import { callReadOnlyFunction, contractPrincipalCV, cvToValue, FungibleConditionCode, listCV, standardPrincipalCV, uintCV } from "@stacks/transactions";
|
|
2
|
-
import axios from "axios";
|
|
3
|
-
import { openContractCall } from "@stacks/connect";
|
|
4
|
-
import BigNumber from "bignumber.js";
|
|
5
|
-
import { getPostConditionFromAsset, getReadOptions, getWriteOptions } from "../util";
|
|
6
|
-
export class StakingManager {
|
|
7
|
-
constructor(stackswap) {
|
|
8
|
-
this.stackswap = stackswap;
|
|
9
|
-
}
|
|
10
|
-
async getStackingRoundFirstBlock(cycle) {
|
|
11
|
-
const function_option = getReadOptions(this.stackswap, this.stackswap.config.CONTRACT_NAME_STACKSWAP_STACKING(), 'get-first-stacks-block-in-reward-cycle', [
|
|
12
|
-
uintCV(cycle)
|
|
13
|
-
]);
|
|
14
|
-
const result_raw = await callReadOnlyFunction(function_option);
|
|
15
|
-
const result = cvToValue(result_raw);
|
|
16
|
-
return result;
|
|
17
|
-
}
|
|
18
|
-
async getStakingSTSWBalance(source) {
|
|
19
|
-
const pair_details_options = getReadOptions(source, this.stackswap.config.BASE_STSW_DATA().addr, 'get-balance', [contractPrincipalCV(this.stackswap.config.STACKSWAP_ADDRESS(), this.stackswap.config.BASE_vSTSW_DATA().addr.split('.')[1])]);
|
|
20
|
-
try {
|
|
21
|
-
const result = await callReadOnlyFunction(pair_details_options);
|
|
22
|
-
return cvToValue(result).value;
|
|
23
|
-
}
|
|
24
|
-
catch (e) {
|
|
25
|
-
return 0;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
async getRewardRoundID_V2(source) {
|
|
29
|
-
try {
|
|
30
|
-
const ADDR = this.stackswap.config.STACKSWAP_ADDRESS() + '.' + this.stackswap.config.CONTRACT_NAME_STACKSWAP_STACKING();
|
|
31
|
-
const round = Number(await this.getCurrentStakingRound(ADDR));
|
|
32
|
-
const options = getReadOptions(source, this.stackswap.config.CONTRACT_NAME_STACKSWAP_STACKING(), 'get-staker', [
|
|
33
|
-
standardPrincipalCV(this.stackswap.getSenderAddress()),
|
|
34
|
-
]);
|
|
35
|
-
const result = await callReadOnlyFunction(options);
|
|
36
|
-
const unclaimedList = [];
|
|
37
|
-
for (const unclaimedElement of cvToValue(result).value) {
|
|
38
|
-
if (unclaimedElement.value < round) {
|
|
39
|
-
unclaimedList.push(unclaimedElement.value);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
return unclaimedList;
|
|
43
|
-
}
|
|
44
|
-
catch (e) {
|
|
45
|
-
return [];
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
async getStakingRound(contract_address, block) {
|
|
49
|
-
try {
|
|
50
|
-
const options = getReadOptions(this.stackswap, contract_address, 'get-reward-cycle', [
|
|
51
|
-
uintCV(block),
|
|
52
|
-
]);
|
|
53
|
-
const result = await callReadOnlyFunction(options);
|
|
54
|
-
return cvToValue(result).value;
|
|
55
|
-
}
|
|
56
|
-
catch (e) {
|
|
57
|
-
return 0;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
async getCurrentStakingRound(contract_address) {
|
|
61
|
-
const bh = await this.stackswap.getCurrentBlock();
|
|
62
|
-
return await this.getStakingRound(contract_address, bh);
|
|
63
|
-
}
|
|
64
|
-
async getRewardRoundID(offset) {
|
|
65
|
-
const ADDR = this.stackswap.config.STACKSWAP_ADDRESS() + '.' + this.stackswap.config.CONTRACT_NAME_STACKSWAP_STACKING();
|
|
66
|
-
const get_result = await axios.get(this.stackswap.config.STACKS_API_URL() + '/extended/v1/address/' + this.stackswap.getSenderAddress() + '/transactions', {
|
|
67
|
-
params: {
|
|
68
|
-
limit: 50,
|
|
69
|
-
offset,
|
|
70
|
-
},
|
|
71
|
-
});
|
|
72
|
-
const results = get_result.data.results;
|
|
73
|
-
const reward_id = [];
|
|
74
|
-
for (const item of results) {
|
|
75
|
-
if ('contract_call' in item) {
|
|
76
|
-
if (ADDR === item.contract_call.contract_id &&
|
|
77
|
-
item.contract_call.function_name === 'stake-tokens' &&
|
|
78
|
-
item.tx_status === 'success') {
|
|
79
|
-
const tx_block_number = item.block_height;
|
|
80
|
-
let start_round = await this.getStakingRound(ADDR, tx_block_number);
|
|
81
|
-
start_round = parseInt(start_round) + 1;
|
|
82
|
-
const end_round = start_round + Number(item.contract_call.function_args[1].repr.replace('u', '')) - 1;
|
|
83
|
-
const round_list = this.range_number(start_round, end_round);
|
|
84
|
-
reward_id.push(...round_list);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
return Array.from(new Set(reward_id)).sort((a, b) => { return a - b; });
|
|
89
|
-
}
|
|
90
|
-
range_number(start, end) {
|
|
91
|
-
let ans = [];
|
|
92
|
-
for (let i = start; i <= end; i++) {
|
|
93
|
-
ans.push(i);
|
|
94
|
-
}
|
|
95
|
-
return ans;
|
|
96
|
-
}
|
|
97
|
-
async getReward(contract_addr, round_id) {
|
|
98
|
-
contract_addr = this.stackswap.config.STACKSWAP_ADDRESS() + '.' + this.stackswap.config.CONTRACT_NAME_STACKSWAP_STACKING();
|
|
99
|
-
const function_option = getReadOptions(this.stackswap, contract_addr, 'get-staking-reward-per-user', [
|
|
100
|
-
standardPrincipalCV(this.stackswap.getSenderAddress()),
|
|
101
|
-
uintCV(round_id)
|
|
102
|
-
]);
|
|
103
|
-
const result_raw = await callReadOnlyFunction(function_option);
|
|
104
|
-
const result = cvToValue(result_raw);
|
|
105
|
-
return Number(result);
|
|
106
|
-
}
|
|
107
|
-
async txClaimReward(round_id, stsw_amount, callback = null) {
|
|
108
|
-
const post_condition = [await getPostConditionFromAsset(this.stackswap, this.stackswap.config.STACKSWAP_ADDRESS() + '.' + this.stackswap.config.CONTRACT_NAME_STACKSWAP_STACKING(), this.stackswap.config.BASE_STSW_DATA().addr, stsw_amount, FungibleConditionCode.Equal)];
|
|
109
|
-
const function_option = getWriteOptions(this.stackswap, this.stackswap.config.CONTRACT_NAME_STACKSWAP_STACKING(), 'claim-staking-reward', [uintCV(round_id)], post_condition, callback);
|
|
110
|
-
await openContractCall(function_option);
|
|
111
|
-
}
|
|
112
|
-
async getUserCountFromVSTSW() {
|
|
113
|
-
const address = this.stackswap.config.BASE_vSTSW_DATA().addr.split('.');
|
|
114
|
-
address[0], address[1];
|
|
115
|
-
const function_option = getReadOptions(this.stackswap, this.stackswap.config.BASE_vSTSW_DATA().addr, 'get-user-count', [standardPrincipalCV(this.stackswap.getSenderAddress())]);
|
|
116
|
-
const result_raw = await callReadOnlyFunction(function_option);
|
|
117
|
-
const result = cvToValue(result_raw);
|
|
118
|
-
if (result == null) {
|
|
119
|
-
return 0;
|
|
120
|
-
}
|
|
121
|
-
else {
|
|
122
|
-
return parseInt(result.value.idx.value);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
async getStakingBalanceFromVSTSW_V2() {
|
|
126
|
-
const function_option = getReadOptions(this.stackswap, this.stackswap.config.BASE_vSTSW_DATA().addr, 'get-user-count', [standardPrincipalCV(this.stackswap.getSenderAddress())]);
|
|
127
|
-
const result_raw = await callReadOnlyFunction(function_option);
|
|
128
|
-
const result = cvToValue(result_raw);
|
|
129
|
-
if (result == null) {
|
|
130
|
-
return 0;
|
|
131
|
-
}
|
|
132
|
-
else {
|
|
133
|
-
return new BigNumber(result.value.stakedSTX.value).dividedBy(new BigNumber(10).exponentiatedBy(this.stackswap.config.BASE_vSTSW_DATA().decimal));
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
async getClaimRewardListFromVSTSW() {
|
|
137
|
-
let res_dict = {};
|
|
138
|
-
let id_list = [];
|
|
139
|
-
const count = await this.getUserCountFromVSTSW();
|
|
140
|
-
for (let i = 1; i <= count; i++) {
|
|
141
|
-
const function_option = getReadOptions(this.stackswap, this.stackswap.config.BASE_vSTSW_DATA().addr, 'get-user-info', [
|
|
142
|
-
standardPrincipalCV(this.stackswap.getSenderAddress()),
|
|
143
|
-
uintCV(i)
|
|
144
|
-
]);
|
|
145
|
-
const result_raw = await callReadOnlyFunction(function_option);
|
|
146
|
-
const result = cvToValue(result_raw);
|
|
147
|
-
if (!result.value.returned.value) {
|
|
148
|
-
res_dict[i] = result;
|
|
149
|
-
id_list.push(i);
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
return [id_list, res_dict];
|
|
153
|
-
}
|
|
154
|
-
async getStakingBalanceFromVSTSW() {
|
|
155
|
-
let staking_balance = 0;
|
|
156
|
-
const count = await this.getUserCountFromVSTSW();
|
|
157
|
-
for (let i = 1; i <= count; i++) {
|
|
158
|
-
const function_option = getReadOptions(this.stackswap, this.stackswap.config.BASE_vSTSW_DATA().addr, 'get-user-info', [
|
|
159
|
-
standardPrincipalCV(this.stackswap.getSenderAddress()),
|
|
160
|
-
uintCV(i)
|
|
161
|
-
]);
|
|
162
|
-
const result_raw = await callReadOnlyFunction(function_option);
|
|
163
|
-
const result = cvToValue(result_raw);
|
|
164
|
-
if (result.value.returned.value === false) {
|
|
165
|
-
staking_balance += parseInt(result.value.amountSTSW.value);
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
return staking_balance;
|
|
169
|
-
}
|
|
170
|
-
async txStakeFromStaking(invest_amt, period_month, callback = null) {
|
|
171
|
-
const invest_amt_bn = new BigNumber(10 ** 6).multipliedBy(invest_amt).toFixed(0).toString();
|
|
172
|
-
const postConditions = [];
|
|
173
|
-
postConditions.push(await getPostConditionFromAsset(this.stackswap, this.stackswap.getSenderAddress(), this.stackswap.config.BASE_STSW_DATA().addr, invest_amt_bn, FungibleConditionCode.Equal));
|
|
174
|
-
const function_option = getWriteOptions(this.stackswap, this.stackswap.config.CONTRACT_NAME_STACKSWAP_STACKING(), 'stake-tokens', [
|
|
175
|
-
uintCV(invest_amt_bn), uintCV(period_month)
|
|
176
|
-
], postConditions, callback);
|
|
177
|
-
await openContractCall(function_option);
|
|
178
|
-
}
|
|
179
|
-
async txCooldownFromVSTSW(index, callback = null) {
|
|
180
|
-
const function_option = getWriteOptions(this.stackswap, this.stackswap.config.BASE_vSTSW_DATA().addr, 'unstake-tokens', [uintCV(index)], [], callback);
|
|
181
|
-
await openContractCall(function_option);
|
|
182
|
-
}
|
|
183
|
-
async txUnstakingFromVSTSW(index, amountSTX, amountvSTX, callback = null) {
|
|
184
|
-
const post_conditions = [];
|
|
185
|
-
post_conditions.push(await getPostConditionFromAsset(this.stackswap, this.stackswap.getSenderAddress(), this.stackswap.config.BASE_vSTSW_DATA().addr, amountvSTX, FungibleConditionCode.Equal));
|
|
186
|
-
post_conditions.push(await getPostConditionFromAsset(this.stackswap, this.stackswap.config.BASE_vSTSW_DATA().addr, this.stackswap.config.BASE_STSW_DATA().addr, amountSTX, FungibleConditionCode.Equal));
|
|
187
|
-
const function_option = getWriteOptions(this.stackswap, this.stackswap.config.BASE_vSTSW_DATA().addr, 'reclaim-tokens', [uintCV(index)], post_conditions, callback);
|
|
188
|
-
await openContractCall(function_option);
|
|
189
|
-
}
|
|
190
|
-
async getRewardBalanceFromStaking() {
|
|
191
|
-
const ADDR = this.stackswap.config.STACKSWAP_ADDRESS() + '.' + this.stackswap.config.CONTRACT_NAME_STACKSWAP_STACKING();
|
|
192
|
-
let reward_balance = 0;
|
|
193
|
-
const current_round = await this.getCurrentStakingRound(ADDR);
|
|
194
|
-
for (let i = 0; i <= current_round; i++) {
|
|
195
|
-
const result = await this.getReward(ADDR, i);
|
|
196
|
-
reward_balance += result;
|
|
197
|
-
}
|
|
198
|
-
return reward_balance;
|
|
199
|
-
}
|
|
200
|
-
async getRewardBalanceFromStakingFromList(unclaimed_list) {
|
|
201
|
-
const ADDR = this.stackswap.config.STACKSWAP_ADDRESS() + '.' + this.stackswap.config.CONTRACT_NAME_STACKSWAP_STACKING();
|
|
202
|
-
let reward_balance = 0;
|
|
203
|
-
const current_round = await this.getCurrentStakingRound(ADDR);
|
|
204
|
-
const temp = [];
|
|
205
|
-
for (const unclaimed of unclaimed_list) {
|
|
206
|
-
if (unclaimed < Number(current_round)) {
|
|
207
|
-
temp.push(uintCV(unclaimed));
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
for (let i = 0; i <= current_round; i++) {
|
|
211
|
-
const result = await this.getReward(ADDR, i);
|
|
212
|
-
reward_balance += result;
|
|
213
|
-
}
|
|
214
|
-
return reward_balance;
|
|
215
|
-
}
|
|
216
|
-
async getRewardTotal(reward_list) {
|
|
217
|
-
const contract_addr = this.stackswap.config.STACKSWAP_ADDRESS() + '.' + this.stackswap.config.CONTRACT_NAME_STACKSWAP_STACKING();
|
|
218
|
-
if (reward_list.length === 0) {
|
|
219
|
-
return 0;
|
|
220
|
-
}
|
|
221
|
-
const temp_list = [];
|
|
222
|
-
for (const rewardListElement of reward_list) {
|
|
223
|
-
temp_list.push(uintCV(rewardListElement));
|
|
224
|
-
}
|
|
225
|
-
const function_option = getReadOptions(this.stackswap, contract_addr, 'get-staking-reward-from-list', [
|
|
226
|
-
standardPrincipalCV(this.stackswap.getSenderAddress()),
|
|
227
|
-
listCV(temp_list),
|
|
228
|
-
]);
|
|
229
|
-
const result_raw = await callReadOnlyFunction(function_option);
|
|
230
|
-
const result = cvToValue(result_raw);
|
|
231
|
-
return result.rewardSum.value;
|
|
232
|
-
}
|
|
233
|
-
}
|
|
1
|
+
import { callReadOnlyFunction, contractPrincipalCV, cvToValue, FungibleConditionCode, listCV, standardPrincipalCV, uintCV } from "@stacks/transactions";
|
|
2
|
+
import axios from "axios";
|
|
3
|
+
import { openContractCall } from "@stacks/connect";
|
|
4
|
+
import BigNumber from "bignumber.js";
|
|
5
|
+
import { getPostConditionFromAsset, getReadOptions, getWriteOptions } from "../util";
|
|
6
|
+
export class StakingManager {
|
|
7
|
+
constructor(stackswap) {
|
|
8
|
+
this.stackswap = stackswap;
|
|
9
|
+
}
|
|
10
|
+
async getStackingRoundFirstBlock(cycle) {
|
|
11
|
+
const function_option = getReadOptions(this.stackswap, this.stackswap.config.CONTRACT_NAME_STACKSWAP_STACKING(), 'get-first-stacks-block-in-reward-cycle', [
|
|
12
|
+
uintCV(cycle)
|
|
13
|
+
]);
|
|
14
|
+
const result_raw = await callReadOnlyFunction(function_option);
|
|
15
|
+
const result = cvToValue(result_raw);
|
|
16
|
+
return result;
|
|
17
|
+
}
|
|
18
|
+
async getStakingSTSWBalance(source) {
|
|
19
|
+
const pair_details_options = getReadOptions(source, this.stackswap.config.BASE_STSW_DATA().addr, 'get-balance', [contractPrincipalCV(this.stackswap.config.STACKSWAP_ADDRESS(), this.stackswap.config.BASE_vSTSW_DATA().addr.split('.')[1])]);
|
|
20
|
+
try {
|
|
21
|
+
const result = await callReadOnlyFunction(pair_details_options);
|
|
22
|
+
return cvToValue(result).value;
|
|
23
|
+
}
|
|
24
|
+
catch (e) {
|
|
25
|
+
return 0;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
async getRewardRoundID_V2(source) {
|
|
29
|
+
try {
|
|
30
|
+
const ADDR = this.stackswap.config.STACKSWAP_ADDRESS() + '.' + this.stackswap.config.CONTRACT_NAME_STACKSWAP_STACKING();
|
|
31
|
+
const round = Number(await this.getCurrentStakingRound(ADDR));
|
|
32
|
+
const options = getReadOptions(source, this.stackswap.config.CONTRACT_NAME_STACKSWAP_STACKING(), 'get-staker', [
|
|
33
|
+
standardPrincipalCV(this.stackswap.getSenderAddress()),
|
|
34
|
+
]);
|
|
35
|
+
const result = await callReadOnlyFunction(options);
|
|
36
|
+
const unclaimedList = [];
|
|
37
|
+
for (const unclaimedElement of cvToValue(result).value) {
|
|
38
|
+
if (unclaimedElement.value < round) {
|
|
39
|
+
unclaimedList.push(unclaimedElement.value);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return unclaimedList;
|
|
43
|
+
}
|
|
44
|
+
catch (e) {
|
|
45
|
+
return [];
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
async getStakingRound(contract_address, block) {
|
|
49
|
+
try {
|
|
50
|
+
const options = getReadOptions(this.stackswap, contract_address, 'get-reward-cycle', [
|
|
51
|
+
uintCV(block),
|
|
52
|
+
]);
|
|
53
|
+
const result = await callReadOnlyFunction(options);
|
|
54
|
+
return cvToValue(result).value;
|
|
55
|
+
}
|
|
56
|
+
catch (e) {
|
|
57
|
+
return 0;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
async getCurrentStakingRound(contract_address) {
|
|
61
|
+
const bh = await this.stackswap.getCurrentBlock();
|
|
62
|
+
return await this.getStakingRound(contract_address, bh);
|
|
63
|
+
}
|
|
64
|
+
async getRewardRoundID(offset) {
|
|
65
|
+
const ADDR = this.stackswap.config.STACKSWAP_ADDRESS() + '.' + this.stackswap.config.CONTRACT_NAME_STACKSWAP_STACKING();
|
|
66
|
+
const get_result = await axios.get(this.stackswap.config.STACKS_API_URL() + '/extended/v1/address/' + this.stackswap.getSenderAddress() + '/transactions', {
|
|
67
|
+
params: {
|
|
68
|
+
limit: 50,
|
|
69
|
+
offset,
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
const results = get_result.data.results;
|
|
73
|
+
const reward_id = [];
|
|
74
|
+
for (const item of results) {
|
|
75
|
+
if ('contract_call' in item) {
|
|
76
|
+
if (ADDR === item.contract_call.contract_id &&
|
|
77
|
+
item.contract_call.function_name === 'stake-tokens' &&
|
|
78
|
+
item.tx_status === 'success') {
|
|
79
|
+
const tx_block_number = item.block_height;
|
|
80
|
+
let start_round = await this.getStakingRound(ADDR, tx_block_number);
|
|
81
|
+
start_round = parseInt(start_round) + 1;
|
|
82
|
+
const end_round = start_round + Number(item.contract_call.function_args[1].repr.replace('u', '')) - 1;
|
|
83
|
+
const round_list = this.range_number(start_round, end_round);
|
|
84
|
+
reward_id.push(...round_list);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return Array.from(new Set(reward_id)).sort((a, b) => { return a - b; });
|
|
89
|
+
}
|
|
90
|
+
range_number(start, end) {
|
|
91
|
+
let ans = [];
|
|
92
|
+
for (let i = start; i <= end; i++) {
|
|
93
|
+
ans.push(i);
|
|
94
|
+
}
|
|
95
|
+
return ans;
|
|
96
|
+
}
|
|
97
|
+
async getReward(contract_addr, round_id) {
|
|
98
|
+
contract_addr = this.stackswap.config.STACKSWAP_ADDRESS() + '.' + this.stackswap.config.CONTRACT_NAME_STACKSWAP_STACKING();
|
|
99
|
+
const function_option = getReadOptions(this.stackswap, contract_addr, 'get-staking-reward-per-user', [
|
|
100
|
+
standardPrincipalCV(this.stackswap.getSenderAddress()),
|
|
101
|
+
uintCV(round_id)
|
|
102
|
+
]);
|
|
103
|
+
const result_raw = await callReadOnlyFunction(function_option);
|
|
104
|
+
const result = cvToValue(result_raw);
|
|
105
|
+
return Number(result);
|
|
106
|
+
}
|
|
107
|
+
async txClaimReward(round_id, stsw_amount, callback = null) {
|
|
108
|
+
const post_condition = [await getPostConditionFromAsset(this.stackswap, this.stackswap.config.STACKSWAP_ADDRESS() + '.' + this.stackswap.config.CONTRACT_NAME_STACKSWAP_STACKING(), this.stackswap.config.BASE_STSW_DATA().addr, stsw_amount, FungibleConditionCode.Equal)];
|
|
109
|
+
const function_option = getWriteOptions(this.stackswap, this.stackswap.config.CONTRACT_NAME_STACKSWAP_STACKING(), 'claim-staking-reward', [uintCV(round_id)], post_condition, callback);
|
|
110
|
+
await openContractCall(function_option);
|
|
111
|
+
}
|
|
112
|
+
async getUserCountFromVSTSW() {
|
|
113
|
+
const address = this.stackswap.config.BASE_vSTSW_DATA().addr.split('.');
|
|
114
|
+
address[0], address[1];
|
|
115
|
+
const function_option = getReadOptions(this.stackswap, this.stackswap.config.BASE_vSTSW_DATA().addr, 'get-user-count', [standardPrincipalCV(this.stackswap.getSenderAddress())]);
|
|
116
|
+
const result_raw = await callReadOnlyFunction(function_option);
|
|
117
|
+
const result = cvToValue(result_raw);
|
|
118
|
+
if (result == null) {
|
|
119
|
+
return 0;
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
return parseInt(result.value.idx.value);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
async getStakingBalanceFromVSTSW_V2() {
|
|
126
|
+
const function_option = getReadOptions(this.stackswap, this.stackswap.config.BASE_vSTSW_DATA().addr, 'get-user-count', [standardPrincipalCV(this.stackswap.getSenderAddress())]);
|
|
127
|
+
const result_raw = await callReadOnlyFunction(function_option);
|
|
128
|
+
const result = cvToValue(result_raw);
|
|
129
|
+
if (result == null) {
|
|
130
|
+
return 0;
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
return new BigNumber(result.value.stakedSTX.value).dividedBy(new BigNumber(10).exponentiatedBy(this.stackswap.config.BASE_vSTSW_DATA().decimal));
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
async getClaimRewardListFromVSTSW() {
|
|
137
|
+
let res_dict = {};
|
|
138
|
+
let id_list = [];
|
|
139
|
+
const count = await this.getUserCountFromVSTSW();
|
|
140
|
+
for (let i = 1; i <= count; i++) {
|
|
141
|
+
const function_option = getReadOptions(this.stackswap, this.stackswap.config.BASE_vSTSW_DATA().addr, 'get-user-info', [
|
|
142
|
+
standardPrincipalCV(this.stackswap.getSenderAddress()),
|
|
143
|
+
uintCV(i)
|
|
144
|
+
]);
|
|
145
|
+
const result_raw = await callReadOnlyFunction(function_option);
|
|
146
|
+
const result = cvToValue(result_raw);
|
|
147
|
+
if (!result.value.returned.value) {
|
|
148
|
+
res_dict[i] = result;
|
|
149
|
+
id_list.push(i);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return [id_list, res_dict];
|
|
153
|
+
}
|
|
154
|
+
async getStakingBalanceFromVSTSW() {
|
|
155
|
+
let staking_balance = 0;
|
|
156
|
+
const count = await this.getUserCountFromVSTSW();
|
|
157
|
+
for (let i = 1; i <= count; i++) {
|
|
158
|
+
const function_option = getReadOptions(this.stackswap, this.stackswap.config.BASE_vSTSW_DATA().addr, 'get-user-info', [
|
|
159
|
+
standardPrincipalCV(this.stackswap.getSenderAddress()),
|
|
160
|
+
uintCV(i)
|
|
161
|
+
]);
|
|
162
|
+
const result_raw = await callReadOnlyFunction(function_option);
|
|
163
|
+
const result = cvToValue(result_raw);
|
|
164
|
+
if (result.value.returned.value === false) {
|
|
165
|
+
staking_balance += parseInt(result.value.amountSTSW.value);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return staking_balance;
|
|
169
|
+
}
|
|
170
|
+
async txStakeFromStaking(invest_amt, period_month, callback = null) {
|
|
171
|
+
const invest_amt_bn = new BigNumber(10 ** 6).multipliedBy(invest_amt).toFixed(0).toString();
|
|
172
|
+
const postConditions = [];
|
|
173
|
+
postConditions.push(await getPostConditionFromAsset(this.stackswap, this.stackswap.getSenderAddress(), this.stackswap.config.BASE_STSW_DATA().addr, invest_amt_bn, FungibleConditionCode.Equal));
|
|
174
|
+
const function_option = getWriteOptions(this.stackswap, this.stackswap.config.CONTRACT_NAME_STACKSWAP_STACKING(), 'stake-tokens', [
|
|
175
|
+
uintCV(invest_amt_bn), uintCV(period_month)
|
|
176
|
+
], postConditions, callback);
|
|
177
|
+
await openContractCall(function_option);
|
|
178
|
+
}
|
|
179
|
+
async txCooldownFromVSTSW(index, callback = null) {
|
|
180
|
+
const function_option = getWriteOptions(this.stackswap, this.stackswap.config.BASE_vSTSW_DATA().addr, 'unstake-tokens', [uintCV(index)], [], callback);
|
|
181
|
+
await openContractCall(function_option);
|
|
182
|
+
}
|
|
183
|
+
async txUnstakingFromVSTSW(index, amountSTX, amountvSTX, callback = null) {
|
|
184
|
+
const post_conditions = [];
|
|
185
|
+
post_conditions.push(await getPostConditionFromAsset(this.stackswap, this.stackswap.getSenderAddress(), this.stackswap.config.BASE_vSTSW_DATA().addr, amountvSTX, FungibleConditionCode.Equal));
|
|
186
|
+
post_conditions.push(await getPostConditionFromAsset(this.stackswap, this.stackswap.config.BASE_vSTSW_DATA().addr, this.stackswap.config.BASE_STSW_DATA().addr, amountSTX, FungibleConditionCode.Equal));
|
|
187
|
+
const function_option = getWriteOptions(this.stackswap, this.stackswap.config.BASE_vSTSW_DATA().addr, 'reclaim-tokens', [uintCV(index)], post_conditions, callback);
|
|
188
|
+
await openContractCall(function_option);
|
|
189
|
+
}
|
|
190
|
+
async getRewardBalanceFromStaking() {
|
|
191
|
+
const ADDR = this.stackswap.config.STACKSWAP_ADDRESS() + '.' + this.stackswap.config.CONTRACT_NAME_STACKSWAP_STACKING();
|
|
192
|
+
let reward_balance = 0;
|
|
193
|
+
const current_round = await this.getCurrentStakingRound(ADDR);
|
|
194
|
+
for (let i = 0; i <= current_round; i++) {
|
|
195
|
+
const result = await this.getReward(ADDR, i);
|
|
196
|
+
reward_balance += result;
|
|
197
|
+
}
|
|
198
|
+
return reward_balance;
|
|
199
|
+
}
|
|
200
|
+
async getRewardBalanceFromStakingFromList(unclaimed_list) {
|
|
201
|
+
const ADDR = this.stackswap.config.STACKSWAP_ADDRESS() + '.' + this.stackswap.config.CONTRACT_NAME_STACKSWAP_STACKING();
|
|
202
|
+
let reward_balance = 0;
|
|
203
|
+
const current_round = await this.getCurrentStakingRound(ADDR);
|
|
204
|
+
const temp = [];
|
|
205
|
+
for (const unclaimed of unclaimed_list) {
|
|
206
|
+
if (unclaimed < Number(current_round)) {
|
|
207
|
+
temp.push(uintCV(unclaimed));
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
for (let i = 0; i <= current_round; i++) {
|
|
211
|
+
const result = await this.getReward(ADDR, i);
|
|
212
|
+
reward_balance += result;
|
|
213
|
+
}
|
|
214
|
+
return reward_balance;
|
|
215
|
+
}
|
|
216
|
+
async getRewardTotal(reward_list) {
|
|
217
|
+
const contract_addr = this.stackswap.config.STACKSWAP_ADDRESS() + '.' + this.stackswap.config.CONTRACT_NAME_STACKSWAP_STACKING();
|
|
218
|
+
if (reward_list.length === 0) {
|
|
219
|
+
return 0;
|
|
220
|
+
}
|
|
221
|
+
const temp_list = [];
|
|
222
|
+
for (const rewardListElement of reward_list) {
|
|
223
|
+
temp_list.push(uintCV(rewardListElement));
|
|
224
|
+
}
|
|
225
|
+
const function_option = getReadOptions(this.stackswap, contract_addr, 'get-staking-reward-from-list', [
|
|
226
|
+
standardPrincipalCV(this.stackswap.getSenderAddress()),
|
|
227
|
+
listCV(temp_list),
|
|
228
|
+
]);
|
|
229
|
+
const result_raw = await callReadOnlyFunction(function_option);
|
|
230
|
+
const result = cvToValue(result_raw);
|
|
231
|
+
return result.rewardSum.value;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
234
|
//# sourceMappingURL=staking.manager.js.map
|
|
@@ -1,39 +1,39 @@
|
|
|
1
|
-
import { StackswapAPI } from "../../index";
|
|
2
|
-
import { LiquidityPool } from "./pool.manager";
|
|
3
|
-
import { Token } from "./token.manager";
|
|
4
|
-
export declare enum SwapType {
|
|
5
|
-
NO_ROUTE = 0,
|
|
6
|
-
ON_LOADING = 1,
|
|
7
|
-
SINGLE = 2,
|
|
8
|
-
ROUTER_STX = 10,
|
|
9
|
-
ROUTER_STSW = 11
|
|
10
|
-
}
|
|
11
|
-
export declare class SwapManager {
|
|
12
|
-
stackswap: StackswapAPI;
|
|
13
|
-
constructor(stackswap: StackswapAPI);
|
|
14
|
-
findRouter2(token_x: Token, token_y: Token, current_block: number): Promise<{
|
|
15
|
-
valid: SwapType;
|
|
16
|
-
[key: string]: any;
|
|
17
|
-
}>;
|
|
18
|
-
findRouter(token_x: Token, token_y: Token, current_block: number): Promise<{
|
|
19
|
-
valid: SwapType;
|
|
20
|
-
[key: string]: any;
|
|
21
|
-
}>;
|
|
22
|
-
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): Promise<void>;
|
|
23
|
-
swapContractCall(token_x_amount: string, token_y_amount: string, pair: LiquidityPool, x_to_y: boolean, tolerance: number, callback?: any): Promise<void>;
|
|
24
|
-
importContractCall(pair: LiquidityPool, x_to_y: boolean, amount_x: string, amount_y: string, callback?: any): Promise<void>;
|
|
25
|
-
removeContractCall(pair: LiquidityPool, percent: number, user_lp_total: string, dx: any, dy: any, swap_contract?: string, callback?: any): Promise<void>;
|
|
26
|
-
createContractCall(token_x: Token, token_y: Token, token_x_amount: string, token_y_amount: string, liquidity_token_addr: any, callback?: any): Promise<void>;
|
|
27
|
-
price_impact(x_to_y: boolean, pair: LiquidityPool, token_x_amount: string): string | 0 | 100;
|
|
28
|
-
price_impact_router(router_from_mode: boolean, router_to_mode: boolean, pair_from: LiquidityPool, pair_to: LiquidityPool, token_from_amount: string): string | 0 | 100;
|
|
29
|
-
getPriceString(pair: LiquidityPool | null, token_x: Token | null, token_y: Token | null, price: number, price_invert: boolean): string;
|
|
30
|
-
getPricesWithSwapData(swap_data: {
|
|
31
|
-
valid: SwapType;
|
|
32
|
-
[key: string]: any;
|
|
33
|
-
}, token_x_amount: string, price_invert: boolean): {
|
|
34
|
-
price: number;
|
|
35
|
-
dy: string;
|
|
36
|
-
price_strings: string;
|
|
37
|
-
[key: string]: any;
|
|
38
|
-
};
|
|
39
|
-
}
|
|
1
|
+
import { StackswapAPI } from "../../index";
|
|
2
|
+
import { LiquidityPool } from "./pool.manager";
|
|
3
|
+
import { Token } from "./token.manager";
|
|
4
|
+
export declare enum SwapType {
|
|
5
|
+
NO_ROUTE = 0,
|
|
6
|
+
ON_LOADING = 1,
|
|
7
|
+
SINGLE = 2,
|
|
8
|
+
ROUTER_STX = 10,
|
|
9
|
+
ROUTER_STSW = 11
|
|
10
|
+
}
|
|
11
|
+
export declare class SwapManager {
|
|
12
|
+
stackswap: StackswapAPI;
|
|
13
|
+
constructor(stackswap: StackswapAPI);
|
|
14
|
+
findRouter2(token_x: Token, token_y: Token, current_block: number): Promise<{
|
|
15
|
+
valid: SwapType;
|
|
16
|
+
[key: string]: any;
|
|
17
|
+
}>;
|
|
18
|
+
findRouter(token_x: Token, token_y: Token, current_block: number): Promise<{
|
|
19
|
+
valid: SwapType;
|
|
20
|
+
[key: string]: any;
|
|
21
|
+
}>;
|
|
22
|
+
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): Promise<void>;
|
|
23
|
+
swapContractCall(token_x_amount: string, token_y_amount: string, pair: LiquidityPool, x_to_y: boolean, tolerance: number, callback?: any): Promise<void>;
|
|
24
|
+
importContractCall(pair: LiquidityPool, x_to_y: boolean, amount_x: string, amount_y: string, callback?: any): Promise<void>;
|
|
25
|
+
removeContractCall(pair: LiquidityPool, percent: number, user_lp_total: string, dx: any, dy: any, swap_contract?: string, callback?: any): Promise<void>;
|
|
26
|
+
createContractCall(token_x: Token, token_y: Token, token_x_amount: string, token_y_amount: string, liquidity_token_addr: any, callback?: any): Promise<void>;
|
|
27
|
+
price_impact(x_to_y: boolean, pair: LiquidityPool, token_x_amount: string): string | 0 | 100;
|
|
28
|
+
price_impact_router(router_from_mode: boolean, router_to_mode: boolean, pair_from: LiquidityPool, pair_to: LiquidityPool, token_from_amount: string): string | 0 | 100;
|
|
29
|
+
getPriceString(pair: LiquidityPool | null, token_x: Token | null, token_y: Token | null, price: number, price_invert: boolean): string;
|
|
30
|
+
getPricesWithSwapData(swap_data: {
|
|
31
|
+
valid: SwapType;
|
|
32
|
+
[key: string]: any;
|
|
33
|
+
}, token_x_amount: string, price_invert: boolean): {
|
|
34
|
+
price: number;
|
|
35
|
+
dy: string;
|
|
36
|
+
price_strings: string;
|
|
37
|
+
[key: string]: any;
|
|
38
|
+
};
|
|
39
|
+
}
|