@haven-fi/solauto-sdk 1.0.40 → 1.0.42

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,
@@ -259,40 +261,41 @@ class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
259
261
  const needDebtAccounts = (!inputIsSupply && rebalanceStep === "A") ||
260
262
  (!outputIsSupply && rebalanceStep === "B") ||
261
263
  (!inputIsSupply && flashLoan !== undefined && rebalanceStep == "B");
264
+ console.log(rebalanceStep, needDebtAccounts);
262
265
  return (0, generated_1.marginfiRebalance)(this.umi, {
263
266
  signer: this.signer,
264
267
  marginfiProgram: (0, umi_1.publicKey)(marginfi_sdk_1.MARGINFI_PROGRAM_ID),
265
268
  ixsSysvar: (0, umi_1.publicKey)(web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY),
266
269
  solautoFeesSupplyTa: rebalanceStep === "B" ? (0, umi_1.publicKey)(this.solautoFeesSupplyTa) : undefined,
267
270
  authorityReferralState: (0, umi_1.publicKey)(this.authorityReferralState),
268
- referredBySupplyTa: rebalanceStep === "B" && this.referredBySupplyTa
271
+ referredBySupplyTa: this.referredBySupplyTa
269
272
  ? (0, umi_1.publicKey)(this.referredBySupplyTa)
270
273
  : undefined,
271
274
  solautoPosition: (0, umi_1.publicKey)(this.solautoPosition),
272
275
  marginfiGroup: (0, umi_1.publicKey)(this.marginfiGroup),
273
276
  marginfiAccount: (0, umi_1.publicKey)(this.marginfiAccountPk),
274
277
  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),
278
+ supplyBank: (0, umi_1.publicKey)(this.marginfiSupplyAccounts.bank),
279
+ supplyPriceOracle: (0, umi_1.publicKey)(this.marginfiSupplyAccounts.priceOracle),
277
280
  positionSupplyTa: (0, umi_1.publicKey)(this.positionSupplyTa),
278
281
  signerSupplyTa: this.selfManaged
279
282
  ? (0, umi_1.publicKey)(this.signerSupplyTa)
280
283
  : undefined,
281
284
  vaultSupplyTa: needSupplyAccounts
282
- ? (0, umi_1.publicKey)(this.marginfiSupplyBankAccounts.liquidityVault)
285
+ ? (0, umi_1.publicKey)(this.marginfiSupplyAccounts.liquidityVault)
283
286
  : undefined,
284
287
  supplyVaultAuthority: needSupplyAccounts
285
- ? (0, umi_1.publicKey)(this.marginfiSupplyBankAccounts.vaultAuthority)
288
+ ? (0, umi_1.publicKey)(this.marginfiSupplyAccounts.vaultAuthority)
286
289
  : undefined,
287
- debtBank: (0, umi_1.publicKey)(this.marginfiDebtBankAccounts.bank),
288
- debtPriceOracle: (0, umi_1.publicKey)(this.marginfiDebtBankAccounts.priceOracle),
290
+ debtBank: (0, umi_1.publicKey)(this.marginfiDebtAccounts.bank),
291
+ debtPriceOracle: (0, umi_1.publicKey)(this.marginfiDebtAccounts.priceOracle),
289
292
  positionDebtTa: (0, umi_1.publicKey)(this.positionDebtTa),
290
293
  signerDebtTa: this.selfManaged ? (0, umi_1.publicKey)(this.signerDebtTa) : undefined,
291
294
  vaultDebtTa: needDebtAccounts
292
- ? (0, umi_1.publicKey)(this.marginfiDebtBankAccounts.liquidityVault)
295
+ ? (0, umi_1.publicKey)(this.marginfiDebtAccounts.liquidityVault)
293
296
  : undefined,
294
297
  debtVaultAuthority: needDebtAccounts
295
- ? (0, umi_1.publicKey)(this.marginfiDebtBankAccounts.vaultAuthority)
298
+ ? (0, umi_1.publicKey)(this.marginfiDebtAccounts.vaultAuthority)
296
299
  : undefined,
297
300
  rebalanceType,
298
301
  targetLiqUtilizationRateBps: targetLiqUtilizationRateBps ?? null,
@@ -301,8 +304,8 @@ class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
301
304
  }
