@haven-fi/solauto-sdk 1.0.648 → 1.0.649

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.
Files changed (45) hide show
  1. package/README.md +0 -1
  2. package/dist/generated/types/positionData.d.ts +6 -6
  3. package/dist/generated/types/positionData.d.ts.map +1 -1
  4. package/dist/generated/types/positionData.js +3 -3
  5. package/dist/idls/switchboard.json +1949 -1
  6. package/dist/pyth-sdk/accounts/index.d.ts +9 -0
  7. package/dist/pyth-sdk/accounts/index.d.ts.map +1 -0
  8. package/dist/pyth-sdk/accounts/index.js +24 -0
  9. package/dist/pyth-sdk/accounts/priceUpdateV2Account.d.ts +39 -0
  10. package/dist/pyth-sdk/accounts/priceUpdateV2Account.d.ts.map +1 -0
  11. package/dist/pyth-sdk/accounts/priceUpdateV2Account.js +75 -0
  12. package/dist/pyth-sdk/index.d.ts +10 -0
  13. package/dist/pyth-sdk/index.d.ts.map +1 -0
  14. package/dist/pyth-sdk/index.js +25 -0
  15. package/dist/pyth-sdk/types/index.d.ts +10 -0
  16. package/dist/pyth-sdk/types/index.d.ts.map +1 -0
  17. package/dist/pyth-sdk/types/index.js +25 -0
  18. package/dist/pyth-sdk/types/priceMessage.d.ts +31 -0
  19. package/dist/pyth-sdk/types/priceMessage.d.ts.map +1 -0
  20. package/dist/pyth-sdk/types/priceMessage.js +23 -0
  21. package/dist/pyth-sdk/types/priceUpdateV2.d.ts +24 -0
  22. package/dist/pyth-sdk/types/priceUpdateV2.d.ts.map +1 -0
  23. package/dist/pyth-sdk/types/priceUpdateV2.js +20 -0
  24. package/dist/services/solauto/solautoClient.d.ts +0 -1
  25. package/dist/services/solauto/solautoClient.d.ts.map +1 -1
  26. package/dist/solautoPosition/solautoPositionEx.d.ts.map +1 -1
  27. package/dist/solautoPosition/solautoPositionEx.js +2 -2
  28. package/dist/utils/pythUtils.d.ts +0 -16
  29. package/dist/utils/pythUtils.d.ts.map +1 -1
  30. package/dist/utils/pythUtils.js +3 -52
  31. package/dist/utils/solautoUtils.js +3 -3
  32. package/package.json +1 -2
  33. package/src/generated/types/positionData.ts +9 -9
  34. package/src/idls/switchboard.json +1949 -1
  35. package/src/pyth-sdk/accounts/index.ts +9 -0
  36. package/src/pyth-sdk/accounts/priceUpdateV2Account.ts +179 -0
  37. package/src/pyth-sdk/index.ts +10 -0
  38. package/src/pyth-sdk/types/index.ts +10 -0
  39. package/src/pyth-sdk/types/priceMessage.ts +58 -0
  40. package/src/pyth-sdk/types/priceUpdateV2.ts +46 -0
  41. package/src/services/solauto/solautoClient.ts +0 -1
  42. package/src/solautoPosition/solautoPositionEx.ts +5 -4
  43. package/src/utils/pythUtils.ts +7 -54
  44. package/src/utils/solautoUtils.ts +3 -3
  45. package/tests/unit/rebalanceCalculations.ts +4 -4
