@haven-fi/solauto-sdk 1.0.11 → 1.0.12
Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,5 @@
|
|
1
|
-
import { PublicKey
|
1
|
+
import { PublicKey } from "@solana/web3.js";
|
2
|
+
import { Umi } from "@metaplex-foundation/umi";
|
2
3
|
import { AutomationSettings, DCASettings, DCASettingsInpArgs, FeeType, LendingPlatform, PositionState, SolautoSettingsParameters, SolautoSettingsParametersInpArgs } from "../../generated";
|
3
4
|
export declare function nextAutomationPeriodTimestamp(automation: AutomationSettings): number;
|
4
5
|
export declare function eligibleForNextAutomationPeriod(automation: AutomationSettings): boolean;
|
@@ -21,6 +22,12 @@ export declare function getSolautoManagedPositions(umi: Umi, authority?: PublicK
|
|
21
22
|
}[]>;
|
22
23
|
export declare function getAllReferralStates(umi: Umi): Promise<PublicKey[]>;
|
23
24
|
export declare function getReferralsByUser(umi: Umi, user: PublicKey): Promise<PublicKey[]>;
|
25
|
+
export interface SolautoPositionDetails {
|
26
|
+
positionId: number;
|
27
|
+
lendingPlatform: LendingPlatform;
|
28
|
+
protocolAccount?: PublicKey;
|
29
|
+
}
|
30
|
+
export declare function getAllPositionsByAuthority(umi: Umi, user: PublicKey): Promise<SolautoPositionDetails[]>;
|
24
31
|
type PositionAdjustment = {
|
25
32
|
type: "supply";
|
26
33
|
value: bigint;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"generalUtils.d.ts","sourceRoot":"","sources":["../../../src/utils/solauto/generalUtils.ts"],"names":[],"mappings":"AAAA,OAAO,
|
1
|
+
{"version":3,"file":"generalUtils.d.ts","sourceRoot":"","sources":["../../../src/utils/solauto/generalUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAoB,GAAG,EAAE,MAAM,0BAA0B,CAAC;AACjE,OAAO,EACL,kBAAkB,EAClB,WAAW,EACX,kBAAkB,EAClB,OAAO,EACP,eAAe,EACf,aAAa,EAEb,yB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value: true });
|
3
|
-
exports.LivePositionUpdates = exports.getReferralsByUser = exports.getAllReferralStates = exports.getSolautoManagedPositions = exports.eligibleForRefresh = exports.eligibileForRebalance = exports.maxRepayTo = exports.maxRepayFrom = exports.getMaxLiqUtilizationRate = exports.getSolautoFeesBps = exports.getAdjustedSettingsFromAutomation = exports.getUpdatedValueFromAutomation = exports.eligibleForNextAutomationPeriod = exports.nextAutomationPeriodTimestamp = void 0;
|
3
|
+
exports.LivePositionUpdates = exports.getAllPositionsByAuthority = exports.getReferralsByUser = exports.getAllReferralStates = exports.getSolautoManagedPositions = exports.eligibleForRefresh = exports.eligibileForRebalance = exports.maxRepayTo = exports.maxRepayFrom = exports.getMaxLiqUtilizationRate = exports.getSolautoFeesBps = exports.getAdjustedSettingsFromAutomation = exports.getUpdatedValueFromAutomation = exports.eligibleForNextAutomationPeriod = exports.nextAutomationPeriodTimestamp = void 0;
|
4
4
|
const umi_1 = require("@metaplex-foundation/umi");
|
5
5
|
const generated_1 = require("../../generated");
|
6
6
|
const generalUtils_1 = require("../generalUtils");
|
@@ -129,7 +129,7 @@ async function getSolautoManagedPositions(umi, authority) {
|
|
129
129
|
? [
|
130
130
|
{
|
131
131
|
memcmp: {
|
132
|
-
bytes: new Uint8Array(
|
132
|
+
bytes: new Uint8Array(authority.toBuffer()),
|
133
133
|
offset: 8,
|
134
134
|
},
|
135
135
|
},
|
@@ -143,7 +143,7 @@ async function getSolautoManagedPositions(umi, authority) {
|
|
143
143
|
...Array((0, generated_1.getSolautoPositionSize)() - x.data.length).fill(0),
|
144
144
|
]));
|
145
145
|
return {
|
146
|
-
authority: position.authority,
|
146
|
+
authority: (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(position.authority),
|
147
147
|
positionId: position.positionId[0],
|
148
148
|
lendingPlatform: position.position.lendingPlatform,
|
149
149
|
};
|
@@ -163,7 +163,7 @@ async function getAllReferralStates(umi) {
|
|
163
163
|
},
|
164
164
|
],
|
165
165
|
});
|
166
|
-
return accounts.map((x) => x.publicKey);
|
166
|
+
return accounts.map((x) => (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(x.publicKey));
|
167
167
|
}
|
168
168
|
exports.getAllReferralStates = getAllReferralStates;
|
169
169
|
async function getReferralsByUser(umi, user) {
|
@@ -171,7 +171,7 @@ async function getReferralsByUser(umi, user) {
|
|
171
171
|
// padding: [u8; 7],
|
172
172
|
// authority: Pubkey,
|
173
173
|
// referred_by_state: Pubkey,
|
174
|
-
const userReferralState = await (0, accountUtils_1.getReferralState)(
|
174
|
+
const userReferralState = await (0, accountUtils_1.getReferralState)(user);
|
175
175
|
const accounts = await umi.rpc.getProgramAccounts(generated_1.SOLAUTO_PROGRAM_ID, {
|
176
176
|
commitment: "finalized",
|
177
177
|
dataSlice: {
|
@@ -190,13 +190,30 @@ async function getReferralsByUser(umi, user) {
|
|
190
190
|
},
|
191
191
|
],
|
192
192
|
});
|
193
|
-
return accounts.map((x) => x.publicKey);
|
193
|
+
return accounts.map((x) => (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(x.publicKey));
|
194
194
|
}
|
195
195
|
exports.getReferralsByUser = getReferralsByUser;
|
196
|
+
async function getAllPositionsByAuthority(umi, user) {
|
197
|
+
const allPositions = [];
|
198
|
+
const solautoManagedPositions = await getSolautoManagedPositions(umi, user);
|
199
|
+
allPositions.push(...solautoManagedPositions.map((x) => ({
|
200
|
+
positionId: x.positionId,
|
201
|
+
lendingPlatform: x.lendingPlatform,
|
202
|
+
})));
|
203
|
+
const marginfiPositions = await (0, marginfiUtils_1.getAllMarginfiAccountsByAuthority)(umi, user);
|
204
|
+
allPositions.push(...marginfiPositions.map((x) => ({
|
205
|
+
positionId: 0,
|
206
|
+
lendingPlatform: generated_1.LendingPlatform.Marginfi,
|
207
|
+
protocolAccount: (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(x.publicKey),
|
208
|
+
})));
|
209
|
+
// TODO support other platforms
|
210
|
+
return [];
|
211
|
+
}
|
212
|
+
exports.getAllPositionsByAuthority = getAllPositionsByAuthority;
|
196
213
|
async function positionStateWithLatestPrices(umi, state, protocolAccount, lendingPlatform) {
|
197
214
|
if ((0, generalUtils_1.currentUnixSeconds)() - Number(state.lastUpdated) > 60 * 60 * 24 * 7) {
|
198
215
|
if (lendingPlatform === generated_1.LendingPlatform.Marginfi) {
|
199
|
-
return await (0, marginfiUtils_1.getMarginfiAccountPositionState)(umi, (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(state.supply.mint), (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(state.debt.mint),
|
216
|
+
return await (0, marginfiUtils_1.getMarginfiAccountPositionState)(umi, (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(state.supply.mint), (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(state.debt.mint), protocolAccount);
|
200
217
|
}
|
201
218
|
else {
|
202
219
|
throw new Error("Lending platorm not yet supported");
|
package/package.json
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
import {
|
1
|
+
import { PublicKey } from "@solana/web3.js";
|
2
|
+
import { isOption, isSome, Umi } from "@metaplex-foundation/umi";
|
2
3
|
import {
|
3
4
|
AutomationSettings,
|
4
5
|
DCASettings,
|
@@ -25,7 +26,10 @@ import { MAX_REPAY_GAP_BPS } from "../../constants/solautoConstants";
|
|
25
26
|
import { getReferralState } from "../accountUtils";
|
26
27
|
import { toWeb3JsPublicKey } from "@metaplex-foundation/umi-web3js-adapters";
|
27
28
|
import { USD_DECIMALS } from "../../constants";
|
28
|
-
import {
|
29
|
+
import {
|
30
|
+
getAllMarginfiAccountsByAuthority,
|
31
|
+
getMarginfiAccountPositionState,
|
32
|
+
} from "../marginfiUtils";
|
29
33
|
|
30
34
|
function newPeriodsPassed(
|
31
35
|
automation: AutomationSettings,
|
@@ -225,7 +229,7 @@ export async function getSolautoManagedPositions(
|
|
225
229
|
? [
|
226
230
|
{
|
227
231
|
memcmp: {
|
228
|
-
bytes: new Uint8Array(
|
232
|
+
bytes: new Uint8Array(authority.toBuffer()),
|
229
233
|
offset: 8,
|
230
234
|
},
|
231
235
|
},
|
@@ -242,7 +246,7 @@ export async function getSolautoManagedPositions(
|
|
242
246
|
])
|
243
247
|
);
|
244
248
|
return {
|
245
|
-
authority: position.authority,
|
249
|
+
authority: toWeb3JsPublicKey(position.authority),
|
246
250
|
positionId: position.positionId[0],
|
247
251
|
lendingPlatform: position.position.lendingPlatform,
|
248
252
|
};
|
@@ -263,7 +267,7 @@ export async function getAllReferralStates(umi: Umi): Promise<PublicKey[]> {
|
|
263
267
|
],
|
264
268
|
});
|
265
269
|
|
266
|
-
return accounts.map((x) => x.publicKey);
|
270
|
+
return accounts.map((x) => toWeb3JsPublicKey(x.publicKey));
|
267
271
|
}
|
268
272
|
|
269
273
|
export async function getReferralsByUser(
|
@@ -275,7 +279,7 @@ export async function getReferralsByUser(
|
|
275
279
|
// authority: Pubkey,
|
276
280
|
// referred_by_state: Pubkey,
|
277
281
|
|
278
|
-
const userReferralState = await getReferralState(
|
282
|
+
const userReferralState = await getReferralState(user);
|
279
283
|
const accounts = await umi.rpc.getProgramAccounts(SOLAUTO_PROGRAM_ID, {
|
280
284
|
commitment: "finalized",
|
281
285
|
dataSlice: {
|
@@ -295,7 +299,41 @@ export async function getReferralsByUser(
|
|
295
299
|
],
|
296
300
|
});
|
297
301
|
|
298
|
-
return accounts.map((x) => x.publicKey);
|
302
|
+
return accounts.map((x) => toWeb3JsPublicKey(x.publicKey));
|
303
|
+
}
|
304
|
+
|
305
|
+
export interface SolautoPositionDetails {
|
306
|
+
positionId: number;
|
307
|
+
lendingPlatform: LendingPlatform;
|
308
|
+
protocolAccount?: PublicKey;
|
309
|
+
}
|
310
|
+
|
311
|
+
export async function getAllPositionsByAuthority(
|
312
|
+
umi: Umi,
|
313
|
+
user: PublicKey
|
314
|
+
): Promise<SolautoPositionDetails[]> {
|
315
|
+
const allPositions: SolautoPositionDetails[] = [];
|
316
|
+
|
317
|
+
const solautoManagedPositions = await getSolautoManagedPositions(umi, user);
|
318
|
+
allPositions.push(
|
319
|
+
...solautoManagedPositions.map((x) => ({
|
320
|
+
positionId: x.positionId,
|
321
|
+
lendingPlatform: x.lendingPlatform,
|
322
|
+
}))
|
323
|
+
);
|
324
|
+
|
325
|
+
const marginfiPositions = await getAllMarginfiAccountsByAuthority(umi, user);
|
326
|
+
allPositions.push(
|
327
|
+
...marginfiPositions.map((x) => ({
|
328
|
+
positionId: 0,
|
329
|
+
lendingPlatform: LendingPlatform.Marginfi,
|
330
|
+
protocolAccount: toWeb3JsPublicKey(x.publicKey),
|
331
|
+
}))
|
332
|
+
);
|
333
|
+
|
334
|
+
// TODO support other platforms
|
335
|
+
|
336
|
+
return [];
|
299
337
|
}
|
300
338
|
|
301
339
|
async function positionStateWithLatestPrices(
|
@@ -310,7 +348,7 @@ async function positionStateWithLatestPrices(
|
|
310
348
|
umi,
|
311
349
|
toWeb3JsPublicKey(state.supply.mint),
|
312
350
|
toWeb3JsPublicKey(state.debt.mint),
|
313
|
-
|
351
|
+
protocolAccount
|
314
352
|
);
|
315
353
|
} else {
|
316
354
|
throw new Error("Lending platorm not yet supported");
|