<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: HaoJie Luo (Fone)</title>
    <description>The latest articles on DEV Community by HaoJie Luo (Fone) (@fonecc).</description>
    <link>https://dev.to/fonecc</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4218870%2Fc5f71046-ac5e-4eaf-a137-16dac62fba0f.png</url>
      <title>DEV Community: HaoJie Luo (Fone)</title>
      <link>https://dev.to/fonecc</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/fonecc"/>
    <language>en</language>
    <item>
      <title>What a 50% prediction score leaves out</title>
      <dc:creator>HaoJie Luo (Fone)</dc:creator>
      <pubDate>Mon, 23 Nov 2026 03:02:28 +0000</pubDate>
      <link>https://dev.to/fonecc/what-a-50-prediction-score-leaves-out-48cf</link>
      <guid>https://dev.to/fonecc/what-a-50-prediction-score-leaves-out-48cf</guid>
      <description>&lt;p&gt;An order can pay a researcher without proving that the submitted prediction was correct. Fone ProofDesk's inspected contract has an expired-window settlement branch that does exactly that. The branch finalizes an answered order using &lt;code&gt;answerCorrect=false&lt;/code&gt; and &lt;code&gt;revealed=false&lt;/code&gt;. Its reputation counters then include that order in both &lt;code&gt;totalFinalized&lt;/code&gt; and &lt;code&gt;incorrect&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A dashboard that labels &lt;code&gt;correct / totalFinalized&lt;/code&gt; as verified prediction accuracy would hide a material difference. A revealed answer checked against the designated result source and an unrevealed order settled after the deadline carry different evidence.&lt;/p&gt;

&lt;p&gt;The source review for this article followed those branches through the reputation update and score calculation. The downloadable exercise turns the distinction into an offline report with explicit denominators. It uses five synthetic orders and Python's standard library. No Polymarket connection, wallet or production research record is involved.&lt;/p&gt;

&lt;h2&gt;
  
  
  Define what each row proves
&lt;/h2&gt;

&lt;p&gt;The inspected workflow accepts price and sports questions. A successful reveal compares a committed structured answer against the applicable result. Correct answers settle; incorrect answers refund. An unanswered order can be cancelled after the answer deadline, while an answered order can follow the separate expiry path. Price expiry settlement additionally requires the stored result snapshot.&lt;/p&gt;

&lt;p&gt;Those behaviors give the report five categories. Verified correct and verified incorrect describe completed reveals. Expired unrevealed describes a finalized order without a revealed answer. Cancelled orders and open orders remain visible in the cohort, but neither enters the finalized denominator.&lt;/p&gt;

&lt;p&gt;The exercise receives a normalized order export. Its &lt;code&gt;verified_correct&lt;/code&gt; field is null when no verification exists. The contract's stored false value for an unrevealed result must therefore be interpreted together with the reveal flag when building that export. Copying the false value into a verified-outcome column would lose its meaning.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"Settled"&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;revealed&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;correct&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"verified_correct"&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"Refunded"&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;revealed&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;correct&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"verified_incorrect"&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"Settled"&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;revealed&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;correct&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"expired_unrevealed"&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This excerpt comes from the supplied &lt;code&gt;classify&lt;/code&gt; function. Its remaining branches accept open and cancelled orders only without reveal evidence. Conflicting combinations raise an exception. A settled, revealed, incorrect row should send the operator back to the export and source transaction for investigation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Run the small cohort
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://fone.cc/wp-content/uploads/2026/10/proofdesk-lab-v1.zip" rel="noopener noreferrer"&gt;Download the status-aware scorecard lab&lt;/a&gt;, extract it into an empty directory and run the following commands with Python 3.10 or later. The recorded run used Python 3.13.3; the minimum version was not separately tested.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python3 -m unittest discover -v
python3 scorecard.py fixture.json

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The fixture has one order in each of the five categories. All belong to a fictional demonstration researcher. The output includes the cohort description and cutoff alongside the report so a copied result retains its context.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Measure&lt;/th&gt;
&lt;th&gt;Numerator&lt;/th&gt;
&lt;th&gt;Denominator&lt;/th&gt;
&lt;th&gt;Fixture result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Contract-style correct share&lt;/td&gt;
&lt;td&gt;Verified correct&lt;/td&gt;
&lt;td&gt;All finalized&lt;/td&gt;
&lt;td&gt;3,333 basis points&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Verified subset correct share&lt;/td&gt;
&lt;td&gt;Verified correct&lt;/td&gt;
&lt;td&gt;Verified correct plus verified incorrect&lt;/td&gt;
&lt;td&gt;5,000 basis points&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Reveal coverage&lt;/td&gt;
&lt;td&gt;All verified&lt;/td&gt;
&lt;td&gt;All finalized&lt;/td&gt;
&lt;td&gt;6,666 basis points&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The integer values follow floor division. They correspond to approximately 33.33%, 50% and 66.66%. The first ratio explains the current counter semantics. The second describes only the revealed subset. The third tells the reader how much of the finalized cohort that subset covers.&lt;/p&gt;

&lt;p&gt;Show the counts beside those rates. One correct result out of two revealed orders offers little evidence for judging future performance. The exercise deliberately uses a small fixture to make every denominator inspectable. It provides no statistical ranking or probability of future success.&lt;/p&gt;

&lt;h2&gt;
  
  
  Preserve an empty denominator
&lt;/h2&gt;

&lt;p&gt;A cohort containing only expired unrevealed orders has no verified accuracy observation. Displaying zero percent would imply observed failures that do not exist. The helper returns null when the denominator is empty.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;bps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numerator&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;denominator&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;denominator&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;numerator&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;10000&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;denominator&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A frontend can render that null as “No verified outcomes” and retain the count of unrevealed settlements beside it. This is a reporting convention introduced by the lab. The contract's own score helper returns zero for an empty finalized cohort; the article does not propose silently changing that stored or computed behavior.&lt;/p&gt;

&lt;p&gt;The distinction also prevents a favorable verified-subset percentage from standing alone. If a researcher reveals only some answers, the subset does not establish results for the remaining orders. The report preserves unrevealed and open counts so an operator can inspect that gap. Detecting selective participation or collusive orders would require more evidence and rules than this exercise supplies.&lt;/p&gt;

&lt;h2&gt;
  
  
  Make retries visible before aggregation
&lt;/h2&gt;

&lt;p&gt;Each row carries a chain ID, escrow identity and order ID. The combination identifies an order in the fixture. An order numbered 1 on a second chain must remain distinct; importing order 1 twice from the same escrow must fail.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;python3&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="s1"&gt;'PY'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;pathlib&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;Path&lt;/span&gt;
&lt;span class="n"&gt;export&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;loads&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"fixture.json"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read_text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;export&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"orders"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;export&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"orders"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nc"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"duplicated.json"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write_text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dumps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;export&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="no"&gt;PY&lt;/span&gt;
&lt;span class="n"&gt;python3&lt;/span&gt; &lt;span class="n"&gt;scorecard&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;py&lt;/span&gt; &lt;span class="n"&gt;duplicated&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The command exits with code 2 and identifies the duplicate. Without that guard, repeating the correct row would change the verified-subset result from one out of two to two out of three. A dashboard could improve its displayed score solely because an import job retried a page.&lt;/p&gt;

&lt;p&gt;Rebuild the input with one final row per scoped order identity before running it again. The checker rejects duplicates even if their contents match, because identical repetition and conflicting historical versions should be resolved in the adapter. It does not choose a current version by file position.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fbz8vourrzicbf944v00i.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fbz8vourrzicbf944v00i.webp" alt="A research order cohort separates open and cancelled orders, then branches finalized orders into verified outcomes and expired unrevealed settlements." width="800" height="500"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Show verified-subset results beside reveal coverage so an unrevealed settlement does not become a verified failure.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Keep token volume in its own columns
&lt;/h2&gt;

&lt;p&gt;The contract's first reputation implementation adds raw payment amounts to &lt;code&gt;totalStake&lt;/code&gt;. Raw units from differently denominated tokens cannot be compared as economic volume without further interpretation. Our fixture deliberately includes a six-decimal test token and an eighteen-decimal test token.&lt;/p&gt;

&lt;p&gt;The report keeps finalized units grouped by token within each chain and escrow. It never adds those two balances into a single monetary figure. Verified-incorrect refunds still belong to finalized order volume under this definition, so that column also must not be labeled researcher revenue. Revenue would need settlement amounts, fees and refund treatment from the corresponding transactions.&lt;/p&gt;

&lt;p&gt;The tests cover those separate token totals, empty denominators and invalid reveal combinations. They also check duplicate imports, distinct chain identities, string values masquerading as booleans and unsupported question types. The original code runs entirely on supplied data.&lt;/p&gt;

