<?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: mujin</title>
    <description>The latest articles on DEV Community by mujin (@mujin).</description>
    <link>https://dev.to/mujin</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%2F4222179%2Fc61ba1b5-653f-43f1-8b14-7a6656269fe4.jpg</url>
      <title>DEV Community: mujin</title>
      <link>https://dev.to/mujin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mujin"/>
    <language>en</language>
    <item>
      <title>Building a mortgage calculator that matches the CFPB's numbers to the cent</title>
      <dc:creator>mujin</dc:creator>
      <pubDate>Sat, 21 Nov 2026 04:48:52 +0000</pubDate>
      <link>https://dev.to/mujin/building-a-mortgage-calculator-that-matches-the-cfpbs-numbers-to-the-cent-1a2p</link>
      <guid>https://dev.to/mujin/building-a-mortgage-calculator-that-matches-the-cfpbs-numbers-to-the-cent-1a2p</guid>
      <description>&lt;h1&gt;
  
  
  Building a mortgage calculator that matches the CFPB's numbers to the cent
&lt;/h1&gt;

&lt;p&gt;Every mortgage calculator I tried disagreed with the others by a few dollars. For a tool people use to decide a 30-year commitment, "close enough" is not a good answer. So I built one that does integer-cent arithmetic, prints every month of the schedule, and refuses to run if its own test cases fail.&lt;/p&gt;

&lt;p&gt;Here is what actually mattered.&lt;/p&gt;

&lt;h2&gt;
  
  
  The formula is the easy part
&lt;/h2&gt;

&lt;p&gt;The standard amortization payment is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;P = L * i / (1 - (1 + i)^-n)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;where &lt;code&gt;L&lt;/code&gt; is the loan, &lt;code&gt;i&lt;/code&gt; the monthly rate, &lt;code&gt;n&lt;/code&gt; the number of payments. Anyone can type that. The disagreements come from everything around it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Floating point is a bug in a money tool
&lt;/h2&gt;

&lt;p&gt;If you compute &lt;code&gt;1234.56 * 360&lt;/code&gt; in doubles and round at the end, you can land a cent off in a table of 360 rows, and users notice — they compare row 240 against their servicer's statement.&lt;/p&gt;

&lt;p&gt;My rule: &lt;strong&gt;convert to integer cents at the input boundary, do all arithmetic in integers, divide only when rendering.&lt;/strong&gt; Rate math is the one place where fractional arithmetic is unavoidable, so the monthly rate is computed once as &lt;code&gt;rate / 100 / 12&lt;/code&gt; and every intermediate money value is rounded to an integer cent at the row level, not at the end.&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;toCents&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dollars&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dollars&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;100&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;pmt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;loanCents&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;monthlyRate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;monthlyRate&lt;/span&gt; &lt;span class="o"&gt;===&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;return&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;loanCents&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;n&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;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;monthlyRate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;n&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;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;loanCents&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;monthlyRate&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="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;f&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="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;monthlyRate === 0&lt;/code&gt; branch is not theoretical: a 0% promo or an input of &lt;code&gt;0&lt;/code&gt; used to produce &lt;code&gt;NaN&lt;/code&gt; and a blank panel.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rounding up is a product decision, not a detail
&lt;/h2&gt;

&lt;p&gt;Payoff calculators have to decide what to do with a fractional final payment. I round &lt;strong&gt;up&lt;/strong&gt;: if the amortization needs 48.3 payments, the answer is month 49, because that is the month the balance actually reaches zero in a servicer's ledger. Rounding down understates the term, which is the direction that misleads.&lt;/p&gt;

&lt;h2&gt;
  
  
  The test suite is the feature
&lt;/h2&gt;

&lt;p&gt;Each calculator exposes the same shape:&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="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;CALC&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;calc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cases&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;selfTest&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;&lt;code&gt;cases&lt;/code&gt; are inputs with expected outputs — including the ugly ones: zero price, 100% down payment, 1e309, a one-megabyte paste, an empty form. &lt;code&gt;selfTest&lt;/code&gt; runs them in the page and returns a pass/fail array. A gate on my side loads the same file in &lt;code&gt;node:vm&lt;/code&gt; and compares against an independent reimplementation, so the browser and the build agree.&lt;/p&gt;

&lt;p&gt;That is also how I caught a real bug: a field declared &lt;code&gt;min: 1&lt;/code&gt; let someone enter a $1 house and produced "$5,000 in closing costs". The fix was a &lt;code&gt;$10,000&lt;/code&gt; floor, consistent with every other price field.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two numbers people misread: 78% and 80%
&lt;/h2&gt;

&lt;p&gt;PMI ends on two different triggers, and they are not the same date:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;78%&lt;/strong&gt; of the home's &lt;em&gt;original&lt;/em&gt; value, on the original amortization schedule — the servicer must terminate it, you do not have to ask.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;80%&lt;/strong&gt; — you can request cancellation in writing, with conditions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;On a $360,000 loan at 6.5% with nothing down, those land at month 156 and month 146. Ten months of $165 premiums is $1,650 you get back by asking. The subtle part: extra principal moves &lt;em&gt;your&lt;/em&gt; request date, not the statutory termination date, because the latter is defined against the original schedule.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I deliberately left out
&lt;/h2&gt;

&lt;p&gt;No framework, no build step, no npm install: one stylesheet, one small &lt;code&gt;core.js&lt;/code&gt;, one file per calculator. The whole site is static, ~140 KB on the heaviest page, and every calculator works with JavaScript enabled and no network after load. No numbers are uploaded — there is no endpoint to upload to.&lt;/p&gt;

&lt;p&gt;Ads are the funding model, and the honest part of that is layout: an ad above the form pushes the form off a phone screen, so the ad unit sits &lt;em&gt;below&lt;/em&gt; the calculator, and an unfilled unit collapses to zero height instead of leaving a 280px hole.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try it and tell me where it is wrong
&lt;/h2&gt;

&lt;p&gt;The calculators, the assumptions, and the citation list are all public: &lt;strong&gt;&lt;a href="https://mujingame.xin" rel="noopener noreferrer"&gt;mujingame.xin&lt;/a&gt;&lt;/strong&gt;. If a number disagrees with your servicer's statement, the methodology page says exactly which convention is used, so the disagreement is checkable instead of a matter of trust.&lt;/p&gt;

&lt;p&gt;If you are building something similar, the two things I would copy are the integer-cent boundary and the self-test contract. Everything else is style.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>showdev</category>
      <category>finance</category>
    </item>
    <item>
      <title>Where the 150 seconds actually go: profiling a local render loop</title>
      <dc:creator>mujin</dc:creator>
      <pubDate>Thu, 12 Nov 2026 14:54:29 +0000</pubDate>
      <link>https://dev.to/mujin/where-the-150-seconds-actually-go-profiling-a-local-render-loop-341h</link>
      <guid>https://dev.to/mujin/where-the-150-seconds-actually-go-profiling-a-local-render-loop-341h</guid>
      <description>&lt;h2&gt;
  
  
  Splitting the 150 seconds into model load, sampling steps, and PNG encode with a stopwatch and cProfile, in a script short enough to paste
&lt;/h2&gt;

&lt;p&gt;My SD1.5 render loop runs in fp32 with model offload on a 4GB GTX 1650. A 512x768 image takes about 150 seconds, but that total does not identify the bottleneck.&lt;/p&gt;

&lt;p&gt;I used &lt;code&gt;perf_counter&lt;/code&gt; for the boundaries I control and &lt;code&gt;cProfile&lt;/code&gt; for calls inside Diffusers. The script assumes the model is already cached locally, so network activity cannot contaminate model-load time.&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;import&lt;/span&gt; &lt;span class="n"&gt;cProfile&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;io&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&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="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;perf_counter&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;torch&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;diffusers&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;StableDiffusionPipeline&lt;/span&gt;