@@ -0,0 +1,9 @@
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
+ export * from './priceUpdateV2Account';
@@ -0,0 +1,179 @@
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 {
10
+ Account,
11
+ Context,
12
+ Pda,
13
+ PublicKey,
14
+ RpcAccount,
15
+ RpcGetAccountOptions,
16
+ RpcGetAccountsOptions,
17
+ assertAccountExists,
18
+ deserializeAccount,
19
+ gpaBuilder,
20
+ publicKey as toPublicKey,
21
+ } from '@metaplex-foundation/umi';
22
+ import {
23
+ Serializer,
24
+ array,
25
+ mapSerializer,
26
+ publicKey as publicKeySerializer,
27
+ struct,
28
+ u64,
29
+ u8,
30
+ } from '@metaplex-foundation/umi/serializers';
31
+ import {
32
+ PriceMessage,
33
+ PriceMessageArgs,
34
+ getPriceMessageSerializer,
35
+ } from '../types';
36
+
37
+ export type PriceUpdateV2Account = Account<PriceUpdateV2AccountAccountData>;
38
+
39
+ export type PriceUpdateV2AccountAccountData = {
40
+ discriminator: Array<number>;
41
+ writeAuthority: PublicKey;
42
+ verificationLevel: number;
43
+ priceMessage: PriceMessage;
44
+ postedSlot: bigint;
45
+ };
46
+
47
+ export type PriceUpdateV2AccountAccountDataArgs = {
48
+ writeAuthority: PublicKey;
49
+ verificationLevel: number;
50
+ priceMessage: PriceMessageArgs;
51
+ postedSlot: number | bigint;
52
+ };
53
+
54
+ export function getPriceUpdateV2AccountAccountDataSerializer(): Serializer<
55
+ PriceUpdateV2AccountAccountDataArgs,
56
+ PriceUpdateV2AccountAccountData
57
+ > {
58
+ return mapSerializer<
59
+ PriceUpdateV2AccountAccountDataArgs,
60
+ any,
61
+ PriceUpdateV2AccountAccountData
62
+ >(
63
+ struct<PriceUpdateV2AccountAccountData>(
64
+ [
65
+ ['discriminator', array(u8(), { size: 8 })],
66
+ ['writeAuthority', publicKeySerializer()],
67
+ ['verificationLevel', u8()],
68
+ ['priceMessage', getPriceMessageSerializer()],
69
+ ['postedSlot', u64()],
70
+ ],
71
+ { description: 'PriceUpdateV2AccountAccountData' }
72
+ ),
73
+ (value) => ({
74
+ ...value,
75
+ discriminator: [93, 77, 250, 192, 211, 157, 97, 248],
76
+ })
77
+ ) as Serializer<
78
+ PriceUpdateV2AccountAccountDataArgs,
79
+ PriceUpdateV2AccountAccountData
80
+ >;
81
+ }
82
+
83
+ export function deserializePriceUpdateV2Account(
84
+ rawAccount: RpcAccount
85
+ ): PriceUpdateV2Account {
86
+ return deserializeAccount(
87
+ rawAccount,
88
+ getPriceUpdateV2AccountAccountDataSerializer()
89
+ );
90
+ }
91
+
92
+ export async function fetchPriceUpdateV2Account(
93
+ context: Pick<Context, 'rpc'>,
94
+ publicKey: PublicKey | Pda,
95
+ options?: RpcGetAccountOptions
96
+ ): Promise<PriceUpdateV2Account> {
97
+ const maybeAccount = await context.rpc.getAccount(
98
+ toPublicKey(publicKey, false),
99
+ options
100
+ );
101
+ assertAccountExists(maybeAccount, 'PriceUpdateV2Account');
102
+ return deserializePriceUpdateV2Account(maybeAccount);
103
+ }
104
+
105
+ export async function safeFetchPriceUpdateV2Account(
106
+ context: Pick<Context, 'rpc'>,
107
+ publicKey: PublicKey | Pda,
108
+ options?: RpcGetAccountOptions
109
+ ): Promise<PriceUpdateV2Account | null> {
110
+ const maybeAccount = await context.rpc.getAccount(
111
+ toPublicKey(publicKey, false),
112
+ options
113
+ );
114
+ return maybeAccount.exists
115
+ ? deserializePriceUpdateV2Account(maybeAccount)
116
+ : null;
117
+ }
118
+
119
+ export async function fetchAllPriceUpdateV2Account(
120
+ context: Pick<Context, 'rpc'>,
121
+ publicKeys: Array<PublicKey | Pda>,
122
+ options?: RpcGetAccountsOptions
123
+ ): Promise<PriceUpdateV2Account[]> {
124
+ const maybeAccounts = await context.rpc.getAccounts(
125
+ publicKeys.map((key) => toPublicKey(key, false)),
126
+ options
127
+ );
128
+ return maybeAccounts.map((maybeAccount) => {
129
+ assertAccountExists(maybeAccount, 'PriceUpdateV2Account');
130
+ return deserializePriceUpdateV2Account(maybeAccount);
131
+ });
132
+ }
133
+
134
+ export async function safeFetchAllPriceUpdateV2Account(
135
+ context: Pick<Context, 'rpc'>,
136
+ publicKeys: Array<PublicKey | Pda>,
137
+ options?: RpcGetAccountsOptions
138
+ ): Promise<PriceUpdateV2Account[]> {
139
+ const maybeAccounts = await context.rpc.getAccounts(
140
+ publicKeys.map((key) => toPublicKey(key, false)),
141
+ options
142
+ );
143
+ return maybeAccounts
144
+ .filter((maybeAccount) => maybeAccount.exists)
145
+ .map((maybeAccount) =>
146
+ deserializePriceUpdateV2Account(maybeAccount as RpcAccount)
147
+ );
148
+ }
149
+
150
+ export function getPriceUpdateV2AccountGpaBuilder(
151
+ context: Pick<Context, 'rpc' | 'programs'>
152
+ ) {
153
+ const programId = context.programs.getPublicKey(
154
+ '',
155
+ 'pythWSnswVUd12oZpeFP8e9CVaEqJg25g1Vtc2biRsT'
156
+ );
157
+ return gpaBuilder(context, programId)
158
+ .registerFields<{
159
+ discriminator: Array<number>;
160
+ writeAuthority: PublicKey;
161
+ verificationLevel: number;
162
+ priceMessage: PriceMessageArgs;
163
+ postedSlot: number | bigint;
164
+ }>({
165
+ discriminator: [0, array(u8(), { size: 8 })],
166
+ writeAuthority: [8, publicKeySerializer()],
167
+ verificationLevel: [40, u8()],
168
+ priceMessage: [41, getPriceMessageSerializer()],
169
+ postedSlot: [125, u64()],
170
+ })
171
+ .deserializeUsing<PriceUpdateV2Account>((account) =>
172
+ deserializePriceUpdateV2Account(account)
173
+ )
174
+ .whereField('discriminator', [93, 77, 250, 192, 211, 157, 97, 248]);
175
+ }
176
+
177
+ export function getPriceUpdateV2AccountSize(): number {
178
+ return 133;
179
+ }
@@ -0,0 +1,10 @@
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
+ export * from './accounts';
10
+ export * from './types';
@@ -0,0 +1,10 @@
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
+ export * from './priceMessage';
10
+ export * from './priceUpdateV2';
@@ -0,0 +1,58 @@
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 { PublicKey } from '@metaplex-foundation/umi';
10
+ import {
11
+ Serializer,
12
+ i32,
13
+ i64,
14
+ publicKey as publicKeySerializer,
15
+ struct,
16
+ u64,
17
+ } from '@metaplex-foundation/umi/serializers';
18
+
19
+ export type PriceMessage = {
20
+ feedId: PublicKey;
21
+ price: bigint;
22
+ conf: bigint;
23
+ exponent: number;
24
+ publishTime: bigint;
25
+ prevPublishTime: bigint;
26
+ emaPrice: bigint;
27
+ emaConf: bigint;
28
+ };
29
+
30
+ export type PriceMessageArgs = {
31
+ feedId: PublicKey;
32
+ price: number | bigint;
33
+ conf: number | bigint;
34
+ exponent: number;
35
+ publishTime: number | bigint;
36
+ prevPublishTime: number | bigint;
37
+ emaPrice: number | bigint;
38
+ emaConf: number | bigint;
39
+ };
40
+
41
+ export function getPriceMessageSerializer(): Serializer<
42
+ PriceMessageArgs,
43
+ PriceMessage
44
+ > {
45
+ return struct<PriceMessage>(
46
+ [
47
+ ['feedId', publicKeySerializer()],
48
+ ['price', i64()],
49
+ ['conf', u64()],
50
+ ['exponent', i32()],
51
+ ['publishTime', i64()],
52
+ ['prevPublishTime', i64()],
53
+ ['emaPrice', i64()],
54
+ ['emaConf', u64()],
55
+ ],
56
+ { description: 'PriceMessage' }
57
+ ) as Serializer<PriceMessageArgs, PriceMessage>;
58
+ }
@@ -0,0 +1,46 @@
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 { PublicKey } from '@metaplex-foundation/umi';
10
+ import {
11
+ Serializer,
12
+ publicKey as publicKeySerializer,
13
+ struct,
14
+ u64,
15
+ u8,
16
+ } from '@metaplex-foundation/umi/serializers';
17
+ import { PriceMessage, PriceMessageArgs, getPriceMessageSerializer } from '.';
18
+
19
+ export type PriceUpdateV2 = {
20
+ writeAuthority: PublicKey;
21
+ verificationLevel: number;
22
+ priceMessage: PriceMessage;
23
+ postedSlot: bigint;
24
+ };
25
+
26
+ export type PriceUpdateV2Args = {
27
+ writeAuthority: PublicKey;
28
+ verificationLevel: number;
29
+ priceMessage: PriceMessageArgs;
30
+ postedSlot: number | bigint;
31
+ };
32
+
33
+ export function getPriceUpdateV2Serializer(): Serializer<
34
+ PriceUpdateV2Args,
35
+ PriceUpdateV2
36
+ > {
37
+ return struct<PriceUpdateV2>(
38
+ [
39
+ ['writeAuthority', publicKeySerializer()],
40
+ ['verificationLevel', u8()],
41
+ ['priceMessage', getPriceMessageSerializer()],
42
+ ['postedSlot', u64()],
43
+ ],
44
+ { description: 'PriceUpdateV2' }
45
+ ) as Serializer<PriceUpdateV2Args, PriceUpdateV2>;
46
+ }
@@ -42,7 +42,6 @@ import {
42
42
  import { FlProviderAggregator } from "../flashLoans";
43
43
 
44
44
  export interface SolautoClientArgs extends ReferralStateManagerArgs {
45
- new?: boolean;
46
45
  positionId?: number;
47
46
  supplyMint?: PublicKey;
48
47
  debtMint?: PublicKey;
@@ -16,7 +16,6 @@ import {
16
16
  import {
17
17
  calcDebtUsd,
18
18
  calcNetWorth,
19
- calcNetWorthUsd,
20
19
  calcSupplyUsd,
21
20
  calcTotalDebt,
22
21
  calcTotalSupply,
@@ -25,7 +24,6 @@ import {
25
24
  currentUnixSeconds,
26
25
  debtLiquidityAvailable,
27
26
  debtLiquidityUsdAvailable,
28
- fetchTokenPrices,
29
27
  getLiqUtilzationRateBps,
30
28
  maxBoostToBps,
31
29
  maxRepayFromBps,
@@ -92,7 +90,7 @@ export abstract class SolautoPositionEx {
92
90
  this.lpUserAccount =
93
91
  args.customArgs?.lpUserAccount ??
94
92
  (args.data.position
95
- ? toWeb3JsPublicKey(args.data.position!.protocolUserAccount)
93
+ ? toWeb3JsPublicKey(args.data.position!.lpUserAccount)
96
94
  : undefined);
97
95
  this.lpEnv = args.customArgs?.lpEnv ?? "Prod";
98
96
 
@@ -193,7 +191,10 @@ export abstract class SolautoPositionEx {
193
191
  }
194
192
 
195
193
  get repayFromBps() {
196
- return (this.settings?.repayToBps ?? 0) + (this.settings?.repayGap ?? 0);
194
+ return Math.min(
195
+ (this.settings?.repayToBps ?? 0) + (this.settings?.repayGap ?? 0),
196
+ this.maxRepayFromBps
197
+ );
197
198
  }
198
199
 
199
200
  get maxRepayFromBps() {
@@ -1,54 +1,8 @@
1
1
  import { PublicKey } from "@solana/web3.js";
2
+ import { publicKey, Umi } from "@metaplex-foundation/umi";
2
3
  import { PYTH_PUSH_PROGRAM } from "../constants";
3
4
  import { u16ToArrayBufferLE, zip } from "./generalUtils";
4
- import * as borsh from "borsh";
5
- import { Umi } from "@metaplex-foundation/umi";
6
- import { fromWeb3JsPublicKey } from "@metaplex-foundation/umi-web3js-adapters";
7
-
8
- type PriceUpdateV2 = {
9
- writeAuthority: Buffer;
10
- verificationLevel: number;
11
- priceMessage: {
12
- feedId: Buffer;
13
- price: bigint;
14
- conf: bigint;
15
- exponent: number;
16
- publishTime: bigint;
17
- prevPublishTime: bigint;
18
- emaPrice: bigint;
19
- emaConf: bigint;
20
- };
21
- };
22
-
23
- const priceUpdateV2Schema = {
24
- struct: {
25
- writeAuthority: {
26
- array: { type: "u8", len: 32 },
27
- },
28
- verificationLevel: "u8",
29
- priceMessage: {
30
- struct: {
31
- feedId: { array: { type: "u8", len: 32 } },
32
- price: "i64",
33
- conf: "u64",
34
- exponent: "i32",
35
- publishTime: "i64",
36
- prevPublishTime: "i64",
37
- emaPrice: "i64",
38
- emaConf: "u64",
39
- },
40
- },
41
- postedSlot: "u64",
42
- },
43
- };
44
-
45
- export function parsePriceInfo(data: Uint8Array): PriceUpdateV2 {
46
- let decoded: PriceUpdateV2 = borsh.deserialize(
47
- priceUpdateV2Schema,
48
- data
49
- ) as any;
50
- return decoded;
51
- }
5
+ import { safeFetchAllPriceUpdateV2Account } from "../pyth-sdk";
52
6
 
53
7
  export async function getMostUpToDatePythOracle(
54
8
  umi: Umi,
@@ -56,12 +10,11 @@ export async function getMostUpToDatePythOracle(
56
10
  ) {
57
11
  const oracles = zip(
58
12
  oracleKeys,
59
- (
60
- await umi.rpc.getAccounts(
61
- oracleKeys.map((x) => fromWeb3JsPublicKey(x)),
62
- { commitment: "confirmed" }
63
- )
64
- ).map((x) => (x.exists ? parsePriceInfo(x!.data.slice(8)) : undefined))
13
+ await safeFetchAllPriceUpdateV2Account(
14
+ umi,
15
+ oracleKeys.map((x) => publicKey(x)),
16
+ { commitment: "confirmed" }
17
+ )
65
18
  ).sort(
66
19
  (a, b) =>
67
20
  Number(b[1]?.priceMessage.publishTime ?? 0) -
@@ -177,10 +177,10 @@ export async function getSolautoManagedPositions(
177
177
  if (position.position.lendingPlatform === LendingPlatform.Marginfi) {
178
178
  tokens = [
179
179
  findMarginfiAccounts(
180
- toWeb3JsPublicKey(position.position.protocolSupplyAccount)
180
+ toWeb3JsPublicKey(position.position.lpSupplyAccount)
181
181
  ).mint,
182
182
  findMarginfiAccounts(
183
- toWeb3JsPublicKey(position.position.protocolDebtAccount)
183
+ toWeb3JsPublicKey(position.position.lpDebtAccount)
184
184
  ).mint,
185
185
  ];
186
186
  }
@@ -192,7 +192,7 @@ export async function getSolautoManagedPositions(
192
192
  positionId: position.positionId[0],
193
193
  lendingPlatform: position.position.lendingPlatform,
194
194
  positionType: position.positionType,
195
- lpUserAccount: toWeb3JsPublicKey(position.position.protocolUserAccount),
195
+ lpUserAccount: toWeb3JsPublicKey(position.position.lpUserAccount),
196
196
  supplyMint: tokens![0],
197
197
  debtMint: tokens![1],
198
198
  };
@@ -36,7 +36,7 @@ function assertAccurateRebalance(
36
36
  SolautoFeesBps.create(
37
37
  false,
38
38
  targetLiqUtilizationRateBps,
39
- client.pos.netWorthUsd
39
+ client.pos.netWorthUsd()
40
40
  ),
41
41
  50
42
42
  );
@@ -99,9 +99,9 @@ async function getFakePosition(
99
99
  state: fakeState,
100
100
  position: {
101
101
  lendingPlatform: LendingPlatform.Marginfi,
102
- protocolUserAccount: publicKey(PublicKey.default),
103
- protocolSupplyAccount: publicKey(PublicKey.default),
104
- protocolDebtAccount: publicKey(PublicKey.default),
102
+ lpUserAccount: publicKey(PublicKey.default),
103
+ lpSupplyAccount: publicKey(PublicKey.default),
104
+ lpDebtAccount: publicKey(PublicKey.default),
105
105
  settings,
106
106
  dca: null,
107
107
  padding: [],