@gearbox-protocol/sdk 13.0.0-next.13 → 13.0.0-next.15

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.
@@ -52,6 +52,7 @@ class AccountOpener extends import_sdk.SDKConstruct {
52
52
  #minDebtMultiplier;
53
53
  #allowMint;
54
54
  #leverageDelta;
55
+ #poolService;
55
56
  constructor(service, options_ = {}) {
56
57
  super(service.sdk);
57
58
  const {
@@ -77,6 +78,7 @@ class AccountOpener extends import_sdk.SDKConstruct {
77
78
  this.#poolDepositMultiplier = BigInt(poolDepositMultiplier);
78
79
  this.#minDebtMultiplier = BigInt(minDebtMultiplier);
79
80
  this.#leverageDelta = BigInt(leverageDelta);
81
+ this.#poolService = new import_sdk.PoolService(service.sdk);
80
82
  this.#logger?.info(
81
83
  {
82
84
  borrower: (0, import_accounts.privateKeyToAccount)(this.borrowerKey).address,
@@ -114,6 +116,10 @@ class AccountOpener extends import_sdk.SDKConstruct {
114
116
  },
115
117
  "opening credit accounts"
116
118
  );
119
+ await Promise.all([
120
+ this.sdk.tokensMeta.loadTokenData(),
121
+ this.sdk.marketRegister.loadZappers()
122
+ ]);
117
123
  let deposits = [];
118
124
  if (depositIntoPools) {
119
125
  try {
@@ -465,9 +471,34 @@ class AccountOpener extends import_sdk.SDKConstruct {
465
471
  this.#logger?.debug(
466
472
  `depositor balance in underlying: ${this.sdk.tokensMeta.formatBN(pool.underlying, allowance, { symbol: true })}`
467
473
  );
474
+ const tokensOut = this.#poolService.getDepositTokensOut(
475
+ address,
476
+ underlying
477
+ );
478
+ this.#logger?.debug(
479
+ { tokensOut: tokensOut.map((t) => this.labelAddress(t)) },
480
+ "deposit tokens out"
481
+ );
482
+ if (tokensOut.length === 0) {
483
+ throw new Error(`no tokens out found for pool ${poolName}`);
484
+ }
485
+ const tokenOut = tokensOut[0];
486
+ const metadata = this.#poolService.getDepositMetadata(
487
+ address,
488
+ underlying,
489
+ tokenOut
490
+ );
491
+ this.logger?.debug(
492
+ {
493
+ underlying: this.labelAddress(underlying),
494
+ tokenOut: this.labelAddress(tokenOut),
495
+ ...metadata
496
+ },
497
+ "pool deposit metadata"
498
+ );
468
499
  txHash = await this.#anvil.writeContract({
469
500
  account: depositor,
470
- address: underlying,
501
+ address: metadata.approveTarget,
471
502
  abi: import_iERC20.ierc20Abi,
472
503
  functionName: "approve",
473
504
  args: [address, allowance],
@@ -484,12 +515,21 @@ class AccountOpener extends import_sdk.SDKConstruct {
484
515
  this.#logger?.debug(
485
516
  `depositor approved underlying for pool ${poolName}: ${txHash}`
486
517
  );
518
+ const depositCall = this.#poolService.addLiquidity({
519
+ collateral: { token: underlying, balance: amount },
520
+ pool: address,
521
+ wallet: depositor.address,
522
+ meta: metadata
523
+ });
524
+ if (!depositCall) {
525
+ throw new Error(`no deposit call could be created for ${poolName}`);
526
+ }
487
527
  txHash = await this.#anvil.writeContract({
488
528
  account: depositor,
489
- address,
490
- abi: import_v300.iPoolV300Abi,
491
- functionName: "deposit",
492
- args: [amount, depositor.address],
529
+ address: depositCall.target,
530
+ abi: depositCall.abi,
531
+ functionName: depositCall.functionName,
532
+ args: depositCall.args,
493
533
  chain: this.#anvil.chain
494
534
  });
495
535
  receipt = await this.#anvil.waitForTransactionReceipt({ hash: txHash });