302
305
  flashBorrow(flashLoanDetails, destinationTokenAccount) {
303
306
  const bank = flashLoanDetails.mint.equals(this.supplyMint)
304
- ? this.marginfiSupplyBankAccounts
305
- : this.marginfiDebtBankAccounts;
307
+ ? this.marginfiSupplyAccounts
308
+ : this.marginfiDebtAccounts;
306
309
  return (0, umi_1.transactionBuilder)()
307
310
  .add((0, marginfi_sdk_1.lendingAccountStartFlashloan)(this.umi, {
308
311
  endIndex: 0,
@@ -323,8 +326,8 @@ class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
323
326
  }
324
327
  flashRepay(flashLoanDetails) {
325
328
  const bank = flashLoanDetails.mint.equals(this.supplyMint)
326
- ? this.marginfiSupplyBankAccounts
327
- : this.marginfiDebtBankAccounts;
329
+ ? this.marginfiSupplyAccounts
330
+ : this.marginfiDebtAccounts;
328
331
  const remainingAccounts = [];
329
332
  let includedFlashLoanToken = false;
330
333
  if (this.intermediaryMarginfiAccount) {
@@ -340,7 +343,7 @@ class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
340
343
  isWritable: false,
341
344
  },
342
345
  {
343
- pubkey: (0, umi_1.publicKey)((0, marginfiUtils_1.findMarginfiBankAccounts)({ bank: x.bankPk.toString() })
346
+ pubkey: (0, umi_1.publicKey)((0, marginfiUtils_1.findMarginfiAccounts)({ bank: x.bankPk.toString() })
344
347
  .priceOracle),
345
348
  isSigner: false,
346
349
  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,4 +1,4 @@
1
- export interface MarginfiTokenAccounts {
1
+ export interface MarginfiAssetAccounts {
2
2
  mint: string;
3
3
  bank: string;
4
4
  liquidityVault: string;
@@ -1,7 +1,7 @@
1
1
  import { PublicKey } from "@solana/web3.js";
2
2
  import { LendingPlatform } from "../generated";
3
3
  export interface SolautoPositionDetails {
4
- publicKey: PublicKey;
4
+ publicKey?: PublicKey;
5
5
  authority: PublicKey;
6
6
  positionId: number;
7
7
  lendingPlatform: LendingPlatform;
@@ -1 +1 @@
1
- {"version":3,"file":"solauto.d.ts","sourceRoot":"","sources":["../../src/types/solauto.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAE/C,MAAM,WAAW,sBAAsB;IACrC,SAAS,EAAE,SAAS,CAAC;IACrB,SAAS,EAAE,SAAS,CAAC;IACrB,UAAU,EAAE,MAAM,CAAC;IACnB,eAAe,EAAE,eAAe,CAAC;IACjC,eAAe,CAAC,EAAE,SAAS,CAAC;IAC5B,UAAU,CAAC,EAAE,SAAS,CAAC;IACvB,QAAQ,CAAC,EAAE,SAAS,CAAC;CACtB"}
1
+ {"version":3,"file":"solauto.d.ts","sourceRoot":"","sources":["../../src/types/solauto.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAE/C,MAAM,WAAW,sBAAsB;IACrC,SAAS,CAAC,EAAE,SAAS,CAAC;IACtB,SAAS,EAAE,SAAS,CAAC;IACrB,UAAU,EAAE,MAAM,CAAC;IACnB,eAAe,EAAE,eAAe,CAAC;IACjC,eAAe,CAAC,EAAE,SAAS,CAAC;IAC5B,UAAU,CAAC,EAAE,SAAS,CAAC;IACvB,QAAQ,CAAC,EAAE,SAAS,CAAC;CACtB"}
@@ -1,13 +1,13 @@
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;
10
+ }): MarginfiAssetAccounts;
11
11
  export declare function getMaxLtvAndLiqThreshold(supplyBank: Bank, debtBank: Bank | null, supplyPrice?: number): Promise<[number, number]>;
12
12
  export declare function getAllMarginfiAccountsByAuthority(umi: Umi, authority: PublicKey, compatibleWithSolauto?: boolean): Promise<{
13
13
  marginfiAccount: 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,IAAI,EAKL,MAAM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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,7 +23,7 @@ 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;
26
+ exports.findMarginfiAccounts = findMarginfiAccounts;
27
27
  async function getMaxLtvAndLiqThreshold(supplyBank, debtBank, supplyPrice) {
28
28
  if (!supplyPrice) {
29
29
  const [price] = await (0, generalUtils_1.getTokenPrices)([(0, umi_web3js_adapters_1.toWeb3JsPublicKey)(supplyBank.mint)]);
@@ -125,10 +125,10 @@ async function getTokenUsage(umi, bank, isAsset, shares, amountUsedAdjustment) {
125
125
  async function getMarginfiAccountPositionState(umi, marginfiAccountPk, supplyMint, debtMint, livePositionUpdates) {
126
126
  let marginfiAccount = await (0, marginfi_sdk_1.safeFetchMarginfiAccount)(umi, (0, umi_1.publicKey)(marginfiAccountPk));
127
127
  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))
128
+ ? await (0, marginfi_sdk_1.safeFetchBank)(umi, (0, umi_1.publicKey)(findMarginfiAccounts({ mint: supplyMint.toString() }).bank))
129
129
  : null;
130
130
  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))
131
+ ? await (0, marginfi_sdk_1.safeFetchBank)(umi, (0, umi_1.publicKey)(findMarginfiAccounts({ mint: debtMint.toString() }).bank))
132
132
  : null;
133
133
  let supplyUsage = undefined;
134
134
  let debtUsage = undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@haven-fi/solauto-sdk",
3
- "version": "1.0.40",
3
+ "version": "1.0.42",
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);
@@ -405,6 +409,7 @@ export class SolautoMarginfiClient extends SolautoClient {
405
409
  (!outputIsSupply && rebalanceStep === "B") ||
406
410
  (!inputIsSupply && flashLoan !== undefined && rebalanceStep == "B");
407
411
 
412
+ console.log(rebalanceStep, needDebtAccounts);
408
413
  return marginfiRebalance(this.umi, {
409
414
  signer: this.signer,
410
415
  marginfiProgram: publicKey(MARGINFI_PROGRAM_ID),
@@ -412,10 +417,9 @@ export class SolautoMarginfiClient extends SolautoClient {
412
417
  solautoFeesSupplyTa:
413
418
  rebalanceStep === "B" ? publicKey(this.solautoFeesSupplyTa) : undefined,
414
419
  authorityReferralState: publicKey(this.authorityReferralState),
415
- referredBySupplyTa:
416
- rebalanceStep === "B" && this.referredBySupplyTa
417
- ? publicKey(this.referredBySupplyTa)
418
- : undefined,
420
+ referredBySupplyTa: this.referredBySupplyTa
421
+ ? publicKey(this.referredBySupplyTa)
422
+ : undefined,
419
423
  solautoPosition: publicKey(this.solautoPosition),
420
424
  marginfiGroup: publicKey(this.marginfiGroup),
421
425
  marginfiAccount: publicKey(this.marginfiAccountPk),
@@ -425,27 +429,27 @@ export class SolautoMarginfiClient extends SolautoClient {
425
429
  swapDetails.inputMint
426
430
  )
427
431
  ),
428
- supplyBank: publicKey(this.marginfiSupplyBankAccounts.bank),
429
- supplyPriceOracle: publicKey(this.marginfiSupplyBankAccounts.priceOracle),
432
+ supplyBank: publicKey(this.marginfiSupplyAccounts.bank),
433
+ supplyPriceOracle: publicKey(this.marginfiSupplyAccounts.priceOracle),
430
434
  positionSupplyTa: publicKey(this.positionSupplyTa),
431
435
  signerSupplyTa: this.selfManaged
432
436
  ? publicKey(this.signerSupplyTa)
433
437
  : undefined,
434
438
  vaultSupplyTa: needSupplyAccounts
435
- ? publicKey(this.marginfiSupplyBankAccounts.liquidityVault)
439
+ ? publicKey(this.marginfiSupplyAccounts.liquidityVault)
436
440
  : undefined,
437
441
  supplyVaultAuthority: needSupplyAccounts
438
- ? publicKey(this.marginfiSupplyBankAccounts.vaultAuthority)
442
+ ? publicKey(this.marginfiSupplyAccounts.vaultAuthority)
439
443
  : undefined,
440
- debtBank: publicKey(this.marginfiDebtBankAccounts.bank),
441
- debtPriceOracle: publicKey(this.marginfiDebtBankAccounts.priceOracle),
444
+ debtBank: publicKey(this.marginfiDebtAccounts.bank),
445
+ debtPriceOracle: publicKey(this.marginfiDebtAccounts.priceOracle),
442
446
  positionDebtTa: publicKey(this.positionDebtTa),
443
447
  signerDebtTa: this.selfManaged ? publicKey(this.signerDebtTa) : undefined,
444
448
  vaultDebtTa: needDebtAccounts
445
- ? publicKey(this.marginfiDebtBankAccounts.liquidityVault)
449
+ ? publicKey(this.marginfiDebtAccounts.liquidityVault)
446
450
  : undefined,
447
451
  debtVaultAuthority: needDebtAccounts
448
- ? publicKey(this.marginfiDebtBankAccounts.vaultAuthority)
452
+ ? publicKey(this.marginfiDebtAccounts.vaultAuthority)
449
453
  : undefined,
450
454
  rebalanceType,
451
455
  targetLiqUtilizationRateBps: targetLiqUtilizationRateBps ?? null,
@@ -458,8 +462,8 @@ export class SolautoMarginfiClient extends SolautoClient {
458
462
  destinationTokenAccount: PublicKey
459
463
  ): TransactionBuilder {
460
464
  const bank = flashLoanDetails.mint.equals(this.supplyMint)
461
- ? this.marginfiSupplyBankAccounts
462
- : this.marginfiDebtBankAccounts;
465
+ ? this.marginfiSupplyAccounts
466
+ : this.marginfiDebtAccounts;
463
467
  return transactionBuilder()
464
468
  .add(
465
469
  lendingAccountStartFlashloan(this.umi, {
@@ -485,8 +489,8 @@ export class SolautoMarginfiClient extends SolautoClient {
485
489
 
486
490
  flashRepay(flashLoanDetails: FlashLoanDetails): TransactionBuilder {
487
491
  const bank = flashLoanDetails.mint.equals(this.supplyMint)
488
- ? this.marginfiSupplyBankAccounts
489
- : this.marginfiDebtBankAccounts;
492
+ ? this.marginfiSupplyAccounts
493
+ : this.marginfiDebtAccounts;
490
494
 
491
495
  const remainingAccounts: AccountMeta[] = [];
492
496
  let includedFlashLoanToken = false;
@@ -507,7 +511,7 @@ export class SolautoMarginfiClient extends SolautoClient {
507
511
  },
508
512
  {
509
513
  pubkey: publicKey(
510
- findMarginfiBankAccounts({ bank: x.bankPk.toString() })
514
+ findMarginfiAccounts({ bank: x.bankPk.toString() })
511
515
  .priceOracle
512
516
  ),
513
517
  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;
@@ -1,4 +1,4 @@
1
- export interface MarginfiTokenAccounts {
1
+ export interface MarginfiAssetAccounts {
2
2
  mint: string;
3
3
  bank: string;
4
4
  liquidityVault: string;
@@ -2,7 +2,7 @@ import { PublicKey } from "@solana/web3.js";
2
2
  import { LendingPlatform } from "../generated";
3
3
 
4
4
  export interface SolautoPositionDetails {
5
- publicKey: PublicKey;
5
+ publicKey?: PublicKey;
6
6
  authority: PublicKey;
7
7
  positionId: number;
8
8
  lendingPlatform: LendingPlatform;
@@ -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()) {
@@ -212,7 +212,7 @@ export async function getMarginfiAccountPositionState(
212
212
  ? await safeFetchBank(
213
213
  umi,
214
214
  publicKey(
215
- findMarginfiBankAccounts({ mint: supplyMint.toString() }).bank
215
+ findMarginfiAccounts({ mint: supplyMint.toString() }).bank
216
216
  )
217
217
  )
218
218
  : null;
@@ -221,7 +221,7 @@ export async function getMarginfiAccountPositionState(
221
221
  ? await safeFetchBank(
222
222
  umi,
223
223
  publicKey(
224
- findMarginfiBankAccounts({ mint: debtMint.toString() }).bank
224
+ findMarginfiAccounts({ mint: debtMint.toString() }).bank
225
225
  )
226
226
  )
227
227
  : null;
@@ -115,7 +115,7 @@ describe("Solauto Marginfi tests", async () => {
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
  );