Skip to content

Commit

Permalink
move read functions to be read-only
Browse files Browse the repository at this point in the history
  • Loading branch information
OxMarco committed Oct 9, 2024
1 parent 6d43fd4 commit e0b6219
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 28 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ The `pyth-oracle-v2` contract is exposing the following method:

```clarity
(define-public (read-price-feed
(price-feed-id (buff 32))
(pyth-storage-address <pyth-storage-trait>)))
(price-feed-id (buff 32))
))
```

That can be consumed with the following invocation:
Expand All @@ -65,7 +65,7 @@ That can be consumed with the following invocation:
'SP2T5JKWWP3FYYX4YRK8GK5BG2YCNGEAEY2P2PKN0.pyth-oracle-v2 ;; Address of the helper contract
read-price-feed
0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43 ;; BTC-USD price identifier
'SP2T5JKWWP3FYYX4YRK8GK5BG2YCNGEAEY2P2PKN0.pyth-store-v1)
)
```

The authenticity of the price feeds is verified during their ingestion, making the cost of queries as light as possible.
Expand Down
9 changes: 3 additions & 6 deletions contracts/pyth-oracle-v2.clar
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@
;; Balance insufficient for handling fee
(define-constant ERR_BALANCE_INSUFFICIENT (err u402))

(define-public (read-price-feed
(price-feed-id (buff 32))
(pyth-storage-address <pyth-storage-trait>))
(define-read-only (read-price-feed
(price-feed-id (buff 32)))
(begin
;; Check execution flow
(try! (contract-call? .pyth-governance-v1 check-storage-contract pyth-storage-address))
;; Perform contract-call
(contract-call? pyth-storage-address read price-feed-id)))
(contract-call? .pyth-store-v1 read price-feed-id)))

(define-public (verify-and-update-price-feeds
(price-feed-bytes (buff 8192))
Expand Down
2 changes: 1 addition & 1 deletion contracts/pyth-store-v1.clar
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

(define-map timestamps (buff 32) uint)

(define-public (read (price-identifier (buff 32)))
(define-read-only (read (price-identifier (buff 32)))
(let ((entry (unwrap! (map-get? prices price-identifier) (err u404))))
(ok entry)))

Expand Down
10 changes: 5 additions & 5 deletions deployments/default.simnet-plan.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ plan:
emulated-sender: ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM
path: contracts/pyth-governance-v1.clar
clarity-version: 3
- emulated-contract-publish:
contract-name: pyth-store-v1
emulated-sender: ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM
path: contracts/pyth-store-v1.clar
clarity-version: 3
- emulated-contract-publish:
contract-name: pyth-oracle-v2
emulated-sender: ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM
Expand All @@ -92,11 +97,6 @@ plan:
emulated-sender: ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM
path: contracts/pyth-pnau-decoder-v1.clar
clarity-version: 3
- emulated-contract-publish:
contract-name: pyth-store-v1
emulated-sender: ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM
path: contracts/pyth-store-v1.clar
clarity-version: 3
- emulated-contract-publish:
contract-name: wormhole-core-v2
emulated-sender: ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM
Expand Down
2 changes: 1 addition & 1 deletion example/cbtc/contracts/cbtc-pool.clar
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,6 @@
(read-price-from-pyth 0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43))

(define-private (read-price-from-pyth (price-id (buff 32)))
(let ((feed (unwrap! (contract-call? 'ST2T5JKWWP3FYYX4YRK8GK5BG2YCNGEAEY1JKX06E.pyth-oracle-v2 read-price-feed price-id 'ST2T5JKWWP3FYYX4YRK8GK5BG2YCNGEAEY1JKX06E.pyth-store-v1) (err u0)))
(let ((feed (unwrap! (contract-call? 'ST2T5JKWWP3FYYX4YRK8GK5BG2YCNGEAEY1JKX06E.pyth-oracle-v2 read-price-feed price-id) (err u0)))
(price (get price feed)))
(ok (to-uint price))))
3 changes: 1 addition & 2 deletions unit-tests/pyth/oracle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,11 @@ describe("pyth-oracle-v2::decode-and-verify-price-feeds mainnet VAAs", () => {
).result;
expect(res).toBeOk(Cl.list([priceUpdated]));

res = simnet.callPublicFn(
res = simnet.callReadOnlyFn(
pythOracleContractName,
"read-price-feed",
[
priceIdentifier,
Cl.contractPrincipal(simnet.deployer, pythStorageContractName),
],
sender,
).result;
Expand Down
9 changes: 3 additions & 6 deletions unit-tests/pyth/pnau.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,12 +271,11 @@ describe("pyth-pnau-decoder-v1::decode-and-verify-price-feeds failures", () => {
});

it("should succeed updating prices once", () => {
let res = simnet.callPublicFn(
let res = simnet.callReadOnlyFn(
pythOracleContractName,
"read-price-feed",
[
Cl.buffer(pyth.BtcPriceIdentifier),
Cl.contractPrincipal(simnet.deployer, pythStorageContractName),
],
sender,
);
Expand Down Expand Up @@ -333,12 +332,11 @@ describe("pyth-pnau-decoder-v1::decode-and-verify-price-feeds failures", () => {
sender,
);

res = simnet.callPublicFn(
res = simnet.callReadOnlyFn(
pythOracleContractName,
"read-price-feed",
[
Cl.buffer(pyth.BtcPriceIdentifier),
Cl.contractPrincipal(simnet.deployer, pythStorageContractName),
],
sender,
);
Expand Down Expand Up @@ -394,12 +392,11 @@ describe("pyth-pnau-decoder-v1::decode-and-verify-price-feeds failures", () => {
sender,
);

res = simnet.callPublicFn(
res = simnet.callReadOnlyFn(
pythOracleContractName,
"read-price-feed",
[
Cl.buffer(pyth.BtcPriceIdentifier),
Cl.contractPrincipal(simnet.deployer, pythStorageContractName),
],
sender,
);
Expand Down
7 changes: 3 additions & 4 deletions unit-tests/pyth/ptgm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -573,16 +573,15 @@ describe("pyth-governance-v1::update-pyth-store-contract", () => {
);
expect(res.result).toBeErr(Cl.uint(4004));

res = simnet.callPublicFn(
res = simnet.callReadOnlyFn(
pythOracleContractName,
`read-price-feed`,
"read-price-feed",
[
Cl.bufferFromHex("00"),
Cl.contractPrincipal(deployer, pythStorageContractName),
],
sender,
);
expect(res.result).toBeErr(Cl.uint(4004));
expect(res.result).toBeErr(Cl.uint(404));
});

it("should fail if action mismatches", () => {
Expand Down

0 comments on commit e0b6219

Please sign in to comment.