@haven-fi/solauto-sdk 1.0.133 → 1.0.135
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/clients/solautoClient.d.ts.map +1 -1
- package/dist/clients/solautoClient.js +49 -39
- package/dist/generated/accounts/solautoPosition.d.ts +1 -7
- package/dist/generated/accounts/solautoPosition.d.ts.map +1 -1
- package/dist/generated/accounts/solautoPosition.js +2 -6
- package/dist/generated/instructions/cancelDCA.d.ts +3 -3
- package/dist/generated/instructions/cancelDCA.d.ts.map +1 -1
- package/dist/generated/instructions/cancelDCA.js +6 -6
- package/dist/generated/instructions/closePosition.d.ts +1 -2
- package/dist/generated/instructions/closePosition.d.ts.map +1 -1
- package/dist/generated/instructions/closePosition.js +6 -11
- package/dist/generated/instructions/updatePosition.d.ts +3 -3
- package/dist/generated/instructions/updatePosition.d.ts.map +1 -1
- package/dist/generated/instructions/updatePosition.js +6 -6
- package/dist/generated/types/dCASettings.d.ts +7 -5
- package/dist/generated/types/dCASettings.d.ts.map +1 -1
- package/dist/generated/types/dCASettings.js +3 -2
- package/dist/generated/types/dCASettingsInp.d.ts +5 -3
- package/dist/generated/types/dCASettingsInp.d.ts.map +1 -1
- package/dist/generated/types/dCASettingsInp.js +2 -1
- package/dist/generated/types/index.d.ts +1 -1
- package/dist/generated/types/index.d.ts.map +1 -1
- package/dist/generated/types/index.js +1 -1
- package/dist/generated/types/{feeType.d.ts → tokenType.d.ts} +6 -6
- package/dist/generated/types/tokenType.d.ts.map +1 -0
- package/dist/generated/types/tokenType.js +22 -0
- package/dist/transactions/transactionUtils.d.ts.map +1 -1
- package/dist/transactions/transactionUtils.js +15 -15
- package/dist/utils/marginfiUtils.d.ts.map +1 -1
- package/dist/utils/solauto/generalUtils.d.ts +15 -5
- package/dist/utils/solauto/generalUtils.d.ts.map +1 -1
- package/dist/utils/solauto/generalUtils.js +54 -37
- package/dist/utils/solauto/rebalanceUtils.d.ts +2 -1
- package/dist/utils/solauto/rebalanceUtils.d.ts.map +1 -1
- package/dist/utils/solauto/rebalanceUtils.js +7 -5
- package/package.json +1 -1
- package/src/clients/solautoClient.ts +50 -45
- package/src/generated/accounts/solautoPosition.ts +2 -15
- package/src/generated/instructions/cancelDCA.ts +9 -9
- package/src/generated/instructions/closePosition.ts +7 -13
- package/src/generated/instructions/updatePosition.ts +9 -9
- package/src/generated/types/dCASettings.ts +14 -7
- package/src/generated/types/dCASettingsInp.ts +9 -3
- package/src/generated/types/index.ts +1 -1
- package/src/generated/types/{feeType.ts → tokenType.ts} +8 -9
- package/src/transactions/transactionUtils.ts +35 -25
- package/src/utils/marginfiUtils.ts +2 -2
- package/src/utils/solauto/generalUtils.ts +75 -59
- package/src/utils/solauto/rebalanceUtils.ts +9 -8
- package/tests/unit/rebalanceCalculations.ts +14 -14
- package/dist/generated/types/feeType.d.ts.map +0 -1
- package/dist/generated/types/feeType.js +0 -20
|
@@ -8,26 +8,32 @@
|
|
|
8
8
|
|
|
9
9
|
import {
|
|
10
10
|
Serializer,
|
|
11
|
-
|
|
11
|
+
array,
|
|
12
12
|
struct,
|
|
13
13
|
u64,
|
|
14
|
+
u8,
|
|
14
15
|
} from '@metaplex-foundation/umi/serializers';
|
|
15
16
|
import {
|
|
16
17
|
AutomationSettings,
|
|
17
18
|
AutomationSettingsArgs,
|
|
19
|
+
TokenType,
|
|
20
|
+
TokenTypeArgs,
|
|
18
21
|
getAutomationSettingsSerializer,
|
|
22
|
+
getTokenTypeSerializer,
|
|
19
23
|
} from '.';
|
|
20
24
|
|
|
21
25
|
export type DCASettings = {
|
|
22
26
|
automation: AutomationSettings;
|
|
23
|
-
|
|
24
|
-
|
|
27
|
+
dcaInBaseUnit: bigint;
|
|
28
|
+
tokenType: TokenType;
|
|
29
|
+
padding: Array<number>;
|
|
25
30
|
};
|
|
26
31
|
|
|
27
32
|
export type DCASettingsArgs = {
|
|
28
33
|
automation: AutomationSettingsArgs;
|
|
29
|
-
|
|
30
|
-
|
|
34
|
+
dcaInBaseUnit: number | bigint;
|
|
35
|
+
tokenType: TokenTypeArgs;
|
|
36
|
+
padding: Array<number>;
|
|
31
37
|
};
|
|
32
38
|
|
|
33
39
|
export function getDCASettingsSerializer(): Serializer<
|
|
@@ -37,8 +43,9 @@ export function getDCASettingsSerializer(): Serializer<
|
|
|
37
43
|
return struct<DCASettings>(
|
|
38
44
|
[
|
|
39
45
|
['automation', getAutomationSettingsSerializer()],
|
|
40
|
-
['
|
|
41
|
-
['
|
|
46
|
+
['dcaInBaseUnit', u64()],
|
|
47
|
+
['tokenType', getTokenTypeSerializer()],
|
|
48
|
+
['padding', array(u8(), { size: 31 })],
|
|
42
49
|
],
|
|
43
50
|
{ description: 'DCASettings' }
|
|
44
51
|
) as Serializer<DCASettingsArgs, DCASettings>;
|
|
@@ -10,17 +10,22 @@ import { Serializer, struct, u64 } from '@metaplex-foundation/umi/serializers';
|
|
|
10
10
|
import {
|
|
11
11
|
AutomationSettingsInp,
|
|
12
12
|
AutomationSettingsInpArgs,
|
|
13
|
+
TokenType,
|
|
14
|
+
TokenTypeArgs,
|
|
13
15
|
getAutomationSettingsInpSerializer,
|
|
16
|
+
getTokenTypeSerializer,
|
|
14
17
|
} from '.';
|
|
15
18
|
|
|
16
19
|
export type DCASettingsInp = {
|
|
17
20
|
automation: AutomationSettingsInp;
|
|
18
|
-
|
|
21
|
+
dcaInBaseUnit: bigint;
|
|
22
|
+
tokenType: TokenType;
|
|
19
23
|
};
|
|
20
24
|
|
|
21
25
|
export type DCASettingsInpArgs = {
|
|
22
26
|
automation: AutomationSettingsInpArgs;
|
|
23
|
-
|
|
27
|
+
dcaInBaseUnit: number | bigint;
|
|
28
|
+
tokenType: TokenTypeArgs;
|
|
24
29
|
};
|
|
25
30
|
|
|
26
31
|
export function getDCASettingsInpSerializer(): Serializer<
|
|
@@ -30,7 +35,8 @@ export function getDCASettingsInpSerializer(): Serializer<
|
|
|
30
35
|
return struct<DCASettingsInp>(
|
|
31
36
|
[
|
|
32
37
|
['automation', getAutomationSettingsInpSerializer()],
|
|
33
|
-
['
|
|
38
|
+
['dcaInBaseUnit', u64()],
|
|
39
|
+
['tokenType', getTokenTypeSerializer()],
|
|
34
40
|
],
|
|
35
41
|
{ description: 'DCASettingsInp' }
|
|
36
42
|
) as Serializer<DCASettingsInpArgs, DCASettingsInp>;
|
|
@@ -10,7 +10,6 @@ export * from './automationSettings';
|
|
|
10
10
|
export * from './automationSettingsInp';
|
|
11
11
|
export * from './dCASettings';
|
|
12
12
|
export * from './dCASettingsInp';
|
|
13
|
-
export * from './feeType';
|
|
14
13
|
export * from './lendingPlatform';
|
|
15
14
|
export * from './podBool';
|
|
16
15
|
export * from './positionData';
|
|
@@ -23,4 +22,5 @@ export * from './solautoSettingsParameters';
|
|
|
23
22
|
export * from './solautoSettingsParametersInp';
|
|
24
23
|
export * from './tokenAmount';
|
|
25
24
|
export * from './tokenBalanceAmount';
|
|
25
|
+
export * from './tokenType';
|
|
26
26
|
export * from './updatePositionData';
|
|
@@ -8,16 +8,15 @@
|
|
|
8
8
|
|
|
9
9
|
import { Serializer, scalarEnum } from '@metaplex-foundation/umi/serializers';
|
|
10
10
|
|
|
11
|
-
export enum
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
export enum TokenType {
|
|
12
|
+
Supply,
|
|
13
|
+
Debt,
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
export type
|
|
16
|
+
export type TokenTypeArgs = TokenType;
|
|
17
17
|
|
|
18
|
-
export function
|
|
19
|
-
return scalarEnum<
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
>;
|
|
18
|
+
export function getTokenTypeSerializer(): Serializer<TokenTypeArgs, TokenType> {
|
|
19
|
+
return scalarEnum<TokenType>(TokenType, {
|
|
20
|
+
description: 'TokenType',
|
|
21
|
+
}) as Serializer<TokenTypeArgs, TokenType>;
|
|
23
22
|
}
|
|
@@ -17,6 +17,7 @@ import {
|
|
|
17
17
|
SOLAUTO_PROGRAM_ID,
|
|
18
18
|
SolautoAction,
|
|
19
19
|
SolautoRebalanceType,
|
|
20
|
+
TokenType,
|
|
20
21
|
convertReferralFees,
|
|
21
22
|
getMarginfiProtocolInteractionInstructionDataSerializer,
|
|
22
23
|
getMarginfiRebalanceInstructionDataSerializer,
|
|
@@ -55,18 +56,20 @@ import {
|
|
|
55
56
|
getLendingAccountWithdrawInstructionDataSerializer,
|
|
56
57
|
MARGINFI_PROGRAM_ID,
|
|
57
58
|
} from "../marginfi-sdk";
|
|
58
|
-
import { PRICES } from "../constants";
|
|
59
59
|
|
|
60
60
|
interface wSolTokenUsage {
|
|
61
61
|
wSolTokenAccount: PublicKey;
|
|
62
|
-
solautoAction
|
|
62
|
+
solautoAction?: SolautoAction;
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
function getWSolUsage(
|
|
66
66
|
client: SolautoClient,
|
|
67
67
|
solautoActions?: SolautoAction[],
|
|
68
|
-
initiatingDcaIn?:
|
|
69
|
-
|
|
68
|
+
initiatingDcaIn?: {
|
|
69
|
+
amount: bigint;
|
|
70
|
+
tokenType: TokenType;
|
|
71
|
+
},
|
|
72
|
+
cancellingDcaIn?: TokenType
|
|
70
73
|
): wSolTokenUsage | undefined {
|
|
71
74
|
const supplyIsWsol = client.supplyMint.equals(NATIVE_MINT);
|
|
72
75
|
const debtIsWsol = client.debtMint.equals(NATIVE_MINT);
|
|
@@ -79,18 +82,23 @@ function getWSolUsage(
|
|
|
79
82
|
isSolautoAction("Deposit", args) || isSolautoAction("Withdraw", args)
|
|
80
83
|
);
|
|
81
84
|
const usingDebtTaAction = solautoActions?.find(
|
|
82
|
-
(args) =>
|
|
83
|
-
isSolautoAction("Borrow", args) ||
|
|
84
|
-
isSolautoAction("Repay", args) ||
|
|
85
|
-
initiatingDcaIn ||
|
|
86
|
-
cancellingDcaIn
|
|
85
|
+
(args) => isSolautoAction("Borrow", args) || isSolautoAction("Repay", args)
|
|
87
86
|
);
|
|
88
|
-
|
|
87
|
+
|
|
88
|
+
const dcaSupply =
|
|
89
|
+
(initiatingDcaIn && initiatingDcaIn.tokenType === TokenType.Supply) ||
|
|
90
|
+
(cancellingDcaIn !== undefined && cancellingDcaIn === TokenType.Supply);
|
|
91
|
+
|
|
92
|
+
const dcaDebt =
|
|
93
|
+
(initiatingDcaIn && initiatingDcaIn.tokenType === TokenType.Debt) ||
|
|
94
|
+
(cancellingDcaIn !== undefined && cancellingDcaIn === TokenType.Debt);
|
|
95
|
+
|
|
96
|
+
if (supplyIsWsol && (usingSupplyTaAction || dcaSupply)) {
|
|
89
97
|
return {
|
|
90
98
|
wSolTokenAccount: client.signerSupplyTa,
|
|
91
99
|
solautoAction: usingSupplyTaAction,
|
|
92
100
|
};
|
|
93
|
-
} else if (debtIsWsol && usingDebtTaAction) {
|
|
101
|
+
} else if (debtIsWsol && (usingDebtTaAction || dcaDebt)) {
|
|
94
102
|
return {
|
|
95
103
|
wSolTokenAccount: client.signerDebtTa,
|
|
96
104
|
solautoAction: usingDebtTaAction,
|
|
@@ -104,7 +112,10 @@ async function transactionChoresBefore(
|
|
|
104
112
|
client: SolautoClient,
|
|
105
113
|
accountsGettingCreated: string[],
|
|
106
114
|
solautoActions?: SolautoAction[],
|
|
107
|
-
initiatingDcaIn?:
|
|
115
|
+
initiatingDcaIn?: {
|
|
116
|
+
amount: bigint;
|
|
117
|
+
tokenType: TokenType;
|
|
118
|
+
}
|
|
108
119
|
): Promise<TransactionBuilder> {
|
|
109
120
|
let chores = transactionBuilder();
|
|
110
121
|
|
|
@@ -162,18 +173,19 @@ async function transactionChoresBefore(
|
|
|
162
173
|
}
|
|
163
174
|
|
|
164
175
|
let amountToTransfer = BigInt(0);
|
|
165
|
-
if (
|
|
176
|
+
if (
|
|
177
|
+
wSolUsage.solautoAction &&
|
|
178
|
+
isSolautoAction("Deposit", wSolUsage.solautoAction)
|
|
179
|
+
) {
|
|
166
180
|
amountToTransfer = BigInt(wSolUsage.solautoAction.fields[0]);
|
|
167
181
|
} else if (
|
|
182
|
+
wSolUsage.solautoAction &&
|
|
168
183
|
isSolautoAction("Repay", wSolUsage.solautoAction) &&
|
|
169
184
|
wSolUsage.solautoAction.fields[0].__kind === "Some"
|
|
170
185
|
) {
|
|
171
186
|
amountToTransfer = BigInt(wSolUsage.solautoAction.fields[0].fields[0]);
|
|
172
|
-
} else if (
|
|
173
|
-
initiatingDcaIn
|
|
174
|
-
client.debtMint.toString() === NATIVE_MINT.toString()
|
|
175
|
-
) {
|
|
176
|
-
amountToTransfer = initiatingDcaIn;
|
|
187
|
+
} else if (initiatingDcaIn) {
|
|
188
|
+
amountToTransfer = initiatingDcaIn.amount;
|
|
177
189
|
}
|
|
178
190
|
|
|
179
191
|
if (amountToTransfer > 0) {
|
|
@@ -349,7 +361,7 @@ export async function rebalanceChoresBefore(
|
|
|
349
361
|
function transactionChoresAfter(
|
|
350
362
|
client: SolautoClient,
|
|
351
363
|
solautoActions?: SolautoAction[],
|
|
352
|
-
cancellingDcaIn?:
|
|
364
|
+
cancellingDcaIn?: TokenType
|
|
353
365
|
): TransactionBuilder {
|
|
354
366
|
let chores = transactionBuilder();
|
|
355
367
|
|
|
@@ -538,9 +550,7 @@ export async function getTransactionChores(
|
|
|
538
550
|
client,
|
|
539
551
|
accountsGettingCreated,
|
|
540
552
|
solautoActions,
|
|
541
|
-
client.livePositionUpdates.
|
|
542
|
-
? client.livePositionUpdates.debtTaBalanceAdjustment
|
|
543
|
-
: undefined
|
|
553
|
+
client.livePositionUpdates.dcaInBalance
|
|
544
554
|
),
|
|
545
555
|
await rebalanceChoresBefore(client, tx, accountsGettingCreated),
|
|
546
556
|
]);
|
|
@@ -549,7 +559,7 @@ export async function getTransactionChores(
|
|
|
549
559
|
transactionChoresAfter(
|
|
550
560
|
client,
|
|
551
561
|
solautoActions,
|
|
552
|
-
client.livePositionUpdates.
|
|
562
|
+
client.livePositionUpdates.cancellingDca
|
|
553
563
|
)
|
|
554
564
|
);
|
|
555
565
|
|
|
@@ -668,8 +678,8 @@ export async function buildSolautoRebalanceTransaction(
|
|
|
668
678
|
"B",
|
|
669
679
|
swapDetails,
|
|
670
680
|
rebalanceType,
|
|
671
|
-
|
|
672
|
-
|
|
681
|
+
jupQuote.slippageBps,
|
|
682
|
+
undefined,
|
|
673
683
|
targetLiqUtilizationRateBps
|
|
674
684
|
),
|
|
675
685
|
]);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { PublicKey } from "@solana/web3.js";
|
|
2
|
-
import { publicKey,
|
|
2
|
+
import { publicKey, Umi } from "@metaplex-foundation/umi";
|
|
3
3
|
import { toWeb3JsPublicKey } from "@metaplex-foundation/umi-web3js-adapters";
|
|
4
4
|
import {
|
|
5
5
|
Bank,
|
|
@@ -16,13 +16,13 @@ import {
|
|
|
16
16
|
toBaseUnit,
|
|
17
17
|
toBps,
|
|
18
18
|
} from "./numberUtils";
|
|
19
|
-
import { PRICES } from "../constants/solautoConstants";
|
|
20
19
|
import { MARGINFI_ACCOUNTS } from "../constants/marginfiAccounts";
|
|
21
20
|
import { MarginfiAssetAccounts } from "../types/accounts";
|
|
22
21
|
import { PositionState, PositionTokenUsage } from "../generated";
|
|
23
22
|
import { USD_DECIMALS } from "../constants/generalAccounts";
|
|
24
23
|
import { LivePositionUpdates } from "./solauto/generalUtils";
|
|
25
24
|
import { currentUnixSecondsSolana } from "./solanaUtils";
|
|
25
|
+
import { USDC_MINT } from "../constants";
|
|
26
26
|
|
|
27
27
|
export function findMarginfiAccounts(bank: PublicKey): MarginfiAssetAccounts {
|
|
28
28
|
for (const key in MARGINFI_ACCOUNTS) {
|
|
@@ -9,9 +9,11 @@ import {
|
|
|
9
9
|
SOLAUTO_PROGRAM_ID,
|
|
10
10
|
SolautoSettingsParameters,
|
|
11
11
|
SolautoSettingsParametersInpArgs,
|
|
12
|
+
TokenType,
|
|
12
13
|
getReferralStateSize,
|
|
13
14
|
getSolautoPositionAccountDataSerializer,
|
|
14
15
|
getSolautoPositionSize,
|
|
16
|
+
safeFetchAllSolautoPosition,
|
|
15
17
|
} from "../../generated";
|
|
16
18
|
import { currentUnixSeconds, fetchTokenPrices } from "../generalUtils";
|
|
17
19
|
import {
|
|
@@ -33,7 +35,8 @@ export function findMintByTicker(ticker: string): PublicKey {
|
|
|
33
35
|
for (const key in TOKEN_INFO) {
|
|
34
36
|
const account = TOKEN_INFO[key];
|
|
35
37
|
if (
|
|
36
|
-
account.ticker.toString().toLowerCase() ===
|
|
38
|
+
account.ticker.toString().toLowerCase() ===
|
|
39
|
+
ticker.toString().toLowerCase()
|
|
37
40
|
) {
|
|
38
41
|
return new PublicKey(key);
|
|
39
42
|
}
|
|
@@ -176,14 +179,18 @@ export async function getSolautoManagedPositions(
|
|
|
176
179
|
// position_id: [u8; 1]
|
|
177
180
|
// self_managed: u8 - (1 for true, 0 for false)
|
|
178
181
|
// padding: [u8; 5]
|
|
179
|
-
// authority:
|
|
182
|
+
// authority: pubkey
|
|
180
183
|
// lending_platform: u8
|
|
184
|
+
// padding: [u8; 7]
|
|
185
|
+
// protocol account: pubkey
|
|
186
|
+
// supply mint: pubkey
|
|
187
|
+
// debt mint: pubkey
|
|
181
188
|
|
|
182
189
|
const accounts = await umi.rpc.getProgramAccounts(SOLAUTO_PROGRAM_ID, {
|
|
183
190
|
commitment: "confirmed",
|
|
184
191
|
dataSlice: {
|
|
185
192
|
offset: 0,
|
|
186
|
-
length: 1 + 1 + 1 + 5 + 32 + 1, // bump + position_id + self_managed + padding + authority (pubkey) + lending_platform
|
|
193
|
+
length: 1 + 1 + 1 + 5 + 32 + 1 + 7 + 32 + 32 + 32, // bump + position_id + self_managed + padding (5) + authority (pubkey) + lending_platform + padding (7) + protocol account (pubkey) + supply mint (pubkey) + debt mint (pubkey)
|
|
187
194
|
},
|
|
188
195
|
filters: [
|
|
189
196
|
{
|
|
@@ -220,6 +227,9 @@ export async function getSolautoManagedPositions(
|
|
|
220
227
|
authority: toWeb3JsPublicKey(position.authority),
|
|
221
228
|
positionId: position.positionId[0],
|
|
222
229
|
lendingPlatform: position.position.lendingPlatform,
|
|
230
|
+
protocolAccount: toWeb3JsPublicKey(position.position.protocolAccount),
|
|
231
|
+
supplyMint: toWeb3JsPublicKey(position.position.supplyMint),
|
|
232
|
+
debtMint: toWeb3JsPublicKey(position.position.debtMint),
|
|
223
233
|
};
|
|
224
234
|
});
|
|
225
235
|
}
|
|
@@ -277,44 +287,44 @@ export async function getAllPositionsByAuthority(
|
|
|
277
287
|
umi: Umi,
|
|
278
288
|
user: PublicKey
|
|
279
289
|
): Promise<SolautoPositionDetails[]> {
|
|
280
|
-
const
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
290
|
+
const solautoCompatiblePositions: SolautoPositionDetails[][] =
|
|
291
|
+
await Promise.all([
|
|
292
|
+
(async () => {
|
|
293
|
+
const solautoManagedPositions = await getSolautoManagedPositions(
|
|
294
|
+
umi,
|
|
295
|
+
user
|
|
296
|
+
);
|
|
297
|
+
return solautoManagedPositions.map((x) => ({
|
|
298
|
+
...x,
|
|
299
|
+
authority: user,
|
|
300
|
+
}));
|
|
301
|
+
})(),
|
|
302
|
+
(async () => {
|
|
303
|
+
let marginfiPositions = await getAllMarginfiAccountsByAuthority(
|
|
304
|
+
umi,
|
|
305
|
+
user,
|
|
306
|
+
true
|
|
307
|
+
);
|
|
308
|
+
marginfiPositions = marginfiPositions.filter(
|
|
309
|
+
(x) =>
|
|
310
|
+
x.supplyMint &&
|
|
311
|
+
(x.debtMint!.equals(PublicKey.default) ||
|
|
312
|
+
ALL_SUPPORTED_TOKENS.includes(x.debtMint!.toString()))
|
|
313
|
+
);
|
|
314
|
+
return marginfiPositions.map((x) => ({
|
|
315
|
+
publicKey: x.marginfiAccount,
|
|
316
|
+
authority: user,
|
|
317
|
+
positionId: 0,
|
|
318
|
+
lendingPlatform: LendingPlatform.Marginfi,
|
|
319
|
+
protocolAccount: x.marginfiAccount,
|
|
320
|
+
supplyMint: x.supplyMint,
|
|
321
|
+
debtMint: x.debtMint,
|
|
322
|
+
}));
|
|
323
|
+
})(),
|
|
324
|
+
// TODO support other platforms
|
|
325
|
+
]);
|
|
316
326
|
|
|
317
|
-
return
|
|
327
|
+
return solautoCompatiblePositions.flat();
|
|
318
328
|
}
|
|
319
329
|
|
|
320
330
|
export async function positionStateWithLatestPrices(
|
|
@@ -441,15 +451,15 @@ export function createFakePositionState(
|
|
|
441
451
|
};
|
|
442
452
|
}
|
|
443
453
|
|
|
444
|
-
export function createSolautoSettings(
|
|
454
|
+
export function createSolautoSettings(
|
|
455
|
+
settings: SolautoSettingsParametersInpArgs
|
|
456
|
+
): SolautoSettingsParameters {
|
|
445
457
|
return {
|
|
446
458
|
automation:
|
|
447
459
|
isOption(settings.automation) && isSome(settings.automation)
|
|
448
460
|
? {
|
|
449
461
|
...settings.automation.value,
|
|
450
|
-
intervalSeconds: BigInt(
|
|
451
|
-
settings.automation.value.intervalSeconds
|
|
452
|
-
),
|
|
462
|
+
intervalSeconds: BigInt(settings.automation.value.intervalSeconds),
|
|
453
463
|
unixStartDate: BigInt(settings.automation.value.unixStartDate),
|
|
454
464
|
padding: new Uint8Array([]),
|
|
455
465
|
padding1: [],
|
|
@@ -463,8 +473,7 @@ export function createSolautoSettings(settings: SolautoSettingsParametersInpArgs
|
|
|
463
473
|
padding1: [],
|
|
464
474
|
},
|
|
465
475
|
targetBoostToBps:
|
|
466
|
-
isOption(settings.targetBoostToBps) &&
|
|
467
|
-
isSome(settings.targetBoostToBps)
|
|
476
|
+
isOption(settings.targetBoostToBps) && isSome(settings.targetBoostToBps)
|
|
468
477
|
? settings.targetBoostToBps.value
|
|
469
478
|
: 0,
|
|
470
479
|
boostGap: settings.boostGap,
|
|
@@ -479,24 +488,24 @@ export function createSolautoSettings(settings: SolautoSettingsParametersInpArgs
|
|
|
479
488
|
type PositionAdjustment =
|
|
480
489
|
| { type: "supply"; value: bigint }
|
|
481
490
|
| { type: "debt"; value: bigint }
|
|
482
|
-
| { type: "debtDcaIn"; value: bigint }
|
|
483
491
|
| { type: "settings"; value: SolautoSettingsParametersInpArgs }
|
|
484
|
-
| { type: "dca"; value: DCASettingsInpArgs }
|
|
492
|
+
| { type: "dca"; value: DCASettingsInpArgs }
|
|
493
|
+
| { type: "dcaInBalance"; value: { amount: bigint; tokenType: TokenType; } }
|
|
494
|
+
| { type: "cancellingDca"; value: TokenType; };
|
|
485
495
|
|
|
486
496
|
export class LivePositionUpdates {
|
|
487
|
-
public supplyAdjustment
|
|
488
|
-
public debtAdjustment
|
|
489
|
-
public debtTaBalanceAdjustment: bigint = BigInt(0);
|
|
497
|
+
public supplyAdjustment = BigInt(0);
|
|
498
|
+
public debtAdjustment = BigInt(0);
|
|
490
499
|
public settings: SolautoSettingsParameters | undefined = undefined;
|
|
491
500
|
public activeDca: DCASettings | undefined = undefined;
|
|
492
|
-
|
|
501
|
+
public dcaInBalance?: { amount: bigint; tokenType: TokenType; } = undefined;
|
|
502
|
+
public cancellingDca: TokenType | undefined = undefined;
|
|
503
|
+
|
|
493
504
|
new(update: PositionAdjustment) {
|
|
494
505
|
if (update.type === "supply") {
|
|
495
506
|
this.supplyAdjustment += update.value;
|
|
496
507
|
} else if (update.type === "debt") {
|
|
497
508
|
this.debtAdjustment += update.value;
|
|
498
|
-
} else if (update.type === "debtDcaIn") {
|
|
499
|
-
this.debtTaBalanceAdjustment += update.value;
|
|
500
509
|
} else if (update.type === "settings") {
|
|
501
510
|
const settings = update.value;
|
|
502
511
|
this.settings = createSolautoSettings(settings);
|
|
@@ -510,26 +519,33 @@ export class LivePositionUpdates {
|
|
|
510
519
|
padding: new Uint8Array([]),
|
|
511
520
|
padding1: [],
|
|
512
521
|
},
|
|
513
|
-
|
|
514
|
-
|
|
522
|
+
dcaInBaseUnit: BigInt(dca.dcaInBaseUnit),
|
|
523
|
+
tokenType: dca.tokenType,
|
|
524
|
+
padding: [],
|
|
515
525
|
};
|
|
526
|
+
} else if (update.type === "cancellingDca") {
|
|
527
|
+
this.cancellingDca = update.value;
|
|
528
|
+
} else if (update.type === "dcaInBalance") {
|
|
529
|
+
this.dcaInBalance = update.value;
|
|
516
530
|
}
|
|
517
531
|
}
|
|
518
532
|
|
|
519
533
|
reset() {
|
|
520
534
|
this.supplyAdjustment = BigInt(0);
|
|
521
535
|
this.debtAdjustment = BigInt(0);
|
|
522
|
-
this.debtTaBalanceAdjustment = BigInt(0);
|
|
523
536
|
this.settings = undefined;
|
|
524
537
|
this.activeDca = undefined;
|
|
538
|
+
this.dcaInBalance = undefined;
|
|
539
|
+
this.cancellingDca = undefined;
|
|
525
540
|
}
|
|
526
541
|
|
|
527
542
|
hasUpdates(): boolean {
|
|
528
543
|
return (
|
|
529
544
|
this.supplyAdjustment !== BigInt(0) ||
|
|
530
545
|
this.debtAdjustment !== BigInt(0) ||
|
|
531
|
-
this.
|
|
532
|
-
this.settings !== undefined
|
|
546
|
+
this.dcaInBalance !== undefined ||
|
|
547
|
+
this.settings !== undefined ||
|
|
548
|
+
this.cancellingDca !== undefined
|
|
533
549
|
);
|
|
534
550
|
}
|
|
535
551
|
}
|
|
@@ -5,6 +5,7 @@ import {
|
|
|
5
5
|
PositionState,
|
|
6
6
|
PositionTokenUsage,
|
|
7
7
|
SolautoSettingsParameters,
|
|
8
|
+
TokenType,
|
|
8
9
|
} from "../../generated";
|
|
9
10
|
import {
|
|
10
11
|
eligibleForNextAutomationPeriod,
|
|
@@ -27,16 +28,14 @@ import {
|
|
|
27
28
|
import { USD_DECIMALS } from "../../constants/generalAccounts";
|
|
28
29
|
import {
|
|
29
30
|
DEFAULT_LIMIT_GAP_BPS,
|
|
30
|
-
MIN_POSITION_STATE_FRESHNESS_SECS,
|
|
31
|
-
PRICES,
|
|
32
31
|
} from "../../constants/solautoConstants";
|
|
33
32
|
|
|
34
33
|
function getAdditionalAmountToDcaIn(dca: DCASettings): number {
|
|
35
|
-
if (dca.
|
|
34
|
+
if (dca.dcaInBaseUnit === BigInt(0)) {
|
|
36
35
|
return 0;
|
|
37
36
|
}
|
|
38
37
|
|
|
39
|
-
const debtBalance = Number(dca.
|
|
38
|
+
const debtBalance = Number(dca.dcaInBaseUnit);
|
|
40
39
|
const updatedDebtBalance = getUpdatedValueFromAutomation(
|
|
41
40
|
debtBalance,
|
|
42
41
|
0,
|
|
@@ -80,7 +79,7 @@ function targetLiqUtilizationRateBpsFromDCA(
|
|
|
80
79
|
);
|
|
81
80
|
|
|
82
81
|
let targetRateBps = 0;
|
|
83
|
-
if (dca.
|
|
82
|
+
if (dca.dcaInBaseUnit > BigInt(0)) {
|
|
84
83
|
targetRateBps = Math.max(
|
|
85
84
|
state.liqUtilizationRateBps,
|
|
86
85
|
adjustedSettings.boostToBps
|
|
@@ -126,7 +125,7 @@ function getTargetRateAndDcaAmount(
|
|
|
126
125
|
dca: DCASettings | undefined,
|
|
127
126
|
currentUnixTime: number,
|
|
128
127
|
targetLiqUtilizationRateBps?: number
|
|
129
|
-
): { targetRateBps: number; amountToDcaIn?: number } {
|
|
128
|
+
): { targetRateBps: number; amountToDcaIn?: number; } {
|
|
130
129
|
if (targetLiqUtilizationRateBps !== undefined) {
|
|
131
130
|
return {
|
|
132
131
|
targetRateBps: targetLiqUtilizationRateBps,
|
|
@@ -164,6 +163,7 @@ export interface RebalanceValues {
|
|
|
164
163
|
debtAdjustmentUsd: number;
|
|
165
164
|
amountToDcaIn: number;
|
|
166
165
|
amountUsdToDcaIn: number;
|
|
166
|
+
dcaTokenType?: TokenType;
|
|
167
167
|
}
|
|
168
168
|
|
|
169
169
|
export function getRebalanceValues(
|
|
@@ -238,6 +238,7 @@ export function getRebalanceValues(
|
|
|
238
238
|
debtAdjustmentUsd,
|
|
239
239
|
amountToDcaIn: amountToDcaIn ?? 0,
|
|
240
240
|
amountUsdToDcaIn,
|
|
241
|
+
dcaTokenType: dca?.tokenType
|
|
241
242
|
};
|
|
242
243
|
}
|
|
243
244
|
|
|
@@ -254,7 +255,7 @@ export function getFlashLoanDetails(
|
|
|
254
255
|
let supplyUsd = fromBaseUnit(
|
|
255
256
|
client.solautoPositionState!.supply.amountUsed.baseAmountUsdValue,
|
|
256
257
|
USD_DECIMALS
|
|
257
|
-
);
|
|
258
|
+
) + (values.dcaTokenType === TokenType.Supply ? values.amountUsdToDcaIn : 0);
|
|
258
259
|
let debtUsd = fromBaseUnit(
|
|
259
260
|
client.solautoPositionState!.debt.amountUsed.baseAmountUsdValue,
|
|
260
261
|
USD_DECIMALS
|
|
@@ -333,7 +334,7 @@ export function getJupSwapRebalanceDetails(
|
|
|
333
334
|
: client.solautoPositionState!.debt;
|
|
334
335
|
|
|
335
336
|
const usdToSwap =
|
|
336
|
-
Math.abs(values.debtAdjustmentUsd) + values.amountUsdToDcaIn;
|
|
337
|
+
Math.abs(values.debtAdjustmentUsd) + (values.dcaTokenType === TokenType.Debt ? values.amountUsdToDcaIn : 0);
|
|
337
338
|
|
|
338
339
|
const inputPrice = values.increasingLeverage
|
|
339
340
|
? safeGetPrice(client.debtMint)
|