&lt;p&gt;This report is a proposed companion exercise for &lt;a href="https://fone.cc/products/proofdesk/" rel="noopener noreferrer"&gt;Fone ProofDesk&lt;/a&gt;, whose current product foundation provides research orders, encrypted delivery, answer commitments and conditional settlement. It is not a shipped analytics integration. A project adopting it would still need a confirmed-block export, canonical address normalization and a completeness check over the chosen cohort. A valid file can omit inconvenient orders; arithmetic alone cannot prove completeness.&lt;/p&gt;

&lt;p&gt;For a research service acceptance review, request a sample order list containing both verified outcomes and an expired unrevealed settlement. Agree on the cohort and the labels before discussing a leaderboard. Those decisions determine what a customer can infer from the numbers.&lt;/p&gt;

&lt;p&gt;Source review covered &lt;code&gt;ConsultationEscrow.sol&lt;/code&gt; settlement and reputation paths on 29 October 2026, together with the ProofDesk product narrative and local audit. Exact paths and SHA-256 hashes are retained in draft metadata. The fixture contains no real consultants, customers or trading results.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://fone.cc/insights/prediction-research-status-aware-scorecard-python/?utm_source=devto&amp;amp;utm_medium=referral&amp;amp;utm_campaign=dev_proofdesk_scorecard_20261123" rel="noopener noreferrer"&gt;Read the original ProofDesk lab on Fone&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>web3</category>
      <category>testing</category>
      <category>datascience</category>
    </item>
    <item>
      <title>Versioning Music Licenses at Checkout</title>
      <dc:creator>HaoJie Luo (Fone)</dc:creator>
      <pubDate>Wed, 18 Nov 2026 08:35:34 +0000</pubDate>
      <link>https://dev.to/fonecc/versioning-music-licenses-at-checkout-k11</link>
      <guid>https://dev.to/fonecc/versioning-music-licenses-at-checkout-k11</guid>
      <description>&lt;p&gt;A buyer returns to a music platform to download a track licensed last month. The track page now shows revised terms. Which terms should the order display?&lt;/p&gt;

&lt;p&gt;The order needs the version the buyer accepted. Store the readable terms and the work identity with the accepted quote, then keep later eligibility and delivery decisions separately. A link to today's offer cannot reconstruct yesterday's purchase.&lt;/p&gt;

&lt;p&gt;This becomes especially useful in an AI music workflow, where the audio, artwork, collaborator agreement and permitted uses may change on different schedules. The data model needs to identify which versions were involved in each purchase.&lt;/p&gt;

&lt;h2&gt;
  
  
  Separate the work from the offer
&lt;/h2&gt;

&lt;p&gt;Start with the asset being licensed. A track title is a display label. It can stay the same while the audio file changes. Preserve a work version and a hash of the delivered asset, together with the terms that apply to it.&lt;/p&gt;

&lt;p&gt;The offer adds commercial information such as permitted uses, price and participating rights holders. The buyer then receives a quote. That quote needs enough information to explain the purchase later, even if the public offer is replaced.&lt;/p&gt;

&lt;p&gt;A practical record connects these objects.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Record&lt;/th&gt;
&lt;th&gt;Retained information&lt;/th&gt;
&lt;th&gt;Why it is useful later&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Work version&lt;/td&gt;
&lt;td&gt;Stable version ID and audio hash&lt;/td&gt;
&lt;td&gt;Identify the file associated with the purchase&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Quote&lt;/td&gt;
&lt;td&gt;Readable terms, terms hash, price, asset and expiry&lt;/td&gt;
&lt;td&gt;Reconstruct what was offered&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Acceptance&lt;/td&gt;
&lt;td&gt;Authenticated buyer, quote hash and terms hash&lt;/td&gt;
&lt;td&gt;Identify the version explicitly accepted&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Order&lt;/td&gt;
&lt;td&gt;Frozen quote snapshot and payment domain&lt;/td&gt;
&lt;td&gt;Match payment to the accepted purchase&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Credential&lt;/td&gt;
&lt;td&gt;Original work and terms, payment reference and effective period&lt;/td&gt;
&lt;td&gt;Explain the issued license without querying today's offer&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Current state&lt;/td&gt;
&lt;td&gt;Suspension, expiry or revocation with its history&lt;/td&gt;
&lt;td&gt;Decide whether another download is allowed now&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A hash helps detect a change when the original data and serialization rules are available. It cannot replace the readable terms. Back up the documents and assets needed to interpret the record.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bind acceptance to the quote
&lt;/h2&gt;

&lt;p&gt;Fone MusicFlow's current local implementation stores the quote snapshot and computes its &lt;code&gt;quoteHash&lt;/code&gt;. The purchase request supplies both &lt;code&gt;acceptedQuoteHash&lt;/code&gt; and &lt;code&gt;acceptedTermsHash&lt;/code&gt;. The server compares them with the stored quote, checks expiry and current sale eligibility, and copies that snapshot into the order.&lt;/p&gt;

&lt;p&gt;This makes an acceptance mismatch observable. If the screen displays one version while the request accepts another, the order creation should fail instead of silently substituting the latest terms. A refreshed quote should require fresh acceptance.&lt;/p&gt;

&lt;p&gt;Retries also need a stable meaning. MusicFlow checks the buyer and request identifier so an identical retry returns the existing order. A different payload under the same identifier is rejected. A quote can only be consumed by one order. These are source-reviewed behaviors in the local implementation; this article does not report a new end-to-end payment run.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7cocu9bfumaetlst8uph.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7cocu9bfumaetlst8uph.webp" alt="A frozen quote and explicit acceptance lead to an order snapshot and credential, while current access state changes separately." width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Keep the original purchase record readable when the current delivery decision changes.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Preserve the record when the catalog changes
&lt;/h2&gt;

&lt;p&gt;The current credential-building path takes the work, terms and agreement from the accepted order snapshot. It adds the verified payment reference and effective period. A later public-page edit should not cause an older credential to be rebuilt from the newest catalog entry.&lt;/p&gt;

&lt;p&gt;Two questions then have separate answers. What did the buyer accept? May this account download the asset now? A dispute or expiry may block a new download while the original record remains available to explain the purchase.&lt;/p&gt;

&lt;p&gt;MusicFlow's documented local flow follows this separation. Removing a work from sale stops new sales and public audio, while an eligible existing buyer can still request the original version. Delivery checks the original asset hash and current entitlement. The implementation cannot withdraw bytes that someone has already downloaded.&lt;/p&gt;

&lt;p&gt;If your product needs a different policy, specify it explicitly. A corrected file, a newly negotiated license and a refunded purchase are different events. Keeping their history is more useful than overwriting one status field and losing the reason for the change.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try the version-change exercise
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://fone.cc/wp-content/uploads/2026/10/music-license-versions-kit-v1.zip" rel="noopener noreferrer"&gt;Download the snapshot exercise and review checklist&lt;/a&gt;. It contains a small Python example with synthetic records. The tests change the current offer after acceptance, reject mismatched acceptance hashes, check retries and preserve the old record when access is suspended.&lt;/p&gt;

&lt;p&gt;The exercise uses in-memory objects to make the version relationship visible. It does not implement authentication, durable storage, concurrent purchases or payment verification. A real checkout still needs database constraints and a transaction around order acceptance. The &lt;a href="https://www.sqlite.org/lang_transaction.html" rel="noopener noreferrer"&gt;SQLite transaction documentation&lt;/a&gt; is useful background for teams adapting the idea to a small relational implementation.&lt;/p&gt;

&lt;p&gt;Use the checklist to decide what your own system must retain. In particular, decide who can read private project details and whether support staff can explain a historical order without seeing unrelated buyer information. A public credential page should not expose a buyer's private project reference by default.&lt;/p&gt;

&lt;p&gt;Technical excerpt from &lt;a href="https://fone.cc/insights/versioning-music-licenses-checkout/?utm_source=devto&amp;amp;utm_medium=referral&amp;amp;utm_campaign=music_license_versions_20261118" rel="noopener noreferrer"&gt;Versioning Music Licenses at Checkout&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>python</category>
    </item>
    <item>
      <title>Build a Node.js Audio Server with HTTP Range Requests</title>
      <dc:creator>HaoJie Luo (Fone)</dc:creator>
      <pubDate>Mon, 16 Nov 2026 02:21:20 +0000</pubDate>
      <link>https://dev.to/fonecc/build-a-nodejs-audio-server-with-http-range-requests-3g17</link>
      <guid>https://dev.to/fonecc/build-a-nodejs-audio-server-with-http-range-requests-3g17</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://fone.cc/insights/nodejs-audio-http-range-requests/?utm_source=devto&amp;amp;utm_medium=referral&amp;amp;utm_campaign=devto_mf_t01_20261112" rel="noopener noreferrer"&gt;Fone&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;A track starts playing, but moving the playhead sends it back to the beginning. Before replacing the player library, inspect the request made when you seek. Does the browser ask for a byte range? Does the server return those bytes with a matching &lt;code&gt;Content-Range&lt;/code&gt; header?&lt;/p&gt;

