08/Selling advice
A clearance with a refund behind it
POST /x402/protected-execute sells a pre-trade clearance for an HBAR/USDC swap with a guaranteed maximum slippage; if the cleared swap slips more, POST /guarantee/claim checks the fill on the Mirror Node and refunds the fee in USDC on-chain. The clearance logic is buildClearance in apps/x402-gateway/src/clearance.ts; the claim is judged by evaluateGuaranteeClaim in packages/quant-kernel/src/guarantee.ts.
What a clearance checks
The gateway reads the live pool 0.0.3964804 from the SaucerSwap API (cached for 10 seconds) and prices your swap on it with estimateSwapImpact: the LP fee plus the average-price impact, which is exact inside the active liquidity range. The guarantee is then fixed as:
guaranteedMaxSlippageBps = ceil(min(your maxSlippageBps,
expected all-in cost + quote.maxSafeSlippageBps))The trade is cleared only if none of these apply:
| Reason | Why |
|---|---|
| Risk agent offline: no fresh toxicity read | The agent has not pushed within three poll intervals plus 30 s |
| Desk mandate inactive | The ENS mandate is paused, revoked or unverified |
| Toxic flow: wait or split into N clips | The regime is TOXIC (score 65 or more) |
| Order crosses multiple liquidity ranges: split the order | The trade would move the price more than 60 ticks |
| Expected all-in cost exceeds your tolerance | Fee plus impact is above your maxSlippageBps |
| Notional above guarantee cap | Notional over CLEARANCE_MAX_NOTIONAL_USD (default $1000) |
A clearance gets an id clr_ plus 20 hex characters, returned in the body and in the x-parity-clearance-id header, and is valid for CLEARANCE_TTL_MS (default 120000, two minutes). Once the x402 payment settles, the gateway binds the paying account to it and publishes parity.clearance.v1 to the P&L topic.
Claiming
- Execute the swap from the account that paid for the clearance, before
guarantee.validUntilMs. - Wait a few seconds for the Mirror Node, then
POST /guarantee/claimwith{ clearanceId, transactionId }. Both0.0.x@secs.nanosand0.0.x-secs-nanoswork. The gateway retries the Mirror Node lookup up to four times. - The gateway rebuilds your fill from the transaction record and compares it with the pool mid it recorded.
How the fill is judged
- The clearance must be OPEN and cleared, and the transaction must exist and be SUCCESS.
- The transaction's payer must be the clearance holder.
- Its consensus time must fall between the clearance's issue and expiry.
- It must be an HBAR to USD swap for that account: HBAR and USDC move in opposite directions. WHBAR (
0.0.1456986) counts as HBAR, and the network fee the account paid is netted out.
Realised slippage is measured against the pool mid at clearance time, so it includes the LP fee, the impact and any drift since. Above the guarantee means REFUNDED; within it means HONORED.
| Result | HTTP | What happens |
|---|---|---|
| REFUNDED | 200 | The fee is sent back in the asset and amount you paid, a GUARANTEE_REFUND cost is booked, refundTx is returned |
| HONORED | 200 | Nothing is owed; the clearance is closed |
| REJECTED | 422 | The claim is invalid; the clearance stays open for a valid claim |
| REFUNDED + error | 502 | The refund is owed but the transfer failed; the clearance is not closed |
| (no result) | 404 / 409 | Unknown clearance; payment not settled yet; or a claim already in progress |
Each clearance can be closed once. Every final result is published as parity.refund.v1 with the swap transaction, realised and guaranteed slippage, and the refund transaction if there was one.
The plugin wraps both steps in parity_check_swap_safety and parity_claim_guarantee; see Hedera Agent Kit plugin.