&lt;span class="n"&gt;wall_start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;perf_counter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;profiler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cProfile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Profile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;profiler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;enable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;load_start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;perf_counter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;pipe&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;StableDiffusionPipeline&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;from_pretrained&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"MODEL"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;torch_dtype&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;float32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;local_files_only&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="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;enable_model_cpu_offload&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;model_load&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;perf_counter&lt;/span&gt;&lt;span class="p"&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;load_start&lt;/span&gt;

&lt;span class="n"&gt;step_marks&lt;/span&gt; &lt;span class="o"&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;def&lt;/span&gt; &lt;span class="nf"&gt;mark_step&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;step&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timestep&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;callback_kwargs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;step_marks&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="nf"&gt;perf_counter&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;callback_kwargs&lt;/span&gt;

&lt;span class="n"&gt;sample_start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;perf_counter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;image&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&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;prompt&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&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;width&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;512&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;768&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;callback_on_step_end&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;mark_step&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;images&lt;/span&gt;
&lt;span class="n"&gt;sample_end&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;step_marks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pop&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;step_marks&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nf"&gt;perf_counter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;sampling_steps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sample_end&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;sample_start&lt;/span&gt;

&lt;span class="n"&gt;png_start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;perf_counter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;io&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;BytesIO&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;image&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"PNG"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;png_encode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;perf_counter&lt;/span&gt;&lt;span class="p"&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;png_start&lt;/span&gt;

&lt;span class="nc"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"OUTPUT"&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;write_bytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getvalue&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;profiler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;disable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;wall_seconds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;perf_counter&lt;/span&gt;&lt;span class="p"&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;wall_start&lt;/span&gt;
&lt;span class="n"&gt;profiler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dump_stats&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"render.prof"&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="p"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"model_load"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;model_load&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"sampling_steps"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;sampling_steps&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"png_encode"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;png_encode&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"wall_seconds"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;wall_seconds&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;I inspect the profile with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python -m pstats render.prof
sort cumtime
stats
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The stopwatch gives stable phase boundaries. &lt;code&gt;cProfile&lt;/code&gt; then shows whether time inside the sampling phase belongs to model execution, scheduler work, tensor transfers, or Python callbacks. I keep wall time as the control because the phase totals intentionally omit some setup and file-write overhead.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the profile changed: the two fixes that cut wall-clock, and the one “optimization” that only moved the cost to disk
&lt;/h2&gt;

&lt;p&gt;Fix A was moving pipeline construction outside the per-image function. The daemon now loads SD1.5 once, enables model offload once, and reuses the pipeline. This removes repeated model loading from subsequent renders without pretending that loading became faster.&lt;/p&gt;

&lt;p&gt;Fix B was eliminating a PNG round trip. The old path saved the image, reopened it to calculate the content hash, and then read it again for the next pipeline stage. The current path encodes once into &lt;code&gt;BytesIO&lt;/code&gt;. The same byte string is hashed and written to disk.&lt;/p&gt;

&lt;p&gt;The false optimization was avoiding PNG compression by writing a large intermediate image. CPU time fell, but file-write time and storage traffic increased. The profile looked better only when I stopped measuring before the write. End-to-end wall time did not receive the same benefit.&lt;/p&gt;

&lt;p&gt;The decision rule is simple: an optimization stays only if &lt;code&gt;wall_seconds&lt;/code&gt; falls when measured from the same start and end points. A faster inner function is not sufficient.&lt;/p&gt;

&lt;h2&gt;
  
  
  The single per-run number worth writing to a text file, so a slow week is visible before it is a mystery
&lt;/h2&gt;

&lt;p&gt;I record total wall seconds for every successful 512x768 render. I do not use sampling time alone because that would hide regressions in model loading, encoding, or disk writes.&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;with&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"render-wall-seconds.txt"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"a"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;encoding&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"utf-8"&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;log&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="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"{wall_seconds}\n"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This file is deliberately plain. It works with shell tools, survives daemon restarts, and makes a sustained slowdown visible without requiring a monitoring service. The detailed profile is diagnostic output; total wall time is the trend.&lt;/p&gt;

&lt;p&gt;The rendered work feeds the collection at &lt;a href="https://thunderous-sunflower-a910b4.netlify.app" rel="noopener noreferrer"&gt;https://thunderous-sunflower-a910b4.netlify.app&lt;/a&gt;, but the same measurement applies to any local image pipeline.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Practical takeaway:&lt;/strong&gt; wrap your render at the process boundary, append total wall seconds to a text file, and reject any optimization that improves only an internal timer.&lt;/p&gt;

</description>
      <category>web3</category>
      <category>nft</category>
      <category>python</category>
      <category>ai</category>
    </item>
    <item>
      <title>Giving the copy to an LLM: a whitelist gate beats a better prompt</title>
      <dc:creator>mujin</dc:creator>
      <pubDate>Thu, 12 Nov 2026 02:53:39 +0000</pubDate>
      <link>https://dev.to/mujin/giving-the-copy-to-an-llm-a-whitelist-gate-beats-a-better-prompt-4j28</link>
      <guid>https://dev.to/mujin/giving-the-copy-to-an-llm-a-whitelist-gate-beats-a-better-prompt-4j28</guid>
      <description>&lt;h2&gt;
  
  
  Giving the copy to an LLM: a whitelist gate beats a better prompt
&lt;/h2&gt;

&lt;p&gt;The short version of a longer build. Stable Diffusion 1.5 in fp32 with model offload on a 4GB GTX 1650, about 150 seconds per 512x768 image. The collection is 131 pieces pinned on-chain, 5 per wallet, free to mint on Base; preload costs about 0.0000008 ETH per piece, and the live minted count is readable from the contract.&lt;/p&gt;

&lt;h3&gt;
  
  
  What the pipeline does
&lt;/h3&gt;

&lt;p&gt;One process owns the wallet and the queue: generate, pin, preload, list. Nothing retries blindly — a failure leaves the item in its previous status, so the next cycle picks up the same work instead of inventing new work. The only shared state is a sqlite file plus a heartbeat that the loop rewrites every cycle.&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;import&lt;/span&gt; &lt;span class="n"&gt;torch&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;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cuda&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_device_name&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="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cuda&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_device_properties&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="n"&gt;total_memory&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  What it costs, measured
&lt;/h3&gt;

&lt;p&gt;Costs come off the receipts, not off a gas widget: gasUsed times effectiveGasPrice per transaction, logged per piece. At the current numbers the preload is about 0.0000008 ETH per piece and minting is free, which is the only pair of numbers this post will claim.&lt;/p&gt;

&lt;h3&gt;
  
  
  What I would copy from it
&lt;/h3&gt;

&lt;p&gt;The parts worth stealing are unglamorous: a length gate before anything is published, a heartbeat written by the loop rather than by the supervisor, and an inventory invariant (remaining is preloaded minus totalSupply) that a crash cannot violate.&lt;/p&gt;

&lt;h3&gt;
  
  
  Takeaway
&lt;/h3&gt;

&lt;p&gt;You do not need rented compute to start. You need a seed you can reproduce, a cost number you measured yourself, and a test suite you actually run before the wallet moves.&lt;/p&gt;

&lt;p&gt;Live collection: &lt;a href="https://thunderous-sunflower-a910b4.netlify.app" rel="noopener noreferrer"&gt;https://thunderous-sunflower-a910b4.netlify.app&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>automation</category>
      <category>llm</category>
      <category>python</category>
    </item>
    <item>
      <title>Build your own cost model: what scales per piece and what is one-off</title>
      <dc:creator>mujin</dc:creator>
      <pubDate>Wed, 11 Nov 2026 14:47:07 +0000</pubDate>
      <link>https://dev.to/mujin/build-your-own-cost-model-what-scales-per-piece-and-what-is-one-off-5a9e</link>
      <guid>https://dev.to/mujin/build-your-own-cost-model-what-scales-per-piece-and-what-is-one-off-5a9e</guid>
      <description>&lt;p&gt;My NFT cost model separates costs that grow with inventory from transactions paid once for a specific deployment or approval state.&lt;/p&gt;