&lt;p&gt;This exercise builds a small Node.js audio endpoint that you can inspect with curl. It serves a locally generated WAV, handles single byte ranges and checks a private route before revealing file metadata. You will get a working server and an automated HTTP test suite. No music provider account, wallet or paid API is needed.&lt;/p&gt;

&lt;p&gt;I used Fone MusicFlow's media handler as the starting point for the problem. Its source includes byte-range delivery, streaming of public media, publication checks and a separate fixed-version license download path. The example below is an independent teaching implementation. It keeps the HTTP behavior small enough to inspect without installing the full product.&lt;/p&gt;

&lt;h2&gt;
  
  
  Start with a file whose bytes you control
&lt;/h2&gt;

&lt;p&gt;Download the &lt;a href="https://fone.cc/wp-content/uploads/2026/10/nodejs-audio-http-range-lab-v1.zip" rel="noopener noreferrer"&gt;audio range lab&lt;/a&gt; and extract it into an empty directory. The package contains &lt;code&gt;tone.mjs&lt;/code&gt;, &lt;code&gt;server.mjs&lt;/code&gt;, &lt;code&gt;server.test.mjs&lt;/code&gt; and a README. It was tested with Node.js 24.0.1 on macOS. It has no npm dependencies.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;node tone.mjs
node --test server.test.mjs
node server.mjs tone.wav 8787
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first command creates a three-second, mono PCM WAV containing a quiet synthetic tone. Its size is &lt;strong&gt;132,344 bytes&lt;/strong&gt;, including the 44-byte header. This is test audio, not an AI-generated track or a MusicFlow quality sample. The command refuses to overwrite an existing file.&lt;/p&gt;

&lt;p&gt;The server binds to &lt;code&gt;127.0.0.1&lt;/code&gt;. Leave it running and use another terminal for the requests below. If the port is busy, pass a different port as the last argument and use that number in each URL.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl -sS -D - -o /dev/null http://127.0.0.1:8787/sample.wav
curl -sS -D - -o first-part.bin \
  -H &lt;span class="s1"&gt;'Range: bytes=44-143'&lt;/span&gt; http://127.0.0.1:8787/sample.wav
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first response is &lt;code&gt;200&lt;/code&gt; with the full length. The second is &lt;code&gt;206&lt;/code&gt;, with &lt;code&gt;Content-Length: 100&lt;/code&gt; and &lt;code&gt;Content-Range: bytes 44-143/132344&lt;/code&gt;. Both endpoints describe the same representation. The range response contains exactly the requested part of it.&lt;/p&gt;

&lt;p&gt;The end position is inclusive. Subtracting &lt;code&gt;start&lt;/code&gt; from &lt;code&gt;end&lt;/code&gt; without adding one produces a header/body disagreement that can be surprisingly difficult to diagnose through a player UI.&lt;/p&gt;

&lt;h2&gt;
  
  
  Select a range before opening the stream
&lt;/h2&gt;

&lt;p&gt;Here is the range selector used by the downloadable server. It supports a closed interval, an open-ended interval and a suffix. Large values are compared as &lt;code&gt;BigInt&lt;/code&gt; before converting valid file offsets to numbers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;selectRange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;header&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;full&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;header&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;full&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;match&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;/^&lt;/span&gt;&lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;\&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;\&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;$&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;header&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;match&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;match&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;match&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;full&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;BigInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;match&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;suffix&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;BigInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;match&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;suffix&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0n&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;416&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;suffix&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;0n&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;suffix&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;BigInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;match&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;match&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nc"&gt;BigInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;match&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;match&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;full&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Invalid byte-range syntax.&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;416&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;206&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A valid range starting at the file size has no bytes available and gets &lt;code&gt;416&lt;/code&gt;. A very large end position can still be satisfied by returning through the actual end of the file. A suffix of zero has no satisfiable bytes.&lt;/p&gt;

&lt;p&gt;This server deliberately ignores multipart ranges and malformed range syntax, returning the full representation with &lt;code&gt;200&lt;/code&gt;. That choice is documented and tested. It does not claim to implement multipart byte-range responses. &lt;a href="https://www.rfc-editor.org/rfc/rfc9110.html#name-range" rel="noopener noreferrer"&gt;HTTP semantics permits a server to ignore Range&lt;/a&gt;; clients need to handle that possibility.&lt;/p&gt;

&lt;p&gt;The HTTP handler then opens only the selected interval. When the client disconnects, it destroys the file stream.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;stream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createReadStream&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;selected&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;selected&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'close'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;destroy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'error'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;destroy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Keep the representation stable during the request. This lab assumes that &lt;code&gt;tone.wav&lt;/code&gt; is not replaced or edited while the server runs. A production media store needs versioned objects or equivalent coordination so the length checked before reading remains the length being served.&lt;/p&gt;

&lt;h2&gt;
  
  
  Check the cases a successful play button misses
&lt;/h2&gt;

&lt;p&gt;Run these requests in the second terminal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Last ten bytes&lt;/span&gt;
curl -sS -D - -o /dev/null -H &lt;span class="s1"&gt;'Range: bytes=-10'&lt;/span&gt; \
  http://127.0.0.1:8787/sample.wav

&lt;span class="c"&gt;# Outside the file&lt;/span&gt;
curl -sS -D - -o /dev/null -H &lt;span class="s1"&gt;'Range: bytes=132344-'&lt;/span&gt; \
  http://127.0.0.1:8787/sample.wav

&lt;span class="c"&gt;# HEAD describes the full representation and sends no body&lt;/span&gt;
curl -sS -I -H &lt;span class="s1"&gt;'Range: bytes=0-9'&lt;/span&gt; \
  http://127.0.0.1:8787/sample.wav
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Expected results are &lt;code&gt;206&lt;/code&gt; with ten bytes, &lt;code&gt;416&lt;/code&gt; with &lt;code&gt;Content-Range: bytes */132344&lt;/code&gt;, and &lt;code&gt;200&lt;/code&gt; with the full length for HEAD. The sample ignores Range on HEAD because RFC 9110 defines Range processing for GET. These are the tutorial's verified semantics; they should not be read as a claim that every detail of MusicFlow's current handler behaves identically.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ff5nsyeeyxs1eqg1t6ust.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ff5nsyeeyxs1eqg1t6ust.webp" alt="An audio request passes authorization before range selection and a bounded file stream" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Authorize before answering a private range request
&lt;/h2&gt;

&lt;p&gt;A private audio route should make its access decision before returning the file size, media type or requested bytes. Changing &lt;code&gt;200&lt;/code&gt; to &lt;code&gt;206&lt;/code&gt; must not create a second path around that decision.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# No credential, even though the range is outside the file&lt;/span&gt;
curl -sS -D - -o /dev/null -H &lt;span class="s1"&gt;'Range: bytes=999999999-'&lt;/span&gt; \
  http://127.0.0.1:8787/private.wav

&lt;span class="c"&gt;# Deliberately public test credential for the localhost exercise&lt;/span&gt;
curl -sS -D - -o /dev/null -H &lt;span class="s1"&gt;'Authorization: Bearer demo-owner'&lt;/span&gt; \
  -H &lt;span class="s1"&gt;'Range: bytes=0-9'&lt;/span&gt; http://127.0.0.1:8787/private.wav
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first request returns a generic &lt;code&gt;404&lt;/code&gt; without &lt;code&gt;Content-Range&lt;/code&gt;. The second returns ten bytes. The fixed token demonstrates ordering only. A deployed system needs an authenticated user and an entitlement check; putting this token into a public application would provide no meaningful protection.&lt;/p&gt;

&lt;p&gt;Responses use &lt;code&gt;private, no-store&lt;/code&gt; in this lab. Public previews and purchased files often need different cache policies, but decide who may receive each object before optimizing cache hits. HTTP authorization can restrict delivery. It cannot prevent an authorized listener from recording audio they receive.&lt;/p&gt;

&lt;h2&gt;
  
  
  What this proves, and what to test in your player
&lt;/h2&gt;

&lt;p&gt;Fourteen automated tests passed against a fresh extraction of the final download. They compare returned bytes, exercise suffix and open-ended requests, check HEAD and authorization, and confirm that the server remains usable after response cancellation. This is HTTP-level evidence on the stated local runtime. It is not a browser compatibility or CDN test.&lt;/p&gt;

&lt;p&gt;For a real player, repeat seeking on the browsers you support and inspect the network panel. A server that returns the correct range can still be paired with an unsupported codec, a stale cached representation or a proxy that changes delivery behavior. Use a known playable file first, then vary one part of that path at a time.&lt;/p&gt;

