Credit cards used to give you money back when something you bought got cheaper a few weeks later. Most issuers quietly killed that perk. The reason wasn't fraud, it was cost: every claim needed a human to open a product page, find the right price, and compare it with a receipt.
That "look at a web page and decide" step is exactly what normal smart contracts can't do. So I built PriceGuard on GenLayer, where contracts are written in Python and can read the web and ask an LLM, with a set of validators who have to agree on the answer.
This post walks through how it works, the parts that were harder than I expected, and the code that keeps it honest.
- Live app: https://yoyoblair333-wq.github.io/priceguard/
- Source (MIT): https://github.com/yoyoblair333-wq/priceguard
The idea in one paragraph
An issuer (a card program, a fintech, a retailer) opens a plan and funds a pool. Purchases get registered: buyer, product page, item, variant, currency, price paid, date. During the protection window, anyone can call check on a purchase. Validators each read the product page, agree on today's price to the cent, and plain Python code refunds the drop from the pool. Nobody files a claim.
Why this needs GenLayer
A product page is messy. The Raspberry Pi 4 page I tested on prints the 4 GB price next to the 8 GB price, plus eight add-on prices and a row of "featured products". My demo lamp page has a struck-through "Was $89.00", a member price, an instalment price and a different finish that costs more.
An oracle that scrapes "the first dollar amount" gets this wrong constantly. An LLM can tell which number is the current selling price of this exact variant. GenLayer lets me use that judgement inside the contract, and the validators check each other's work.
The consensus pattern: leader + validator
GenLayer's run_nondet_unsafe takes two functions. The leader does the non-deterministic work. Every validator runs its own copy and decides whether to accept the leader's answer.
def leader_fn() -> dict: rec, det = read_price(item, variant, cur, url, fence) return {"record": rec, "detail": det} def validator_fn(leader_res) -> bool: if not isinstance(leader_res, gl.vm.Return): return False d = leader_res.calldata if not isinstance(d, dict) or not record_ok(d["record"]): return False mine, _det = read_price(item, variant, cur, url, fence) return mine == d["record"] # EXACT: status and price in cents res = gl.vm.run_nondet_unsafe(leader_fn, validator_fn) The record they compare is tiny on purpose:
{"status": "PRICED | UNAVAILABLE | AMBIGUOUS | UNREADABLE", "price_cents": 12000} No tolerance band. If validators don't land on the same status and the same cents, there's no payout. Out of stock, unclear and unreadable all pay nothing. It fails closed.
Lesson 1: never trust the model's number on its own
The model is good at picking the right price, but I don't let its answer through raw. Every validator reduces it with deterministic code against its own copy of the page:
def canonicalize(reply: dict, currency: str, text: str) -> dict: st = reply.get("status") if st == "UNREADABLE": return {"status": "UNREADABLE", "price_cents": 0} if st == "UNAVAILABLE": return {"status": "UNAVAILABLE", "price_cents": 0} if st != "PRICED": return {"status": "AMBIGUOUS", "price_cents": 0} cur = str(reply.get("currency", "")).strip().upper() cents = price_to_cents(reply.get("price")) if cur != currency or cents is None or cents <= 0: return {"status": "AMBIGUOUS", "price_cents": 0} if not price_on_page(cents, currency, text): return {"status": "AMBIGUOUS", "price_cents": 0} return {"status": "PRICED", "price_cents": cents} price_on_page requires the price to be printed on the page with the currency sign or code right next to it. So $1120.00 can never certify 120.00, and a bare 120.00 points doesn't count. If the model invents a number, it becomes AMBIGUOUS and pays nothing.
A few more cheap guards before the model is even asked:
- Script contents are stripped, so prices hidden in tracking code can't count.
- Every word of the item name must appear on the page, or it's
UNREADABLE. - The page goes between random fences in the prompt and is declared untrusted, so text on the page can't instruct the model.
Lesson 2: keep the money math out of the AI
Consensus decides one fact: today's price. Everything else is boring code:
def refund_cents(paid, lowest, coverage_bp, min_drop, cap): drop = paid - lowest if drop <= 0 or drop < min_drop: return 0 return min((drop * coverage_bp) // 10_000, cap) The contract tracks the lowest price ever seen, so a deeper drop later pays only the extra difference, the same drop never pays twice, and a price that goes back up never claws anything back.
Lesson 3: account in the unit you actually pay
My first version tracked refunds in cents. A reviewer-style test exposed a bug: if the pool ran low and paid only part of a refund, a later top-up could pay out more than the purchase was entitled to.
The fix was to do the accounting in wei actually paid:
entitled_wei = cents_to_wei(due, int(p.wei_per_usd)) owed_wei = entitled_wei - int(c.credited_wei) if owed_wei > 0: got = min(owed_wei, int(p.balance)) # ... pay `got`, add it to c.credited_wei There's a regression test for it (TestPartialPoolRefill): a 2-cent drop is worth 20 wei; the pool pays 5, gets refilled, and the total stays at 20. If you build anything with partial payouts, test this case.
What the live run showed
On GenLayer's studionet, against the real Adafruit Raspberry Pi 4 page and a clearly labelled demo store:
| Purchase | Validators read | Refund |
|---|---|---|
| Pi 4, 4 GB, paid $135 | PRICED $120.00 | $15.00 |
| Pi 4, 8 GB (same page), paid $199 | PRICED $190.00, not the 4 GB price | $9.00 |
| Pi 4, 4 GB, paid $120 | PRICED $120.00 | $0, no drop |
| Demo lamp, paid $89 | PRICED $69.00, ignoring the "was", member and instalment prices | $20.00 |
| Demo headphones, paid $149 | UNAVAILABLE (sold out) | $0 |
Every round reached consensus. The transaction hashes are in the repo's docs/LIVE_EVIDENCE.md.
Honest limits
- The price paid is attested by the issuer, who saw the receipt. The contract proves the current price.
- Sites that block bots or render prices only in JavaScript may come back
UNREADABLE, which pays nothing. - Testnet only, test tokens, not affiliated with any retailer.
Try it, or build your own
You can run the whole flow in your browser in about a minute: open the live app, click Create / load test key (free faucet), register the demo lamp, click Check price, and withdraw your $20 test refund.
If this pattern clicks for you, the fastest way to start is GenLayer Studio (zero setup, runs in the browser). GenLayer also runs a builder program on its portal that tracks contributions such as deployed contracts, projects and tutorials. If you want to join, you can sign up through my link: https://portal.genlayer.foundation/?ref=JTR01FHP. Full disclosure: it's a referral link, so I get credit if you join and ship something. It costs you nothing, and I'm happy to answer questions in the comments.
What would you hand to a validator jury next?
