@haven-fi/solauto-sdk 1.0.215 → 1.0.217
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/clients/referralStateManager.d.ts +6 -5
- package/dist/clients/referralStateManager.d.ts.map +1 -1
- package/dist/clients/referralStateManager.js +9 -10
- package/dist/clients/solautoClient.d.ts +8 -17
- package/dist/clients/solautoClient.d.ts.map +1 -1
- package/dist/clients/solautoClient.js +15 -42
- package/dist/clients/solautoMarginfiClient.d.ts +2 -2
- package/dist/clients/solautoMarginfiClient.d.ts.map +1 -1
- package/dist/clients/solautoMarginfiClient.js +20 -13
- package/dist/clients/txHandler.d.ts +1 -4
- package/dist/clients/txHandler.d.ts.map +1 -1
- package/dist/generated/instructions/marginfiRebalance.d.ts +1 -1
- package/dist/generated/instructions/marginfiRebalance.d.ts.map +1 -1
- package/dist/generated/types/index.d.ts +1 -0
- package/dist/generated/types/index.d.ts.map +1 -1
- package/dist/generated/types/index.js +1 -0
- package/dist/generated/types/rebalanceData.d.ts +3 -3
- package/dist/generated/types/rebalanceData.d.ts.map +1 -1
- package/dist/generated/types/rebalanceData.js +2 -2
- package/dist/generated/types/rebalanceDirection.d.ts +15 -0
- package/dist/generated/types/rebalanceDirection.d.ts.map +1 -0
- package/dist/generated/types/rebalanceDirection.js +22 -0
- package/dist/transactions/transactionUtils.d.ts.map +1 -1
- package/dist/transactions/transactionUtils.js +14 -13
- package/dist/utils/solauto/rebalanceUtils.d.ts +2 -0
- package/dist/utils/solauto/rebalanceUtils.d.ts.map +1 -1
- package/dist/utils/solauto/rebalanceUtils.js +1 -0
- package/package.json +1 -1
- package/src/clients/referralStateManager.ts +17 -15
- package/src/clients/solautoClient.ts +21 -75
- package/src/clients/solautoMarginfiClient.ts +31 -20
- package/src/clients/txHandler.ts +0 -5
- package/src/generated/instructions/marginfiRebalance.ts +1 -1
- package/src/generated/types/index.ts +1 -0
- package/src/generated/types/rebalanceData.ts +7 -5
- package/src/generated/types/rebalanceDirection.ts +25 -0
- package/src/transactions/transactionUtils.ts +19 -13
- package/src/utils/solauto/rebalanceUtils.ts +4 -0
- package/tests/transactions/solautoMarginfi.ts +4 -4
@@ -51,14 +51,13 @@ function getWSolUsage(client, solautoActions, initiatingDcaIn, cancellingDcaIn)
|
|
51
51
|
}
|
52
52
|
async function transactionChoresBefore(client, accountsGettingCreated, solautoActions, initiatingDcaIn) {
|
53
53
|
let chores = (0, umi_1.transactionBuilder)();
|
54
|
-
if (client.
|
55
|
-
(client.
|
56
|
-
client.
|
54
|
+
if (client.referralStateData === null ||
|
55
|
+
(client.referredBy !== undefined &&
|
56
|
+
client.referralStateData?.referredByState ===
|
57
57
|
(0, umi_1.publicKey)(web3_js_1.PublicKey.default)) ||
|
58
58
|
(client.authorityLutAddress !== undefined &&
|
59
|
-
client.
|
60
|
-
|
61
|
-
chores = chores.add(client.referralStateManager.updateReferralStatesIx(undefined, client.authorityLutAddress));
|
59
|
+
client.referralStateData.lookupTable == (0, umi_1.publicKey)(web3_js_1.PublicKey.default))) {
|
60
|
+
chores = chores.add(client.updateReferralStatesIx(undefined, client.authorityLutAddress));
|
62
61
|
}
|
63
62
|
if (client.selfManaged) {
|
64
63
|
if (client.solautoPositionData === null) {
|
@@ -127,14 +126,16 @@ async function rebalanceChoresBefore(client, tx, accountsGettingCreated) {
|
|
127
126
|
const usesAccount = (key) => tx
|
128
127
|
.getInstructions()
|
129
128
|
.some((t) => t.keys.some((k) => (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(k.pubkey).equals(key)));
|
130
|
-
const checkReferralSupplyTa = client.referredBySupplyTa && usesAccount(client.referredBySupplyTa);
|
129
|
+
const checkReferralSupplyTa = client.referredBySupplyTa() && usesAccount(client.referredBySupplyTa());
|
131
130
|
const checkSolautoFeesTa = usesAccount(client.solautoFeesSupplyTa);
|
132
131
|
const checkIntermediaryMfiAccount = client.lendingPlatform === generated_1.LendingPlatform.Marginfi &&
|
133
132
|
usesAccount(client.intermediaryMarginfiAccountPk);
|
134
133
|
const checkSignerSupplyTa = usesAccount(client.signerSupplyTa);
|
135
134
|
const checkSignerDebtTa = usesAccount(client.signerDebtTa);
|
136
135
|
const accountsNeeded = [
|
137
|
-
...[
|
136
|
+
...[
|
137
|
+
checkReferralSupplyTa ? client.referredBySupplyTa() : web3_js_1.PublicKey.default,
|
138
|
+
],
|
138
139
|
...[checkSolautoFeesTa ? client.solautoFeesSupplyTa : web3_js_1.PublicKey.default],
|
139
140
|
...[
|
140
141
|
checkIntermediaryMfiAccount
|
@@ -148,7 +149,7 @@ async function rebalanceChoresBefore(client, tx, accountsGettingCreated) {
|
|
148
149
|
let chores = (0, umi_1.transactionBuilder)();
|
149
150
|
if (checkReferralSupplyTa && !(0, generalUtils_1.rpcAccountCreated)(referredBySupplyTa)) {
|
150
151
|
client.log("Creating referred-by TA for ", client.supplyMint.toString());
|
151
|
-
chores = chores.add((0, solanaUtils_1.createAssociatedTokenAccountUmiIx)(client.signer, client.
|
152
|
+
chores = chores.add((0, solanaUtils_1.createAssociatedTokenAccountUmiIx)(client.signer, client.referredByState, client.supplyMint));
|
152
153
|
}
|
153
154
|
if (checkSolautoFeesTa && !(0, generalUtils_1.rpcAccountCreated)(solautoFeesSupplyTa)) {
|
154
155
|
client.log("Creating Solauto fees TA for ", client.supplyMint.toString());
|
@@ -380,11 +381,11 @@ async function buildSolautoRebalanceTransaction(client, targetLiqUtilizationRate
|
|
380
381
|
client.flashBorrow(flashLoan, (0, accountUtils_1.getTokenAccount)((0, umi_web3js_adapters_1.toWeb3JsPublicKey)(client.signer.publicKey), swapDetails.inputMint)),
|
381
382
|
...(addFirstRebalance
|
382
383
|
? [
|
383
|
-
client.rebalance("A", jupQuote, rebalanceType, flashLoan, targetLiqUtilizationRateBps),
|
384
|
+
client.rebalance("A", jupQuote, rebalanceType, values, flashLoan, targetLiqUtilizationRateBps),
|
384
385
|
]
|
385
386
|
: []),
|
386
387
|
swapIx,
|
387
|
-
client.rebalance("B", jupQuote, rebalanceType, flashLoan, targetLiqUtilizationRateBps),
|
388
|
+
client.rebalance("B", jupQuote, rebalanceType, values, flashLoan, targetLiqUtilizationRateBps),
|
388
389
|
client.flashRepay(flashLoan),
|
389
390
|
]);
|
390
391
|
}
|
@@ -393,9 +394,9 @@ async function buildSolautoRebalanceTransaction(client, targetLiqUtilizationRate
|
|
393
394
|
tx = tx.add([
|
394
395
|
setupInstructions,
|
395
396
|
tokenLedgerIx,
|
396
|
-
client.rebalance("A", jupQuote, rebalanceType, undefined, targetLiqUtilizationRateBps),
|
397
|
+
client.rebalance("A", jupQuote, rebalanceType, values, undefined, targetLiqUtilizationRateBps),
|
397
398
|
swapIx,
|
398
|
-
client.rebalance("B", jupQuote, rebalanceType, undefined, targetLiqUtilizationRateBps),
|
399
|
+
client.rebalance("B", jupQuote, rebalanceType, values, undefined, targetLiqUtilizationRateBps),
|
399
400
|
]);
|
400
401
|
}
|
401
402
|
return {
|
@@ -3,6 +3,7 @@ import { SolautoClient } from "../../clients/solautoClient";
|
|
3
3
|
import { DCASettings, PositionState, SolautoSettingsParameters, TokenType } from "../../generated";
|
4
4
|
import { QuoteResponse } from "@jup-ag/api";
|
5
5
|
import { JupSwapDetails } from "../jupiterUtils";
|
6
|
+
import { RebalanceAction } from "../../types";
|
6
7
|
export interface RebalanceValues {
|
7
8
|
increasingLeverage: boolean;
|
8
9
|
debtAdjustmentUsd: number;
|
@@ -10,6 +11,7 @@ export interface RebalanceValues {
|
|
10
11
|
amountToDcaIn: number;
|
11
12
|
amountUsdToDcaIn: number;
|
12
13
|
dcaTokenType?: TokenType;
|
14
|
+
rebalanceAction: RebalanceAction;
|
13
15
|
}
|
14
16
|
export declare function getRebalanceValues(state: PositionState, settings: SolautoSettingsParameters | undefined, dca: DCASettings | undefined, currentUnixTime: number, supplyPrice: number, debtPrice: number, targetLiqUtilizationRateBps?: number): RebalanceValues;
|
15
17
|
export interface FlashLoanDetails {
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"rebalanceUtils.d.ts","sourceRoot":"","sources":["../../../src/utils/solauto/rebalanceUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAC5D,OAAO,EACL,WAAW,EACX,aAAa,EAEb,yBAAyB,EACzB,SAAS,EACV,MAAM,iBAAiB,CAAC;AAOzB,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;
|
1
|
+
{"version":3,"file":"rebalanceUtils.d.ts","sourceRoot":"","sources":["../../../src/utils/solauto/rebalanceUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAC5D,OAAO,EACL,WAAW,EACX,aAAa,EAEb,yBAAyB,EACzB,SAAS,EACV,MAAM,iBAAiB,CAAC;AAOzB,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAajD,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAkI9C,MAAM,WAAW,eAAe;IAC9B,kBAAkB,EAAE,OAAO,CAAC;IAC5B,iBAAiB,EAAE,MAAM,CAAC;IAC1B,qBAAqB,EAAE,OAAO,CAAC;IAC/B,aAAa,EAAE,MAAM,CAAC;IACtB,gBAAgB,EAAE,MAAM,CAAC;IACzB,YAAY,CAAC,EAAE,SAAS,CAAC;IACzB,eAAe,EAAE,eAAe,CAAC;CAClC;AAED,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,aAAa,EACpB,QAAQ,EAAE,yBAAyB,GAAG,SAAS,EAC/C,GAAG,EAAE,WAAW,GAAG,SAAS,EAC5B,eAAe,EAAE,MAAM,EACvB,WAAW,EAAE,MAAM,EACnB,SAAS,EAAE,MAAM,EACjB,2BAA2B,CAAC,EAAE,MAAM,GACnC,eAAe,CAmDjB;AAED,MAAM,WAAW,gBAAgB;IAC/B,cAAc,EAAE,MAAM,CAAC;IACvB,IAAI,EAAE,SAAS,CAAC;CACjB;AAED,wBAAgB,mBAAmB,CACjC,MAAM,EAAE,aAAa,EACrB,MAAM,EAAE,eAAe,EACvB,QAAQ,EAAE,aAAa,GACtB,gBAAgB,GAAG,SAAS,CA0D9B;AAED,wBAAgB,0BAA0B,CACxC,MAAM,EAAE,aAAa,EACrB,MAAM,EAAE,eAAe,EACvB,2BAA2B,CAAC,EAAE,MAAM,EACpC,UAAU,CAAC,EAAE,MAAM,GAClB,cAAc,CAyChB"}
|
@@ -100,6 +100,7 @@ function getRebalanceValues(state, settings, dca, currentUnixTime, supplyPrice,
|
|
100
100
|
amountToDcaIn: amountToDcaIn ?? 0,
|
101
101
|
amountUsdToDcaIn,
|
102
102
|
dcaTokenType: dca?.tokenType,
|
103
|
+
rebalanceAction: (amountToDcaIn ?? 0) > 0 ? "dca" : increasingLeverage ? "boost" : "repay",
|
103
104
|
};
|
104
105
|
}
|
105
106
|
function getFlashLoanDetails(client, values, jupQuote) {
|
package/package.json
CHANGED
@@ -23,10 +23,10 @@ import { getReferralState, getTokenAccount } from "../utils";
|
|
23
23
|
import { TxHandler } from "./txHandler";
|
24
24
|
import { SOLAUTO_LUT } from "../constants";
|
25
25
|
|
26
|
-
interface ReferralStateManagerArgs {
|
26
|
+
export interface ReferralStateManagerArgs {
|
27
27
|
signer?: Signer;
|
28
28
|
wallet?: WalletAdapter;
|
29
|
-
|
29
|
+
authority?: PublicKey;
|
30
30
|
referredByAuthority?: PublicKey;
|
31
31
|
}
|
32
32
|
|
@@ -34,15 +34,18 @@ export class ReferralStateManager extends TxHandler {
|
|
34
34
|
public umi!: Umi;
|
35
35
|
public signer!: Signer;
|
36
36
|
|
37
|
-
public
|
37
|
+
public authority!: PublicKey;
|
38
38
|
public referralState!: PublicKey;
|
39
39
|
public referralStateData!: ReferralState | null;
|
40
40
|
|
41
|
+
public referredBy?: PublicKey;
|
42
|
+
public referredByState?: PublicKey;
|
43
|
+
|
41
44
|
constructor(
|
42
|
-
|
45
|
+
rpcUrl: string,
|
43
46
|
public localTest?: boolean
|
44
47
|
) {
|
45
|
-
super(
|
48
|
+
super(rpcUrl, localTest);
|
46
49
|
this.umi = this.umi.use({
|
47
50
|
install(umi) {
|
48
51
|
umi.programs.add(createSolautoProgram(), false);
|
@@ -59,10 +62,11 @@ export class ReferralStateManager extends TxHandler {
|
|
59
62
|
? signerIdentity(args.signer)
|
60
63
|
: walletAdapterIdentity(args.wallet!, true)
|
61
64
|
);
|
62
|
-
|
63
65
|
this.signer = this.umi.identity;
|
66
|
+
this.authority = args.authority ?? toWeb3JsPublicKey(this.signer.publicKey);
|
67
|
+
|
64
68
|
this.referralState = getReferralState(
|
65
|
-
args.
|
69
|
+
args.authority ?? toWeb3JsPublicKey(this.signer.publicKey)
|
66
70
|
);
|
67
71
|
this.referralStateData = await safeFetchReferralState(
|
68
72
|
this.umi,
|
@@ -82,12 +86,10 @@ export class ReferralStateManager extends TxHandler {
|
|
82
86
|
: [SOLAUTO_LUT];
|
83
87
|
}
|
84
88
|
|
85
|
-
|
86
|
-
const authorityReferralStateData = this.referralStateData;
|
89
|
+
setReferredBy(referredBy?: PublicKey) {
|
87
90
|
const hasReferredBy =
|
88
|
-
|
89
|
-
|
90
|
-
publicKey(PublicKey.default);
|
91
|
+
this.referralStateData &&
|
92
|
+
this.referralStateData.referredByState !== publicKey(PublicKey.default);
|
91
93
|
const finalReferredBy =
|
92
94
|
!hasReferredBy &&
|
93
95
|
referredBy &&
|
@@ -98,8 +100,8 @@ export class ReferralStateManager extends TxHandler {
|
|
98
100
|
this.referredBy = finalReferredBy;
|
99
101
|
this.referredByState = finalReferredBy
|
100
102
|
? getReferralState(finalReferredBy)
|
101
|
-
:
|
102
|
-
? toWeb3JsPublicKey(
|
103
|
+
: this.referralStateData
|
104
|
+
? toWeb3JsPublicKey(this.referralStateData.referredByState)
|
103
105
|
: undefined;
|
104
106
|
}
|
105
107
|
|
@@ -143,7 +145,7 @@ export class ReferralStateManager extends TxHandler {
|
|
143
145
|
signerWsolTa: publicKey(
|
144
146
|
getTokenAccount(toWeb3JsPublicKey(this.signer.publicKey), NATIVE_MINT)
|
145
147
|
),
|
146
|
-
referralAuthority: publicKey(this.
|
148
|
+
referralAuthority: publicKey(this.authority),
|
147
149
|
referralState: publicKey(this.referralState),
|
148
150
|
referralFeesDestTa: publicKey(referralDestTa),
|
149
151
|
referralFeesDestMint: publicKey(referralFeesDestMint),
|
@@ -8,13 +8,8 @@ import {
|
|
8
8
|
PublicKey as UmiPublicKey,
|
9
9
|
isSome,
|
10
10
|
transactionBuilder,
|
11
|
-
signerIdentity,
|
12
11
|
} from "@metaplex-foundation/umi";
|
13
12
|
import { toWeb3JsPublicKey } from "@metaplex-foundation/umi-web3js-adapters";
|
14
|
-
import {
|
15
|
-
WalletAdapter,
|
16
|
-
walletAdapterIdentity,
|
17
|
-
} from "@metaplex-foundation/umi-signer-wallet-adapters";
|
18
13
|
import {
|
19
14
|
DCASettings,
|
20
15
|
DCASettingsInpArgs,
|
@@ -29,12 +24,10 @@ import {
|
|
29
24
|
UpdatePositionDataArgs,
|
30
25
|
cancelDCA,
|
31
26
|
closePosition,
|
32
|
-
createSolautoProgram,
|
33
27
|
safeFetchSolautoPosition,
|
34
28
|
updatePosition,
|
35
29
|
} from "../generated";
|
36
30
|
import {
|
37
|
-
getReferralState,
|
38
31
|
getSolautoPositionAccount,
|
39
32
|
getTokenAccount,
|
40
33
|
} from "../utils/accountUtils";
|
@@ -43,32 +36,28 @@ import {
|
|
43
36
|
getWrappedInstruction,
|
44
37
|
splTokenTransferUmiIx,
|
45
38
|
} from "../utils/solanaUtils";
|
46
|
-
import { FlashLoanDetails } from "../utils/solauto/rebalanceUtils";
|
39
|
+
import { FlashLoanDetails, RebalanceValues } from "../utils/solauto/rebalanceUtils";
|
47
40
|
import {
|
48
41
|
MIN_POSITION_STATE_FRESHNESS_SECS,
|
49
42
|
SOLAUTO_LUT,
|
50
43
|
} from "../constants/solautoConstants";
|
51
44
|
import { currentUnixSeconds } from "../utils/generalUtils";
|
52
45
|
import { LivePositionUpdates } from "../utils/solauto/generalUtils";
|
53
|
-
import {
|
54
|
-
|
46
|
+
import {
|
47
|
+
ReferralStateManager,
|
48
|
+
ReferralStateManagerArgs,
|
49
|
+
} from "./referralStateManager";
|
55
50
|
import { QuoteResponse } from "@jup-ag/api";
|
56
51
|
|
57
|
-
export interface SolautoClientArgs {
|
52
|
+
export interface SolautoClientArgs extends ReferralStateManagerArgs {
|
58
53
|
new?: boolean;
|
59
|
-
authority?: PublicKey;
|
60
54
|
positionId?: number;
|
61
|
-
signer?: Signer;
|
62
|
-
wallet?: WalletAdapter;
|
63
|
-
|
64
55
|
supplyMint?: PublicKey;
|
65
56
|
debtMint?: PublicKey;
|
66
|
-
|
67
|
-
referredByAuthority?: PublicKey;
|
68
57
|
}
|
69
58
|
|
70
|
-
export abstract class SolautoClient extends
|
71
|
-
public lendingPlatform
|
59
|
+
export abstract class SolautoClient extends ReferralStateManager {
|
60
|
+
public lendingPlatform?: LendingPlatform;
|
72
61
|
|
73
62
|
public authority!: PublicKey;
|
74
63
|
public signer!: Signer;
|
@@ -90,9 +79,6 @@ export abstract class SolautoClient extends TxHandler {
|
|
90
79
|
public positionDebtTa!: PublicKey;
|
91
80
|
public signerDebtTa!: PublicKey;
|
92
81
|
|
93
|
-
public referralStateManager!: ReferralStateManager;
|
94
|
-
public referredBySupplyTa?: PublicKey;
|
95
|
-
|
96
82
|
public solautoFeesWallet!: PublicKey;
|
97
83
|
public solautoFeesSupplyTa!: PublicKey;
|
98
84
|
|
@@ -100,32 +86,11 @@ export abstract class SolautoClient extends TxHandler {
|
|
100
86
|
|
101
87
|
public livePositionUpdates: LivePositionUpdates = new LivePositionUpdates();
|
102
88
|
|
103
|
-
|
104
|
-
super(
|
105
|
-
|
106
|
-
this.umi = this.umi.use({
|
107
|
-
install(umi) {
|
108
|
-
umi.programs.add(createSolautoProgram(), false);
|
109
|
-
},
|
110
|
-
});
|
111
|
-
}
|
112
|
-
|
113
|
-
async initialize(args: SolautoClientArgs, lendingPlatform: LendingPlatform) {
|
114
|
-
if (!args.signer && !args.wallet) {
|
115
|
-
throw new Error("Signer or wallet must be provided");
|
116
|
-
}
|
117
|
-
this.umi = this.umi.use(
|
118
|
-
args.signer
|
119
|
-
? signerIdentity(args.signer)
|
120
|
-
: walletAdapterIdentity(args.wallet!, true)
|
121
|
-
);
|
122
|
-
|
123
|
-
this.signer = this.umi.identity;
|
124
|
-
this.authority = args.authority ?? toWeb3JsPublicKey(this.signer.publicKey);
|
89
|
+
async initialize(args: SolautoClientArgs) {
|
90
|
+
await super.initialize(args);
|
125
91
|
|
126
92
|
this.positionId = args.positionId ?? 0;
|
127
93
|
this.selfManaged = this.positionId === 0;
|
128
|
-
this.lendingPlatform = lendingPlatform;
|
129
94
|
this.solautoPosition = getSolautoPositionAccount(
|
130
95
|
this.authority,
|
131
96
|
this.positionId
|
@@ -167,35 +132,18 @@ export abstract class SolautoClient extends TxHandler {
|
|
167
132
|
this.debtMint
|
168
133
|
);
|
169
134
|
|
170
|
-
this.referralStateManager = new ReferralStateManager(this.rpcUrl);
|
171
|
-
await this.referralStateManager.initialize({
|
172
|
-
referralAuthority: this.authority,
|
173
|
-
referredByAuthority: args.referredByAuthority,
|
174
|
-
signer: args.signer,
|
175
|
-
wallet: args.wallet,
|
176
|
-
});
|
177
|
-
console.log(this.referralStateManager.referredBy?.toString());
|
178
|
-
if (this.referralStateManager.referredByState !== undefined) {
|
179
|
-
this.referredBySupplyTa = getTokenAccount(
|
180
|
-
this.referralStateManager.referredByState,
|
181
|
-
this.supplyMint
|
182
|
-
);
|
183
|
-
}
|
184
|
-
|
185
135
|
this.solautoFeesWallet = SOLAUTO_FEES_WALLET;
|
186
136
|
this.solautoFeesSupplyTa = getTokenAccount(
|
187
137
|
this.solautoFeesWallet,
|
188
138
|
this.supplyMint
|
189
139
|
);
|
190
140
|
|
191
|
-
const authorityReferralStateData =
|
192
|
-
this.referralStateManager.referralStateData;
|
193
141
|
this.authorityLutAddress =
|
194
|
-
|
195
|
-
!toWeb3JsPublicKey(
|
142
|
+
this.referralStateData?.lookupTable &&
|
143
|
+
!toWeb3JsPublicKey(this.referralStateData.lookupTable).equals(
|
196
144
|
PublicKey.default
|
197
145
|
)
|
198
|
-
? toWeb3JsPublicKey(
|
146
|
+
? toWeb3JsPublicKey(this.referralStateData.lookupTable)
|
199
147
|
: undefined;
|
200
148
|
|
201
149
|
this.log("Position state: ", this.solautoPositionState);
|
@@ -212,14 +160,11 @@ export abstract class SolautoClient extends TxHandler {
|
|
212
160
|
);
|
213
161
|
}
|
214
162
|
|
215
|
-
|
216
|
-
this.
|
217
|
-
|
218
|
-
this.referredBySupplyTa = getTokenAccount(
|
219
|
-
this.referralStateManager.referredByState!,
|
220
|
-
this.supplyMint
|
221
|
-
);
|
163
|
+
referredBySupplyTa(): PublicKey | undefined {
|
164
|
+
if (this.referredByState !== undefined) {
|
165
|
+
return getTokenAccount(this.referredByState, this.supplyMint);
|
222
166
|
}
|
167
|
+
return undefined;
|
223
168
|
}
|
224
169
|
|
225
170
|
async resetLiveTxUpdates(success?: boolean) {
|
@@ -268,8 +213,8 @@ export abstract class SolautoClient extends TxHandler {
|
|
268
213
|
this.solautoPosition,
|
269
214
|
this.positionSupplyTa,
|
270
215
|
this.positionDebtTa,
|
271
|
-
this.
|
272
|
-
...(this.referredBySupplyTa ? [this.referredBySupplyTa] : []),
|
216
|
+
this.referralState,
|
217
|
+
...(this.referredBySupplyTa() ? [this.referredBySupplyTa()!] : []),
|
273
218
|
];
|
274
219
|
}
|
275
220
|
|
@@ -333,7 +278,7 @@ export abstract class SolautoClient extends TxHandler {
|
|
333
278
|
const addingReferredBy =
|
334
279
|
accountsToAdd.length === 1 &&
|
335
280
|
accountsToAdd[0].toString().toLowerCase() ===
|
336
|
-
this.referredBySupplyTa?.toString().toLowerCase();
|
281
|
+
this.referredBySupplyTa()?.toString().toLowerCase();
|
337
282
|
|
338
283
|
if (tx.getInstructions().length > 0) {
|
339
284
|
this.log("Updating authority lookup table...");
|
@@ -592,6 +537,7 @@ export abstract class SolautoClient extends TxHandler {
|
|
592
537
|
rebalanceStep: "A" | "B",
|
593
538
|
jupQuote: QuoteResponse,
|
594
539
|
rebalanceType: SolautoRebalanceTypeArgs,
|
540
|
+
rebalanceValues: RebalanceValues,
|
595
541
|
flashLoan?: FlashLoanDetails,
|
596
542
|
targetLiqUtilizationRateBps?: number
|
597
543
|
): TransactionBuilder;
|
@@ -32,7 +32,11 @@ import {
|
|
32
32
|
marginfiRebalance,
|
33
33
|
marginfiRefreshData,
|
34
34
|
} from "../generated";
|
35
|
-
import {
|
35
|
+
import {
|
36
|
+
getMarginfiAccountPDA,
|
37
|
+
getReferralState,
|
38
|
+
getTokenAccount,
|
39
|
+
} from "../utils/accountUtils";
|
36
40
|
import { generateRandomU64, safeGetPrice } from "../utils/generalUtils";
|
37
41
|
import {
|
38
42
|
MARGINFI_PROGRAM_ID,
|
@@ -47,7 +51,10 @@ import {
|
|
47
51
|
safeFetchAllMarginfiAccount,
|
48
52
|
safeFetchMarginfiAccount,
|
49
53
|
} from "../marginfi-sdk";
|
50
|
-
import {
|
54
|
+
import {
|
55
|
+
FlashLoanDetails,
|
56
|
+
RebalanceValues,
|
57
|
+
} from "../utils/solauto/rebalanceUtils";
|
51
58
|
import {
|
52
59
|
findMarginfiAccounts,
|
53
60
|
getAllMarginfiAccountsByAuthority,
|
@@ -85,7 +92,9 @@ export class SolautoMarginfiClient extends SolautoClient {
|
|
85
92
|
public intermediaryMarginfiAccount?: MarginfiAccount;
|
86
93
|
|
87
94
|
async initialize(args: SolautoMarginfiClientArgs) {
|
88
|
-
await super.initialize(args
|
95
|
+
await super.initialize(args);
|
96
|
+
|
97
|
+
this.lendingPlatform = LendingPlatform.Marginfi;
|
89
98
|
|
90
99
|
if (this.selfManaged) {
|
91
100
|
this.marginfiAccount =
|
@@ -262,12 +271,12 @@ export class SolautoMarginfiClient extends SolautoClient {
|
|
262
271
|
return marginfiOpenPosition(this.umi, {
|
263
272
|
signer: this.signer,
|
264
273
|
marginfiProgram: publicKey(MARGINFI_PROGRAM_ID),
|
265
|
-
signerReferralState: publicKey(this.
|
266
|
-
referredByState: this.
|
267
|
-
? publicKey(this.
|
274
|
+
signerReferralState: publicKey(this.referralState),
|
275
|
+
referredByState: this.referredByState
|
276
|
+
? publicKey(this.referredByState)
|
268
277
|
: undefined,
|
269
|
-
referredBySupplyTa: this.referredBySupplyTa
|
270
|
-
? publicKey(this.referredBySupplyTa)
|
278
|
+
referredBySupplyTa: this.referredBySupplyTa()
|
279
|
+
? publicKey(this.referredBySupplyTa()!)
|
271
280
|
: undefined,
|
272
281
|
solautoPosition: publicKey(this.solautoPosition),
|
273
282
|
marginfiGroup: publicKey(this.marginfiGroup),
|
@@ -447,6 +456,7 @@ export class SolautoMarginfiClient extends SolautoClient {
|
|
447
456
|
rebalanceStep: "A" | "B",
|
448
457
|
jupQuote: QuoteResponse,
|
449
458
|
rebalanceType: SolautoRebalanceTypeArgs,
|
459
|
+
rebalanceValues: RebalanceValues,
|
450
460
|
flashLoan?: FlashLoanDetails,
|
451
461
|
targetLiqUtilizationRateBps?: number
|
452
462
|
): TransactionBuilder {
|
@@ -471,13 +481,14 @@ export class SolautoMarginfiClient extends SolautoClient {
|
|
471
481
|
ixsSysvar: publicKey(SYSVAR_INSTRUCTIONS_PUBKEY),
|
472
482
|
solautoFeesSupplyTa:
|
473
483
|
rebalanceStep === "B" ? publicKey(this.solautoFeesSupplyTa) : undefined,
|
474
|
-
authorityReferralState: publicKey(
|
475
|
-
|
476
|
-
|
477
|
-
referredBySupplyTa: this.referredBySupplyTa
|
478
|
-
? publicKey(this.referredBySupplyTa)
|
484
|
+
authorityReferralState: publicKey(this.referralState),
|
485
|
+
referredBySupplyTa: this.referredBySupplyTa()
|
486
|
+
? publicKey(this.referredBySupplyTa()!)
|
479
487
|
: undefined,
|
480
|
-
positionAuthority:
|
488
|
+
positionAuthority:
|
489
|
+
rebalanceValues.rebalanceAction === "dca"
|
490
|
+
? publicKey(this.authority)
|
491
|
+
: undefined,
|
481
492
|
solautoPosition: publicKey(this.solautoPosition),
|
482
493
|
marginfiGroup: publicKey(this.marginfiGroup),
|
483
494
|
marginfiAccount: publicKey(this.marginfiAccountPk),
|
@@ -490,9 +501,9 @@ export class SolautoMarginfiClient extends SolautoClient {
|
|
490
501
|
supplyBank: publicKey(this.marginfiSupplyAccounts.bank),
|
491
502
|
supplyPriceOracle: publicKey(this.supplyPriceOracle),
|
492
503
|
positionSupplyTa: publicKey(this.positionSupplyTa),
|
493
|
-
authoritySupplyTa:
|
494
|
-
getTokenAccount(this.authority, this.supplyMint)
|
495
|
-
|
504
|
+
authoritySupplyTa: this.selfManaged
|
505
|
+
? publicKey(getTokenAccount(this.authority, this.supplyMint))
|
506
|
+
: undefined,
|
496
507
|
vaultSupplyTa: needSupplyAccounts
|
497
508
|
? publicKey(this.marginfiSupplyAccounts.liquidityVault)
|
498
509
|
: undefined,
|
@@ -502,9 +513,9 @@ export class SolautoMarginfiClient extends SolautoClient {
|
|
502
513
|
debtBank: publicKey(this.marginfiDebtAccounts.bank),
|
503
514
|
debtPriceOracle: publicKey(this.debtPriceOracle),
|
504
515
|
positionDebtTa: publicKey(this.positionDebtTa),
|
505
|
-
authorityDebtTa:
|
506
|
-
getTokenAccount(this.authority, this.debtMint)
|
507
|
-
|
516
|
+
authorityDebtTa: this.selfManaged
|
517
|
+
? publicKey(getTokenAccount(this.authority, this.debtMint))
|
518
|
+
: undefined,
|
508
519
|
vaultDebtTa: needDebtAccounts
|
509
520
|
? publicKey(this.marginfiDebtAccounts.liquidityVault)
|
510
521
|
: undefined,
|
package/src/clients/txHandler.ts
CHANGED
@@ -7,9 +7,6 @@ export abstract class TxHandler {
|
|
7
7
|
public umi!: Umi;
|
8
8
|
public connection!: Connection;
|
9
9
|
|
10
|
-
public referredBy?: PublicKey;
|
11
|
-
public referredByState?: PublicKey;
|
12
|
-
|
13
10
|
constructor(
|
14
11
|
rpcUrl: string,
|
15
12
|
localTest?: boolean
|
@@ -31,6 +28,4 @@ export abstract class TxHandler {
|
|
31
28
|
abstract defaultLookupTables(): string[];
|
32
29
|
|
33
30
|
abstract resetLiveTxUpdates(success?: boolean): Promise<void>;
|
34
|
-
|
35
|
-
abstract setReferredBy(referredBy?: PublicKey): void;
|
36
31
|
}
|
@@ -46,7 +46,7 @@ export type MarginfiRebalanceInstructionAccounts = {
|
|
46
46
|
solautoFeesSupplyTa?: PublicKey | Pda;
|
47
47
|
authorityReferralState: PublicKey | Pda;
|
48
48
|
referredBySupplyTa?: PublicKey | Pda;
|
49
|
-
positionAuthority
|
49
|
+
positionAuthority?: PublicKey | Pda;
|
50
50
|
solautoPosition: PublicKey | Pda;
|
51
51
|
marginfiGroup: PublicKey | Pda;
|
52
52
|
marginfiAccount: PublicKey | Pda;
|
@@ -17,6 +17,7 @@ export * from './positionState';
|
|
17
17
|
export * from './positionTokenUsage';
|
18
18
|
export * from './positionType';
|
19
19
|
export * from './rebalanceData';
|
20
|
+
export * from './rebalanceDirection';
|
20
21
|
export * from './solautoAction';
|
21
22
|
export * from './solautoRebalanceType';
|
22
23
|
export * from './solautoSettingsParameters';
|
@@ -11,20 +11,22 @@ import {
|
|
11
11
|
array,
|
12
12
|
bytes,
|
13
13
|
struct,
|
14
|
-
u16,
|
15
14
|
u64,
|
16
15
|
u8,
|
17
16
|
} from '@metaplex-foundation/umi/serializers';
|
18
17
|
import {
|
18
|
+
RebalanceDirection,
|
19
|
+
RebalanceDirectionArgs,
|
19
20
|
SolautoRebalanceType,
|
20
21
|
SolautoRebalanceTypeArgs,
|
22
|
+
getRebalanceDirectionSerializer,
|
21
23
|
getSolautoRebalanceTypeSerializer,
|
22
24
|
} from '.';
|
23
25
|
|
24
26
|
export type RebalanceData = {
|
25
27
|
rebalanceType: SolautoRebalanceType;
|
26
28
|
padding1: Array<number>;
|
27
|
-
|
29
|
+
rebalanceDirection: RebalanceDirection;
|
28
30
|
padding2: Array<number>;
|
29
31
|
flashLoanAmount: bigint;
|
30
32
|
padding: Uint8Array;
|
@@ -33,7 +35,7 @@ export type RebalanceData = {
|
|
33
35
|
export type RebalanceDataArgs = {
|
34
36
|
rebalanceType: SolautoRebalanceTypeArgs;
|
35
37
|
padding1: Array<number>;
|
36
|
-
|
38
|
+
rebalanceDirection: RebalanceDirectionArgs;
|
37
39
|
padding2: Array<number>;
|
38
40
|
flashLoanAmount: number | bigint;
|
39
41
|
padding: Uint8Array;
|
@@ -47,8 +49,8 @@ export function getRebalanceDataSerializer(): Serializer<
|
|
47
49
|
[
|
48
50
|
['rebalanceType', getSolautoRebalanceTypeSerializer()],
|
49
51
|
['padding1', array(u8(), { size: 7 })],
|
50
|
-
['
|
51
|
-
['padding2', array(u8(), { size:
|
52
|
+
['rebalanceDirection', getRebalanceDirectionSerializer()],
|
53
|
+
['padding2', array(u8(), { size: 7 })],
|
52
54
|
['flashLoanAmount', u64()],
|
53
55
|
['padding', bytes({ size: 32 })],
|
54
56
|
],
|
@@ -0,0 +1,25 @@
|
|
1
|
+
/**
|
2
|
+
* This code was AUTOGENERATED using the kinobi library.
|
3
|
+
* Please DO NOT EDIT THIS FILE, instead use visitors
|
4
|
+
* to add features, then rerun kinobi to update it.
|
5
|
+
*
|
6
|
+
* @see https://github.com/metaplex-foundation/kinobi
|
7
|
+
*/
|
8
|
+
|
9
|
+
import { Serializer, scalarEnum } from '@metaplex-foundation/umi/serializers';
|
10
|
+
|
11
|
+
export enum RebalanceDirection {
|
12
|
+
Boost,
|
13
|
+
Repay,
|
14
|
+
}
|
15
|
+
|
16
|
+
export type RebalanceDirectionArgs = RebalanceDirection;
|
17
|
+
|
18
|
+
export function getRebalanceDirectionSerializer(): Serializer<
|
19
|
+
RebalanceDirectionArgs,
|
20
|
+
RebalanceDirection
|
21
|
+
> {
|
22
|
+
return scalarEnum<RebalanceDirection>(RebalanceDirection, {
|
23
|
+
description: 'RebalanceDirection',
|
24
|
+
}) as Serializer<RebalanceDirectionArgs, RebalanceDirection>;
|
25
|
+
}
|