&lt;p&gt;For a buyer commissioning a music platform, a useful acceptance case is specific. Seek within a published preview, request an unpublished file without a session, and download the version associated with an authorized purchase. Record both allowed and denied responses. A screenshot of a working player covers only the first few seconds of that exercise.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://fone.cc/products/musicflow/" rel="noopener noreferrer"&gt;Fone MusicFlow&lt;/a&gt; brings together creation, discovery, license orders and media delivery. Its inspected media code provides the concrete connection for this tutorial. Its current economic demonstration uses local test-chain and mock-generation flows; this lab makes no claim about live provider quality or production readiness. If your project needs licensed downloads, the next related design is &lt;a href="https://fone.cc/insights/versioning-music-licenses-checkout/" rel="noopener noreferrer"&gt;preserving the music license version accepted at checkout&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.rfc-editor.org/rfc/rfc9110.html#name-range" rel="noopener noreferrer"&gt;HTTP range request semantics&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://nodejs.org/docs/latest-v24.x/api/fs.html#fscreatereadstreampath-options" rel="noopener noreferrer"&gt;Node.js file streams&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://fone.cc/wp-content/uploads/2026/10/nodejs-audio-http-range-lab-v1.zip" rel="noopener noreferrer"&gt;Download the complete server and tests&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Prepared with AI assistance. The source wording and code are preserved.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>node</category>
      <category>webdev</category>
      <category>tutorial</category>
      <category>testing</category>
    </item>
    <item>
      <title>Build Exact RWA Subscription and Redemption Quotes in Python</title>
      <dc:creator>HaoJie Luo (Fone)</dc:creator>
      <pubDate>Fri, 13 Nov 2026 02:15:55 +0000</pubDate>
      <link>https://dev.to/fonecc/build-exact-rwa-subscription-and-redemption-quotes-in-python-ene</link>
      <guid>https://dev.to/fonecc/build-exact-rwa-subscription-and-redemption-quotes-in-python-ene</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://fone.cc/insights/rwa-integer-subscription-redemption-quotes-python/?utm_source=devto&amp;amp;utm_medium=referral&amp;amp;utm_campaign=devto_rw_l01_20261112" rel="noopener noreferrer"&gt;Fone&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;One unit priced at 1.005 currency units cannot be represented exactly in a currency with two decimal places. An investor portal needs an explicit rounding rule before it can quote the amount due or the redemption payment.&lt;/p&gt;

&lt;p&gt;Aurum RWA's current subscription model rounds gross amounts up to the currency's smallest unit. Its redemption model rounds gross amounts down. Both round the proportional fee up, then add a fixed fee. Subscription fees increase the amount payable; redemption fees reduce the amount to settle. Those rules were checked in the local source on 29 October 2026.&lt;/p&gt;

&lt;p&gt;This lab turns those arithmetic choices into a runnable Python calculator and a quote acceptance check. It uses synthetic prices and investor identifiers. You will calculate both directions, reject a changed quote, and test amounts larger than ordinary floating-point integers can reliably represent. &lt;a href="https://fone.cc/wp-content/uploads/2026/10/rwa-integer-subscription-redemption-quotes-python-lab-v1.zip" rel="noopener noreferrer"&gt;Download the code, tests and README&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Run the two directions
&lt;/h2&gt;

&lt;p&gt;The archive needs Python only. Verification used Python 3.13.3 with its standard library.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python3 quotes.py
python3 -m unittest -v
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The demo uses 100 raw units with two token decimals, which represents one whole token. The price is the string &lt;code&gt;1.005&lt;/code&gt;, the currency has two decimals, the fixed fee is one minor unit, and the proportional fee is one basis point. The values are deliberately small enough to expose rounding.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Operation&lt;/th&gt;
&lt;th&gt;Gross minor units&lt;/th&gt;
&lt;th&gt;Fee minor units&lt;/th&gt;
&lt;th&gt;Net minor units&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Subscription&lt;/td&gt;
&lt;td&gt;101&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;103&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Redemption&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;98&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The five-unit difference between subscription payable and redemption receivable follows from this synthetic fee schedule and the two gross rounding rules. It is not a return estimate. The example performs no purchase, redemption or bank transfer.&lt;/p&gt;

&lt;p&gt;In a portal, these amounts deserve separate labels. An investor can check the quoted asset value, the service charge and the amount that changes hands. A single unlabeled total leaves operations staff reconstructing the calculation whenever somebody asks about a difference.&lt;/p&gt;

&lt;h2&gt;
  
  
  Keep decimal input as text
&lt;/h2&gt;

&lt;p&gt;The calculator accepts the price as a plain decimal string. It rejects exponent notation, negative values and excess precision. That keeps parsing choices visible at the boundary rather than silently rounding an input before the calculation starts.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;scaled&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;scale&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="nb"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fullmatch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="s2"&gt;"(?:0|[1-9][0-9]*)(?:\.[0-9]+)?"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"plain decimal string required"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;whole&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fraction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;partition&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fraction&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;scale&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"excess precision"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;whole&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;scale&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fraction&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ljust&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scale&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"0"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="s2"&gt;"0"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The parser creates a scaled integer using eighteen fractional places. Token quantity and currency scale remain separate. A quantity of 100 means something different for a token with two decimals than for one with six, even when the displayed price is identical.&lt;/p&gt;

&lt;p&gt;Python integers let this teaching program retain arbitrarily large intermediate values. A deployed integration still needs storage limits and input bounds appropriate to its database and contracts. The Aurum source validates its resulting monetary strings through its own money parser. The downloadable example does not reproduce every product constraint or database column.&lt;/p&gt;

&lt;h2&gt;
  
  
  Make rounding direction a named choice
&lt;/h2&gt;

&lt;p&gt;The calculator takes &lt;code&gt;subscription&lt;/code&gt; or &lt;code&gt;redemption&lt;/code&gt; explicitly. It computes the quotient and remainder of the gross amount, then increments the quotient only when a subscription has a remainder.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;quote&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;units&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wallet&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;kind&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"subscription"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"redemption"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"unknown kind"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;natural&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;units&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="nb"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wallet&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;wallet&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"positive units and wallet required"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;token_decimals&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;currency_scale&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fixed_minor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                  &lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fee_bps&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;valid_from&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;expires_at&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;natural&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;token_decimals&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;currency_scale&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fee_bps&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"unsupported scale or fee"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;expires_at&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;valid_from&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"invalid validity interval"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;numerator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;units&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;scaled&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;currency_scale&lt;/span&gt;
    &lt;span class="n"&gt;denominator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;token_decimals&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;gross&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;remainder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;divmod&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numerator&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;denominator&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;kind&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"subscription"&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;remainder&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;gross&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="n"&gt;fee&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fixed_minor&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gross&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fee_bps&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;9999&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="mi"&gt;10000&lt;/span&gt;
    &lt;span class="n"&gt;net&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;gross&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;fee&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;kind&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"subscription"&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;gross&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;fee&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;gross&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;net&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"nonpositive amount"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;snapshot&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"terms"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;asdict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"kind"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"units"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;units&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="s2"&gt;"wallet"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;wallet&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"gross_minor"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gross&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="s2"&gt;"fee_minor"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fee&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"net_minor"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;net&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;encoded&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dumps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;snapshot&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sort_keys&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;separators&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;","&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;":"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;snapshot&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"hash"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;sha256&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;encoded&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hexdigest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The proportional fee uses integer ceiling division by 10,000. A nonzero fractional fee therefore rounds to at least one minor unit. That is why one basis point on a gross amount of 101 produces one minor unit before the fixed fee is added.&lt;/p&gt;

&lt;p&gt;For redemption, the program rejects a fee that consumes the entire gross amount. Returning zero or a negative payout would leave the next stage with an invalid settlement obligation. The product's redemption model likewise checks that gross exceeds the fee.&lt;/p&gt;

&lt;p&gt;The returned snapshot stores gross, fee and net as strings. A later JavaScript client can display or parse them deliberately without receiving a large JSON number that may already have lost precision. The model also carries token decimals, currency scale and the original pricing inputs inside its terms record.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F3mtcag0div6y0t5jpa9j.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F3mtcag0div6y0t5jpa9j.webp" alt="Raw units and versioned terms produce rounded amounts that pass a fresh acceptance check" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Recompute from authoritative terms
&lt;/h2&gt;

&lt;p&gt;A quote hash identifies a particular snapshot. Anyone who can edit a snapshot can also calculate a new unkeyed hash. The hash in this exercise therefore serves as a stable fingerprint; acceptance relies on recomputing the full quote from terms supplied by the server-side caller.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;snapshot&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;authoritative_terms&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;units&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wallet&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;natural&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;authoritative_terms&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;valid_from&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;authoritative_terms&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;expires_at&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"quote outside validity window"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;expected&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;quote&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;authoritative_terms&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;units&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wallet&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;snapshot&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;expected&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"quote changed; review again"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;expected&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"net_minor"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The check binds the accepted result to direction, wallet and quantity. It also compares every term in the current version, including the price, fees and document version. A stale browser cannot turn an old snapshot into a current offer by changing only the displayed total.&lt;/p&gt;

