@gearbox-protocol/sdk 3.0.0-vfour.157 → 3.0.0-vfour.159

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.
@@ -3,6 +3,7 @@
3
3
  var viem = require('viem');
4
4
  var accounts = require('viem/accounts');
5
5
  var sdk = require('../sdk');
6
+ var constants = require('../sdk/constants');
6
7
  var promises = require('fs/promises');
7
8
 
8
9
  // src/dev/AccountOpener.ts
@@ -126,13 +127,32 @@ var AccountOpener = class {
126
127
  slippage,
127
128
  target: collateral
128
129
  });
129
- logger?.debug("found open strategy");
130
+ logger?.debug(strategy, "found open strategy");
131
+ const debt = minDebt * BigInt(leverage - 1);
130
132
  const { tx, calls } = await this.#service.openCA({
131
133
  creditManager: cm.creditManager.address,
132
- averageQuota: [],
133
- minQuota: [],
134
+ averageQuota: [
135
+ {
136
+ token: collateral,
137
+ balance: this.#calcQuota(
138
+ strategy.amount,
139
+ debt,
140
+ BigInt(cm.collateralTokens[collateral])
141
+ )
142
+ }
143
+ ],
144
+ minQuota: [
145
+ {
146
+ token: collateral,
147
+ balance: this.#calcQuota(
148
+ strategy.minAmount,
149
+ debt,
150
+ BigInt(cm.collateralTokens[collateral])
151
+ )
152
+ }
153
+ ],
134
154
  collateral: [{ token: underlying, balance: minDebt }],
135
- debt: minDebt * BigInt(leverage - 1),
155
+ debt,
136
156
  calls: strategy.calls,
137
157
  ethAmount: 0n,
138
158
  permits: {},
@@ -231,7 +251,21 @@ var AccountOpener = class {
231
251
  }
232
252
  async #approve(token, cm) {
233
253
  const borrower = await this.#getBorrower();
254
+ const symbol = this.#service.sdk.tokensMeta.symbol(token);
234
255
  try {
256
+ if (symbol === "USDT") {
257
+ const hash2 = await this.#anvil.writeContract({
258
+ account: borrower,
259
+ address: token,
260
+ abi: sdk.ierc20Abi,
261
+ functionName: "approve",
262
+ args: [cm.creditManager.address, 0n],
263
+ chain: this.#anvil.chain
264
+ });
265
+ await this.#anvil.waitForTransactionReceipt({
266
+ hash: hash2
267
+ });
268
+ }
235
269
  const hash = await this.#anvil.writeContract({
236
270
  account: borrower,
237
271
  address: token,
@@ -245,16 +279,16 @@ var AccountOpener = class {
245
279
  });
246
280
  if (receipt.status === "reverted") {
247
281
  this.#logger?.error(
248
- `failed to allowed credit manager ${cm.creditManager.name} to spend ${token}, tx reverted: ${hash}`
282
+ `failed to allowed credit manager ${cm.creditManager.name} to spend ${symbol} (${token}), tx reverted: ${hash}`
249
283
  );
250
284
  } else {
251
285
  this.#logger?.debug(
252
- `allowed credit manager ${cm.creditManager.name} to spend ${token}, tx: ${hash}`
286
+ `allowed credit manager ${cm.creditManager.name} to spend ${symbol} (${token}), tx: ${hash}`
253
287
  );
254
288
  }
255
289
  } catch (e) {
256
290
  this.#logger?.error(
257
- `failed to allowed credit manager ${cm.creditManager.name} to spend ${token}: ${e}`
291
+ `failed to allowed credit manager ${cm.creditManager.name} to spend ${symbol} (${token}): ${e}`
258
292
  );
259
293
  }
260
294
  }
@@ -317,6 +351,12 @@ var AccountOpener = class {
317
351
  }
318
352
  return this.#borrower;
319
353
  }
