@gearbox-protocol/sdk 9.3.1 → 9.4.0

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.
@@ -162,28 +162,39 @@ class AccountOpener extends import_sdk.SDKConstruct {
162
162
  });
163
163
  logger?.debug(`opening account #${index}/${total}`);
164
164
  const borrower = await this.#getBorrower();
165
- const tx = await this.prepareOpen(input);
165
+ const preview = await this.prepareOpen(input);
166
+ const humanizedInput = this.#humanizePreview(input, preview);
166
167
  let hash;
167
168
  try {
168
169
  hash = await (0, import_sdk.sendRawTx)(this.#anvil, {
169
- tx,
170
+ tx: preview.tx,
170
171
  account: borrower
171
172
  });
172
173
  logger?.debug(`send transaction ${hash}`);
173
174
  } catch (e) {
174
175
  return {
175
176
  input,
177
+ humanizedInput,
176
178
  error: e,
177
- rawTx: tx
179
+ rawTx: {
180
+ to: preview.tx.to,
181
+ callData: preview.tx.callData,
182
+ value: preview.tx.value
183
+ }
178
184
  };
179
185
  }
180
186
  const receipt = await this.#anvil.waitForTransactionReceipt({ hash });
181
187
  if (receipt.status === "reverted") {
182
188
  return {
183
189
  input,
190
+ humanizedInput,
184
191
  error: new OpenTxRevertedError(hash),
185
192
  txHash: hash,
186
- rawTx: tx
193
+ rawTx: {
194
+ to: preview.tx.to,
195
+ callData: preview.tx.callData,
196
+ value: preview.tx.value
197
+ }
187
198
  };
188
199
  }
189
200
  logger?.info(`opened credit account ${index}/${total}`);
@@ -208,8 +219,13 @@ class AccountOpener extends import_sdk.SDKConstruct {
208
219
  }
209
220
  return {
210
221
  input,
222
+ humanizedInput,
211
223
  txHash: hash,
212
- rawTx: tx,
224
+ rawTx: {
225
+ to: preview.tx.to,
226
+ callData: preview.tx.callData,
227
+ value: preview.tx.value
228
+ },
213
229
  account
214
230
  };
215
231
  }
@@ -318,7 +334,14 @@ class AccountOpener extends import_sdk.SDKConstruct {
318
334
  );
319
335
  }
320
336
  logger?.debug("prepared open account transaction");
321
- return tx;
337
+ return {
338
+ tx,
339
+ leverage,
340
+ collateral: { token: underlying, balance: minDebt },
341
+ minQuota,
342
+ averageQuota,
343
+ slippage
344
+ };
322
345
  }
