@haven-fi/solauto-sdk 1.0.41 → 1.0.43

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  import { Signer, TransactionBuilder } from "@metaplex-foundation/umi";
2
2
  import { PublicKey } from "@solana/web3.js";
3
3
  import { SolautoClient, SolautoClientArgs } from "./solautoClient";
4
- import { MarginfiTokenAccounts } from "../types/accounts";
4
+ import { MarginfiAssetAccounts } from "../types/accounts";
5
5
  import { DCASettingsInpArgs, PositionState, SolautoActionArgs, SolautoRebalanceTypeArgs, SolautoSettingsParametersInpArgs } from "../generated";
6
6
  import { MarginfiAccount } from "../marginfi-sdk";
7
7
  import { JupSwapDetails } from "../utils/jupiterUtils";
@@ -18,8 +18,8 @@ export declare class SolautoMarginfiClient extends SolautoClient {
18
18
  marginfiAccount: PublicKey | Signer;
19
19
  marginfiAccountPk: PublicKey;
20
20
  marginfiGroup: PublicKey;
21
- marginfiSupplyBankAccounts: MarginfiTokenAccounts;
22
- marginfiDebtBankAccounts: MarginfiTokenAccounts;
21
+ marginfiSupplyAccounts: MarginfiAssetAccounts;
22
+ marginfiDebtAccounts: MarginfiAssetAccounts;
23
23
  intermediaryMarginfiAccountSigner?: Signer;
24
24
  intermediaryMarginfiAccountPk: PublicKey;
25
25
  intermediaryMarginfiAccount?: MarginfiAccount;
@@ -1 +1 @@
1
- {"version":3,"file":"solautoMarginfiClient.d.ts","sourceRoot":"","sources":["../../src/clients/solautoMarginfiClient.ts"],"names":[],"mappings":"AAIA,OAAO,EACL,MAAM,EACN,kBAAkB,EAMnB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,SAAS,EAA8B,MAAM,iBAAiB,CAAC;AACxE,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AACnE,OAAO,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAC;AAK1D,OAAO,EACL,kBAAkB,EAElB,aAAa,EACb,iBAAiB,EACjB,wBAAwB,EACxB,gCAAgC,EAKjC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 +39,10 @@ class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
39
39
  this.marginfiGroup = marginfiAccountData
40
40
  ? (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(marginfiAccountData.group)
41
41
  : args.marginfiGroup ?? new web3_js_1.PublicKey(marginfiAccounts_1.DEFAULT_MARGINFI_GROUP);
42
- this.marginfiSupplyBankAccounts = (0, marginfiUtils_1.findMarginfiBankAccounts)({
42
+ this.marginfiSupplyAccounts = (0, marginfiUtils_1.findMarginfiAccounts)({
43
43
  mint: this.supplyMint.toString(),
44
44
  });
45
- this.marginfiDebtBankAccounts = (0, marginfiUtils_1.findMarginfiBankAccounts)({
45
+ this.marginfiDebtAccounts = (0, marginfiUtils_1.findMarginfiAccounts)({
46
46
  mint: this.debtMint.toString(),
47
47
  });
48
48
  if (!this.initialized) {
@@ -51,7 +51,9 @@ class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
51
51
  this.initialized = true;
52
52
  }
53
53
  async setIntermediaryMarginfiDetails() {
54
- const existingMarginfiAccounts = (await (0, marginfiUtils_1.getAllMarginfiAccountsByAuthority)(this.umi, (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(this.signer.publicKey), false)).filter((x) => !x.marginfiAccount.equals(this.marginfiAccountPk));
54
+ const existingMarginfiAccounts = (await (0, marginfiUtils_1.getAllMarginfiAccountsByAuthority)(this.umi, (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(this.signer.publicKey), false))
55
+ .filter((x) => !x.marginfiAccount.equals(this.marginfiAccountPk))
56
+ .sort((a, b) => a.marginfiAccount.toString().localeCompare(b.marginfiAccount.toString()));
55
57
  const emptyMarginfiAccounts = existingMarginfiAccounts.length > 0
56
58
  ? (await (0, marginfi_sdk_1.safeFetchAllMarginfiAccount)(this.umi, existingMarginfiAccounts.map((x) => (0, umi_1.publicKey)(x.marginfiAccount)))).filter((x) => x.lendingAccount.balances.find((y) => y.bankPk.toString() !== web3_js_1.PublicKey.default.toString() &&
57
59
  (Math.round((0, numberUtils_1.bytesToI80F48)(y.assetShares.value)) != 0 ||
@@ -114,10 +116,10 @@ class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
114
116
  ? this.marginfiAccount
115
117
  : (0, umi_1.publicKey)(this.marginfiAccount),
116
118
  supplyMint: (0, umi_1.publicKey)(this.supplyMint),
117
- supplyBank: (0, umi_1.publicKey)(this.marginfiSupplyBankAccounts.bank),
119
+ supplyBank: (0, umi_1.publicKey)(this.marginfiSupplyAccounts.bank),
118
120
  positionSupplyTa: (0, umi_1.publicKey)(this.positionSupplyTa),
119
121
  debtMint: (0, umi_1.publicKey)(this.debtMint),
120
- debtBank: (0, umi_1.publicKey)(this.marginfiDebtBankAccounts.bank),
122
+ debtBank: (0, umi_1.publicKey)(this.marginfiDebtAccounts.bank),
121
123
  positionDebtTa: (0, umi_1.publicKey)(this.positionDebtTa),
122
124
  signerDebtTa: signerDebtTa,
123
125
  positionData: {
@@ -136,10 +138,10 @@ class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
136
138
  marginfiProgram: (0, umi_1.publicKey)(marginfi_sdk_1.MARGINFI_PROGRAM_ID),
137
139
  marginfiGroup: (0, umi_1.publicKey)(this.marginfiGroup),
138
140
  marginfiAccount: (0, umi_1.publicKey)(this.marginfiAccount),
139
- supplyBank: (0, umi_1.publicKey)(this.marginfiSupplyBankAccounts.bank),
140
- supplyPriceOracle: (0, umi_1.publicKey)(this.marginfiSupplyBankAccounts.priceOracle),
141
- debtBank: (0, umi_1.publicKey)(this.marginfiDebtBankAccounts.bank),
142
- debtPriceOracle: (0, umi_1.publicKey)(this.marginfiDebtBankAccounts.priceOracle),
141
+ supplyBank: (0, umi_1.publicKey)(this.marginfiSupplyAccounts.bank),
142
+ supplyPriceOracle: (0, umi_1.publicKey)(this.marginfiSupplyAccounts.priceOracle),
143
+ debtBank: (0, umi_1.publicKey)(this.marginfiDebtAccounts.bank),
144
+ debtPriceOracle: (0, umi_1.publicKey)(this.marginfiDebtAccounts.priceOracle),
143
145
  solautoPosition: (0, umi_1.publicKey)(this.solautoPosition),
144
146
  });
145
147
  }
@@ -161,8 +163,8 @@ class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
161
163
  signerTokenAccount: (0, umi_1.publicKey)(this.signerSupplyTa),
162
164
  marginfiAccount: (0, umi_1.publicKey)(this.marginfiAccountPk),
163
165
  marginfiGroup: (0, umi_1.publicKey)(this.marginfiGroup),
164
- bank: (0, umi_1.publicKey)(this.marginfiSupplyBankAccounts.bank),
165
- bankLiquidityVault: (0, umi_1.publicKey)(this.marginfiSupplyBankAccounts.liquidityVault),
166
+ bank: (0, umi_1.publicKey)(this.marginfiSupplyAccounts.bank),
167
+ bankLiquidityVault: (0, umi_1.publicKey)(this.marginfiSupplyAccounts.liquidityVault),
166
168
  });
167
169
  }
168
170
  case "Borrow": {
@@ -172,9 +174,9 @@ class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
172
174
  destinationTokenAccount: (0, umi_1.publicKey)(this.signerDebtTa),
173
175
  marginfiAccount: (0, umi_1.publicKey)(this.marginfiAccountPk),
174
176
  marginfiGroup: (0, umi_1.publicKey)(this.marginfiGroup),
175
- bank: (0, umi_1.publicKey)(this.marginfiDebtBankAccounts.bank),
176
- bankLiquidityVault: (0, umi_1.publicKey)(this.marginfiDebtBankAccounts.liquidityVault),
177
- bankLiquidityVaultAuthority: (0, umi_1.publicKey)(this.marginfiDebtBankAccounts.vaultAuthority),
177
+ bank: (0, umi_1.publicKey)(this.marginfiDebtAccounts.bank),
178
+ bankLiquidityVault: (0, umi_1.publicKey)(this.marginfiDebtAccounts.liquidityVault),
179
+ bankLiquidityVaultAuthority: (0, umi_1.publicKey)(this.marginfiDebtAccounts.vaultAuthority),
178
180
  });
179
181
  }
180
182
  case "Repay": {
@@ -185,8 +187,8 @@ class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
185
187
  signerTokenAccount: (0, umi_1.publicKey)(this.signerDebtTa),
186
188
  marginfiAccount: (0, umi_1.publicKey)(this.marginfiAccountPk),
187
189
  marginfiGroup: (0, umi_1.publicKey)(this.marginfiGroup),
188
- bank: (0, umi_1.publicKey)(this.marginfiDebtBankAccounts.bank),
189
- bankLiquidityVault: (0, umi_1.publicKey)(this.marginfiDebtBankAccounts.liquidityVault),
190
+ bank: (0, umi_1.publicKey)(this.marginfiDebtAccounts.bank),
191
+ bankLiquidityVault: (0, umi_1.publicKey)(this.marginfiDebtAccounts.liquidityVault),
190
192
  });
191
193
  }
192
194
  case "Withdraw": {
@@ -197,9 +199,9 @@ class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
197
199
  destinationTokenAccount: (0, umi_1.publicKey)(this.signerSupplyTa),
198
200
  marginfiAccount: (0, umi_1.publicKey)(this.marginfiAccountPk),
199
201
  marginfiGroup: (0, umi_1.publicKey)(this.marginfiGroup),
200
- bank: (0, umi_1.publicKey)(this.marginfiSupplyBankAccounts.bank),
201
- bankLiquidityVault: (0, umi_1.publicKey)(this.marginfiSupplyBankAccounts.liquidityVault),
202
- bankLiquidityVaultAuthority: (0, umi_1.publicKey)(this.marginfiSupplyBankAccounts.vaultAuthority),
202
+ bank: (0, umi_1.publicKey)(this.marginfiSupplyAccounts.bank),
203
+ bankLiquidityVault: (0, umi_1.publicKey)(this.marginfiSupplyAccounts.liquidityVault),
204
+ bankLiquidityVaultAuthority: (0, umi_1.publicKey)(this.marginfiSupplyAccounts.vaultAuthority),
203
205
  });
204
206
  }
205
207
  }
@@ -212,8 +214,8 @@ class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
212
214
  positionSupplyTa = (0, umi_1.publicKey)(args.__kind === "Withdraw" || this.selfManaged
213
215
  ? this.signerSupplyTa
214
216
  : this.positionSupplyTa);
215
- vaultSupplyTa = (0, umi_1.publicKey)(this.marginfiSupplyBankAccounts.liquidityVault);
216
- supplyVaultAuthority = (0, umi_1.publicKey)(this.marginfiSupplyBankAccounts.vaultAuthority);
217
+ vaultSupplyTa = (0, umi_1.publicKey)(this.marginfiSupplyAccounts.liquidityVault);
218
+ supplyVaultAuthority = (0, umi_1.publicKey)(this.marginfiSupplyAccounts.vaultAuthority);
217
219
  }