&lt;p&gt;The collection runs on Base. Minting is free, with buyers paying their own gas. That makes cost tracking important: the mint itself does not recover issuer-side expenses.&lt;/p&gt;

&lt;h2&gt;
  
  
  The line items that scale per piece versus the one-offs
&lt;/h2&gt;

&lt;p&gt;I use variables until a transaction or invoice provides an actual value.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Line item&lt;/th&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;Formula&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;On-chain preload transaction&lt;/td&gt;
&lt;td&gt;Per piece&lt;/td&gt;
&lt;td&gt;&lt;code&gt;N × V&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Storage pin&lt;/td&gt;
&lt;td&gt;Per piece&lt;/td&gt;
&lt;td&gt;&lt;code&gt;N × S&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;ERC-721 contract deployment&lt;/td&gt;
&lt;td&gt;One-off&lt;/td&gt;
&lt;td&gt;&lt;code&gt;D&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Wallet approval for the marketplace operator&lt;/td&gt;
&lt;td&gt;One-off per wallet and operator state&lt;/td&gt;
&lt;td&gt;&lt;code&gt;A&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;N&lt;/code&gt; is the number of pieces prepared. &lt;code&gt;V&lt;/code&gt; is the measured preload transaction cost per piece. &lt;code&gt;S&lt;/code&gt; is the storage pin cost allocated to each piece. &lt;code&gt;D&lt;/code&gt; and &lt;code&gt;A&lt;/code&gt; come from their transaction receipts.&lt;/p&gt;

&lt;p&gt;My measured on-chain preload is about &lt;code&gt;0.0000008 ETH&lt;/code&gt; per piece. For the full &lt;code&gt;131&lt;/code&gt;-piece drop, the preload term is therefore:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;131 × 0.0000008 ETH
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Storage stays separate because its billing depends on the pinning arrangement. I do not replace an unknown invoice with a convenient estimate.&lt;/p&gt;

&lt;p&gt;The complete model is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;total cost = D + A + N × (V + S)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Deployment is one-off for that contract. Approval is one-off only while the same wallet, operator, and approval state remain in use. Revoking approval or changing operators creates another transaction, so I record it rather than silently treating it as permanent.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;5&lt;/code&gt;-per-wallet limit affects distribution, but it does not change the issuer’s preload cost per piece.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to measure instead of estimate
&lt;/h2&gt;

&lt;p&gt;I record the receipt after every preload transaction. The chain already reports the inputs needed for the actual gas charge: &lt;code&gt;gasUsed&lt;/code&gt; and &lt;code&gt;effectiveGasPrice&lt;/code&gt;.&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;receipt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;provider&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getTransactionReceipt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;txHash&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;gasUsed&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;receipt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;gasUsed&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;effectiveGasPrice&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;receipt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;effectiveGasPrice&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;wei&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;gasUsed&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;effectiveGasPrice&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nf"&gt;appendFileSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"preload-costs.csv"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sb"&gt;`${tokenId},${txHash},${gasUsed},${effectiveGasPrice},${wei}\n`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;tokenId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;eth&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ethers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;formatEther&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wei&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;The CSV gives me an auditable per-piece ledger. I store the token identifier, transaction hash, gas usage, effective gas price, and final cost.&lt;/p&gt;

&lt;p&gt;I use the same procedure for deployment and wallet approval, but classify those rows as one-offs. If a transaction covers multiple pieces, I allocate its measured cost across the pieces included in that transaction instead of calling the entire receipt a per-piece cost.&lt;/p&gt;

&lt;p&gt;This matters because gas settings and network conditions can change. The receipt is a measurement; a gas estimate is only a planning input.&lt;/p&gt;

&lt;h2&gt;
  
  
  Break-even math at the mint price using only the numbers in this post
&lt;/h2&gt;

&lt;p&gt;The mint price is free. Buyers pay their own mint gas, but that payment goes to network execution rather than to me. There is therefore no issuer-side mint revenue to offset preload, pinning, deployment, or approval costs.&lt;/p&gt;

&lt;p&gt;For the complete drop, the known variable preload expression is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;131 × 0.0000008 ETH
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Storage pinning and the measured one-offs must be added to that expression. Because mint revenue is free, there is no finite break-even point from primary mint revenue alone.&lt;/p&gt;

&lt;p&gt;If I replace the free mint with a paid sale price &lt;code&gt;P&lt;/code&gt;, I use net receipts rather than the displayed price:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;break-even pieces = (D + A) / (P - V - S)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That formula only works when &lt;code&gt;P&lt;/code&gt; is greater than the per-piece cost &lt;code&gt;V + S&lt;/code&gt;. If the sale price is wrong and falls below that cost, every additional sale increases the unrecovered balance. Raising &lt;code&gt;P&lt;/code&gt; improves the arithmetic but does not guarantee that buyers will execute the sale.&lt;/p&gt;

&lt;p&gt;Practical takeaway: add receipt logging before adding inventory, then calculate from measured costs rather than wallet balance changes. The live collection count is readable from the contract linked at &lt;a href="https://thunderous-sunflower-a910b4.netlify.app" rel="noopener noreferrer"&gt;https://thunderous-sunflower-a910b4.netlify.app&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>web3</category>
      <category>nft</category>
      <category>python</category>
      <category>ai</category>
    </item>
    <item>
      <title>Listing, repricing, cancelling: a state machine for one token</title>
      <dc:creator>mujin</dc:creator>
      <pubDate>Wed, 11 Nov 2026 02:40:38 +0000</pubDate>
      <link>https://dev.to/mujin/listing-repricing-cancelling-a-state-machine-for-one-token-e9o</link>
      <guid>https://dev.to/mujin/listing-repricing-cancelling-a-state-machine-for-one-token-e9o</guid>
      <description>&lt;h2&gt;
  
  
  The state machine for one token (unlisted, listed, sold, cancelled) and the only legal transitions between those states
&lt;/h2&gt;

&lt;p&gt;I store listing state separately from mint state. Minting is free on Base; the buyer pays only gas. The collection cap is 131 pieces, with 5 per wallet. The live minted count is readable on the contract rather than copied into the listing database.&lt;/p&gt;

&lt;p&gt;The legal transitions are:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Current state&lt;/th&gt;
&lt;th&gt;Next state&lt;/th&gt;
&lt;th&gt;Required evidence&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;unlisted&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;listed&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Seaport order accepted and its order id stored&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;listed&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;sold&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Ownership changed through a fulfilled order&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;listed&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;cancelled&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Cancellation confirmed&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;cancelled&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;listed&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Replacement order accepted&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;There is no direct &lt;code&gt;listed → listed&lt;/code&gt; transition. Repricing is cancellation followed by a new listing. &lt;code&gt;sold&lt;/code&gt; is terminal for my daemon because the wallet no longer owns the token.&lt;/p&gt;

&lt;p&gt;I enforce the transition table before signing or submitting anything:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;legal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;unlisted&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"listed"&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;listed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"sold"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"cancelled"&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;cancelled&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"listed"&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;sold&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Set&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="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;transition&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;next&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;legal&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;next&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;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`illegal transition: ${token.state} -&amp;gt; ${next}`&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;token&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;next&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 database row also stores the order id, price, owner, and order payload. The on-chain preload cost I logged was about 0.0000008 ETH per piece. That cost is separate from listing state and does not change during repricing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why cancel-then-list has to be sequential, and what a parallel version did to the order book
&lt;/h2&gt;

&lt;p&gt;My first repricing worker launched cancellation and replacement listing together. That was incorrect.&lt;/p&gt;

&lt;p&gt;The replacement order did not depend on confirmed cancellation. Seaport therefore had a period where the old signed order was still live while the new order had already entered the order book. The token appeared with both prices. Local state was worse: whichever request completed last overwrote the stored order id, even when the other order remained fillable.&lt;/p&gt;

