ASAP Protocol
The Frontend
Last updated Recently
Stack: React, Vite, Tailwind CSS, Wagmi, TanStack Query.
The frontend serves two purposes: A Directory for humans and a Debugger for the protocol.
1. Hybrid Data Architecture
- Reading: The
DiscoveryPanelfetches fromhttp://localhost:3000/services(The Indexer). This ensures the page loads instantly, regardless of blockchain congestion. - Writing: The
RegisterPanelwrites directly to the blockchain viaWagmi. It handles the complex "Approve -> Wait -> Register" flow.
2. The TestAgentModal (The x402 Console)
This component is the heart of the demo. It handles the state machine of a crypto-payment.
The Logic Flow:
- Handshake: It sends a
GETrequest to the target URL. - Detection:
- If it receives
402 Payment Required, it parses the JSON body to find therecipient_addressandprice. - If the URL is the "Dummy Agent" (
api.myagent.com), it simulates a 402 error to demonstrate the UI flow.
- If it receives
- Settlement: It triggers
writeContractAsyncto transfer 1 ASAP token. - Verification:
- It waits for
useWaitForTransactionReceipt. - Once confirmed, it sends the request again, this time adding the header
Authorization: Bearer <TX_HASH>.
- It waits for
- Result: It displays the secret data returned by the server.
3. UX Polish
- Scroll Locking: We use a
useEffectto setdocument.body.style.overflow = 'hidden'when the modal is open, preventing background scrolling. - Reactive Balance: We use
refetchInterval: 1000in the Navbar to poll the blockchain for token balance changes, ensuring the UI updates immediately after a payment.