218
220
  let positionDebtTa = undefined;
219
221
  let vaultDebtTa = undefined;
@@ -222,14 +224,14 @@ class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
222
224
  positionDebtTa = (0, umi_1.publicKey)(args.__kind === "Borrow" || this.selfManaged
223
225
  ? this.signerDebtTa
224
226
  : this.positionDebtTa);
225
- vaultDebtTa = (0, umi_1.publicKey)(this.marginfiDebtBankAccounts.liquidityVault);
226
- debtVaultAuthority = (0, umi_1.publicKey)(this.marginfiDebtBankAccounts.vaultAuthority);
227
+ vaultDebtTa = (0, umi_1.publicKey)(this.marginfiDebtAccounts.liquidityVault);
228
+ debtVaultAuthority = (0, umi_1.publicKey)(this.marginfiDebtAccounts.vaultAuthority);
227
229
  }
228
230
  let supplyPriceOracle = undefined;
229
231
  let debtPriceOracle = undefined;
230
232
  if (args.__kind === "Withdraw" || args.__kind === "Borrow") {
231
- supplyPriceOracle = (0, umi_1.publicKey)(this.marginfiSupplyBankAccounts.priceOracle);
232
- debtPriceOracle = (0, umi_1.publicKey)(this.marginfiDebtBankAccounts.priceOracle);
233
+ supplyPriceOracle = (0, umi_1.publicKey)(this.marginfiSupplyAccounts.priceOracle);
234
+ debtPriceOracle = (0, umi_1.publicKey)(this.marginfiDebtAccounts.priceOracle);
233
235
  }