&lt;p&gt;The safe rule is: do not construct the replacement until cancellation is confirmed and the old order is no longer live.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;reprice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;token&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="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;cancelOrder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;orderId&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="nf"&gt;waitUntilNotLive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;orderId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;transition&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"cancelled"&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;order&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;buildAndSubmitOrder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;token&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="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;orderId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;transition&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"listed"&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="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;token&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;This costs latency, but it removes ambiguous inventory. I prefer a temporarily unlisted token to multiple valid asks for the same token.&lt;/p&gt;

&lt;p&gt;The worker also locks the token row while repricing. Without that lock, the restock daemon and a manual reprice can both pass the initial state check and create separate replacement orders.&lt;/p&gt;

&lt;h2&gt;
  
  
  The reconciliation pass that finds tokens with an order id but no live order, and how it repairs them
&lt;/h2&gt;

&lt;p&gt;A stored order id is not proof of a listing. An order can be filled, cancelled, invalidated by ownership changes, or absent from the order book while the local write remains.&lt;/p&gt;

&lt;p&gt;The reconciliation candidate query is deliberately simple:&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;SELECT&lt;/span&gt; &lt;span class="n"&gt;token_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;state&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;owner&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;tokens&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'listed'&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;order_id&lt;/span&gt; &lt;span class="k"&gt;IS&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For each row, the daemon asks whether the order is live and checks the current ERC-721 owner. If the order is live, it leaves the row unchanged. If ownership moved away from the seller, it records &lt;code&gt;sold&lt;/code&gt; and clears the active order id. If the seller still owns the token but the order is not live, it records &lt;code&gt;cancelled&lt;/code&gt;, clears the stale id, and sends the token through the normal listing path.&lt;/p&gt;

&lt;p&gt;The repair never edits a stale row directly back to &lt;code&gt;listed&lt;/code&gt;. It must create a fresh order, receive a fresh order id, and then apply the legal &lt;code&gt;cancelled → listed&lt;/code&gt; transition.&lt;/p&gt;

&lt;p&gt;Practical takeaway: model repricing as &lt;code&gt;cancel → confirm dead → list&lt;/code&gt;, then run a reconciliation query against every locally listed token. The collection using this pipeline is at &lt;a href="https://thunderous-sunflower-a910b4.netlify.app" rel="noopener noreferrer"&gt;https://thunderous-sunflower-a910b4.netlify.app&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>web3</category>
      <category>nft</category>
      <category>python</category>
      <category>ai</category>
    </item>
    <item>
      <title>Restocking without double-spending: an idempotent inventory loop</title>
      <dc:creator>mujin</dc:creator>
      <pubDate>Tue, 10 Nov 2026 14:35:59 +0000</pubDate>
      <link>https://dev.to/mujin/restocking-without-double-spending-an-idempotent-inventory-loop-38n7</link>
      <guid>https://dev.to/mujin/restocking-without-double-spending-an-idempotent-inventory-loop-38n7</guid>
      <description>&lt;h2&gt;
  
  
  The invariant that keeps inventory honest: remaining is preloaded minus totalSupply, and why preloaded alone is a lie
&lt;/h2&gt;

&lt;p&gt;My inventory daemon uses this invariant:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;remaining = preloaded - totalSupply
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;preloaded&lt;/code&gt; is the amount of metadata registered for future minting. It is not the amount currently available. Every successful mint consumes inventory by increasing &lt;code&gt;totalSupply&lt;/code&gt;, while the preload count stays unchanged.&lt;/p&gt;

&lt;p&gt;A dashboard that displays only &lt;code&gt;preloaded&lt;/code&gt; therefore overstates availability as soon as minting starts. The same mistake in a restocking daemon is worse: the daemon may decide inventory is healthy even though buyers have consumed most of it.&lt;/p&gt;

&lt;p&gt;I read both values from the NFT contract on Base during every loop. I do not maintain a separate mutable “remaining” counter. That would create another value that could drift after a crash, RPC failure, or manual transaction.&lt;/p&gt;

&lt;p&gt;The contract caps the drop at 131 pieces and limits each wallet to 5. Minting is free; the buyer pays only gas. The changing live count is readable on the contract rather than embedded in copy or configuration.&lt;/p&gt;

&lt;p&gt;The decision rule is simple: calculate remaining from current contract state, then restock only when the local policy says that value is too low. If either contract read fails, the loop does nothing.&lt;/p&gt;

&lt;h2&gt;
  
  
  The order of operations (upload, preload, verify, then log) and which crash points make a retry safe or unsafe
&lt;/h2&gt;

&lt;p&gt;My write path is deliberately ordered:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;upload asset and metadata
preload the piece on Base
verify the successful preload from chain state
upsert the local inventory log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Uploading first prevents the contract from pointing at an object that does not exist. I use a stable piece identifier across the metadata, preload call, verification, and local log.&lt;/p&gt;

&lt;p&gt;The on-chain preload costs about 0.0000008 ETH per piece. That is small, but blindly repeating it is still incorrect. A retry must begin with discovery, not another write.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Crash point&lt;/th&gt;
&lt;th&gt;Blind retry&lt;/th&gt;
&lt;th&gt;Correct recovery&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Before upload completes&lt;/td&gt;
&lt;td&gt;Safe if the upload destination is content-addressed&lt;/td&gt;
&lt;td&gt;Upload again and confirm the resulting reference&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;After upload, before preload&lt;/td&gt;
&lt;td&gt;Safe&lt;/td&gt;
&lt;td&gt;Reuse the same reference and submit the preload&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;After preload submission, before receipt&lt;/td&gt;
&lt;td&gt;Unsafe&lt;/td&gt;
&lt;td&gt;Search chain state and canonical logs for the piece identifier before sending anything&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;After verification, before local logging&lt;/td&gt;
&lt;td&gt;Unsafe if retry resubmits the preload&lt;/td&gt;
&lt;td&gt;Re-read the chain, then upsert the log&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;After local logging&lt;/td&gt;
&lt;td&gt;Safe&lt;/td&gt;
&lt;td&gt;The upsert returns the existing record&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The dangerous window is between transaction submission and local logging. A timeout does not mean failure; it means the result is unknown.&lt;/p&gt;

&lt;p&gt;I treat “unknown” as a reconciliation state. The daemon checks whether the piece identifier already exists on-chain. If it does, the daemon records the existing transaction. If it does not, only then may it submit a preload.&lt;/p&gt;

&lt;h2&gt;
  
  
  The reconciliation query that proves no piece was preloaded twice
&lt;/h2&gt;

&lt;p&gt;I index successful preload events into &lt;code&gt;preload_events&lt;/code&gt;. The table includes the contract address, piece identifier, transaction hash, block position, transaction position, and log position.&lt;/p&gt;

&lt;p&gt;This self-join returns every piece identifier with an earlier and later preload event:&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;SELECT&lt;/span&gt;
    &lt;span class="n"&gt;earlier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;piece_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;earlier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tx_hash&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;earlier_tx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;later&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tx_hash&lt;/span&gt;   &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;later_tx&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;preload_events&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;earlier&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;preload_events&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;later&lt;/span&gt;
  &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;later&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;contract_address&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;earlier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;contract_address&lt;/span&gt;
 &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;later&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;piece_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;earlier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;piece_id&lt;/span&gt;
 &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="n"&gt;earlier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;block_number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;earlier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;transaction_index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;earlier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;log_index&lt;/span&gt;
 &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="n"&gt;later&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;block_number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;later&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;transaction_index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;later&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;log_index&lt;/span&gt;
 &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;earlier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;contract_address&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;contract_address&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An empty result means the indexed canonical event history contains no repeated preload for the same piece identifier. I run this after ingestion and before allowing the daemon to leave reconciliation mode. The indexer also discards logs removed by chain reorganization, so the query reflects canonical history rather than a stale receipt.&lt;/p&gt;

