.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/economics/mev/plot_01_flash_boys_gas_auctions.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code or to run this example in your browser via JupyterLite. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_economics_mev_plot_01_flash_boys_gas_auctions.py: Flash Boys 2.0: priority gas auctions and front-running (Daian et al. 2019) =========================================================================== When an arbitrage opportunity appears on a decentralized exchange, only the first transaction to execute can take it, and transactions in a block run in fee order. Daian et al. watched bots compete by repeatedly replacing their pending transaction with one paying a higher fee: a *priority gas auction*. Each replacement must raise the fee by a minimum step (10% in go-ethereum), so with bots that value the opportunity alike, bidding stops only when the next step would exceed its value :math:`V`: .. math:: \text{price} > \frac{V}{1.1}, \qquad \text{profit} < V - \frac{V}{1.1} \approx 0.09\,V. Competition hands more than nine tenths of the value to whoever orders the block. Daian et al. called it *miner extractable value*, and showed that it can even pay a miner to reorder or rewrite recent blocks. .. GENERATED FROM PYTHON SOURCE LINES 23-28 .. code-block:: Python import matplotlib.pyplot as plt import blockchainkit as bk from blockchainkit.economics.visualizers import plot_gas_auction .. GENERATED FROM PYTHON SOURCE LINES 29-31 Two bots and one opportunity ---------------------------- .. GENERATED FROM PYTHON SOURCE LINES 31-40 .. code-block:: Python OPPORTUNITY = 50_000 auction = bk.economics.priority_gas_auction(OPPORTUNITY, ["bot A", "bot B"], start=10, seed=2) print(f"{len(auction.bids)} bids; {auction.winner} pays {auction.price:,} for {OPPORTUNITY:,}") assert auction.price > OPPORTUNITY / 1.1 and auction.profit < 0.1 * OPPORTUNITY fig, (left, right) = plt.subplots(1, 2, figsize=(12, 4.5)) plot_gas_auction(auction, opportunity=OPPORTUNITY, ax=left) .. image-sg:: /api/gallery/economics/mev/images/sphx_glr_plot_01_flash_boys_gas_auctions_001.png :alt: bot B wins, paying 48,164 of 50,000 :srcset: /api/gallery/economics/mev/images/sphx_glr_plot_01_flash_boys_gas_auctions_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 86 bids; bot B pays 48,164 for 50,000 .. GENERATED FROM PYTHON SOURCE LINES 41-43 The bump decides how much the bots keep --------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 43-63 .. code-block:: Python bumps = [1, 2, 5, 10, 12, 15, 20, 25, 30] kept = [] for bump in bumps: shares = [ bk.economics.priority_gas_auction(OPPORTUNITY, ["a", "b", "c"], bump=bump, seed=s).profit / OPPORTUNITY for s in range(20) ] kept.append(sum(shares) / len(shares)) assert max(shares) < bump / (100 + bump) + 1 / OPPORTUNITY right.plot(bumps, kept, "o-", color="#2563eb", label="winner's profit") right.plot(bumps, [b / (100 + b) for b in bumps], ":", color="black", label="bound b / (100 + b)") right.set(xlabel="minimum raise (%)", ylabel="fraction of the opportunity kept") right.set_title("Everything else goes to the block producer") right.legend() fig.tight_layout() plt.show() .. GENERATED FROM PYTHON SOURCE LINES 64-69 Exercise -------- If the block producer runs its own bot, it pays its fees to itself. How much of the opportunity does it keep, and what does that suggest about who ends up ordering transactions? .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.093 seconds) .. _sphx_glr_download_api_gallery_economics_mev_plot_01_flash_boys_gas_auctions.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../../../lite/lab/index.html?path=api/gallery/economics/mev/plot_01_flash_boys_gas_auctions.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_flash_boys_gas_auctions.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_flash_boys_gas_auctions.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_flash_boys_gas_auctions.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_