234
236
  return (0, generated_1.marginfiProtocolInteraction)(this.umi, {
235
237
  signer: this.signer,
@@ -237,12 +239,12 @@ class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
237
239
  solautoPosition: (0, umi_1.publicKey)(this.solautoPosition),
238
240
  marginfiGroup: (0, umi_1.publicKey)(this.marginfiGroup),
239
241
  marginfiAccount: (0, umi_1.publicKey)(this.marginfiAccountPk),
240
- supplyBank: (0, umi_1.publicKey)(this.marginfiSupplyBankAccounts.bank),
242
+ supplyBank: (0, umi_1.publicKey)(this.marginfiSupplyAccounts.bank),
241
243
  supplyPriceOracle,
242
244
  positionSupplyTa,
243
245
  vaultSupplyTa,
244
246
  supplyVaultAuthority,
245
- debtBank: (0, umi_1.publicKey)(this.marginfiDebtBankAccounts.bank),
247
+ debtBank: (0, umi_1.publicKey)(this.marginfiDebtAccounts.bank),
246
248
  debtPriceOracle,
247
249
  positionDebtTa,
248
250
  vaultDebtTa,
@@ -265,34 +267,34 @@ class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
265
267
  ixsSysvar: (0, umi_1.publicKey)(web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY),
266
268
  solautoFeesSupplyTa: rebalanceStep === "B" ? (0, umi_1.publicKey)(this.solautoFeesSupplyTa) : undefined,
267
269
  authorityReferralState: (0, umi_1.publicKey)(this.authorityReferralState),
268
- referredBySupplyTa: rebalanceStep === "B" && this.referredBySupplyTa
270
+ referredBySupplyTa: this.referredBySupplyTa
269
271
  ? (0, umi_1.publicKey)(this.referredBySupplyTa)
270
272
  : undefined,
271
273
  solautoPosition: (0, umi_1.publicKey)(this.solautoPosition),
272
274
  marginfiGroup: (0, umi_1.publicKey)(this.marginfiGroup),
273
275
  marginfiAccount: (0, umi_1.publicKey)(this.marginfiAccountPk),
274
276
  intermediaryTa: (0, umi_1.publicKey)((0, accountUtils_1.getTokenAccount)((0, umi_web3js_adapters_1.toWeb3JsPublicKey)(this.signer.publicKey), swapDetails.inputMint)),
275
- supplyBank: (0, umi_1.publicKey)(this.marginfiSupplyBankAccounts.bank),
276
- supplyPriceOracle: (0, umi_1.publicKey)(this.marginfiSupplyBankAccounts.priceOracle),
277
+ supplyBank: (0, umi_1.publicKey)(this.marginfiSupplyAccounts.bank),
278
+ supplyPriceOracle: (0, umi_1.publicKey)(this.marginfiSupplyAccounts.priceOracle),
277
279
  positionSupplyTa: (0, umi_1.publicKey)(this.positionSupplyTa),
278
280
  signerSupplyTa: this.selfManaged
279
281
  ? (0, umi_1.publicKey)(this.signerSupplyTa)
280
282
  : undefined,
281
283
  vaultSupplyTa: needSupplyAccounts
282
- ? (0, umi_1.publicKey)(this.marginfiSupplyBankAccounts.liquidityVault)
284
+ ? (0, umi_1.publicKey)(this.marginfiSupplyAccounts.liquidityVault)
283
285
  : undefined,
284
286
  supplyVaultAuthority: needSupplyAccounts
285
- ? (0, umi_1.publicKey)(this.marginfiSupplyBankAccounts.vaultAuthority)
287
+ ? (0, umi_1.publicKey)(this.marginfiSupplyAccounts.vaultAuthority)
286
288
  : undefined,
287
- debtBank: (0, umi_1.publicKey)(this.marginfiDebtBankAccounts.bank),
288
- debtPriceOracle: (0, umi_1.publicKey)(this.marginfiDebtBankAccounts.priceOracle),
289
+ debtBank: (0, umi_1.publicKey)(this.marginfiDebtAccounts.bank),
290
+ debtPriceOracle: (0, umi_1.publicKey)(this.marginfiDebtAccounts.priceOracle),
289
291
  positionDebtTa: (0, umi_1.publicKey)(this.positionDebtTa),
290
292
  signerDebtTa: this.selfManaged ? (0, umi_1.publicKey)(this.signerDebtTa) : undefined,
291
293
  vaultDebtTa: needDebtAccounts
292
- ? (0, umi_1.publicKey)(this.marginfiDebtBankAccounts.liquidityVault)
294
+ ? (0, umi_1.publicKey)(this.marginfiDebtAccounts.liquidityVault)
293
295
  : undefined,
294
296
  debtVaultAuthority: needDebtAccounts
295
- ? (0, umi_1.publicKey)(this.marginfiDebtBankAccounts.vaultAuthority)
297
+ ? (0, umi_1.publicKey)(this.marginfiDebtAccounts.vaultAuthority)
296
298
  : undefined,
297
299
  rebalanceType,
298
300
  targetLiqUtilizationRateBps: targetLiqUtilizationRateBps ?? null,
@@ -301,8 +303,8 @@ class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
301
303
  }
302
304
  flashBorrow(flashLoanDetails, destinationTokenAccount) {
303
305
  const bank = flashLoanDetails.mint.equals(this.supplyMint)
304
- ? this.marginfiSupplyBankAccounts
305
- : this.marginfiDebtBankAccounts;
306
+ ? this.marginfiSupplyAccounts
307
+ : this.marginfiDebtAccounts;
306
308
  return (0, umi_1.transactionBuilder)()
307
309
  .add((0, marginfi_sdk_1.lendingAccountStartFlashloan)(this.umi, {
308
310
  endIndex: 0,
@@ -323,8 +325,8 @@ class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
323
325
  }
324
326
  flashRepay(flashLoanDetails) {
325
327
  const bank = flashLoanDetails.mint.equals(this.supplyMint)
326
- ? this.marginfiSupplyBankAccounts
327
- : this.marginfiDebtBankAccounts;
328
+ ? this.marginfiSupplyAccounts
329
+ : this.marginfiDebtAccounts;
328
330
  const remainingAccounts = [];
329
331
  let includedFlashLoanToken = false;
330
332
  if (this.intermediaryMarginfiAccount) {
@@ -340,7 +342,7 @@ class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
340
342
  isWritable: false,
341
343
  },
342
344
  {
343
- pubkey: (0, umi_1.publicKey)((0, marginfiUtils_1.findMarginfiBankAccounts)({ bank: x.bankPk.toString() })
345
+ pubkey: (0, umi_1.publicKey)((0, marginfiUtils_1.findMarginfiAccounts)({ bank: x.bankPk.toString() })
344
346
  .priceOracle),
345
347
  isSigner: false,
346
348
  isWritable: false,
@@ -1,7 +1,7 @@
1
- import { MarginfiTokenAccounts } from "../types/accounts";
1
+ import { MarginfiAssetAccounts } from "../types/accounts";
2
2
  export declare const DEFAULT_MARGINFI_GROUP = "4qp6Fx6tnZkY5Wropq9wUYgtFxXKwE6viZxFHg3rdAG8";
3
3
  export declare const MARGINFI_ACCOUNTS: {
4
- [key: string]: MarginfiTokenAccounts;
4
+ [key: string]: MarginfiAssetAccounts;
5
5
  };
6
6
  export declare const MARGINFI_ACCOUNTS_LOOKUP_TABLE = "GAjmWmBPcH5Gxbiykasydj6RsCEaCLyHEvK6kHdFigc6";
7
7
  //# sourceMappingURL=marginfiAccounts.d.ts.map
@@ -114,7 +114,7 @@ function marginfiRebalance(context, input) {
114
114
  },
115
115
  supplyVaultAuthority: {
116
116
  index: 17,
117
- isWritable: false,
117
+ isWritable: true,
118
118
  value: input.supplyVaultAuthority ?? null,
119
119
  },
120
120
  debtBank: {
@@ -144,7 +144,7 @@ function marginfiRebalance(context, input) {
144
144
  },
145
145
  debtVaultAuthority: {
146
146
  index: 23,
147
- isWritable: false,
147
+ isWritable: true,
148
148
  value: input.debtVaultAuthority ?? null,
149
149
  },
150
150
  };
@@ -1 +1 @@
1
- {"version":3,"file":"transactionsManager.d.ts","sourceRoot":"","sources":["../../src/transactions/transactionsManager.ts"],"names":[],"mappings":"AACA,OAAO,EACL,uBAAuB,EAGvB,kBAAkB,EACnB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAKzD,OAAO,EACL,aAAa,EAEd,MAAM,uBAAuB,CAAC;AAK/B,cAAM,YAAY;IAIJ,OAAO,CAAC,MAAM;IAH1B,WAAW,EAAE,MAAM,EAAE,CAAM;IAC3B,KAAK,EAAE,uBAAuB,EAAE,CAAM;gBAElB,MAAM,EAAE,aAAa;IAInC,YAAY,CAChB,mBAAmB,EAAE,MAAM,EAAE,GAC5B,OAAO,CAAC,uBAAuB,EAAE,CAAC;CAqBtC;AAED,qBAAa,eAAe;IAKjB,OAAO,EAAE,CACd,UAAU,EAAE,MAAM,KACf,OAAO,CACV;QAAE,EAAE,EAAE,kBAAkB,CAAC;QAAC,oBAAoB,CAAC,EAAE,MAAM,EAAE,CAAA;KAAE,GAAG,SAAS,CACxE;IACM,IAAI,CAAC;IATd,oBAAoB,EAAG,MAAM,EAAE,CAAC;IAChC,EAAE,CAAC,EAAE,kBAAkB,CAAC;gBAGf,OAAO,EAAE,CACd,UAAU,EAAE,MAAM,KACf,OAAO,CACV;QAAE,EAAE,EAAE,kBAAkB,CAAC;QAAC,oBAAoB,CAAC,EAAE,MAAM,EAAE,CAAA;KAAE,GAAG,SAAS,CACxE,EACM,IAAI,CAAC,oBAAQ;IAGhB,UAAU;IAIV,OAAO,CAAC,UAAU,EAAE,MAAM;IAMhC,cAAc,IAAI,MAAM,EAAE;CAY3B;AAED,cAAM,cAAc;IAEhB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,EAAE,cAAc,EAAE,EAAE,EAAE,kBAAkB;IA6B7D,IAAI;CAyJX"}
1
+ {"version":3,"file":"transactionsManager.d.ts","sourceRoot":"","sources":["../../src/transactions/transactionsManager.ts"],"names":[],"mappings":"AACA,OAAO,EACL,uBAAuB,EAGvB,kBAAkB,EACnB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAKzD,OAAO,EACL,aAAa,EAEd,MAAM,uBAAuB,CAAC;AAK/B,cAAM,YAAY;IAIJ,OAAO,CAAC,MAAM;IAH1B,WAAW,EAAE,MAAM,EAAE,CAAM;IAC3B,KAAK,EAAE,uBAAuB,EAAE,CAAM;gBAElB,MAAM,EAAE,aAAa;IAInC,YAAY,CAChB,mBAAmB,EAAE,MAAM,EAAE,GAC5B,OAAO,CAAC,uBAAuB,EAAE,CAAC;CAqBtC;AAED,qBAAa,eAAe;IAKjB,OAAO,EAAE,CACd,UAAU,EAAE,MAAM,KACf,OAAO,CACV;QAAE,EAAE,EAAE,kBAAkB,CAAC;QAAC,oBAAoB,CAAC,EAAE,MAAM,EAAE,CAAA;KAAE,GAAG,SAAS,CACxE;IACM,IAAI,CAAC;IATd,oBAAoB,EAAG,MAAM,EAAE,CAAC;IAChC,EAAE,CAAC,EAAE,kBAAkB,CAAC;gBAGf,OAAO,EAAE,CACd,UAAU,EAAE,MAAM,KACf,OAAO,CACV;QAAE,EAAE,EAAE,kBAAkB,CAAC;QAAC,oBAAoB,CAAC,EAAE,MAAM,EAAE,CAAA;KAAE,GAAG,SAAS,CACxE,EACM,IAAI,CAAC,oBAAQ;IAGhB,UAAU;IAIV,OAAO,CAAC,UAAU,EAAE,MAAM;IAMhC,cAAc,IAAI,MAAM,EAAE;CAY3B;AAED,cAAM,cAAc;IAEhB,OAAO,CAAC,MAAM;IACP,YAAY,EAAE,YAAY;IAC1B,KAAK,EAAE,eAAe,EAAE;gBAFvB,MAAM,EAAE,aAAa,EACtB,YAAY,EA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structions()) {
192
192
  const ixAccounts = ix.keys.map((x) => x.pubkey);
193
193
  const accountsNotInLut = ixAccounts.filter((x) => !lutAccounts.includes(x));
194
- console.log("Program", ix.programId, " data ", ix.data.length, ix.keys.filter((x) => lutAccounts.includes(x.pubkey)).length * 3);
194
+ this.client.log(`Program ${ix.programId}, data len: ${ix.data.length}, LUT accounts data: ${ix.keys.filter((x) => lutAccounts.includes(x.pubkey)).length * 3}`);
195
195
  if (accountsNotInLut.length > 0) {
196
- console.log("Accounts not in LUT:");
196
+ this.client.log(`${accountsNotInLut.length} accounts not in LUT:`);
197
197
  for (const key of accountsNotInLut) {
198
- console.log(key.toString());
198
+ this.client.log(key.toString());
199
199
  }
200
- console.log(ix.keys.length);
201
200
  }
202
201
  }
203
202
  }
@@ -290,7 +289,7 @@ class TransactionsManager {
290
289
  const txSig = await (0, solanaUtils_1.sendSingleOptimizedTransaction)(this.client.umi, this.client.connection, tx, this.simulateOnly, attemptNum);
291
290
  this.updateStatus(itemSet.name(), TransactionStatus.Successful, txSig ? bs58_1.default.encode(txSig) : undefined);
292
291
  }
293
- }, 3, 150, this.errorsToThrow);
292
+ }, 4, 150, this.errorsToThrow);
294
293
  }
295
294
  }