323
346
  async getOpenedAccounts() {
324
347
  return await this.#service.getCreditAccounts({
@@ -838,6 +861,19 @@ class AccountOpener extends import_sdk.SDKConstruct {
838
861
  result = BigInt(Math.floor(Number(result) / 1e3) * 1e3);
839
862
  return result;
840
863
  }
864
+ #humanizePreview(input, preview) {
865
+ return {
866
+ creditManager: this.sdk.labelAddress(input.creditManager),
867
+ target: this.sdk.labelAddress(input.target),
868
+ leverage: Number(preview.leverage),
869
+ slippage: preview.slippage,
870
+ collateral: this.sdk.tokensMeta.formatBN(preview.collateral, {
871
+ symbol: true
872
+ }),
873
+ minQuota: preview.minQuota.filter((q) => q.balance > 10n).map((q) => this.sdk.tokensMeta.formatBN(q, { symbol: true })),
874
+ averageQuota: preview.averageQuota.filter((q) => q.balance > 10n).map((q) => this.sdk.tokensMeta.formatBN(q, { symbol: true }))
875
+ };
876
+ }
841
877
  get faucet() {
842
878
  if (!this.#faucet) {
843
879
  throw new Error("faucet not found");
@@ -32,8 +32,10 @@ class TokensMeta extends import_utils.AddressMap {
32
32
  formatBN(arg0, arg1, arg2) {
33
33
  const token = typeof arg0 === "object" ? arg0.token : arg0;
34
34
  const amount = typeof arg0 === "object" ? arg0.balance : arg1;
35
- const precision = typeof arg0 === "object" ? arg1 : arg2;
36
- return (0, import_utils.formatBN)(amount, this.decimals(token), precision);
35
+ const options = typeof arg0 === "object" ? arg1 : arg2;
36
+ const { precision, symbol } = options ?? {};
37
+ const asStr = (0, import_utils.formatBN)(amount, this.decimals(token), precision);
38
+ return symbol ? `${asStr} ${this.symbol(token)}` : asStr;
37
39
  }
38
40
  findBySymbol(symbol) {
39
41
  return this.values().find((v) => v.symbol === symbol);
@@ -155,28 +155,39 @@ class AccountOpener extends SDKConstruct {
155
155
  });
156
156
  logger?.debug(`opening account #${index}/${total}`);
157
157
  const borrower = await this.#getBorrower();
158
- const tx = await this.prepareOpen(input);
158
+ const preview = await this.prepareOpen(input);
159
+ const humanizedInput = this.#humanizePreview(input, preview);
159
160
  let hash;
160
161
  try {
161
162
  hash = await sendRawTx(this.#anvil, {
162
- tx,
163
+ tx: preview.tx,
163
164
  account: borrower
164
165
  });
165
166
  logger?.debug(`send transaction ${hash}`);
166
167
  } catch (e) {
167
168
  return {
168
169
  input,
170
+ humanizedInput,
169
171
  error: e,
170
- rawTx: tx
172
+ rawTx: {
173
+ to: preview.tx.to,
174
+ callData: preview.tx.callData,
175
+ value: preview.tx.value
176
+ }
171
177
  };
172
178
  }
173
179
  const receipt = await this.#anvil.waitForTransactionReceipt({ hash });
174
180
  if (receipt.status === "reverted") {
175
181
  return {
176
182
  input,
183
+ humanizedInput,
177
184
  error: new OpenTxRevertedError(hash),
178
185
  txHash: hash,
179
- rawTx: tx
186
+ rawTx: {
187
+ to: preview.tx.to,
188
+ callData: preview.tx.callData,
189
+ value: preview.tx.value
190
+ }
180
191
  };
181
192
  }
182
193
  logger?.info(`opened credit account ${index}/${total}`);
@@ -201,8 +212,13 @@ class AccountOpener extends SDKConstruct {
201
212
  }
202
213
  return {
203
214
  input,
215
+ humanizedInput,
204
216
  txHash: hash,
205
- rawTx: tx,
217
+ rawTx: {
218
+ to: preview.tx.to,
219
+ callData: preview.tx.callData,
220
+ value: preview.tx.value
221
+ },
206
222
  account
207
223
  };
208
224
  }
@@ -311,7 +327,14 @@ class AccountOpener extends SDKConstruct {
311
327
  );
312
328
  }
313
329
  logger?.debug("prepared open account transaction");
314
- return tx;
330
+ return {
331
+ tx,
332
+ leverage,
333
+ collateral: { token: underlying, balance: minDebt },
334
+ minQuota,
335
+ averageQuota,
336
+ slippage
337
+ };
315
338
  }
