-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathoracle.h
482 lines (394 loc) · 14.5 KB
/
oracle.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#pragma once
#include <stdbool.h>
#include "util/compat_stdint.h"
#include "features.h"
#ifdef __cplusplus
extern "C" {
#endif
// magic number at head of account
#define PC_MAGIC 0xa1b2c3d4
// current program version
#define PC_VERSION 2
// max latency in slots between send and receive
#define PC_MAX_SEND_LATENCY 25
// various size constants
#define PC_PUBKEY_SIZE 32
#define PC_PUBKEY_SIZE_64 (PC_PUBKEY_SIZE/sizeof(uint64_t))
#define PC_MAP_TABLE_SIZE 640
// Total price component slots available
#define PC_NUM_COMP_PYTHNET 128
// PC_NUM_COMP - number of price components in use
// Not whole PC_NUM_COMP_PYTHNET because of stack issues appearing in upd_aggregate()
#define PC_NUM_COMP 64
#define PC_PROD_ACC_SIZE 512
#define PC_EXP_DECAY -9
#ifndef PC_HEAP_START
#define PC_HEAP_START (0x300000000)
#endif
// price types
#define PC_PTYPE_UNKNOWN 0
#define PC_PTYPE_PRICE 1
// symbol status
#define PC_STATUS_UNKNOWN 0
#define PC_STATUS_TRADING 1
#define PC_STATUS_HALTED 2
#define PC_STATUS_AUCTION 3
#define PC_STATUS_IGNORED 4
// account types
#define PC_ACCTYPE_MAPPING 1
#define PC_ACCTYPE_PRODUCT 2
#define PC_ACCTYPE_PRICE 3
#define PC_ACCTYPE_TEST 4
#define PC_ACCTYPE_PERMISSIONS 5
// Compute budget requested per price update instruction
// The biggest instruction appears to be about ~10300 CUs, so overestimate by 100%.
#define CU_BUDGET_PER_IX 20000
// binary version of sysvar_clock account id
const uint64_t sysvar_clock[] = {
0xc974c71817d5a706UL,
0xb65e1d6998635628UL,
0x5c6d4b9ba3b85e8bUL,
0x215b5573UL
};
// compute budget program id in hex (but wrong endianness)
/*
321721e56f460603
e79bc372baadecff
6b12f7c5bbe58cbc
403a9b432c
*/
// correct
const uint64_t compute_budget_program_id[] = {
0x321721e56f460603UL,
0xe79bc372baadecffUL,
0x6b12f7c5bbe58cbcUL,
0x403a9b432cUL
};
// public key of symbol or publisher account
typedef union pc_pub_key
{
uint8_t k1_[PC_PUBKEY_SIZE];
uint64_t k8_[PC_PUBKEY_SIZE_64];
} pc_pub_key_t;
static_assert( sizeof( pc_pub_key_t ) == 32, "" );
// account header information
typedef struct pc_acc
{
uint32_t magic_; // pyth magic number
uint32_t ver_; // program/account version
uint32_t type_; // account type
uint32_t size_; // size of populated region of account
} pc_acc_t;
static_assert( sizeof( pc_acc_t ) == 16, "" );
// hash table of symbol to price account mappings
typedef struct pc_map_table
{
uint32_t magic_; // pyth magic number
uint32_t ver_; // program/account version
uint32_t type_; // account type
uint32_t size_; // size of populated region of account
uint32_t num_; // number of symbols
uint32_t unused_; // 64bit padding
pc_pub_key_t next_; // next mapping account in chain
pc_pub_key_t prod_[PC_MAP_TABLE_SIZE]; // product accounts
} pc_map_table_t;
static_assert( sizeof( pc_map_table_t ) == 20536, "" );
// variable length string
typedef struct pc_str
{
uint8_t len_;
char data_[];
} pc_str_t;
static_assert( sizeof( pc_str_t ) == 1, "" );
// product reference data
typedef struct pc_prod
{
uint32_t magic_;
uint32_t ver_; // program version
uint32_t type_; // account type
uint32_t size_; // size of populated region of account
pc_pub_key_t px_acc_; // first price (or quote) account
// variable number of reference key/value attribute pairs
// stored as strings (pc_str_t)
} pc_prod_t;
static_assert( sizeof( pc_prod_t ) == 48, "" );
// price et al. for some component or aggregate
typedef struct pc_price_info
{
int64_t price_; // price per ptype_
uint64_t conf_; // price confidence interval
uint32_t status_; // symbol status as of last update
uint32_t corp_act_status_; // corp action status as of last update
uint64_t pub_slot_; // publish slot of price
} pc_price_info_t;
static_assert( sizeof( pc_price_info_t ) == 32, "" );
// published component price for contributing provider
typedef struct pc_price_comp
{
pc_pub_key_t pub_; // publishing key of component price
pc_price_info_t agg_; // price used in aggregate calc
pc_price_info_t latest_; // latest contributed prices
} pc_price_comp_t;
static_assert( sizeof( pc_price_comp_t ) == 96, "" );
// time-weighted exponential moving average
typedef struct pc_ema
{
int64_t val_; // current value of ema
int64_t numer_; // numerator at full precision
int64_t denom_; // denominator at full precision
} pc_ema_t;
static_assert( sizeof( pc_ema_t ) == 24, "" );
// price account containing aggregate and all component prices
typedef struct pc_price
{
uint32_t magic_; // pyth magic number
uint32_t ver_; // program version
uint32_t type_; // account type
uint32_t size_; // price account size
uint32_t ptype_; // price or calculation type
int32_t expo_; // price exponent
uint32_t num_; // number of component prices
uint32_t num_qt_; // number of quoters that make up aggregate
uint64_t last_slot_; // slot of last valid aggregate price
uint64_t valid_slot_; // valid on-chain slot of agg. price
pc_ema_t twap_; // time-weighted average price
pc_ema_t twac_; // time-weighted average conf interval
int64_t timestamp_; // unix timestamp of aggregate price
uint8_t min_pub_; // min publishers for valid price
int8_t message_sent_; // flag to indicate if the current aggregate price has been sent as a message to the message buffer, 0 if not sent, 1 if sent
uint8_t max_latency_; // configurable max latency in slots between send and receive
int8_t drv3_; // space for future derived values
int32_t drv4_; // space for future derived values
pc_pub_key_t prod_; // product id/ref-account
pc_pub_key_t next_; // next price account in list
uint64_t prev_slot_; // valid slot of previous aggregate with TRADING status
int64_t prev_price_; // aggregate price of previous aggregate with TRADING status
uint64_t prev_conf_; // confidence interval of previous aggregate with TRADING status
int64_t prev_timestamp_; // unix timestamp of previous aggregate with TRADING status
pc_price_info_t agg_; // aggregate price information
pc_price_comp_t comp_[PC_NUM_COMP];// component prices
} pc_price_t;
/*
The value 240 is derived from the fixed size of the pc_price_t struct excluding the size of the comp_ array.
Here is the breakdown of the sizes (in bytes) for each field within the pc_price_t struct:
- magic_ (uint32_t): 4 bytes
- ver_ (uint32_t): 4 bytes
- type_ (uint32_t): 4 bytes
- size_ (uint32_t): 4 bytes
- ptype_ (uint32_t): 4 bytes
- expo_ (int32_t): 4 bytes
- num_ (uint32_t): 4 bytes
- num_qt_ (uint32_t): 4 bytes
- last_slot_ (uint64_t): 8 bytes
- valid_slot_ (uint64_t): 8 bytes
- twap_ (pc_ema_t): 24 bytes (3 fields of int64_t each taking 8 bytes)
- twac_ (pc_ema_t): 24 bytes (similar to twap_)
- timestamp_ (int64_t): 8 bytes
- min_pub_ (uint8_t): 1 byte
- message_sent_ (int8_t): 1 byte
- max_latency_ (uint8_t): 1 byte
- drv3_ (int8_t): 1 byte
- drv4_ (int32_t): 4 bytes
- prod_ (pc_pub_key_t): 32 bytes (assuming pc_pub_key_t is a 32-byte array or struct)
- next_ (pc_pub_key_t): 32 bytes (similar to prod_)
- prev_slot_ (uint64_t): 8 bytes
- prev_price_ (int64_t): 8 bytes
- prev_conf_ (uint64_t): 8 bytes
- prev_timestamp_ (int64_t): 8 bytes
- agg_ (pc_price_info_t): 32 bytes
Adding up all these sizes gives us a total of 240 bytes for the fixed part of the pc_price_t struct.
The size of the comp_ array is variable and depends on PC_NUM_COMP, hence it is calculated separately and added to the base size of 240 bytes.
*/
#define PC_EXPECTED_PRICE_T_SIZE_PYTHNET (240 \
+ PC_NUM_COMP * sizeof(pc_price_comp_t) \
)
static_assert( sizeof( pc_price_t ) == PC_EXPECTED_PRICE_T_SIZE_PYTHNET, "" );
#undef PC_EXPECTED_PRICE_T_SIZE_PYTHNET
// This constant needs to be an upper bound of the price account size, it is used within pythd for ztsd.
// It is set tighly to the current price account + 96 component prices + 48 bytes for cumulative sums
const uint64_t ZSTD_UPPER_BOUND = 3312 + 96 * sizeof( pc_price_comp_t) + 48;
// command enumeration
typedef enum {
// initialize first mapping list account
// key[0] funding account [signer writable]
// key[1] mapping account [signer writable]
e_cmd_init_mapping = 0,
// initialize and add new mapping account
// key[0] funding account [signer writable]
// key[1] tail mapping account [signer writable]
// key[2] new mapping account [signer writable]
e_cmd_add_mapping,
// initialize and add new product reference data account
// key[0] funding account [signer writable]
// key[1] mapping account [signer writable]
// key[2] new product account [signer writable]
e_cmd_add_product,
// update product account
// key[0] funding account [signer writable]
// key[1] product account [signer writable]
e_cmd_upd_product,
// add new price account to a product account
// key[0] funding account [signer writable]
// key[1] product account [signer writable]
// key[2] new price account [signer writable]
e_cmd_add_price,
// add publisher to symbol account
// key[0] funding account [signer writable]
// key[1] price account [signer writable]
e_cmd_add_publisher,
// delete publisher from symbol account
// key[0] funding account [signer writable]
// key[1] price account [signer writable]
e_cmd_del_publisher,
// publish component price
// key[0] funding account [signer writable]
// key[1] price account [writable]
// key[2] sysvar_clock account [readable]
e_cmd_upd_price,
// compute aggregate price
// key[0] funding account [signer writable]
// key[1] price account [writable]
// key[2] sysvar_clock account [readable]
e_cmd_agg_price,
// (re)initialize price account
// key[0] funding account [signer writable]
// key[1] new price account [signer writable]
e_cmd_init_price,
// deprecated
e_cmd_init_test,
// deprecated
e_cmd_upd_test,
// set min publishers
// key[0] funding account [signer writable]
// key[1] price account [signer writable]
e_cmd_set_min_pub,
// publish component price, never returning an error even if the update failed
// key[0] funding account [signer writable]
// key[1] price account [writable]
// key[2] sysvar_clock account [readable]
e_cmd_upd_price_no_fail_on_error,
// resizes a price account so that it fits the Time Machine
// key[0] funding account [signer writable]
// key[1] price account [Signer writable]
// key[2] system program [readable]
e_cmd_resize_price_account,
// deletes a price account
// key[0] funding account [signer writable]
// key[1] product account [signer writable]
// key[2] price account [signer writable]
e_cmd_del_price,
// deletes a product account
// key[0] funding account [signer writable]
// key[1] mapping account [signer writable]
// key[2] product account [signer writable]
e_cmd_del_product,
} command_t;
typedef struct cmd_hdr
{
uint32_t ver_;
int32_t cmd_;
} cmd_hdr_t;
static_assert( sizeof( cmd_hdr_t ) == 8, "" );
typedef struct cmd_add_product
{
uint32_t ver_;
int32_t cmd_;
} cmd_add_product_t;
static_assert( sizeof( cmd_add_product_t ) == 8, "" );
typedef struct cmd_upd_product
{
uint32_t ver_;
int32_t cmd_;
// set of key-value pairs
} cmd_upd_product_t;
static_assert( sizeof( cmd_upd_product_t ) == 8, "" );
typedef struct cmd_add_price
{
uint32_t ver_;
int32_t cmd_;
int32_t expo_;
uint32_t ptype_;
} cmd_add_price_t;
static_assert( sizeof( cmd_add_price_t ) == 16, "" );
typedef struct cmd_init_price
{
uint32_t ver_;
int32_t cmd_;
int32_t expo_;
uint32_t ptype_;
} cmd_init_price_t;
static_assert( sizeof( cmd_init_price_t ) == 16, "" );
typedef struct cmd_set_min_pub
{
uint32_t ver_;
int32_t cmd_;
uint8_t min_pub_;
} cmd_set_min_pub_t;
static_assert( sizeof( cmd_set_min_pub_t ) == 12, "" );
typedef struct cmd_add_publisher
{
uint32_t ver_;
int32_t cmd_;
pc_pub_key_t pub_;
} cmd_add_publisher_t;
static_assert( sizeof( cmd_add_publisher_t ) == 40, "" );
typedef struct cmd_del_publisher
{
uint32_t ver_;
int32_t cmd_;
pc_pub_key_t pub_;
} cmd_del_publisher_t;
static_assert( sizeof( cmd_del_publisher_t ) == 40, "" );
typedef struct cmd_upd_price
{
uint32_t ver_;
int32_t cmd_;
uint32_t status_;
uint32_t unused_;
int64_t price_;
uint64_t conf_;
uint64_t pub_slot_;
} cmd_upd_price_t;
static_assert( sizeof( cmd_upd_price_t ) == 40, "" );
// structure of clock sysvar account
typedef struct sysvar_clock
{
uint64_t slot_;
int64_t epoch_start_timestamp_;
uint64_t epoch_;
uint64_t leader_schedule_epoch_;
int64_t unix_timestamp_;
} sysvar_clock_t;
static_assert( sizeof( sysvar_clock_t ) == 40, "" );
// compare if two pub_keys (accounts) are the same
inline bool pc_pub_key_equal( pc_pub_key_t *p1, pc_pub_key_t *p2 )
{
return p1->k8_[0] == p2->k8_[0] &&
p1->k8_[1] == p2->k8_[1] &&
p1->k8_[2] == p2->k8_[2] &&
p1->k8_[3] == p2->k8_[3];
}
// check for null (zero) public key
inline bool pc_pub_key_is_zero( pc_pub_key_t *p )
{
return p->k8_[0] == 0UL &&
p->k8_[1] == 0UL &&
p->k8_[2] == 0UL &&
p->k8_[3] == 0UL;
}
// set public key to zero
inline void pc_pub_key_set_zero( pc_pub_key_t *p )
{
p->k8_[0] = p->k8_[1] = p->k8_[2] = p->k8_[3] = 0UL;
}
// assign one pub_key from another
inline void pc_pub_key_assign( pc_pub_key_t *tgt, pc_pub_key_t *src )
{
tgt->k8_[0] = src->k8_[0];
tgt->k8_[1] = src->k8_[1];
tgt->k8_[2] = src->k8_[2];
tgt->k8_[3] = src->k8_[3];
}
#ifdef __cplusplus
}
#endif