@@ -817,7 +817,7 @@ class AbstractCreditAccountService extends import_base.SDKConstruct {
817
817
  collateral,
818
818
  permits,
819
819
  debt,
820
- withdrawDebt,
820
+ withdrawToken,
821
821
  referralCode,
822
822
  to,
823
823
  calls: openPathCalls,
@@ -826,6 +826,12 @@ class AbstractCreditAccountService extends import_base.SDKConstruct {
826
826
  }) {
827
827
  const cmSuite = this.sdk.marketRegister.findCreditManager(creditManager);
828
828
  const cm = cmSuite.creditManager;
829
+ let tokenToWithdraw;
830
+ if (withdrawToken === true) {
831
+ tokenToWithdraw = cm.underlying;
832
+ } else if (typeof withdrawToken === "string") {
833
+ tokenToWithdraw = withdrawToken;
834
+ }
829
835
  const priceUpdatesCalls = await this.getPriceUpdatesForFacade({
830
836
  creditManager: cm.address,
831
837
  desiredQuotas: averageQuota
@@ -835,7 +841,15 @@ class AbstractCreditAccountService extends import_base.SDKConstruct {
835
841
  this.#prepareIncreaseDebt(cm.creditFacade, debt),
836
842
  ...this.prepareAddCollateral(cm.creditFacade, collateral, permits),
837
843
  ...openPathCalls,
838
- ...withdrawDebt ? [this.prepareWithdrawToken(cm.creditFacade, cm.underlying, debt, to)] : [],
844
+ // путь из underlying в withdrawal token
845
+ ...tokenToWithdraw ? [
846
+ this.prepareWithdrawToken(
847
+ cm.creditFacade,
848
+ tokenToWithdraw,
849
+ import_constants.MAX_UINT256,
850
+ to
851
+ )
852
+ ] : [],
839
853
  ...this.prepareUpdateQuotas(cm.creditFacade, {
840
854
  minQuota,
841
855
  averageQuota
@@ -7,7 +7,7 @@ import {
7
7
  } from "viem";
8
8
  import { generatePrivateKey, privateKeyToAccount } from "viem/accounts";
9
9
  import { ierc20Abi } from "../abi/iERC20.js";
10
- import { iCreditFacadeV300Abi, iPoolV300Abi } from "../abi/v300.js";
10
+ import { iCreditFacadeV300Abi } from "../abi/v300.js";
11
11
  import {
12
12
  ADDRESS_0X0,
13
13
  AddressMap,
@@ -15,6 +15,7 @@ import {
15
15
  childLogger,
16
16
  MAX_UINT256,
17
17
  PERCENTAGE_FACTOR,
18
+ PoolService,
18
19
  SDKConstruct,
19
20
  sendRawTx
20
21
  } from "../sdk/index.js";
@@ -43,6 +44,7 @@ class AccountOpener extends SDKConstruct {
43
44
  #minDebtMultiplier;
44
45
  #allowMint;
45
46
  #leverageDelta;
47
+ #poolService;
46
48
  constructor(service, options_ = {}) {
47
49
  super(service.sdk);
48
50
  const {
@@ -68,6 +70,7 @@ class AccountOpener extends SDKConstruct {
68
70
  this.#poolDepositMultiplier = BigInt(poolDepositMultiplier);
69
71
  this.#minDebtMultiplier = BigInt(minDebtMultiplier);
70
72
  this.#leverageDelta = BigInt(leverageDelta);
73
+ this.#poolService = new PoolService(service.sdk);
71
74
  this.#logger?.info(
72
75
  {
73
76
  borrower: privateKeyToAccount(this.borrowerKey).address,
@@ -105,6 +108,10 @@ class AccountOpener extends SDKConstruct {
105
108
  },
106
109
  "opening credit accounts"
107
110
  );
111
+ await Promise.all([
112
+ this.sdk.tokensMeta.loadTokenData(),
113
+ this.sdk.marketRegister.loadZappers()
114
+ ]);
108
115
  let deposits = [];
109
116
  if (depositIntoPools) {
110
117
  try {
@@ -456,9 +463,34 @@ class AccountOpener extends SDKConstruct {
456
463
  this.#logger?.debug(
457
464
  `depositor balance in underlying: ${this.sdk.tokensMeta.formatBN(pool.underlying, allowance, { symbol: true })}`
458
465
  );
466
+ const tokensOut = this.#poolService.getDepositTokensOut(
467
+ address,
468
+ underlying
469
+ );
470
+ this.#logger?.debug(
471
+ { tokensOut: tokensOut.map((t) => this.labelAddress(t)) },
472
+ "deposit tokens out"
473
+ );
474
+ if (tokensOut.length === 0) {
475
+ throw new Error(`no tokens out found for pool ${poolName}`);
476
+ }
477
+ const tokenOut = tokensOut[0];
478
+ const metadata = this.#poolService.getDepositMetadata(
479
+ address,
480
+ underlying,
481
+ tokenOut
482
+ );
483
+ this.logger?.debug(
484
+ {
485
+ underlying: this.labelAddress(underlying),
486
+ tokenOut: this.labelAddress(tokenOut),
487
+ ...metadata
488
+ },
489
+ "pool deposit metadata"
490
+ );
459
491
  txHash = await this.#anvil.writeContract({
460
492
  account: depositor,
461
- address: underlying,
493
+ address: metadata.approveTarget,
462
494
  abi: ierc20Abi,
463
495
  functionName: "approve",
464
496
  args: [address, allowance],
@@ -475,12 +507,21 @@ class AccountOpener extends SDKConstruct {
475
507
  this.#logger?.debug(
476
508
  `depositor approved underlying for pool ${poolName}: ${txHash}`
477
509
  );
510
+ const depositCall = this.#poolService.addLiquidity({
511
+ collateral: { token: underlying, balance: amount },
512
+ pool: address,
513
+ wallet: depositor.address,
514
+ meta: metadata
515
+ });
516
+ if (!depositCall) {
517
+ throw new Error(`no deposit call could be created for ${poolName}`);
518
+ }
478
519
  txHash = await this.#anvil.writeContract({
479
520
  account: depositor,
480
- address,
481
- abi: iPoolV300Abi,
482
- functionName: "deposit",
483
- args: [amount, depositor.address],
521
+ address: depositCall.target,
522
+ abi: depositCall.abi,
523
+ functionName: depositCall.functionName,
524
+ args: depositCall.args,
484
525
  chain: this.#anvil.chain
485
526
  });
486
527
  receipt = await this.#anvil.waitForTransactionReceipt({ hash: txHash });
@@ -809,7 +809,7 @@ class AbstractCreditAccountService extends SDKConstruct {
809
809
  collateral,
810
810
  permits,
811
811
  debt,
812
- withdrawDebt,
812
+ withdrawToken,
813
813
  referralCode,
814
814
  to,
815
815
  calls: openPathCalls,
@@ -818,6 +818,12 @@ class AbstractCreditAccountService extends SDKConstruct {
818
818
  }) {
819
819
  const cmSuite = this.sdk.marketRegister.findCreditManager(creditManager);
820
820
  const cm = cmSuite.creditManager;
821
+ let tokenToWithdraw;
822
+ if (withdrawToken === true) {
823
+ tokenToWithdraw = cm.underlying;
824
+ } else if (typeof withdrawToken === "string") {
825
+ tokenToWithdraw = withdrawToken;
826
+ }
821
827
  const priceUpdatesCalls = await this.getPriceUpdatesForFacade({
822
828
  creditManager: cm.address,
823
829
  desiredQuotas: averageQuota
@@ -827,7 +833,15 @@ class AbstractCreditAccountService extends SDKConstruct {
827
833
  this.#prepareIncreaseDebt(cm.creditFacade, debt),
828
834
  ...this.prepareAddCollateral(cm.creditFacade, collateral, permits),
829
835
  ...openPathCalls,
830
- ...withdrawDebt ? [this.prepareWithdrawToken(cm.creditFacade, cm.underlying, debt, to)] : [],
836
+ // путь из underlying в withdrawal token
837
+ ...tokenToWithdraw ? [
838
+ this.prepareWithdrawToken(
839
+ cm.creditFacade,
840
+ tokenToWithdraw,
841
+ MAX_UINT256,
842
+ to
843
+ )
844
+ ] : [],
831
845
  ...this.prepareUpdateQuotas(cm.creditFacade, {
832
846
  minQuota,
833
847
  averageQuota
@@ -173,7 +173,7 @@ export declare abstract class AbstractCreditAccountService extends SDKConstruct
173
173
  * @param {Array<Asset>} minQuota - minimum quota for tokens after open {@link Asset}
174
174
  * @returns All necessary data to execute the transaction (call, credit facade)
175
175
  */
176
- openCA({ ethAmount, creditManager, collateral, permits, debt, withdrawDebt, referralCode, to, calls: openPathCalls, minQuota, averageQuota, }: OpenCAProps): Promise<CreditAccountOperationResult>;
176
+ openCA({ ethAmount, creditManager, collateral, permits, debt, withdrawToken, referralCode, to, calls: openPathCalls, minQuota, averageQuota, }: OpenCAProps): Promise<CreditAccountOperationResult>;
177
177
  /**
178
178
  * Returns borrow rate with 4 digits precision (10000 = 100%)
179
179
  * @param ca
@@ -275,8 +275,9 @@ export interface OpenCAProps extends PrepareUpdateQuotasProps {
275
275
  /**
276
276
  * Flag to withdraw debt to wallet after opening credit account;
277
277
  * used for borrowing functionality
278
+ * If true, will withdraw underlying token, otherwise will withdraw specified token
278
279
  */
279
- withdrawDebt?: boolean;
280
+ withdrawToken?: boolean | Address;
280
281
  /**
281
282
  * Permits of collateral tokens (in any permittable token is present) {@link PermitResult}
282
283
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gearbox-protocol/sdk",
3
- "version": "13.0.0-next.13",
3
+ "version": "13.0.0-next.15",
4
4
  "description": "Gearbox SDK",
5
5
  "license": "MIT",
6
6
  "main": "./dist/cjs/sdk/index.js",