354
+ #calcQuota(amount, debt, lt) {
355
+ let quota = amount * lt / constants.PERCENTAGE_FACTOR;
356
+ quota = debt < quota ? debt : quota;
357
+ quota = quota * (constants.PERCENTAGE_FACTOR + 500n) / constants.PERCENTAGE_FACTOR;
358
+ return quota / constants.PERCENTAGE_FACTOR * constants.PERCENTAGE_FACTOR;
359
+ }
320
360
  get sdk() {
321
361
  return this.#service.sdk;
322
362
  }
@@ -1,6 +1,7 @@
1
1
  import { createTestClient, publicActions, walletActions, toHex, isAddress, parseEther, createWalletClient, http, createPublicClient } from 'viem';
2
2
  import { privateKeyToAccount, generatePrivateKey } from 'viem/accounts';
3
- import { childLogger, sendRawTx, ADDRESS_0X0, formatBN, ierc20Abi, MAX_UINT256, iDegenNftv2Abi, PERCENTAGE_FACTOR, GearboxSDK, json_stringify, WAD } from '../sdk/index.mjs';
3
+ import { childLogger, sendRawTx, ADDRESS_0X0, formatBN, ierc20Abi, MAX_UINT256, iDegenNftv2Abi, PERCENTAGE_FACTOR as PERCENTAGE_FACTOR$1, GearboxSDK, json_stringify, WAD } from '../sdk/index.mjs';
4
+ import { PERCENTAGE_FACTOR } from '../sdk/constants';
4
5
  import { writeFile, readFile } from 'node:fs/promises';
5
6
 
6
7
  // src/dev/AccountOpener.ts
@@ -124,13 +125,32 @@ var AccountOpener = class {
124
125
  slippage,
125
126
  target: collateral
126
127
  });
127
- logger?.debug("found open strategy");
128
+ logger?.debug(strategy, "found open strategy");
129
+ const debt = minDebt * BigInt(leverage - 1);
128
130
  const { tx, calls } = await this.#service.openCA({
129
131
  creditManager: cm.creditManager.address,
130
- averageQuota: [],
131
- minQuota: [],
132
+ averageQuota: [
133
+ {
134
+ token: collateral,
135
+ balance: this.#calcQuota(
136
+ strategy.amount,
137
+ debt,
138
+ BigInt(cm.collateralTokens[collateral])
139
+ )
140
+ }
141
+ ],
142
+ minQuota: [
143
+ {
144
+ token: collateral,
145
+ balance: this.#calcQuota(
146
+ strategy.minAmount,
147
+ debt,
148
+ BigInt(cm.collateralTokens[collateral])
149
+ )
150
+ }
151
+ ],
132
152
  collateral: [{ token: underlying, balance: minDebt }],
133
- debt: minDebt * BigInt(leverage - 1),
153
+ debt,
134
154
  calls: strategy.calls,
135
155
  ethAmount: 0n,
136
156
  permits: {},
@@ -229,7 +249,21 @@ var AccountOpener = class {
229
249
  }
230
250
  async #approve(token, cm) {
231
251
  const borrower = await this.#getBorrower();
252
+ const symbol = this.#service.sdk.tokensMeta.symbol(token);
232
253
  try {
254
+ if (symbol === "USDT") {
255
+ const hash2 = await this.#anvil.writeContract({
256
+ account: borrower,
257
+ address: token,
258
+ abi: ierc20Abi,
259
+ functionName: "approve",
260
+ args: [cm.creditManager.address, 0n],
261
+ chain: this.#anvil.chain
262
+ });
263
+ await this.#anvil.waitForTransactionReceipt({
264
+ hash: hash2
265
+ });
266
+ }
233
267
  const hash = await this.#anvil.writeContract({
234
268
  account: borrower,
235
269
  address: token,
@@ -243,16 +277,16 @@ var AccountOpener = class {
243
277
  });
244
278
  if (receipt.status === "reverted") {
245
279
  this.#logger?.error(
246
- `failed to allowed credit manager ${cm.creditManager.name} to spend ${token}, tx reverted: ${hash}`
280
+ `failed to allowed credit manager ${cm.creditManager.name} to spend ${symbol} (${token}), tx reverted: ${hash}`
247
281
  );
