@haven-fi/solauto-sdk 1.0.830 → 1.0.832

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,6 +11,8 @@ export declare class MarginfiFlProvider extends FlProviderBase {
11
11
  private debtImfiAccount;
12
12
  private supplyRemainingAccounts;
13
13
  private debtRemainingAccounts;
14
+ private supplyBankHealthAccounts;
15
+ private debtBankHealthAccounts;
14
16
  initialize(): Promise<void>;
15
17
  private setAvailableBanks;
16
18
  private setIntermediaryAccount;
@@ -19,6 +21,7 @@ export declare class MarginfiFlProvider extends FlProviderBase {
19
21
  liquiditySource(source: TokenType): PublicKey;
20
22
  private liquidityBank;
21
23
  private iMfiAccount;
24
+ private bankHealthAccounts;
22
25
  liquidityAvailable(source: TokenType): bigint;
23
26
  flFeeBps(source: TokenType, signerFlashLoan?: boolean): number;
24
27
  flashBorrow(flashLoan: FlashLoanDetails, destTokenAccount: PublicKey): TransactionBuilder;
@@ -1 +1 @@
1
- {"version":3,"file":"marginfiFlProvider.d.ts","sourceRoot":"","sources":["../../../src/services/flashLoans/marginfiFlProvider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAA8B,MAAM,iBAAiB,CAAC;AACxE,OAAO,EAML,kBAAkB,EACnB,MAAM,0BAA0B,CAAC;AAclC,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAkBlD,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAQ5C,qBAAa,kBAAmB,SAAQ,cAAc;IACpD,OAAO,CAAC,wBAAwB,CAAqB;IACrD,OAAO,CAAC,yBAAyB,CAAQ;IACzC,OAAO,CAAC,uBAAuB,CAAQ;IAEvC,OAAO,CAAC,iBAAiB,CAAe;IACxC,OAAO,CAAC,eAAe,CAAe;IACtC,OAAO,CAAC,uBAAuB,CAAiB;IAChD,OAAO,CAAC,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;IAC9C,OAAO,CAAC,wBAAwB,CAAiB;IACjD,OAAO,CAAC,sBAAsB,CAAiB;IAEzC,UAAU;YAiBF,iBAAiB;YAgEjB,sBAAsB;IAsD9B,sBAAsB,IAAI,OAAO,CAAC,kBAAkB,CAAC;IAyC3D,gBAAgB,IAAI,SAAS,EAAE;IAY/B,eAAe,CAAC,MAAM,EAAE,SAAS,GAAG,SAAS;IAI7C,OAAO,CAAC,aAAa;IAMrB,OAAO,CAAC,WAAW;IAMnB,OAAO,CAAC,kBAAkB;IAM1B,kBAAkB,CAAC,MAAM,EAAE,SAAS,GAAG,MAAM;IAI7C,QAAQ,CAAC,MAAM,EAAE,SAAS,EAAE,eAAe,CAAC,EAAE,OAAO,GAAG,MAAM;IAc9D,WAAW,CACT,SAAS,EAAE,gBAAgB,EAC3B,gBAAgB,EAAE,SAAS,GAC1B,kBAAkB;IAsDrB,UAAU,CAAC,SAAS,EAAE,gBAAgB,GAAG,kBAAkB;IAiD3D,YAAY,CACV,eAAe,EAAE,SAAS,EAC1B,IAAI,EAAE,SAAS,EACf,aAAa,EAAE,SAAS;CAW3B"}
@@ -51,6 +51,22 @@ class MarginfiFlProvider extends flProviderBase_1.FlProviderBase {
51
51
  const debtBanks = mapBanksAndBalances(this.debtMint);
52
52
  this.supplyBankLiquiditySource = supplyBanks[0][1];
53
53
  this.debtBankLiquiditySource = debtBanks[0][1];
54
+ const [supplyPriceOracle, debtPriceOracle] = await Promise.all([
55
+ (0, utils_1.getMarginfiPriceOracle)(this.umi, {
56
+ data: this.supplyBankLiquiditySource,
57
+ }),
58
+ (0, utils_1.getMarginfiPriceOracle)(this.umi, {
59
+ data: this.debtBankLiquiditySource,
60
+ }),
61
+ ]);
62
+ this.supplyBankHealthAccounts = [
63
+ (0, utils_1.getAccountMeta)((0, umi_web3js_adapters_1.toWeb3JsPublicKey)(this.supplyBankLiquiditySource.publicKey)),
64
+ (0, utils_1.getAccountMeta)(supplyPriceOracle),
65
+ ];
66
+ this.debtBankHealthAccounts = [
67
+ (0, utils_1.getAccountMeta)((0, umi_web3js_adapters_1.toWeb3JsPublicKey)(this.debtBankLiquiditySource.publicKey)),
68
+ (0, utils_1.getAccountMeta)(debtPriceOracle),
69
+ ];
54
70
  }
55
71
  async setIntermediaryAccount(sources) {
56
72
  const compatibleMarginfiAccounts = this.existingMarginfiAccounts.filter((x) => x.group.toString() == this.liquidityBank(sources[0]).group);
@@ -137,6 +153,11 @@ class MarginfiFlProvider extends flProviderBase_1.FlProviderBase {
137
153
  ? this.supplyImfiAccount
138
154
  : this.debtImfiAccount;
139
155
  }
156
+ bankHealthAccounts(source) {
157
+ return source === generated_1.TokenType.Supply
158
+ ? this.supplyBankHealthAccounts
159
+ : this.debtBankHealthAccounts;
160
+ }
140
161
  liquidityAvailable(source) {
141
162
  return (0, utils_1.getBankLiquidityAvailableBaseUnit)(this.liquidityBank(source), false);
142
163
  }
@@ -154,6 +175,15 @@ class MarginfiFlProvider extends flProviderBase_1.FlProviderBase {
154
175
  const bank = this.liquidityBank(flashLoan.liquiditySource);
155
176
  const associatedBankAccs = (0, utils_1.findMarginfiAccounts)((0, umi_web3js_adapters_1.toWeb3JsPublicKey)(bank.publicKey));
156
177
  const iMfiAccount = this.iMfiAccount(flashLoan.liquiditySource);
178
+ const existingRemainingAccounts = flashLoan.liquiditySource === generated_1.TokenType.Supply
179
+ ? this.supplyRemainingAccounts
180
+ : this.debtRemainingAccounts;
181
+ // Marginfi's circuit-breaker gate scans every active bank during borrow,
182
+ // including borrows made while the account is inside a flash loan.
183
+ const borrowRemainingAccounts = [...existingRemainingAccounts];
184
+ if (!borrowRemainingAccounts.some((account) => account.pubkey.toString() === bank.publicKey.toString())) {
185
+ borrowRemainingAccounts.push(...this.bankHealthAccounts(flashLoan.liquiditySource));
186
+ }
157
187
  return (0, umi_1.transactionBuilder)()
158
188
  .add((0, marginfi_1.lendingAccountStartFlashloan)(this.umi, {
159
189
  endIndex: 0, // We set this after building the transaction
@@ -170,7 +200,7 @@ class MarginfiFlProvider extends flProviderBase_1.FlProviderBase {
170
200
  marginfiAccount: (0, umi_1.publicKey)(iMfiAccount.accountPk),
171
201
  marginfiGroup: this.liquidityBank(flashLoan.liquiditySource).group,
172
202
  signer: this.signer,
173
- }));
203
+ }).addRemainingAccounts((0, utils_1.composeRemainingAccounts)(borrowRemainingAccounts)));
174
204
  }
175
205
  flashRepay(flashLoan) {
176
206
  if (flashLoan.signerFlashLoan) {
@@ -1 +1 @@
1
- {"version":3,"file":"general.d.ts","sourceRoot":"","sources":["../../../src/utils/marginfi/general.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,WAAW,EAAE,OAAO,EAAa,GAAG,EAAE,MAAM,0BAA0B,CAAC;AAKhF,OAAO,EAAE,UAAU,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAChE,OAAO,EAEL,uBAAuB,EACxB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EACL,OAAO,EACP,IAAI,EAIJ,eAAe,EAGhB,MAAM,6BAA6B,CAAC;AASrC,wBAAgB,4BAA4B,CAAC,GAAG,CAAC,EAAE,UAAU,GAAG,OAAO,CActE;AAED,wBAAgB,sBAAsB,CAAC,GAAG,EAAE,GAAG,EAAE,WAAW,CAAC,EAAE,UAAU,OASxE;AAED,wBAAsB,yBAAyB,CAC7C,GAAG,EAAE,GAAG,EACR,eAAe,EAAE,uBAAuB,GACvC,OAAO,CAAC,SAAS,EAAE,CAAC,CA8BtB;AAED,wBAAsB,kBAAkB,CAAC,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,SAAS;;;;;GAanE;AAED,wBAAsB,sBAAsB,CAC1C,GAAG,EAAE,GAAG,EACR,IAAI,EAAE;IAAE,EAAE,CAAC,EAAE,SAAS,CAAC;IAAC,IAAI,CAAC,EAAE,IAAI,CAAA;CAAE,sBAiBtC;AAED,UAAU,wBAAyB,SAAQ,qBAAqB;IAC9D,IAAI,E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AsB,CAC1C,GAAG,EAAE,GAAG,EACR,IAAI,EAAE;IAAE,EAAE,CAAC,EAAE,SAAS,CAAC;IAAC,IAAI,CAAC,EAAE,IAAI,CAAA;CAAE,sBAiBtC;AAED,UAAU,wBAAyB,SAAQ,qBAAqB;IAC9D,IAAI,EAAE,SAAS,CAAC;CACjB;AAED,wBAAgB,oBAAoB,CAClC,IAAI,EAAE,SAAS,GACd,wBAAwB,CAyB1B;AAED,wBAAsB,0CAA0C,CAC9D,GAAG,EAAE,GAAG,EACR,OAAO,EAAE,OAAO,GACf,OAAO,CAAC,WAAW,EAAE,CAAC,CAWxB;AAED,wBAAgB,oCAAoC,CAClD,UAAU,EAAE,IAAI,EAChB,QAAQ,EAAE,IAAI,EACd,WAAW,EAAE,MAAM,GAClB,CAAC,MAAM,EAAE,MAAM,CAAC,CA6BlB;AAED,wBAAgB,oBAAoB,CAAC,eAAe,EAAE,eAAe,WASpE;AAED,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,WAAW,EAAE,GAAG,WAAW,EAAE,CAoB3E"}
@@ -141,6 +141,15 @@ function marginfiAccountEmpty(marginfiAccount) {
141
141
  function composeRemainingAccounts(accs) {
142
142
  const banksAndOracles = accs.reduce((acc, _, i) => i % 2 === 0 ? [...acc, [accs[i], accs[i + 1]]] : acc, []);
143
143
  return banksAndOracles
144
- .sort((a, b) => b[0].pubkey.toString().localeCompare(a[0].pubkey.toString()))
144
+ .sort((a, b) => {
145
+ const aBytes = (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(a[0].pubkey).toBuffer();
146
+ const bBytes = (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(b[0].pubkey).toBuffer();
147
+ for (let i = 0; i < aBytes.length; i++) {
148
+ if (aBytes[i] !== bBytes[i]) {
149
+ return bBytes[i] - aBytes[i];
150
+ }
151
+ }
152
+ return 0;
153
+ })
145
154
  .flat();
146
155
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@haven-fi/solauto-sdk",
3
- "version": "1.0.830",
3
+ "version": "1.0.832",
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",
@@ -30,6 +30,7 @@ import {
30
30
  fromBaseUnit,
31
31
  getBankLiquidityAvailableBaseUnit,
32
32
  getEmptyMarginfiAccountsByAuthority,
33
+ getAccountMeta,
33
34
  getMarginfiPriceOracle,
34
35
  getRemainingAccountsForMarginfiHealthCheck,
35
36
  getTokenAccount,
@@ -56,6 +57,8 @@ export class MarginfiFlProvider extends FlProviderBase {
56
57
  private debtImfiAccount!: IMFIAccount;
57
58
  private supplyRemainingAccounts!: AccountMeta[];
58
59
  private debtRemainingAccounts!: AccountMeta[];
60
+ private supplyBankHealthAccounts!: AccountMeta[];
61
+ private debtBankHealthAccounts!: AccountMeta[];
59
62
 
60
63
  async initialize() {
61
64
  await this.setAvailableBanks();
@@ -117,6 +120,25 @@ export class MarginfiFlProvider extends FlProviderBase {
117
120
 
118
121
  this.supplyBankLiquiditySource = supplyBanks[0][1];
119
122
  this.debtBankLiquiditySource = debtBanks[0][1];
123
+
124
+ const [supplyPriceOracle, debtPriceOracle] = await Promise.all([
125
+ getMarginfiPriceOracle(this.umi, {
126
+ data: this.supplyBankLiquiditySource,
127
+ }),
128
+ getMarginfiPriceOracle(this.umi, {
129
+ data: this.debtBankLiquiditySource,
130
+ }),
131
+ ]);
132
+ this.supplyBankHealthAccounts = [
133
+ getAccountMeta(
134
+ toWeb3JsPublicKey(this.supplyBankLiquiditySource.publicKey)
135
+ ),
136
+ getAccountMeta(supplyPriceOracle),
137
+ ];
138
+ this.debtBankHealthAccounts = [
139
+ getAccountMeta(toWeb3JsPublicKey(this.debtBankLiquiditySource.publicKey)),
140
+ getAccountMeta(debtPriceOracle),
141
+ ];
120
142
  }
121
143
 
122
144
  private async setIntermediaryAccount(sources: TokenType[]) {
@@ -242,6 +264,12 @@ export class MarginfiFlProvider extends FlProviderBase {
242
264
  : this.debtImfiAccount;
243
265
  }
244
266
 
267
+ private bankHealthAccounts(source: TokenType): AccountMeta[] {
268
+ return source === TokenType.Supply
269
+ ? this.supplyBankHealthAccounts
270
+ : this.debtBankHealthAccounts;
271
+ }
272
+
245
273
  liquidityAvailable(source: TokenType): bigint {
246
274
  return getBankLiquidityAvailableBaseUnit(this.liquidityBank(source), false);
247
275
  }
@@ -273,6 +301,22 @@ export class MarginfiFlProvider extends FlProviderBase {
273
301
  toWeb3JsPublicKey(bank.publicKey)
274
302
  );
275
303
  const iMfiAccount = this.iMfiAccount(flashLoan.liquiditySource)!;
304
+ const existingRemainingAccounts =
305
+ flashLoan.liquiditySource === TokenType.Supply
306
+ ? this.supplyRemainingAccounts
307
+ : this.debtRemainingAccounts;
308
+ // Marginfi's circuit-breaker gate scans every active bank during borrow,
309
+ // including borrows made while the account is inside a flash loan.
310
+ const borrowRemainingAccounts = [...existingRemainingAccounts];
311
+ if (
312
+ !borrowRemainingAccounts.some(
313
+ (account) => account.pubkey.toString() === bank.publicKey.toString()
314
+ )
315
+ ) {
316
+ borrowRemainingAccounts.push(
317
+ ...this.bankHealthAccounts(flashLoan.liquiditySource)
318
+ );
319
+ }
276
320
 
277
321
  return transactionBuilder()
278
322
  .add(
@@ -295,7 +339,9 @@ export class MarginfiFlProvider extends FlProviderBase {
295
339
  marginfiAccount: publicKey(iMfiAccount.accountPk),
296
340
  marginfiGroup: this.liquidityBank(flashLoan.liquiditySource).group,
297
341
  signer: this.signer,
298
- })
342
+ }).addRemainingAccounts(
343
+ composeRemainingAccounts(borrowRemainingAccounts)
344
+ )
299
345
  );
300
346
  }
301
347
 
@@ -227,8 +227,17 @@ export function composeRemainingAccounts(accs: AccountMeta[]): AccountMeta[] {
227
227
  []
228
228
  );
229
229
  return banksAndOracles
230
- .sort((a, b) =>
231
- b[0].pubkey.toString().localeCompare(a[0].pubkey.toString())
232
- )
230
+ .sort((a, b) => {
231
+ const aBytes = toWeb3JsPublicKey(a[0].pubkey).toBuffer();
232
+ const bBytes = toWeb3JsPublicKey(b[0].pubkey).toBuffer();
233
+
234
+ for (let i = 0; i < aBytes.length; i++) {
235
+ if (aBytes[i] !== bBytes[i]) {
236
+ return bBytes[i] - aBytes[i];
237
+ }
238
+ }
239
+
240
+ return 0;
241
+ })
233
242
  .flat();
234
243
  }
@@ -0,0 +1,45 @@
1
+ import { assert } from "chai";
2
+ import { describe, it } from "mocha";
3
+ import { AccountMeta, publicKey } from "@metaplex-foundation/umi";
4
+ import { composeRemainingAccounts } from "../../src";
5
+
6
+ function readonlyMeta(address: string): AccountMeta {
7
+ return {
8
+ pubkey: publicKey(address),
9
+ isSigner: false,
10
+ isWritable: false,
11
+ };
12
+ }
13
+
14
+ describe("Marginfi remaining accounts", () => {
15
+ it("sorts bank groups by descending public-key bytes", () => {
16
+ // Base58 locale ordering puts DMoq before DeyH, but Marginfi's Rust
17
+ // comparator orders their decoded public-key bytes in the opposite order.
18
+ const deYhBank = readonlyMeta(
19
+ "DeyH7QxWvnbbaVB4zFrf4hoq7Q8z1ZT14co42BGwGtfM"
20
+ );
21
+ const deYhOracle = readonlyMeta(
22
+ "SysvarC1ock11111111111111111111111111111111"
23
+ );
24
+ const dMoqBank = readonlyMeta(
25
+ "DMoqjmsuoru986HgfjqrKEvPv8YBufvBGADHUonkadC5"
26
+ );
27
+ const dMoqOracle = readonlyMeta(
28
+ "SysvarRent111111111111111111111111111111111"
29
+ );
30
+
31
+ const result = composeRemainingAccounts([
32
+ dMoqBank,
33
+ dMoqOracle,
34
+ deYhBank,
35
+ deYhOracle,
36
+ ]);
37
+
38
+ assert.deepEqual(
39
+ result.map((account) => account.pubkey.toString()),
40
+ [deYhBank, deYhOracle, dMoqBank, dMoqOracle].map((account) =>
41
+ account.pubkey.toString()
42
+ )
43
+ );
44
+ });
45
+ });