&lt;p&gt;Practical takeaway: make retries read chain state before they write, and audit the invariant against the live contract at &lt;a href="https://thunderous-sunflower-a910b4.netlify.app" rel="noopener noreferrer"&gt;the collection page&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>web3</category>
      <category>nft</category>
      <category>python</category>
      <category>ai</category>
    </item>
    <item>
      <title>A watchdog that tells you it is dead: heartbeat, not logs</title>
      <dc:creator>mujin</dc:creator>
      <pubDate>Mon, 09 Nov 2026 14:29:48 +0000</pubDate>
      <link>https://dev.to/mujin/a-watchdog-that-tells-you-it-is-dead-heartbeat-not-logs-4mpl</link>
      <guid>https://dev.to/mujin/a-watchdog-that-tells-you-it-is-dead-heartbeat-not-logs-4mpl</guid>
      <description>&lt;h2&gt;
  
  
  Why “no errors in the log” is not health, and the heartbeat file the loop rewrites every cycle
&lt;/h2&gt;

&lt;p&gt;My restocking daemon manages a finite queue on Base. The state I logged was 131 pieces generated and pinned, 60 minted, and 71 still to mint, with a limit of 5 per wallet. If the daemon stops progressing, inventory eventually stops changing.&lt;/p&gt;

&lt;p&gt;I initially treated a quiet error log as a healthy process. That test is incomplete. A loop can block before it reaches an exception handler or logging call. The process remains visible to the operating system, but it produces neither work nor errors.&lt;/p&gt;

&lt;p&gt;I added a heartbeat file. The daemon atomically replaces it whenever a loop cycle returns, including cycles that handle an ordinary operation error.&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="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timezone&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;pathlib&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Path&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;

&lt;span class="n"&gt;HEARTBEAT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;HEARTBEAT_FILE&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;write_heartbeat&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;temporary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;HEARTBEAT&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;with_suffix&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;.tmp&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;temporary&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;datetime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;timezone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;utc&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;isoformat&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="n"&gt;temporary&lt;/span&gt;&lt;span class="p"&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;HEARTBEAT&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="nf"&gt;stop_requested&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;run_restock_cycle&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exception&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;restock cycle failed&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;finally&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;write_heartbeat&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="nf"&gt;wait_for_next_cycle&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The temporary file and atomic replacement prevent the watchdog from reading partially written content. My external check uses the file modification time, but the timestamp inside the file is useful when inspecting it manually.&lt;/p&gt;

&lt;p&gt;The placement matters. Writing the heartbeat before &lt;code&gt;run_restock_cycle()&lt;/code&gt; would only prove that the loop started. Writing it after the attempt proves that control returned. If the cycle blocks, the heartbeat stops changing.&lt;/p&gt;

&lt;p&gt;The cost is a small local file write per cycle. That is preferable to deriving health from log volume, which changes whenever logging policy changes.&lt;/p&gt;

&lt;h2&gt;
  
  
  The staleness rule (a heartbeat older than a few cycles means dead) and the one command that checks it from outside the process
&lt;/h2&gt;

&lt;p&gt;I define the maximum age as a few expected cycle durations. A delayed cycle can then finish without causing an alert, while repeated missed cycles mark the daemon unhealthy.&lt;/p&gt;

&lt;p&gt;The monitor receives &lt;code&gt;HEARTBEAT_FILE&lt;/code&gt; and &lt;code&gt;HEARTBEAT_MAX_AGE&lt;/code&gt; through its environment. The latter is expressed in seconds. This shell command succeeds only when the file exists and is fresh:&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="nb"&gt;test&lt;/span&gt; &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$HEARTBEAT_FILE&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="se"&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="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;date&lt;/span&gt; +%s&lt;span class="si"&gt;)&lt;/span&gt; - &lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;stat&lt;/span&gt; &lt;span class="nt"&gt;-c&lt;/span&gt; %Y &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$HEARTBEAT_FILE&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt; &amp;lt;&lt;span class="o"&gt;=&lt;/span&gt; HEARTBEAT_MAX_AGE &lt;span class="o"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I run that command outside the daemon. A health endpoint implemented inside the same loop would share the failure mode it is meant to detect.&lt;/p&gt;

&lt;p&gt;The decision rule is mechanical:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Missing process: crash condition.&lt;/li&gt;
&lt;li&gt;Running process with a fresh heartbeat: loop is progressing.&lt;/li&gt;
&lt;li&gt;Running process with a stale heartbeat: loop is stalled.&lt;/li&gt;
&lt;li&gt;Missing heartbeat: startup failure or incorrect deployment configuration.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The main tuning cost is alert latency. A short maximum age detects stalls quickly but is sensitive to slow cycles. A long maximum age suppresses noise but leaves the daemon ineffective for longer. I set it from observed cycle duration rather than from log frequency.&lt;/p&gt;

&lt;h2&gt;
  
  
  The failure it caught: process alive, loop stuck, and why that alert had to look different from a crash alert
&lt;/h2&gt;

&lt;p&gt;The watchdog caught a case where the process still existed but the loop had stopped returning. The process check passed. There was no new exception in the log. The heartbeat age continued increasing, so the external check failed.&lt;/p&gt;

&lt;p&gt;I report that as &lt;code&gt;loop_stalled&lt;/code&gt;, not &lt;code&gt;process_missing&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A crash alert says the operating system no longer has the process. Restarting it and inspecting its exit output are reasonable first actions. A stall alert says the process exists but cannot complete useful work. Restarting may restore service, but it can also erase the state needed to identify the blocking operation.&lt;/p&gt;

&lt;p&gt;The different label changes the response: capture process state first, then restart if required. It also prevents a supervisor’s “process is running” result from closing the incident incorrectly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Practical takeaway:&lt;/strong&gt; add an atomically replaced heartbeat file to one background loop today, then monitor its age from a separate process. Treat “missing” and “alive but stale” as different failures.&lt;/p&gt;

&lt;p&gt;The collection this daemon supports is at &lt;a href="https://thunderous-sunflower-a910b4.netlify.app" rel="noopener noreferrer"&gt;https://thunderous-sunflower-a910b4.netlify.app&lt;/a&gt;&lt;/p&gt;

</description>
      <category>automation</category>
      <category>backend</category>
      <category>monitoring</category>
      <category>python</category>
    </item>
    <item>
      <title>Shipping a mint page you cannot redeploy at 3am</title>
      <dc:creator>mujin</dc:creator>
      <pubDate>Mon, 09 Nov 2026 02:25:09 +0000</pubDate>
      <link>https://dev.to/mujin/shipping-a-mint-page-you-cannot-redeploy-at-3am-h93</link>
      <guid>https://dev.to/mujin/shipping-a-mint-page-you-cannot-redeploy-at-3am-h93</guid>
      <description>&lt;p&gt;A mint page can be technically available and still be wrong: the wallet is on another network, the displayed price is stale, or the page points at a contract address that does not match the deployment. My pre-deploy check is about catching those mismatches before a transaction can take money.&lt;/p&gt;

&lt;h2&gt;
  
  
  The pre-deploy checklist: one address, checked chain, live price
&lt;/h2&gt;

&lt;p&gt;I keep the contract address in exactly one place: the page’s configuration. The mint flow reads it from there; it does not duplicate the address in a component, a helper, and a deployment note. That makes address changes reviewable and prevents different parts of the page from quietly targeting different contracts.&lt;/p&gt;

&lt;p&gt;Before every mint, I check the wallet’s &lt;code&gt;chainId&lt;/code&gt; against the expected chain. A check on page load is not enough: the user can switch networks after loading the page. The check belongs immediately before sending the transaction.&lt;/p&gt;

