@symmetry-hq/sdk 1.0.1
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/src/constants.d.ts +23 -0
- package/dist/src/constants.js +38 -0
- package/dist/src/index.d.ts +804 -0
- package/dist/src/index.js +2097 -0
- package/dist/src/instructions/automation/auction.d.ts +6 -0
- package/dist/src/instructions/automation/auction.js +40 -0
- package/dist/src/instructions/automation/claimBounty.d.ts +12 -0
- package/dist/src/instructions/automation/claimBounty.js +44 -0
- package/dist/src/instructions/automation/flashSwap.d.ts +21 -0
- package/dist/src/instructions/automation/flashSwap.js +74 -0
- package/dist/src/instructions/automation/priceUpdate.d.ts +19 -0
- package/dist/src/instructions/automation/priceUpdate.js +89 -0
- package/dist/src/instructions/automation/rebalanceIntent.d.ts +32 -0
- package/dist/src/instructions/automation/rebalanceIntent.js +117 -0
- package/dist/src/instructions/automation/rebalanceSwap.d.ts +11 -0
- package/dist/src/instructions/automation/rebalanceSwap.js +42 -0
- package/dist/src/instructions/management/addBounty.d.ts +7 -0
- package/dist/src/instructions/management/addBounty.js +41 -0
- package/dist/src/instructions/management/admin.d.ts +9 -0
- package/dist/src/instructions/management/admin.js +53 -0
- package/dist/src/instructions/management/claimFees.d.ts +15 -0
- package/dist/src/instructions/management/claimFees.js +95 -0
- package/dist/src/instructions/management/createBasket.d.ts +21 -0
- package/dist/src/instructions/management/createBasket.js +98 -0
- package/dist/src/instructions/management/edit.d.ts +51 -0
- package/dist/src/instructions/management/edit.js +477 -0
- package/dist/src/instructions/management/luts.d.ts +30 -0
- package/dist/src/instructions/management/luts.js +99 -0
- package/dist/src/instructions/pda.d.ts +25 -0
- package/dist/src/instructions/pda.js +128 -0
- package/dist/src/instructions/user/deposit.d.ts +20 -0
- package/dist/src/instructions/user/deposit.js +100 -0
- package/dist/src/instructions/user/withdraw.d.ts +8 -0
- package/dist/src/instructions/user/withdraw.js +36 -0
- package/dist/src/jup.d.ts +49 -0
- package/dist/src/jup.js +80 -0
- package/dist/src/keeperMonitor.d.ts +52 -0
- package/dist/src/keeperMonitor.js +624 -0
- package/dist/src/layouts/basket.d.ts +191 -0
- package/dist/src/layouts/basket.js +51 -0
- package/dist/src/layouts/config.d.ts +281 -0
- package/dist/src/layouts/config.js +237 -0
- package/dist/src/layouts/fraction.d.ts +20 -0
- package/dist/src/layouts/fraction.js +164 -0
- package/dist/src/layouts/intents/bounty.d.ts +18 -0
- package/dist/src/layouts/intents/bounty.js +19 -0
- package/dist/src/layouts/intents/intent.d.ts +209 -0
- package/dist/src/layouts/intents/intent.js +97 -0
- package/dist/src/layouts/intents/rebalanceIntent.d.ts +212 -0
- package/dist/src/layouts/intents/rebalanceIntent.js +94 -0
- package/dist/src/layouts/lookupTable.d.ts +7 -0
- package/dist/src/layouts/lookupTable.js +10 -0
- package/dist/src/layouts/oracle.d.ts +63 -0
- package/dist/src/layouts/oracle.js +96 -0
- package/dist/src/states/basket.d.ts +14 -0
- package/dist/src/states/basket.js +479 -0
- package/dist/src/states/config.d.ts +3 -0
- package/dist/src/states/config.js +71 -0
- package/dist/src/states/intents/intent.d.ts +10 -0
- package/dist/src/states/intents/intent.js +316 -0
- package/dist/src/states/intents/rebalanceIntent.d.ts +42 -0
- package/dist/src/states/intents/rebalanceIntent.js +680 -0
- package/dist/src/states/oracles/constants.d.ts +9 -0
- package/dist/src/states/oracles/constants.js +15 -0
- package/dist/src/states/oracles/oracle.d.ts +24 -0
- package/dist/src/states/oracles/oracle.js +168 -0
- package/dist/src/states/oracles/pythOracle.d.ts +132 -0
- package/dist/src/states/oracles/pythOracle.js +609 -0
- package/dist/src/states/oracles/raydiumClmmOracle.d.ts +184 -0
- package/dist/src/states/oracles/raydiumClmmOracle.js +843 -0
- package/dist/src/states/oracles/raydiumCpmmOracle.d.ts +120 -0
- package/dist/src/states/oracles/raydiumCpmmOracle.js +540 -0
- package/dist/src/states/oracles/switchboardOracle.d.ts +0 -0
- package/dist/src/states/oracles/switchboardOracle.js +1 -0
- package/dist/src/states/withdrawBasketFees.d.ts +10 -0
- package/dist/src/states/withdrawBasketFees.js +154 -0
- package/dist/src/txUtils.d.ts +65 -0
- package/dist/src/txUtils.js +306 -0
- package/dist/test.d.ts +1 -0
- package/dist/test.js +561 -0
- package/package.json +31 -0
- package/src/constants.ts +40 -0
- package/src/index.ts +2431 -0
- package/src/instructions/automation/auction.ts +55 -0
- package/src/instructions/automation/claimBounty.ts +69 -0
- package/src/instructions/automation/flashSwap.ts +104 -0
- package/src/instructions/automation/priceUpdate.ts +117 -0
- package/src/instructions/automation/rebalanceIntent.ts +181 -0
- package/src/instructions/management/addBounty.ts +55 -0
- package/src/instructions/management/admin.ts +72 -0
- package/src/instructions/management/claimFees.ts +129 -0
- package/src/instructions/management/createBasket.ts +138 -0
- package/src/instructions/management/edit.ts +602 -0
- package/src/instructions/management/luts.ts +157 -0
- package/src/instructions/pda.ts +151 -0
- package/src/instructions/user/deposit.ts +143 -0
- package/src/instructions/user/withdraw.ts +53 -0
- package/src/jup.ts +113 -0
- package/src/keeperMonitor.ts +585 -0
- package/src/layouts/basket.ts +233 -0
- package/src/layouts/config.ts +576 -0
- package/src/layouts/fraction.ts +164 -0
- package/src/layouts/intents/bounty.ts +35 -0
- package/src/layouts/intents/intent.ts +324 -0
- package/src/layouts/intents/rebalanceIntent.ts +306 -0
- package/src/layouts/lookupTable.ts +14 -0
- package/src/layouts/oracle.ts +157 -0
- package/src/states/basket.ts +527 -0
- package/src/states/config.ts +62 -0
- package/src/states/intents/intent.ts +311 -0
- package/src/states/intents/rebalanceIntent.ts +751 -0
- package/src/states/oracles/constants.ts +13 -0
- package/src/states/oracles/oracle.ts +212 -0
- package/src/states/oracles/pythOracle.ts +874 -0
- package/src/states/oracles/raydiumClmmOracle.ts +1193 -0
- package/src/states/oracles/raydiumCpmmOracle.ts +784 -0
- package/src/states/oracles/switchboardOracle.ts +0 -0
- package/src/states/withdrawBasketFees.ts +160 -0
- package/src/txUtils.ts +424 -0
- package/test.ts +609 -0
- package/tsconfig.json +101 -0
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
import BN from "bn.js";
|
|
2
|
+
import { PublicKey } from "@solana/web3.js";
|
|
3
|
+
import { array, publicKey, str, struct, u128, u16, u64, u8 } from '@coral-xyz/borsh';
|
|
4
|
+
import { Fraction, FractionLayout } from "../fraction";
|
|
5
|
+
import { Bounty, BountyLayout } from "./bounty";
|
|
6
|
+
import { OraclePriceLayout, OraclePriceOnChain } from "../oracle";
|
|
7
|
+
import { MAX_SUPPORTED_TOKENS_PER_BASKET } from '../../constants';
|
|
8
|
+
import { FeeSettings, FeeSettingsLayout } from "../config";
|
|
9
|
+
import { FormattedFeeSettings } from "../basket";
|
|
10
|
+
import { FormattedBountySchedule } from "./intent";
|
|
11
|
+
|
|
12
|
+
export enum RebalanceType {
|
|
13
|
+
Deposit,
|
|
14
|
+
Withdraw,
|
|
15
|
+
Basket,
|
|
16
|
+
BasketCustom,
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export enum RebalanceAction {
|
|
20
|
+
NotActive,
|
|
21
|
+
DepositTokens,
|
|
22
|
+
PlaceholderAction,
|
|
23
|
+
UpdatePrices,
|
|
24
|
+
Auction
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export interface AuctionTimestamps {
|
|
28
|
+
startTime: BN; // u64
|
|
29
|
+
endTime: BN; // u64
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export const AuctionTimestampsLayout = struct<AuctionTimestamps>([
|
|
33
|
+
u64('startTime'),
|
|
34
|
+
u64('endTime'),
|
|
35
|
+
]);
|
|
36
|
+
|
|
37
|
+
export interface TokenAuction {
|
|
38
|
+
mint: PublicKey;
|
|
39
|
+
amount: BN; // u64
|
|
40
|
+
targetAmount: BN; // u64
|
|
41
|
+
price: OraclePriceOnChain;
|
|
42
|
+
keepToken: number; // u8
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export const TokenAuctionLayout = struct<TokenAuction>([
|
|
46
|
+
publicKey('mint'),
|
|
47
|
+
u64('amount'),
|
|
48
|
+
u64('targetAmount'),
|
|
49
|
+
OraclePriceLayout.replicate('price'),
|
|
50
|
+
u8('keepToken'),
|
|
51
|
+
]);
|
|
52
|
+
|
|
53
|
+
export interface TaskCompletion {
|
|
54
|
+
completedBy: PublicKey; //Pubkey
|
|
55
|
+
completedBounty: BN; //u64
|
|
56
|
+
completedTime: BN; //u64
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export const TaskCompletionLayout = struct<TaskCompletion>([
|
|
60
|
+
publicKey('completedBy'),
|
|
61
|
+
u64('completedBounty'),
|
|
62
|
+
u64('completedTime'),
|
|
63
|
+
]);
|
|
64
|
+
|
|
65
|
+
export type FormattedRebalanceType = "deposit" | "withdraw" | "basket" | "basket_custom";
|
|
66
|
+
export const REBALANCE_TYPE_STRINGS: Map<number, FormattedRebalanceType> = new Map([
|
|
67
|
+
[0, "deposit"],
|
|
68
|
+
[1, "withdraw"],
|
|
69
|
+
[2, "basket"],
|
|
70
|
+
[3, "basket_custom"],
|
|
71
|
+
]);
|
|
72
|
+
|
|
73
|
+
export type FormattedRebalanceAction = "not_active" | "deposit_tokens" | "placeholder_action" | "update_prices" | "auction";
|
|
74
|
+
export const REBALANCE_ACTION_STRINGS: Map<number, FormattedRebalanceAction> = new Map([
|
|
75
|
+
[0, "not_active"],
|
|
76
|
+
[1, "deposit_tokens"],
|
|
77
|
+
[2, "placeholder_action"],
|
|
78
|
+
[3, "update_prices"],
|
|
79
|
+
[4, "auction"],
|
|
80
|
+
]);
|
|
81
|
+
|
|
82
|
+
export interface FormattedOraclePrice {
|
|
83
|
+
price: number;
|
|
84
|
+
conf: number;
|
|
85
|
+
update_time: number;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export interface FormattedTokenAuction {
|
|
89
|
+
mint: string;
|
|
90
|
+
amount: number;
|
|
91
|
+
target_amount: number;
|
|
92
|
+
price: FormattedOraclePrice;
|
|
93
|
+
keep_token: boolean;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export interface FormattedTaskCompletion {
|
|
97
|
+
completed_by: string;
|
|
98
|
+
completed_bounty: number;
|
|
99
|
+
completed_time: number;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export interface DepositData {
|
|
103
|
+
tokens: {
|
|
104
|
+
mint: string;
|
|
105
|
+
amount: number;
|
|
106
|
+
}[];
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
export interface PriceUpdatesData {
|
|
110
|
+
tokens: {
|
|
111
|
+
mint: string;
|
|
112
|
+
updated: boolean;
|
|
113
|
+
expiration: number;
|
|
114
|
+
update_time: number;
|
|
115
|
+
price: number;
|
|
116
|
+
conf: number;
|
|
117
|
+
}[];
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
export interface AuctionData {
|
|
121
|
+
auction_stages: {
|
|
122
|
+
start_time: number;
|
|
123
|
+
end_time: number;
|
|
124
|
+
}[];
|
|
125
|
+
tokens: {
|
|
126
|
+
mint: string;
|
|
127
|
+
amount: number;
|
|
128
|
+
target_amount: number;
|
|
129
|
+
price: number;
|
|
130
|
+
conf: number;
|
|
131
|
+
}[];
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
export interface MintData {
|
|
135
|
+
mintAmount: number;
|
|
136
|
+
mintValue: number;
|
|
137
|
+
fees: {
|
|
138
|
+
host: number;
|
|
139
|
+
creator: number;
|
|
140
|
+
managers: number;
|
|
141
|
+
symmetry: number;
|
|
142
|
+
basket: number;
|
|
143
|
+
};
|
|
144
|
+
tokens: {
|
|
145
|
+
mint: string;
|
|
146
|
+
contribution_amount: number;
|
|
147
|
+
remaining_amount: number;
|
|
148
|
+
}[];
|
|
149
|
+
}[];
|
|
150
|
+
|
|
151
|
+
export interface RedeemData {
|
|
152
|
+
tokens: {
|
|
153
|
+
mint: string;
|
|
154
|
+
amount: number;
|
|
155
|
+
}[];
|
|
156
|
+
}[];
|
|
157
|
+
|
|
158
|
+
export interface ClaimBountyData {
|
|
159
|
+
bounty_mint: string;
|
|
160
|
+
keepers: {
|
|
161
|
+
pubkey: string;
|
|
162
|
+
bounty_amount: number;
|
|
163
|
+
}[];
|
|
164
|
+
unused_bounty: {
|
|
165
|
+
pubkey: string;
|
|
166
|
+
bounty_amount: number;
|
|
167
|
+
};
|
|
168
|
+
rent: {
|
|
169
|
+
pubkey: string;
|
|
170
|
+
sol_amount: number;
|
|
171
|
+
}
|
|
172
|
+
}[];
|
|
173
|
+
|
|
174
|
+
export interface UIRebalanceIntent {
|
|
175
|
+
rebalance_type: FormattedRebalanceType;
|
|
176
|
+
|
|
177
|
+
deposit_data: DepositData | null;
|
|
178
|
+
price_updates_data: PriceUpdatesData | null;
|
|
179
|
+
auction_data: AuctionData | null;
|
|
180
|
+
mint_data: MintData | null;
|
|
181
|
+
redeem_data: RedeemData | null;
|
|
182
|
+
claim_bounty_data: ClaimBountyData | null;
|
|
183
|
+
|
|
184
|
+
formatted_data: FormattedRebalanceIntent;
|
|
185
|
+
chain_data: RebalanceIntent;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export interface FormattedRebalanceIntent {
|
|
189
|
+
pubkey: string;
|
|
190
|
+
sol_balance: number;
|
|
191
|
+
basket: string;
|
|
192
|
+
owner: string;
|
|
193
|
+
rent_payer: string;
|
|
194
|
+
rebalance_type: FormattedRebalanceType;
|
|
195
|
+
current_action: FormattedRebalanceAction;
|
|
196
|
+
withdraw_params_burn_amount: number;
|
|
197
|
+
withdraw_params_amount_wo_fees: number;
|
|
198
|
+
withdraw_params_keep_tokens_bitmask: number[];
|
|
199
|
+
withdraw_params_keep_all_tokens: boolean;
|
|
200
|
+
basket_fee_settings: FormattedFeeSettings;
|
|
201
|
+
execution_start_time: number;
|
|
202
|
+
rebalance_threshold_slippage_bps: number;
|
|
203
|
+
per_trade_rebalance_threshold_slippage_bps: number;
|
|
204
|
+
initial_tvl: number;
|
|
205
|
+
auction_update_timestamp: number;
|
|
206
|
+
auctions: {
|
|
207
|
+
start_time: number;
|
|
208
|
+
end_time: number;
|
|
209
|
+
}[];
|
|
210
|
+
tokens: FormattedTokenAuction[];
|
|
211
|
+
last_action_timestamp: number;
|
|
212
|
+
bounty: {
|
|
213
|
+
bounty_depositor: string;
|
|
214
|
+
bounty_mint: string;
|
|
215
|
+
bounty_per_price_update_task: FormattedBountySchedule;
|
|
216
|
+
bounty_per_task: FormattedBountySchedule;
|
|
217
|
+
bounty_total: number;
|
|
218
|
+
bounty_left: number;
|
|
219
|
+
};
|
|
220
|
+
bounty_adjustment_amount: number;
|
|
221
|
+
placeholder_task: FormattedTaskCompletion;
|
|
222
|
+
price_update_tasks: FormattedTaskCompletion[];
|
|
223
|
+
finish_price_update_task: FormattedTaskCompletion;
|
|
224
|
+
auction_creation_task: FormattedTaskCompletion;
|
|
225
|
+
mint_basket_task: FormattedTaskCompletion;
|
|
226
|
+
cancel_rebalance_task: FormattedTaskCompletion;
|
|
227
|
+
token_settlement_tasks: FormattedTaskCompletion[];
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
export interface RebalanceIntent {
|
|
231
|
+
basket: PublicKey;
|
|
232
|
+
owner: PublicKey;
|
|
233
|
+
rentPayer: PublicKey;
|
|
234
|
+
rebalanceType: RebalanceType;
|
|
235
|
+
currentAction: RebalanceAction;
|
|
236
|
+
|
|
237
|
+
withdrawParamsBurnAmount: BN,
|
|
238
|
+
withdrawParamsAmountWoFees: BN,
|
|
239
|
+
withdrawParamsKeepTokensBitmask: number[],
|
|
240
|
+
withdrawParamsKeepAllTokens: u8,
|
|
241
|
+
|
|
242
|
+
basketFeeSettings: FeeSettings;
|
|
243
|
+
executionStartTime: BN; // u64
|
|
244
|
+
rebalanceThresholdSlippageBps: number; // u16
|
|
245
|
+
perTradeRebalanceThresholdSlippageBps: number; // u16
|
|
246
|
+
initialTvl: Fraction; //
|
|
247
|
+
auctionUpdateTimestamp: BN; // u64
|
|
248
|
+
auctions: AuctionTimestamps[]; // AuctionTimestamps[]
|
|
249
|
+
|
|
250
|
+
tokens: TokenAuction[]; // MAX_SUPPORTED_TOKENS_PER_BASKET
|
|
251
|
+
|
|
252
|
+
lastActionTimestamp: BN; // u64
|
|
253
|
+
bounty: Bounty; // u64
|
|
254
|
+
bountyAdjustmentAmount: BN; // u64
|
|
255
|
+
|
|
256
|
+
placeholderTask: TaskCompletion;
|
|
257
|
+
priceUpdateTasks: TaskCompletion[]; // MAX_SUPPORTED_TOKENS_PER_BASKET
|
|
258
|
+
finishPriceUpdateTask: TaskCompletion;
|
|
259
|
+
|
|
260
|
+
auctionCreationTask: TaskCompletion;
|
|
261
|
+
mintBasketTask: TaskCompletion;
|
|
262
|
+
cancelRebalanceTask: TaskCompletion;
|
|
263
|
+
|
|
264
|
+
tokenSettlementTasks: TaskCompletion[]; // MAX_SUPPORTED_TOKENS_PER_BASKET
|
|
265
|
+
|
|
266
|
+
ownAddress?: PublicKey;
|
|
267
|
+
solBalance?: number;
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
export const RebalanceIntentLayout = struct<RebalanceIntent>([
|
|
272
|
+
publicKey('basket'),
|
|
273
|
+
publicKey('owner'),
|
|
274
|
+
publicKey('rentPayer'),
|
|
275
|
+
u8('rebalanceType'),
|
|
276
|
+
u8('currentAction'),
|
|
277
|
+
|
|
278
|
+
u64('withdrawParamsBurnAmount'),
|
|
279
|
+
u64('withdrawParamsAmountWoFees'),
|
|
280
|
+
array(u8(), 16, 'withdrawParamsKeepTokensBitmask'),
|
|
281
|
+
u8('withdrawParamsKeepAllTokens'),
|
|
282
|
+
|
|
283
|
+
FeeSettingsLayout.replicate('basketFeeSettings'),
|
|
284
|
+
u64('executionStartTime'),
|
|
285
|
+
u16('rebalanceThresholdSlippageBps'),
|
|
286
|
+
u16('perTradeRebalanceThresholdSlippageBps'),
|
|
287
|
+
FractionLayout.replicate('initialTvl'),
|
|
288
|
+
u64('auctionUpdateTimestamp'),
|
|
289
|
+
array(AuctionTimestampsLayout, 3, 'auctions'),
|
|
290
|
+
|
|
291
|
+
array(TokenAuctionLayout, MAX_SUPPORTED_TOKENS_PER_BASKET, 'tokens'),
|
|
292
|
+
|
|
293
|
+
u64('lastActionTimestamp'),
|
|
294
|
+
BountyLayout.replicate('bounty'),
|
|
295
|
+
u64('bountyAdjustmentAmount'),
|
|
296
|
+
|
|
297
|
+
TaskCompletionLayout.replicate('placeholderTask'),
|
|
298
|
+
array(TaskCompletionLayout, MAX_SUPPORTED_TOKENS_PER_BASKET, 'priceUpdateTasks'),
|
|
299
|
+
TaskCompletionLayout.replicate('finishPriceUpdateTask'),
|
|
300
|
+
|
|
301
|
+
TaskCompletionLayout.replicate('auctionCreationTask'),
|
|
302
|
+
TaskCompletionLayout.replicate('mintBasketTask'),
|
|
303
|
+
TaskCompletionLayout.replicate('cancelRebalanceTask'),
|
|
304
|
+
|
|
305
|
+
array(TaskCompletionLayout, MAX_SUPPORTED_TOKENS_PER_BASKET, 'tokenSettlementTasks'),
|
|
306
|
+
]);
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { publicKey, struct } from '@coral-xyz/borsh';
|
|
2
|
+
import { PublicKey } from '@solana/web3.js';
|
|
3
|
+
|
|
4
|
+
export interface LookupTableInfo {
|
|
5
|
+
lookupTable: PublicKey,
|
|
6
|
+
authority: PublicKey,
|
|
7
|
+
creator: PublicKey
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export const LookupTableInfoLayout = struct<LookupTableInfo>([
|
|
11
|
+
publicKey("lookupTable"),
|
|
12
|
+
publicKey("authority"),
|
|
13
|
+
publicKey("creator")
|
|
14
|
+
]);
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import BN from 'bn.js';
|
|
2
|
+
import { array, struct, u16, u64, u8 } from '@coral-xyz/borsh';
|
|
3
|
+
|
|
4
|
+
import { Fraction, FractionLayout } from './fraction';
|
|
5
|
+
import { HUNDRED_PERCENT_BPS, USDC_DECIMALS, WSOL_DECIMALS } from '../constants';
|
|
6
|
+
|
|
7
|
+
export const MAX_ORACLES_PER_TOKEN = 4;
|
|
8
|
+
export const MAX_ACCOUNTS_PER_ORACLE = 4;
|
|
9
|
+
|
|
10
|
+
export type FormattedOracleType = "pyth" | "raydium_clmm" | "raydium_cpmm" | "switchboard" | "example";
|
|
11
|
+
export const ORACLE_TYPES_STRINGS: Map<number, FormattedOracleType> = new Map([
|
|
12
|
+
[0, "pyth"],
|
|
13
|
+
[1, "raydium_clmm"],
|
|
14
|
+
[2, "raydium_cpmm"],
|
|
15
|
+
[3, "switchboard"],
|
|
16
|
+
[255, "example"],
|
|
17
|
+
]);
|
|
18
|
+
|
|
19
|
+
export interface OraclePriceOnChain {
|
|
20
|
+
price: Fraction;
|
|
21
|
+
conf: Fraction;
|
|
22
|
+
updateTime: BN; // u64
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export const OraclePriceLayout = struct<OraclePriceOnChain>([
|
|
26
|
+
FractionLayout.replicate('price'),
|
|
27
|
+
FractionLayout.replicate('conf'),
|
|
28
|
+
u64('updateTime'),
|
|
29
|
+
]);
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
export enum OracleType {
|
|
33
|
+
Pyth = 0,
|
|
34
|
+
RaydiumClmm = 1,
|
|
35
|
+
RaydiumCpmm = 2,
|
|
36
|
+
SwitchBoard = 3,
|
|
37
|
+
Example = 4,
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export enum Quote {
|
|
41
|
+
Usdc = 0,
|
|
42
|
+
Wsol = 1,
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export enum Side {
|
|
46
|
+
Base = 0,
|
|
47
|
+
Quote = 1,
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export interface OracleSettings {
|
|
51
|
+
// type of the oracle (Pyth, CP swap, ...)
|
|
52
|
+
oracleType: OracleType; // u8
|
|
53
|
+
numRequiredAccounts: number; // u8
|
|
54
|
+
// weight used in aggregation
|
|
55
|
+
weight: number; // u16
|
|
56
|
+
// whether oracle is required for aggregation
|
|
57
|
+
isRequired: number; // u8
|
|
58
|
+
// threshold for confidence ratio (conf / price < threshold)
|
|
59
|
+
confThreshBps: number; // u16
|
|
60
|
+
// how much internal price derivations can differ (≥ 1.0)
|
|
61
|
+
volatilityThreshBps: number; // u16
|
|
62
|
+
// max slippage allowed for min liquidity
|
|
63
|
+
maxSlippageBps: number; // u16
|
|
64
|
+
// minimum liquidity (in quote tokens) for valid price
|
|
65
|
+
minLiquidity: BN; //u64
|
|
66
|
+
// how old a price can be before it's invalid
|
|
67
|
+
stalenessThresh: BN; // u64
|
|
68
|
+
// rate (bps) that inflates confidence with time staleness
|
|
69
|
+
stalenessConfRateBps: number; // u16
|
|
70
|
+
tokenDecimals: number; // u8
|
|
71
|
+
// Special parameters specific to Raydium
|
|
72
|
+
twapSecondsAgo: BN; // u64, primary twap window in seconds
|
|
73
|
+
twapSecondarySecondsAgo: BN; // u64, secondary twap window - to check volatility
|
|
74
|
+
quote: Quote; // u8, 1 = USDC, 2 = WSOL
|
|
75
|
+
side: Side; // 0 = base, 1 = quote
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export const OracleSettingsLayout = struct<OracleSettings>([
|
|
79
|
+
u8('oracleType'),
|
|
80
|
+
u8('numRequiredAccounts'),
|
|
81
|
+
u16('weight'),
|
|
82
|
+
u8('isRequired'),
|
|
83
|
+
u16('confThreshBps'),
|
|
84
|
+
u16('volatilityThreshBps'),
|
|
85
|
+
u16('maxSlippageBps'),
|
|
86
|
+
u64('minLiquidity'),
|
|
87
|
+
u64('stalenessThresh'),
|
|
88
|
+
u16('stalenessConfRateBps'),
|
|
89
|
+
u8('tokenDecimals'),
|
|
90
|
+
u64('twapSecondsAgo'),
|
|
91
|
+
u64('twapSecondarySecondsAgo'),
|
|
92
|
+
u8('quote'),
|
|
93
|
+
u8('side'),
|
|
94
|
+
]);
|
|
95
|
+
|
|
96
|
+
export interface OracleData {
|
|
97
|
+
oracleSettings: OracleSettings;
|
|
98
|
+
accountsToLoadLutIds: number[]; // length MAX_ACCOUNTS_PER_ORACLE
|
|
99
|
+
accountsToLoadLutIndices: number[]; // length MAX_ACCOUNTS_PER_ORACLE
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
export const OracleDataLayout = struct<OracleData>([
|
|
103
|
+
OracleSettingsLayout.replicate('oracleSettings'),
|
|
104
|
+
array(u8(), MAX_ACCOUNTS_PER_ORACLE, 'accountsToLoadLutIds'),
|
|
105
|
+
array(u8(), MAX_ACCOUNTS_PER_ORACLE, 'accountsToLoadLutIndices'),
|
|
106
|
+
]);
|
|
107
|
+
|
|
108
|
+
export interface OracleAggregator {
|
|
109
|
+
numOracles: number; // u8
|
|
110
|
+
// minimum number of oracle results required for valid price
|
|
111
|
+
minOraclesThresh: number; // u8
|
|
112
|
+
oracles: OracleData[]; // [OracleData; MAX_ORACLES_PER_TOKEN]
|
|
113
|
+
// minimum confidence ratio allowed (conf / price)
|
|
114
|
+
minConfBps: number; // u16
|
|
115
|
+
// maximum confidence ratio allowed (conf / price)
|
|
116
|
+
confThreshBps: number; // u16
|
|
117
|
+
// factor to expand confidence bands (≥ 1.0)
|
|
118
|
+
confMultiplier: Fraction;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export const OracleAggregatorLayout = struct<OracleAggregator>([
|
|
122
|
+
u8('numOracles'),
|
|
123
|
+
u8('minOraclesThresh'),
|
|
124
|
+
array(OracleDataLayout, MAX_ORACLES_PER_TOKEN, 'oracles'),
|
|
125
|
+
u16('minConfBps'),
|
|
126
|
+
u16('confThreshBps'),
|
|
127
|
+
FractionLayout.replicate('confMultiplier')
|
|
128
|
+
]);
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
export const DEFAULT_ORACLE_SETTINGS: OracleSettings = {
|
|
132
|
+
oracleType: OracleType.Pyth,
|
|
133
|
+
numRequiredAccounts: 1,
|
|
134
|
+
weight: HUNDRED_PERCENT_BPS,
|
|
135
|
+
isRequired: 1,
|
|
136
|
+
confThreshBps: 100, // TODO: 1% ??
|
|
137
|
+
volatilityThreshBps: 50000,
|
|
138
|
+
maxSlippageBps: 1000,
|
|
139
|
+
minLiquidity: new BN(0),
|
|
140
|
+
stalenessThresh: new BN(120),
|
|
141
|
+
stalenessConfRateBps: 50,
|
|
142
|
+
tokenDecimals: WSOL_DECIMALS,
|
|
143
|
+
twapSecondsAgo:new BN(0),
|
|
144
|
+
twapSecondarySecondsAgo: new BN(0),
|
|
145
|
+
quote: Quote.Wsol,
|
|
146
|
+
side: Side.Base,
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
export const PYTH_WSOL_ORACLE_SETTINGS: OracleSettings = {
|
|
150
|
+
...DEFAULT_ORACLE_SETTINGS,
|
|
151
|
+
tokenDecimals: WSOL_DECIMALS,
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export const PYTH_USDC_ORACLE_SETTINGS: OracleSettings = {
|
|
155
|
+
...DEFAULT_ORACLE_SETTINGS,
|
|
156
|
+
tokenDecimals: USDC_DECIMALS,
|
|
157
|
+
}
|