跳到正文
DEV Community· rene·· 3 小時前AI 評分38

無狀態商店:以公開區塊鏈數據建立無後端電子商務結帳流程

The Stateless Store: Building a Backendless E-commerce Checkout on Public Blockchain Data

AI 導讀

這套無後端加密貨幣商店讓瀏覽器直接讀取 BNB Smart Chain(BSC)的 USDT 餘額核實付款,毋須後端、資料庫或 webhook。頁面每 20 秒輪詢餘額,訂單有效期為 60 分鐘,餘額增幅最多可低於報價 0.1%。付款須經人手核實後才交付;商店由 AI 智能體羣建構及營運。

正文

The Stateless Store: Building a Backendless E-commerce Checkout on Public Blockchain Data

The Problem Nobody Talks About

A friend who runs a one-person digital-goods shop once lost a weekend to a single support ticket. A customer had paid, the payment processor had confirmed it, but a webhook had silently failed and the order sat in "pending" in his database. The money was real. The record of it was not. He spent two days reconciling processor logs against his own tables before he could ship a file that took thirty seconds to send.

That is the hidden cost of the traditional checkout. The merchant's database is a copy of the truth, and copies drift. Webhooks get dropped, servers restart mid-write, and the merchant becomes a referee who has to be awake at 3 a.m. to confirm the score.

What if the store never kept a copy at all? What if it read the score directly from the one ledger that cannot drift, in the customer's own browser, with no server in between? That is the architecture behind our stateless crypto store: there is no backend, no database, and no webhook to lose. The blockchain is the only source of truth, and the checkout page simply reads it.


Technical Implementation

The flow replaces the usual write-to-database step with a read-from-blockchain polling loop. Everything below runs in the browser against a public BNB Smart Chain (BSC) RPC endpoint.

1. Querying the Balance with eth_call

The store accepts USDT as a BEP-20 token. A token balance is not a native field on the account, so we call the token contract's balanceOf(address) function. Its four-byte selector is 0x70a08231, followed by the merchant wallet address left-padded to 32 bytes. The RPC node executes the call locally and returns the raw integer balance. No transaction is sent, no gas is spent, and no server of ours is involved.

2. Capturing a Baseline

When the customer clicks "Pay", the page reads the merchant wallet's current USDT balance and stores it as the order's baseline. Everything afterwards is measured as a delta against this number.

3. Polling and Eventual Consistency

The page re-reads the balance every 20 seconds until the order is paid or expires. This interval balances responsiveness with minimizing unnecessary blockchain queries and accounts for eventual consistency in blockchain reads.

4. Comparison Logic

If the delta between the current and baseline balance reaches the quoted amount, the order is marked paid. We allow the delta to fall short by up to 0.1% to account for minor discrepancies in exchange fees or wallet rounding.

5. Time-to-Live

Each order lives for 60 minutes. After that, the page stops polling, discards the baseline, and asks the customer to start a new order.


Security Considerations

The design addresses several potential security concerns:

  • One Payment, Two Orders: Mitigated by appending a tiny random suffix to each quoted amount and manual verification by the owner on BscScan.

  • Client-Side Tampering: The store cannot ship anything without the owner's manual confirmation.

  • Fake Tokens: The store queries a hard-coded canonical USDT contract address.

  • RPC Poisoning: The poller uses multiple RPC endpoints and manual verification ensures accuracy.

  • Baseline Capture Race: Mitigated by reading the baseline twice, five seconds apart, and taking the lower value.


Design Trade-offs

  • No Backend: The entire store is static, reducing infrastructure complexity and costs.
  • Concurrent Buyers: The weakest point, mitigated by unique amount suffixes and manual verification.
  • Human in the Loop: Ensures accuracy and security, though it adds a delay.

AI-Assistance Disclosure

The store is built and operated by an AI agent swarm, with manual verification and delivery handled by a human.


Future Improvements

  • Event Listeners: Replace polling with WebSocket-based event listeners for real-time updates.
  • Per-Order Addresses: Eliminate concurrent buyer ambiguity by using unique deposit addresses.
  • Multi-Endpoint Quorum: Require agreement from multiple RPC providers before marking an order as paid.

Conclusion

This approach showcases the power of decentralized technologies in creating a stateless, transparent, and efficient payment verification system. By relying on the blockchain, we eliminate the need for a backend server and database, making the store more secure and user-friendly.

Try out the store yourself and experience the future of stateless e-commerce. Visit https://renvt89.github.io/crypto-store/ to see it in action.

來源:DEV Community · dev.to