@strkfarm/sdk 1.0.40 → 1.0.41

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.
@@ -55182,14 +55182,24 @@ var strkfarm_risk_engine = (() => {
55182
55182
  _solveExpectedAmountsEq(availableAmount0, availableAmount1, ratio, price) {
55183
55183
  const y = ratio.multipliedBy(availableAmount1).minus(availableAmount0).dividedBy(ratio.plus(1 / price));
55184
55184
  const x = y.dividedBy(price);
55185
+ logger.verbose(
55186
+ `${_EkuboCLVault.name}: _solveExpectedAmountsEq => x: ${x.toString()}, y: ${y.toString()}, amount0: ${availableAmount0.toString()}, amount1: ${availableAmount1.toString()}`
55187
+ );
55188
+ if (ratio.eq(0)) {
55189
+ return {
55190
+ amount0: Web3Number.fromWei("0", availableAmount0.decimals),
55191
+ amount1: availableAmount1.minus(y),
55192
+ ratio: 0
55193
+ };
55194
+ }
55185
55195
  return {
55186
55196
  amount0: availableAmount0.plus(x),
55187
55197
  amount1: availableAmount1.minus(y),
55188
55198
  ratio: Number(ratio.toString())
55189
55199
  };
55190
55200
  }
55191
- async getSwapInfoToHandleUnused(considerRebalance = true) {
55192
- const poolKey = await this.getPoolKey();
55201
+ async unusedBalances(_poolKey) {
55202
+ const poolKey = _poolKey || await this.getPoolKey();
55193
55203
  const erc20Mod = new ERC20(this.config);
55194
55204
  const token0Info = await Global.getTokenInfoFromAddr(poolKey.token0);
55195
55205
  const token1Info = await Global.getTokenInfoFromAddr(poolKey.token1);
@@ -55210,11 +55220,24 @@ var strkfarm_risk_engine = (() => {
55210
55220
  const token1Price = await this.pricer.getPrice(token1Info.symbol);
55211
55221
  const token0PriceUsd = token0Price.price * Number(token0Bal1.toFixed(13));
55212
55222
  const token1PriceUsd = token1Price.price * Number(token1Bal1.toFixed(13));
55213
- if (token0PriceUsd > 1 && token1PriceUsd > 1) {
55214
- throw new Error(
55215
- "Both tokens are non-zero and above $1, call handle_fees first"
55216
- );
55217
- }
55223
+ return {
55224
+ token0: {
55225
+ amount: token0Bal1,
55226
+ tokenInfo: token0Info,
55227
+ usdValue: token0PriceUsd
55228
+ },
55229
+ token1: {
55230
+ amount: token1Bal1,
55231
+ tokenInfo: token1Info,
55232
+ usdValue: token1PriceUsd
55233
+ }
55234
+ };
55235
+ }
55236
+ async getSwapInfoToHandleUnused(considerRebalance = true) {
55237
+ const poolKey = await this.getPoolKey();
55238
+ const unusedBalances = await this.unusedBalances(poolKey);
55239
+ const { amount: token0Bal1, usdValue: token0PriceUsd } = unusedBalances.token0;
55240
+ const { amount: token1Bal1, usdValue: token1PriceUsd } = unusedBalances.token1;
55218
55241
  let token0Bal = token0Bal1;
55219
55242
  let token1Bal = token1Bal1;
55220
55243
  if (considerRebalance) {
@@ -55232,25 +55255,33 @@ var strkfarm_risk_engine = (() => {
55232
55255
  logger.verbose(
55233
55256
  `${_EkuboCLVault.name}: getSwapInfoToHandleUnused => token0Bal: ${token0Bal.toString()}, token1Bal: ${token1Bal.toString()}`
55234
55257
  );
55235
- const newBounds = await this.getNewBounds();
55258
+ let ekuboBounds;
55259
+ if (considerRebalance) {
55260
+ ekuboBounds = await this.getNewBounds();
55261
+ } else {
55262
+ ekuboBounds = await this.getCurrentBounds();
55263
+ }
55236
55264
  logger.verbose(
55237
- `${_EkuboCLVault.name}: getSwapInfoToHandleUnused => newBounds: ${newBounds.lowerTick}, ${newBounds.upperTick}`
55265
+ `${_EkuboCLVault.name}: getSwapInfoToHandleUnused => newBounds: ${ekuboBounds.lowerTick}, ${ekuboBounds.upperTick}`
55238
55266
  );
55239
55267
  return await this.getSwapInfoGivenAmounts(
55240
55268
  poolKey,
55241
55269
  token0Bal,
55242
55270
  token1Bal,
55243
- newBounds
55271
+ ekuboBounds
55244
55272
  );
55245
55273
  }
55246
55274
  async getSwapInfoGivenAmounts(poolKey, token0Bal, token1Bal, bounds) {
55275
+ logger.verbose(
55276
+ `${_EkuboCLVault.name}: getSwapInfoGivenAmounts::pre => token0Bal: ${token0Bal.toString()}, token1Bal: ${token1Bal.toString()}`
55277
+ );
55247
55278
  let expectedAmounts = await this._getExpectedAmountsForLiquidity(
55248
55279
  token0Bal,
55249
55280
  token1Bal,
55250
55281
  bounds
55251
55282
  );
55252
55283
  logger.verbose(
55253
- `${_EkuboCLVault.name}: getSwapInfoToHandleUnused => expectedAmounts: ${expectedAmounts.amount0.toString()}, ${expectedAmounts.amount1.toString()}`
55284
+ `${_EkuboCLVault.name}: getSwapInfoToHandleUnused => expectedAmounts2: ${expectedAmounts.amount0.toString()}, ${expectedAmounts.amount1.toString()}`
55254
55285
  );
55255
55286
  let retry = 0;
55256
55287
  const maxRetry = 10;
@@ -55523,6 +55554,7 @@ var strkfarm_risk_engine = (() => {
55523
55554
  const token0Info = await Global.getTokenInfoFromAddr(poolKey.token0);
55524
55555
  const token1Info = await Global.getTokenInfoFromAddr(poolKey.token1);
55525
55556
  const bounds = await this.getCurrentBounds();
55557
+ logger.verbose(`${_EkuboCLVault.name}: harvest => unClaimedRewards: ${unClaimedRewards.length}`);
55526
55558
  const calls = [];
55527
55559
  for (let claim of unClaimedRewards) {
55528
55560
  const fee = claim.claim.amount.multipliedBy(this.metadata.additionalInfo.feeBps).dividedBy(1e4);
@@ -55555,12 +55587,28 @@ var strkfarm_risk_engine = (() => {
55555
55587
  18
55556
55588
  // cause its always STRK?
55557
55589
  );
55590
+ logger.verbose(
55591
+ `${_EkuboCLVault.name}: harvest => swap1Amount: ${swap1Amount}`
55592
+ );
55558
55593
  const remainingAmount = postFeeAmount.minus(swap1Amount);
55594
+ logger.verbose(
55595
+ `${_EkuboCLVault.name}: harvest => remainingAmount: ${remainingAmount}`
55596
+ );
55559
55597
  const swapInfo2 = {
55560
55598
  ...swapInfo,
55561
55599
  token_from_amount: uint256_exports.bnToUint256(remainingAmount.toWei())
55562
55600
  };
55563
55601
  swapInfo2.token_to_address = token1Info.address.address;
55602
+ logger.verbose(
55603
+ `${_EkuboCLVault.name}: harvest => swapInfo: ${JSON.stringify(
55604
+ swapInfo
55605
+ )}`
55606
+ );
55607
+ logger.verbose(
55608
+ `${_EkuboCLVault.name}: harvest => swapInfo2: ${JSON.stringify(
55609
+ swapInfo2
55610
+ )}`
55611
+ );
55564
55612
  const calldata = [
55565
55613
  claim.rewardsContract.address,
55566
55614
  {
@@ -18976,14 +18976,24 @@ var EkuboCLVault = class _EkuboCLVault extends BaseStrategy {
18976
18976
  _solveExpectedAmountsEq(availableAmount0, availableAmount1, ratio, price) {
18977
18977
  const y = ratio.multipliedBy(availableAmount1).minus(availableAmount0).dividedBy(ratio.plus(1 / price));
18978
18978
  const x = y.dividedBy(price);
18979
+ logger.verbose(
18980
+ `${_EkuboCLVault.name}: _solveExpectedAmountsEq => x: ${x.toString()}, y: ${y.toString()}, amount0: ${availableAmount0.toString()}, amount1: ${availableAmount1.toString()}`
18981
+ );
18982
+ if (ratio.eq(0)) {
18983
+ return {
18984
+ amount0: Web3Number.fromWei("0", availableAmount0.decimals),
18985
+ amount1: availableAmount1.minus(y),
18986
+ ratio: 0
18987
+ };
18988
+ }
18979
18989
  return {
18980
18990
  amount0: availableAmount0.plus(x),
18981
18991
  amount1: availableAmount1.minus(y),
18982
18992
  ratio: Number(ratio.toString())
18983
18993
  };
18984
18994
  }
18985
- async getSwapInfoToHandleUnused(considerRebalance = true) {
18986
- const poolKey = await this.getPoolKey();
18995
+ async unusedBalances(_poolKey) {
18996
+ const poolKey = _poolKey || await this.getPoolKey();
18987
18997
  const erc20Mod = new ERC20(this.config);
18988
18998
  const token0Info = await Global.getTokenInfoFromAddr(poolKey.token0);
18989
18999
  const token1Info = await Global.getTokenInfoFromAddr(poolKey.token1);
@@ -19004,11 +19014,24 @@ var EkuboCLVault = class _EkuboCLVault extends BaseStrategy {
19004
19014
  const token1Price = await this.pricer.getPrice(token1Info.symbol);
19005
19015
  const token0PriceUsd = token0Price.price * Number(token0Bal1.toFixed(13));
19006
19016
  const token1PriceUsd = token1Price.price * Number(token1Bal1.toFixed(13));
19007
- if (token0PriceUsd > 1 && token1PriceUsd > 1) {
19008
- throw new Error(
19009
- "Both tokens are non-zero and above $1, call handle_fees first"
19010
- );
19011
- }
19017
+ return {
19018
+ token0: {
19019
+ amount: token0Bal1,
19020
+ tokenInfo: token0Info,
19021
+ usdValue: token0PriceUsd
19022
+ },
19023
+ token1: {
19024
+ amount: token1Bal1,
19025
+ tokenInfo: token1Info,
19026
+ usdValue: token1PriceUsd
19027
+ }
19028
+ };
19029
+ }
19030
+ async getSwapInfoToHandleUnused(considerRebalance = true) {
19031
+ const poolKey = await this.getPoolKey();
19032
+ const unusedBalances = await this.unusedBalances(poolKey);
19033
+ const { amount: token0Bal1, usdValue: token0PriceUsd } = unusedBalances.token0;
19034
+ const { amount: token1Bal1, usdValue: token1PriceUsd } = unusedBalances.token1;
19012
19035
  let token0Bal = token0Bal1;
19013
19036
  let token1Bal = token1Bal1;
19014
19037
  if (considerRebalance) {
@@ -19026,25 +19049,33 @@ var EkuboCLVault = class _EkuboCLVault extends BaseStrategy {
19026
19049
  logger.verbose(
19027
19050
  `${_EkuboCLVault.name}: getSwapInfoToHandleUnused => token0Bal: ${token0Bal.toString()}, token1Bal: ${token1Bal.toString()}`
19028
19051
  );
19029
- const newBounds = await this.getNewBounds();
19052
+ let ekuboBounds;
19053
+ if (considerRebalance) {
19054
+ ekuboBounds = await this.getNewBounds();
19055
+ } else {
19056
+ ekuboBounds = await this.getCurrentBounds();
19057
+ }
19030
19058
  logger.verbose(
19031
- `${_EkuboCLVault.name}: getSwapInfoToHandleUnused => newBounds: ${newBounds.lowerTick}, ${newBounds.upperTick}`
19059
+ `${_EkuboCLVault.name}: getSwapInfoToHandleUnused => newBounds: ${ekuboBounds.lowerTick}, ${ekuboBounds.upperTick}`
19032
19060
  );
19033
19061
  return await this.getSwapInfoGivenAmounts(
19034
19062
  poolKey,
19035
19063
  token0Bal,
19036
19064
  token1Bal,
19037
- newBounds
19065
+ ekuboBounds
19038
19066
  );
19039
19067
  }
19040
19068
  async getSwapInfoGivenAmounts(poolKey, token0Bal, token1Bal, bounds) {
19069
+ logger.verbose(
19070
+ `${_EkuboCLVault.name}: getSwapInfoGivenAmounts::pre => token0Bal: ${token0Bal.toString()}, token1Bal: ${token1Bal.toString()}`
19071
+ );
19041
19072
  let expectedAmounts = await this._getExpectedAmountsForLiquidity(
19042
19073
  token0Bal,
19043
19074
  token1Bal,
19044
19075
  bounds
19045
19076
  );
19046
19077
  logger.verbose(
19047
- `${_EkuboCLVault.name}: getSwapInfoToHandleUnused => expectedAmounts: ${expectedAmounts.amount0.toString()}, ${expectedAmounts.amount1.toString()}`
19078
+ `${_EkuboCLVault.name}: getSwapInfoToHandleUnused => expectedAmounts2: ${expectedAmounts.amount0.toString()}, ${expectedAmounts.amount1.toString()}`
19048
19079
  );
19049
19080
  let retry = 0;
19050
19081
  const maxRetry = 10;
@@ -19317,6 +19348,7 @@ var EkuboCLVault = class _EkuboCLVault extends BaseStrategy {
19317
19348
  const token0Info = await Global.getTokenInfoFromAddr(poolKey.token0);
19318
19349
  const token1Info = await Global.getTokenInfoFromAddr(poolKey.token1);
19319
19350
  const bounds = await this.getCurrentBounds();
19351
+ logger.verbose(`${_EkuboCLVault.name}: harvest => unClaimedRewards: ${unClaimedRewards.length}`);
19320
19352
  const calls = [];
19321
19353
  for (let claim of unClaimedRewards) {
19322
19354
  const fee = claim.claim.amount.multipliedBy(this.metadata.additionalInfo.feeBps).dividedBy(1e4);
@@ -19349,12 +19381,28 @@ var EkuboCLVault = class _EkuboCLVault extends BaseStrategy {
19349
19381
  18
19350
19382
  // cause its always STRK?
19351
19383
  );
19384
+ logger.verbose(
19385
+ `${_EkuboCLVault.name}: harvest => swap1Amount: ${swap1Amount}`
19386
+ );
19352
19387
  const remainingAmount = postFeeAmount.minus(swap1Amount);
19388
+ logger.verbose(
19389
+ `${_EkuboCLVault.name}: harvest => remainingAmount: ${remainingAmount}`
19390
+ );
19353
19391
  const swapInfo2 = {
19354
19392
  ...swapInfo,
19355
19393
  token_from_amount: uint2564.bnToUint256(remainingAmount.toWei())
19356
19394
  };
19357
19395
  swapInfo2.token_to_address = token1Info.address.address;
19396
+ logger.verbose(
19397
+ `${_EkuboCLVault.name}: harvest => swapInfo: ${JSON.stringify(
19398
+ swapInfo
19399
+ )}`
19400
+ );
19401
+ logger.verbose(
19402
+ `${_EkuboCLVault.name}: harvest => swapInfo2: ${JSON.stringify(
19403
+ swapInfo2
19404
+ )}`
19405
+ );
19358
19406
  const calldata = [
19359
19407
  claim.rewardsContract.address,
19360
19408
  {
package/dist/index.d.ts CHANGED
@@ -672,6 +672,18 @@ declare class EkuboCLVault extends BaseStrategy<DualTokenInfo, DualActionAmount>
672
672
  */
673
673
  private _getExpectedAmountsForLiquidity;
674
674
  private _solveExpectedAmountsEq;
675
+ unusedBalances(_poolKey?: EkuboPoolKey): Promise<{
676
+ token0: {
677
+ amount: Web3Number;
678
+ tokenInfo: TokenInfo;
679
+ usdValue: number;
680
+ };
681
+ token1: {
682
+ amount: Web3Number;
683
+ tokenInfo: TokenInfo;
684
+ usdValue: number;
685
+ };
686
+ }>;
675
687
  getSwapInfoToHandleUnused(considerRebalance?: boolean): Promise<SwapInfo>;
676
688
  getSwapInfoGivenAmounts(poolKey: EkuboPoolKey, token0Bal: Web3Number, token1Bal: Web3Number, bounds: EkuboBounds): Promise<SwapInfo>;
677
689
  /**
package/dist/index.js CHANGED
@@ -19090,14 +19090,24 @@ var EkuboCLVault = class _EkuboCLVault extends BaseStrategy {
19090
19090
  _solveExpectedAmountsEq(availableAmount0, availableAmount1, ratio, price) {
19091
19091
  const y = ratio.multipliedBy(availableAmount1).minus(availableAmount0).dividedBy(ratio.plus(1 / price));
19092
19092
  const x = y.dividedBy(price);
19093
+ logger.verbose(
19094
+ `${_EkuboCLVault.name}: _solveExpectedAmountsEq => x: ${x.toString()}, y: ${y.toString()}, amount0: ${availableAmount0.toString()}, amount1: ${availableAmount1.toString()}`
19095
+ );
19096
+ if (ratio.eq(0)) {
19097
+ return {
19098
+ amount0: Web3Number.fromWei("0", availableAmount0.decimals),
19099
+ amount1: availableAmount1.minus(y),
19100
+ ratio: 0
19101
+ };
19102
+ }
19093
19103
  return {
19094
19104
  amount0: availableAmount0.plus(x),
19095
19105
  amount1: availableAmount1.minus(y),
19096
19106
  ratio: Number(ratio.toString())
19097
19107
  };
19098
19108
  }
19099
- async getSwapInfoToHandleUnused(considerRebalance = true) {
19100
- const poolKey = await this.getPoolKey();
19109
+ async unusedBalances(_poolKey) {
19110
+ const poolKey = _poolKey || await this.getPoolKey();
19101
19111
  const erc20Mod = new ERC20(this.config);
19102
19112
  const token0Info = await Global.getTokenInfoFromAddr(poolKey.token0);
19103
19113
  const token1Info = await Global.getTokenInfoFromAddr(poolKey.token1);
@@ -19118,11 +19128,24 @@ var EkuboCLVault = class _EkuboCLVault extends BaseStrategy {
19118
19128
  const token1Price = await this.pricer.getPrice(token1Info.symbol);
19119
19129
  const token0PriceUsd = token0Price.price * Number(token0Bal1.toFixed(13));
19120
19130
  const token1PriceUsd = token1Price.price * Number(token1Bal1.toFixed(13));
19121
- if (token0PriceUsd > 1 && token1PriceUsd > 1) {
19122
- throw new Error(
19123
- "Both tokens are non-zero and above $1, call handle_fees first"
19124
- );
19125
- }
19131
+ return {
19132
+ token0: {
19133
+ amount: token0Bal1,
19134
+ tokenInfo: token0Info,
19135
+ usdValue: token0PriceUsd
19136
+ },
19137
+ token1: {
19138
+ amount: token1Bal1,
19139
+ tokenInfo: token1Info,
19140
+ usdValue: token1PriceUsd
19141
+ }
19142
+ };
19143
+ }
19144
+ async getSwapInfoToHandleUnused(considerRebalance = true) {
19145
+ const poolKey = await this.getPoolKey();
19146
+ const unusedBalances = await this.unusedBalances(poolKey);
19147
+ const { amount: token0Bal1, usdValue: token0PriceUsd } = unusedBalances.token0;
19148
+ const { amount: token1Bal1, usdValue: token1PriceUsd } = unusedBalances.token1;
19126
19149
  let token0Bal = token0Bal1;
19127
19150
  let token1Bal = token1Bal1;
19128
19151
  if (considerRebalance) {
@@ -19140,25 +19163,33 @@ var EkuboCLVault = class _EkuboCLVault extends BaseStrategy {
19140
19163
  logger.verbose(
19141
19164
  `${_EkuboCLVault.name}: getSwapInfoToHandleUnused => token0Bal: ${token0Bal.toString()}, token1Bal: ${token1Bal.toString()}`
19142
19165
  );
19143
- const newBounds = await this.getNewBounds();
19166
+ let ekuboBounds;
19167
+ if (considerRebalance) {
19168
+ ekuboBounds = await this.getNewBounds();
19169
+ } else {
19170
+ ekuboBounds = await this.getCurrentBounds();
19171
+ }
19144
19172
  logger.verbose(
19145
- `${_EkuboCLVault.name}: getSwapInfoToHandleUnused => newBounds: ${newBounds.lowerTick}, ${newBounds.upperTick}`
19173
+ `${_EkuboCLVault.name}: getSwapInfoToHandleUnused => newBounds: ${ekuboBounds.lowerTick}, ${ekuboBounds.upperTick}`
19146
19174
  );
19147
19175
  return await this.getSwapInfoGivenAmounts(
19148
19176
  poolKey,
19149
19177
  token0Bal,
19150
19178
  token1Bal,
19151
- newBounds
19179
+ ekuboBounds
19152
19180
  );
19153
19181
  }
19154
19182
  async getSwapInfoGivenAmounts(poolKey, token0Bal, token1Bal, bounds) {
19183
+ logger.verbose(
19184
+ `${_EkuboCLVault.name}: getSwapInfoGivenAmounts::pre => token0Bal: ${token0Bal.toString()}, token1Bal: ${token1Bal.toString()}`
19185
+ );
19155
19186
  let expectedAmounts = await this._getExpectedAmountsForLiquidity(
19156
19187
  token0Bal,
19157
19188
  token1Bal,
19158
19189
  bounds
19159
19190
  );
19160
19191
  logger.verbose(
19161
- `${_EkuboCLVault.name}: getSwapInfoToHandleUnused => expectedAmounts: ${expectedAmounts.amount0.toString()}, ${expectedAmounts.amount1.toString()}`
19192
+ `${_EkuboCLVault.name}: getSwapInfoToHandleUnused => expectedAmounts2: ${expectedAmounts.amount0.toString()}, ${expectedAmounts.amount1.toString()}`
19162
19193
  );
19163
19194
  let retry = 0;
19164
19195
  const maxRetry = 10;
@@ -19431,6 +19462,7 @@ var EkuboCLVault = class _EkuboCLVault extends BaseStrategy {
19431
19462
  const token0Info = await Global.getTokenInfoFromAddr(poolKey.token0);
19432
19463
  const token1Info = await Global.getTokenInfoFromAddr(poolKey.token1);
19433
19464
  const bounds = await this.getCurrentBounds();
19465
+ logger.verbose(`${_EkuboCLVault.name}: harvest => unClaimedRewards: ${unClaimedRewards.length}`);
19434
19466
  const calls = [];
19435
19467
  for (let claim of unClaimedRewards) {
19436
19468
  const fee = claim.claim.amount.multipliedBy(this.metadata.additionalInfo.feeBps).dividedBy(1e4);
@@ -19463,12 +19495,28 @@ var EkuboCLVault = class _EkuboCLVault extends BaseStrategy {
19463
19495
  18
19464
19496
  // cause its always STRK?
19465
19497
  );
19498
+ logger.verbose(
19499
+ `${_EkuboCLVault.name}: harvest => swap1Amount: ${swap1Amount}`
19500
+ );
19466
19501
  const remainingAmount = postFeeAmount.minus(swap1Amount);
19502
+ logger.verbose(
19503
+ `${_EkuboCLVault.name}: harvest => remainingAmount: ${remainingAmount}`
19504
+ );
19467
19505
  const swapInfo2 = {
19468
19506
  ...swapInfo,
19469
19507
  token_from_amount: import_starknet9.uint256.bnToUint256(remainingAmount.toWei())
19470
19508
  };
19471
19509
  swapInfo2.token_to_address = token1Info.address.address;
19510
+ logger.verbose(
19511
+ `${_EkuboCLVault.name}: harvest => swapInfo: ${JSON.stringify(
19512
+ swapInfo
19513
+ )}`
19514
+ );
19515
+ logger.verbose(
19516
+ `${_EkuboCLVault.name}: harvest => swapInfo2: ${JSON.stringify(
19517
+ swapInfo2
19518
+ )}`
19519
+ );
19472
19520
  const calldata = [
19473
19521
  claim.rewardsContract.address,
19474
19522
  {
package/dist/index.mjs CHANGED
@@ -19025,14 +19025,24 @@ var EkuboCLVault = class _EkuboCLVault extends BaseStrategy {
19025
19025
  _solveExpectedAmountsEq(availableAmount0, availableAmount1, ratio, price) {
19026
19026
  const y = ratio.multipliedBy(availableAmount1).minus(availableAmount0).dividedBy(ratio.plus(1 / price));
19027
19027
  const x = y.dividedBy(price);
19028
+ logger.verbose(
19029
+ `${_EkuboCLVault.name}: _solveExpectedAmountsEq => x: ${x.toString()}, y: ${y.toString()}, amount0: ${availableAmount0.toString()}, amount1: ${availableAmount1.toString()}`
19030
+ );
19031
+ if (ratio.eq(0)) {
19032
+ return {
19033
+ amount0: Web3Number.fromWei("0", availableAmount0.decimals),
19034
+ amount1: availableAmount1.minus(y),
19035
+ ratio: 0
19036
+ };
19037
+ }
19028
19038
  return {
19029
19039
  amount0: availableAmount0.plus(x),
19030
19040
  amount1: availableAmount1.minus(y),
19031
19041
  ratio: Number(ratio.toString())
19032
19042
  };
19033
19043
  }
19034
- async getSwapInfoToHandleUnused(considerRebalance = true) {
19035
- const poolKey = await this.getPoolKey();
19044
+ async unusedBalances(_poolKey) {
19045
+ const poolKey = _poolKey || await this.getPoolKey();
19036
19046
  const erc20Mod = new ERC20(this.config);
19037
19047
  const token0Info = await Global.getTokenInfoFromAddr(poolKey.token0);
19038
19048
  const token1Info = await Global.getTokenInfoFromAddr(poolKey.token1);
@@ -19053,11 +19063,24 @@ var EkuboCLVault = class _EkuboCLVault extends BaseStrategy {
19053
19063
  const token1Price = await this.pricer.getPrice(token1Info.symbol);
19054
19064
  const token0PriceUsd = token0Price.price * Number(token0Bal1.toFixed(13));
19055
19065
  const token1PriceUsd = token1Price.price * Number(token1Bal1.toFixed(13));
19056
- if (token0PriceUsd > 1 && token1PriceUsd > 1) {
19057
- throw new Error(
19058
- "Both tokens are non-zero and above $1, call handle_fees first"
19059
- );
19060
- }
19066
+ return {
19067
+ token0: {
19068
+ amount: token0Bal1,
19069
+ tokenInfo: token0Info,
19070
+ usdValue: token0PriceUsd
19071
+ },
19072
+ token1: {
19073
+ amount: token1Bal1,
19074
+ tokenInfo: token1Info,
19075
+ usdValue: token1PriceUsd
19076
+ }
19077
+ };
19078
+ }
19079
+ async getSwapInfoToHandleUnused(considerRebalance = true) {
19080
+ const poolKey = await this.getPoolKey();
19081
+ const unusedBalances = await this.unusedBalances(poolKey);
19082
+ const { amount: token0Bal1, usdValue: token0PriceUsd } = unusedBalances.token0;
19083
+ const { amount: token1Bal1, usdValue: token1PriceUsd } = unusedBalances.token1;
19061
19084
  let token0Bal = token0Bal1;
19062
19085
  let token1Bal = token1Bal1;
19063
19086
  if (considerRebalance) {
@@ -19075,25 +19098,33 @@ var EkuboCLVault = class _EkuboCLVault extends BaseStrategy {
19075
19098
  logger.verbose(
19076
19099
  `${_EkuboCLVault.name}: getSwapInfoToHandleUnused => token0Bal: ${token0Bal.toString()}, token1Bal: ${token1Bal.toString()}`
19077
19100
  );
19078
- const newBounds = await this.getNewBounds();
19101
+ let ekuboBounds;
19102
+ if (considerRebalance) {
19103
+ ekuboBounds = await this.getNewBounds();
19104
+ } else {
19105
+ ekuboBounds = await this.getCurrentBounds();
19106
+ }
19079
19107
  logger.verbose(
19080
- `${_EkuboCLVault.name}: getSwapInfoToHandleUnused => newBounds: ${newBounds.lowerTick}, ${newBounds.upperTick}`
19108
+ `${_EkuboCLVault.name}: getSwapInfoToHandleUnused => newBounds: ${ekuboBounds.lowerTick}, ${ekuboBounds.upperTick}`
19081
19109
  );
19082
19110
  return await this.getSwapInfoGivenAmounts(
19083
19111
  poolKey,
19084
19112
  token0Bal,
19085
19113
  token1Bal,
19086
- newBounds
19114
+ ekuboBounds
19087
19115
  );
19088
19116
  }
19089
19117
  async getSwapInfoGivenAmounts(poolKey, token0Bal, token1Bal, bounds) {
19118
+ logger.verbose(
19119
+ `${_EkuboCLVault.name}: getSwapInfoGivenAmounts::pre => token0Bal: ${token0Bal.toString()}, token1Bal: ${token1Bal.toString()}`
19120
+ );
19090
19121
  let expectedAmounts = await this._getExpectedAmountsForLiquidity(
19091
19122
  token0Bal,
19092
19123
  token1Bal,
19093
19124
  bounds
19094
19125
  );
19095
19126
  logger.verbose(
19096
- `${_EkuboCLVault.name}: getSwapInfoToHandleUnused => expectedAmounts: ${expectedAmounts.amount0.toString()}, ${expectedAmounts.amount1.toString()}`
19127
+ `${_EkuboCLVault.name}: getSwapInfoToHandleUnused => expectedAmounts2: ${expectedAmounts.amount0.toString()}, ${expectedAmounts.amount1.toString()}`
19097
19128
  );
19098
19129
  let retry = 0;
19099
19130
  const maxRetry = 10;
@@ -19366,6 +19397,7 @@ var EkuboCLVault = class _EkuboCLVault extends BaseStrategy {
19366
19397
  const token0Info = await Global.getTokenInfoFromAddr(poolKey.token0);
19367
19398
  const token1Info = await Global.getTokenInfoFromAddr(poolKey.token1);
19368
19399
  const bounds = await this.getCurrentBounds();
19400
+ logger.verbose(`${_EkuboCLVault.name}: harvest => unClaimedRewards: ${unClaimedRewards.length}`);
19369
19401
  const calls = [];
19370
19402
  for (let claim of unClaimedRewards) {
19371
19403
  const fee = claim.claim.amount.multipliedBy(this.metadata.additionalInfo.feeBps).dividedBy(1e4);
@@ -19398,12 +19430,28 @@ var EkuboCLVault = class _EkuboCLVault extends BaseStrategy {
19398
19430
  18
19399
19431
  // cause its always STRK?
19400
19432
  );
19433
+ logger.verbose(
19434
+ `${_EkuboCLVault.name}: harvest => swap1Amount: ${swap1Amount}`
19435
+ );
19401
19436
  const remainingAmount = postFeeAmount.minus(swap1Amount);
19437
+ logger.verbose(
19438
+ `${_EkuboCLVault.name}: harvest => remainingAmount: ${remainingAmount}`
19439
+ );
19402
19440
  const swapInfo2 = {
19403
19441
  ...swapInfo,
19404
19442
  token_from_amount: uint2564.bnToUint256(remainingAmount.toWei())
19405
19443
  };
19406
19444
  swapInfo2.token_to_address = token1Info.address.address;
19445
+ logger.verbose(
19446
+ `${_EkuboCLVault.name}: harvest => swapInfo: ${JSON.stringify(
19447
+ swapInfo
19448
+ )}`
19449
+ );
19450
+ logger.verbose(
19451
+ `${_EkuboCLVault.name}: harvest => swapInfo2: ${JSON.stringify(
19452
+ swapInfo2
19453
+ )}`
19454
+ );
19407
19455
  const calldata = [
19408
19456
  claim.rewardsContract.address,
19409
19457
  {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@strkfarm/sdk",
3
- "version": "1.0.40",
3
+ "version": "1.0.41",
4
4
  "description": "STRKFarm TS SDK (Meant for our internal use, but feel free to use it)",
5
5
  "typings": "dist/index.d.ts",
6
6
  "types": "dist/index.d.ts",
@@ -795,6 +795,7 @@ export class EkuboCLVault extends BaseStrategy<
795
795
  ratio: Web3Number,
796
796
  price: number
797
797
  ) {
798
+
798
799
  // (amount0 + x) / (amount1 - y) = ratio
799
800
  // x = y * Py / Px ---- (1)
800
801
  // => (amount0 + y * Py / Px) / (amount1 - y) = ratio
@@ -807,6 +808,17 @@ export class EkuboCLVault extends BaseStrategy<
807
808
  .minus(availableAmount0)
808
809
  .dividedBy(ratio.plus(1 / price));
809
810
  const x = y.dividedBy(price);
811
+ logger.verbose(
812
+ `${EkuboCLVault.name}: _solveExpectedAmountsEq => x: ${x.toString()}, y: ${y.toString()}, amount0: ${availableAmount0.toString()}, amount1: ${availableAmount1.toString()}`
813
+ );
814
+
815
+ if (ratio.eq(0)) {
816
+ return {
817
+ amount0: Web3Number.fromWei("0", availableAmount0.decimals),
818
+ amount1: availableAmount1.minus(y),
819
+ ratio: 0,
820
+ };
821
+ }
810
822
  return {
811
823
  amount0: availableAmount0.plus(x),
812
824
  amount1: availableAmount1.minus(y),
@@ -814,10 +826,8 @@ export class EkuboCLVault extends BaseStrategy<
814
826
  };
815
827
  }
816
828
 
817
- async getSwapInfoToHandleUnused(considerRebalance: boolean = true) {
818
- const poolKey = await this.getPoolKey();
819
-
820
- // fetch current unused balances of vault
829
+ async unusedBalances(_poolKey?: EkuboPoolKey) {
830
+ const poolKey = _poolKey || (await this.getPoolKey());
821
831
  const erc20Mod = new ERC20(this.config);
822
832
  const token0Info = await Global.getTokenInfoFromAddr(poolKey.token0);
823
833
  const token1Info = await Global.getTokenInfoFromAddr(poolKey.token1);
@@ -831,6 +841,7 @@ export class EkuboCLVault extends BaseStrategy<
831
841
  this.address.address,
832
842
  token1Info.decimals
833
843
  );
844
+
834
845
  logger.verbose(
835
846
  `${
836
847
  EkuboCLVault.name
@@ -841,16 +852,39 @@ export class EkuboCLVault extends BaseStrategy<
841
852
  const token1Price = await this.pricer.getPrice(token1Info.symbol);
842
853
  const token0PriceUsd = token0Price.price * Number(token0Bal1.toFixed(13));
843
854
  const token1PriceUsd = token1Price.price * Number(token1Bal1.toFixed(13));
844
- if (token0PriceUsd > 1 && token1PriceUsd > 1) {
845
- // the swap is designed to handle one token only.
846
- // i.e. all balance should be in one token
847
- // except small amount of dust
848
- // so we need to call handle_fees first, which will atleast use
849
- // most of one token
850
- throw new Error(
851
- "Both tokens are non-zero and above $1, call handle_fees first"
852
- );
855
+
856
+ return {
857
+ token0: {
858
+ amount: token0Bal1,
859
+ tokenInfo: token0Info,
860
+ usdValue: token0PriceUsd,
861
+ },
862
+ token1: {
863
+ amount: token1Bal1,
864
+ tokenInfo: token1Info,
865
+ usdValue: token1PriceUsd,
866
+ },
853
867
  }
868
+ }
869
+
870
+ async getSwapInfoToHandleUnused(considerRebalance: boolean = true) {
871
+ const poolKey = await this.getPoolKey();
872
+
873
+ // fetch current unused balances of vault
874
+ const unusedBalances = await this.unusedBalances(poolKey);
875
+ const { amount: token0Bal1, usdValue: token0PriceUsd} = unusedBalances.token0;
876
+ const { amount: token1Bal1, usdValue: token1PriceUsd} = unusedBalances.token1;
877
+
878
+ // if (token0PriceUsd > 1 && token1PriceUsd > 1) {
879
+ // // the swap is designed to handle one token only.
880
+ // // i.e. all balance should be in one token
881
+ // // except small amount of dust
882
+ // // so we need to call handle_fees first, which will atleast use
883
+ // // most of one token
884
+ // throw new Error(
885
+ // "Both tokens are non-zero and above $1, call handle_fees first"
886
+ // );
887
+ // }
854
888
 
855
889
  let token0Bal = token0Bal1;
856
890
  let token1Bal = token1Bal1;
@@ -875,16 +909,21 @@ export class EkuboCLVault extends BaseStrategy<
875
909
  );
876
910
 
877
911
  // get expected amounts for liquidity
878
- const newBounds = await this.getNewBounds();
912
+ let ekuboBounds: EkuboBounds;
913
+ if (considerRebalance) {
914
+ ekuboBounds = await this.getNewBounds();
915
+ } else {
916
+ ekuboBounds = await this.getCurrentBounds();
917
+ }
879
918
  logger.verbose(
880
- `${EkuboCLVault.name}: getSwapInfoToHandleUnused => newBounds: ${newBounds.lowerTick}, ${newBounds.upperTick}`
919
+ `${EkuboCLVault.name}: getSwapInfoToHandleUnused => newBounds: ${ekuboBounds.lowerTick}, ${ekuboBounds.upperTick}`
881
920
  );
882
921
 
883
922
  return await this.getSwapInfoGivenAmounts(
884
923
  poolKey,
885
924
  token0Bal,
886
925
  token1Bal,
887
- newBounds
926
+ ekuboBounds
888
927
  );
889
928
  }
890
929
 
@@ -894,6 +933,9 @@ export class EkuboCLVault extends BaseStrategy<
894
933
  token1Bal: Web3Number,
895
934
  bounds: EkuboBounds
896
935
  ): Promise<SwapInfo> {
936
+ logger.verbose(
937
+ `${EkuboCLVault.name}: getSwapInfoGivenAmounts::pre => token0Bal: ${token0Bal.toString()}, token1Bal: ${token1Bal.toString()}`
938
+ );
897
939
  let expectedAmounts = await this._getExpectedAmountsForLiquidity(
898
940
  token0Bal,
899
941
  token1Bal,
@@ -902,7 +944,7 @@ export class EkuboCLVault extends BaseStrategy<
902
944
  logger.verbose(
903
945
  `${
904
946
  EkuboCLVault.name
905
- }: getSwapInfoToHandleUnused => expectedAmounts: ${expectedAmounts.amount0.toString()}, ${expectedAmounts.amount1.toString()}`
947
+ }: getSwapInfoToHandleUnused => expectedAmounts2: ${expectedAmounts.amount0.toString()}, ${expectedAmounts.amount1.toString()}`
906
948
  );
907
949
 
908
950
  // get swap info
@@ -1266,6 +1308,7 @@ export class EkuboCLVault extends BaseStrategy<
1266
1308
  const token0Info = await Global.getTokenInfoFromAddr(poolKey.token0);
1267
1309
  const token1Info = await Global.getTokenInfoFromAddr(poolKey.token1);
1268
1310
  const bounds = await this.getCurrentBounds();
1311
+ logger.verbose(`${EkuboCLVault.name}: harvest => unClaimedRewards: ${unClaimedRewards.length}`);
1269
1312
  const calls: Call[] = [];
1270
1313
  for (let claim of unClaimedRewards) {
1271
1314
  const fee = claim.claim.amount
@@ -1279,6 +1322,8 @@ export class EkuboCLVault extends BaseStrategy<
1279
1322
  EkuboCLVault.name
1280
1323
  }: harvest => Processing claim, isToken1: ${isToken1} amount: ${postFeeAmount.toWei()}`
1281
1324
  );
1325
+
1326
+ // todo what if the claim token is neither token0 or token1
1282
1327
  const token0Amt = isToken1
1283
1328
  ? new Web3Number(0, token0Info.decimals)
1284
1329
  : postFeeAmount;
@@ -1310,12 +1355,28 @@ export class EkuboCLVault extends BaseStrategy<
1310
1355
  uint256.uint256ToBN(swapInfo1.token_from_amount).toString(),
1311
1356
  18, // cause its always STRK?
1312
1357
  );
1358
+ logger.verbose(
1359
+ `${EkuboCLVault.name}: harvest => swap1Amount: ${swap1Amount}`
1360
+ );
1313
1361
  const remainingAmount = postFeeAmount.minus(swap1Amount);
1362
+ logger.verbose(
1363
+ `${EkuboCLVault.name}: harvest => remainingAmount: ${remainingAmount}`
1364
+ );
1314
1365
  const swapInfo2 = {
1315
1366
  ...swapInfo,
1316
1367
  token_from_amount: uint256.bnToUint256(remainingAmount.toWei()),
1317
1368
  };
1318
1369
  swapInfo2.token_to_address = token1Info.address.address;
1370
+ logger.verbose(
1371
+ `${EkuboCLVault.name}: harvest => swapInfo: ${JSON.stringify(
1372
+ swapInfo
1373
+ )}`
1374
+ );
1375
+ logger.verbose(
1376
+ `${EkuboCLVault.name}: harvest => swapInfo2: ${JSON.stringify(
1377
+ swapInfo2
1378
+ )}`
1379
+ );
1319
1380
  const calldata = [
1320
1381
  claim.rewardsContract.address,
1321
1382
  {