316
339
  async getOpenedAccounts() {
317
340
  return await this.#service.getCreditAccounts({
@@ -831,6 +854,19 @@ class AccountOpener extends SDKConstruct {
831
854
  result = BigInt(Math.floor(Number(result) / 1e3) * 1e3);
832
855
  return result;
833
856
  }
857
+ #humanizePreview(input, preview) {
858
+ return {
859
+ creditManager: this.sdk.labelAddress(input.creditManager),
860
+ target: this.sdk.labelAddress(input.target),
861
+ leverage: Number(preview.leverage),
862
+ slippage: preview.slippage,
863
+ collateral: this.sdk.tokensMeta.formatBN(preview.collateral, {
864
+ symbol: true
865
+ }),
866
+ minQuota: preview.minQuota.filter((q) => q.balance > 10n).map((q) => this.sdk.tokensMeta.formatBN(q, { symbol: true })),
867
+ averageQuota: preview.averageQuota.filter((q) => q.balance > 10n).map((q) => this.sdk.tokensMeta.formatBN(q, { symbol: true }))
868
+ };
869
+ }
834
870
  get faucet() {
835
871
  if (!this.#faucet) {
836
872
  throw new Error("faucet not found");
@@ -9,8 +9,10 @@ class TokensMeta extends AddressMap {
9
9
  formatBN(arg0, arg1, arg2) {
10
10
  const token = typeof arg0 === "object" ? arg0.token : arg0;
11
11
  const amount = typeof arg0 === "object" ? arg0.balance : arg1;
12
- const precision = typeof arg0 === "object" ? arg1 : arg2;
13
- return formatBN(amount, this.decimals(token), precision);
12
+ const options = typeof arg0 === "object" ? arg1 : arg2;
13
+ const { precision, symbol } = options ?? {};
14
+ const asStr = formatBN(amount, this.decimals(token), precision);
15
+ return symbol ? `${asStr} ${this.symbol(token)}` : asStr;
14
16
  }
15
17
  findBySymbol(symbol) {
16
18
  return this.values().find((v) => v.symbol === symbol);
@@ -1,6 +1,6 @@
1
1
  import type { Address, Hash, Hex, PrivateKeyAccount } from "viem";
2
2
  import { BaseError } from "viem";
3
- import type { CreditAccountData, ICreditAccountsService, RawTx } from "../sdk/index.js";
3
+ import type { Asset, CreditAccountData, ICreditAccountsService, RawTx } from "../sdk/index.js";
4
4
  import { SDKConstruct } from "../sdk/index.js";
5
5
  export declare class OpenTxRevertedError extends BaseError {
6
6
  readonly txHash: Hash;
@@ -42,11 +42,35 @@ export interface TargetAccount {
42
42
  */
43
43
  slippage?: number;
44
44
  }
45
+ interface OpenAccountPreview {
46
+ tx: RawTx;
47
+ /**
48
+ * Actual leverage, with percentage factor
49
+ */
50
+ leverage: bigint;
51
+ /**
52
+ * What user adds as collateral
53
+ */
54
+ collateral: Asset;
55
+ minQuota: Asset[];
56
+ averageQuota: Asset[];
57
+ slippage: number;
58
+ }
59
+ export interface OpenAccountHumanizedPreview {
60
+ creditManager: string;
61
+ target: string;
62
+ collateral: string;
63
+ leverage: number;
64
+ slippage: number;
65
+ minQuota: string[];
66
+ averageQuota: string[];
67
+ }
45
68
  export interface OpenAccountResult {
46
69
  input: TargetAccount;
70
+ humanizedInput?: OpenAccountHumanizedPreview;
47
71
  error?: Error;
48
72
  txHash?: string;
49
- rawTx?: RawTx;
73
+ rawTx?: Pick<RawTx, "to" | "callData" | "value">;
50
74
  account?: CreditAccountData;
51
75
  }
52
76
  export type PoolDepositResult = {
@@ -76,7 +100,8 @@ export declare class AccountOpener extends SDKConstruct {
76
100
  * Tries to open account with underlying only in each CM
77
101
  */
78
102
  openCreditAccounts(targets: TargetAccount[], depositIntoPools?: boolean, claimFromFaucet?: boolean): Promise<OpenAccountsResult>;
79
- prepareOpen(input: TargetAccount): Promise<RawTx>;
103
+ prepareOpen(input: TargetAccount): Promise<OpenAccountPreview>;
80
104
  getOpenedAccounts(): Promise<CreditAccountData[]>;
81
105
  get faucet(): Address;
82
106
  }
107
+ export {};
@@ -2,11 +2,15 @@ import type { Address } from "viem";
2
2
  import type { Asset } from "../router/index.js";
3
3
  import { AddressMap } from "../utils/index.js";
4
4
  import type { TokenMetaData } from "./types.js";
5
+ export interface FormatBNOptions {
6
+ precision?: number;
7
+ symbol?: boolean;
8
+ }
5
9
  export declare class TokensMeta extends AddressMap<TokenMetaData> {
6
10
  symbol(token: Address): string;
7
11
  decimals(token: Address): number;
8
- formatBN(asset: Asset, precision?: number): string;
9
- formatBN(token: Address, amount: number | bigint | string | undefined, precision?: number): string;
12
+ formatBN(asset: Asset, options?: FormatBNOptions): string;
13
+ formatBN(token: Address, amount: number | bigint | string | undefined, options?: FormatBNOptions): string;
10
14
  findBySymbol(symbol: string): TokenMetaData | undefined;
11
15
  mustFindBySymbol(symbol: string): TokenMetaData;
12
16
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gearbox-protocol/sdk",
3
- "version": "9.3.1",
3
+ "version": "9.4.0",
4
4
  "description": "Gearbox SDK",
5
5
  "license": "MIT",
6
6
  "main": "./dist/cjs/sdk/index.js",