&lt;p&gt;Time comes from an explicit &lt;code&gt;now&lt;/code&gt; argument so tests can exercise the boundary precisely. The valid interval includes its start and excludes its end. A quote expiring at 200 is accepted at 199 and rejected at 200. An application should supply that time from its trusted service, rather than accept the customer's browser clock.&lt;/p&gt;

&lt;p&gt;The product's subscription snapshot expires at the earlier of quote validity and offering end. The lab takes one effective expiry as an input, so an adapter must compute that effective time before creating &lt;code&gt;Terms&lt;/code&gt;. Its SHA-256 format is original teaching code and is not compatible with Aurum's wire format or typed authorizations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Verify money with an independent calculation
&lt;/h2&gt;

&lt;p&gt;Twelve named tests passed after extracting the ZIP into a fresh temporary directory. The arithmetic grid checks 300 quantities in both directions against Python's exact &lt;code&gt;Fraction&lt;/code&gt; type. That gives the integer implementation a separate mathematical reference for gross rounding and fees.&lt;/p&gt;

&lt;p&gt;A large-value case uses &lt;code&gt;2**60 + 1&lt;/code&gt; raw units with a price of one and zero fees. The expected result retains that final one. Other tests reject altered totals, changed prices and a different wallet. The expiry test checks both sides of the validity interval, while the fee test rejects a redemption whose net would be nonpositive.&lt;/p&gt;

&lt;p&gt;Try changing the fee rounding expression to ordinary truncating division. The small subscription case should fail because its fee drops from two minor units to one. Try rounding redemption gross up and the paired fixture should fail as well. These mutations make the acceptance criteria observable instead of relying on a reviewer recognizing the formula.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use the calculator in an implementation review
&lt;/h2&gt;

&lt;p&gt;The earlier &lt;a href="https://fone.cc/insights/rwa-redemption-offchain-settlement/" rel="noopener noreferrer"&gt;RWA redemption article&lt;/a&gt; follows the workflow from locked units to settlement evidence. This exercise concentrates on the numeric obligation that enters that workflow. Quote correctness and payment completion need separate checks.&lt;/p&gt;

&lt;p&gt;For a team evaluating &lt;a href="https://fone.cc/products/aurum-rwa/" rel="noopener noreferrer"&gt;Aurum RWA&lt;/a&gt;, bring one proposed asset denomination, currency and fee schedule to the walkthrough. Ask the team to show the smallest permitted subscription, the smallest viable redemption and a quote that expires while the investor is reviewing it. Compare the displayed numbers with an independently calculated fixture before connecting payment or identity services.&lt;/p&gt;

&lt;p&gt;Aurum supplies software for the investor and operations workflow. Asset rights and participation conditions belong in the project's actual documents. This calculator verifies a narrow set of software rules and provides no evidence about asset value, regulatory eligibility or completed payment. Its useful output is a set of exact amounts and failing cases that your integration team can carry into acceptance testing.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Prepared with AI assistance. The source wording and code are preserved.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>web3</category>
      <category>python</category>
      <category>testing</category>
      <category>programming</category>
    </item>
    <item>
      <title>Cancel Stale Wallet Prompts with Async Generation Checks</title>
      <dc:creator>HaoJie Luo (Fone)</dc:creator>
      <pubDate>Fri, 13 Nov 2026 01:50:10 +0000</pubDate>
      <link>https://dev.to/fonecc/cancel-stale-wallet-prompts-with-async-generation-checks-3jb0</link>
      <guid>https://dev.to/fonecc/cancel-stale-wallet-prompts-with-async-generation-checks-3jb0</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://fone.cc/insights/cancel-stale-wallet-prompts-async-generation-checks/?utm_source=devto&amp;amp;utm_medium=referral&amp;amp;utm_campaign=devto_gu_l01_20261112" rel="noopener noreferrer"&gt;Fone&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;A canceled authorization review should never open a wallet prompt a moment later. That can happen when a page waits for simulation or an account check, the user cancels, and the old asynchronous function resumes with values it captured before cancellation.&lt;/p&gt;

&lt;p&gt;This lab builds a small asynchronous controller that reproduces that race and prevents the stale prompt. The result is a Python program with deterministic tests, including cancellation during the final wallet read. It uses fake callbacks and contains no signing keys, RPC calls or transactions. &lt;a href="https://fone.cc/wp-content/uploads/2026/10/cancel-stale-wallet-prompts-async-generation-checks-lab-v1.zip" rel="noopener noreferrer"&gt;Download the complete runnable lab&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The engineering question comes from Agent Wallet Guard's Owner authorization flow. A source review on 29 October 2026 found a generation check after simulation and another after the final wallet checks, immediately before the transaction path. Its historical local acceptance report also records a cancellation fix. Here we isolate that narrow behavior so you can inspect the timing without running the application stack.&lt;/p&gt;

&lt;h2&gt;
  
  
  Run the race locally
&lt;/h2&gt;

&lt;p&gt;Use Python 3.13.3, the version used for this lab's verification. There are no packages to install. Extract the ZIP, open its directory and run the following commands.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python3 preflight.py
python3 -m unittest -v
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The demo pauses simulation using an event, cancels the review, then releases simulation. Its output reports &lt;code&gt;stale&lt;/code&gt;, zero wallet prompts and the &lt;code&gt;idle&lt;/code&gt; phase. No sleep duration determines the result. The test controls exactly when the delayed work is allowed to finish.&lt;/p&gt;

&lt;p&gt;That detail matters during debugging. A test that waits ten milliseconds and hopes cancellation wins may pass on one machine and fail on another. An event gives the test a specific observation point. The simulated preflight has started, its result has not returned, and cancellation happens inside that interval.&lt;/p&gt;

&lt;h2&gt;
  
  
  Capture what the Owner actually reviewed
&lt;/h2&gt;

&lt;p&gt;The controller uses a frozen context containing an Owner label, chain ID, deployment label and Session label. These are synthetic identifiers. An integration would use the actual account and contract context required by its authorization protocol.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nd"&gt;@dataclass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;frozen&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;
    &lt;span class="n"&gt;chain&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
    &lt;span class="n"&gt;deployment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;
    &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A chain ID alone cannot describe the entire review. Two deployments can use the same local chain ID, while a page can switch to another Session without changing the connected account. Comparing all four fields lets the exercise detect those separate changes.&lt;/p&gt;

&lt;p&gt;Each review also receives an increasing generation number. Canceling a review advances that number. Old work can still finish, but it no longer owns the right to open a prompt or reset the current interface. This distinction lets the page begin a new review while an older request is still returning.&lt;/p&gt;

&lt;p&gt;The number is local control state. It does not travel to a contract and does not grant spending permission. Agent Wallet Guard's actual Session validation remains a separate part of execution. The existing &lt;a href="https://fone.cc/insights/ai-agent-wallet-spending-limit-enforcement/" rel="noopener noreferrer"&gt;spending limit article&lt;/a&gt; explains that other boundary.&lt;/p&gt;

&lt;h2&gt;
  
  
  Check again after the last wait
&lt;/h2&gt;

&lt;p&gt;The following method is the complete prompt gate from the downloadable controller. &lt;code&gt;simulate&lt;/code&gt;, &lt;code&gt;read_wallet&lt;/code&gt; and &lt;code&gt;prompt&lt;/code&gt; are injected callbacks, which lets the test stop each stage independently.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;confirm&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ticket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;simulate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;read_wallet&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;generation&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;reviewed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ticket&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;generation&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;generation&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;phase&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s2"&gt;"review"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"stale"&lt;/span&gt;
    &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;phase&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"preflight"&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;simulate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;reviewed&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;generation&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;generation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"stale"&lt;/span&gt;
        &lt;span class="n"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;read_wallet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;generation&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;generation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"stale"&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;reviewed&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;reviewed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;phase&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"idle"&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"context_changed"&lt;/span&gt;
        &lt;span class="c1"&gt;# There is no await between the final guard and entering wallet phase.&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;phase&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"wallet"&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;reviewed&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;phase&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"submitted"&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"submitted"&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;generation&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;generation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="c1"&gt;# Once invoked, a failed provider response may hide a broadcast.&lt;/span&gt;
            &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;phase&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"uncertain"&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;phase&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"wallet"&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="s2"&gt;"idle"&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first check rejects a duplicate confirm call as soon as another call enters preflight. The next check runs after simulation. A further check runs after reading the wallet, because cancellation could have happened during that read too. Passing the earlier check gives no guarantee about later suspended work.&lt;/p&gt;

&lt;p&gt;Immediately before invoking the prompt, the code compares the returned context with the reviewed context and the controller's current context. It then moves into the wallet phase without another wait between the final guard and that transition. Within this single asyncio event loop, another task cannot slip a cancellation into that synchronous segment.&lt;/p&gt;

