@haven-fi/solauto-sdk 1.0.682 → 1.0.684
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.
- package/dist/services/rebalance/rebalanceTxBuilder.d.ts +2 -2
- package/dist/services/rebalance/rebalanceTxBuilder.d.ts.map +1 -1
- package/dist/services/rebalance/rebalanceTxBuilder.js +6 -5
- package/dist/services/transactions/transactionsManager.d.ts +3 -3
- package/dist/services/transactions/transactionsManager.d.ts.map +1 -1
- package/dist/services/transactions/transactionsManager.js +9 -9
- package/dist/utils/instructionUtils.d.ts.map +1 -1
- package/dist/utils/instructionUtils.js +1 -1
- package/local/txSandbox.ts +22 -62
- package/package.json +1 -1
- package/src/services/rebalance/rebalanceTxBuilder.ts +8 -10
- package/src/services/transactions/transactionsManager.ts +12 -8
- package/src/utils/instructionUtils.ts +4 -3
@@ -3,13 +3,13 @@ import { TransactionItemInputs } from "../../types";
|
|
3
3
|
export declare class RebalanceTxBuilder {
|
4
4
|
private client;
|
5
5
|
private targetLiqUtilizationRateBps?;
|
6
|
+
private optimizeSize?;
|
6
7
|
private values;
|
7
8
|
private rebalanceType;
|
8
9
|
private swapManager;
|
9
10
|
private flRequirements?;
|
10
11
|
private priceType;
|
11
|
-
|
12
|
-
constructor(client: SolautoClient, targetLiqUtilizationRateBps?: number | undefined);
|
12
|
+
constructor(client: SolautoClient, targetLiqUtilizationRateBps?: number | undefined, optimizeSize?: boolean | undefined);
|
13
13
|
private shouldProceedWithRebalance;
|
14
14
|
private getRebalanceValues;
|
15
15
|
private getFlLiquiditySource;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"rebalanceTxBuilder.d.ts","sourceRoot":"","sources":["../../../src/services/rebalance/rebalanceTxBuilder.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAC3C,OAAO,EAGL,qBAAqB,EACtB,MAAM,aAAa,CAAC;
|
1
|
+
{"version":3,"file":"rebalanceTxBuilder.d.ts","sourceRoot":"","sources":["../../../src/services/rebalance/rebalanceTxBuilder.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAC3C,OAAO,EAGL,qBAAqB,EACtB,MAAM,aAAa,CAAC;AA8BrB,qBAAa,kBAAkB;IAQ3B,OAAO,CAAC,MAAM;IACd,OAAO,CAAC,2BAA2B,CAAC;IACpC,OAAO,CAAC,YAAY,CAAC;IATvB,OAAO,CAAC,MAAM,CAAmB;IACjC,OAAO,CAAC,aAAa,CAAwB;IAC7C,OAAO,CAAC,WAAW,CAAwB;IAC3C,OAAO,CAAC,cAAc,CAAC,CAAwB;IAC/C,OAAO,CAAC,SAAS,CAAiC;gBAGxC,MAAM,EAAE,aAAa,EACrB,2BAA2B,CAAC,EAAE,MAAM,YAAA,EACpC,YAAY,CAAC,EAAE,OAAO,YAAA;IAGhC,OAAO,CAAC,0BAA0B;IAclC,OAAO,CAAC,kBAAkB;IAc1B,OAAO,CAAC,oBAAoB;IA2C5B,OAAO,CAAC,iCAAiC;YAgC3B,qBAAqB;IA2CnC,OAAO,CAAC,mBAAmB;IAwB3B,OAAO,CAAC,gBAAgB;IA+BxB,OAAO,CAAC,yBAAyB;YAqBnB,mBAAmB;YAwBnB,sBAAsB;YAyBtB,mBAAmB;IAmEpB,gBAAgB,CAC3B,UAAU,EAAE,MAAM,GACjB,OAAO,CAAC,qBAAqB,GAAG,SAAS,CAAC;CAe9C"}
|
@@ -10,9 +10,10 @@ const rebalanceValues_1 = require("./rebalanceValues");
|
|
10
10
|
const solautoFees_1 = require("./solautoFees");
|
11
11
|
const rebalanceSwapManager_1 = require("./rebalanceSwapManager");
|
12
12
|
class RebalanceTxBuilder {
|
13
|
-
constructor(client, targetLiqUtilizationRateBps) {
|
13
|
+
constructor(client, targetLiqUtilizationRateBps, optimizeSize) {
|
14
14
|
this.client = client;
|
15
15
|
this.targetLiqUtilizationRateBps = targetLiqUtilizationRateBps;
|
16
|
+
this.optimizeSize = optimizeSize;
|
16
17
|
this.priceType = generated_1.PriceType.Realtime;
|
17
18
|
}
|
18
19
|
shouldProceedWithRebalance() {
|
@@ -64,14 +65,14 @@ class RebalanceTxBuilder {
|
|
64
65
|
});
|
65
66
|
return intermediaryLiqUtilizationRateBps;
|
66
67
|
}
|
67
|
-
async flashLoanRequirements(
|
68
|
+
async flashLoanRequirements() {
|
68
69
|
const intermediaryLiqUtilizationRateBps = this.intermediaryLiqUtilizationRateBps();
|
69
|
-
const maxLtvRateBps = (0, utils_1.getMaxLiqUtilizationRateBps)(this.client.pos.state.maxLtvBps, this.client.pos.state.liqThresholdBps, 0.
|
70
|
+
const maxLtvRateBps = (0, utils_1.getMaxLiqUtilizationRateBps)(this.client.pos.state.maxLtvBps, this.client.pos.state.liqThresholdBps, 0.015);
|
70
71
|
if (intermediaryLiqUtilizationRateBps < maxLtvRateBps) {
|
71
72
|
return undefined;
|
72
73
|
}
|
73
74
|
const stdFlLiquiditySource = this.getFlLiquiditySource(this.client.flProvider.liquidityAvailable(generated_1.TokenType.Supply), this.client.flProvider.liquidityAvailable(generated_1.TokenType.Debt));
|
74
|
-
if (
|
75
|
+
if (stdFlLiquiditySource === undefined || this.optimizeSize) {
|
75
76
|
(0, utils_1.consoleLog)("Checking signer liquidity");
|
76
77
|
const { supplyBalance, debtBalance } = await this.client.signerBalances();
|
77
78
|
const signerFlLiquiditySource = this.getFlLiquiditySource(supplyBalance, debtBalance);
|
@@ -158,7 +159,7 @@ class RebalanceTxBuilder {
|
|
158
159
|
return false;
|
159
160
|
}
|
160
161
|
this.values = rebalanceValues;
|
161
|
-
this.flRequirements = await this.flashLoanRequirements(
|
162
|
+
this.flRequirements = await this.flashLoanRequirements();
|
162
163
|
if (this.flRequirements?.flFeeBps) {
|
163
164
|
this.values = this.getRebalanceValues();
|
164
165
|
}
|
@@ -6,15 +6,15 @@ export declare class TransactionTooLargeError extends Error {
|
|
6
6
|
constructor(message: string);
|
7
7
|
}
|
8
8
|
export declare class TransactionItem {
|
9
|
-
fetchTx: (attemptNum: number) => Promise<TransactionItemInputs | undefined>;
|
9
|
+
fetchTx: (attemptNum: number, prevError?: Error) => Promise<TransactionItemInputs | undefined>;
|
10
10
|
name?: string | undefined;
|
11
11
|
lookupTableAddresses: string[];
|
12
12
|
tx?: TransactionBuilder;
|
13
13
|
initialized: boolean;
|
14
14
|
orderPrio: number;
|
15
|
-
constructor(fetchTx: (attemptNum: number) => Promise<TransactionItemInputs | undefined>, name?: string | undefined);
|
15
|
+
constructor(fetchTx: (attemptNum: number, prevError?: Error) => Promise<TransactionItemInputs | undefined>, name?: string | undefined);
|
16
16
|
initialize(): Promise<void>;
|
17
|
-
refetch(attemptNum: number): Promise<void>;
|
17
|
+
refetch(attemptNum: number, prevError?: Error): Promise<void>;
|
18
18
|
uniqueAccounts(): string[];
|
19
19
|
}
|
20
20
|
export declare enum TransactionStatus {
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"transactionsManager.d.ts","sourceRoot":"","sources":["../../../src/services/transactions/transactionsManager.ts"],"names":[],"mappings":"AAKA,OAAO,EAGL,kBAAkB,EAEnB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EACL,kBAAkB,EAElB,qBAAqB,EACrB,kBAAkB,EACnB,MAAM,aAAa,CAAC;AAMrB,OAAO,EAEL,aAAa,EAQd,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,aAAa,EAAE,oBAAoB,EAAa,MAAM,YAAY,CAAC;AAM5E,qBAAa,wBAAyB,SAAQ,KAAK;gBACrC,OAAO,EAAE,MAAM;CAK5B;AAqCD,qBAAa,eAAe;IAOjB,OAAO,EAAE,CACd,UAAU,EAAE,MAAM,
|
1
|
+
{"version":3,"file":"transactionsManager.d.ts","sourceRoot":"","sources":["../../../src/services/transactions/transactionsManager.ts"],"names":[],"mappings":"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"}
|
@@ -52,8 +52,8 @@ class TransactionItem {
|
|
52
52
|
await this.refetch(0);
|
53
53
|
this.initialized = true;
|
54
54
|
}
|
55
|
-
async refetch(attemptNum) {
|
56
|
-
const resp = await this.fetchTx(attemptNum);
|
55
|
+
async refetch(attemptNum, prevError) {
|
56
|
+
const resp = await this.fetchTx(attemptNum, prevError);
|
57
57
|
this.tx = resp?.tx;
|
58
58
|
this.lookupTableAddresses = resp?.lookupTableAddresses ?? [];
|
59
59
|
this.orderPrio = resp?.orderPrio ?? 0;
|
@@ -108,9 +108,9 @@ class TransactionSet {
|
|
108
108
|
async reset() {
|
109
109
|
await this.txHandler.resetLiveTxUpdates();
|
110
110
|
}
|
111
|
-
async refetchAll(attemptNum) {
|
111
|
+
async refetchAll(attemptNum, prevError) {
|
112
112
|
for (const item of this.items) {
|
113
|
-
await item.refetch(attemptNum);
|
113
|
+
await item.refetch(attemptNum, prevError);
|
114
114
|
}
|
115
115
|
}
|
116
116
|
async getSingleTransaction() {
|
@@ -366,7 +366,7 @@ class TransactionsManager {
|
|
366
366
|
}
|
367
367
|
num = attemptNum;
|
368
368
|
if (attemptNum > 0) {
|
369
|
-
const refreshedSets = await this.refreshItemSets(itemSets, attemptNum);
|
369
|
+
const refreshedSets = await this.refreshItemSets(itemSets, attemptNum, prevError);
|
370
370
|
if (!refreshedSets || !refreshedSets.length) {
|
371
371
|
return;
|
372
372
|
}
|
@@ -427,7 +427,7 @@ class TransactionsManager {
|
|
427
427
|
throw prevError;
|
428
428
|
}
|
429
429
|
if (currentIndex > 0 || attemptNum > 0) {
|
430
|
-
const refreshedSets = await this.refreshItemSets(itemSets, attemptNum, currentIndex);
|
430
|
+
const refreshedSets = await this.refreshItemSets(itemSets, attemptNum, prevError, currentIndex);
|
431
431
|
itemSet = refreshedSets ? refreshedSets[0] : undefined;
|
432
432
|
}
|
433
433
|
if (!itemSet)
|
@@ -442,15 +442,15 @@ class TransactionsManager {
|
|
442
442
|
}
|
443
443
|
}, this.totalRetries, this.retryDelay, this.errorsToThrow);
|
444
444
|
}
|
445
|
-
async refreshItemSets(itemSets, attemptNum, currentIndex) {
|
445
|
+
async refreshItemSets(itemSets, attemptNum, prevError, currentIndex) {
|
446
446
|
if (currentIndex !== undefined) {
|
447
447
|
const itemSet = itemSets[currentIndex];
|
448
448
|
await itemSet.reset();
|
449
|
-
await itemSet.refetchAll(attemptNum);
|
449
|
+
await itemSet.refetchAll(attemptNum, prevError);
|
450
450
|
}
|
451
451
|
else {
|
452
452
|
await Promise.all(itemSets.map((itemSet) => itemSet.reset()));
|
453
|
-
await Promise.all(itemSets.map((itemSet) => itemSet.refetchAll(attemptNum)));
|
453
|
+
await Promise.all(itemSets.map((itemSet) => itemSet.refetchAll(attemptNum, prevError)));
|
454
454
|
}
|
455
455
|
const newItemSets = await this.assembleTransactionSets(currentIndex !== undefined
|
456
456
|
? [
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"instructionUtils.d.ts","sourceRoot":"","sources":["../../src/utils/instructionUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAsB,MAAM,0BAA0B,CAAC;AAChF,OAAO,EACL,kBAAkB,EAClB,gCAAgC,EACjC,MAAM,cAAc,CAAC;AACtB,OAAO,EAGL,aAAa,EAEb,eAAe,
|
1
|
+
{"version":3,"file":"instructionUtils.d.ts","sourceRoot":"","sources":["../../src/utils/instructionUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAsB,MAAM,0BAA0B,CAAC;AAChF,OAAO,EACL,kBAAkB,EAClB,gCAAgC,EACjC,MAAM,cAAc,CAAC;AACtB,OAAO,EAGL,aAAa,EAEb,eAAe,EAEhB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAG5C,wBAAgB,mBAAmB,CACjC,MAAM,EAAE,aAAa,EACrB,aAAa,EAAE,gCAAgC,EAC/C,GAAG,CAAC,EAAE,kBAAkB,mBAQzB;AAED,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,aAAa,mBAOzD;AAED,wBAAgB,qBAAqB,CACnC,MAAM,EAAE,aAAa,EACrB,QAAQ,EAAE,gBAAgB,CAAC,gCAAgC,CAAC,EAC5D,GAAG,EAAE,gBAAgB,CAAC,kBAAkB,CAAC,mBAY1C;AAED,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,aAAa,mBAOrD;AAED,wBAAgB,OAAO,CAAC,MAAM,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,mBAUpE;AAED,wBAAgB,MAAM,CAAC,MAAM,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,mBAUnE;AAED,wBAAgB,QAAQ,CAAC,MAAM,EAAE,aAAa,EAAE,MAAM,EAAE,KAAK,GAAG,MAAM,mBAcrE;AAED,wBAAgB,KAAK,CAAC,MAAM,EAAE,aAAa,EAAE,MAAM,EAAE,KAAK,GAAG,MAAM,mBAclE;AAED,wBAAgB,SAAS,CACvB,MAAM,EAAE,aAAa,EACrB,2BAA2B,CAAC,EAAE,MAAM,mBAWrC;AAED,wBAAgB,eAAe,CAC7B,MAAM,EAAE,aAAa,EACrB,WAAW,EAAE,SAAS,EACtB,qBAAqB,EAAE,MAAM,qBAyC9B"}
|
@@ -78,7 +78,7 @@ function repay(client, amount) {
|
|
78
78
|
}), "repay");
|
79
79
|
}
|
80
80
|
function rebalance(client, targetLiqUtilizationRateBps) {
|
81
|
-
return new services_1.TransactionItem(async (attemptNum) => await new services_1.RebalanceTxBuilder(client, targetLiqUtilizationRateBps).buildRebalanceTx(attemptNum), "rebalance");
|
81
|
+
return new services_1.TransactionItem(async (attemptNum, prevError) => await new services_1.RebalanceTxBuilder(client, targetLiqUtilizationRateBps, attemptNum > 2 && prevError instanceof services_1.TransactionTooLargeError).buildRebalanceTx(attemptNum), "rebalance");
|
82
82
|
}
|
83
83
|
function swapThenDeposit(client, depositMint, depositAmountBaseUnit) {
|
84
84
|
return [
|
package/local/txSandbox.ts
CHANGED
@@ -1,9 +1,8 @@
|
|
1
1
|
import { Keypair, PublicKey } from "@solana/web3.js";
|
2
|
-
import { createSignerFromKeypair
|
2
|
+
import { createSignerFromKeypair } from "@metaplex-foundation/umi";
|
3
3
|
import { fromWeb3JsKeypair } from "@metaplex-foundation/umi-web3js-adapters";
|
4
4
|
import {
|
5
5
|
consoleLog,
|
6
|
-
fromBaseUnit,
|
7
6
|
getBatches,
|
8
7
|
getClient,
|
9
8
|
getPositionExBulk,
|
@@ -11,7 +10,6 @@ import {
|
|
11
10
|
getSolautoManagedPositions,
|
12
11
|
LendingPlatform,
|
13
12
|
LOCAL_IRONFORGE_API_URL,
|
14
|
-
openSolautoPosition,
|
15
13
|
PriceType,
|
16
14
|
PriorityFeeSetting,
|
17
15
|
ProgramEnv,
|
@@ -19,25 +17,13 @@ import {
|
|
19
17
|
SOLAUTO_PROD_PROGRAM,
|
20
18
|
SOLAUTO_TEST_PROGRAM,
|
21
19
|
SolautoClient,
|
22
|
-
tokenInfo,
|
23
20
|
TransactionItem,
|
24
21
|
TransactionsManager,
|
25
|
-
USDC,
|
26
|
-
deposit,
|
27
|
-
toBaseUnit,
|
28
|
-
RETARDIO,
|
29
|
-
withdraw,
|
30
|
-
closeSolautoPosition,
|
31
|
-
fetchBank,
|
32
|
-
getBankLiquidityAvailableBaseUnit,
|
33
|
-
fetchTokenPrices,
|
34
|
-
fetchMarginfiAccount,
|
35
|
-
marginfiAccountEmpty,
|
36
22
|
} from "../src";
|
37
23
|
import { getSecretKey } from "./shared";
|
38
24
|
|
39
25
|
const payForTransaction = true;
|
40
|
-
const testProgram =
|
26
|
+
const testProgram = false;
|
41
27
|
const lpEnv: ProgramEnv = "Prod";
|
42
28
|
|
43
29
|
let [, umi] = getSolanaRpcConnection(
|
@@ -48,7 +34,7 @@ let [, umi] = getSolanaRpcConnection(
|
|
48
34
|
|
49
35
|
const signer = createSignerFromKeypair(
|
50
36
|
umi,
|
51
|
-
fromWeb3JsKeypair(Keypair.fromSecretKey(getSecretKey()))
|
37
|
+
fromWeb3JsKeypair(Keypair.fromSecretKey(getSecretKey("solauto-manager")))
|
52
38
|
);
|
53
39
|
|
54
40
|
export async function main() {
|
@@ -60,53 +46,27 @@ export async function main() {
|
|
60
46
|
lpEnv,
|
61
47
|
});
|
62
48
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
// // "GEokw9jqbh6d1xUNA3qaeYFFetbSR5Y1nt7C3chwwgSz"
|
71
|
-
// // ),
|
72
|
-
// });
|
73
|
-
|
74
|
-
const bank = await fetchBank(client.umi, publicKey("3J5rKmCi7JXG6qmiobFJyAidVTnnNAMGj4jomfBxKGRM"));
|
75
|
-
|
76
|
-
const mint = new PublicKey(RETARDIO);
|
77
|
-
const [price] = await fetchTokenPrices([mint]);
|
78
|
-
const debtAvailableBaseUnit = getBankLiquidityAvailableBaseUnit(bank, false);
|
79
|
-
const debtAvailable = fromBaseUnit(debtAvailableBaseUnit, tokenInfo(mint).decimals);
|
80
|
-
const debtAvailableUsd = debtAvailable * price;
|
81
|
-
|
82
|
-
console.log(debtAvailable, debtAvailableUsd);
|
83
|
-
|
84
|
-
const account = await fetchMarginfiAccount(umi, publicKey("CKDgZfh7b7FdYyL7qWxYTKmyKYgr2PzsQcm2b5ptDxYW"));
|
85
|
-
|
86
|
-
console.log(marginfiAccountEmpty(account));
|
49
|
+
await client.initialize({
|
50
|
+
positionId: 3,
|
51
|
+
authority: new PublicKey("61rtn5tzVkesapo6Cz83SPoShUfAePSxJsqniuF2wRKC"),
|
52
|
+
// lpUserAccount: new PublicKey(
|
53
|
+
// "GEokw9jqbh6d1xUNA3qaeYFFetbSR5Y1nt7C3chwwgSz"
|
54
|
+
// ),
|
55
|
+
});
|
87
56
|
|
88
|
-
|
89
|
-
// openSolautoPosition(client, {
|
90
|
-
// boostGap: 250,
|
91
|
-
// boostToBps: 8681,
|
92
|
-
// repayGap: 250,
|
93
|
-
// repayToBps: 8681,
|
94
|
-
// }),
|
95
|
-
// deposit(client, toBaseUnit(10, tokenInfo(new PublicKey(USDC)).decimals)),
|
96
|
-
// rebalance(client, 8500),
|
97
|
-
// ];
|
57
|
+
const transactionItems = [rebalance(client)];
|
98
58
|
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
59
|
+
const txManager = new TransactionsManager(
|
60
|
+
client,
|
61
|
+
undefined,
|
62
|
+
payForTransaction ? "normal" : "only-simulate",
|
63
|
+
PriorityFeeSetting.High,
|
64
|
+
true,
|
65
|
+
undefined,
|
66
|
+
{ totalRetries: 5 }
|
67
|
+
);
|
68
|
+
const statuses = await txManager.clientSend(transactionItems);
|
69
|
+
consoleLog(statuses);
|
110
70
|
}
|
111
71
|
|
112
72
|
async function refreshAll() {
|
package/package.json
CHANGED
@@ -15,12 +15,10 @@ import {
|
|
15
15
|
getTokenAccount,
|
16
16
|
hasFirstRebalance,
|
17
17
|
hasLastRebalance,
|
18
|
-
realtimeUsdToEmaUsd,
|
19
18
|
safeGetPrice,
|
20
19
|
tokenInfo,
|
21
20
|
} from "../../utils";
|
22
21
|
import {
|
23
|
-
LendingPlatform,
|
24
22
|
PositionTokenState,
|
25
23
|
PriceType,
|
26
24
|
RebalanceDirection,
|
@@ -44,11 +42,11 @@ export class RebalanceTxBuilder {
|
|
44
42
|
private swapManager!: RebalanceSwapManager;
|
45
43
|
private flRequirements?: FlashLoanRequirements;
|
46
44
|
private priceType: PriceType = PriceType.Realtime;
|
47
|
-
private flFeeBps?: number;
|
48
45
|
|
49
46
|
constructor(
|
50
47
|
private client: SolautoClient,
|
51
|
-
private targetLiqUtilizationRateBps?: number
|
48
|
+
private targetLiqUtilizationRateBps?: number,
|
49
|
+
private optimizeSize?: boolean
|
52
50
|
) {}
|
53
51
|
|
54
52
|
private shouldProceedWithRebalance() {
|
@@ -154,15 +152,15 @@ export class RebalanceTxBuilder {
|
|
154
152
|
return intermediaryLiqUtilizationRateBps;
|
155
153
|
}
|
156
154
|
|
157
|
-
private async flashLoanRequirements(
|
158
|
-
|
159
|
-
|
155
|
+
private async flashLoanRequirements(): Promise<
|
156
|
+
FlashLoanRequirements | undefined
|
157
|
+
> {
|
160
158
|
const intermediaryLiqUtilizationRateBps =
|
161
159
|
this.intermediaryLiqUtilizationRateBps();
|
162
160
|
const maxLtvRateBps = getMaxLiqUtilizationRateBps(
|
163
161
|
this.client.pos.state.maxLtvBps,
|
164
162
|
this.client.pos.state.liqThresholdBps,
|
165
|
-
0.
|
163
|
+
0.015
|
166
164
|
);
|
167
165
|
if (intermediaryLiqUtilizationRateBps < maxLtvRateBps) {
|
168
166
|
return undefined;
|
@@ -173,7 +171,7 @@ export class RebalanceTxBuilder {
|
|
173
171
|
this.client.flProvider.liquidityAvailable(TokenType.Debt)
|
174
172
|
);
|
175
173
|
|
176
|
-
if (
|
174
|
+
if (stdFlLiquiditySource === undefined || this.optimizeSize) {
|
177
175
|
consoleLog("Checking signer liquidity");
|
178
176
|
const { supplyBalance, debtBalance } = await this.client.signerBalances();
|
179
177
|
const signerFlLiquiditySource = this.getFlLiquiditySource(
|
@@ -280,7 +278,7 @@ export class RebalanceTxBuilder {
|
|
280
278
|
}
|
281
279
|
this.values = rebalanceValues;
|
282
280
|
|
283
|
-
this.flRequirements = await this.flashLoanRequirements(
|
281
|
+
this.flRequirements = await this.flashLoanRequirements();
|
284
282
|
if (this.flRequirements?.flFeeBps) {
|
285
283
|
this.values = this.getRebalanceValues()!;
|
286
284
|
}
|
@@ -88,7 +88,8 @@ export class TransactionItem {
|
|
88
88
|
|
89
89
|
constructor(
|
90
90
|
public fetchTx: (
|
91
|
-
attemptNum: number
|
91
|
+
attemptNum: number,
|
92
|
+
prevError?: Error
|
92
93
|
) => Promise<TransactionItemInputs | undefined>,
|
93
94
|
public name?: string
|
94
95
|
) {}
|
@@ -98,8 +99,8 @@ export class TransactionItem {
|
|
98
99
|
this.initialized = true;
|
99
100
|
}
|
100
101
|
|
101
|
-
async refetch(attemptNum: number) {
|
102
|
-
const resp = await this.fetchTx(attemptNum);
|
102
|
+
async refetch(attemptNum: number, prevError?: Error) {
|
103
|
+
const resp = await this.fetchTx(attemptNum, prevError);
|
103
104
|
this.tx = resp?.tx;
|
104
105
|
this.lookupTableAddresses = resp?.lookupTableAddresses ?? [];
|
105
106
|
this.orderPrio = resp?.orderPrio ?? 0;
|
@@ -182,9 +183,9 @@ class TransactionSet {
|
|
182
183
|
await this.txHandler.resetLiveTxUpdates();
|
183
184
|
}
|
184
185
|
|
185
|
-
async refetchAll(attemptNum: number) {
|
186
|
+
async refetchAll(attemptNum: number, prevError?: Error) {
|
186
187
|
for (const item of this.items) {
|
187
|
-
await item.refetch(attemptNum);
|
188
|
+
await item.refetch(attemptNum, prevError);
|
188
189
|
}
|
189
190
|
}
|
190
191
|
|
@@ -593,7 +594,8 @@ export class TransactionsManager {
|
|
593
594
|
if (attemptNum > 0) {
|
594
595
|
const refreshedSets = await this.refreshItemSets(
|
595
596
|
itemSets,
|
596
|
-
attemptNum
|
597
|
+
attemptNum,
|
598
|
+
prevError
|
597
599
|
);
|
598
600
|
if (!refreshedSets || !refreshedSets.length) {
|
599
601
|
return;
|
@@ -734,6 +736,7 @@ export class TransactionsManager {
|
|
734
736
|
const refreshedSets = await this.refreshItemSets(
|
735
737
|
itemSets,
|
736
738
|
attemptNum,
|
739
|
+
prevError,
|
737
740
|
currentIndex
|
738
741
|
);
|
739
742
|
itemSet = refreshedSets ? refreshedSets[0] : undefined;
|
@@ -766,16 +769,17 @@ export class TransactionsManager {
|
|
766
769
|
private async refreshItemSets(
|
767
770
|
itemSets: TransactionSet[],
|
768
771
|
attemptNum: number,
|
772
|
+
prevError?: Error,
|
769
773
|
currentIndex?: number
|
770
774
|
): Promise<TransactionSet[] | undefined> {
|
771
775
|
if (currentIndex !== undefined) {
|
772
776
|
const itemSet = itemSets[currentIndex];
|
773
777
|
await itemSet.reset();
|
774
|
-
await itemSet.refetchAll(attemptNum);
|
778
|
+
await itemSet.refetchAll(attemptNum, prevError);
|
775
779
|
} else {
|
776
780
|
await Promise.all(itemSets.map((itemSet) => itemSet.reset()));
|
777
781
|
await Promise.all(
|
778
|
-
itemSets.map((itemSet) => itemSet.refetchAll(attemptNum))
|
782
|
+
itemSets.map((itemSet) => itemSet.refetchAll(attemptNum, prevError))
|
779
783
|
);
|
780
784
|
}
|
781
785
|
|
@@ -9,9 +9,9 @@ import {
|
|
9
9
|
SolautoClient,
|
10
10
|
SwapInput,
|
11
11
|
TransactionItem,
|
12
|
+
TransactionTooLargeError,
|
12
13
|
} from "../services";
|
13
14
|
import { PublicKey } from "@solana/web3.js";
|
14
|
-
import { tokenInfo } from "./generalUtils";
|
15
15
|
import { toWeb3JsPublicKey } from "@metaplex-foundation/umi-web3js-adapters";
|
16
16
|
|
17
17
|
export function openSolautoPosition(
|
@@ -123,10 +123,11 @@ export function rebalance(
|
|
123
123
|
targetLiqUtilizationRateBps?: number
|
124
124
|
) {
|
125
125
|
return new TransactionItem(
|
126
|
-
async (attemptNum) =>
|
126
|
+
async (attemptNum, prevError) =>
|
127
127
|
await new RebalanceTxBuilder(
|
128
128
|
client,
|
129
|
-
targetLiqUtilizationRateBps
|
129
|
+
targetLiqUtilizationRateBps,
|
130
|
+
attemptNum > 2 && prevError instanceof TransactionTooLargeError
|
130
131
|
).buildRebalanceTx(attemptNum),
|
131
132
|
"rebalance"
|
132
133
|
);
|