248
282
  } else {
249
283
  this.#logger?.debug(
250
- `allowed credit manager ${cm.creditManager.name} to spend ${token}, tx: ${hash}`
284
+ `allowed credit manager ${cm.creditManager.name} to spend ${symbol} (${token}), tx: ${hash}`
251
285
  );
252
286
  }
253
287
  } catch (e) {
254
288
  this.#logger?.error(
255
- `failed to allowed credit manager ${cm.creditManager.name} to spend ${token}: ${e}`
289
+ `failed to allowed credit manager ${cm.creditManager.name} to spend ${symbol} (${token}): ${e}`
256
290
  );
257
291
  }
258
292
  }
@@ -315,6 +349,12 @@ var AccountOpener = class {
315
349
  }
316
350
  return this.#borrower;
317
351
  }
352
+ #calcQuota(amount, debt, lt) {
353
+ let quota = amount * lt / PERCENTAGE_FACTOR;
354
+ quota = debt < quota ? debt : quota;
355
+ quota = quota * (PERCENTAGE_FACTOR + 500n) / PERCENTAGE_FACTOR;
356
+ return quota / PERCENTAGE_FACTOR * PERCENTAGE_FACTOR;
357
+ }
318
358
  get sdk() {
319
359
  return this.#service.sdk;
320
360
  }
@@ -328,7 +368,7 @@ async function calcLiquidatableLTs(sdk, ca, factor = 9990n, logger) {
328
368
  const lt = BigInt(cm.collateralTokens[token]);
329
369
  return {
330
370
  token,
331
- weightedBalance: balanceU * lt / PERCENTAGE_FACTOR,
371
+ weightedBalance: balanceU * lt / PERCENTAGE_FACTOR$1,
332
372
  lt
333
373
  };
334
374
  }).sort((a, b) => {
@@ -337,7 +377,7 @@ async function calcLiquidatableLTs(sdk, ca, factor = 9990n, logger) {
337
377
  return b.weightedBalance > a.weightedBalance ? 1 : -1;
338
378
  });
339
379
  let divisor = 0n;
340
- let dividend = factor * (ca.debt + ca.accruedInterest + ca.accruedFees) / PERCENTAGE_FACTOR;
380
+ let dividend = factor * (ca.debt + ca.accruedInterest + ca.accruedFees) / PERCENTAGE_FACTOR$1;
341
381
  for (const { token, weightedBalance } of weightedBalances) {
342
382
  if (token === ca.underlying) {
343
383
  dividend -= weightedBalance;
@@ -1900,7 +1940,7 @@ async function setLTZero(anvil, cm, logger) {
1900
1940
  args: [
1901
1941
  cm.feeInterest,
1902
1942
  cm.liquidationDiscount - 1,
1903
- Number(PERCENTAGE_FACTOR) - cm.liquidationDiscount,
1943
+ Number(PERCENTAGE_FACTOR$1) - cm.liquidationDiscount,
1904
1944
  cm.feeLiquidationExpired,
1905
1945
  cm.liquidationDiscountExpired
1906
1946
  ]
@@ -1916,7 +1956,7 @@ async function setLTZero(anvil, cm, logger) {
1916
1956
  args: [
1917
1957
  cm.feeInterest,
1918
1958
  cm.feeLiquidation,
1919
- Number(PERCENTAGE_FACTOR) - cm.liquidationDiscount,
1959
+ Number(PERCENTAGE_FACTOR$1) - cm.liquidationDiscount,
1920
1960
  cm.feeLiquidationExpired,
1921
1961
  cm.liquidationDiscountExpired
1922
1962
  ]
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gearbox-protocol/sdk",
3
- "version": "3.0.0-vfour.157",
3
+ "version": "3.0.0-vfour.159",
4
4
  "description": "Gearbox SDK",
5
5
  "license": "MIT",
6
6
  "main": "./dist/cjs/sdk/index.cjs",