&lt;p&gt;This is a control-flow guarantee inside the teaching model. A browser wallet and a chain remain separate systems. A real integration must also handle provider account changes and validate the actual transaction. The small controller helps reviewers locate one race; it cannot prove every race in those other systems is absent.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fr0s6rf4ddcvhfnbqrqff.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fr0s6rf4ddcvhfnbqrqff.webp" alt="A canceled review invalidates delayed preflight work before it reaches the wallet prompt" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Test the latest possible cancellation
&lt;/h2&gt;

&lt;p&gt;The most revealing test pauses the final wallet read. Simulation has already passed, which catches an implementation that checks cancellation only after simulation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_cancel_during_last_wallet_read_blocks_prompt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;entered&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;release&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Event&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Event&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;slow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;entered&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;release&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;wait&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;
    &lt;span class="n"&gt;task&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create_task&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run_ticket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;flow&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;review&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wallet&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;slow&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;entered&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;wait&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;flow&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cancel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;release&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;assertEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;task&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"stale"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;assertEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;prompts&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run this test alone while changing the controller. If you remove the generation check after &lt;code&gt;read_wallet&lt;/code&gt;, the old continuation can incorrectly open a prompt when the returned context still matches. The test expects zero prompts and therefore fails on the unsafe behavior.&lt;/p&gt;

&lt;p&gt;Another test delays an old simulation until a fresh review exists, then makes the old simulation fail. The exception handler checks the generation before changing the phase. Without that condition, a late error from the abandoned attempt could reset the new review to idle. This case often gets missed when cancellation is tested only on successful requests.&lt;/p&gt;

&lt;p&gt;The suite also changes each context field independently, submits the same ticket twice, and checks a preflight exception. Ten named tests passed from a fresh extraction of the archive. These are tests of the teaching controller; the product's application and contract suites were not rerun for this article.&lt;/p&gt;

&lt;h2&gt;
  
  
  Decide where cancellation stops
&lt;/h2&gt;

&lt;p&gt;Once the prompt callback starts, &lt;code&gt;cancel&lt;/code&gt; returns false. That is a deliberate boundary in this exercise. Dismissing a page-level review cannot undo an already invoked external operation.&lt;/p&gt;

&lt;p&gt;The same distinction controls error handling. A simulation error returns the controller to idle. An error from the prompt leaves it in &lt;code&gt;uncertain&lt;/code&gt;, where a new review is blocked. A provider response could have been lost after a transaction was submitted, so this model requires a separate recovery decision before another attempt. It deliberately has no button that silently retries an uncertain send.&lt;/p&gt;

&lt;p&gt;Agent Wallet Guard has a broader recovery flow that distinguishes chain confirmation from API synchronization. The &lt;a href="https://fone.cc/insights/onchain-authorization-api-registration-recovery/" rel="noopener noreferrer"&gt;authorization recovery article&lt;/a&gt; covers those records. Keep that recovery concern separate when porting this lab. A generation counter belongs to the currently mounted interaction; it cannot replace a durable transaction record across a refresh.&lt;/p&gt;

&lt;h2&gt;
  
  
  Turn the race into a handover demonstration
&lt;/h2&gt;

&lt;p&gt;For a team adding paid actions to an Agent product, this gives a concrete review script. Start an authorization, delay preflight, cancel it, and prove that the wallet stays closed after the delayed response arrives. Repeat during the last account read, then switch the account or Session and check that a new review is required.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://fone.cc/products/agent-wallet-guard/" rel="noopener noreferrer"&gt;Agent Wallet Guard&lt;/a&gt; provides the Owner workspace, independent signing components and Session authorization path that motivate this exercise. Its recorded demonstration uses local chain 1337 and MockUSDC. Actual Agent services, target networks and wallet combinations require their own integration and acceptance work. Bring the cancellation test to that walkthrough and ask to see the exact point where an abandoned review loses permission to prompt.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Prepared with AI assistance. The source wording and code are preserved.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>web3</category>
      <category>python</category>
      <category>testing</category>
      <category>security</category>
    </item>
    <item>
      <title>Payment Webhooks After a Refund: Check Before Granting Access</title>
      <dc:creator>HaoJie Luo (Fone)</dc:creator>
      <pubDate>Thu, 12 Nov 2026 09:41:36 +0000</pubDate>
      <link>https://dev.to/fonecc/a-valid-payment-webhook-can-arrive-after-a-full-refund-594f</link>
      <guid>https://dev.to/fonecc/a-valid-payment-webhook-can-arrive-after-a-full-refund-594f</guid>
      <description>&lt;p&gt;A paid webhook arrives after the customer has received a full refund. Its signature is valid. Should your app grant access?&lt;/p&gt;

&lt;p&gt;Check three things before fulfillment: the original signed bytes, the purchase saved by your store, and the current order state. The Python exercise below accepts the signature and returns &lt;code&gt;False&lt;/code&gt; for delivery.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://fone.cc/wp-content/uploads/2026/10/payment-webhook-signature-fulfillment-checks-kit-v1.zip" rel="noopener noreferrer"&gt;Download the exercise and acceptance worksheet&lt;/a&gt; to run the refund case and 12 tests with Python’s standard library. For the complete merchant flow, use the &lt;a href="https://fone.cc/insights/payment-webhook-signature-fulfillment-checks/?utm_source=devto&amp;amp;utm_medium=referral&amp;amp;utm_campaign=webhook_refresh_20261117&amp;amp;utm_content=opening" rel="noopener noreferrer"&gt;walkthrough&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Verify the event, then change the current order
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://fone.cc/wp-content/uploads/2026/10/payment-webhook-signature-fulfillment-checks-kit-v1.zip" rel="noopener noreferrer"&gt;Download the exercise&lt;/a&gt; and extract it into an empty directory. Run this from that directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;exercise&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;verify&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;eligible&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;test_exercise&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;signed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;SECRET&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;EXPECTED&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;ORDER&lt;/span&gt;

&lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;headers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;signed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;event&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;verify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;SECRET&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;refunded_order&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="no"&gt;ORDER&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;revision&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'8'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;refund_status&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'full'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;refunded&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="no"&gt;ORDER&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'amount'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'id'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;eligible&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;EXPECTED&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;refunded_order&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;local_revision&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'8'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This prints the event ID and the delivery decision.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sample-event
False
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The verifier accepted the signed event and returned its ID. The eligibility check then rejected delivery because the current order records a full refund.&lt;/p&gt;

&lt;p&gt;The event keeps revision 7. The refunded order is revision 8. Replacing the old payment event with current order state would lose useful history. The receiver can keep both and use the newer state when deciding whether to grant access.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F8t62hs9ynyjh3kzjh417.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F8t62hs9ynyjh3kzjh417.webp" alt="Payment webhook checks from original request bytes through event validation, local order matching and current fulfillment eligibility" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Check the failure modes separately
&lt;/h2&gt;

&lt;p&gt;Run the included suite from the same extracted directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python3 -m unittest -v
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It contains 12 tests. They cover changed signed bytes, timestamp boundaries, event identity, a correctly signed wrong amount, a different chain, a newer refund state and repeated verification of the same event.&lt;/p&gt;

&lt;p&gt;These tests help distinguish two questions. Did the sender sign these exact bytes? Does the merchant currently have an eligible purchase to fulfill?&lt;/p&gt;

&lt;p&gt;For example, the fixture uses a 300-second timestamp window. Replaying the same delivery inside that window still passes verification. The receiver needs persistent duplicate handling as well as a signature check.&lt;/p&gt;

&lt;p&gt;Current-state reads also need a recovery rule. If an order API returns a snapshot older than the event, this exercise raises an error so the caller can retry. Choosing how to persist that pending work is part of the receiver design.&lt;/p&gt;

&lt;p&gt;The download implements selected signature and order checks over synthetic data. An HTTP integration also needs raw-body capture, complete schema validation, durable event storage and a worker that can recover after failure.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://fone.cc/insights/payment-webhook-signature-fulfillment-checks/?utm_source=devto&amp;amp;utm_medium=referral&amp;amp;utm_campaign=devto_st_t01_20261112" rel="noopener noreferrer"&gt;full walkthrough&lt;/a&gt; follows these checks through the merchant workflow and includes an acceptance worksheet.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The downloadable exercise's 12 tests and the example above were rerun while preparing it.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>python</category>
      <category>testing</category>
      <category>security</category>
    </item>
    <item>
      <title>Cross-Chain Deposits: Check Finality Before Crediting a User</title>
      <dc:creator>HaoJie Luo (Fone)</dc:creator>
      <pubDate>Tue, 10 Nov 2026 13:43:35 +0000</pubDate>
      <link>https://dev.to/fonecc/two-rpcs-agree-at-height-2-would-you-mark-the-deposit-final-316o</link>
      <guid>https://dev.to/fonecc/two-rpcs-agree-at-height-2-would-you-mark-the-deposit-final-316o</guid>
      <description>&lt;p&gt;&lt;a href="https://fone.cc/wp-content/uploads/2026/10/python-bridge-indexer-finality-receipt-lab-v1.zip" rel="noopener noreferrer"&gt;Download the Python lab&lt;/a&gt; and run the 14 tests locally. Check conflicting finalized chains and mismatched deposit logs before crediting a deposit.&lt;/p&gt;

