Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: runbook compliance #41

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 22 additions & 21 deletions Clarinet.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,48 @@ authors = []
telemetry = false
cache_dir = './.cache'

[[project.requirements]]
contract_id = 'SP2J933XB2CP2JQ1A4FGN8JA968BBG3NK3EKZ7Q9F.hk-cursor-v2'

[[project.requirements]]
contract_id = 'SP2J933XB2CP2JQ1A4FGN8JA968BBG3NK3EKZ7Q9F.hk-ecc-v1'
[contracts.hk-cursor]
path = 'contracts/requirements/hk-cursor.clar'
clarity_version = 2
epoch = 2.4

[[project.requirements]]
contract_id = 'SP2J933XB2CP2JQ1A4FGN8JA968BBG3NK3EKZ7Q9F.hk-merkle-tree-keccak160-v1'
[contracts.hk-merkle-tree-keccak160]
path = 'contracts/requirements/hk-merkle-tree-keccak160.clar'
clarity_version = 2
epoch = 2.4

[contracts.pyth-governance-v1]
path = 'contracts/pyth-governance-v1.clar'
[contracts.pyth-governance]
path = 'contracts/pyth-governance.clar'
clarity_version = 2
epoch = 2.4

[contracts.pyth-oracle-v2]
path = 'contracts/pyth-oracle-v2.clar'
[contracts.pyth-oracle]
path = 'contracts/pyth-oracle.clar'
clarity_version = 2
epoch = 2.4

[contracts.pyth-store-v1]
path = 'contracts/pyth-store-v1.clar'
[contracts.pyth-store]
path = 'contracts/pyth-store.clar'
clarity_version = 2
epoch = 2.4

[contracts.pyth-pnau-decoder-v1]
path = 'contracts/pyth-pnau-decoder-v1.clar'
[contracts.pyth-pnau-decoder]
path = 'contracts/pyth-pnau-decoder.clar'
clarity_version = 2
epoch = 2.4

[contracts.pyth-traits-v1]
path = 'contracts/pyth-traits-v1.clar'
[contracts.pyth-traits]
path = 'contracts/pyth-traits.clar'
clarity_version = 2
epoch = 2.4

[contracts.wormhole-core-v2]
path = 'contracts/wormhole/wormhole-core-v2.clar'
[contracts.wormhole-core]
path = 'contracts/wormhole/wormhole-core.clar'
clarity_version = 2
epoch = 2.4

[contracts.wormhole-traits-v1]
path = 'contracts/wormhole/wormhole-traits-v1.clar'
[contracts.wormhole-traits]
path = 'contracts/wormhole/wormhole-traits.clar'
clarity_version = 2
epoch = 2.4

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
;; 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)
(use-trait pyth-proxy-trait .pyth-traits.proxy-trait)
(use-trait pyth-decoder-trait .pyth-traits.decoder-trait)
(use-trait pyth-storage-trait .pyth-traits.storage-trait)
(use-trait wormhole-core-trait .wormhole-traits.core-trait)

(define-constant PTGM_MAGIC 0x5054474d) ;; 'PTGM': Pyth Governance Message

Expand Down Expand Up @@ -72,10 +72,10 @@
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
pyth-oracle-contract: .pyth-oracle,
pyth-decoder-contract: .pyth-pnau-decoder,
pyth-storage-contract: .pyth-store,
wormhole-core-contract: .wormhole-core
})