&lt;p&gt;I also read the mint price from the contract at runtime. I do not treat a price embedded in the page as authoritative. The page can display the chain value and pass that same value into the transaction, rather than trusting a cached label.&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="nx"&gt;address&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;contractAddress&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;mint&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;chainId&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;wallet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getNetwork&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="nx"&gt;chainId&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;expectedChainId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Wrong network&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mintPrice&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mint&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;price&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 checklist is simple: one configured address, a chain check at the point of action, and a price read from the contract.&lt;/p&gt;

&lt;h2&gt;
  
  
  What happens when the page and the contract disagree
&lt;/h2&gt;

&lt;p&gt;The page is not allowed to resolve a disagreement by guessing. If the expected chain does not match the wallet, the mint stops before it sends a transaction. If the contract call fails, or the value needed for the mint cannot be read, the page stops there too.&lt;/p&gt;

&lt;p&gt;That is the important guard: fail loudly, before taking the buyer’s money. A disabled button or a clear error is better than sending a transaction with a value the page assumed was correct. The contract remains the authority for whether a mint is valid; the page’s job is to make that boundary visible and avoid submitting a transaction when its own checks fail.&lt;/p&gt;

&lt;p&gt;For a deploy, I verify the configured address and chain against the deployment, then exercise the mint path with those checks enabled. The test suite has about 473 tests, but that does not replace checking the actual page configuration and live contract reads for the release.&lt;/p&gt;

&lt;h2&gt;
  
  
  A rollback that needs no new deploy
&lt;/h2&gt;

&lt;p&gt;A page can have a rollback switch without changing the contract. I keep a flag in the page that disables mint submission, then use a read-only contract call to decide whether the page should allow minting. Read-only calls do not send a transaction or take payment.&lt;/p&gt;

&lt;p&gt;When the flag is off, the page does not expose a mint action. When it is on, the page still checks the chain and reads the current contract state and price before allowing a transaction. The flag is an operational brake, not a replacement for contract checks.&lt;/p&gt;

&lt;p&gt;The useful rollback property is that the page can stop initiating transactions without waiting for a new contract deployment. That gives the operator a way to halt the interface while investigating a mismatch; it does not claim to reverse transactions already sent.&lt;/p&gt;

&lt;h2&gt;
  
  
  Takeaway
&lt;/h2&gt;

&lt;p&gt;Before shipping a mint page, make the contract address singular, check the wallet’s chain immediately before every mint, read the price from the contract, and include a page-level switch that can stop new transactions. The collection page is at &lt;a href="https://thunderous-sunflower-a910b4.netlify.app" rel="noopener noreferrer"&gt;https://thunderous-sunflower-a910b4.netlify.app&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>blockchain</category>
      <category>ethereum</category>
      <category>frontend</category>
      <category>web3</category>
    </item>
    <item>
      <title>What a large test suite actually buys a bot with a wallet</title>
      <dc:creator>mujin</dc:creator>
      <pubDate>Sun, 08 Nov 2026 14:22:14 +0000</pubDate>
      <link>https://dev.to/mujin/what-a-large-test-suite-actually-buys-a-bot-with-a-wallet-3k09</link>
      <guid>https://dev.to/mujin/what-a-large-test-suite-actually-buys-a-bot-with-a-wallet-3k09</guid>
      <description>&lt;p&gt;A bot that controls a wallet can turn a small state bug into an irreversible transaction. My test suite has about 473 tests, but the count is not the useful property. The useful property is that it separates deterministic business rules from effects that cross a trust boundary.&lt;/p&gt;

&lt;h2&gt;
  
  
  The split that matters: pure-logic tests versus anything that touches a network or a wallet, and why the second group is stubbed at the boundary instead of mocked everywhere
&lt;/h2&gt;

&lt;p&gt;Pure-logic tests cover price conversion, listing state transitions, metadata validation, supply limits, and copy gates. They receive values and return values. These tests are fast, deterministic, and do not need Base, a wallet, or a pinned asset.&lt;/p&gt;

&lt;p&gt;Network and wallet tests stop at an explicit boundary. I stub the RPC client, signer, storage client, and copywriter adapter. The application code still builds the request, evaluates the response, and updates local state normally.&lt;/p&gt;

&lt;p&gt;I do not mock internal functions throughout the call graph. Deep mocks encode implementation details: rename a helper or move validation into another module, and unrelated tests fail. A boundary stub encodes the contract that matters.&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;rpc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;send_transaction&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;return_value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;receipt&lt;/span&gt;
&lt;span class="n"&gt;signer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sign&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;return_value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;signed_transaction&lt;/span&gt;

&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;restocker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;candidate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;listed&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;signer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sign&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;called&lt;/span&gt;
&lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;rpc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;send_transaction&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;called&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The cost is that a stub cannot prove Base accepted a real transaction. I keep a smaller manual deployment check for that. The main suite answers a different question: given each boundary response, does the bot make the correct decision before spending funds?&lt;/p&gt;

&lt;h2&gt;
  
  
  Three tests that paid for themselves: price rounding, retry-after-failure, and the length gate
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Test&lt;/th&gt;
&lt;th&gt;Bug caught&lt;/th&gt;
&lt;th&gt;Assertion&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Price rounding&lt;/td&gt;
&lt;td&gt;A floating-point conversion produced the wrong base-unit amount before a listing was signed.&lt;/td&gt;
&lt;td&gt;Convert through an exact decimal representation and compare the final integer amount.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Retry after failure&lt;/td&gt;
&lt;td&gt;A failed network call advanced local state, so the next daemon pass treated unfinished work as complete.&lt;/td&gt;
&lt;td&gt;Failure leaves the candidate retryable; success alone commits the state transition.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Length gate&lt;/td&gt;
&lt;td&gt;Generated copy passed the fact gate but exceeded the configured field limit.&lt;/td&gt;
&lt;td&gt;Reject text above the configured maximum before metadata is pinned or signed.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The price test protects both listing construction and the on-chain preload calculation. The preload is about 0.0000008 ETH per piece, so conversion code must not silently reinterpret decimal input.&lt;/p&gt;

&lt;p&gt;The retry test matters because the daemon restocks unattended. A timeout is not proof of success, and an exception is not permission to discard work. The test feeds a boundary failure into the same state machine used in production, then verifies that the candidate remains eligible.&lt;/p&gt;

&lt;p&gt;The length test sits after the LLM and fact gate. Factual text can still be operationally invalid. The gate measures the final serialized value, not an earlier draft, because normalization can change what is actually stored.&lt;/p&gt;

&lt;h2&gt;
  
  
  The rule for adding a test: it must fail on the bug you just fixed, plus the command that has to be green before a deploy
&lt;/h2&gt;

&lt;p&gt;My rule is mechanical: reproduce the bug as a test, run it against the broken code, and confirm that it fails for the expected reason. Then apply the fix and run the entire suite.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pytest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That command must be green before a deploy. A test written only after the fix, without demonstrating the failure, may merely confirm the implementation that already exists. It does not prove that the original defect is covered.&lt;/p&gt;

&lt;p&gt;The cost is maintenance. Boundary contracts must stay explicit, fixtures must remain readable, and obsolete tests must be removed when behavior intentionally changes. About 473 tests are useful only while each failure points to a real decision.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Practical takeaway:&lt;/strong&gt; identify the wallet and network boundaries in your bot today, stub those boundaries, then turn the next production bug into a failing regression test before editing the implementation.&lt;/p&gt;

</description>
      <category>blockchain</category>
      <category>security</category>
      <category>testing</category>
      <category>web3</category>
    </item>
    <item>
      <title>ERC-2981 in practice: what a 5% royalty actually pays out</title>
      <dc:creator>mujin</dc:creator>
      <pubDate>Sun, 08 Nov 2026 02:21:28 +0000</pubDate>
      <link>https://dev.to/mujin/erc-2981-in-practice-what-a-5-royalty-actually-pays-out-3ock</link>
      <guid>https://dev.to/mujin/erc-2981-in-practice-what-a-5-royalty-actually-pays-out-3ock</guid>
      <description>&lt;h2&gt;
  
  
  How an ERC-2981 royalty actually lands in a sale: in Seaport it is an extra consideration item with a fixed recipient, not a post-hoc transfer