&lt;p&gt;Both RPCs return the same block when you ask for height 2. One also reports a finalized head at height 3 whose parent is a different block. If your indexer compares only the two answers at height 2, it can miss the inconsistency in the provider's own responses.&lt;/p&gt;

&lt;p&gt;Here's a small way to reproduce that failure in Python. The hashes and transactions are synthetic, and the lab runs offline.&lt;/p&gt;

&lt;h2&gt;
  
  
  Give one head the wrong parent
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://fone.cc/wp-content/uploads/2026/10/python-bridge-indexer-finality-receipt-lab-v1.zip" rel="noopener noreferrer"&gt;Download the Python lab&lt;/a&gt; and extract it into an empty directory. Run this snippet from the directory containing &lt;code&gt;finality.py&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;dataclasses&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;replace&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;finality&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;fixture&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;agreed_anchor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Conflict&lt;/span&gt;

&lt;span class="n"&gt;chain&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;receipt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;fixture&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;fork&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chain&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;fork&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chain&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"other-block-2"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;agreed_anchor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fork&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fork&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;chain&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;chain&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nc"&gt;Conflict&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# tagged branch changed&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both dictionaries still hold the same block at numeric height 2. The primary head points to &lt;code&gt;other-block-2&lt;/code&gt;. When the checker walks back from that head, it expects to see &lt;code&gt;other-block-2&lt;/code&gt; and finds &lt;code&gt;block-2&lt;/code&gt; instead.&lt;/p&gt;

&lt;p&gt;The checker uses the lower reported finalized height as a meeting point. It walks each branch from its own tagged head, checking that each retrieved block has the hash expected from the previous step. A missing ancestor yields &lt;code&gt;Unavailable&lt;/code&gt;. A block with the wrong hash yields &lt;code&gt;Conflict&lt;/code&gt;. Either result keeps the deposit waiting, but the latter gives you contradictory evidence to investigate.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjcf0wr4iw1trqccrgzq5.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjcf0wr4iw1trqccrgzq5.webp" alt="Two tagged RPC heads traced by parent hash before checking the receipt and deposit log" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Check the receipt, too
&lt;/h2&gt;

&lt;p&gt;Even if the heads agree, the transaction receipt still has to contain the deposit event. The lab checks the contract address, block and transaction identities, success status and the full log. The tests cover cases where the receipt belongs to the right transaction but the claimed event doesn't match.&lt;/p&gt;

&lt;p&gt;Run the suite and demo from the extracted directory. Python 3.10 or newer is sufficient.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python3 -m unittest -v
python3 finality.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The suite runs 14 tests. The demo accepts the synthetic receipt-backed event at block 1, then prints &lt;code&gt;rejected: tagged branch changed&lt;/code&gt;. This lab checks the supplied observations. Live use requires a finality policy for the chain and a separate decision about when funds can move.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://fone.cc/insights/python-bridge-indexer-finality-receipt-lab/?utm_source=devto&amp;amp;utm_medium=referral&amp;amp;utm_campaign=devto_br_l01_20261109" rel="noopener noreferrer"&gt;full walkthrough&lt;/a&gt; shows where the parent walk stops and how the receipt checks handle missing data.&lt;/p&gt;

</description>
      <category>web3</category>
      <category>python</category>
      <category>blockchain</category>
      <category>testing</category>
    </item>
    <item>
      <title>Your Order API Needs More Than an Idempotency Key</title>
      <dc:creator>HaoJie Luo (Fone)</dc:creator>
      <pubDate>Sat, 31 Oct 2026 11:14:29 +0000</pubDate>
      <link>https://dev.to/fonecc/your-order-api-needs-more-than-an-idempotency-key-46j0</link>
      <guid>https://dev.to/fonecc/your-order-api-needs-more-than-an-idempotency-key-46j0</guid>
      <description>&lt;p&gt;An order API can accept an &lt;code&gt;Idempotency-Key&lt;/code&gt; header on every request and still create a second order for the same intended purchase. Generate a fresh key after a timeout, and a server that deduplicates by key has no way to connect the retry to the first attempt.&lt;/p&gt;

&lt;p&gt;There is a separate server-side problem too. A lookup followed by an insert leaves a race if competing requests can both observe that the key is absent. The key needs an atomic relationship with the saved order.&lt;/p&gt;

&lt;p&gt;That is the argument worth testing. How much does your retry guarantee actually cover?&lt;/p&gt;

&lt;p&gt;This Python and SQLite lab commits an order, deliberately drops the response, and lets you retry it. You can inspect the database instead of guessing whether the request succeeded. It creates local order records only and never charges a wallet.&lt;/p&gt;

&lt;h2&gt;
  
  
  Download and start the API
&lt;/h2&gt;

&lt;p&gt;Use Python 3.10 or later with &lt;code&gt;sqlite3&lt;/code&gt;, and curl. The package has no third-party dependencies. The commands below use a macOS or Linux shell; on Windows, run the curl commands in WSL. The recorded checks used Python 3.13.3 on macOS.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://fone.cc/wp-content/uploads/2026/10/python-sqlite-idempotent-order-api-lab-v1.zip" rel="noopener noreferrer"&gt;Download the complete checkout retry lab&lt;/a&gt;, extract it and open a terminal inside the extracted directory. You should see &lt;code&gt;checkout.py&lt;/code&gt;, &lt;code&gt;test_checkout.py&lt;/code&gt; and &lt;code&gt;README.txt&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The same files are available in the &lt;a href="https://github.com/imminer668/idempotent-order-api-lab" rel="noopener noreferrer"&gt;GitHub repository&lt;/a&gt;. Clone it if you prefer working from Git, then run the commands below from the repository directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python3 -B -m unittest -v
python3 checkout.py --db checkout-demo.sqlite --port 8765
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The test suite creates its own temporary database and chooses a free local port. All seven included tests passed when rerun for this draft on Python 3.13.3. The second command leaves a server running on &lt;code&gt;127.0.0.1:8765&lt;/code&gt;. Keep that terminal open and use another terminal for the requests below. If port 8765 is occupied, choose another port and change the request URLs to match.&lt;/p&gt;

&lt;p&gt;This is a local learning server. Python's documentation does not recommend &lt;code&gt;http.server&lt;/code&gt; for production. The example also has no customer authentication or merchant isolation. &lt;a href="https://docs.python.org/3/library/http.server.html" rel="noopener noreferrer"&gt;Python HTTP server documentation&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create one order, then repeat the request
&lt;/h2&gt;

&lt;p&gt;The browser is allowed to choose a known SKU. The server's catalog owns the amount. In this example, &lt;code&gt;field-kit&lt;/code&gt; costs the synthetic raw amount &lt;code&gt;100000000&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl -i http://127.0.0.1:8765/orders \
  -H &lt;span class="s1"&gt;'Content-Type: application/json'&lt;/span&gt; \
  -H &lt;span class="s1"&gt;'Idempotency-Key: reader-attempt-0001'&lt;/span&gt; \
  -d &lt;span class="s1"&gt;'{"sku":"field-kit"}'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first response has status &lt;code&gt;201&lt;/code&gt;. Its JSON contains a generated order &lt;code&gt;id&lt;/code&gt;, the SKU, the saved amount and &lt;code&gt;status&lt;/code&gt; set to &lt;code&gt;open&lt;/code&gt;. Run the same command again. You should receive &lt;code&gt;200&lt;/code&gt; with the same JSON body and the same order ID.&lt;/p&gt;

&lt;p&gt;The different status codes are deliberate in this teaching API. &lt;code&gt;201&lt;/code&gt; means this request created the record; &lt;code&gt;200&lt;/code&gt; means it recovered an existing one. Other APIs have different replay contracts. Stripe, for example, documents response retention and parameter comparison in its own idempotency interface. Match the contract of the system you integrate. &lt;a href="https://docs.stripe.com/api/idempotent_requests" rel="noopener noreferrer"&gt;Stripe idempotent requests&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Put the key and the order in the same transaction
&lt;/h2&gt;

