This repository was archived by the owner on Jan 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpyth-governance-v1.clar
470 lines (437 loc) · 24.5 KB
/
pyth-governance-v1.clar
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
;; Title: pyth-governance
;; Version: v1
;; Check for latest version: https://github.com/hirosystems/stacks-pyth-bridge#latest-version
;; Report an issue: https://github.com/hirosystems/stacks-pyth-bridge/issues
(use-trait pyth-proxy-trait .pyth-traits-v1.proxy-trait)
(use-trait pyth-decoder-trait .pyth-traits-v1.decoder-trait)
(use-trait pyth-storage-trait .pyth-traits-v1.storage-trait)
(use-trait wormhole-core-trait .wormhole-traits-v1.core-trait)
(define-constant PTGM_MAGIC 0x5054474d) ;; 'PTGM': Pyth Governance Message
;; VAA including some commands for administrating Pyth contract
;; The oracle contract address must be upgraded
(define-constant PTGM_UPDATE_PYTH_ORACLE_ADDRESS 0x00)
;; Authorize governance change
(define-constant PTGM_UPDATE_GOVERNANCE_DATA_SOURCE 0x01)
;; Which wormhole emitter is allowed to send price updates
(define-constant PTGM_UPDATE_PRICES_DATA_SOURCES 0x02)
;; Fee is charged when you submit a new price
(define-constant PTGM_UPDATE_FEE 0x03)
;; Stale price threshold
(define-constant PTGM_STALE_PRICE_THRESHOLD 0x04)
;; Upgrade wormhole contract
(define-constant PTGM_UPDATE_WORMHOLE_CORE_ADDRESS 0x06)
;; Special Stacks operation: update recipient address
(define-constant PTGM_UPDATE_RECIPIENT_ADDRESS 0xa0)
;; Special Stacks operation: update storage contract address
(define-constant PTGM_UPDATE_PYTH_STORE_ADDRESS 0xa1)
;; Special Stacks operation: update decoder contract address
(define-constant PTGM_UPDATE_PYTH_DECODER_ADDRESS 0xa2)
;; Stacks chain id attributed by Pyth
(define-constant EXPECTED_CHAIN_ID (if is-in-mainnet 0xea86 0xc377))
;; Stacks module id attributed by Pyth
(define-constant EXPECTED_MODULE 0x03)
;; Error unauthorized control flow
(define-constant ERR_UNAUTHORIZED_ACCESS (err u4004))
;; Error unexpected action
(define-constant ERR_UNEXPECTED_ACTION (err u4001))
;; Error unexpected action payload
(define-constant ERR_UNEXPECTED_ACTION_PAYLOAD (err u4002))
;; Error unexpected action
(define-constant ERR_INVALID_ACTION_PAYLOAD (err u4003))
;; Error outdated action
(define-constant ERR_OUTDATED (err u4005))
;; Error unauthorized update
(define-constant ERR_UNAUTHORIZED_UPDATE (err u4006))
;; Error parsing PTGM
(define-constant ERR_INVALID_PTGM (err u4007))
(define-data-var governance-data-source
{ emitter-chain: uint, emitter-address: (buff 32) }
{ emitter-chain: u1, emitter-address: 0x5635979a221c34931e32620b9293a463065555ea71fe97cd6237ade875b12e9e })
(define-data-var prices-data-sources
(list 255 { emitter-chain: uint, emitter-address: (buff 32) })
(list
{ emitter-chain: u1, emitter-address: 0x6bb14509a612f01fbbc4cffeebd4bbfb492a86df717ebe92eb6df432a3f00a25 }
{ emitter-chain: u26, emitter-address: 0xf8cd23c2ab91237730770bbea08d61005cdda0984348f3f6eecb559638c0bba0 }
{ emitter-chain: u26, emitter-address: 0xe101faedac5851e32b9b23b5f9411a8c2bac4aae3ed4dd7b811dd1a72ea4aa71 }))
(define-data-var fee-value
{ mantissa: uint, exponent: uint }
{ mantissa: u1, exponent: u1 })
(define-data-var stale-price-threshold uint (if is-in-mainnet (* u2 u60 u60) (* u5 u365 u24 u60 u60))) ;; defaults: 2 hours on Mainnet, 5 years on Testnet
(define-data-var fee-recipient-address principal (if is-in-mainnet 'SP3CRXBDXQ2N5P7E25Q39MEX1HSMRDSEAP3CFK2Z3 'ST3CRXBDXQ2N5P7E25Q39MEX1HSMRDSEAP1JST19D))
(define-data-var last-sequence-processed uint u0)
;; Execution plan management
(define-data-var current-execution-plan {
pyth-oracle-contract: principal,
pyth-decoder-contract: principal,
pyth-storage-contract: principal,
wormhole-core-contract: principal
} {
pyth-oracle-contract: .pyth-oracle-v2,
pyth-decoder-contract: .pyth-pnau-decoder-v1,
pyth-storage-contract: .pyth-store-v1,
wormhole-core-contract: .wormhole-core-v2
})
(define-read-only (check-execution-flow
(former-contract-caller principal)
(execution-plan-opt (optional {
pyth-storage-contract: <pyth-storage-trait>,
pyth-decoder-contract: <pyth-decoder-trait>,
wormhole-core-contract: <wormhole-core-trait>
})))
(let ((expected-execution-plan (var-get current-execution-plan))
(success (if (is-eq contract-caller (get pyth-storage-contract expected-execution-plan))
;; The storage contract is checking its execution flow
;; Must always be invoked by the proxy
(try! (expect-contract-call-performed-by-expected-oracle-contract former-contract-caller expected-execution-plan))
;; Other contract
(if (is-eq contract-caller (get pyth-decoder-contract expected-execution-plan))
;; The decoding contract is checking its execution flow
(try! (expect-contract-call-performed-by-expected-oracle-contract former-contract-caller expected-execution-plan))
(if (is-eq contract-caller (get pyth-oracle-contract expected-execution-plan))
;; The proxy contract is checking its execution flow
(let ((execution-plan (unwrap! execution-plan-opt ERR_UNAUTHORIZED_ACCESS)))
;; Ensure that storage contract is the one expected
(try! (expect-active-storage-contract (get pyth-storage-contract execution-plan) expected-execution-plan))
;; Ensure that decoder contract is the one expected
(try! (expect-active-decoder-contract (get pyth-decoder-contract execution-plan) expected-execution-plan))
;; Ensure that wormhole contract is the one expected
(try! (expect-active-wormhole-contract (get wormhole-core-contract execution-plan) expected-execution-plan)))
false)))))
(if success (ok true) ERR_UNAUTHORIZED_ACCESS)))
(define-read-only (check-storage-contract
(storage-contract <pyth-storage-trait>))
(let ((expected-execution-plan (var-get current-execution-plan)))
;; Ensure that storage contract is the one expected
(expect-active-storage-contract storage-contract expected-execution-plan)))
(define-read-only (get-current-execution-plan)
(var-get current-execution-plan))
(define-read-only (get-fee-info)
(merge (var-get fee-value) { address: (var-get fee-recipient-address) }))
(define-read-only (get-stale-price-threshold)
(var-get stale-price-threshold))
(define-read-only (get-authorized-prices-data-sources)
(var-get prices-data-sources))
(define-public (update-fee-value (vaa-bytes (buff 8192)) (wormhole-core-contract <wormhole-core-trait>))
(let ((expected-execution-plan (var-get current-execution-plan))
(vaa (try! (contract-call? wormhole-core-contract parse-and-verify-vaa vaa-bytes)))
(ptgm (try! (parse-and-verify-ptgm (get payload vaa) (get sequence vaa)))))
;; Ensure action's expectation
(asserts! (is-eq (get action ptgm) PTGM_UPDATE_FEE) ERR_UNEXPECTED_ACTION)
;; Ensure that the action is authorized
(try! (check-update-source (get emitter-chain vaa) (get emitter-address vaa)))
;; Ensure that the lastest wormhole contract is used
(try! (expect-active-wormhole-contract wormhole-core-contract expected-execution-plan))
;; Update fee-value
(let ((updated-data (try! (parse-and-verify-fee-value (get body ptgm)))))
(var-set fee-value updated-data)
;; Emit event
(print { type: "fee-value", action: "updated", data: updated-data })
(ok updated-data))))
(define-public (update-stale-price-threshold (vaa-bytes (buff 8192)) (wormhole-core-contract <wormhole-core-trait>))
(let ((expected-execution-plan (var-get current-execution-plan))
(vaa (try! (contract-call? wormhole-core-contract parse-and-verify-vaa vaa-bytes)))
(ptgm (try! (parse-and-verify-ptgm (get payload vaa) (get sequence vaa)))))
;; Ensure action's expectation
(asserts! (is-eq (get action ptgm) PTGM_STALE_PRICE_THRESHOLD) ERR_UNEXPECTED_ACTION)
;; Ensure that the action is authorized
(try! (check-update-source (get emitter-chain vaa) (get emitter-address vaa)))
;; Ensure that the lastest wormhole contract is used
(try! (expect-active-wormhole-contract wormhole-core-contract expected-execution-plan))
;; Update fee-value
(let ((updated-data (try! (parse-and-verify-stale-price-threshold (get body ptgm)))))
(var-set stale-price-threshold updated-data)
;; Emit event
(print { type: "stale-price-threshold", action: "updated", data: updated-data })
(ok updated-data))))
(define-public (update-fee-recipient-address (vaa-bytes (buff 8192)) (wormhole-core-contract <wormhole-core-trait>))
(let ((expected-execution-plan (var-get current-execution-plan))
(vaa (try! (contract-call? wormhole-core-contract parse-and-verify-vaa vaa-bytes)))
(ptgm (try! (parse-and-verify-ptgm (get payload vaa) (get sequence vaa)))))
;; Ensure action's expectation
(asserts! (is-eq (get action ptgm) PTGM_UPDATE_RECIPIENT_ADDRESS) ERR_UNEXPECTED_ACTION)
;; Ensure that the action is authorized
(try! (check-update-source (get emitter-chain vaa) (get emitter-address vaa)))
;; Ensure that the lastest wormhole contract is used
(try! (expect-active-wormhole-contract wormhole-core-contract expected-execution-plan))
;; Update fee-recipient address
(let ((updated-data (try! (parse-principal (get body ptgm)))))
(var-set fee-recipient-address updated-data)
;; Emit event
(print { type: "fee-recipient", action: "updated", data: updated-data })
(ok updated-data))))
(define-public (update-wormhole-core-contract (vaa-bytes (buff 8192)) (wormhole-core-contract <wormhole-core-trait>))
(let ((expected-execution-plan (var-get current-execution-plan))
(vaa (try! (contract-call? wormhole-core-contract parse-and-verify-vaa vaa-bytes)))
(ptgm (try! (parse-and-verify-ptgm (get payload vaa) (get sequence vaa)))))
;; Ensure action's expectation
(asserts! (is-eq (get action ptgm) PTGM_UPDATE_WORMHOLE_CORE_ADDRESS) ERR_UNEXPECTED_ACTION)
;; Ensure that the action is authorized
(try! (check-update-source (get emitter-chain vaa) (get emitter-address vaa)))
;; Ensure that the lastest wormhole contract is used
(try! (expect-active-wormhole-contract wormhole-core-contract expected-execution-plan))
;; Update execution plan
(let ((updated-data (try! (parse-principal (get body ptgm)))))
(var-set current-execution-plan (merge expected-execution-plan { wormhole-core-contract: updated-data }))
;; Emit event
(print { type: "wormhole-core-contract", action: "updated", data: updated-data })
(ok (var-get current-execution-plan)))))
(define-public (update-pyth-oracle-contract (vaa-bytes (buff 8192)) (wormhole-core-contract <wormhole-core-trait>))
(let ((expected-execution-plan (var-get current-execution-plan))
(vaa (try! (contract-call? wormhole-core-contract parse-and-verify-vaa vaa-bytes)))
(ptgm (try! (parse-and-verify-ptgm (get payload vaa) (get sequence vaa)))))
;; Ensure action's expectation
(asserts! (is-eq (get action ptgm) PTGM_UPDATE_PYTH_ORACLE_ADDRESS) ERR_UNEXPECTED_ACTION)
;; Ensure that the action is authorized
(try! (check-update-source (get emitter-chain vaa) (get emitter-address vaa)))
;; Ensure that the lastest wormhole contract is used
(try! (expect-active-wormhole-contract wormhole-core-contract expected-execution-plan))
;; Update execution plan
(let ((updated-data (try! (parse-principal (get body ptgm)))))
(var-set current-execution-plan (merge expected-execution-plan { pyth-oracle-contract: updated-data }))
;; Emit event
(print { type: "pyth-oracle-contract", action: "updated", data: updated-data })
(ok (var-get current-execution-plan)))))
(define-public (update-pyth-decoder-contract (vaa-bytes (buff 8192)) (wormhole-core-contract <wormhole-core-trait>))
(let ((expected-execution-plan (var-get current-execution-plan))
(vaa (try! (contract-call? wormhole-core-contract parse-and-verify-vaa vaa-bytes)))
(ptgm (try! (parse-and-verify-ptgm (get payload vaa) (get sequence vaa)))))
;; Ensure action's expectation
(asserts! (is-eq (get action ptgm) PTGM_UPDATE_PYTH_DECODER_ADDRESS) ERR_UNEXPECTED_ACTION)
;; Ensure that the action is authorized
(try! (check-update-source (get emitter-chain vaa) (get emitter-address vaa)))
;; Ensure that the lastest wormhole contract is used
(try! (expect-active-wormhole-contract wormhole-core-contract expected-execution-plan))
;; Update execution plan
(let ((updated-data (try! (parse-principal (get body ptgm)))))
(var-set current-execution-plan (merge expected-execution-plan { pyth-decoder-contract: updated-data }))
;; Emit event
(print { type: "pyth-decoder-contract", action: "updated", data: updated-data })
(ok (var-get current-execution-plan)))))
(define-public (update-pyth-store-contract (vaa-bytes (buff 8192)) (wormhole-core-contract <wormhole-core-trait>))
(let ((expected-execution-plan (var-get current-execution-plan))
(vaa (try! (contract-call? wormhole-core-contract parse-and-verify-vaa vaa-bytes)))
(ptgm (try! (parse-and-verify-ptgm (get payload vaa) (get sequence vaa)))))
;; Ensure action's expectation
(asserts! (is-eq (get action ptgm) PTGM_UPDATE_PYTH_STORE_ADDRESS) ERR_UNEXPECTED_ACTION)
;; Ensure that the action is authorized
(try! (check-update-source (get emitter-chain vaa) (get emitter-address vaa)))
;; Ensure that the lastest wormhole contract is used
(try! (expect-active-wormhole-contract wormhole-core-contract expected-execution-plan))
;; Update execution plan
(let ((updated-data (try! (parse-principal (get body ptgm)))))
(var-set current-execution-plan (merge expected-execution-plan { pyth-storage-contract: updated-data }))
;; Emit event
(print { type: "pyth-storage-contract", action: "updated", data: updated-data })
(ok (var-get current-execution-plan)))))
(define-public (update-prices-data-sources (vaa-bytes (buff 8192)) (wormhole-core-contract <wormhole-core-trait>))
(let ((expected-execution-plan (var-get current-execution-plan))
(vaa (try! (contract-call? wormhole-core-contract parse-and-verify-vaa vaa-bytes)))
(ptgm (try! (parse-and-verify-ptgm (get payload vaa) (get sequence vaa)))))
;; Ensure action's expectation
(asserts! (is-eq (get action ptgm) PTGM_UPDATE_PRICES_DATA_SOURCES) ERR_UNEXPECTED_ACTION)
;; Ensure that the action is authorized
(try! (check-update-source (get emitter-chain vaa) (get emitter-address vaa)))
;; Ensure that the lastest wormhole contract is used
(try! (expect-active-wormhole-contract wormhole-core-contract expected-execution-plan))
;; Update prices-data-sources
(let ((updated-data (try! (parse-and-verify-prices-data-sources (get body ptgm)))))
(var-set prices-data-sources updated-data)
;; Emit event
(print { type: "prices-data-sources", action: "updated", data: updated-data })
(ok updated-data))))
(define-public (update-governance-data-source (vaa-bytes (buff 8192)) (wormhole-core-contract <wormhole-core-trait>))
(let ((expected-execution-plan (var-get current-execution-plan))
(vaa (try! (contract-call? wormhole-core-contract parse-and-verify-vaa vaa-bytes)))
(ptgm (try! (parse-and-verify-ptgm (get payload vaa) (get sequence vaa)))))
;; Ensure action's expectation
(asserts! (is-eq (get action ptgm) PTGM_UPDATE_GOVERNANCE_DATA_SOURCE) ERR_UNEXPECTED_ACTION)
;; Ensure that the action is authorized
(try! (check-update-source (get emitter-chain vaa) (get emitter-address vaa)))
;; Ensure that the lastest wormhole contract is used
(try! (expect-active-wormhole-contract wormhole-core-contract expected-execution-plan))
;; Update prices-data-sources
(let ((updated-data (try! (parse-and-verify-governance-data-source (get body ptgm)))))
(var-set governance-data-source updated-data)
;; Emit event
(print { type: "governance-data-source", action: "updated", data: updated-data })
(ok updated-data))))
(define-private (check-update-source (emitter-chain uint) (emitter-address (buff 32)))
(let ((authorized-data-source (var-get governance-data-source)))
;; Check data-source
(asserts! (is-eq
authorized-data-source
{ emitter-chain: emitter-chain, emitter-address: emitter-address })
ERR_UNAUTHORIZED_UPDATE)
(ok true)))
(define-private (expect-contract-call-performed-by-expected-oracle-contract
(former-contract-caller principal)
(expected-plan {
pyth-oracle-contract: principal,
pyth-decoder-contract: principal,
pyth-storage-contract: principal,
wormhole-core-contract: principal
}))
(begin
(asserts!
(is-eq former-contract-caller (get pyth-oracle-contract expected-plan))
ERR_UNAUTHORIZED_ACCESS)
(ok true)))
(define-private (expect-active-storage-contract
(storage-contract <pyth-storage-trait>)
(expected-plan {
pyth-oracle-contract: principal,
pyth-decoder-contract: principal,
pyth-storage-contract: principal,
wormhole-core-contract: principal
}))
(begin
(asserts!
(is-eq
(contract-of storage-contract)
(get pyth-storage-contract expected-plan))
ERR_UNAUTHORIZED_ACCESS)
(ok true)))
(define-private (expect-active-decoder-contract
(decoder-contract <pyth-decoder-trait>)
(expected-plan {
pyth-oracle-contract: principal,
pyth-decoder-contract: principal,
pyth-storage-contract: principal,
wormhole-core-contract: principal
}))
(begin
(asserts!
(is-eq
(contract-of decoder-contract)
(get pyth-decoder-contract expected-plan))
ERR_UNAUTHORIZED_ACCESS)
(ok true)))
(define-private (expect-active-wormhole-contract
(wormhole-contract <wormhole-core-trait>)
(expected-plan {
pyth-oracle-contract: principal,
pyth-decoder-contract: principal,
pyth-storage-contract: principal,
wormhole-core-contract: principal
}))
(begin
(asserts!
(is-eq
(contract-of wormhole-contract)
(get wormhole-core-contract expected-plan))
ERR_UNAUTHORIZED_ACCESS)
(ok true)))
(define-private (parse-and-verify-ptgm (ptgm-bytes (buff 8192)) (sequence uint))
(let ((cursor-magic (unwrap! (contract-call? 'SP2J933XB2CP2JQ1A4FGN8JA968BBG3NK3EKZ7Q9F.hk-cursor-v2 read-buff-4 { bytes: ptgm-bytes, pos: u0 })
ERR_INVALID_PTGM))
(cursor-module (unwrap! (contract-call? 'SP2J933XB2CP2JQ1A4FGN8JA968BBG3NK3EKZ7Q9F.hk-cursor-v2 read-buff-1 (get next cursor-magic))
ERR_INVALID_PTGM))
(cursor-action (unwrap! (contract-call? 'SP2J933XB2CP2JQ1A4FGN8JA968BBG3NK3EKZ7Q9F.hk-cursor-v2 read-buff-1 (get next cursor-module))
ERR_INVALID_PTGM))
(cursor-target-chain-id (unwrap! (contract-call? 'SP2J933XB2CP2JQ1A4FGN8JA968BBG3NK3EKZ7Q9F.hk-cursor-v2 read-buff-2 (get next cursor-action))
ERR_INVALID_PTGM))
(cursor-body (unwrap! (contract-call? 'SP2J933XB2CP2JQ1A4FGN8JA968BBG3NK3EKZ7Q9F.hk-cursor-v2 read-buff-8192-max (get next cursor-target-chain-id) none)
ERR_INVALID_PTGM)))
;; Check magic bytes
(asserts! (is-eq (get value cursor-magic) PTGM_MAGIC) ERR_INVALID_PTGM)
;; Check target-chain-id
(asserts! (is-eq (get value cursor-target-chain-id) EXPECTED_CHAIN_ID) ERR_INVALID_PTGM)
;; Check module
(asserts! (is-eq (get value cursor-module) EXPECTED_MODULE) ERR_INVALID_PTGM)
;; Check Sequence
(asserts! (> sequence (var-get last-sequence-processed)) ERR_OUTDATED)
;; Update Sequence
(var-set last-sequence-processed sequence)
(ok {
action: (get value cursor-action),
target-chain-id: (get value cursor-target-chain-id),
module: (get value cursor-module),
cursor: cursor-target-chain-id,
body: (get value cursor-body)
})))
(define-private (parse-and-verify-fee-value (ptgm-body (buff 8192)))
(let ((cursor-ptgm-body (contract-call? 'SP2J933XB2CP2JQ1A4FGN8JA968BBG3NK3EKZ7Q9F.hk-cursor-v2 new ptgm-body none))
(cursor-mantissa (unwrap! (contract-call? 'SP2J933XB2CP2JQ1A4FGN8JA968BBG3NK3EKZ7Q9F.hk-cursor-v2 read-uint-64 (get next cursor-ptgm-body))
ERR_INVALID_ACTION_PAYLOAD))
(cursor-exponent (unwrap! (contract-call? 'SP2J933XB2CP2JQ1A4FGN8JA968BBG3NK3EKZ7Q9F.hk-cursor-v2 read-uint-64 (get next cursor-mantissa))
ERR_INVALID_ACTION_PAYLOAD)))
(ok {
mantissa: (get value cursor-mantissa),
exponent: (get value cursor-exponent)
})))
(define-private (parse-and-verify-stale-price-threshold (ptgm-body (buff 8192)))
(let ((cursor-ptgm-body (contract-call? 'SP2J933XB2CP2JQ1A4FGN8JA968BBG3NK3EKZ7Q9F.hk-cursor-v2 new ptgm-body none))
(cursor-stale-price-threshold (unwrap! (contract-call? 'SP2J933XB2CP2JQ1A4FGN8JA968BBG3NK3EKZ7Q9F.hk-cursor-v2 read-uint-64 (get next cursor-ptgm-body))
ERR_INVALID_ACTION_PAYLOAD)))
(ok (get value cursor-stale-price-threshold))))
(define-private (parse-and-verify-governance-data-source (ptgm-body (buff 8192)))
(let ((cursor-ptgm-body (contract-call? 'SP2J933XB2CP2JQ1A4FGN8JA968BBG3NK3EKZ7Q9F.hk-cursor-v2 new ptgm-body none))
(cursor-emitter-chain (unwrap! (contract-call? 'SP2J933XB2CP2JQ1A4FGN8JA968BBG3NK3EKZ7Q9F.hk-cursor-v2 read-uint-16 (get next cursor-ptgm-body))
ERR_INVALID_ACTION_PAYLOAD))
(cursor-emitter-address (unwrap! (contract-call? 'SP2J933XB2CP2JQ1A4FGN8JA968BBG3NK3EKZ7Q9F.hk-cursor-v2 read-buff-32 (get next cursor-emitter-chain))
ERR_INVALID_ACTION_PAYLOAD)))
(ok {
emitter-chain: (get value cursor-emitter-chain),
emitter-address: (get value cursor-emitter-address)
})))
(define-private (parse-principal (ptgm-body (buff 8192)))
(let ((cursor-ptgm-body (contract-call? 'SP2J933XB2CP2JQ1A4FGN8JA968BBG3NK3EKZ7Q9F.hk-cursor-v2 new ptgm-body none))
(cursor-principal-len (try! (contract-call? 'SP2J933XB2CP2JQ1A4FGN8JA968BBG3NK3EKZ7Q9F.hk-cursor-v2 read-uint-8 (get next cursor-ptgm-body))))
(principal-bytes (contract-call? 'SP2J933XB2CP2JQ1A4FGN8JA968BBG3NK3EKZ7Q9F.hk-cursor-v2 slice (get next cursor-principal-len) (some (get value cursor-principal-len))))
(new-principal (unwrap! (from-consensus-buff? principal principal-bytes) ERR_UNEXPECTED_ACTION_PAYLOAD)))
(ok new-principal)))
(define-private (parse-and-verify-prices-data-sources (ptgm-body (buff 8192)))
(let ((cursor-ptgm-body (contract-call? 'SP2J933XB2CP2JQ1A4FGN8JA968BBG3NK3EKZ7Q9F.hk-cursor-v2 new ptgm-body none))
(cursor-num-data-sources (try! (contract-call? 'SP2J933XB2CP2JQ1A4FGN8JA968BBG3NK3EKZ7Q9F.hk-cursor-v2 read-uint-8 (get next cursor-ptgm-body))))
(cursor-data-sources-bytes (contract-call? 'SP2J933XB2CP2JQ1A4FGN8JA968BBG3NK3EKZ7Q9F.hk-cursor-v2 slice (get next cursor-num-data-sources) none))
(data-sources (get result (fold parse-data-source cursor-data-sources-bytes {
result: (list),
cursor: {
index: u0,
next-update-index: u0
},
bytes: cursor-data-sources-bytes,
limit: (get value cursor-num-data-sources)
}))))
(ok data-sources)))
(define-private (parse-data-source
(entry (buff 1))
(acc {
cursor: {
index: uint,
next-update-index: uint
},
bytes: (buff 8192),
result: (list 255 { emitter-chain: uint, emitter-address: (buff 32) }),
limit: uint
}))
(if (is-eq (len (get result acc)) (get limit acc))
acc
(if (is-eq (get index (get cursor acc)) (get next-update-index (get cursor acc)))
;; Parse update
(let ((buffer (contract-call? 'SP2J933XB2CP2JQ1A4FGN8JA968BBG3NK3EKZ7Q9F.hk-cursor-v2 new (get bytes acc) (some (get index (get cursor acc)))))
(cursor-emitter-chain (unwrap-panic (contract-call? 'SP2J933XB2CP2JQ1A4FGN8JA968BBG3NK3EKZ7Q9F.hk-cursor-v2 read-uint-16 (get next buffer))))
(cursor-emitter-address (unwrap-panic (contract-call? 'SP2J933XB2CP2JQ1A4FGN8JA968BBG3NK3EKZ7Q9F.hk-cursor-v2 read-buff-32 (get next cursor-emitter-chain)))))
;; Perform assertions
{
cursor: {
index: (+ (get index (get cursor acc)) u1),
next-update-index: (+ (get index (get cursor acc)) u34),
},
bytes: (get bytes acc),
result: (unwrap-panic (as-max-len? (append (get result acc) {
emitter-chain: (get value cursor-emitter-chain),
emitter-address: (get value cursor-emitter-address)
}) u255)),
limit: (get limit acc),
})
;; Increment position
{
cursor: {
index: (+ (get index (get cursor acc)) u1),
next-update-index: (get next-update-index (get cursor acc)),
},
bytes: (get bytes acc),
result: (get result acc),
limit: (get limit acc)
})))