Skip to content

Commit

Permalink
Merge pull request #4 from Trust-Machines/GRNT-1113
Browse files Browse the repository at this point in the history
GRNT-1113: allow empty price updates with zero fees charged
  • Loading branch information
hackercf authored Dec 2, 2024
2 parents 5b83ed6 + a685a68 commit 83084a2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
11 changes: 9 additions & 2 deletions contracts/pyth-oracle-v2.clar
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@
(fee-info (contract-call? .pyth-governance-v1 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)
(if (> fee-amount u0)
(unwrap! (stx-transfer? fee-amount tx-sender (get address fee-info)) ERR_BALANCE_INSUFFICIENT)
true
)

(ok updated-prices))))

(define-public (decode-price-feeds
Expand All @@ -60,5 +64,8 @@
(fee-info (contract-call? .pyth-governance-v1 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)
(if (> fee-amount u0)
(unwrap! (stx-transfer? fee-amount tx-sender (get address fee-info)) ERR_BALANCE_INSUFFICIENT)
true
)
(ok decoded-prices))))
5 changes: 1 addition & 4 deletions contracts/pyth-store-v2.clar
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

(define-constant ERR_NEWER_PRICE_AVAILABLE (err u5000))
(define-constant ERR_STALE_PRICE (err u5001))
(define-constant ERR_INVALID_UPDATES (err u5003))
(define-constant ERR_RESTRICTED_TO_TESTNET (err u5004))
(define-constant ERR_RESTRICTED_TO_TESTNET (err u5003))

(define-constant STACKS_BLOCK_TIME u5)

Expand Down Expand Up @@ -62,8 +61,6 @@
(let ((successful-updates (map unwrapped-entry (filter only-ok-entry (map write-batch-entry batch-updates)))))
;; Ensure that updates are always coming from the right contract
(try! (contract-call? .pyth-governance-v1 check-execution-flow contract-caller none))
;; Ensure we have at least one entry
(asserts! (> (len successful-updates) u0) ERR_INVALID_UPDATES)
(ok successful-updates)))

(define-private (write-batch-entry (entry {
Expand Down
4 changes: 2 additions & 2 deletions unit-tests/pyth/pnau.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ describe("pyth-pnau-decoder-v1::decode-and-verify-price-feeds failures", () => {
expect(res.result).toBeErr(Cl.uint(2008));
});

it("should fail if the price is below stale threshold", () => {
it("should not update prices if the price is below stale threshold", () => {
let onChainTime = pyth.timestampNow() + BigInt(simnet.blockHeight);
simnet.mineEmptyBlocks(21); // stale threshold set to 10800 (3 hours), so by mining 21 blocks (1800s), we are advancing enough
let actualPricesUpdates = pyth.buildPriceUpdateBatch([
Expand Down Expand Up @@ -710,7 +710,7 @@ describe("pyth-pnau-decoder-v1::decode-and-verify-price-feeds failures", () => {
[Cl.buffer(pnau), executionPlan],
sender,
);
expect(res.result).toBeErr(Cl.uint(5003));
expect(res.result).toBeOk(Cl.list([]));
});

it("should only return validated prices and filter invalid prices", () => {
Expand Down

0 comments on commit 83084a2

Please sign in to comment.