stackswap-front-api-test-02 1.0.39 → 1.0.47

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.
Files changed (35) hide show
  1. package/dist/esm/index.d.ts +10 -4
  2. package/dist/esm/index.js +38 -18
  3. package/dist/esm/index.js.map +1 -1
  4. package/dist/esm/stackswap/config.d.ts +29 -3
  5. package/dist/esm/stackswap/config.js +62 -3
  6. package/dist/esm/stackswap/config.js.map +1 -1
  7. package/dist/esm/stackswap/manager/farm2.manager.d.ts +43 -20
  8. package/dist/esm/stackswap/manager/farm2.manager.js +171 -62
  9. package/dist/esm/stackswap/manager/farm2.manager.js.map +1 -1
  10. package/dist/esm/stackswap/manager/token.manager.js +10 -2
  11. package/dist/esm/stackswap/manager/token.manager.js.map +1 -1
  12. package/dist/esm/stackswap/util.d.ts +0 -2
  13. package/dist/esm/stackswap/util.js +3 -41
  14. package/dist/esm/stackswap/util.js.map +1 -1
  15. package/dist/index.d.ts +10 -4
  16. package/dist/index.js +40 -18
  17. package/dist/index.js.map +1 -1
  18. package/dist/index.umd.js +1 -1
  19. package/dist/stackswap/config.d.ts +29 -3
  20. package/dist/stackswap/config.js +64 -4
  21. package/dist/stackswap/config.js.map +1 -1
  22. package/dist/stackswap/manager/farm2.manager.d.ts +43 -20
  23. package/dist/stackswap/manager/farm2.manager.js +171 -62
  24. package/dist/stackswap/manager/farm2.manager.js.map +1 -1
  25. package/dist/stackswap/manager/token.manager.js +10 -2
  26. package/dist/stackswap/manager/token.manager.js.map +1 -1
  27. package/dist/stackswap/util.d.ts +0 -2
  28. package/dist/stackswap/util.js +4 -44
  29. package/dist/stackswap/util.js.map +1 -1
  30. package/package.json +1 -1
  31. package/src/index.ts +43 -18
  32. package/src/stackswap/config.ts +79 -5
  33. package/src/stackswap/manager/farm2.manager.ts +201 -127
  34. package/src/stackswap/manager/token.manager.ts +10 -2
  35. package/src/stackswap/util.ts +6 -38
@@ -8,20 +8,24 @@ import {
8
8
  } from '@stacks/transactions';
9
9
  import BigNumber from 'bignumber.js';
10
10
  import {openContractCall} from '@stacks/connect';
11
- import {getPostConditionFromAsset, getReadOptions, getWriteOptions, num2decimal, numFormat_} from "../util";
11
+ import {getPostConditionFromAsset, getReadOptions, getWriteOptions, num2decimal} from "../util";
12
12
  import {StackswapAPI} from "../../index";
13
13
 
14
+ export enum Mode {
15
+ WITH_SERVER,
16
+ ONLY_BLOCKCHAIN
17
+ }
14
18
 