(define-read-only (check-execution-flow
Expand Down Expand Up @@ -349,15 +349,15 @@
(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 })
(let ((cursor-magic (unwrap! (contract-call? .hk-cursor 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))
(cursor-module (unwrap! (contract-call? .hk-cursor 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))
(cursor-action (unwrap! (contract-call? .hk-cursor 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))
(cursor-target-chain-id (unwrap! (contract-call? .hk-cursor 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)
(cursor-body (unwrap! (contract-call? .hk-cursor 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)
Expand All @@ -378,44 +378,44 @@
})))

(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))
(let ((cursor-ptgm-body (contract-call? .hk-cursor new ptgm-body none))
(cursor-mantissa (unwrap! (contract-call? .hk-cursor 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))
(cursor-exponent (unwrap! (contract-call? .hk-cursor 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))
(let ((cursor-ptgm-body (contract-call? .hk-cursor new ptgm-body none))
(cursor-stale-price-threshold (unwrap! (contract-call? .hk-cursor 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))
(let ((cursor-ptgm-body (contract-call? .hk-cursor new ptgm-body none))
(cursor-emitter-chain (unwrap! (contract-call? .hk-cursor 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))
(cursor-emitter-address (unwrap! (contract-call? .hk-cursor 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))))
(let ((cursor-ptgm-body (contract-call? .hk-cursor new ptgm-body none))
(cursor-principal-len (try! (contract-call? .hk-cursor read-uint-8 (get next cursor-ptgm-body))))
(principal-bytes (contract-call? .hk-cursor 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))
(let ((cursor-ptgm-body (contract-call? .hk-cursor new ptgm-body none))
(cursor-num-data-sources (try! (contract-call? .hk-cursor read-uint-8 (get next cursor-ptgm-body))))
(cursor-data-sources-bytes (contract-call? .hk-cursor slice (get next cursor-num-data-sources) none))
(data-sources (get result (fold parse-data-source cursor-data-sources-bytes {
result: (list),
cursor: {
Expand All @@ -442,9 +442,9 @@
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)))))
(let ((buffer (contract-call? .hk-cursor new (get bytes acc) (some (get index (get cursor acc)))))
(cursor-emitter-chain (unwrap-panic (contract-call? .hk-cursor read-uint-16 (get next buffer))))
(cursor-emitter-address (unwrap-panic (contract-call? .hk-cursor read-buff-32 (get next cursor-emitter-chain)))))
;; Perform assertions
{
cursor: {
Expand Down
16 changes: 8 additions & 8 deletions contracts/pyth-oracle-v2.clar → contracts/pyth-oracle.clar
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
;; 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-storage-trait .pyth-traits-v1.storage-trait)
(use-trait pyth-decoder-trait .pyth-traits-v1.decoder-trait)
(use-trait wormhole-core-trait .wormhole-traits-v1.core-trait)
(use-trait pyth-storage-trait .pyth-traits.storage-trait)
(use-trait pyth-decoder-trait .pyth-traits.decoder-trait)
(use-trait wormhole-core-trait .wormhole-traits.core-trait)

;; Generic error
(define-constant ERR_PANIC (err u0))
Expand All @@ -17,7 +17,7 @@
(pyth-storage-address <pyth-storage-trait>))
(begin
;; Check execution flow
(try! (contract-call? .pyth-governance-v1 check-storage-contract pyth-storage-address))
(try! (contract-call? .pyth-governance check-storage-contract pyth-storage-address))
;; Perform contract-call
(contract-call? pyth-storage-address read price-feed-id)))

Expand All @@ -30,14 +30,14 @@
}))
(begin
;; Check execution flow
(try! (contract-call? .pyth-governance-v1 check-execution-flow contract-caller (some execution-plan)))
(try! (contract-call? .pyth-governance check-execution-flow contract-caller (some execution-plan)))
;; Perform contract-call
(let ((pyth-decoder-contract (get pyth-decoder-contract execution-plan))
(wormhole-core-contract (get wormhole-core-contract execution-plan))
(pyth-storage-contract (get pyth-storage-contract execution-plan))
(decoded-prices (try! (contract-call? pyth-decoder-contract decode-and-verify-price-feeds price-feed-bytes wormhole-core-contract)))
(updated-prices (try! (contract-call? pyth-storage-contract write decoded-prices)))
(fee-info (contract-call? .pyth-governance-v1 get-fee-info))
(fee-info (contract-call? .pyth-governance get-fee-info))
(fee-amount (* (len updated-prices) (* (get mantissa fee-info) (pow u10 (get exponent fee-info))))))
;; Charge fee
(unwrap! (stx-transfer? fee-amount tx-sender (get address fee-info)) ERR_BALANCE_INSUFFICIENT)
Expand All @@ -52,12 +52,12 @@
}))
(begin
;; Check execution flow
(try! (contract-call? .pyth-governance-v1 check-execution-flow contract-caller (some execution-plan)))
(try! (contract-call? .pyth-governance check-execution-flow contract-caller (some execution-plan)))
;; Perform contract-call
(let ((pyth-decoder-contract (get pyth-decoder-contract execution-plan))
(wormhole-core-contract (get wormhole-core-contract execution-plan))
(decoded-prices (try! (contract-call? pyth-decoder-contract decode-and-verify-price-feeds price-feed-bytes wormhole-core-contract)))
(fee-info (contract-call? .pyth-governance-v1 get-fee-info))
(fee-info (contract-call? .pyth-governance get-fee-info))
(fee-amount (* (len decoded-prices) (* (get mantissa fee-info) (pow u10 (get exponent fee-info))))))
;; Charge fee
(unwrap! (stx-transfer? fee-amount tx-sender (get address fee-info)) ERR_BALANCE_INSUFFICIENT)
Expand Down
File renamed without changes.
Loading
Loading