296
295
  if (!this.simulateOnly) {
@@ -1,4 +1,4 @@
1
- export interface MarginfiTokenAccounts {
1
+ export interface MarginfiAssetAccounts {
2
2
  mint: string;
3
3
  bank: string;
4
4
  liquidityVault: string;
@@ -94,7 +94,7 @@ function retryWithExponentialBackoff(fn, retries = 5, delay = 150, errorsToThrow
94
94
  return;
95
95
  }
96
96
  if (attemptNum < retries) {
97
- console.log(error.message);
97
+ console.log(error);
98
98
  setTimeout(() => {
99
99
  console.log("Retrying...");
100
100
  return attempt(attemptNum);
@@ -34,7 +34,7 @@ async function getJupSwapTransaction(signer, swapDetails, attemptNum) {
34
34
  slippageBps: 10,
35
35
  maxAccounts: 60,
36
36
  }), 3);
37
- const finalPriceSlippageBps = Math.round(Math.max(quoteResponse.slippageBps ?? 10, Math.round((0, numberUtils_1.toBps)(parseFloat(quoteResponse.priceImpactPct)))) *
37
+ const finalPriceSlippageBps = Math.round(Math.max(quoteResponse.slippageBps, Math.round((0, numberUtils_1.toBps)(parseFloat(quoteResponse.priceImpactPct))) + 1) *
38
38
  (1 + (swapDetails.slippageBpsIncFactor ?? 0)));
39
39
  quoteResponse.slippageBps = finalPriceSlippageBps;
40
40
  console.log(quoteResponse);
@@ -1,14 +1,20 @@
1
1
  import { PublicKey } from "@solana/web3.js";
2
2
  import { Umi } from "@metaplex-foundation/umi";
3
3
  import { Bank } from "../marginfi-sdk";
4
- import { MarginfiTokenAccounts } from "../types/accounts";
4
+ import { MarginfiAssetAccounts } from "../types/accounts";
5
5
  import { PositionState } from "../generated";
6
6
  import { LivePositionUpdates } from "./solauto/generalUtils";
7
- export declare function findMarginfiBankAccounts({ mint, bank, }: {
7
+ export declare function findMarginfiAccounts({ mint, bank, }: {
8
8
  mint?: string;
9
9
  bank?: string;
10
- }): MarginfiTokenAccounts;
11
- export declare function getMaxLtvAndLiqThreshold(supplyBank: Bank, debtBank: Bank | null, supplyPrice?: number): Promise<[number, number]>;
10
+ }): MarginfiAssetAccounts;
11
+ export declare function getMaxLtvAndLiqThreshold(umi: Umi, supply: {
12
+ mint: PublicKey;
13
+ bank: Bank | null;
14
+ }, debt: {
15
+ mint: PublicKey;
16
+ bank: Bank | null;
17
+ }, supplyPrice?: number): Promise<[number, number]>;
12
18
  export declare function getAllMarginfiAccountsByAuthority(umi: Umi, authority: PublicKey, compatibleWithSolauto?: boolean): Promise<{
13
19
  marginfiAccount: PublicKey;
14
20
  supplyMint?: PublicKey;
@@ -1 +1 @@
1
- {"version":3,"file":"marginfiUtils.d.ts","sourceRoot":"","sources":["../../src/utils/marginfiUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAa,GAAG,EAAE,MAAM,0BAA0B,CAAC;AAE1D,OAAO,EACL,IA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G,SAAS,CAAC,CA+IpC;AA+DD,wBAAsB,sBAAsB,CAC1C,GAAG,EAAE,GAAG,EACR,IAAI,EAAE,IAAI,GACT,OAAO,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CA0B3B"}
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getUpToDateShareValues = exports.getMarginfiAccountPositionState = exports.getAllMarginfiAccountsByAuthority = exports.getMaxLtvAndLiqThreshold = exports.findMarginfiBankAccounts = void 0;
3
+ exports.getUpToDateShareValues = exports.getMarginfiAccountPositionState = exports.getAllMarginfiAccountsByAuthority = exports.getMaxLtvAndLiqThreshold = exports.findMarginfiAccounts = void 0;
4
4
  const web3_js_1 = require("@solana/web3.js");
5
5
  const umi_1 = require("@metaplex-foundation/umi");
6
6
  const umi_web3js_adapters_1 = require("@metaplex-foundation/umi-web3js-adapters");
@@ -11,7 +11,7 @@ const solautoConstants_1 = require("../constants/solautoConstants");
11
11
  const marginfiAccounts_1 = require("../constants/marginfiAccounts");
12
12
  const generalAccounts_1 = require("../constants/generalAccounts");
13
13
  const solanaUtils_1 = require("./solanaUtils");
14
- function findMarginfiBankAccounts({ mint, bank, }) {
14
+ function findMarginfiAccounts({ mint, bank, }) {
15
15
  for (const key in marginfiAccounts_1.MARGINFI_ACCOUNTS) {
16
16
  const account = marginfiAccounts_1.MARGINFI_ACCOUNTS[key];
17
17
  if (mint && account.mint.toString().toLowerCase() === mint.toLowerCase()) {
@@ -23,24 +23,36 @@ function findMarginfiBankAccounts({ mint, bank, }) {
23
23
  }
24
24
  throw new Error(`Marginfi accounts not found by the mint: ${mint}`);
25
25
  }
26
- exports.findMarginfiBankAccounts = findMarginfiBankAccounts;
27
- async function getMaxLtvAndLiqThreshold(supplyBank, debtBank, supplyPrice) {
26
+ exports.findMarginfiAccounts = findMarginfiAccounts;
27
+ async function getMaxLtvAndLiqThreshold(umi, supply, debt, supplyPrice) {
28
+ if (!supply.bank) {
29
+ supply.bank = await (0, marginfi_sdk_1.safeFetchBank)(umi, (0, umi_1.publicKey)(findMarginfiAccounts({
30
+ mint: supply.mint.toString(),
31
+ }).bank));
32
+ }
33
+ if (!debt.bank && !debt.mint.equals(web3_js_1.PublicKey.default)) {
34
+ debt.bank = await (0, marginfi_sdk_1.safeFetchBank)(umi, (0, umi_1.publicKey)(findMarginfiAccounts({
35
+ mint: debt.mint.toString(),
36
+ }).bank));
37
+ }
28
38
  if (!supplyPrice) {
29
- const [price] = await (0, generalUtils_1.getTokenPrices)([(0, umi_web3js_adapters_1.toWeb3JsPublicKey)(supplyBank.mint)]);
39
+ const [price] = await (0, generalUtils_1.getTokenPrices)([
40
+ (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(supply.bank.mint),
41
+ ]);
30
42
  supplyPrice = price;
31
43
  }
32
- if (debtBank === null) {
44
+ if (debt.bank === null) {
33
45
  return [0, 0];
34
46
  }
35
- let maxLtv = (0, numberUtils_1.bytesToI80F48)(supplyBank.config.assetWeightInit.value) /
36
- (0, numberUtils_1.bytesToI80F48)(debtBank.config.liabilityWeightInit.value);
37
- const liqThreshold = (0, numberUtils_1.bytesToI80F48)(supplyBank.config.assetWeightMaint.value) /
38
- (0, numberUtils_1.bytesToI80F48)(debtBank.config.liabilityWeightMaint.value);
39
- const totalDepositedUsdValue = (0, numberUtils_1.fromBaseUnit)(BigInt(Math.round((0, numberUtils_1.bytesToI80F48)(supplyBank.totalAssetShares.value) *
40
- (0, numberUtils_1.bytesToI80F48)(supplyBank.assetShareValue.value))), supplyBank.mintDecimals) * supplyPrice;
41
- if (supplyBank.config.totalAssetValueInitLimit !== BigInt(0) &&
42
- totalDepositedUsdValue > supplyBank.config.totalAssetValueInitLimit) {
43
- const discount = Number(supplyBank.config.totalAssetValueInitLimit) /
47
+ let maxLtv = (0, numberUtils_1.bytesToI80F48)(supply.bank.config.assetWeightInit.value) /
48
+ (0, numberUtils_1.bytesToI80F48)(debt.bank.config.liabilityWeightInit.value);
49
+ const liqThreshold = (0, numberUtils_1.bytesToI80F48)(supply.bank.config.assetWeightMaint.value) /
50
+ (0, numberUtils_1.bytesToI80F48)(debt.bank.config.liabilityWeightMaint.value);
51
+ const totalDepositedUsdValue = (0, numberUtils_1.fromBaseUnit)(BigInt(Math.round((0, numberUtils_1.bytesToI80F48)(supply.bank.totalAssetShares.value) *
52
+ (0, numberUtils_1.bytesToI80F48)(supply.bank.assetShareValue.value))), supply.bank.mintDecimals) * supplyPrice;
53
+ if (supply.bank.config.totalAssetValueInitLimit !== BigInt(0) &&
54
+ totalDepositedUsdValue > supply.bank.config.totalAssetValueInitLimit) {
55
+ const discount = Number(supply.bank.config.totalAssetValueInitLimit) /
44
56
  totalDepositedUsdValue;
45
57
  maxLtv = Math.round(maxLtv * Number(discount));
46
58
  }
@@ -106,13 +118,16 @@ async function getTokenUsage(umi, bank, isAsset, shares, amountUsedAdjustment) {
106
118
  decimals: bank?.mintDecimals ?? 0,
107
119
  amountUsed: {
108
120
  baseUnit: BigInt(Math.round(amountUsed)),
109
- baseAmountUsdValue: bank ? (0, numberUtils_1.toBaseUnit)((0, numberUtils_1.fromBaseUnit)(BigInt(Math.round(amountUsed)), bank.mintDecimals) *
110
- marketPrice, generalAccounts_1.USD_DECIMALS) : BigInt(0),
121
+ baseAmountUsdValue: bank
122
+ ? (0, numberUtils_1.toBaseUnit)((0, numberUtils_1.fromBaseUnit)(BigInt(Math.round(amountUsed)), bank.mintDecimals) *
123
+ marketPrice, generalAccounts_1.USD_DECIMALS)
124
+ : BigInt(0),
111
125
  },
112
126
  amountCanBeUsed: {
113
127
  baseUnit: BigInt(Math.round(amountCanBeUsed)),
114
- baseAmountUsdValue: bank ? (0, numberUtils_1.toBaseUnit)((0, numberUtils_1.fromBaseUnit)(BigInt(Math.round(amountCanBeUsed)), bank.mintDecimals) *
115
- marketPrice, generalAccounts_1.USD_DECIMALS) : BigInt(0),
128
+ baseAmountUsdValue: bank
129
+ ? (0, numberUtils_1.toBaseUnit)((0, numberUtils_1.fromBaseUnit)(BigInt(Math.round(amountCanBeUsed)), bank.mintDecimals) * marketPrice, generalAccounts_1.USD_DECIMALS)
130
+ : BigInt(0),
116
131
  },
117
132
  baseAmountMarketPriceUsd: (0, numberUtils_1.toBaseUnit)(marketPrice, generalAccounts_1.USD_DECIMALS),
118
133
  flashLoanFeeBps: 0,
@@ -125,10 +140,10 @@ async function getTokenUsage(umi, bank, isAsset, shares, amountUsedAdjustment) {
125
140
  async function getMarginfiAccountPositionState(umi, marginfiAccountPk, supplyMint, debtMint, livePositionUpdates) {
126
141
  let marginfiAccount = await (0, marginfi_sdk_1.safeFetchMarginfiAccount)(umi, (0, umi_1.publicKey)(marginfiAccountPk));
127
142
  let supplyBank = supplyMint && supplyMint !== web3_js_1.PublicKey.default
128
- ? await (0, marginfi_sdk_1.safeFetchBank)(umi, (0, umi_1.publicKey)(findMarginfiBankAccounts({ mint: supplyMint.toString() }).bank))
143
+ ? await (0, marginfi_sdk_1.safeFetchBank)(umi, (0, umi_1.publicKey)(findMarginfiAccounts({ mint: supplyMint.toString() }).bank))
129
144
  : null;
130
145
  let debtBank = debtMint && debtMint !== web3_js_1.PublicKey.default
131
- ? await (0, marginfi_sdk_1.safeFetchBank)(umi, (0, umi_1.publicKey)(findMarginfiBankAccounts({ mint: debtMint.toString() }).bank))
146
+ ? await (0, marginfi_sdk_1.safeFetchBank)(umi, (0, umi_1.publicKey)(findMarginfiAccounts({ mint: debtMint.toString() }).bank))
132
147
  : null;
133
148
  let supplyUsage = undefined;
134
149
  let debtUsage = undefined;
@@ -169,7 +184,13 @@ async function getMarginfiAccountPositionState(umi, marginfiAccountPk, supplyMin
169
184
  debtUsage = await getTokenUsage(umi, debtBank, false, 0, livePositionUpdates?.debtAdjustment);
170
185
  }
171
186
  const supplyPrice = solautoConstants_1.PRICES[supplyMint.toString()].price;
172
- let [maxLtv, liqThreshold] = await getMaxLtvAndLiqThreshold(supplyBank, debtBank, supplyPrice);
187
+ let [maxLtv, liqThreshold] = await getMaxLtvAndLiqThreshold(umi, {
188
+ mint: (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(supplyBank.mint),
189
+ bank: supplyBank,
190
+ }, {
191
+ mint: debtBank ? (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(debtBank.mint) : web3_js_1.PublicKey.default,
192
+ bank: debtBank,
193
+ }, supplyPrice);
173
194
  const supplyUsd = (0, numberUtils_1.fromBaseUnit)(supplyUsage.amountUsed.baseAmountUsdValue, generalAccounts_1.USD_DECIMALS);
174
195
  const debtUsd = (0, numberUtils_1.fromBaseUnit)(debtUsage?.amountUsed?.baseAmountUsdValue ?? BigInt(0), generalAccounts_1.USD_DECIMALS);
175
196
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@haven-fi/solauto-sdk",
3
- "version": "1.0.41",
3
+ "version": "1.0.43",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "description": "Typescript SDK for the Solauto program on the Solana blockchain",
@@ -13,7 +13,7 @@ import {
13
13
  } from "@metaplex-foundation/umi";
14
14
  import { PublicKey, SYSVAR_INSTRUCTIONS_PUBKEY } from "@solana/web3.js";
15
15
  import { SolautoClient, SolautoClientArgs } from "./solautoClient";
16
- import { MarginfiTokenAccounts } from "../types/accounts";
16
+ import { MarginfiAssetAccounts } from "../types/accounts";
17
17
  import {
18
18
  DEFAULT_MARGINFI_GROUP,
19
19
  MARGINFI_ACCOUNTS_LOOKUP_TABLE,
@@ -48,7 +48,7 @@ import {
48
48
  import { JupSwapDetails } from "../utils/jupiterUtils";
49
49
  import { FlashLoanDetails } from "../utils/solauto/rebalanceUtils";
50
50
  import {
51
- findMarginfiBankAccounts,
51
+ findMarginfiAccounts,
52
52
  getAllMarginfiAccountsByAuthority,
53
53
  getMarginfiAccountPositionState,
54
54
  } from "../utils/marginfiUtils";
@@ -70,8 +70,8 @@ export class SolautoMarginfiClient extends SolautoClient {
70
70
  public marginfiAccountPk!: PublicKey;
71
71
  public marginfiGroup!: PublicKey;
72
72
 
73
- public marginfiSupplyBankAccounts!: MarginfiTokenAccounts;
74
- public marginfiDebtBankAccounts!: MarginfiTokenAccounts;
73
+ public marginfiSupplyAccounts!: MarginfiAssetAccounts;
74
+ public marginfiDebtAccounts!: MarginfiAssetAccounts;
75
75
 
76
76
  // For flash loans
77
77
  public intermediaryMarginfiAccountSigner?: Signer;
@@ -107,10 +107,10 @@ export class SolautoMarginfiClient extends SolautoClient {
107
107
  ? toWeb3JsPublicKey(marginfiAccountData.group)
108
108
  : args.marginfiGroup ?? new PublicKey(DEFAULT_MARGINFI_GROUP);
109
109
 
110
- this.marginfiSupplyBankAccounts = findMarginfiBankAccounts({
110
+ this.marginfiSupplyAccounts = findMarginfiAccounts({
111
111
  mint: this.supplyMint.toString(),
112
112
  })!;
113
- this.marginfiDebtBankAccounts = findMarginfiBankAccounts({
113
+ this.marginfiDebtAccounts = findMarginfiAccounts({
114
114
  mint: this.debtMint.toString(),
115
115
  })!;
116
116
 
@@ -127,7 +127,11 @@ export class SolautoMarginfiClient extends SolautoClient {
127
127
  toWeb3JsPublicKey(this.signer.publicKey),
128
128
  false
129
129
  )
130
- ).filter((x) => !x.marginfiAccount.equals(this.marginfiAccountPk));
130
+ )
131
+ .filter((x) => !x.marginfiAccount.equals(this.marginfiAccountPk))
132
+ .sort((a, b) =>
133
+ a.marginfiAccount.toString().localeCompare(b.marginfiAccount.toString())
134
+ );
131
135
  const emptyMarginfiAccounts =
132
136
  existingMarginfiAccounts.length > 0
133
137
  ? (
@@ -216,10 +220,10 @@ export class SolautoMarginfiClient extends SolautoClient {
216
220
  ? (this.marginfiAccount as Signer)
217
221
  : publicKey(this.marginfiAccount),
218
222
  supplyMint: publicKey(this.supplyMint),
219
- supplyBank: publicKey(this.marginfiSupplyBankAccounts.bank),
223
+ supplyBank: publicKey(this.marginfiSupplyAccounts.bank),
220
224
  positionSupplyTa: publicKey(this.positionSupplyTa),
221
225
  debtMint: publicKey(this.debtMint),
222
- debtBank: publicKey(this.marginfiDebtBankAccounts.bank),
226
+ debtBank: publicKey(this.marginfiDebtAccounts.bank),
223
227
  positionDebtTa: publicKey(this.positionDebtTa),
224
228
  signerDebtTa: signerDebtTa,
225
229
  positionData: {
@@ -239,10 +243,10 @@ export class SolautoMarginfiClient extends SolautoClient {
239
243
  marginfiProgram: publicKey(MARGINFI_PROGRAM_ID),
240
244
  marginfiGroup: publicKey(this.marginfiGroup),
241
245
  marginfiAccount: publicKey(this.marginfiAccount),
242
- supplyBank: publicKey(this.marginfiSupplyBankAccounts.bank),
243
- supplyPriceOracle: publicKey(this.marginfiSupplyBankAccounts.priceOracle),
244
- debtBank: publicKey(this.marginfiDebtBankAccounts.bank),
245
- debtPriceOracle: publicKey(this.marginfiDebtBankAccounts.priceOracle),
246
+ supplyBank: publicKey(this.marginfiSupplyAccounts.bank),
247
+ supplyPriceOracle: publicKey(this.marginfiSupplyAccounts.priceOracle),
248
+ debtBank: publicKey(this.marginfiDebtAccounts.bank),
249
+ debtPriceOracle: publicKey(this.marginfiDebtAccounts.priceOracle),
246
250
  solautoPosition: publicKey(this.solautoPosition),
247
251
  });
248
252
  }
@@ -266,9 +270,9 @@ export class SolautoMarginfiClient extends SolautoClient {
266
270
  signerTokenAccount: publicKey(this.signerSupplyTa),
267
271
  marginfiAccount: publicKey(this.marginfiAccountPk),
268
272
  marginfiGroup: publicKey(this.marginfiGroup),
269
- bank: publicKey(this.marginfiSupplyBankAccounts.bank),
273
+ bank: publicKey(this.marginfiSupplyAccounts.bank),
270
274
  bankLiquidityVault: publicKey(
271
- this.marginfiSupplyBankAccounts.liquidityVault
275
+ this.marginfiSupplyAccounts.liquidityVault
272
276
  ),
273
277
  });
274
278
  }
@@ -279,12 +283,12 @@ export class SolautoMarginfiClient extends SolautoClient {
279
283
  destinationTokenAccount: publicKey(this.signerDebtTa),
280
284
  marginfiAccount: publicKey(this.marginfiAccountPk),
281
285
  marginfiGroup: publicKey(this.marginfiGroup),
282
- bank: publicKey(this.marginfiDebtBankAccounts.bank),
286
+ bank: publicKey(this.marginfiDebtAccounts.bank),
283
287
  bankLiquidityVault: publicKey(
284
- this.marginfiDebtBankAccounts.liquidityVault
288
+ this.marginfiDebtAccounts.liquidityVault
285
289
  ),
286
290
  bankLiquidityVaultAuthority: publicKey(
287
- this.marginfiDebtBankAccounts.vaultAuthority
291
+ this.marginfiDebtAccounts.vaultAuthority
288
292
  ),
289
293
  });
290
294
  }
@@ -297,9 +301,9 @@ export class SolautoMarginfiClient extends SolautoClient {
297
301
  signerTokenAccount: publicKey(this.signerDebtTa),
298
302
  marginfiAccount: publicKey(this.marginfiAccountPk),
299
303
  marginfiGroup: publicKey(this.marginfiGroup),
300
- bank: publicKey(this.marginfiDebtBankAccounts.bank),
304
+ bank: publicKey(this.marginfiDebtAccounts.bank),
301
305
  bankLiquidityVault: publicKey(
302
- this.marginfiDebtBankAccounts.liquidityVault
306
+ this.marginfiDebtAccounts.liquidityVault
303
307
  ),
304
308
  });
305
309
  }
@@ -312,12 +316,12 @@ export class SolautoMarginfiClient extends SolautoClient {
312
316
  destinationTokenAccount: publicKey(this.signerSupplyTa),
313
317
  marginfiAccount: publicKey(this.marginfiAccountPk),
314
318
  marginfiGroup: publicKey(this.marginfiGroup),
315
- bank: publicKey(this.marginfiSupplyBankAccounts.bank),
319
+ bank: publicKey(this.marginfiSupplyAccounts.bank),
316
320
  bankLiquidityVault: publicKey(
317
- this.marginfiSupplyBankAccounts.liquidityVault
321
+ this.marginfiSupplyAccounts.liquidityVault
318
322
  ),
319
323
  bankLiquidityVaultAuthority: publicKey(
320
- this.marginfiSupplyBankAccounts.vaultAuthority
324
+ this.marginfiSupplyAccounts.vaultAuthority
321
325
  ),
322
326
  });
323
327
  }
@@ -336,9 +340,9 @@ export class SolautoMarginfiClient extends SolautoClient {
336
340
  ? this.signerSupplyTa
337
341
  : this.positionSupplyTa
338
342
  );
339
- vaultSupplyTa = publicKey(this.marginfiSupplyBankAccounts.liquidityVault);
343
+ vaultSupplyTa = publicKey(this.marginfiSupplyAccounts.liquidityVault);
340
344
  supplyVaultAuthority = publicKey(
341
- this.marginfiSupplyBankAccounts.vaultAuthority
345
+ this.marginfiSupplyAccounts.vaultAuthority
342
346
  );
343
347
  }
344
348
 
@@ -351,9 +355,9 @@ export class SolautoMarginfiClient extends SolautoClient {
351
355
  ? this.signerDebtTa
352
356
  : this.positionDebtTa
353
357
  );
354
- vaultDebtTa = publicKey(this.marginfiDebtBankAccounts.liquidityVault);
358
+ vaultDebtTa = publicKey(this.marginfiDebtAccounts.liquidityVault);
355
359
  debtVaultAuthority = publicKey(
356
- this.marginfiDebtBankAccounts.vaultAuthority
360
+ this.marginfiDebtAccounts.vaultAuthority
357
361
  );
358
362
  }
359
363
 
@@ -361,9 +365,9 @@ export class SolautoMarginfiClient extends SolautoClient {
361
365
  let debtPriceOracle: UmiPublicKey | undefined = undefined;
362
366
  if (args.__kind === "Withdraw" || args.__kind === "Borrow") {
363
367
  supplyPriceOracle = publicKey(
364
- this.marginfiSupplyBankAccounts.priceOracle
368
+ this.marginfiSupplyAccounts.priceOracle
365
369
  );
366
- debtPriceOracle = publicKey(this.marginfiDebtBankAccounts.priceOracle);
370
+ debtPriceOracle = publicKey(this.marginfiDebtAccounts.priceOracle);
367
371
  }
368
372
 
369
373
  return marginfiProtocolInteraction(this.umi, {
@@ -372,12 +376,12 @@ export class SolautoMarginfiClient extends SolautoClient {
372
376
  solautoPosition: publicKey(this.solautoPosition),
373
377
  marginfiGroup: publicKey(this.marginfiGroup),
374
378
  marginfiAccount: publicKey(this.marginfiAccountPk),
375
- supplyBank: publicKey(this.marginfiSupplyBankAccounts.bank),
379
+ supplyBank: publicKey(this.marginfiSupplyAccounts.bank),
376
380
  supplyPriceOracle,
377
381
  positionSupplyTa,
378
382
  vaultSupplyTa,
379
383
  supplyVaultAuthority,
380
- debtBank: publicKey(this.marginfiDebtBankAccounts.bank),
384
+ debtBank: publicKey(this.marginfiDebtAccounts.bank),
381
385
  debtPriceOracle,
382
386
  positionDebtTa,
383
387
  vaultDebtTa,
@@ -392,7 +396,7 @@ export class SolautoMarginfiClient extends SolautoClient {
392
396
  rebalanceType: SolautoRebalanceTypeArgs,
393
397
  flashLoan?: FlashLoanDetails,
394
398
  targetLiqUtilizationRateBps?: number,
395
- limitGapBps?: number,
399
+ limitGapBps?: number
396
400
  ): TransactionBuilder {
397
401
  const inputIsSupply = swapDetails.inputMint.equals(this.supplyMint);
398
402
  const outputIsSupply = swapDetails.outputMint.equals(this.supplyMint);
@@ -412,10 +416,9 @@ export class SolautoMarginfiClient extends SolautoClient {
412
416
  solautoFeesSupplyTa:
413
417
  rebalanceStep === "B" ? publicKey(this.solautoFeesSupplyTa) : undefined,
414
418
  authorityReferralState: publicKey(this.authorityReferralState),
415
- referredBySupplyTa:
416
- rebalanceStep === "B" && this.referredBySupplyTa
417
- ? publicKey(this.referredBySupplyTa)
418
- : undefined,
419
+ referredBySupplyTa: this.referredBySupplyTa
420
+ ? publicKey(this.referredBySupplyTa)
421
+ : undefined,
419
422
  solautoPosition: publicKey(this.solautoPosition),
420
423
  marginfiGroup: publicKey(this.marginfiGroup),
421
424
  marginfiAccount: publicKey(this.marginfiAccountPk),
@@ -425,27 +428,27 @@ export class SolautoMarginfiClient extends SolautoClient {
425
428
  swapDetails.inputMint
426
429
  )
427
430
  ),
428
- supplyBank: publicKey(this.marginfiSupplyBankAccounts.bank),
429
- supplyPriceOracle: publicKey(this.marginfiSupplyBankAccounts.priceOracle),
431
+ supplyBank: publicKey(this.marginfiSupplyAccounts.bank),
432
+ supplyPriceOracle: publicKey(this.marginfiSupplyAccounts.priceOracle),
430
433
  positionSupplyTa: publicKey(this.positionSupplyTa),
431
434
  signerSupplyTa: this.selfManaged
432
435
  ? publicKey(this.signerSupplyTa)
433
436
  : undefined,
434
437
  vaultSupplyTa: needSupplyAccounts
435
- ? publicKey(this.marginfiSupplyBankAccounts.liquidityVault)
438
+ ? publicKey(this.marginfiSupplyAccounts.liquidityVault)
436
439
  : undefined,
437
440
  supplyVaultAuthority: needSupplyAccounts
438
- ? publicKey(this.marginfiSupplyBankAccounts.vaultAuthority)
441
+ ? publicKey(this.marginfiSupplyAccounts.vaultAuthority)
439
442
  : undefined,
440
- debtBank: publicKey(this.marginfiDebtBankAccounts.bank),
441
- debtPriceOracle: publicKey(this.marginfiDebtBankAccounts.priceOracle),
443
+ debtBank: publicKey(this.marginfiDebtAccounts.bank),
444
+ debtPriceOracle: publicKey(this.marginfiDebtAccounts.priceOracle),
442
445
  positionDebtTa: publicKey(this.positionDebtTa),
443
446
  signerDebtTa: this.selfManaged ? publicKey(this.signerDebtTa) : undefined,
444
447
  vaultDebtTa: needDebtAccounts
445
- ? publicKey(this.marginfiDebtBankAccounts.liquidityVault)
448
+ ? publicKey(this.marginfiDebtAccounts.liquidityVault)
446
449
  : undefined,
447
450
  debtVaultAuthority: needDebtAccounts
448
- ? publicKey(this.marginfiDebtBankAccounts.vaultAuthority)
451
+ ? publicKey(this.marginfiDebtAccounts.vaultAuthority)
449
452
  : undefined,
450
453
  rebalanceType,
451
454
  targetLiqUtilizationRateBps: targetLiqUtilizationRateBps ?? null,
@@ -458,8 +461,8 @@ export class SolautoMarginfiClient extends SolautoClient {
458
461
  destinationTokenAccount: PublicKey
459
462
  ): TransactionBuilder {
460
463
  const bank = flashLoanDetails.mint.equals(this.supplyMint)
461
- ? this.marginfiSupplyBankAccounts
462
- : this.marginfiDebtBankAccounts;
464
+ ? this.marginfiSupplyAccounts
465
+ : this.marginfiDebtAccounts;
463
466
  return transactionBuilder()
464
467
  .add(
465
468
  lendingAccountStartFlashloan(this.umi, {
@@ -485,8 +488,8 @@ export class SolautoMarginfiClient extends SolautoClient {
485
488
 
486
489
  flashRepay(flashLoanDetails: FlashLoanDetails): TransactionBuilder {
487
490
  const bank = flashLoanDetails.mint.equals(this.supplyMint)
488
- ? this.marginfiSupplyBankAccounts
489
- : this.marginfiDebtBankAccounts;
491
+ ? this.marginfiSupplyAccounts
492
+ : this.marginfiDebtAccounts;
490
493
 
491
494
  const remainingAccounts: AccountMeta[] = [];
492
495
  let includedFlashLoanToken = false;
@@ -507,7 +510,7 @@ export class SolautoMarginfiClient extends SolautoClient {
507
510
  },
508
511
  {
509
512
  pubkey: publicKey(
510
- findMarginfiBankAccounts({ bank: x.bankPk.toString() })
513
+ findMarginfiAccounts({ bank: x.bankPk.toString() })
511
514
  .priceOracle
512
515
  ),
513
516
  isSigner: false,
@@ -1,10 +1,10 @@
1
1
  import { NATIVE_MINT } from "@solana/spl-token";
2
2
  import { USDC_MINT } from "./tokenConstants";
3
- import { MarginfiTokenAccounts } from "../types/accounts";
3
+ import { MarginfiAssetAccounts } from "../types/accounts";
4
4
 
5
5
  export const DEFAULT_MARGINFI_GROUP = "4qp6Fx6tnZkY5Wropq9wUYgtFxXKwE6viZxFHg3rdAG8";
6
6
 
7
- export const MARGINFI_ACCOUNTS: { [key: string]: MarginfiTokenAccounts } = {
7
+ export const MARGINFI_ACCOUNTS: { [key: string]: MarginfiAssetAccounts } = {
8
8
  SOL: {
9
9
  mint: NATIVE_MINT.toString(),
10
10
  bank: "CCKtUs6Cgwo4aaQUmBPmyoApH2gUDErxNZCAntD6LYGh",
@@ -206,7 +206,7 @@ export function marginfiRebalance(
206
206
  },
207
207
  supplyVaultAuthority: {
208
208
  index: 17,
209
- isWritable: false as boolean,
209
+ isWritable: true as boolean,
210
210
  value: input.supplyVaultAuthority ?? null,
211
211
  },
212
212
  debtBank: {
@@ -236,7 +236,7 @@ export function marginfiRebalance(
236
236
  },
237
237
  debtVaultAuthority: {
238
238
  index: 23,
239
- isWritable: false as boolean,
239
+ isWritable: true as boolean,
240
240
  value: input.debtVaultAuthority ?? null,
241
241
  },
242
242
  } satisfies ResolvedAccountsWithIndices;
@@ -265,19 +265,12 @@ export class TransactionsManager {
265
265
  const accountsNotInLut = ixAccounts.filter(
266
266
  (x) => !lutAccounts.includes(x)
267
267
  );
268
- console.log(
269
- "Program",
270
- ix.programId,
271
- " data ",
272
- ix.data.length,
273
- ix.keys.filter((x) => lutAccounts.includes(x.pubkey)).length * 3
274
- );
268
+ this.client.log(`Program ${ix.programId}, data len: ${ix.data.length}, LUT accounts data: ${ix.keys.filter((x) => lutAccounts.includes(x.pubkey)).length * 3}`);
275
269
  if (accountsNotInLut.length > 0) {
276
- console.log("Accounts not in LUT:");
270
+ this.client.log(`${accountsNotInLut.length} accounts not in LUT:`);
277
271
  for (const key of accountsNotInLut) {
278
- console.log(key.toString());
272
+ this.client.log(key.toString());
279
273
  }
280
- console.log(ix.keys.length);
281
274
  }
282
275
  }
283
276
  }
@@ -424,7 +417,7 @@ export class TransactionsManager {
424
417
  );
425
418
  }
426
419
  },
427
- 3,
420
+ 4,
428
421
  150,
429
422
  this.errorsToThrow
430
423
  );
@@ -1,4 +1,4 @@
1
- export interface MarginfiTokenAccounts {
1
+ export interface MarginfiAssetAccounts {
2
2
  mint: string;
3
3
  bank: string;
4
4
  liquidityVault: string;
@@ -121,7 +121,7 @@ export function retryWithExponentialBackoff<T>(
121
121
  }
122
122
 
123
123
  if (attemptNum < retries) {
124
- console.log(error.message);
124
+ console.log(error);
125
125
  setTimeout(() => {
126
126
  console.log("Retrying...");
127
127
  return attempt(attemptNum);
@@ -73,8 +73,8 @@ export async function getJupSwapTransaction(
73
73
 
74
74
  const finalPriceSlippageBps = Math.round(
75
75
  Math.max(
76
- quoteResponse.slippageBps ?? 10,
77
- Math.round(toBps(parseFloat(quoteResponse.priceImpactPct)))
76
+ quoteResponse.slippageBps,
77
+ Math.round(toBps(parseFloat(quoteResponse.priceImpactPct))) + 1
78
78
  ) *
79
79
  (1 + (swapDetails.slippageBpsIncFactor ?? 0))
80
80
  );
@@ -17,19 +17,19 @@ import {
17
17
  } from "./numberUtils";
18
18
  import { PRICES } from "../constants/solautoConstants";
19
19
  import { MARGINFI_ACCOUNTS } from "../constants/marginfiAccounts";
20
- import { MarginfiTokenAccounts } from "../types/accounts";
20
+ import { MarginfiAssetAccounts } from "../types/accounts";
21
21
  import { PositionState, PositionTokenUsage } from "../generated";
22
22
  import { USD_DECIMALS } from "../constants/generalAccounts";
23
23
  import { LivePositionUpdates } from "./solauto/generalUtils";
24
24
  import { currentUnixSecondsSolana } from "./solanaUtils";
25
25
 
26
- export function findMarginfiBankAccounts({
26
+ export function findMarginfiAccounts({
27
27
  mint,
28
28
  bank,
29
29
  }: {
30
30
  mint?: string;
31
31
  bank?: string;
32
- }): MarginfiTokenAccounts {
32
+ }): MarginfiAssetAccounts {
33
33
  for (const key in MARGINFI_ACCOUNTS) {
34
34
  const account = MARGINFI_ACCOUNTS[key];
35
35
  if (mint && account.mint.toString().toLowerCase() === mint.toLowerCase()) {
@@ -43,42 +43,73 @@ export function findMarginfiBankAccounts({
43
43
  }
44
44
 
45
45
  export async function getMaxLtvAndLiqThreshold(
46
- supplyBank: Bank,
47
- debtBank: Bank | null,
46
+ umi: Umi,
47
+ supply: {
48
+ mint: PublicKey;
49
+ bank: Bank | null;
50
+ },
51
+ debt: {
52
+ mint: PublicKey;
53
+ bank: Bank | null;
54
+ },
48
55
  supplyPrice?: number
49
56
  ): Promise<[number, number]> {
57
+ if (!supply.bank) {
58
+ supply.bank = await safeFetchBank(
59
+ umi,
60
+ publicKey(
61
+ findMarginfiAccounts({
62
+ mint: supply.mint.toString(),
63
+ }).bank
64
+ )
65
+ );
66
+ }
67
+
68
+ if (!debt.bank && !debt.mint.equals(PublicKey.default)) {
69
+ debt.bank = await safeFetchBank(
70
+ umi,
71
+ publicKey(
72
+ findMarginfiAccounts({
73
+ mint: debt.mint.toString(),
74
+ }).bank
75
+ )
76
+ );
77
+ }
78
+
50
79
  if (!supplyPrice) {
51
- const [price] = await getTokenPrices([toWeb3JsPublicKey(supplyBank.mint)]);
80
+ const [price] = await getTokenPrices([
81
+ toWeb3JsPublicKey(supply.bank!.mint),
82
+ ]);
52
83
  supplyPrice = price;
53
84
  }
54
85
 
55
- if (debtBank === null) {
86
+ if (debt.bank === null) {
56
87
  return [0, 0];
57
88
  }
58
89
 
59
90
  let maxLtv =
60
- bytesToI80F48(supplyBank.config.assetWeightInit.value) /
61
- bytesToI80F48(debtBank.config.liabilityWeightInit.value);
91
+ bytesToI80F48(supply.bank!.config.assetWeightInit.value) /
92
+ bytesToI80F48(debt.bank.config.liabilityWeightInit.value);
62
93
  const liqThreshold =
63
- bytesToI80F48(supplyBank.config.assetWeightMaint.value) /
64
- bytesToI80F48(debtBank.config.liabilityWeightMaint.value);
94
+ bytesToI80F48(supply.bank!.config.assetWeightMaint.value) /
95
+ bytesToI80F48(debt.bank.config.liabilityWeightMaint.value);
65
96
 
66
97
  const totalDepositedUsdValue =
67
98
  fromBaseUnit(
68
99
  BigInt(
69
100
  Math.round(
70
- bytesToI80F48(supplyBank!.totalAssetShares.value) *
71
- bytesToI80F48(supplyBank!.assetShareValue.value)
101
+ bytesToI80F48(supply.bank!.totalAssetShares.value) *
102
+ bytesToI80F48(supply.bank!.assetShareValue.value)
72
103
  )
73
104
  ),
74
- supplyBank!.mintDecimals
105
+ supply.bank!.mintDecimals
75
106
  ) * supplyPrice!;
76
107
  if (
77
- supplyBank!.config.totalAssetValueInitLimit !== BigInt(0) &&
78
- totalDepositedUsdValue > supplyBank!.config.totalAssetValueInitLimit
108
+ supply.bank!.config.totalAssetValueInitLimit !== BigInt(0) &&
109
+ totalDepositedUsdValue > supply.bank!.config.totalAssetValueInitLimit
79
110
  ) {
80
111
  const discount =
81
- Number(supplyBank!.config.totalAssetValueInitLimit) /
112
+ Number(supply.bank!.config.totalAssetValueInitLimit) /
82
113
  totalDepositedUsdValue;
83
114
  maxLtv = Math.round(maxLtv * Number(discount));
84
115
  }
@@ -158,7 +189,7 @@ async function getTokenUsage(
158
189
  );
159
190
  const shareValue = isAsset ? assetShareValue : liabilityShareValue;
160
191
  amountUsed = shares * shareValue + Number(amountUsedAdjustment ?? 0);
161
-
192
+
162
193
  const totalDeposited =
163
194
  bytesToI80F48(bank.totalAssetShares.value) * assetShareValue;
164
195
  amountCanBeUsed = isAsset
@@ -172,19 +203,25 @@ async function getTokenUsage(
172
203
  decimals: bank?.mintDecimals ?? 0,
173
204
  amountUsed: {
174
205
  baseUnit: BigInt(Math.round(amountUsed)),
175
- baseAmountUsdValue: bank ? toBaseUnit(
176
- fromBaseUnit(BigInt(Math.round(amountUsed)), bank.mintDecimals) *
177
- marketPrice,
178
- USD_DECIMALS
179
- ) : BigInt(0),
206
+ baseAmountUsdValue: bank
207
+ ? toBaseUnit(
208
+ fromBaseUnit(BigInt(Math.round(amountUsed)), bank.mintDecimals) *
209
+ marketPrice,
210
+ USD_DECIMALS
211
+ )
212
+ : BigInt(0),
180
213
  },
181
214
  amountCanBeUsed: {
182
215
  baseUnit: BigInt(Math.round(amountCanBeUsed)),
183
- baseAmountUsdValue: bank ? toBaseUnit(
184
- fromBaseUnit(BigInt(Math.round(amountCanBeUsed)), bank.mintDecimals) *
185
- marketPrice,
186
- USD_DECIMALS
187
- ) : BigInt(0),
216
+ baseAmountUsdValue: bank
217
+ ? toBaseUnit(
218
+ fromBaseUnit(
219
+ BigInt(Math.round(amountCanBeUsed)),
220
+ bank.mintDecimals
221
+ ) * marketPrice,
222
+ USD_DECIMALS
223
+ )
224
+ : BigInt(0),
188
225
  },
189
226
  baseAmountMarketPriceUsd: toBaseUnit(marketPrice, USD_DECIMALS),
190
227
  flashLoanFeeBps: 0,
@@ -211,18 +248,14 @@ export async function getMarginfiAccountPositionState(
211
248
  supplyMint && supplyMint !== PublicKey.default
212
249
  ? await safeFetchBank(
213
250
  umi,
214
- publicKey(
215
- findMarginfiBankAccounts({ mint: supplyMint.toString() }).bank
216
- )
251
+ publicKey(findMarginfiAccounts({ mint: supplyMint.toString() }).bank)
217
252
  )
218
253
  : null;
219
254
  let debtBank: Bank | null =
220
255
  debtMint && debtMint !== PublicKey.default
221
256
  ? await safeFetchBank(
222
257
  umi,
223
- publicKey(
224
- findMarginfiBankAccounts({ mint: debtMint.toString() }).bank
225
- )
258
+ publicKey(findMarginfiAccounts({ mint: debtMint.toString() }).bank)
226
259
  )
227
260
  : null;
228
261
 
@@ -306,8 +339,15 @@ export async function getMarginfiAccountPositionState(
306
339
 
307
340
  const supplyPrice = PRICES[supplyMint!.toString()].price;
308
341
  let [maxLtv, liqThreshold] = await getMaxLtvAndLiqThreshold(
309
- supplyBank,
310
- debtBank,
342
+ umi,
343
+ {
344
+ mint: toWeb3JsPublicKey(supplyBank.mint),
345
+ bank: supplyBank,
346
+ },
347
+ {
348
+ mint: debtBank ? toWeb3JsPublicKey(debtBank.mint) : PublicKey.default,
349
+ bank: debtBank,
350
+ },
311
351
  supplyPrice
312
352
  );
313
353
  const supplyUsd = fromBaseUnit(
@@ -111,11 +111,11 @@ describe("Solauto Marginfi tests", async () => {
111
111
  // };
112
112
  // }, "deposit")
113
113
  // );
114
-
114
+
115
115
  transactionItems.push(
116
116
  new TransactionItem(
117
117
  async (attemptNum) =>
118
- await buildSolautoRebalanceTransaction(client, 5000, attemptNum),
118
+ await buildSolautoRebalanceTransaction(client, 5500, attemptNum),
119
119
  "rebalance"
120
120
  )
121
121
  );