&lt;p&gt;This is the table created by the downloaded server.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="k"&gt;IF&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;EXISTS&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;request_key&lt;/span&gt; &lt;span class="kt"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;sku&lt;/span&gt; &lt;span class="kt"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="kt"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The unique constraint makes the key durable alongside the order. The function below, extracted from &lt;code&gt;checkout.py&lt;/code&gt;, keeps one transaction around the lookup and insert. Run it through the supplied server, which provides the imports and initializes this table.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create_order&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;db_path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fullmatch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="s1"&gt;'[A-Za-z0-9_-]{8,128}'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s1"&gt;'error'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s1"&gt;'invalid_idempotency_key'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="nb"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="kt"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s1"&gt;'sku'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'sku'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="no"&gt;CATALOG&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s1"&gt;'error'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s1"&gt;'send_only_a_known_sku'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;closing&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sqlite3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;db_path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;isolation_level&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'BEGIN IMMEDIATE'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'SELECT id, sku, amount FROM orders WHERE request_key=?'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;commit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'sku'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;409&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s1"&gt;'error'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s1"&gt;'idempotency_conflict'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s1"&gt;'id'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'sku'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'amount'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'status'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s1"&gt;'open'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="n"&gt;order_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uuid&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;uuid4&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;CATALOG&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'sku'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
            &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'INSERT INTO orders VALUES (?,?,?,?)'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'sku'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;commit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;201&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s1"&gt;'id'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'sku'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'sku'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'amount'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'status'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s1"&gt;'open'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nc"&gt;BaseException&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;rollback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;raise&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;BEGIN IMMEDIATE&lt;/code&gt; acquires the SQLite write transaction before the lookup. A competing writer has to wait or fail, rather than independently reading an absent key and proceeding with its own insert. The unique constraint is an additional guard; the server returns success only after commit. SQLite allows one writer at a time, so this approach trades write concurrency for a small, inspectable implementation. See &lt;a href="https://www.sqlite.org/lang_transaction.html" rel="noopener noreferrer"&gt;SQLite transaction behavior&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If SQLite cannot obtain the lock within the configured timeout, the HTTP handler returns &lt;code&gt;503&lt;/code&gt; with &lt;code&gt;database_busy_retry_same_key&lt;/code&gt;. A caller should retry with backoff and keep the same purchase key. This example does not establish a production throughput limit.&lt;/p&gt;

&lt;p&gt;There is a small but consequential choice in the replay branch. It returns the amount saved with the order. It does not calculate a new price from the catalog. A price change between attempts should not silently change the purchase already accepted under that key.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ffnsns6diesj02nhu5x7a.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ffnsns6diesj02nhu5x7a.webp" alt="A create-order request committing one database record, losing its response and returning the same order when the purchase key is retried" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The saved purchase key connects the retry to the order that already exists.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Reproduce the uncertain result
&lt;/h2&gt;

&lt;p&gt;Now use a fresh key and the lab's fault-injection header.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl -i http://127.0.0.1:8765/orders \
  -H &lt;span class="s1"&gt;'Content-Type: application/json'&lt;/span&gt; \
  -H &lt;span class="s1"&gt;'Idempotency-Key: reader-attempt-0002'&lt;/span&gt; \
  -H &lt;span class="s1"&gt;'X-Demo-Drop-Response: yes'&lt;/span&gt; \
  -d &lt;span class="s1"&gt;'{"sku":"field-kit"}'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Curl should report an empty response, typically exit code 52. The handler committed the order and then shut down the connection before sending response bytes. This is a controlled failure in the lab, not evidence of a real network incident.&lt;/p&gt;

&lt;p&gt;Retry without the fault header, keeping the same key.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl -i http://127.0.0.1:8765/orders \
  -H &lt;span class="s1"&gt;'Content-Type: application/json'&lt;/span&gt; \
  -H &lt;span class="s1"&gt;'Idempotency-Key: reader-attempt-0002'&lt;/span&gt; \
  -d &lt;span class="s1"&gt;'{"sku":"field-kit"}'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The response should be &lt;code&gt;200&lt;/code&gt;. The test suite checks that the disconnected call left one database row and the retry did not add another. If you repeat the fault-injected call after running the example before, the saved order will already exist; the database filename is intentionally persistent.&lt;/p&gt;

&lt;h2&gt;
  
  
  Now change only the key
&lt;/h2&gt;

&lt;p&gt;After recovering &lt;code&gt;reader-attempt-0002&lt;/code&gt;, send the same purchase under a new key.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl -i http://127.0.0.1:8765/orders \
  -H &lt;span class="s1"&gt;'Content-Type: application/json'&lt;/span&gt; \
  -H &lt;span class="s1"&gt;'Idempotency-Key: reader-attempt-0003'&lt;/span&gt; \
  -d &lt;span class="s1"&gt;'{"sku":"field-kit"}'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On a fresh run, this returns &lt;code&gt;201&lt;/code&gt; and a different order ID. Both requests have an idempotency header. The database still contains two separate purchases for those two keys.&lt;/p&gt;

&lt;p&gt;Inspect the records from another terminal in the same directory. Stop the API with Ctrl+C when you are done.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python3 - &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="s1"&gt;'CHECK'&lt;/span&gt;
import sqlite3
with sqlite3.connect&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'checkout-demo.sqlite'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; as db:
    &lt;span class="k"&gt;for&lt;/span&gt; row &lt;span class="k"&gt;in&lt;/span&gt; db.execute&lt;span class="o"&gt;(&lt;/span&gt;
        &lt;span class="s2"&gt;"SELECT request_key, id, sku, amount FROM orders ORDER BY request_key"&lt;/span&gt;
    &lt;span class="o"&gt;)&lt;/span&gt;:
        print&lt;span class="o"&gt;(&lt;/span&gt;row&lt;span class="o"&gt;)&lt;/span&gt;
CHECK
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you followed all the commands once with a fresh database, expect three rows in total, one for each of &lt;code&gt;0001&lt;/code&gt;, &lt;code&gt;0002&lt;/code&gt; and &lt;code&gt;0003&lt;/code&gt;. Repeating the commands against that same database reuses those rows.&lt;/p&gt;

&lt;p&gt;The client needs to keep a key for the lifetime of one purchase attempt, including uncertain outcomes. If your business needs to recognize the same purchase across tabs, devices or newly generated keys, it also needs a separate business identity and uniqueness rule. Two identical baskets can be two legitimate purchases, so deduplicating by basket contents alone would make a different mistake.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reject a changed purchase under the same key
&lt;/h2&gt;

&lt;p&gt;Reuse &lt;code&gt;reader-attempt-0001&lt;/code&gt;, but ask for the other SKU.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl -i http://127.0.0.1:8765/orders \
  -H &lt;span class="s1"&gt;'Content-Type: application/json'&lt;/span&gt; \
  -H &lt;span class="s1"&gt;'Idempotency-Key: reader-attempt-0001'&lt;/span&gt; \
  -d &lt;span class="s1"&gt;'{"sku":"field-notes"}'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Expect &lt;code&gt;409&lt;/code&gt; and &lt;code&gt;idempotency_conflict&lt;/code&gt;. The existing purchase remains unchanged. Sending an extra &lt;code&gt;amount&lt;/code&gt; field instead returns &lt;code&gt;400&lt;/code&gt;; the client cannot override the server price through this endpoint.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where this guarantee ends
&lt;/h2&gt;

&lt;p&gt;The test suite sends twelve HTTP requests from six client threads and checks that they produce one order ID. It also restarts the server against the same database. Those are useful checks of this local service, not a distributed exactly-once guarantee or a power-loss test.&lt;/p&gt;

&lt;p&gt;For a payment checkout, the next boundary is the call to a payment provider or blockchain. A committed SQLite row cannot by itself prevent a second external charge. Define a stable identity for that external operation and a recovery path for an uncertain result before adding real payments.&lt;/p&gt;

&lt;p&gt;This sample also lacks authentication and merchant isolation. In a multi-tenant API, derive the key's scope from the authenticated account and operation. Define which request fields must match and how long the key remains valid. Remove the demonstration disconnect header before adapting the pattern to a deployed service.&lt;/p&gt;

&lt;p&gt;A practical review question is whether the integration can recover the original order after its response is lost. “We send an idempotency header” leaves that question unanswered.&lt;/p&gt;

&lt;p&gt;For an API you maintain, would you return the original &lt;code&gt;201&lt;/code&gt; on replay, or use &lt;code&gt;200&lt;/code&gt; as this lab does? The important part is documenting the contract so the caller can handle a recovered purchase correctly.&lt;/p&gt;

&lt;p&gt;Adapted from &lt;a href="https://fone.cc/insights/python-sqlite-idempotent-order-api/?utm_source=devto&amp;amp;utm_medium=referral&amp;amp;utm_campaign=dev_st_l01_20261031" rel="noopener noreferrer"&gt;my original Python and SQLite tutorial&lt;/a&gt;. The runnable lab and illustrations are available there as well.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This draft was prepared by an AI coding agent from my existing tutorial and code. The seven included tests were rerun, and additional checks confirmed that a new key creates a second order and that replay keeps the saved price. This is a local teaching example, not evidence of a production payment deployment.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>webdev</category>
      <category>database</category>
      <category>testing</category>
    </item>
  </channel>
</rss>
