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

fix: wormhole update #32

Merged
merged 3 commits into from
Jan 15, 2024
Merged
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
4 changes: 2 additions & 2 deletions Clarinet.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ path = 'contracts/pyth-traits-v1.clar'
clarity_version = 2
epoch = 2.4

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

Expand Down
26 changes: 12 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@

/ / ▶ Stacks Pyth Bridge
/ / ▶ Stacks Pyth Bridge
/ --- / Bridging Pyth price feeds to the Stacks blockchain.
/ / Retrieve trading pairs (BTC-USD, STX-USD, etc.) from Clarity smart contracts.

/ / Retrieve trading pairs (BTC-USD, STX-USD, etc.) from Clarity smart contracts.

                                   [![Introduction](https://img.shields.io/badge/%23-%20Introduction%20-orange?labelColor=gray)](#Introduction)
    [![Features](https://img.shields.io/badge/%23-Features-orange?labelColor=gray)](#Features)
    [![Getting started](https://img.shields.io/badge/%23-Quick%20Start-orange?labelColor=gray)](#Quick-start)
    [![Documentation](https://img.shields.io/badge/%23-Documentation-orange?labelColor=gray)](#Documentation)
    [![Contribute](https://img.shields.io/badge/%23-Contribute-orange?labelColor=gray)](#Contribute)

***
---

# Introduction

Expand Down Expand Up @@ -43,8 +41,8 @@ $ npm test

### Latest Deployments

| network | address |
|---------|---------------------------------------------------------------------|
| network | address |
| ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| testnet | [ST2T5JKWWP3FYYX4YRK8GK5BG2YCNGEAEY1JKX06E.pyth-helper-v1](https://explorer.hiro.so/txid/0x5339f90ccdbb88e437b9b889613f1554c377d5815e3b90bbc6305b317b7bb8e8?chain=testnet) |
| mainnet | [SP2T5JKWWP3FYYX4YRK8GK5BG2YCNGEAEY2P2PKN0.pyth-helper-v1](https://explorer.hiro.so/txid/0xd86c2fda8a090c43016250c33231878673af62ac95d9c50645f6e2c303b9a173?chain=mainnet) |

Expand All @@ -53,21 +51,21 @@ $ npm test
The `pyth-helper-v1` contract is exposing the following method:

```clarity
(define-public (read-price
(define-public (read-price
(price-feed-id (buff 32))))
```

That can be consumed with the following invocation:

```clarity
(contract-call?
(contract-call?
'SP2T5JKWWP3FYYX4YRK8GK5BG2YCNGEAEY2P2PKN0.pyth-oracle-v2 ;; Address of the helper contract
read-price
0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43) ;; BTC-USD price identifier
{
pyth-storage-contract: 'SP2T5JKWWP3FYYX4YRK8GK5BG2YCNGEAEY2P2PKN0.pyth-storage-v1,
pyth-decoder-contract: 'SP2T5JKWWP3FYYX4YRK8GK5BG2YCNGEAEY2P2PKN0.pyth-pnau-decoder-v1,
wormhole-core-contract: 'SP2T5JKWWP3FYYX4YRK8GK5BG2YCNGEAEY2P2PKN0.wormhole-core-v1
wormhole-core-contract: 'SP2T5JKWWP3FYYX4YRK8GK5BG2YCNGEAEY2P2PKN0.wormhole-core-v2
}
```

Expand Down Expand Up @@ -125,21 +123,21 @@ This sequence of bytes is a Verified Action Approvals (VAA) including the price
This VAA can be encoded as a Clarity buffer, and submitted to the Pyth contract using the following:

```clarity
(contract-call?
(contract-call?
'SP2T5JKWWP3FYYX4YRK8GK5BG2YCNGEAEY2P2PKN0.pyth-oracle-v2 ;; Address of the helper contract
verify-and-update-price
0x504e41550100000003b8...a7b10321ad7c2404a910 ;; BTC-USD price update
{
pyth-storage-contract: 'SP2T5JKWWP3FYYX4YRK8GK5BG2YCNGEAEY2P2PKN0.pyth-storage-v1,
pyth-decoder-contract: 'SP2T5JKWWP3FYYX4YRK8GK5BG2YCNGEAEY2P2PKN0.pyth-pnau-decoder-v1,
wormhole-core-contract: 'SP2T5JKWWP3FYYX4YRK8GK5BG2YCNGEAEY2P2PKN0.wormhole-core-v1
wormhole-core-contract: 'SP2T5JKWWP3FYYX4YRK8GK5BG2YCNGEAEY2P2PKN0.wormhole-core-v2
})
```

If the VAA is valid, the contract call will return a payload with the following signature:

```clarity
(response
(response
(list 64 {
price-identifier: (buff 32),
price: int,
Expand All @@ -149,7 +147,7 @@ If the VAA is valid, the contract call will return a payload with the following
ema-conf: uint,
publish-time: uint,
prev-publish-time: uint,
})
})
uint)
```

Expand Down
2 changes: 1 addition & 1 deletion contracts/pyth-governance-v1.clar
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
pyth-oracle-contract: .pyth-oracle-v2,
pyth-decoder-contract: .pyth-pnau-decoder-v1,
pyth-storage-contract: .pyth-store-v1,
wormhole-core-contract: .wormhole-core-v1
wormhole-core-contract: .wormhole-core-v2
})

(define-read-only (check-execution-flow
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@
(define-constant ERR_GSU_CHECK_CHAIN (err u1303))
;; Guardian Set Update new index invalid
(define-constant ERR_GSU_CHECK_INDEX (err u1304))
;; Guardian Set Update emission payload unauthorized
(define-constant ERR_GSU_CHECK_EMITTER (err u1305))

;; Guardian set upgrade emitting address
(define-constant GSU-EMITTING-ADDRESS 0x0000000000000000000000000000000000000000000000000000000000000004)
;; Guardian set upgrade emitting chain
(define-constant GSU-EMITTING-CHAIN u1)


(define-constant hk-cursor-v2 'SP2J933XB2CP2JQ1A4FGN8JA968BBG3NK3EKZ7Q9F.hk-cursor-v2)

Expand Down Expand Up @@ -198,6 +206,10 @@
;; Ensure that enough uncompressed-public-keys were provided
(asserts! (is-eq (len uncompressed-public-keys) (len eth-addresses))
ERR_GSU_UNCOMPRESSED_PUBLIC_KEYS)
;; Check emitting address
(asserts! (is-eq (get emitter-address vaa) GSU-EMITTING-ADDRESS) ERR_GSU_CHECK_EMITTER)
;; Check emitting address
(asserts! (is-eq (get emitter-chain vaa) GSU-EMITTING-CHAIN) ERR_GSU_CHECK_EMITTER)
;; Update storage
(map-set guardian-sets { set-id: set-id } (get result consolidated-public-keys))
(var-set active-guardian-set-id set-id)
Expand Down
2 changes: 1 addition & 1 deletion unit-tests/pyth/oracle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { wormhole } from "../wormhole/helpers";
const pythOracleContractName = "pyth-oracle-v2";
const pythDecoderPnauContractName = "pyth-pnau-decoder-v1";
const pythStorageContractName = "pyth-store-v1";
const wormholeCoreContractName = "wormhole-core-v1";
const wormholeCoreContractName = "wormhole-core-v2";

describe("pyth-oracle-v2::decode-and-verify-price-feeds mainnet VAAs", () => {
const accounts = simnet.getAccounts();
Expand Down
2 changes: 1 addition & 1 deletion unit-tests/pyth/pnau.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const pythOracleContractName = "pyth-oracle-v2";
const pythDecoderPnauContractName = "pyth-pnau-decoder-v1";
const pythGovernanceContractName = "pyth-governance-v1";
const pythStorageContractName = "pyth-store-v1";
const wormholeCoreContractName = "wormhole-core-v1";
const wormholeCoreContractName = "wormhole-core-v2";

describe("pyth-pnau-decoder-v1::decode-and-verify-price-feeds success", () => {
const accounts = simnet.getAccounts();
Expand Down
6 changes: 3 additions & 3 deletions unit-tests/pyth/ptgm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { Cl, ClarityType } from "@stacks/transactions";
import { beforeEach, describe, expect, it } from "vitest";
import { wormhole } from "../wormhole/helpers";
import { pyth } from "./helpers";
import { hexToBytes } from "@noble/hashes/utils";
import { bytesToHex, hexToBytes } from "@noble/hashes/utils";
import { ParsedTransactionResult } from "@hirosystems/clarinet-sdk";
import { ptgmTestnetVaas } from "./fixtures";

const pythOracleContractName = "pyth-oracle-v2";
const pythStorageContractName = "pyth-store-v1";
const pythDecoderPnauContractName = "pyth-pnau-decoder-v1";
const pythGovernanceContractName = "pyth-governance-v1";
const wormholeCoreContractName = "wormhole-core-v1";
const wormholeCoreContractName = "wormhole-core-v2";
const initialFeeRecipient = "ST3CRXBDXQ2N5P7E25Q39MEX1HSMRDSEAP1JST19D";

describe("pyth-governance-v1::update-fee-value mainnet VAAs", () => {
Expand Down Expand Up @@ -244,7 +244,7 @@ describe("pyth-governance-v1::update-wormhole-core-contract", () => {
const guardianSet = wormhole.generateGuardianSetKeychain(19);
let updateWormholeContract = {
address: "ST2CY5V39NHDPWSXMW9QDT3HC3GD6Q6XX4CFRK9AG",
contractName: "wormhole-core-v2",
contractName: "wormhole-core-v3",
};
let ptgmVaaPayload = pyth.buildPtgmVaaPayload({ updateWormholeContract });

Expand Down
9 changes: 9 additions & 0 deletions unit-tests/wormhole/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { hmac } from "@noble/hashes/hmac";
import { sha256 } from "@noble/hashes/sha256";
import { gsuMainnetVaas } from "./fixtures";
import { pyth } from "../pyth/helpers";
import { hexToBytes } from "@noble/hashes/utils";

secp.etc.hmacSha256Sync = (k, ...m) =>
hmac(sha256, k, secp.etc.concatBytes(...m));
Expand Down Expand Up @@ -81,6 +82,13 @@ export namespace wormhole {
payload?: Uint8Array;
}

export const GovernanceUpdateEmitter = {
chain: 1,
address: hexToBytes(
"0000000000000000000000000000000000000000000000000000000000000004",
),
};

export namespace fc_ext {
// Helper for generating a VAA Body;
// Wire format reminder:
Expand Down Expand Up @@ -466,6 +474,7 @@ export namespace wormhole {
);
let vaaBody = wormhole.buildValidVaaBodySpecs({
payload: guardianRotationPayload,
emitter: wormhole.GovernanceUpdateEmitter,
});
let vaaHeader = wormhole.buildValidVaaHeader(keychain, vaaBody, {
version: 1,
Expand Down
Loading