15
19
  export class Farm2Manager {
16
20
  stackswap: StackswapAPI;
17
21
 
18
- constructor(stackswap: StackswapAPI) {
22
+ constructor(stackswap: StackswapAPI) {``
19
23
  this.stackswap = stackswap;
20
24
  }
21
-
22
- async getFarmingRound( bh: number) {
25
+
26
+ async getFarmingRound2(bh: number, farming_contract: string) {
23
27
  try {
24
- const function_option = getReadOptions(this.stackswap, this.stackswap.config.CONTRACT_NAME_STACKSWAP_FARMING(), 'getRewardRound', [
28
+ const function_option = getReadOptions(this.stackswap, farming_contract, 'getRewardRound', [
25
29
  uintCV(bh)
26
30
  ]);
27
31
  const result_raw = await callReadOnlyFunction(function_option);
@@ -33,9 +37,8 @@ export class Farm2Manager {
33
37
  }
34
38
  }
35
39
 
36
-
37
- async getFarmingRoundFirstBlock( cycle: number) {
38
- const function_option = getReadOptions(this.stackswap, this.stackswap.config.CONTRACT_NAME_STACKSWAP_FARMING(), 'getFirstBlockOfRound', [
40
+ async getFarmingRoundFirstBlock2(cycle: number, farming_contract: string) {
41
+ const function_option = getReadOptions(this.stackswap, farming_contract, 'getFirstBlockOfRound', [
39
42
  uintCV(cycle)
40
43
  ]);
41
44
  const result_raw = await callReadOnlyFunction(function_option);
@@ -44,10 +47,9 @@ export class Farm2Manager {
44
47
  return result;
45
48
  }
46
49
 
47
-
48
- async isUserMigratable( pool_address: string) {
50
+ async isUserMigratable2(pool_address: string, farming_contract: string) {
49
51
  const address = pool_address.split('.');
50
- const function_option = getReadOptions(this.stackswap, this.stackswap.config.CONTRACT_NAME_STACKSWAP_FARMING(), 'isUserMigratable', [
52
+ const function_option = getReadOptions(this.stackswap, farming_contract, 'isUserMigratable', [
51
53
  contractPrincipalCV(address[0], address[1]),
52
54
  standardPrincipalCV(this.stackswap.getSenderAddress()),
53
55
  ]);
@@ -61,29 +63,29 @@ export class Farm2Manager {
61
63
  }
62
64
  }
63
65
 
64
- getPoolUSDPriceFarm(farm: any, farm_total: any, oracle_stx: any, oracle_stsw: any, oracle_lbtc: any) {
66
+ getPoolUSDPriceFarm(farm: any, farm_total: any, oracle_data: { oracle_stx: any, oracle_stsw: any, oracle_lbtc: any }) {
65
67
  // BigNumber.config({ DECIMAL_PLACES: 6});
66
68
  let tvl = 0;
67
69
  switch (farm.BASE) {
68
70
  case 'STX':
69
71
  if (farm.token_x.symbol === farm.BASE) {
70
- tvl = (new BigNumber(farm.pair['balance-x'].value).multipliedBy(farm_total).dividedBy(farm.pair['shares-total'].value).dividedBy(new BigNumber(10 ** 12))).multipliedBy(2).multipliedBy(new BigNumber(oracle_stx)).toNumber();
72
+ tvl = (new BigNumber(farm.pair['balance-x'].value).multipliedBy(farm_total).dividedBy(farm.pair['shares-total'].value).dividedBy(new BigNumber(10 ** 12))).multipliedBy(2).multipliedBy(new BigNumber(oracle_data.oracle_stx)).toNumber();
71
73
  } else {
72
- tvl = (new BigNumber(farm.pair['balance-y'].value).multipliedBy(farm_total).dividedBy(farm.pair['shares-total'].value).dividedBy(new BigNumber(10 ** 12))).multipliedBy(2).multipliedBy(new BigNumber(oracle_stx)).toNumber();
74
+ tvl = (new BigNumber(farm.pair['balance-y'].value).multipliedBy(farm_total).dividedBy(farm.pair['shares-total'].value).dividedBy(new BigNumber(10 ** 12))).multipliedBy(2).multipliedBy(new BigNumber(oracle_data.oracle_stx)).toNumber();
73
75
  }
74
76
  break;
75
77
  case 'STSW':
76
78
  if (farm.token_x.symbol === farm.BASE) {
77
- tvl = (new BigNumber(farm.pair['balance-x'].value).multipliedBy(farm_total).dividedBy(farm.pair['shares-total'].value).dividedBy(new BigNumber(10 ** 12))).multipliedBy(2).multipliedBy(new BigNumber(oracle_stsw)).toNumber();
79
+ tvl = (new BigNumber(farm.pair['balance-x'].value).multipliedBy(farm_total).dividedBy(farm.pair['shares-total'].value).dividedBy(new BigNumber(10 ** 12))).multipliedBy(2).multipliedBy(new BigNumber(oracle_data.oracle_stsw)).toNumber();
78
80
  } else {
79
- tvl = (new BigNumber(farm.pair['balance-y'].value).multipliedBy(farm_total).dividedBy(farm.pair['shares-total'].value).dividedBy(new BigNumber(10 ** 12))).multipliedBy(2).multipliedBy(new BigNumber(oracle_stsw)).toNumber();
81
+ tvl = (new BigNumber(farm.pair['balance-y'].value).multipliedBy(farm_total).dividedBy(farm.pair['shares-total'].value).dividedBy(new BigNumber(10 ** 12))).multipliedBy(2).multipliedBy(new BigNumber(oracle_data.oracle_stsw)).toNumber();
80
82
  }
81
83
  break;
82
84
  case 'lBTC':
83
85
  if (farm.token_x.symbol === farm.BASE) {
84
- tvl = (new BigNumber(farm.pair['balance-x'].value).multipliedBy(farm_total).dividedBy(farm.pair['shares-total'].value).dividedBy(new BigNumber(10 ** 14))).multipliedBy(2).multipliedBy(new BigNumber(oracle_lbtc)).toNumber();
86
+ tvl = (new BigNumber(farm.pair['balance-x'].value).multipliedBy(farm_total).dividedBy(farm.pair['shares-total'].value).dividedBy(new BigNumber(10 ** 14))).multipliedBy(2).multipliedBy(new BigNumber(oracle_data.oracle_lbtc)).toNumber();
85
87
  } else {
86
- tvl = (new BigNumber(farm.pair['balance-y'].value).multipliedBy(farm_total).dividedBy(farm.pair['shares-total'].value).dividedBy(new BigNumber(10 ** 14))).multipliedBy(2).multipliedBy(new BigNumber(oracle_lbtc)).toNumber();
88
+ tvl = (new BigNumber(farm.pair['balance-y'].value).multipliedBy(farm_total).dividedBy(farm.pair['shares-total'].value).dividedBy(new BigNumber(10 ** 14))).multipliedBy(2).multipliedBy(new BigNumber(oracle_data.oracle_lbtc)).toNumber();
87
89
  }
88
90
  break;
89
91
  default:
@@ -92,35 +94,35 @@ export class Farm2Manager {
92
94
  return tvl;
93
95
  }
94
96
 
95
-
96
- async getFarming(pair: any, token_x: any, token_y: any, farm_total: any) {
97
- // BigNumber.config({ DECIMAL_PLACES: 6});
98
- const x_token_addr = token_x.addr;
99
- // console.log(token_x.addr);
100
- const y_token_addr = token_y.addr;
101
- // console.log(token_y.addr);
102
- let total_balacne = new BigNumber(-1);
103
- if (x_token_addr === this.stackswap.config.BASE_WSTX_DATA().addr) {
104
- total_balacne = (new BigNumber(pair['balance-x'].value).multipliedBy(farm_total).dividedBy(pair['shares-total'].value).dividedBy(new BigNumber(10 ** token_x.decimal))).multipliedBy(2);
105
- } else if (y_token_addr === this.stackswap.config.BASE_WSTX_DATA().addr) {
106
- total_balacne = (new BigNumber(pair['balance-y'].value).multipliedBy(farm_total).dividedBy(pair['shares-total'].value).dividedBy(new BigNumber(10 ** token_y.decimal))).multipliedBy(2);
107
- }
108
- // console.log('pool_stx_price', token_x.addr, token_y.addr,token_x.balance, token_y.balance,total_balacne.toNumber());
109
- // console.log(pair["balance-x"].value, pair["balance-y"].value, total_balacne.toNumber());
110
- return total_balacne;
111
- }
112
-
113
-
114
- async getFarmingStakerStatus( pool_address: string, cur_round: number) {
97
+ //
98
+ // async getFarming(pair: any ,token_x: any, token_y: any, farm_total: any){
99
+ // // BigNumber.config({ DECIMAL_PLACES: 6});
100
+ // const x_token_addr = token_x.addr;
101
+ // // console.log(token_x.addr);
102
+ // const y_token_addr = token_y.addr;
103
+ // // console.log(token_y.addr);
104
+ // let total_balacne = new BigNumber(-1);
105
+ // if(x_token_addr === this.stackswap.config.BASE_WSTX_DATA().addr){
106
+ // total_balacne = (new BigNumber(pair['balance-x'].value).multipliedBy(farm_total).dividedBy(pair['shares-total'].value).dividedBy(new BigNumber(10 ** token_x.decimal))).multipliedBy(2);
107
+ // }else if(y_token_addr === this.stackswap.config.BASE_WSTX_DATA().addr){
108
+ // total_balacne = (new BigNumber(pair['balance-y'].value).multipliedBy(farm_total).dividedBy(pair['shares-total'].value).dividedBy(new BigNumber(10 ** token_y.decimal))).multipliedBy(2);
109
+ // }
110
+ // // console.log('pool_stx_price', token_x.addr, token_y.addr,token_x.balance, token_y.balance,total_balacne.toNumber());
111
+ // // console.log(pair["balance-x"].value, pair["balance-y"].value, total_balacne.toNumber());
112
+ // return total_balacne;
113
+ // }
114
+
115
+ async getFarmingStakerStatus2(pool_address: string, cur_round: number, farming_contract: string) {
115
116
  const address = pool_address.split('.');
116
- const function_option = getReadOptions(this.stackswap, this.stackswap.config.CONTRACT_NAME_STACKSWAP_FARMING(), 'getLPUserData', [
117
+ console.log('getFarmingStakerStatus', farming_contract);
118
+ const function_option = getReadOptions(this.stackswap, farming_contract, 'getLPUserData', [
117
119
  contractPrincipalCV(address[0], address[1]),
118
120
  standardPrincipalCV(this.stackswap.getSenderAddress()),
119
121
  ]);
120
122
  const result_raw = await callReadOnlyFunction(function_option);
121
123
  const result = cvToValue(result_raw);
122
124
  try {
123
- // console.log('getFarmingStakerStatus', result);
125
+ console.log('getFarmingStakerStatus', result);
124
126
  const pooledToken = result.value.amountLP.value;
125
127
  const unclaimedList = [];
126
128
  const stakedList = []
@@ -134,15 +136,14 @@ export class Farm2Manager {
134
136
  // console.log({pooledToken, unclaimedList, stakedList});
135
137
  return {pooledToken, unclaimedList, stakedList};
136
138
  } catch (e) {
137
- // console.log(e);
139
+ console.log(e);
138
140
  return {pooledToken: 0, unclaimedList: [], stakedList: []}
139
141
  }
140
142
  }
141
143
 
142
-
143
- async getFarmingTotalStatusCycle( cycle: number) {
144
+ async getFarmingTotalStatusCycle2(cycle: number, farming_contract: string) {
144
145
  // console.log(cycle);
145
- const function_option = getReadOptions(this.stackswap, this.stackswap.config.CONTRACT_NAME_STACKSWAP_FARMING(), 'getTotalRoundDataOrDefault', [
146
+ const function_option = getReadOptions(this.stackswap, farming_contract, 'getTotalRoundDataOrDefault', [
146
147
  uintCV(cycle),
147
148
  ]);
148
149
  const result_raw = await callReadOnlyFunction(function_option);
@@ -162,31 +163,9 @@ export class Farm2Manager {
162
163
  }
163
164
  }
164
165
 
165
-
166
- /**
167
- *
168
- * @param this.stackswap
169
- * @param pool_address
170
- * @param cycle
171
- *
172
- * {
173
- * "type": "(tuple (amountLP uint) (weight uint))",
174
- * "value": {
175
- * "amountLP": {
176
- * "type": "uint",
177
- * "value": "122000000"
178
- * },
179
- * "weight": {
180
- * "type": "uint",
181
- * "value": "1"
182
- * }
183
- * }
184
- * }
185
- */
186
-
187
- async getFarmingTotalPoolStatus( pool_address: string) {
166
+ async getFarmingTotalPoolStatus2(pool_address: string, farming_contract: string) {
188
167
  const address = pool_address.split('.');
189
- const function_option = getReadOptions(this.stackswap, this.stackswap.config.CONTRACT_NAME_STACKSWAP_FARMING(), 'getLPTotalData', [
168
+ const function_option = getReadOptions(this.stackswap, farming_contract, 'getLPTotalData', [
190
169
  contractPrincipalCV(address[0], address[1]),
191
170
  ]);
192
171
  const result_raw = await callReadOnlyFunction(function_option);
@@ -200,23 +179,9 @@ export class Farm2Manager {
200
179
  }
201
180
  }
202
181
 
203
- /**
204
- *
205
- * @param this.stackswap
206
- * @param pool_address
207
- * @param cycle
208
- *
209
- * {
210
- * value: u0,
211
- * weight: u0,
212
- * amountLP: u0,
213
- * price: u0
214
- * }
215
- */
216
-
217
- async getFarmingTotalPoolStatusCycle( pool_address: string, cycle: number) {
182
+ async getFarmingTotalPoolStatusCycle2(pool_address: string, cycle: number, farming_contract: string) {
218
183
  const address = pool_address.split('.');
219
- const function_option = getReadOptions(this.stackswap, this.stackswap.config.CONTRACT_NAME_STACKSWAP_FARMING(), 'getLPRoundData', [
184
+ const function_option = getReadOptions(this.stackswap, farming_contract, 'getLPRoundData', [
220
185
  contractPrincipalCV(address[0], address[1]),
221
186
  uintCV(cycle),
222
187
  ]);
@@ -230,11 +195,10 @@ export class Farm2Manager {
230
195
  }
231
196
  }
232
197
 
233
-
234
- async getFarmingTotalValue( pool_address: string) {
198
+ async getFarmingTotalValue2(pool_address: string, farming_contract: string) {
235
199
  try {
236
200
  const address = pool_address.split('.');
237
- const function_option = getReadOptions(this.stackswap, this.stackswap.config.CONTRACT_NAME_STACKSWAP_FARMING(), 'getLPTotalData', [
201
+ const function_option = getReadOptions(this.stackswap, farming_contract, 'getLPTotalData', [
238
202
  contractPrincipalCV(address[0], address[1]),
239
203
  ]);
240
204
  const result_raw = await callReadOnlyFunction(function_option);
@@ -246,11 +210,9 @@ export class Farm2Manager {
246
210
  }
247
211
  }
248
212
 
249
-
250
-
251
- async getFarmingUserRewardPerCycle( pool_address: string, cycle: number) {
213
+ async getFarmingUserRewardPerCycle2(pool_address: string, cycle: number, farming_contract: string) {
252
214
  const address = pool_address.split('.');
253
- const function_option = getReadOptions(this.stackswap, this.stackswap.config.CONTRACT_NAME_STACKSWAP_FARMING(), 'getFarmingReward', [
215
+ const function_option = getReadOptions(this.stackswap, farming_contract, 'getFarmingReward', [
254
216
  contractPrincipalCV(address[0], address[1]),
255
217
  standardPrincipalCV(this.stackswap.getSenderAddress()),
256
218
  uintCV(cycle),
@@ -261,11 +223,9 @@ export class Farm2Manager {
261
223
  return result;
262
224
  }
263
225
 
264
-
265
-
266
- async getFarmingUserRewardList( pool_address: string, unclaimed_list: any) {
226
+ async getFarmingUserRewardList2(pool_address: string, unclaimed_list: any, farming_contract: string) {
267
227
  const address = pool_address.split('.');
268
- const function_option = getReadOptions(this.stackswap, this.stackswap.config.CONTRACT_NAME_STACKSWAP_FARMING(), 'getFarmingRewardFromList', [
228
+ const function_option = getReadOptions(this.stackswap, farming_contract, 'getFarmingRewardFromList', [
269
229
  standardPrincipalCV(this.stackswap.getSenderAddress()),
270
230
  contractPrincipalCV(address[0], address[1]),
271
231
  listCV(unclaimed_list),
@@ -277,9 +237,9 @@ export class Farm2Manager {
277
237
  }
278
238
 
279
239
 
280
- async getFarmingRewardInfo( pool_address: string, cycle: number) {
240
+ async getFarmingRewardInfo2(pool_address: string, cycle: number, farming_contract: string) {
281
241
  const address = pool_address.split('.');
282
- const function_option = getReadOptions(this.stackswap, this.stackswap.config.CONTRACT_NAME_STACKSWAP_FARMING(), 'getLPUserRoundData', [
242
+ const function_option = getReadOptions(this.stackswap, farming_contract, 'getLPUserRoundData', [
283
243
  contractPrincipalCV(address[0], address[1]),
284
244
  standardPrincipalCV(this.stackswap.getSenderAddress()),
285
245
  uintCV(cycle),
@@ -290,22 +250,20 @@ export class Farm2Manager {
290
250
  return result.value.returnLP.value;
291
251
  }
292
252
 
293
-
294
- async migrateToVersion2( pool_address: string, callback: any = null) {
253
+ async migrateToVersion2(pool_address: string, farming_contract: string, callback: any) {
295
254
  const address = pool_address.split('.');
296
255
  // console.log(amountTokens);
297
- const function_option = getWriteOptions(this.stackswap, this.stackswap.config.CONTRACT_NAME_STACKSWAP_FARMING(), 'migrateFromVersion1', [
256
+ const function_option = getWriteOptions(this.stackswap, farming_contract, 'migrateFromVersion1', [
298
257
  contractPrincipalCV(address[0], address[1]),
299
258
  ], [], callback);
300
259
  await openContractCall(function_option);
301
260
  }
302
261
 
303
-
304
- async stakeFarmingContract( pool_address: string, amountTokens: string, lockPeriod: number, callback: any = null) {
262
+ async stakeFarmingContract2(pool_address: string, amountTokens: string, lockPeriod: number, callback: any, farming_contract: string) {
305
263
  const address = pool_address.split('.');
306
264
  // console.log(amountTokens);
307
265
  const amount = new BigNumber(amountTokens).multipliedBy(10 ** 6).toFixed(0).toString();
308
- const function_option = getWriteOptions(this.stackswap, this.stackswap.config.CONTRACT_NAME_STACKSWAP_FARMING(), 'stakeTokens', [
266
+ const function_option = getWriteOptions(this.stackswap, farming_contract, 'stakeTokens', [
309
267
  uintCV(amount),
310
268
  contractPrincipalCV(address[0], address[1]),
311
269
  uintCV(lockPeriod),
@@ -314,10 +272,10 @@ export class Farm2Manager {
314
272
  await openContractCall(function_option);
315
273
  }
316
274
 
317
- async getFarmBalance( token: any) {
275
+ async getFarmBalance2(token: any, farming_contract: string) {
318
276
  // console.log("getBalance", token)
319
277
  const pair_details_options: any = getReadOptions(this.stackswap, token.addr, 'get-balance',
320
- [contractPrincipalCV(this.stackswap.config.STACKSWAP_ADDRESS(), this.stackswap.config.CONTRACT_NAME_STACKSWAP_FARMING())]);
278
+ [contractPrincipalCV(this.stackswap.config.STACKSWAP_ADDRESS(), farming_contract)]);
321
279
  try {
322
280
  const result = await callReadOnlyFunction(pair_details_options);
323
281
  return cvToValue(result).value;
@@ -326,13 +284,12 @@ export class Farm2Manager {
326
284
  }
327
285
  }
328
286
 
329
-
330
- async claimFarmingContract( pool_address: string, cycle: number, amount_STSW: string, amount_LP: string, callback: any = null) {
287
+ async claimFarmingContract2(pool_address: string, cycle: number, amount_STSW: string, amount_LP: string, callback: any, farming_contract: string) {
331
288
  const address = pool_address.split('.');
332
289
  const post_condition = [];
333
- post_condition.push(await getPostConditionFromAsset(this.stackswap, this.stackswap.config.STACKSWAP_ADDRESS() + '.' + this.stackswap.config.CONTRACT_NAME_STACKSWAP_FARMING(), this.stackswap.config.BASE_STSW_DATA().addr, amount_STSW, FungibleConditionCode.Equal));
334
- post_condition.push(await getPostConditionFromAsset(this.stackswap, this.stackswap.config.STACKSWAP_ADDRESS() + '.' + this.stackswap.config.CONTRACT_NAME_STACKSWAP_FARMING(), pool_address, amount_LP, FungibleConditionCode.Equal));
335
- const function_option = getWriteOptions(this.stackswap, this.stackswap.config.CONTRACT_NAME_STACKSWAP_FARMING(), 'claimStakingReward', [
290
+ post_condition.push(await getPostConditionFromAsset(this.stackswap, this.stackswap.config.STACKSWAP_ADDRESS() + '.' + farming_contract, this.stackswap.config.BASE_STSW_DATA().addr, amount_STSW, FungibleConditionCode.Equal));
291
+ post_condition.push(await getPostConditionFromAsset(this.stackswap, this.stackswap.config.STACKSWAP_ADDRESS() + '.' + farming_contract, pool_address, amount_LP, FungibleConditionCode.Equal));
292
+ const function_option = getWriteOptions(this.stackswap, farming_contract, 'claimStakingReward', [
336
293
  uintCV(cycle),
337
294
  contractPrincipalCV(address[0], address[1]),
338
295
  contractPrincipalCV(this.stackswap.config.STACKSWAP_ADDRESS(), this.stackswap.config.CONTRACT_NAME_ORACLE())
@@ -341,48 +298,46 @@ export class Farm2Manager {
341
298
  }
342
299
 
343
300
 
344
- async unstakeFarmingContract( pool_address: string, cycle: number, amount_LP: string, callback: any = null) {
301
+ async unstakeFarmingContract2(pool_address: string, cycle: number, amount_LP: string, callback: any, farming_contract: string) {
345
302
  const address = pool_address.split('.');
346
303
  const post_condition = [];
347
- // post_condition.push(await getPostConditionFromAsset(STACKSWAP_ADDRESS()+'.'+CONTRACT_NAME_STACKSWAP_FARMING(), BASE_STSW_DATA().addr, amount_STSW, FungibleConditionCode.Equal));
348
- post_condition.push(await getPostConditionFromAsset(this.stackswap, this.stackswap.config.STACKSWAP_ADDRESS() + '.' + this.stackswap.config.CONTRACT_NAME_STACKSWAP_FARMING(), pool_address, amount_LP, FungibleConditionCode.Equal));
349
- const function_option = getWriteOptions(this.stackswap, this.stackswap.config.CONTRACT_NAME_STACKSWAP_FARMING(), 'unstake-from-farming', [
304
+ post_condition.push(await getPostConditionFromAsset(this.stackswap, this.stackswap.config.STACKSWAP_ADDRESS() + '.' + farming_contract, pool_address, amount_LP, FungibleConditionCode.Equal));
305
+ const function_option = getWriteOptions(this.stackswap, farming_contract, 'unstake-from-farming', [
350
306
  uintCV(cycle),
351
307
  contractPrincipalCV(address[0], address[1]),
352
308
  ], post_condition, callback);
353
309
  await openContractCall(function_option);
354
310
  }
355
311
 
356
-
357
- async getCurrentFarmingRound() {
358
- const bh:number = Number(await this.stackswap.getCurrentBlock());
359
- return await this. getFarmingRound(bh);
312
+ async getCurrentFarmingRound2(farming_contract: string) {
313
+ const bh = await this.stackswap.getCurrentBlock();
314
+ return await this.getFarmingRound2(Number(bh), farming_contract);
360
315
  }
361
316
 
362
-
363
- async getRewardBalanceFromFarming( pool_addr: string, unclaimed_list: any) {
317
+ async getRewardBalanceFromFarming2(pool_addr: string, unclaimed_list: any, farming_contract: string) {
364
318
  // let reward_balance = new BigNumber(0);
365
319
  const num_list = [];
366
320
  for (const unclaimed of unclaimed_list) {
367
321
  num_list.push(uintCV(unclaimed));
368
322
  }
369
323
  let reward_num = new BigNumber(0);
370
-
324
+ console.log(pool_addr, num_list, farming_contract)
371
325
  try {
372
326
  for (let i = 0; i < num_list.length; i += 4) {
373
- // console.log(i, num_list.slice(i, i+4 < num_list.length ? i + 4 : num_list.length));
374
- const result = await this.getFarmingUserRewardList(pool_addr, num_list.slice(i, i + 4 < num_list.length ? i + 4 : num_list.length));
327
+ console.log(pool_addr, num_list.slice(i, i + 4 < num_list.length ? i + 4 : num_list.length), farming_contract);
328
+ const result = await this.getFarmingUserRewardList2(pool_addr, num_list.slice(i, i + 4 < num_list.length ? i + 4 : num_list.length), farming_contract);
375
329
  // rewardSum.value;
376
- // console.log('temp result', result);
330
+ console.log('temp result', result.rewardSum.value);
377
331
  reward_num = reward_num.plus(new BigNumber(result.rewardSum.value));
378
332
  }
379
- return numFormat_(num2decimal(reward_num.toString(), 6));
333
+ return num2decimal(reward_num.toString(), 6);
380
334
  } catch (e) {
381
335
  // console.log(e);
382
- return numFormat_(num2decimal(reward_num.toString(), 6));
336
+ return num2decimal(reward_num.toString(), 6);
383
337
  }
384
338
  }
385
-
339
+
340
+
386
341
  getFarmingAPR(tvl: string, stsw_price: string, reward: string, weight: string) {
387
342
  // console.log('farmingAPR');
388
343
  // console.log({tvl, stsw_price, reward, weight})
@@ -404,4 +359,123 @@ export class Farm2Manager {
404
359
  return new_reward_apr.toFixed(3).toString();
405
360
  }
406
361
  }
362
+
363
+ async getFarmDataFromData(data: any, type: Mode) {
364
+ const oracle_stsw = (await this.stackswap.lbtcManager.getPrice('STSW'))['last-price'].value;
365
+ const oracle_stx = (await this.stackswap.lbtcManager.getPrice('STX'))['last-price'].value;
366
+ const oracle_lbtc = (await this.stackswap.lbtcManager.getPrice('lBTC'))['last-price'].value;
367
+ const bh = await this.stackswap.getCurrentBlock();
368
+ return await this.getEachFarmData(data, bh, {oracle_stsw, oracle_lbtc, oracle_stx}, type)
369
+
370
+ }
371
+
372
+ async getEachFarmData(data: any, bh: string, oracle_data: { oracle_stx: any, oracle_stsw: any, oracle_lbtc: any }, type: Mode) {
373
+
374
+ const tvl_apr = new BigNumber(0);
375
+ const farms : any[] = [];
376
+ const farm_length: number = data.length;
377
+
378
+ for (let i = 0; i < data.length; i++) {
379
+ let farming_contract = '';
380
+ if (data[i].farm_group === 1) {
381
+ farming_contract = this.stackswap.config.CONTRACT_NAME_STACKSWAP_FARMING2_1();
382
+ } else if (data[i].farm_group === 5) {
383
+ farming_contract = this.stackswap.config.CONTRACT_NAME_STACKSWAP_FARMING2_5();
384
+ }
385
+
386
+ const farm_round = await this.getCurrentFarmingRound2(farming_contract);
387
+ this.getFarmData(data[i], farm_round, bh, tvl_apr, farming_contract, oracle_data, type).then((value) => {
388
+ value.farm_group = data[i].farm_group;
389
+ value.farm_contract = farming_contract;
390
+ farms.push(value);
391
+
392
+ if (farms.length === farm_length) {
393
+ const tvls = {'group1': '0', 'group5': '0'}
394
+ for (const farm_temp of farms) {
395
+ switch (farm_temp.farm_group) {
396
+ case 1:
397
+ tvls.group1 = new BigNumber(tvls.group1).plus(new BigNumber(this.getPoolUSDPriceFarm(farm_temp, farm_temp.total_locked_lp, oracle_data)).multipliedBy(farm_temp.weight)).toString();
398
+ break;
399
+ case 5:
400
+ tvls.group5 = new BigNumber(tvls.group5).plus(new BigNumber(this.getPoolUSDPriceFarm(farm_temp, farm_temp.total_locked_lp, oracle_data)).multipliedBy(farm_temp.weight)).toString();
401
+ break;
402
+ }
403
+ }
404
+ console.log({tvls})
405
+ for (const farm_temp of farms) {
406
+ switch (farm_temp.farm_group) {
407
+ case 1:
408
+ farm_temp.APR = this.getFarmingAPR(tvls.group1, oracle_data.oracle_stsw, farm_temp.reward, farm_temp.weight);
409
+ break;
410
+ case 5:
411
+ farm_temp.APR = this.getFarmingAPR(tvls.group5, oracle_data.oracle_stsw, farm_temp.reward, farm_temp.weight);
412
+ break;
413
+ }
414
+ console.log(farm_temp.APR)
415
+ }
416
+ }
417
+ })
418
+ }
419
+ return farms;
420
+ }
421
+
422
+ async getFarmData(farm: any, farm_round: any, block_height: string, tvl_apr: BigNumber, farming_contract: string, oracle_data: { oracle_stx: any, oracle_stsw: any, oracle_lbtc: any }, type : Mode ) {
423
+ farm.valid = false;
424
+ farm.cur_round = farm_round;
425
+ switch (type) {
426
+ case Mode.ONLY_BLOCKCHAIN:
427
+ farm.pair = (await this.stackswap.poolManager.getPairDataFromContract(farm.token_x, farm.token_y));
428
+ break;
429
+ case Mode.WITH_SERVER:
430
+ farm.pair = (await this.stackswap.swapManager.findRouter(farm.token_x, farm.token_y, Number(block_height))).pair;
431
+ break;
432
+ }
433
+ farm.pair.sharetoken = {balance: 0, addr: farm.pair['liquidity-token'].value};
434
+ farm.pair.price = (farm.pair['balance-y'].value / (10 ** farm.token_y.decimal)) / (farm.pair['balance-x'].value / (10 ** farm.token_x.decimal));
435
+ const farm_total_v2 = await this.getFarmBalance2(farm.pair.sharetoken, farming_contract);
436
+ const farm_total_v1 = await this.stackswap.farmManager.getFarmBalance(farm.pair.sharetoken);
437
+ farm.TVL = this.getPoolUSDPriceFarm(farm, new BigNumber(farm_total_v1).plus(farm_total_v2).toString(), oracle_data);
438
+ farm.total_locked_lp = new BigNumber(farm_total_v1).plus(farm_total_v2).toString();
439
+ const farm_temp_total_data = await this.getFarmingTotalStatusCycle2(farm_round, farming_contract);
440
+ farm.reward = farm_temp_total_data.reward;
441
+ const lpTotalData = await this.getFarmingTotalPoolStatus2(farm.token_swapr, farming_contract);
442
+ farm.weight = lpTotalData.weight;
443
+ tvl_apr = tvl_apr.plus(new BigNumber(this.getPoolUSDPriceFarm(farm, farm.total_locked_lp, oracle_data)).multipliedBy(farm.weight));
444
+ console.log(farming_contract, farm.TVL, tvl_apr);
445
+ farm.valid = true;
446
+
447
+ return farm;
448
+ }
449
+
450
+ async getFarmUserData(farm: any, farm_round: string, farming_contract: string, oracle_data: { oracle_stx: any, oracle_stsw: any, oracle_lbtc: any }){
451
+ farm.isMigratable = await this.isUserMigratable2(farm.pair['liquidity-token'].value, farming_contract); // 신규는 무조건 true
452
+ this.getFarmingStakerStatus2(farm.token_swapr, Number(farm_round), farming_contract).then((status_v2) => {
453
+ if (!farm.isMigratable) {
454
+ this.stackswap.farmManager.getFarmingStakerStatus(farm.token_swapr, Number(farm_round)).then((status_v1) => {
455
+ farm.claimable_list = status_v2.unclaimedList.concat(status_v1.unclaimedList);
456
+ farm.pooled_stsw = new BigNumber(status_v2.pooledToken).plus(status_v1.pooledToken).toString();
457
+ farm.stakedList = status_v2.stakedList;
458
+ farm.user_TVL = this.getPoolUSDPriceFarm(farm, farm.pooled_stsw, oracle_data);
459
+ // console.log('migrated', status_v2, status_v1);
460
+ this.getRewardBalanceFromFarming2(farm.token_swapr, status_v2.unclaimedList, farming_contract).then(reward_v2 => {
461
+ this.stackswap.farmManager.getRewardBalanceFromFarming(farm.token_swapr, status_v1.unclaimedList).then(reward_v1 => {
462
+ console.log({reward_v2})
463
+ farm.total_reward = new BigNumber(reward_v2).plus(reward_v1).toString();
464
+ }
465
+ )
466
+ });
467
+ });
468
+ } else {
469
+ console.log('not yet migrated', status_v2);
470
+ farm.claimable_list = status_v2.unclaimedList;
471
+ farm.pooled_stsw = status_v2.pooledToken;
472
+ farm.stakedList = status_v2.stakedList;
473
+ farm.user_TVL = this.getPoolUSDPriceFarm(farm, farm.pooled_stsw, oracle_data);
474
+ this.getRewardBalanceFromFarming2(farm.token_swapr, status_v2.unclaimedList, farming_contract).then(reward => {
475
+ console.log({reward})
476
+ farm.total_reward = reward;
477
+ });
478
+ }
479
+ });
480
+ }
407
481
  }
@@ -121,17 +121,25 @@ export class TokenManager {
121
121
  const uri_result = await axios.get(token_uri);
122
122
 
123
123
  try {
124
- await axios.get(uri_result.data.image);
125
124
  token_icon = uri_result.data.image;
126
125
  } catch (e) {
127
126
  // console.log('no img file');
128
127
  }
129
128
  try {
130
- await axios.get(uri_result.data.vector);
131
129
  token_icon = uri_result.data.vector;
132
130
  } catch (e) {
133
131
  // console.log('no svg file');
134
132
  }
133
+ try {
134
+ token_icon = uri_result.data.properties.image.descriptions;
135
+ } catch (e) {
136
+ // console.log('no img file');
137
+ }
138
+ try {
139
+ token_icon = uri_result.data.properties.vector.descriptions;
140
+ } catch (e) {
141
+ // console.log('no svg file');
142
+ }
135
143
  } catch (e) {
136
144
  // console.log(e);
137
145
  }
@@ -159,11 +159,14 @@ export function decimal2integer(input:any, decimal:number = 6){
159
159
 
160
160
  export function numWithComma(input:any) {
161
161
  if(typeof (input) === 'string'){
162
- return input.replace(/\B(?<!\.\d*)(?=(\d{3})+(?!\d))/g, ',');
162
+ return new BigNumber(input).toNumber().toLocaleString();
163
+ // return input.replace(/\B(?<!\.\d*)(?=(\d{3})+(?!\d))/g, ',');
163
164
  }else if(typeof (input) === 'number'){
164
- return input.toString().replace(/\B(?<!\.\d*)(?=(\d{3})+(?!\d))/g, ',');
165
+ return new BigNumber(input).toNumber().toLocaleString();
166
+ // return input.toString().replace(/\B(?<!\.\d*)(?=(\d{3})+(?!\d))/g, ',');
165
167
  }else if(BigNumber.isBigNumber(input)){
166
- return input.toString().replace(/\B(?<!\.\d*)(?=(\d{3})+(?!\d))/g, ',');
168
+ return input.toNumber().toLocaleString();
169
+ // return input.toString().replace(/\B(?<!\.\d*)(?=(\d{3})+(?!\d))/g, ',');
167
170
  }else{
168
171
  return 'ERROR';
169
172
  }
@@ -232,38 +235,3 @@ export function getAmountStringWithDecimal(amount: string, decimal: number) {
232
235
  return new BigNumber(10 ** decimal).multipliedBy(new BigNumber(amount)).toString();
233
236
  }
234
237
  }
235
- export function getDecimalStringfromString2(amount: string, decimal: number) {
236
- const amount_bn = new BigNumber(amount);
237
- const decimal_bn = new BigNumber(10**decimal);
238
- BigNumber.config({ DECIMAL_PLACES: 3});
239
- return (amount_bn.dividedBy(decimal_bn)).toFixed(0).toString();
240
- }
241
-
242
- export function getDecimalStringfromString(amount: string, decimal: number) {
243
- const len_str = amount.length;
244
- if ( len_str > decimal) {
245
- const temp =amount.substr(0, len_str-decimal) + '.'+amount.substr(len_str-decimal, len_str).replace(/.0+$/, '')
246
- if (temp === '0.') {
247
- return '0';
248
- } else{
249
- if(temp.charAt(temp.length-1) === '.'){
250
- return temp.substr(0, temp.length-1);
251
- } else {
252
- return temp;
253
- }
254
- }
255
- } else {
256
- const temp = '0'.repeat(decimal-len_str+1) + amount;
257
- const temp2 = temp.substr(0, temp.length-decimal) + '.'+temp.substr(temp.length - decimal, temp.length).replace(/0+$/, '');
258
-
259
- if (temp2 === '0.') {
260
- return '0';
261
- } else{
262
- if(temp2.charAt(temp2.length-1) === '.'){
263
- return temp2.substr(0, temp2.length-1);
264
- } else {
265
- return temp2;
266
- }
267
- }
268
- }
269
- }