&lt;/h2&gt;

&lt;p&gt;ERC-2981 does not transfer a royalty. It answers a question: for this token and sale price, which address should receive the royalty, and how much should it receive?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(address receiver, uint256 royaltyAmount) =
    royaltyInfo(tokenId, salePrice);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When I build a Seaport listing, I call &lt;code&gt;royaltyInfo&lt;/code&gt; before signing the order. I then add the returned amount and recipient to the order’s consideration array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [receiver, royaltyAmount] =
  await nft.royaltyInfo(tokenId, salePrice);

const consideration = [
  {
    itemType: paymentItemType,
    token: paymentToken,
    identifierOrCriteria: noIdentifier,
    startAmount: sellerAmount,
    endAmount: sellerAmount,
    recipient: seller,
  },
  {
    itemType: paymentItemType,
    token: paymentToken,
    identifierOrCriteria: noIdentifier,
    startAmount: royaltyAmount,
    endAmount: royaltyAmount,
    recipient: receiver,
  },
];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Settlement pays both recipients from the same Seaport fulfillment. There is no later royalty sweep and no post-hoc transfer initiated by the NFT contract.&lt;/p&gt;

&lt;p&gt;My decision rule is simple: reject an order if its royalty consideration recipient or amount differs from the current &lt;code&gt;royaltyInfo&lt;/code&gt; result.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the seller nets after marketplace fee plus royalty, worked out on a real price with the arithmetic written out
&lt;/h2&gt;

&lt;p&gt;The only concrete ETH amount I can publish from my logs is the on-chain preload cost: &lt;code&gt;0.0000008 ETH&lt;/code&gt; per piece. It is not a claimed sale. I use it here as a real logged input so the arithmetic remains reproducible without inventing a listing price or marketplace fee.&lt;/p&gt;

&lt;p&gt;For a five percent royalty:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;price       = 0.0000008 ETH
royalty     = price * five percent
market fee  = F
seller net  = price - royalty - F

seller net  = 0.0000008 ETH
              - (0.0000008 ETH * five percent)
              - F
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I do not hard-code &lt;code&gt;F&lt;/code&gt; in accounting code. I derive it from the signed order’s consideration items. That matters because the storefront’s displayed fee policy is not the settlement record.&lt;/p&gt;

&lt;p&gt;The practical implementation is to classify every payment recipient before signing:&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="nx"&gt;royalty&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;nft&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;royaltyInfo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tokenId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sellerNet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;consideration&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;recipient&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;seller&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;startAmount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;noAmount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nf"&gt;assertRoyaltyItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;consideration&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;royalty&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nf"&gt;assertTotalConsiderationEqualsPrice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;consideration&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The cost is extra order validation and more tests. My broader suite has about &lt;code&gt;473&lt;/code&gt; tests; royalty checks belong beside order construction and fulfillment tests.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where marketplaces deviate from the standard: optional royalties, overrides, and how to check what a specific collection is really paying
&lt;/h2&gt;

&lt;p&gt;ERC-2981 reports royalty information. It does not force a marketplace to request it or include it in settlement. A marketplace can make royalties optional, apply its own override, or omit the royalty consideration entirely.&lt;/p&gt;

&lt;p&gt;I check three sources, in this order:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Call &lt;code&gt;royaltyInfo(tokenId, salePrice)&lt;/code&gt; directly on Base.&lt;/li&gt;
&lt;li&gt;Decode the signed Seaport order and inspect every consideration recipient and amount.&lt;/li&gt;
&lt;li&gt;Inspect the fulfilled transaction’s payment transfers.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The order shows what was promised. The transaction shows what was paid. The contract call shows what the collection requested. A mismatch between those records is the deviation.&lt;/p&gt;

&lt;p&gt;The collection is available at &lt;a href="https://thunderous-sunflower-a910b4.netlify.app" rel="noopener noreferrer"&gt;https://thunderous-sunflower-a910b4.netlify.app&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Practical takeaway: before signing or accepting a listing today, compare &lt;code&gt;royaltyInfo&lt;/code&gt; with the Seaport consideration array; do not infer royalty payment from marketplace copy.&lt;/p&gt;

</description>
      <category>web3</category>
      <category>nft</category>
      <category>python</category>
      <category>ai</category>
    </item>
    <item>
      <title>NFT mint arbitrage: why floor price alone loses money</title>
      <dc:creator>mujin</dc:creator>
      <pubDate>Sat, 07 Nov 2026 02:10:26 +0000</pubDate>
      <link>https://dev.to/mujin/nft-mint-arbitrage-why-floor-price-alone-loses-money-4al</link>
      <guid>https://dev.to/mujin/nft-mint-arbitrage-why-floor-price-alone-loses-money-4al</guid>
      <description>&lt;h2&gt;
  
  
  Why the naive filter fails: floor price minus mint price ignores marketplace fee, creator royalty, and gas on both ends
&lt;/h2&gt;

&lt;p&gt;The first version of my screen ranked drops with this expression:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;headline_profit = floor_price - mint_price
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is not profit. It is the spread shown by two unrelated interfaces.&lt;/p&gt;

&lt;p&gt;A mint transaction costs gas. Selling costs another transaction or settlement path. The marketplace takes a fee. The collection may enforce or signal a creator royalty. If the floor is stale, the executable sale price may be lower again.&lt;/p&gt;

&lt;p&gt;I now calculate:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;net = executable_sale_price
    - mint_price
    - marketplace_fee
    - creator_royalty
    - mint_gas_cost
    - sale_gas_cost
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;“Executable” matters. I use the highest bid I can actually accept or a conservative sale estimate from current orders. I do not treat the lowest listing as guaranteed revenue.&lt;/p&gt;

&lt;p&gt;My decision rule is simple: reject unless &lt;code&gt;net&lt;/code&gt; remains positive after every known cost is included. Unknown fees are not assumed away; they make the candidate incomplete.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Input&lt;/th&gt;
&lt;th&gt;Source&lt;/th&gt;
&lt;th&gt;Failure rule&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Mint price&lt;/td&gt;
&lt;td&gt;Contract call or transaction simulation&lt;/td&gt;
&lt;td&gt;Reject if the page disagrees&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sale proceeds&lt;/td&gt;
&lt;td&gt;Fillable marketplace order&lt;/td&gt;
&lt;td&gt;Reject stale or invalid orders&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Marketplace fee&lt;/td&gt;
&lt;td&gt;Order consideration&lt;/td&gt;
&lt;td&gt;Include before ranking&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Royalty&lt;/td&gt;
&lt;td&gt;Order consideration or collection policy&lt;/td&gt;
&lt;td&gt;Include before ranking&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Gas on entry&lt;/td&gt;
&lt;td&gt;Mint simulation&lt;/td&gt;
&lt;td&gt;Reject if estimation fails&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Gas on exit&lt;/td&gt;
&lt;td&gt;Sale path estimate&lt;/td&gt;
&lt;td&gt;Include before ranking&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;With that accounting, drops that looked profitable under &lt;code&gt;floor_price - mint_price&lt;/code&gt; became negative. The screen was finding displayed spreads, not arbitrage.&lt;/p&gt;

&lt;h2&gt;
  
  
  The volume filter that removes drops nobody trades
&lt;/h2&gt;

&lt;p&gt;A floor only proves that somebody listed an NFT. It does not prove that anybody buys it.&lt;/p&gt;

&lt;p&gt;Before evaluating price, I query recent &lt;code&gt;Transfer&lt;/code&gt; events and separate mint transfers from wallet-to-wallet transfers. A collection with no recent secondary transfers has no demonstrated exit. Its floor can stay visible while every holder waits for a buyer.&lt;/p&gt;

&lt;p&gt;The filter is:&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;SELECT&lt;/span&gt; &lt;span class="n"&gt;collection&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;transfers&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="k"&gt;chain&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Base'&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;block_time&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;recent_cutoff&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;from_address&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;zero_address&lt;/span&gt;
&lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;collection&lt;/span&gt;
&lt;span class="k"&gt;HAVING&lt;/span&gt; &lt;span class="k"&gt;COUNT&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="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;minimum_secondary_transfers&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both parameters are configuration, not universal constants. I set them from the holding period and turnover I am willing to accept. A faster strategy needs a stricter cutoff and a higher transfer requirement.&lt;/p&gt;

&lt;p&gt;I run this filter before contract probing because event queries are cheaper than preparing and simulating mint calls. No recent secondary activity means rejection regardless of floor price. This removes collections where the apparent upside depends on finding the first buyer after I mint.&lt;/p&gt;

&lt;h2&gt;
  
  
  Probing a contract before trusting its mint page
&lt;/h2&gt;

&lt;p&gt;The mint page is presentation. The contract is the executable state.&lt;/p&gt;

&lt;p&gt;For every surviving candidate, I build the exact mint calldata and simulate it from the wallet that would submit it. I include the intended value and use the current Base state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;cast call &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$NFT&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$MINT_SIGNATURE&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nv"&gt;$MINT_ARGUMENTS&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--from&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$BUYER&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--value&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$MINT_VALUE&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--rpc-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$BASE_RPC&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;

cast sig-error &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$ERROR_SIGNATURE&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If simulation succeeds, I keep the estimated gas and continue. If it reverts, I capture the revert data, take its selector, and match that selector against custom errors declared in verified source or recovered from the ABI.&lt;/p&gt;

&lt;p&gt;The reason determines the result. A supply error, wallet-limit error, inactive-sale error, invalid-payment error, or proof error is a rejection until the required state or arguments are available. I do not override that decision because the page still shows an enabled mint button.&lt;/p&gt;

&lt;p&gt;I apply the same fact-gate pattern in my local NFT pipeline: generated copy cannot contradict contract state. The collection is available at &lt;a href="https://thunderous-sunflower-a910b4.netlify.app" rel="noopener noreferrer"&gt;https://thunderous-sunflower-a910b4.netlify.app&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Practical takeaway:&lt;/strong&gt; replace your floor-minus-mint filter today with ordered gates: recent secondary transfers, successful mint simulation, then net proceeds after fees, royalties, and gas.&lt;/p&gt;

</description>
      <category>blockchain</category>
      <category>crypto</category>
      <category>ethereum</category>
      <category>web3</category>
    </item>
    <item>
      <title>Free mint on Base: what the buyer pays and what the creator stores</title>
      <dc:creator>mujin</dc:creator>
      <pubDate>Fri, 06 Nov 2026 14:06:04 +0000</pubDate>
      <link>https://dev.to/mujin/free-mint-on-base-what-the-buyer-pays-and-what-the-creator-stores-124b</link>
      <guid>https://dev.to/mujin/free-mint-on-base-what-the-buyer-pays-and-what-the-creator-stores-124b</guid>
      <description>&lt;h2&gt;
  
  
  What a buyer actually pays on a free mint: gas only, with the real number on Base and how it compares to buying the same token on the secondary market
&lt;/h2&gt;

&lt;p&gt;My mint function takes no payment. The buyer pays only the Base transaction gas required to execute the mint.&lt;/p&gt;

&lt;p&gt;There is no honest fixed ETH number for that gas cost. Base fees change with network conditions, and the final amount also depends on the contract execution path. The real number is the wallet’s gas estimate multiplied by the current gas price when the transaction is submitted.&lt;/p&gt;

&lt;p&gt;I check it immediately before minting:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;account&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;buyer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;address&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;contractAddress&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;abi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;functionName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;mint&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;gas&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;publicClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;estimateContractGas&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;gasPrice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;publicClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getGasPrice&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;buyerCost&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;gas&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;gasPrice&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;formatEther&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;buyerCost&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ETH&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That output is the number I show as the expected buyer cost. The transaction receipt is the source of truth after confirmation.&lt;/p&gt;

&lt;p&gt;For a secondary-market purchase, the buyer pays the token’s sale price plus gas. The transaction may also execute marketplace transfers and fee logic. A free mint has no sale consideration, so its buyer-side comparison is straightforward:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Path&lt;/th&gt;
&lt;th&gt;Token payment&lt;/th&gt;
&lt;th&gt;Buyer also pays&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Free mint on Base&lt;/td&gt;
&lt;td&gt;Free&lt;/td&gt;
&lt;td&gt;Mint transaction gas&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Secondary-market purchase&lt;/td&gt;
&lt;td&gt;Seller’s asking price&lt;/td&gt;
&lt;td&gt;Purchase transaction gas&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The decision rule is: never describe “free mint” as “free transaction.” It means the contract charges no mint price.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the creator stores: preloading token URIs versus lazy minting, and the storage cost each one puts on chain for a small drop
&lt;/h2&gt;

&lt;p&gt;I preload each token URI on chain before it can be minted. For my contract, that write costs about &lt;strong&gt;0.0000008 ETH per piece&lt;/strong&gt; on Base.&lt;/p&gt;

&lt;p&gt;The drop contains &lt;strong&gt;131 pieces&lt;/strong&gt; generated and pinned. &lt;strong&gt;60&lt;/strong&gt; are minted and &lt;strong&gt;71&lt;/strong&gt; remain available to mint. Preloading makes the creator pay for URI registration before demand is known, including inventory that may remain unminted.&lt;/p&gt;

&lt;p&gt;My accounting query is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;preloadCost&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;parseEther&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;0.0000008&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;remaining&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;71&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;formatEther&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;preloadCost&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;remaining&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ETH&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lazy minting moves that storage write to the mint path. The creator avoids preloading every available URI, but mint execution becomes more complex and the buyer may fund the extra gas needed to commit token data. It also requires a signed authorization or another rule proving that the requested metadata belongs to the collection.&lt;/p&gt;

&lt;p&gt;My decision rule is based on inventory risk:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Preload when the drop is small, metadata is final, and predictable mint behavior matters.&lt;/li&gt;
&lt;li&gt;Lazy mint when uncertain demand makes writing every token URI in advance wasteful.&lt;/li&gt;
&lt;li&gt;In either design, pin the metadata before exposing the mint. On-chain storage of a URI does not guarantee the referenced content exists.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The revert that catches people testing their own contract: the wallet limit fires for the deployer's address too, so simulate from a fresh address
&lt;/h2&gt;

&lt;p&gt;The contract limit is &lt;strong&gt;5 per wallet&lt;/strong&gt;. The deployer is not exempt.&lt;/p&gt;

&lt;p&gt;This matters during testing because deployment scripts, setup mints, and manual calls often reuse the deployer account. Once that address reaches the limit, another simulated mint reverts even when the mint logic is otherwise correct.&lt;/p&gt;

&lt;p&gt;I test the public path from a fresh address:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;publicClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;simulateContract&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;account&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;freshBuyer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;address&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;contractAddress&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;abi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;functionName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;mint&lt;/span&gt;&lt;span class="dl"&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 checklist is short: use a fresh address, confirm its minted balance, simulate with that address, then submit from the same address. Do not diagnose a wallet-limit revert using only the deployer.&lt;/p&gt;

&lt;p&gt;The collection is available at &lt;a href="https://thunderous-sunflower-a910b4.netlify.app" rel="noopener noreferrer"&gt;https://thunderous-sunflower-a910b4.netlify.app&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Practical takeaway:&lt;/strong&gt; estimate the Base transaction from the buyer’s actual wallet, account separately for creator-side URI writes, and test wallet limits from a fresh address before investigating the contract.&lt;/p&gt;

</description>
      <category>blockchain</category>
      <category>ethereum</category>
      <category>typescript</category>
      <category>web3</category>
    </item>
  </channel>
</rss>
