<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0"><channel><title><![CDATA[noxx]]></title><description><![CDATA[EVM Technical Deep Dives]]></description><link>https://noxx.substack.com</link><image><url>https://substackcdn.com/image/fetch/$s_!mgwK!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fcae9a342-a375-4ed7-aff1-014d08177175_400x400.png</url><title>noxx</title><link>https://noxx.substack.com</link></image><generator>Substack</generator><lastBuildDate>Wed, 08 Apr 2026 02:54:42 GMT</lastBuildDate><atom:link href="https://noxx.substack.com/feed" rel="self" type="application/rss+xml"/><copyright><![CDATA[noxx]]></copyright><language><![CDATA[en]]></language><webMaster><![CDATA[noxx@substack.com]]></webMaster><itunes:owner><itunes:email><![CDATA[noxx@substack.com]]></itunes:email><itunes:name><![CDATA[noxx]]></itunes:name></itunes:owner><itunes:author><![CDATA[noxx]]></itunes:author><googleplay:owner><![CDATA[noxx@substack.com]]></googleplay:owner><googleplay:email><![CDATA[noxx@substack.com]]></googleplay:email><googleplay:author><![CDATA[noxx]]></googleplay:author><itunes:block><![CDATA[Yes]]></itunes:block><item><title><![CDATA[Smart Contract Patterns: The Proxy]]></title><description><![CDATA[A deep dive into the architecture of upgradable contracts]]></description><link>https://noxx.substack.com/p/smart-contract-patterns-the-proxy</link><guid isPermaLink="false">https://noxx.substack.com/p/smart-contract-patterns-the-proxy</guid><dc:creator><![CDATA[noxx]]></dc:creator><pubDate>Mon, 01 Jan 2024 18:08:09 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff483e84d-9248-4c90-9203-1c0606183581_2062x1521.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Understanding how to architect large smart contract projects efficiently and securely is a key skill for smart contract developers. To do this effectively various &#8220;Smart Contract Patterns&#8221; are utilised.  </p><p>This series &#8220;Smart Contract Patterns&#8221; will explore a number of these, diving into their origins, applications, and rationale. We&#8217;ll explore in-depth how traditional architecture &amp; development principles translate into the world of smart contracts. </p><p>Each new pattern is a tool, one that can be used to aid you in building &amp; maintaining decentralised applications.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!qI1B!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F518c80b8-4974-4df6-963d-6d1172cbbf94_5663x4599.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!qI1B!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F518c80b8-4974-4df6-963d-6d1172cbbf94_5663x4599.jpeg 424w, https://substackcdn.com/image/fetch/$s_!qI1B!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F518c80b8-4974-4df6-963d-6d1172cbbf94_5663x4599.jpeg 848w, https://substackcdn.com/image/fetch/$s_!qI1B!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F518c80b8-4974-4df6-963d-6d1172cbbf94_5663x4599.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!qI1B!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F518c80b8-4974-4df6-963d-6d1172cbbf94_5663x4599.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!qI1B!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F518c80b8-4974-4df6-963d-6d1172cbbf94_5663x4599.jpeg" width="1456" height="1182" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/518c80b8-4974-4df6-963d-6d1172cbbf94_5663x4599.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1182,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:5380248,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!qI1B!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F518c80b8-4974-4df6-963d-6d1172cbbf94_5663x4599.jpeg 424w, https://substackcdn.com/image/fetch/$s_!qI1B!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F518c80b8-4974-4df6-963d-6d1172cbbf94_5663x4599.jpeg 848w, https://substackcdn.com/image/fetch/$s_!qI1B!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F518c80b8-4974-4df6-963d-6d1172cbbf94_5663x4599.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!qI1B!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F518c80b8-4974-4df6-963d-6d1172cbbf94_5663x4599.jpeg 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Before we delve into these patterns, an extremely useful pre-requisite is a solid understanding of the EVM. Our previous series <a href="https://noxx.substack.com/p/evm-deep-dives-the-path-to-shadowy?utm_source=url">&#8220;EVM Deep Dives&#8221;</a> explores the low-level implementation of the EVM and I would highly recommend reading that before starting. </p><p>Now let&#8217;s kick off the series with one of the most common patterns in smart contract development, &#8220;The Proxy&#8221;.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://noxx.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://noxx.substack.com/subscribe?"><span>Subscribe now</span></a></p><h2>History</h2><p>Understanding the history behind a given &#8220;Smart Contract Pattern&#8221; is extremely valuable. It sheds light on why it emerged, the specific problem it solved, and the design trade-offs made along the way.</p><h4>Why Does This Pattern Exist?</h4><p>For every pattern, we should start with a simple question.</p><p>&#8220;Why?&#8221;.</p><p>Why was this pattern created and what problem did it solve?</p><p>For &#8220;The Proxy&#8221; the why came from the immutability of smart contracts.  Contracts are immutable by design, which prevents any updates to the business logic once the contract is deployed. This raised an obvious question.</p><div class="pullquote"><p><strong>How do we upgrade our smart contracts?</strong></p></div><p>This question was initially solved with &#8220;contract migrations&#8221;. A new version of the contract would be deployed and all state and balances would be transferred to this new instance.</p><p>One obvious drawback of this was that the new deployment resulted in a new contract address. For applications integrated into wider ecosystems, it would require all third parties to also update their codebase to point at the new contract.</p><p>Another drawback was the operational complexity of transferring the state and balances to this new instance. Not only would this be very expensive from a gas perspective it would also be a very sensitive operation. Incorrectly updating the state of the new contract could break functionality and lead to security vulnerabilities.  </p><p>The need for a simpler solution was apparent. How could we update the underlying logic of a contract without changing its address? How could we minimise the operational overhead? </p><p>From these questions &#8220;The Proxy Pattern&#8221; was formed.</p><div><hr></div><h4>The Original Proxy (The Delegate Proxy) </h4><p>The original proxy, known as the delegate proxy used a few simple ideas to answer these questions. </p><p>First, we needed to separate the business logic and data storage into separate contracts. This was achieved with two contracts, &#8220;The Proxy Contract&#8221; for the data and &#8220;The Implementation Contract&#8221; (also known as the logic contract) for the business logic. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!3Sxb!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F496efc92-5fc7-468a-813f-cc621079be4b_1920x1080.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!3Sxb!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F496efc92-5fc7-468a-813f-cc621079be4b_1920x1080.jpeg 424w, https://substackcdn.com/image/fetch/$s_!3Sxb!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F496efc92-5fc7-468a-813f-cc621079be4b_1920x1080.jpeg 848w, https://substackcdn.com/image/fetch/$s_!3Sxb!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F496efc92-5fc7-468a-813f-cc621079be4b_1920x1080.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!3Sxb!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F496efc92-5fc7-468a-813f-cc621079be4b_1920x1080.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!3Sxb!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F496efc92-5fc7-468a-813f-cc621079be4b_1920x1080.jpeg" width="1456" height="819" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/496efc92-5fc7-468a-813f-cc621079be4b_1920x1080.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:819,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;Pura Penataran Agung Lempuyang | Bulgari Resort Bali&quot;,&quot;title&quot;:&quot;Pura Penataran Agung Lempuyang | Bulgari Resort Bali&quot;,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Pura Penataran Agung Lempuyang | Bulgari Resort Bali" title="Pura Penataran Agung Lempuyang | Bulgari Resort Bali" srcset="https://substackcdn.com/image/fetch/$s_!3Sxb!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F496efc92-5fc7-468a-813f-cc621079be4b_1920x1080.jpeg 424w, https://substackcdn.com/image/fetch/$s_!3Sxb!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F496efc92-5fc7-468a-813f-cc621079be4b_1920x1080.jpeg 848w, https://substackcdn.com/image/fetch/$s_!3Sxb!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F496efc92-5fc7-468a-813f-cc621079be4b_1920x1080.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!3Sxb!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F496efc92-5fc7-468a-813f-cc621079be4b_1920x1080.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">The Proxy Pattern splits the logic from the storage</figcaption></figure></div><p>Next, we could utilise a delegate call from &#8220;The Proxy Contract&#8221; to access and use the business logic within &#8220;The Implementation Contract&#8221; in the context of &#8220;The Proxy&#8217;s&#8221; storage. </p><p>The fallback function within &#8220;The Proxy&#8221; would facilitate this delegate call. A fallback function is a function  that executes when a non-existent function is called on the contract. This allows the contract to respond to arbitrary Ethereum transactions.</p><p>With this, we could access function signatures not defined in &#8220;The Proxy&#8221; and still use the &#8220;The Proxy&#8221; storage. </p><p>If you are unfamiliar with delegate call please refer to this <a href="https://noxx.substack.com/p/evm-deep-dives-the-path-to-shadowy-a5f?utm_source=%2Fprofile%2F80455042-noxx&amp;utm_medium=reader2&amp;s=r">article</a> for an in-depth overview, a deep understanding of it is essential to understand the proxy pattern. </p><p>In short delegate call allowed us to use &#8220;The Implementation Contract&#8221; in the same way a Web2 app uses a library. This separation allows &#8220;The Proxy&#8221; to upgrade its business logic akin to updating a package version.</p><p>The image below shows these 2 contracts and a function call from a user to them. First with the v1 implementation and then with the v2 implementation.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!lsnN!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1c5eb126-aa1e-42e7-b87f-cb0fd76a35e4_681x273.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!lsnN!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1c5eb126-aa1e-42e7-b87f-cb0fd76a35e4_681x273.png 424w, https://substackcdn.com/image/fetch/$s_!lsnN!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1c5eb126-aa1e-42e7-b87f-cb0fd76a35e4_681x273.png 848w, https://substackcdn.com/image/fetch/$s_!lsnN!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1c5eb126-aa1e-42e7-b87f-cb0fd76a35e4_681x273.png 1272w, https://substackcdn.com/image/fetch/$s_!lsnN!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1c5eb126-aa1e-42e7-b87f-cb0fd76a35e4_681x273.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!lsnN!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1c5eb126-aa1e-42e7-b87f-cb0fd76a35e4_681x273.png" width="681" height="273" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/1c5eb126-aa1e-42e7-b87f-cb0fd76a35e4_681x273.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:273,&quot;width&quot;:681,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:51215,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!lsnN!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1c5eb126-aa1e-42e7-b87f-cb0fd76a35e4_681x273.png 424w, https://substackcdn.com/image/fetch/$s_!lsnN!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1c5eb126-aa1e-42e7-b87f-cb0fd76a35e4_681x273.png 848w, https://substackcdn.com/image/fetch/$s_!lsnN!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1c5eb126-aa1e-42e7-b87f-cb0fd76a35e4_681x273.png 1272w, https://substackcdn.com/image/fetch/$s_!lsnN!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1c5eb126-aa1e-42e7-b87f-cb0fd76a35e4_681x273.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">The Proxy Pattern - Execution flow when function is called</figcaption></figure></div><p>The Delegate Proxy was a big step forward however it was not without its challenges </p><h4>Problems With The Delegate Proxy</h4><p>Several issues came from the new proxy architecture. Two key ones focused on collisions between the 2 contracts. Storage slot collisions and function signature collisions.</p><h4>Storage Collisions</h4><p>In Solidity, the storage layout is determined by the order of your variable declarations in your code. Alterations to this ordering during upgrades can lead to storage collisions - a critical issue where data is incorrectly read or overwritten. </p><p>If you&#8217;re unfamiliar with storage slots read <a href="https://open.substack.com/pub/noxx/p/evm-deep-dives-the-path-to-shadowy-3ea?r=1bwfia&amp;utm_campaign=post&amp;utm_medium=web">this article</a> for a deep dive.</p><p>Broadly these collisions could be broken into 2 types:</p><ul><li><p>Storage collisions between &#8220;The Proxy&#8221; and &#8220;The Implementation&#8221;</p></li><li><p>Storage collisions between different versions of &#8220;The Implementation&#8221;</p></li></ul><h5>Proxy and Implementation Storage Collisions</h5><p>The proxy pattern requires &#8220;The Proxy&#8221; and its &#8220;Implementation Contract&#8221; to share the same storage layout. If there's a mismatch it can lead to storage collisions.</p><p>A storage collision is where 2 different variables are assigned the same storage slot across 2 contracts. This can lead to a variable being incorrectly read or overwritten.</p><p>Say &#8220;The Proxy&#8221; has varA at storage slot 0 while &#8220;The Implementation&#8221; has varB at storage slot 0. You can see how this could cause issues when a delegate call is made from &#8220;The Proxy&#8221; to &#8220;The Implementation&#8221;. </p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!FwS9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e895e04-b9f8-487c-ac11-a49153125fe4_645x196.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!FwS9!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e895e04-b9f8-487c-ac11-a49153125fe4_645x196.png 424w, https://substackcdn.com/image/fetch/$s_!FwS9!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e895e04-b9f8-487c-ac11-a49153125fe4_645x196.png 848w, https://substackcdn.com/image/fetch/$s_!FwS9!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e895e04-b9f8-487c-ac11-a49153125fe4_645x196.png 1272w, https://substackcdn.com/image/fetch/$s_!FwS9!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e895e04-b9f8-487c-ac11-a49153125fe4_645x196.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!FwS9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e895e04-b9f8-487c-ac11-a49153125fe4_645x196.png" width="645" height="196" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5e895e04-b9f8-487c-ac11-a49153125fe4_645x196.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:196,&quot;width&quot;:645,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:29560,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!FwS9!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e895e04-b9f8-487c-ac11-a49153125fe4_645x196.png 424w, https://substackcdn.com/image/fetch/$s_!FwS9!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e895e04-b9f8-487c-ac11-a49153125fe4_645x196.png 848w, https://substackcdn.com/image/fetch/$s_!FwS9!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e895e04-b9f8-487c-ac11-a49153125fe4_645x196.png 1272w, https://substackcdn.com/image/fetch/$s_!FwS9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e895e04-b9f8-487c-ac11-a49153125fe4_645x196.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption">Storage collision between &#8220;The Proxy&#8221; &amp; &#8220;The Implementation&#8221;</figcaption></figure></div><p>This isn&#8217;t an unlikely scenario, &#8220;The Proxy&#8221; will  likely have variables that &#8220;The Implementation&#8221; does not. For example, &#8220;The Proxy&#8221; needs to store the address of &#8220;The Implementation&#8221; contract at some storage slot. </p><p>It&#8217;s key that &#8220;The Implementation&#8221; contract doesn&#8217;t overwrite the implementation address slot, if it does it will have effectively broken &#8220;The Proxy&#8221;.</p><p>This was such a common problem that a standard, <a href="https://eips.ethereum.org/EIPS/eip-1967">ERC-1967</a>, was created. </p><p>This defined a specific storage slot (one guaranteed to be never allocated by a compiler) where &#8220;The Implementation&#8221; address should be stored.</p><h5>Implementation Version Storage Collisions</h5><p>When upgrading &#8220;The Implementation&#8221; contract, changes in the order or types of state variables can cause storage slots to be reassigned.</p><p>Take a look at the below example. </p><pre><code><strong>Original Contract Storage Layout:</strong>

address owner; // Slot 0
uint256 totalSupply; // Slot 1
mapping(address =&gt; uint256) balances; // Slot 2 (starting point of a dynamic mapping)


<strong>Upgraded Contract:</strong> 

uint256 rewardMultiplier; // Now at Slot 0
address owner; // Moved to Slot 1
uint256 totalSupply; // Moved to Slot 2
mapping(address =&gt; uint256) balances; // Slot 3 (starting point of the dynamic mapping has shifted)</code></pre><p>It&#8217;s easy to see how this slot reassignment could cause havoc for this contract and open many security vulnerabilities. One obvious example is the moving of the owner variable to a new slot.</p><p>If the contract logic attempts to access &#8220;owner&#8221; using its original slot 0, it would mistakenly interact with &#8220;rewardMultiplier&#8221; instead.</p><p>Care needs to be taken when upgrading &#8220;The Implementation&#8221; to ensure the storage layout isn&#8217;t compromised. </p><h4>Function Collisions</h4><p>Another major issue was function signature collisions.</p><p>To understand function signature collisions first you must understand how the EVM interprets function calls to a solidity contract. </p><p>An in-depth overview of this can be found <a href="https://open.substack.com/pub/noxx/p/evm-deep-dives-the-path-to-shadowy?r=1bwfia&amp;utm_campaign=post&amp;utm_medium=web">here</a> if you need a refresher.</p><p>In summary, function selection within a contract is determined by the 4-byte function signature. These are derived from the name of the function and its input types.</p><p>Collisions of these signatures can occur, leading to ambiguity in function calls and security vulnerabilities. Let&#8217;s highlight one of these vulnerabilities.</p><pre><code><strong>Proxy Contract:</strong><code>

function collate_propagate_storage(bytes16) external {
        implementation.delegatecall(abi.encodeWithSignature(
            "transfer(address,uint256)", proxyOwner, 1000
        ));
}


</code><strong>Implementation Contract:</strong><code> 

function burn(uint256 value) public virtual {
        _burn(_msgSender(), value);
}

function transfer(address to, uint256 value) public virtual returns (bool) {
        address owner = _msgSender();
        _transfer(owner, to, value);
        return true;
}</code></code></pre><p>The above highlights certain functions in &#8220;The Proxy&#8221; and  &#8220;The Implementation&#8221; contracts. </p><p>The setup for this vulnerability is that an untrustworthy proxy is pointing to a trusted implementation contract. </p><p>This untrustworthy proxy has implemented a new function collate_propagate_storage(bytes16) in &#8220;The Proxy&#8221; contract. </p><p>A user comes along to interact with &#8220;The Proxy&#8221;,  let&#8217;s say they heard there was an associated airdrop if they used it. They focus on checking &#8220;The Implementation&#8221; to verify it isn&#8217;t doing anything malicious, this is where all business logic is located. </p><p>&#8220;The Implementation&#8221; uses a standard OpenZepplin contract which is trusted and has been thoroughly tested. They notice collate_propagate_storage(bytes16) in &#8220;The Proxy&#8221; but think nothing of it. It is not the function or code they will be interacting with. </p><p>The user now satisfied calls burn(1) on &#8220;The Proxy&#8221; to burn one of their tokens. When the transaction lands on-chain they see that instead of burning 1 token they have transferred 1000 tokens to another unknown account.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!zmoq!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F913365ee-3d57-42ae-bd36-87cc33743c7f_1380x248.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!zmoq!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F913365ee-3d57-42ae-bd36-87cc33743c7f_1380x248.png 424w, https://substackcdn.com/image/fetch/$s_!zmoq!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F913365ee-3d57-42ae-bd36-87cc33743c7f_1380x248.png 848w, https://substackcdn.com/image/fetch/$s_!zmoq!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F913365ee-3d57-42ae-bd36-87cc33743c7f_1380x248.png 1272w, https://substackcdn.com/image/fetch/$s_!zmoq!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F913365ee-3d57-42ae-bd36-87cc33743c7f_1380x248.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!zmoq!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F913365ee-3d57-42ae-bd36-87cc33743c7f_1380x248.png" width="1380" height="248" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/913365ee-3d57-42ae-bd36-87cc33743c7f_1380x248.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:248,&quot;width&quot;:1380,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:49937,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!zmoq!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F913365ee-3d57-42ae-bd36-87cc33743c7f_1380x248.png 424w, https://substackcdn.com/image/fetch/$s_!zmoq!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F913365ee-3d57-42ae-bd36-87cc33743c7f_1380x248.png 848w, https://substackcdn.com/image/fetch/$s_!zmoq!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F913365ee-3d57-42ae-bd36-87cc33743c7f_1380x248.png 1272w, https://substackcdn.com/image/fetch/$s_!zmoq!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F913365ee-3d57-42ae-bd36-87cc33743c7f_1380x248.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption">User calls &#8220;The Proxy&#8221; with burn(1)</figcaption></figure></div><p><em><strong>What just happened?</strong></em></p><p>Let&#8217;s look at the function signatures of burn and collate_propagate_storage.</p><ul><li><p>burn(uint256) = 0x42966c68</p></li><li><p>collate_propagate_storage(bytes16) = 0x42966c68</p></li></ul><p>You can check these out for yourself <a href="https://emn178.github.io/online-tools/keccak_256.html">here</a>, paste the function name and input types above into the keccak256 simulator and see the resulting hash. </p><p>Note that the full hash is different but it doesn&#8217;t matter since we only need the first 4 bytes to match.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!FT3p!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F10d3dc44-06af-4b83-8d4b-97456afee9b4_1380x280.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!FT3p!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F10d3dc44-06af-4b83-8d4b-97456afee9b4_1380x280.png 424w, https://substackcdn.com/image/fetch/$s_!FT3p!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F10d3dc44-06af-4b83-8d4b-97456afee9b4_1380x280.png 848w, https://substackcdn.com/image/fetch/$s_!FT3p!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F10d3dc44-06af-4b83-8d4b-97456afee9b4_1380x280.png 1272w, https://substackcdn.com/image/fetch/$s_!FT3p!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F10d3dc44-06af-4b83-8d4b-97456afee9b4_1380x280.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!FT3p!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F10d3dc44-06af-4b83-8d4b-97456afee9b4_1380x280.png" width="1380" height="280" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/10d3dc44-06af-4b83-8d4b-97456afee9b4_1380x280.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:280,&quot;width&quot;:1380,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:56996,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!FT3p!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F10d3dc44-06af-4b83-8d4b-97456afee9b4_1380x280.png 424w, https://substackcdn.com/image/fetch/$s_!FT3p!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F10d3dc44-06af-4b83-8d4b-97456afee9b4_1380x280.png 848w, https://substackcdn.com/image/fetch/$s_!FT3p!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F10d3dc44-06af-4b83-8d4b-97456afee9b4_1380x280.png 1272w, https://substackcdn.com/image/fetch/$s_!FT3p!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F10d3dc44-06af-4b83-8d4b-97456afee9b4_1380x280.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption">What the EVM sees</figcaption></figure></div><p>When the user called burn(uint256) the following occurred:</p><ul><li><p> The EVM didn&#8217;t see a call to burn(uint256) it saw a function call to 0x42966c68. </p></li><li><p>Since this function signature existed in &#8220;The Proxy&#8221; as collate_propagate_storage(bytes16) the call wasn&#8217;t passed to the fallback function. </p></li><li><p>Instead, it was passed to collate_propagate_storage(bytes16). This in turn called a transfer of 1000 tokens to the proxyOwner. </p></li></ul><p>(See <a href="https://forum.openzeppelin.com/t/beware-of-the-proxy-learn-how-to-exploit-function-clashing/1070">here</a> for a deeper breakdown by tincho).</p><p>While the example above highlights an exploit the same is true when &#8220;The Proxy&#8221; and &#8220;The Implementation&#8221; have functions with identical signatures for non-malicous valid reasons.</p><p>Say both contracts have an updateSettings() function. When a user tries to invoke this function how does the contract know which function you are intending to call, &#8220;The Proxy&#8221; or &#8220;The Implementation&#8221;? </p><p>This ambiguity can lead to unintended errors or even malicious exploits. </p><p>It was such a serious problem that a new proxy was created to solve this exact issue, the transparent proxy.</p><div><hr></div><h4>The Transparent Proxy </h4><p>The core idea of the transparent proxy was to have 2 different execution paths for admin and non-admin users.</p><p>If the admin was calling the contract &#8220;The Proxy&#8221; functions would be available. For anyone else, all calls would be delegated to &#8220;The Implementation&#8221; via the fallback even if there was a matching function signature.</p><p>This removed the ambiguity, admins could interact with &#8220;The Proxy&#8221; functions and non-admins could only interact with the &#8220;The Implementation&#8221; functions.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!VZOs!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4dcb5da5-feef-48b1-b5d6-c593be78890f_938x606.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!VZOs!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4dcb5da5-feef-48b1-b5d6-c593be78890f_938x606.png 424w, https://substackcdn.com/image/fetch/$s_!VZOs!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4dcb5da5-feef-48b1-b5d6-c593be78890f_938x606.png 848w, https://substackcdn.com/image/fetch/$s_!VZOs!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4dcb5da5-feef-48b1-b5d6-c593be78890f_938x606.png 1272w, https://substackcdn.com/image/fetch/$s_!VZOs!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4dcb5da5-feef-48b1-b5d6-c593be78890f_938x606.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!VZOs!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4dcb5da5-feef-48b1-b5d6-c593be78890f_938x606.png" width="938" height="606" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/4dcb5da5-feef-48b1-b5d6-c593be78890f_938x606.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:606,&quot;width&quot;:938,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:41402,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!VZOs!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4dcb5da5-feef-48b1-b5d6-c593be78890f_938x606.png 424w, https://substackcdn.com/image/fetch/$s_!VZOs!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4dcb5da5-feef-48b1-b5d6-c593be78890f_938x606.png 848w, https://substackcdn.com/image/fetch/$s_!VZOs!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4dcb5da5-feef-48b1-b5d6-c593be78890f_938x606.png 1272w, https://substackcdn.com/image/fetch/$s_!VZOs!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4dcb5da5-feef-48b1-b5d6-c593be78890f_938x606.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">High level set up of the transparent proxy, we deep dive into this later in the article</figcaption></figure></div><p>One drawback of this setup was that normal users would no longer be able to access the read methods on &#8220;The Proxy&#8221;. For example, accessing the getter for &#8220;The Implementation&#8221; address.</p><p>Instead, they would have to use web3.eth.getStorageAt( ) and the problem with getStorageAt( ) is that you need to know where in the storage to look. </p><h5>ERC-1967</h5><p>Before <a href="https://eips.ethereum.org/EIPS/eip-1967">ERC-1967</a>, the standard we mentioned earlier, various proxies would implement different storage locations for &#8220;The Implementation&#8221; address.</p><p>This meant third-party apps like Etherscan couldn&#8217;t identify which slot to check to get information about &#8220;The Implementation&#8221;.</p><p><a href="https://eips.ethereum.org/EIPS/eip-1967">ERC-1967</a> solved this by providing a pre-defined storage slot for &#8220;The Implementation&#8221; address.</p><p>If we look at Etherscan they show the code for both &#8220;The Proxy&#8221; and &#8220;The Implementation&#8221; on their explorer. This is only possible if we have a known storage slot for obtaining &#8220;The Implementation&#8221; address. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!-6bU!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F03d1bcc0-b1fa-40ac-b8bf-194796342834_634x570.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!-6bU!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F03d1bcc0-b1fa-40ac-b8bf-194796342834_634x570.png 424w, https://substackcdn.com/image/fetch/$s_!-6bU!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F03d1bcc0-b1fa-40ac-b8bf-194796342834_634x570.png 848w, https://substackcdn.com/image/fetch/$s_!-6bU!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F03d1bcc0-b1fa-40ac-b8bf-194796342834_634x570.png 1272w, https://substackcdn.com/image/fetch/$s_!-6bU!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F03d1bcc0-b1fa-40ac-b8bf-194796342834_634x570.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!-6bU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F03d1bcc0-b1fa-40ac-b8bf-194796342834_634x570.png" width="634" height="570" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/03d1bcc0-b1fa-40ac-b8bf-194796342834_634x570.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:570,&quot;width&quot;:634,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!-6bU!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F03d1bcc0-b1fa-40ac-b8bf-194796342834_634x570.png 424w, https://substackcdn.com/image/fetch/$s_!-6bU!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F03d1bcc0-b1fa-40ac-b8bf-194796342834_634x570.png 848w, https://substackcdn.com/image/fetch/$s_!-6bU!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F03d1bcc0-b1fa-40ac-b8bf-194796342834_634x570.png 1272w, https://substackcdn.com/image/fetch/$s_!-6bU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F03d1bcc0-b1fa-40ac-b8bf-194796342834_634x570.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Etherscan: Both &#8220;The Proxy&#8221; and &#8220;The Implementation&#8221; contracts are available </figcaption></figure></div><p>The ERC-1967 also provided defined slots for the &#8220;beacon address&#8221; (we&#8217;ll touch on this later) and the &#8220;admin address&#8221; as well as ensuring events were emitted when any were changed.</p><h4>Code Deep Dive</h4><p>Let&#8217;s now take a look through the OpenZepplin implementation of the transparent proxy and the 2 execution paths (admin &amp; non-admin) to get a better understanding of what&#8217;s going on.</p><p>We&#8217;ll start with the user (non-admin) execution path.</p><h4>User Access (Non-Admin)</h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Bwi7!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8a6a62c-35a8-41a0-82f3-59bac0821617_2082x1818.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Bwi7!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8a6a62c-35a8-41a0-82f3-59bac0821617_2082x1818.png 424w, https://substackcdn.com/image/fetch/$s_!Bwi7!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8a6a62c-35a8-41a0-82f3-59bac0821617_2082x1818.png 848w, https://substackcdn.com/image/fetch/$s_!Bwi7!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8a6a62c-35a8-41a0-82f3-59bac0821617_2082x1818.png 1272w, https://substackcdn.com/image/fetch/$s_!Bwi7!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8a6a62c-35a8-41a0-82f3-59bac0821617_2082x1818.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Bwi7!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8a6a62c-35a8-41a0-82f3-59bac0821617_2082x1818.png" width="1200" height="1047.5274725274726" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e8a6a62c-35a8-41a0-82f3-59bac0821617_2082x1818.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:1271,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:482717,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Bwi7!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8a6a62c-35a8-41a0-82f3-59bac0821617_2082x1818.png 424w, https://substackcdn.com/image/fetch/$s_!Bwi7!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8a6a62c-35a8-41a0-82f3-59bac0821617_2082x1818.png 848w, https://substackcdn.com/image/fetch/$s_!Bwi7!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8a6a62c-35a8-41a0-82f3-59bac0821617_2082x1818.png 1272w, https://substackcdn.com/image/fetch/$s_!Bwi7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8a6a62c-35a8-41a0-82f3-59bac0821617_2082x1818.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><ol><li><p>Let&#8217;s start with the inheritance structure of the transparent proxy, we have 3 contracts one of which is abstract. An abstract contract is akin to an abstract class in that it cannot be instantiated on its own because it contains at least one function without a concrete implementation (this must be defined by the developer). Our core proxy contract &#8220;TransparentUpgradeableProxy&#8221;  inherits from &#8220;ERC1967Proxy&#8221; which inherits from the &#8220;Abstract Proxy&#8221;. These represent the 3 contracts you see above.</p></li><li><p>Next, let&#8217;s briefly touch on the constructor. Constructors are executed in the order of inheritance, starting from the base class to the derived class. This means first the &#8220;ERC1967Proxy&#8221; constructor will be called then &#8220;TransparentUpgradeableProxy&#8221;. The &#8220;Abstract Proxy&#8221; contract has no constructor. Inherited contract constructors are automatically called however we must explicitly call them if they take in parameters such as &#8220;ERC1967Proxy&#8221; does. This is why we have ERC1967Proxy(_logic, _data) in the &#8220;TransparentUpgradeableProxy&#8221; constructor. This is the syntax for explicitly calling the constructor with specific inputs.</p></li><li><p>Now let&#8217;s start with the actual function call. A non-admin user will make a call to &#8220;TransparentUpgradeableProxy&#8221;. Note that every function in all 3 contracts above is private (denoted by _ prefix), as a result, any call is going to be passed to the fallback( ) function. Note the difference between _fallback( ) which is private and fallback( ) which is the actual fallback function. There is no fallback( ) in &#8220;TransparentUpgradeableProxy&#8221; but there is in the inherited &#8220;Abstract Proxy&#8221; contract. That will be our entry point.</p></li><li><p>The fallback( ) function just passes us along to the internal _fallback( ) function. A _fallback( ) function exists in both the &#8220;Abstract Proxy&#8221; contract and the &#8220;TransparentUpgradeableProxy&#8221; contract. Since &#8220;TransparentUpgradeableProxy&#8221; is the derived contract its _fallback( ) overrides &#8220;Abstract Proxy&#8221; and is therefore where the call goes.</p></li><li><p>At the _fallback( ) there are some checks to see if the user is the admin user, since we are not the admin we will be passed to super._fallback( ). Super is a keyword to call a function in the parent class, in our case &#8220;ERC1967Proxy&#8221;.</p></li><li><p>Since &#8220;ERC1967Proxy&#8221; doesn&#8217;t contain a _fallback( ) function we need to go up one level to the _fallback( ) in &#8220;Abstract Proxy&#8221;. This _fallback( ) in turn calls _delegate(_implementation( )) where _implementation( ) returns the address of the implementation contract.</p></li><li><p>The _delegate( ) implementation utilises some inline assembly to make the delegate call. (See <a href="https://www.evm.codes/?fork=shanghai">evm.codes</a> for a breakdown of each OPCODE)</p><ol><li><p><strong>Line 27</strong>: calldatacopy(destOffset = 0, srcOffset = 0, length = calldatasize())</p><ol><li><p>This copies the calldata, at offset 0 of length calldatasize() into memory at offset 0 to be used in the delegate call.</p></li></ol></li><li><p><strong>Line 31</strong>:<strong> </strong>delegatecall(g = gas(), a = implementation, in = 0, insize = calldatasize(), out = 0, outsize = 0)</p><ol><li><p>g = gas - The amount of gas to be sent along with the call. This must be enough for execution.</p></li><li><p>a = address - The address of the contract for the delegate call, in our case the implementation contract.</p></li><li><p>in = start memory location input - This marks the start position of the input data in memory that will be sent to the target contract, remember that calldatacopy copied to memory position 0. </p></li><li><p>insize = input size - The size (in bytes) of the input data, in our case the calldatasize( ) since we are passing everything along.</p></li><li><p>out = start memory location output - Marks the start position in memory where the output data of the delegate call will be stored, position 0 is selected.</p></li><li><p>outsize = output size - The size (in bytes) of the output area in memory, in our case this is 0 which means nothing will be stored in memory. </p></li><li><p>Note the output value (not the result) of the delegate call will be stored in the return data buffer. This can be accessed using returnDataCopy. This means the return value is still available even though we didn&#8217;t save it to memory.</p></li></ol></li><li><p>The variable &#8220;result&#8221; captures whether the delegate call was executed successfully or not. 0 indicates that the execution was unsuccessful.</p></li><li><p> <strong>Line 34</strong>: returndatacopy(destOffset = 0, srcOffset = 0, length = returndatasize())</p><ol><li><p>This copies the contents of the return data buffer at offset 0 of length returndatasize() (which contains the output of our delegate call) into memory at offset 0.</p></li><li><p>You may have noticed that returndatacopy() just copies the return data to memory and asked why didn&#8217;t we do that in the delegate call with &#8220;out&#8221; and &#8220;outsize&#8221;. The issue is that we didn&#8217;t know the size of the return data at that point. If we had we could have copied the return data to memory immediately via the delegate call removing the need for returndatacopy().</p></li></ol></li><li><p><strong>Line 36</strong>: A switch statement, in both cases the output is returned to the user either via a return or a revert depending on whether the delegate call was successful or not.</p><ol><li><p>return(offset = 0, size = returndatasize()) - Returns from memory starting at the offset 0 the specified size returndatasize() which is the output of the delegate call.</p></li><li><p>revert(offset = 0, size = returndatasize()) - Same as return but state changes are reverted and unused gas is returned to the caller.</p></li></ol></li></ol></li></ol><p>With that we have covered the User (Non-Admin) flow, now let&#8217;s take a quick look at the Admin flow for the Transparent Proxy.</p><h4>Admin Access</h4><p>The Admin flow introduces a new contract &#8220;The Proxy Admin&#8221; and the library ERC1967Utils. Below you will see how they are utilised.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!iYBr!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff483e84d-9248-4c90-9203-1c0606183581_2062x1521.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!iYBr!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff483e84d-9248-4c90-9203-1c0606183581_2062x1521.png 424w, https://substackcdn.com/image/fetch/$s_!iYBr!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff483e84d-9248-4c90-9203-1c0606183581_2062x1521.png 848w, https://substackcdn.com/image/fetch/$s_!iYBr!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff483e84d-9248-4c90-9203-1c0606183581_2062x1521.png 1272w, https://substackcdn.com/image/fetch/$s_!iYBr!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff483e84d-9248-4c90-9203-1c0606183581_2062x1521.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!iYBr!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff483e84d-9248-4c90-9203-1c0606183581_2062x1521.png" width="1200" height="885.1648351648352" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f483e84d-9248-4c90-9203-1c0606183581_2062x1521.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:1074,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:423092,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!iYBr!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff483e84d-9248-4c90-9203-1c0606183581_2062x1521.png 424w, https://substackcdn.com/image/fetch/$s_!iYBr!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff483e84d-9248-4c90-9203-1c0606183581_2062x1521.png 848w, https://substackcdn.com/image/fetch/$s_!iYBr!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff483e84d-9248-4c90-9203-1c0606183581_2062x1521.png 1272w, https://substackcdn.com/image/fetch/$s_!iYBr!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff483e84d-9248-4c90-9203-1c0606183581_2062x1521.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p><ol><li><p>To understand the admin flow we first need to see who the _admin is in our core proxy contract &#8220;TransparentUpgradeableProxy&#8221;. We can see admin is set in the constructor. The constructor initialises a &#8220;ProxyAdmin&#8221; contract and sets the _admin as the ProxyAdmin contract address. This means it is the &#8220;ProxyAdmin&#8221; contract, not the ProxyAdmin owner EOA that is authorised. </p></li><li><p>Because of this, our call from our admin user must go through the &#8220;ProxyAdmin&#8221; contract not directly to &#8220;TransparentUpgradeableProxy&#8221;. We must be the owner of the &#8220;ProxyAdmin&#8221; contract.</p></li><li><p>We call upgradeAndCall passing in the proxy we want to target, the new implementation address and the data for an (optional) call on that new implementation. This in turn calls upgradeToAndCall on the proxy.</p></li><li><p>As explained before all calls will end at the fallback since all other methods are private. The fallback( ) then calls the private _fallback( ) method.</p></li><li><p>We have the admin check again using msg.sender but this time we are the _proxyAdmin( ). Note _proxyAdmin is just a getter on _admin. This a good time to remind you of the difference between tx.origin and msg.sender. tx.origin refers to the original external account (EOA) that initiated the transaction, in this case, the owner of the ProxyAdmin contract. msg.sender is the immediate caller of this contract which in this case is the ProxyAdmin contract. We pass the admin check, then verify only the specific method &#8220;upgradeToAndCall&#8221; is being called. if it isn&#8217;t revert the call, if it is, call the function _dispatchUpgradeToAndCall( ).</p></li><li><p>_dispatchUpgradeToAndCall( ) grabs the new implementation address from the calldata and then uses the ERC1967Utils.UpgradeToAndCall, passing in the new implementation address and any data for a subsequent call.</p></li><li><p> ERC1967Utils.UpgradeToAndCall verifies the code at the new implementation address is non-zero and then updates the storage space specified in ERC-1967 with the new implementation address.</p></li><li><p>If the data length is &gt; 0 it means the user wanted to make some call after the update so make that delegate call at the new address. If data length is 0 verify the call has no value, this is just to prevent funds getting stuck in the contract. </p></li></ol><p>And that&#8217;s the Admin flow.</p><div><hr></div><p>Concepts have become code, you have now seen how the theory is implemented within solidity. This will help us deepen our understanding of how proxies work and potential security vulnerabilities to watch out for.  </p><p>However the transparent proxy was not the last iteration of the proxy pattern, there is one more that we need to review. The reasons for why this proxy was created are primarily focused on the gas usage of the transparent proxy. </p><p>The introduction of the admin check within &#8220;The Proxy&#8221; meant that the admin needed to be loaded from storage on every call. Solidity devs will know loading from storage is one of the most expensive OPCODES in the EVM.</p><p>This gas overhead for users (and therefore increased costs) led to the development of the UUPS Proxy (Universally Upgradable Proxy Standard).</p><div><hr></div><h3>UUPS Proxy </h3><p>The key differentiator with the UUPS proxy was moving the &#8220;upgradeToAndCall&#8221; logic from &#8220;The Proxy&#8221; contract to &#8220;The Implementation&#8221; contract.<br><br>This change meant &#8220;The Proxy&#8221; became a simple forwarder of all calls to &#8220;The Implementation&#8221; contract via delegate call.</p><p>With the auth now in &#8220;The Implementation&#8221;, we no longer needed the ProxyAdmin contract and we reduced the gas overhead of checking msg.sender against the &#8220;admin&#8221; for every call to &#8220;The Proxy&#8221;. </p><p>Instead, the authorisation logic and subsequently the gas-expensive SLOAD of the admin address is only executed when upgradeToAndCall is called. As a result, all non-admin user calls avoid this SLOAD.</p><p>OpenZepplin provides an <a href="https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/proxy/utils/UUPSUpgradeable.sol">abstract UUPS contract</a> that can be used as a base for your &#8220;Implementation&#8221;. It leaves one function _authorizeUpgrade(address newImplementation) undefined so that you, the developer, can implement your own custom authorisation. Here features such as time lock upgrades, multi-sig upgrades, etc. can be implemented.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!K0a4!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F57adfaf4-a2a2-4f30-89ad-588079f15d7b_6000x3376.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!K0a4!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F57adfaf4-a2a2-4f30-89ad-588079f15d7b_6000x3376.jpeg 424w, https://substackcdn.com/image/fetch/$s_!K0a4!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F57adfaf4-a2a2-4f30-89ad-588079f15d7b_6000x3376.jpeg 848w, https://substackcdn.com/image/fetch/$s_!K0a4!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F57adfaf4-a2a2-4f30-89ad-588079f15d7b_6000x3376.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!K0a4!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F57adfaf4-a2a2-4f30-89ad-588079f15d7b_6000x3376.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!K0a4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F57adfaf4-a2a2-4f30-89ad-588079f15d7b_6000x3376.jpeg" width="1456" height="819" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/57adfaf4-a2a2-4f30-89ad-588079f15d7b_6000x3376.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:819,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1763560,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!K0a4!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F57adfaf4-a2a2-4f30-89ad-588079f15d7b_6000x3376.jpeg 424w, https://substackcdn.com/image/fetch/$s_!K0a4!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F57adfaf4-a2a2-4f30-89ad-588079f15d7b_6000x3376.jpeg 848w, https://substackcdn.com/image/fetch/$s_!K0a4!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F57adfaf4-a2a2-4f30-89ad-588079f15d7b_6000x3376.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!K0a4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F57adfaf4-a2a2-4f30-89ad-588079f15d7b_6000x3376.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">A custom _authorizeUpgrade must be implemented in &#8220;The Implementation&#8221; contract</figcaption></figure></div><p>When coding your &#8220;Implementation&#8221; contract one important item to be aware of  is initialiser functions. You should know why they exist and their differences from constructors.</p><h5>Constructors vs Initialisers</h5><p>Constructors are used for initialising state in contracts. They are executed once at contract deployment and their code isn&#8217;t included in the contract bytecode.  </p><p>In the proxy pattern, &#8220;The Proxy&#8221; (holding the state) and &#8220;The Implementation&#8221; (holding the logic) are separate. Because of this any state initialisation done in the constructor of &#8220;The Implementation&#8221; affects only &#8220;The Implementation&#8221; contract's storage, not &#8220;The Proxy's&#8221;.</p><p>To address the limitation of constructors in proxy setups, initialiser functions are used. These are designed to set up the initial state in &#8220;The Proxy's&#8221; storage.</p><p>Initializers are designed to be executed via a delegatecall from &#8220;The Proxy&#8221;. This ensures that the state initialised is within &#8220;The Proxy's&#8221; storage, aligning with the intended logic of &#8220;The Implementation&#8221; contract. </p><p>Similar to constructors, initialisers are meant to be executed only once. This is typically enforced through a mechanism, like a boolean flag, to prevent re-initialization, which could lead to security vulnerabilities.</p><p>One thing to be aware of is that, unlike constructors, initializers do not automatically handle inheritance. Under the hood, an initialiser is just a normal function meaning if any of &#8220;The Implementation&#8217;s&#8221; parent contracts have constructors, they need to be explicitly called in the initializer. This differs from a constructor where parent constructors are automatically called.</p><p>When implementing upgradable contracts, extra care must be taken with initialiser functions. Ensuring they are secure and only callable as intended is crucial to maintaining the integrity of the contract.</p><p>Now back from that detour to the advantages and disadvantages of the UUPS Proxy.</p><div><hr></div><h5>Advantages &amp; Disadvantages</h5><p>There are both pros and cons to the UUPS. </p><p>The major advantages are as follows: </p><ul><li><p>Reduction in gas overhead for your users.</p></li><li><p>Removes the need for the ProxyAdmin contract. </p></li><li><p>The upgradeAndCall logic itself becomes upgradable since it&#8217;s in the implementation and the implementation can be upgraded. This includes the ability to eventually remove it and enshrine a contract in its current state.</p></li></ul><p>The disadvantages include: </p><ul><li><p>Reduced separation of concerns, your implementation contract now handles both your auth logic and your business logic. </p></li><li><p>Increase risk when updating the implementation contract. Since your implementation now contains your auth logic every upgrade potentially changes the attack surface of your auth logic.</p></li><li><p>Risk of &#8220;bricking&#8221; the proxy. If there is inadvertently an upgrade to the implementation contract that doesn&#8217;t contain the upgradeAndCall function, the upgrade functionality for the proxy is lost forever. </p></li></ul><p>If you want to dive into the UUPS abstract implementation contract you can <a href="https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/proxy/utils/UUPSUpgradeable.sol">here</a>. </p><p>Our final topics for today are to briefly touch on the minimal proxy (also known as clones) and the beacon proxy. Two proxy concepts you may see out in the wild.</p><div><hr></div><h4>The Minimal Proxy</h4><p>The concept of a minimal proxy is to offer a streamlined approach to deploying multiple instances of a contract that share common logic but require individual storage.</p><p>An example of this could be Gnosis Safe contracts, where each Safe is distinct but the underlying multisig logic remains consistent.</p><p>Rather than redeploying the entire logic for each new instance, which is gas-intensive and costly, the minimal proxy pattern involves deploying a single implementation contract and then creating lightweight proxy contracts for each new instance. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!uRHT!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce1219c8-e26f-497b-9454-8c831de67d66_3734x1112.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!uRHT!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce1219c8-e26f-497b-9454-8c831de67d66_3734x1112.png 424w, https://substackcdn.com/image/fetch/$s_!uRHT!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce1219c8-e26f-497b-9454-8c831de67d66_3734x1112.png 848w, https://substackcdn.com/image/fetch/$s_!uRHT!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce1219c8-e26f-497b-9454-8c831de67d66_3734x1112.png 1272w, https://substackcdn.com/image/fetch/$s_!uRHT!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce1219c8-e26f-497b-9454-8c831de67d66_3734x1112.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!uRHT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce1219c8-e26f-497b-9454-8c831de67d66_3734x1112.png" width="1456" height="434" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ce1219c8-e26f-497b-9454-8c831de67d66_3734x1112.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:434,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:396113,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!uRHT!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce1219c8-e26f-497b-9454-8c831de67d66_3734x1112.png 424w, https://substackcdn.com/image/fetch/$s_!uRHT!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce1219c8-e26f-497b-9454-8c831de67d66_3734x1112.png 848w, https://substackcdn.com/image/fetch/$s_!uRHT!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce1219c8-e26f-497b-9454-8c831de67d66_3734x1112.png 1272w, https://substackcdn.com/image/fetch/$s_!uRHT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce1219c8-e26f-497b-9454-8c831de67d66_3734x1112.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Uniswap V3 Pools, Gnosis Safes &amp; Yearn Vaults are all examples of protocols using minimal proxies</figcaption></figure></div><p>Minimal proxies do not include upgradeability or authorization functionality, simplifying their structure and reducing both deployment and runtime gas costs. They are static and immutable once deployed.</p><h4>The Beacon Proxy </h4><p>The Beacon Proxy pattern introduces an efficient upgrade mechanism for systems where multiple proxy contracts require synchronized updates.</p><p>The design utilises a separate contract known as &#8220;The Beacon&#8221;, which holds the implementation address used by all associated proxies. Each &#8220;Proxy&#8221; then simply queries &#8220;The Beacon&#8221; to retrieve the current implementation address rather than holding it themselves.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!lEur!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe93f6385-1f6d-411d-affd-b4857d89ea66_814x510.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!lEur!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe93f6385-1f6d-411d-affd-b4857d89ea66_814x510.png 424w, https://substackcdn.com/image/fetch/$s_!lEur!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe93f6385-1f6d-411d-affd-b4857d89ea66_814x510.png 848w, https://substackcdn.com/image/fetch/$s_!lEur!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe93f6385-1f6d-411d-affd-b4857d89ea66_814x510.png 1272w, https://substackcdn.com/image/fetch/$s_!lEur!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe93f6385-1f6d-411d-affd-b4857d89ea66_814x510.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!lEur!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe93f6385-1f6d-411d-affd-b4857d89ea66_814x510.png" width="814" height="510" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e93f6385-1f6d-411d-affd-b4857d89ea66_814x510.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:510,&quot;width&quot;:814,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:25249,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!lEur!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe93f6385-1f6d-411d-affd-b4857d89ea66_814x510.png 424w, https://substackcdn.com/image/fetch/$s_!lEur!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe93f6385-1f6d-411d-affd-b4857d89ea66_814x510.png 848w, https://substackcdn.com/image/fetch/$s_!lEur!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe93f6385-1f6d-411d-affd-b4857d89ea66_814x510.png 1272w, https://substackcdn.com/image/fetch/$s_!lEur!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe93f6385-1f6d-411d-affd-b4857d89ea66_814x510.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">The Proxies reference &#8220;The Beacon&#8221; for the implementation address</figcaption></figure></div><p>The Beacon Proxy is very useful when an update across numerous proxy instances is required. Going back to our Gnosis Safe example, where each user's Safe is a proxy contract, imagine a critical update was needed.</p><p>Updating the implementation address of each Safe (Proxy) individually would be prohibitively expensive in terms of gas and require huge coordination efforts with users (the deployers and owners of their Proxies) to update. </p><p>With a Beacon Proxy, the maintainers of the platform, such as the Gnosis team, need only to update the implementation address in &#8220;The Beacon&#8221; contract. </p><p>All proxy instances pointed at &#8220;The Beacon&#8221; will be updated as well. </p><p>This not only saves gas (as the implementation address only needs to be updated in one location) but also significantly reduces the operational burden since users don&#8217;t need to perform individual upgrades.</p><p>The one drawback is of course that the pattern does centralise control over the implementation address. The owner of the beacon represents a significant point of trust.</p><p>To mitigate this trust and enhance security, mechanisms such as multi-signature wallets and time locks can be implemented.</p><div><hr></div><p>We&#8217;ve covered a lot in this article, traversing the intricate landscape of Proxy contracts. Hopefully, you&#8217;ve learned new concepts and added one more pattern to your toolkit.</p><p>Till next time.</p><p>noxx</p><p>Twitter <a href="https://twitter.com/noxx3xxon">@noxx3xxon</a></p>]]></content:encoded></item><item><title><![CDATA[Order Flows: Kingmaker of the Block Builders 👑]]></title><description><![CDATA[The Post-Merge Block Builder Battle Royal]]></description><link>https://noxx.substack.com/p/order-flows-kingmaker-of-the-block</link><guid isPermaLink="false">https://noxx.substack.com/p/order-flows-kingmaker-of-the-block</guid><dc:creator><![CDATA[noxx]]></dc:creator><pubDate>Thu, 01 Sep 2022 12:35:51 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!NNjO!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F640e9049-dc51-472b-829a-5dfa6db1e00c_2288x2048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>The Merge is just over 2 weeks away and it is set to change the Ethereum MEV landscape forever. </p><p>Block builders, a new entity in the post-merge world are about to enter a brutal game of battle royal. </p><p>And their weapon of choice - Order Flows.</p><p>In this article we&#8217;ll be discussing how builders will weaponise their order flows, the risks it poses to the Ethereum chain and take a peek at the potential futures that lay before us.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://noxx.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://noxx.substack.com/subscribe?"><span>Subscribe now</span></a></p><h2>State of Play: Pre-Merge</h2><p>Before we take a look at how things could play out post-merge it&#8217;s good for us to have an understanding of how things work at the moment.</p><p>The image below maps out a Uniswap trade from a user. It highlights the actors involved as the transaction makes its way from the user to the miner and shows some of the paths the transaction can take to get on-chain.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!NNjO!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F640e9049-dc51-472b-829a-5dfa6db1e00c_2288x2048.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!NNjO!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F640e9049-dc51-472b-829a-5dfa6db1e00c_2288x2048.png 424w, https://substackcdn.com/image/fetch/$s_!NNjO!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F640e9049-dc51-472b-829a-5dfa6db1e00c_2288x2048.png 848w, https://substackcdn.com/image/fetch/$s_!NNjO!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F640e9049-dc51-472b-829a-5dfa6db1e00c_2288x2048.png 1272w, https://substackcdn.com/image/fetch/$s_!NNjO!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F640e9049-dc51-472b-829a-5dfa6db1e00c_2288x2048.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!NNjO!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F640e9049-dc51-472b-829a-5dfa6db1e00c_2288x2048.png" width="1200" height="1073.901098901099" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/640e9049-dc51-472b-829a-5dfa6db1e00c_2288x2048.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:1303,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:487482,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!NNjO!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F640e9049-dc51-472b-829a-5dfa6db1e00c_2288x2048.png 424w, https://substackcdn.com/image/fetch/$s_!NNjO!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F640e9049-dc51-472b-829a-5dfa6db1e00c_2288x2048.png 848w, https://substackcdn.com/image/fetch/$s_!NNjO!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F640e9049-dc51-472b-829a-5dfa6db1e00c_2288x2048.png 1272w, https://substackcdn.com/image/fetch/$s_!NNjO!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F640e9049-dc51-472b-829a-5dfa6db1e00c_2288x2048.png 1456w" sizes="100vw" loading="lazy" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><ol><li><p>A User has some &#8220;intent&#8221; in this case the intent is to trade some tokens on Uniswap.</p></li><li><p>The User navigates to the app he wants to use and connects using his wallet provider, for this example let&#8217;s say he uses Metamask.</p></li><li><p>The user specifies the trade he wants to make 10 ETH &#8594; 20,000 DAI. The app constructs some call data for him to sign (0x5ae4&#8230;000) which creates a transaction that represents his trade. This signed data is what will be sent to the RPC endpoints in the next section.</p></li><li><p>Let&#8217;s Assume the user hasn&#8217;t changed any of the default settings on Metamask, specifically the RPC Endpoint for the Ethereum network. If this is the case RPC endpoint will be set to <a href="https://mainnet.infura.io/v3/">https://mainnet.infura.io/v3/</a>. This means the transaction will be sent to <a href="https://infura.io/">Infura</a>.</p></li><li><p><a href="https://infura.io/">Infura</a> will then propagate this transaction out to the rest of the nodes in the Ethereum network. When the transactions land at each node they are placed in the public mempool, a staging area for transactions before they are committed to the chain. The transaction will sit here until it is sequenced into a block.</p></li><li><p>Miners create blocks by selecting and sequencing transactions in their mempool. Typically they prioritise high gas transactions first.</p></li><li><p>Potentially unbeknown to this user another group of actors called searchers are monitoring the mempool. They are looking for valuable transactions. When they see the user&#8217;s ETH &#8594; DAI transaction their bot sandwichs it to give him the worst possible execution price and themselves the greatest profit.</p></li><li><p>They do this by submitting bundles of transactions to the Flashbots relay where they are guaranteed to be top of block and that either all the transactions in the bundle execute or none do.</p></li><li><p>These bundles are merged together by Flashbots into a megabundle and sent on to whitelisted miners to be included at the top of the block.</p></li><li><p>If we look back at the start of our journey the user could have changed his RPC endpoint in the Metamask settings. He could have switched to a private RPC endpoint, one that bypasses the mempool. An example of a private RPC endpoint is Flashbots Protect, they guarantee no front running or sandwiching enabling you to avoid the monsters in the dark forest.</p></li><li><p>The private RPC is set up to route to specific miners where agreements are made about what can be done with the transactions. These miners typically will have a custom client (ie mev-geth) which enables them to have a private mempool that doesn&#8217;t broadcast the transactions sent there. The miners extract as much &#8220;ethical&#8221; value (ie via back running) as they can and order them in the block. </p></li></ol><p>The purpose of this is to show you the order flows that already exist. I have highlighted the &#8220;user public order flow&#8221; which comes from the public mempool, the &#8220;user private order flow&#8221; which comes via private RPC endpoints and the &#8220;searcher order flow&#8221; which comes via searcher auctions/relays. </p><p>The public user order flow is available to everyone.  </p><p>The searcher &amp; private user order flows on the other hand are &#8220;exclusive&#8221; to the entities that set up those private relays / RPC endpoints.</p><p>It&#8217;s this exclusive order flow that has the power to change the game.</p><h2>Post-Merge World</h2><p>Ok, that&#8217;s what happens in the pre-merge world but what happens in the post-merge one?</p><p>For us to understand that there are two important changes happening at the merge we need to review. </p><p>PBS, proposer builder separation and the concept of blinded blocks.</p><p>Let&#8217;s start with PBS.</p><h3>Proposer Builder Separation (PBS)</h3><p>PBS is the ability to separate block building from block proposal. Let&#8217;s take a look at each one.</p><h4>Block Building</h4><p>Block building involves ordering/sequencing transactions within a block. It is a game where hardware, algorithms and information determine your success. </p><p>It&#8217;s a competitive game, this means you will need to upgrade your hardware, improve your algorithms and get access to the best order flows (information) to stay in the game. </p><p>These requirements mean block building has a high barrier to entry.</p><h4>Block Proposing</h4><p>Block proposals require you to have put up some stake 32 ETH and run some client software. </p><p>The hardware requirements of this software are currently low enough that an individual staker can afford to maintain their own validator. </p><p>The only way you compete is by staking more ETH the computation requirements do not increase by much as you increase you&#8217;re validator count.</p><p>While the initial stake 32 ETH may be prohibitive for many, the computation requirements have been kept low enough for individual stakers to compete in block proposals.</p><h4>MEV Blocks</h4><p>Let&#8217;s think about what happens in a world where the validator both builds &amp; proposes the block.</p><p>The individual validator, doesn&#8217;t have access to the hardware, algorithms &amp; information required to build a MEV Block so instead builds a vanilla block.</p><p>This vanilla block uses the execution clients default sequencing logic for transactions ordering by gas price. </p><p>The MEV validators, who will be larger incumbent entities with access to the hardware, algorithms &amp; information will be able to produce these more valuable MEV based blocks.</p><h4>MEV Yield</h4><p>There are some estimates that MEV could make up to 60% of a validator&#8217;s yield for the year. </p><p>This means that if our individual validator is making 4% per year with their vanilla blocks the MEV validators with their MEV blocks will be making 10%.</p><p>The diagram below shows what this yield delta can do to the individual validator over time.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Zh5P!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9dbb68f1-4053-47a5-96ac-2b7190965fa1_1804x1844.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Zh5P!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9dbb68f1-4053-47a5-96ac-2b7190965fa1_1804x1844.png 424w, https://substackcdn.com/image/fetch/$s_!Zh5P!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9dbb68f1-4053-47a5-96ac-2b7190965fa1_1804x1844.png 848w, https://substackcdn.com/image/fetch/$s_!Zh5P!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9dbb68f1-4053-47a5-96ac-2b7190965fa1_1804x1844.png 1272w, https://substackcdn.com/image/fetch/$s_!Zh5P!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9dbb68f1-4053-47a5-96ac-2b7190965fa1_1804x1844.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Zh5P!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9dbb68f1-4053-47a5-96ac-2b7190965fa1_1804x1844.png" width="1456" height="1488" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/9dbb68f1-4053-47a5-96ac-2b7190965fa1_1804x1844.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1488,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:398263,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Zh5P!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9dbb68f1-4053-47a5-96ac-2b7190965fa1_1804x1844.png 424w, https://substackcdn.com/image/fetch/$s_!Zh5P!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9dbb68f1-4053-47a5-96ac-2b7190965fa1_1804x1844.png 848w, https://substackcdn.com/image/fetch/$s_!Zh5P!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9dbb68f1-4053-47a5-96ac-2b7190965fa1_1804x1844.png 1272w, https://substackcdn.com/image/fetch/$s_!Zh5P!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9dbb68f1-4053-47a5-96ac-2b7190965fa1_1804x1844.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Let&#8217;s run through what&#8217;s going on here.</p><ol><li><p>This equation represents the compounding interest of the individual validator.</p><ol><li><p>The 1.04 represents a 4% annual yield.</p></li><li><p>x represents the number of years the node has been running for. </p></li><li><p>Note in reality staking doesn't auto compound your rewards you need to take out what you have earnt and create a new validator / buy more liquid staking tokens.</p></li></ol></li><li><p>This equation represents the compounding interest for the MEV validator.</p><ol><li><p> The 1.10 represents the 10% annual yield.</p></li></ol></li><li><p>The graph shows the returns of the 2 validators over the course of 30 years. We can clearly see that as the yield difference compounds the MEV validator begins to vastly outperform the individual validator. This gets worst at an accelerated rate as time progresses.</p></li><li><p>At the 30-year stage, the individual validator has earned 2.25x its original investment while the MEV validator has earned 16.5x. </p><ol><li><p>The MEV validator has been able to take these profits and spin up new validators at a much faster rate than the individual validator reducing their market share. </p></li><li><p>It&#8217;s clear to see that if the entire validator set was split 50/50 with one half being individual validators and the other half being MEV validators the MEV validators quickly gain dominance in the validator set. </p></li></ol></li></ol><p>This graph shows us how much of a centralising force MEV could be if block building &amp; block proposals are kept together.</p><p>Now you may be thinking if you split the roles you just move this centralising force to the builders rather than the validators and you would be right.</p><p>Implicit in the design of PBS is saying MEV is a centralising force but we would rather the centralisation happen at the builder level than at the validator level.</p><p>It&#8217;s through PBS a new entity officially enters the Ethereum ecosystem. </p><p>An entity focused solely on building blocks.</p><h3>Block Builders</h3><p>Block builders already exist today. </p><p>The mining pools that have private relays with dedicated searcher teams. Flashbots with the Flashbots auction that receives searchers bundles. Both of these entities are given a set of transactions/bundles and order them in a way that captures the most value.</p><p>The mining pools do this for the entire block and Flashbots do this for their megabundles.</p><h4>Pre-Merge: Trust-Based System</h4><p>Pre-merge these &#8220;builders&#8221; would only entrust their &#8220;builds&#8221; to themselves or miners they had agreements with. It was a trust-based system.</p><p>If we look at a &#8220;builder&#8221; like Flashbots, they had to send their megabundles to the miners and trust them not to steal the MEV contained within.</p><p>Miners have the ultimate power as validators will post-merge.</p><p>If a searcher sent a transaction for a $10 million liquidation in theory the miner could just call that liquidation with their own address at the top of the block stealing the opportunity.</p><p>To prevent this partnerships were formed with large mining pools where incentives meant stealing an individual transaction wasn&#8217;t worth damaging the relationship with Flashbots and losing their access to the MEV bundles.</p><p>These incentives didn&#8217;t exist for individual miners where stealing the MEV and damaging the relationship may yield the highest expected value given how infrequently they won a block.</p><p>The result of this was a permissioned system with only some miners gaining access to the Flashbots MEV orderflow and profits.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!hvDl!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc6dcc5ba-8ce4-4317-a345-bf8615f7ed30_1462x1090.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!hvDl!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc6dcc5ba-8ce4-4317-a345-bf8615f7ed30_1462x1090.png 424w, https://substackcdn.com/image/fetch/$s_!hvDl!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc6dcc5ba-8ce4-4317-a345-bf8615f7ed30_1462x1090.png 848w, https://substackcdn.com/image/fetch/$s_!hvDl!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc6dcc5ba-8ce4-4317-a345-bf8615f7ed30_1462x1090.png 1272w, https://substackcdn.com/image/fetch/$s_!hvDl!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc6dcc5ba-8ce4-4317-a345-bf8615f7ed30_1462x1090.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!hvDl!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc6dcc5ba-8ce4-4317-a345-bf8615f7ed30_1462x1090.png" width="1456" height="1086" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/c6dcc5ba-8ce4-4317-a345-bf8615f7ed30_1462x1090.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1086,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:137557,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!hvDl!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc6dcc5ba-8ce4-4317-a345-bf8615f7ed30_1462x1090.png 424w, https://substackcdn.com/image/fetch/$s_!hvDl!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc6dcc5ba-8ce4-4317-a345-bf8615f7ed30_1462x1090.png 848w, https://substackcdn.com/image/fetch/$s_!hvDl!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc6dcc5ba-8ce4-4317-a345-bf8615f7ed30_1462x1090.png 1272w, https://substackcdn.com/image/fetch/$s_!hvDl!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc6dcc5ba-8ce4-4317-a345-bf8615f7ed30_1462x1090.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p><h4>Post-Merge: Trustless System</h4><p>In Ethereum post-merge the builder/validator relationship which is analogous to the builder/miner relationship pre-merge moves closer to a trustless model.</p><p>This is enabled via a new feature in Ethereum called &#8220;Blinded Blocks&#8221;.</p><p>Blinded blocks are blocks that only include the block headers present in an Ethereum block. No transactions are sent along with the block.</p><p>The transactions are sent after the validator signs the blinded block and sends it to the builder.</p><p>Since the builder will emit this signed block to the network it means the validator cannot steal the MEV contained within until the block has already begun propagating to other nodes. </p><p>At this point, if the validator were to propose a new block where it &#8220;stole&#8221; a MEV opportunity it would have &#8220;<em><strong>proposed two different blocks at the same height</strong></em>&#8221; which is a slashable offense. </p><p>Additionally, there is no guarantee that the block that claims the MEV opportunity ends up as the canonical one. There's a 50/50 chance the original does and the validator gets slashed and doesn&#8217;t manage to claim the MEV.</p><p>This provides a deterrent to validators since the cost of slashing is likely higher than the potential gain from stolen MEV. (People are looking into cases where the highest expected value is to attempt to steal the MEV however these instances will not be common)</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!dYI4!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff44c2c0b-1af8-4a5d-83c8-a780b436dd6c_2712x2042.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!dYI4!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff44c2c0b-1af8-4a5d-83c8-a780b436dd6c_2712x2042.png 424w, https://substackcdn.com/image/fetch/$s_!dYI4!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff44c2c0b-1af8-4a5d-83c8-a780b436dd6c_2712x2042.png 848w, https://substackcdn.com/image/fetch/$s_!dYI4!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff44c2c0b-1af8-4a5d-83c8-a780b436dd6c_2712x2042.png 1272w, https://substackcdn.com/image/fetch/$s_!dYI4!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff44c2c0b-1af8-4a5d-83c8-a780b436dd6c_2712x2042.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!dYI4!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff44c2c0b-1af8-4a5d-83c8-a780b436dd6c_2712x2042.png" width="1200" height="903.2967032967033" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/f44c2c0b-1af8-4a5d-83c8-a780b436dd6c_2712x2042.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:1096,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:936529,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!dYI4!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff44c2c0b-1af8-4a5d-83c8-a780b436dd6c_2712x2042.png 424w, https://substackcdn.com/image/fetch/$s_!dYI4!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff44c2c0b-1af8-4a5d-83c8-a780b436dd6c_2712x2042.png 848w, https://substackcdn.com/image/fetch/$s_!dYI4!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff44c2c0b-1af8-4a5d-83c8-a780b436dd6c_2712x2042.png 1272w, https://substackcdn.com/image/fetch/$s_!dYI4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff44c2c0b-1af8-4a5d-83c8-a780b436dd6c_2712x2042.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Ethereum Specs - Blinded Blocks Only Have Access to the Transactions Root</figcaption></figure></div><p>Note that when we said that the relationship moves closer to a trustless model, this applies in the direction of the builder to the validator.</p><p>Since the block is blinded the validator can&#8217;t determine if it is a valid block. It has to trust that the builder has sent one.</p><p>The builder is obviously incentivised to send valid blocks, if it doesn&#8217;t it loses out on value it could have captured &amp; may be blacklisted for the future. </p><p>Fortunately, the downside for the validator is limited to missing their block proposal as an invalid block is not a slashable offense.</p><p>In the post-merge world, any validator will be able to connect with any builder and access these MEV transactions via blinded blocks. </p><p>This puts an individual validator on an even playing field with the larger entities as both now have access to the enhanced rewards via MEV.</p><h3>MEV Supply Chain</h3><p>Next, let&#8217;s take a look at the MEV Supply chain in the post-merge world focusing specifically on Builders.</p><p>We&#8217;ll use the &#8220;Flashbots Builder&#8221; as an example and show some of the different fronts they can compete on.</p><ul><li><p>Exclusive Transaction Order Flows</p><ul><li><p>Searcher Order Flow - Flashbots Relay</p></li><li><p>User Private Order Flow - Flashbots Protect</p></li></ul></li><li><p>Internal MEV</p><ul><li><p>Identifying MEV that isn&#8217;t in your &#8220;Searcher Order Flow&#8221;</p></li><li><p>Capturing Ethical MEV from the &#8220;User Private Order Flow&#8221;</p></li></ul></li><li><p>Latency</p><ul><li><p>MEV boost will introduce some latency from the additional network hops. </p></li><li><p>Vertical integration / co-location could limit the time taken for these hops.</p></li><li><p>This could give the builder more time to see transactions / calculate the best block before the deadline to submit. </p></li></ul></li><li><p>Block Building Algorithms </p><ul><li><p>More efficiently packing a block to include the most value.</p></li></ul></li></ul><p>Of all of these &#8220;Exclusive Transaction Order Flows&#8221; will almost certainly be the most important factor.</p><p>Let&#8217;s revist the Uniswap trade that was mapped out earlier but now with the builder/validator model.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!-5XN!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F2a73f5e1-e237-4891-aa2e-f6c53e2c2b83_2288x2450.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!-5XN!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F2a73f5e1-e237-4891-aa2e-f6c53e2c2b83_2288x2450.png 424w, https://substackcdn.com/image/fetch/$s_!-5XN!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F2a73f5e1-e237-4891-aa2e-f6c53e2c2b83_2288x2450.png 848w, https://substackcdn.com/image/fetch/$s_!-5XN!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F2a73f5e1-e237-4891-aa2e-f6c53e2c2b83_2288x2450.png 1272w, https://substackcdn.com/image/fetch/$s_!-5XN!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F2a73f5e1-e237-4891-aa2e-f6c53e2c2b83_2288x2450.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!-5XN!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F2a73f5e1-e237-4891-aa2e-f6c53e2c2b83_2288x2450.png" width="1200" height="1284.89010989011" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/2a73f5e1-e237-4891-aa2e-f6c53e2c2b83_2288x2450.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:1559,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:450776,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!-5XN!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F2a73f5e1-e237-4891-aa2e-f6c53e2c2b83_2288x2450.png 424w, https://substackcdn.com/image/fetch/$s_!-5XN!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F2a73f5e1-e237-4891-aa2e-f6c53e2c2b83_2288x2450.png 848w, https://substackcdn.com/image/fetch/$s_!-5XN!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F2a73f5e1-e237-4891-aa2e-f6c53e2c2b83_2288x2450.png 1272w, https://substackcdn.com/image/fetch/$s_!-5XN!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F2a73f5e1-e237-4891-aa2e-f6c53e2c2b83_2288x2450.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The white box shows where the majority of the change is occurring. I&#8217;ve omitted the relays which act as aggregators of the block builders and deliver the most profitable block to the validator.</p><ol><li><p>Here this private RPC endpoint represents Flashbots Protect. Before these order flows were sent onto the whitelisted Miners. Now they will be exclusive order flows for the Flashbots Builder. Exclusive meaning other competing builders won&#8217;t have access to them.</p></li><li><p>The searcher order flow here is the bundles the Flashbots Relay receives. Again this will be an exclusive order flow, only the Flashbots Builder will have access to it.</p></li><li><p>The public order flow found in the mempool, the Flashbots Builder will also have access to these transactions however so will every other builder meaning it provides no competitive advantage.</p></li><li><p>Validators running mev-boost will be able to hook into this builder network. They will receive blinded blocks from the builders and will propose the block  that delivers the most value to them.</p></li></ol><p>This example looks specifically at Flashbots but we can expect the same themes to play out with every builder. They will have their own searcher and private transaction endpoints which they will be trying to incentivise people to use. This is their exclusive order flow and the key competitive advantage.</p><p>Below is an excellent diagram from Flashbots which shows the dotted white line box above in a bit more detail.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!2fcU!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F4b0a7ea1-822e-4852-9b0b-dd593abcef0f_3003x1039.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!2fcU!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F4b0a7ea1-822e-4852-9b0b-dd593abcef0f_3003x1039.png 424w, https://substackcdn.com/image/fetch/$s_!2fcU!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F4b0a7ea1-822e-4852-9b0b-dd593abcef0f_3003x1039.png 848w, https://substackcdn.com/image/fetch/$s_!2fcU!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F4b0a7ea1-822e-4852-9b0b-dd593abcef0f_3003x1039.png 1272w, https://substackcdn.com/image/fetch/$s_!2fcU!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F4b0a7ea1-822e-4852-9b0b-dd593abcef0f_3003x1039.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!2fcU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F4b0a7ea1-822e-4852-9b0b-dd593abcef0f_3003x1039.png" width="1456" height="504" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/4b0a7ea1-822e-4852-9b0b-dd593abcef0f_3003x1039.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:504,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;mevboost-searcher-bundle-flow.png&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="mevboost-searcher-bundle-flow.png" title="mevboost-searcher-bundle-flow.png" srcset="https://substackcdn.com/image/fetch/$s_!2fcU!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F4b0a7ea1-822e-4852-9b0b-dd593abcef0f_3003x1039.png 424w, https://substackcdn.com/image/fetch/$s_!2fcU!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F4b0a7ea1-822e-4852-9b0b-dd593abcef0f_3003x1039.png 848w, https://substackcdn.com/image/fetch/$s_!2fcU!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F4b0a7ea1-822e-4852-9b0b-dd593abcef0f_3003x1039.png 1272w, https://substackcdn.com/image/fetch/$s_!2fcU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F4b0a7ea1-822e-4852-9b0b-dd593abcef0f_3003x1039.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><a href="https://writings.flashbots.net/writings/searching-post-merge/">Searching Post-Merge</a></figcaption></figure></div><p>Let&#8217;s now zoom in on the &#8220;Private RPC Endpoints&#8221; and the benefits they offer to users.</p><h3>Benefits of Private RPC Endpoints</h3><p>Private RPC endpoints can provide a number of benefits to users some of which we&#8217;ve already touched on. Let&#8217;s run through them.</p><ul><li><p>Pre Trade Privacy </p><ul><li><p>Frontrunning / Sandwhich protection</p></li></ul></li><li><p>Transaction Simulation</p><ul><li><p>Revert Protection - Never include if it fails/reverts</p></li></ul></li><li><p>Generalised Protection (In the Future)</p><ul><li><p>Warn you if you are sending tokens to a known honey pot address</p></li><li><p>If Gas paid is extremely high double-check with the user this is their intention</p></li></ul></li></ul><p>So with all these great features why aren&#8217;t all users using them?</p><p>Primarily it&#8217;s down to knowledge &amp; the friction in getting it set up.</p><p>If we look at knowledge a lot of users don&#8217;t know this exists or that there are solutions to some of their problems. They don&#8217;t know they&#8217;re being front-run or sandwiched. They don&#8217;t know that they could have prevented that expensive revert during an NFT mint.</p><p>To look at friction let&#8217;s see how we would set this up on Metamask as an example. </p><p>The user has to go into the Settings &#8594; Networks &#8594; Add Network and from there has to give the new &#8220;network&#8221; a name ie Ethereum(Custom RPC), add the RPC endpoint you want to use, with a chainID of 1 and currency of ETH. </p><p>An advanced DeFi user will understand how to do this and what they are doing. For casual users, this is a complicated process and they likely don&#8217;t want to play around with default settings.</p><p>One could argue this isn&#8217;t something the user should have to deal with, there&#8217;s too much knowledge required to make an informed decision.</p><p>So how do we resolve this? </p><p>One way would be for the wallet provider ie Metamask to change their global default setting to a private relay.   </p><h3>Metamask - A Sleeping Kingmaker</h3><p>Let&#8217;s think about how powerful this ability to change the global default endpoint is for a wallet provider like Metamask?  </p><p>Think about how much of the public mempool comes from a Metamask wallet. Some studies have said up to ~70% of the transactions being made on Ethereum are via Metamask. </p><p>How much would a builder be willing to pay for exclusive access to that order flow. Or how easily Metamask could dominate if they decided to create their own builder entity.</p><p>In other words, Metamask appears to be a &#8220;Sleeping Kingmaker&#8221; and one that could crown itself if it wanted to.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!qKFD!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe7d8e2b0-dd82-4478-b8d9-efa456116a99_394x334.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!qKFD!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe7d8e2b0-dd82-4478-b8d9-efa456116a99_394x334.png 424w, https://substackcdn.com/image/fetch/$s_!qKFD!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe7d8e2b0-dd82-4478-b8d9-efa456116a99_394x334.png 848w, https://substackcdn.com/image/fetch/$s_!qKFD!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe7d8e2b0-dd82-4478-b8d9-efa456116a99_394x334.png 1272w, https://substackcdn.com/image/fetch/$s_!qKFD!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe7d8e2b0-dd82-4478-b8d9-efa456116a99_394x334.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!qKFD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe7d8e2b0-dd82-4478-b8d9-efa456116a99_394x334.png" width="394" height="334" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/e7d8e2b0-dd82-4478-b8d9-efa456116a99_394x334.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:334,&quot;width&quot;:394,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:43950,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!qKFD!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe7d8e2b0-dd82-4478-b8d9-efa456116a99_394x334.png 424w, https://substackcdn.com/image/fetch/$s_!qKFD!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe7d8e2b0-dd82-4478-b8d9-efa456116a99_394x334.png 848w, https://substackcdn.com/image/fetch/$s_!qKFD!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe7d8e2b0-dd82-4478-b8d9-efa456116a99_394x334.png 1272w, https://substackcdn.com/image/fetch/$s_!qKFD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe7d8e2b0-dd82-4478-b8d9-efa456116a99_394x334.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Metamask The Sleeping Kingmaker</figcaption></figure></div><p>For Metamask it&#8217;s a difficult path to navigate. </p><p>They have access to this huge amount of value via their order flow but they want to make sure they don&#8217;t damage their users&#8217; experience and that the community is on board with any changes that are being made. </p><p>If Metamask used a default private RPC and the builder on the other end wasn&#8217;t winning builds consistently users may be waiting many blocks for their transactions to confirm.</p><p>Since their transactions aren&#8217;t in the mempool they can&#8217;t get on-chain until Metamask&#8217;s builder wins a build.</p><p>To limit this wait time Metamask could easily embed some default logic. An example would be stating that transactions are sent to the public mempool if they haven&#8217;t been included within 3 blocks.</p><h4>Monetising Order Flow</h4><p>Wallet providers are thinking about monetising their order flows. </p><p>Monetising their order flows enables them to capture MEV value and give it back to their users. </p><p>They can do this via gas discounts, token emissions that represent a share of the value captured etc.</p><p>If the dominant wallets decide not to monetise order flows do we could see a great migration from those that ignore order flows to wallets that acknowledge and exploit it.</p><blockquote><p><em>A payment for order flow solution to me exists at the application and wallet level, the best applications and wallets will be the ones that are able to return the maximum amount of value to their users</em></p><p><em>Stephane Gosselin - Flashbots - <a href="https://youtu.be/AhnaiZNv3O4?t=3570">Unchained Podcast</a>  </em></p></blockquote><p>Does it force the hand of the dominant wallet providers to enter the order flow game? </p><p>Is this too much value to ignore? <br><br>Maybe let&#8217;s take a look at how these order flows could be sold.</p><h4>Exclusivity Agreements</h4><p>The simplest way for a wallet provider (Metamask) to sell order flow would be via an agreement to provide exclusive order flow for one or more builders.</p><p>The agreement would have terms of service.</p><ul><li><p>No Malicious MEV ie front-running, sandwiching </p></li><li><p>Tx will be emitted to the mempool after 3 blocks</p></li><li><p>6 month contract</p></li><li><p>etc.</p></li></ul><p>I would argue that this approach would be very centralising for the builder market. If only one builder got access to Metamasks order flows that might be game over for everyone else.</p><h4>Order Flow Auctions</h4><p>Another possibility is the concept of individual tx auctions. </p><p>The wallet provide could provide an auction service to all builders where they exposed a stream of unsigned transactions to be bid on.</p><p>The auctions would be time bound to ensure inclusion and only builders who were registered with the validator that was up next to propose the block would be able to bid. </p><p>If no bids were received for a tx it could be emitted to the public mempool. Transactions known to have no &#8220;MEV value such as simple ETH value transfers could also be set up to default to them mempool.</p><p>When a builder successfully won a bid they would pay the fee and be sent the signed transaction. </p><p>Similar to the &#8220;Exclusivity Agreements&#8221; there would be terms of service with the auction ie No front-running sandwiching, Tx will be emitted to the mempool after 3 blocks etc.</p><p>The Builders would need to write algorithms/models to automatically price different transactions and determine what to bid.</p><p>The wallet provider could take a small percentage from these auctions and return the remainder to the user for that specific transaction. </p><p>The more valuable a user&#8217;s transaction, the more the builder will bid and the higher the payment back to them.</p><p>This auction method fixes the issue of attributing MEV to different addresses when a batch of transactions is sold.</p><p>In &#8220;Exclusivity Agreements&#8221; how can you determine which transactions generated the MEV and therefore which users should be compensated. Here we have a clear 1-to-1 mapping of Tx (user) to the price paid.</p><p>Whoever the auctioneer is in this model has a significant amount of power since they hold all the signed transactions. </p><p>While it would likely start as a centralised entity efforts would need to be made to decentralise/trust minimise that component.</p><h4>Psyops</h4><p>One other thing we could see with order flow auctions is builders making predictions based on the available unsigned transactions in the same way bots look at exchange order books.</p><p>Could entities spoof transactions in the auction to gain some advantage? It would be up to the wallet providers / auctioneer to prevent this from happening.</p><h3>A Centralising Force</h3><p>Let&#8217;s look at what happens when 1 builder has exclusive access to the majority of transactions on Ethereum.</p><p>Let&#8217;s say 20 builders have access to 300 Txs in the mempool. There is a single builder who has exclusive access to Metamask&#8217;s 700 Txs in addition to the 300 mempool Txs giving them 1000 Txs to build with.</p><p>The 1000 Tx block builder gets to build the majority of blocks. They have a larger search space from which to build a block and exclusive access to some of the value.</p><p>Slowly the block builders with mempool only access go out of business. </p><p>They&#8217;re not winning enough blocks. Any exclusive order flow agreements they may have had aren&#8217;t getting renewed or are being terminated.</p><p>How often a builder wins a block has a direct impact on their user&#8217;s experience, eventually, they are unable to compete, unable to operate at a profit. </p><blockquote><p><em>Exclusive order flow is a very big risk and very big danger in the MEV space over the next couple years.</em></p><p><em>Hasu - Flashbots - <a href="https://youtu.be/yJQKjDcusYQ?t=1619">Wintermute MEV Hackathon</a></em></p></blockquote><p>A world where block builders compete on exclusive order flow is a world that gravitates strongly to a winner takes all effect occurring in block building.</p><h4>MEV Dystopia</h4><p>The image below is from <a href="https://www.youtube.com/watch?v=AuMkr5AnUvI">Stephane&#8217;s MEV Utopia and Dystopia talk</a> and highlights a decentralised MEV supply chain. The red block I have added represents the vertical integration that could occur via exclusive order flow. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!0HeV!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8431a906-e09f-4ed4-a745-3c0ec2a34134_1588x702.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!0HeV!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8431a906-e09f-4ed4-a745-3c0ec2a34134_1588x702.png 424w, https://substackcdn.com/image/fetch/$s_!0HeV!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8431a906-e09f-4ed4-a745-3c0ec2a34134_1588x702.png 848w, https://substackcdn.com/image/fetch/$s_!0HeV!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8431a906-e09f-4ed4-a745-3c0ec2a34134_1588x702.png 1272w, https://substackcdn.com/image/fetch/$s_!0HeV!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8431a906-e09f-4ed4-a745-3c0ec2a34134_1588x702.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!0HeV!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8431a906-e09f-4ed4-a745-3c0ec2a34134_1588x702.png" width="1456" height="644" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/8431a906-e09f-4ed4-a745-3c0ec2a34134_1588x702.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:644,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:236902,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!0HeV!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8431a906-e09f-4ed4-a745-3c0ec2a34134_1588x702.png 424w, https://substackcdn.com/image/fetch/$s_!0HeV!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8431a906-e09f-4ed4-a745-3c0ec2a34134_1588x702.png 848w, https://substackcdn.com/image/fetch/$s_!0HeV!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8431a906-e09f-4ed4-a745-3c0ec2a34134_1588x702.png 1272w, https://substackcdn.com/image/fetch/$s_!0HeV!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8431a906-e09f-4ed4-a745-3c0ec2a34134_1588x702.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><a href="https://www.youtube.com/watch?v=AuMkr5AnUvI">MEV Utopia and Dystopia</a></figcaption></figure></div><p>Let's run a simulation where Metamask creates their own builder and see what could happen.</p><p>Metamask own 70% of the public order flow, they make this exclusive order flow to themselves and are able to quickly dominate the block building space. </p><p>Since they are dominating the block builds searchers have no choice but to send their bundles to the Metamask builder. If they don&#8217;t they&#8217;re only able to access MEV on the blocks their current builder wins which due to Metamasks dominance is a low percentage.</p><p>Metamask begins to build an internal searcher team who learns from the searcher order flow they have access to.</p><p>The internal searcher team gets to extract value from 70% of the Ethereum transactions with no competition while the external searchers are left to fight for the value in the remaining 30% in the mempool.</p><p>By dominating the builder space Metamask also gets to keep more of the MEV for itself rather than passing it on to the validator. </p><p>In a competitive market, margins are cut and the majority of value ends up in the validators hands. </p><p>In a monopoly, Metamask just needs to ensure their block build pays slightly more than the next best builder.</p><p>Very quickly Metamask has come to dominate the wallet, searcher &amp; builder market.</p><p>How the community reacts would be interesting. </p><p>Likely Metamask would have to self-regulate in the same way mining pools and validator pools have had to.     </p><p>The diagram below from <a href="https://twitter.com/jon_charb">Jon Charbonneau</a> shows the feedback loop discussed above that can quickly lead to centralisation.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!NUaK!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fda6ca926-84ab-4c8c-8789-154f55e78c4e_1242x685.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!NUaK!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fda6ca926-84ab-4c8c-8789-154f55e78c4e_1242x685.jpeg 424w, https://substackcdn.com/image/fetch/$s_!NUaK!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fda6ca926-84ab-4c8c-8789-154f55e78c4e_1242x685.jpeg 848w, https://substackcdn.com/image/fetch/$s_!NUaK!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fda6ca926-84ab-4c8c-8789-154f55e78c4e_1242x685.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!NUaK!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fda6ca926-84ab-4c8c-8789-154f55e78c4e_1242x685.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!NUaK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fda6ca926-84ab-4c8c-8789-154f55e78c4e_1242x685.jpeg" width="1242" height="685" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/da6ca926-84ab-4c8c-8789-154f55e78c4e_1242x685.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:685,&quot;width&quot;:1242,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;Image&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Image" title="Image" srcset="https://substackcdn.com/image/fetch/$s_!NUaK!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fda6ca926-84ab-4c8c-8789-154f55e78c4e_1242x685.jpeg 424w, https://substackcdn.com/image/fetch/$s_!NUaK!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fda6ca926-84ab-4c8c-8789-154f55e78c4e_1242x685.jpeg 848w, https://substackcdn.com/image/fetch/$s_!NUaK!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fda6ca926-84ab-4c8c-8789-154f55e78c4e_1242x685.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!NUaK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fda6ca926-84ab-4c8c-8789-154f55e78c4e_1242x685.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><a href="https://twitter.com/jon_charb/status/1562916372505665536">Private orderflow &amp; the block builder market</a></figcaption></figure></div><h3>App Level Optimisations, Cross-Chain &amp; CEX </h3><p>There are lots of factors I haven&#8217;t touched on that will impact how this future plays out. Let&#8217;s briefly touch on each one and provide a high-level overview.</p><h4>Application Optimisations</h4><p>Decentralised applications such as Uniswap, Sushiswap, etc. have a huge incentive to reduce the MEV exposed by their protocol. </p><p>Ideally, they can capture the value for themselves and their users at the Tx origin. </p><p>A great example of this is the collaboration between Sushiswap &amp; Manifold Finance (Manifold would like to be one of the main block builders post-merge).</p><p>Sushi is going to replace their &#8220;transaction router&#8221; with Manifolds &#8220;MEV router&#8221; at the protocol level. </p><p>The &#8220;MEV router&#8221; looks to capture arbitrage opportunities as the user makes the trade and return the value back to the protocol. You can read more about it <a href="https://snapshot.org/#/sushigov.eth/proposal/bafkreigjjhjx2j2b526d3poeg23w2xan4rcgnzvcxinsssv7n37lu5p7s4">here</a>.</p><p>These application/builder relationships will be another area in which builders will compete.</p><h4>Cross-Chain MEV</h4><p>Cross-chain MEV is already occurring however we can expect it to get more competitive in the future.</p><p>Cross-chain MEV is non-atomic, has higher execution risk, involves inventory management and requires deep knowledge across multiple chains. </p><p>It, therefore, has a much higher barrier to entry.</p><p>Everything we have talked about is roughly mirrored on other chains. Image gaining access to Solana orderflow, Avalanche orderflow, etc. </p><p>The order flows on other chains will influence what can be done on Ethereum.</p><p>Increasing the search space of valuable transactions is always better for finding the highest value combination.</p><p>How big an impact this &#8220;other chain&#8221; order flow has on the builder network is yet to be seen but in an ultra-competitive environment it would provide an edge.</p><h4>Centralised Exchanges</h4><p>Like cross-chain MEV we are already seeing the on-chain/off-chain MEV opportunities be exploited.</p><p>With centralised exchanges, builders will have to minimise their latency through co-location and negotiate deals to get the lowest trading fee.</p><p>If one builder has negotiated a trading fee of 0.02% and another has a trading fee of 0.08% how does this impact the on-chain/off-chain opportunities available to them?</p><p>This will be another area in which builders can compete and try to find their edge. </p><h3>An Unknown Future</h3><p>Who knows how this future will play out, this is an active area of research that has lots of moving parts.</p><p>Stephane&#8217;s quote below captures the reality of the builder market but also the mission of the community to keep it as decentralised / flat as possible.</p><blockquote><p><em>Ultimately it&#8217;s likely the builder marketplace is going to have some power law distribution where the top builders aggregate the majority of the proposal power but we want to keep this distribution as flat as possible.</em></p><p><em>Stephane Gosselin - Flashbots - <a href="https://youtu.be/xex9WVmVfbg?t=5158">Bankless</a></em></p></blockquote><p>And who knows maybe there is light at the end of the tunnel.</p><div class="twitter-embed" data-attrs="{&quot;url&quot;:&quot;https://twitter.com/thegostep/status/1564744185852444673?s=20&amp;t=746OLih7jF5ycja4xcb0sA&quot;,&quot;full_text&quot;:&quot;Exclusive orderflow has a solution&quot;,&quot;username&quot;:&quot;thegostep&quot;,&quot;name&quot;:&quot;Stephane &#9889;&#129302;&quot;,&quot;profile_image_url&quot;:&quot;&quot;,&quot;date&quot;:&quot;Tue Aug 30 22:37:40 +0000 2022&quot;,&quot;photos&quot;:[],&quot;quoted_tweet&quot;:{},&quot;reply_count&quot;:0,&quot;retweet_count&quot;:0,&quot;like_count&quot;:35,&quot;impression_count&quot;:0,&quot;expanded_url&quot;:{},&quot;video_url&quot;:null,&quot;belowTheFold&quot;:true}" data-component-name="Twitter2ToDOM"></div><p>Til next time</p><p>noxx</p><p>Follow me on Twitter <a href="https://twitter.com/noxx3xxon">@noxx3xxon</a></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://noxx.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading noxx! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[DEX Arbitrage, Mathematical Optimisations & Me]]></title><description><![CDATA[How to efficiently maximise arbitrage profit over a set of exchanges via Convex Optimisation]]></description><link>https://noxx.substack.com/p/dex-arbitrage-mathematical-optimisations</link><guid isPermaLink="false">https://noxx.substack.com/p/dex-arbitrage-mathematical-optimisations</guid><dc:creator><![CDATA[noxx]]></dc:creator><pubDate>Mon, 22 Aug 2022 14:56:37 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/h_600,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5ec33ce-be24-4467-8329-fe2a3f0536e4_2940x1120.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>If you&#8217;re a scholar of MEV, understanding how to maximise arbitrage profit is something you should be interested in.</p><p>Lucky for you that&#8217;s what we&#8217;ll be diving into today.</p><p>By far the largest form of MEV on-chain is arbitrage. The concept is simple, find price differences between exchanges on given tokens, execute a trade, pocket some profit and in the process bring the token prices on the exchanges back in line with each other.</p><p>While the concept is simple, finding the highest yielding arbitrage across a large set of exchanges and tokens can be difficult.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!pHye!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8e589dbf-1f9e-40e1-a249-52336eac4918_2090x760.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!pHye!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8e589dbf-1f9e-40e1-a249-52336eac4918_2090x760.png 424w, https://substackcdn.com/image/fetch/$s_!pHye!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8e589dbf-1f9e-40e1-a249-52336eac4918_2090x760.png 848w, https://substackcdn.com/image/fetch/$s_!pHye!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8e589dbf-1f9e-40e1-a249-52336eac4918_2090x760.png 1272w, https://substackcdn.com/image/fetch/$s_!pHye!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8e589dbf-1f9e-40e1-a249-52336eac4918_2090x760.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!pHye!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8e589dbf-1f9e-40e1-a249-52336eac4918_2090x760.png" width="1456" height="529" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/8e589dbf-1f9e-40e1-a249-52336eac4918_2090x760.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:529,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:367865,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!pHye!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8e589dbf-1f9e-40e1-a249-52336eac4918_2090x760.png 424w, https://substackcdn.com/image/fetch/$s_!pHye!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8e589dbf-1f9e-40e1-a249-52336eac4918_2090x760.png 848w, https://substackcdn.com/image/fetch/$s_!pHye!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8e589dbf-1f9e-40e1-a249-52336eac4918_2090x760.png 1272w, https://substackcdn.com/image/fetch/$s_!pHye!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8e589dbf-1f9e-40e1-a249-52336eac4918_2090x760.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><a href="https://explore.flashbots.net/">Flashbots MEV Explorer</a>: MEV by DEX and Technique</figcaption></figure></div><p>When finding an optimal arbitrage you&#8217;re racing against the block time of the chain and there are lots of moving parts. New transactions in the mempool, changing reference prices on centralised exchanges etc.</p><p>Lucky for us finding this optimal arbitrage has been found to be a &#8220;convex optimsation&#8221; problem for Constant Function Market Makers (CFMMs) like Uniswap, Sushiswap, Balancer etc.</p><p>What this means will be the subject of this article but to give you a taster it means that we can efficiently solve the arbitrage problem to global optimality. </p><p>Or in plain English, we have a formula that can efficiently find the set of trades that maximises arbitrage profit across a set of CFMMs.  </p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://noxx.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://noxx.substack.com/subscribe?"><span>Subscribe now</span></a></p><h2>Mathematical Optimisations</h2><p>Convex optimisations are a subfield of mathematical optimisations. </p><p>If we want to learn about convex optimisations a good place to start is gaining a high-level understanding of what a mathematical optimisation is. </p><blockquote><p>Mathematical optimisation is the selection of a best element, with regard to some criterion, from some set of available alternatives</p></blockquote><p>So what does this actually mean, let&#8217;s focus on the most common use case which is using mathematical optimisations when handling min/max problems. </p><p>Min/Max problems are problems that minimise or maximise an <strong>&#8220;objective function&#8221; </strong>given some <strong>&#8220;constraints&#8221;</strong> on the input values of that function. </p><p>Note that constraints can come in the form of inequality constraints (x^2 + y^2 &gt;= 1) or equality constraints (x^2 + y^2 = 1).</p><p>For example, given a budget buy as many pieces of fruit from the supermarket as possible where each fruit has a different price. Our constraints could be we can buy no more than 5 bananas (b &lt;= 5 inequality constraint) and that we must buy exactly one orange (o = 1 equality constraint).</p><p>Now let&#8217;s look at a more complex example and the notation that would be used to represent the problem. I will also provide an alternative way to view it which may be more intuitive for someone from a programming background.</p><h3>Constrained Optimisation Example</h3><p>You are asked to maximise the objective function f( x, y ) where, </p><pre><code>f( x, y ) = x^2 * y </code></pre><p>with the constraint</p><pre><code>x^2 + y^2 = 1</code></pre><p>Let&#8217;s start with the constraint, the constraint provides us with a set of viable x &amp; y values. </p><p>For example, when x = 1 &amp; y = 1, x^2 + y^2 = 2 which is not 1. Therefore it does not meet the constraint and is not a valid x, y position.</p><p>When x = 0.70710678118 &amp; y = 0.70710678118, x^2 + y^2 = 1 which is equal to 1. Therefore these x, y co-ordinates do meet the constraint and are valid. </p><p>This same objective function and constraints can be represented in the form of a function in python. Our objective is to maximise what the function returns.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!TR9P!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe56d99b7-aeb4-4deb-8a3a-a45d6bb89f98_1276x340.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!TR9P!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe56d99b7-aeb4-4deb-8a3a-a45d6bb89f98_1276x340.png 424w, https://substackcdn.com/image/fetch/$s_!TR9P!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe56d99b7-aeb4-4deb-8a3a-a45d6bb89f98_1276x340.png 848w, https://substackcdn.com/image/fetch/$s_!TR9P!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe56d99b7-aeb4-4deb-8a3a-a45d6bb89f98_1276x340.png 1272w, https://substackcdn.com/image/fetch/$s_!TR9P!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe56d99b7-aeb4-4deb-8a3a-a45d6bb89f98_1276x340.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!TR9P!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe56d99b7-aeb4-4deb-8a3a-a45d6bb89f98_1276x340.png" width="1276" height="340" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/e56d99b7-aeb4-4deb-8a3a-a45d6bb89f98_1276x340.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:340,&quot;width&quot;:1276,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:64714,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!TR9P!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe56d99b7-aeb4-4deb-8a3a-a45d6bb89f98_1276x340.png 424w, https://substackcdn.com/image/fetch/$s_!TR9P!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe56d99b7-aeb4-4deb-8a3a-a45d6bb89f98_1276x340.png 848w, https://substackcdn.com/image/fetch/$s_!TR9P!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe56d99b7-aeb4-4deb-8a3a-a45d6bb89f98_1276x340.png 1272w, https://substackcdn.com/image/fetch/$s_!TR9P!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe56d99b7-aeb4-4deb-8a3a-a45d6bb89f98_1276x340.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Objective function defined as python function</figcaption></figure></div><p>The function objective_function( x, y ) and is equivalent to f( x, y ). For the constraint, I have raised an exception if it isn&#8217;t met rather than using an indicator/penalty function.</p><p>This problem can be viewed graphically with 3 axes, one for x, one for y and one for the returned result f( x, y ).</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!KXua!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F690e8638-ea16-4bb0-827b-14c1a02e7d17_1430x1156.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!KXua!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F690e8638-ea16-4bb0-827b-14c1a02e7d17_1430x1156.png 424w, https://substackcdn.com/image/fetch/$s_!KXua!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F690e8638-ea16-4bb0-827b-14c1a02e7d17_1430x1156.png 848w, https://substackcdn.com/image/fetch/$s_!KXua!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F690e8638-ea16-4bb0-827b-14c1a02e7d17_1430x1156.png 1272w, https://substackcdn.com/image/fetch/$s_!KXua!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F690e8638-ea16-4bb0-827b-14c1a02e7d17_1430x1156.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!KXua!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F690e8638-ea16-4bb0-827b-14c1a02e7d17_1430x1156.png" width="1430" height="1156" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/690e8638-ea16-4bb0-827b-14c1a02e7d17_1430x1156.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1156,&quot;width&quot;:1430,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:483515,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!KXua!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F690e8638-ea16-4bb0-827b-14c1a02e7d17_1430x1156.png 424w, https://substackcdn.com/image/fetch/$s_!KXua!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F690e8638-ea16-4bb0-827b-14c1a02e7d17_1430x1156.png 848w, https://substackcdn.com/image/fetch/$s_!KXua!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F690e8638-ea16-4bb0-827b-14c1a02e7d17_1430x1156.png 1272w, https://substackcdn.com/image/fetch/$s_!KXua!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F690e8638-ea16-4bb0-827b-14c1a02e7d17_1430x1156.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><a href="https://www.youtube.com/watch?v=vwUV2IDLP8Q">Constrained Optimization Introduction</a></figcaption></figure></div><p>Above is a 3-dimensional graph that represents this problem. The red circle represents all the x, y coordinates that satisfy the constraint </p><pre><code>x^2 + y^2 = 1</code></pre><p>Another way to visualise this graph is to imagine drawing the x &amp; y axes on the floor in a large playground using some chalk. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!9SHS!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb842a3aa-b6a3-4451-84f1-dc4d111ce012_1402x1100.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!9SHS!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb842a3aa-b6a3-4451-84f1-dc4d111ce012_1402x1100.png 424w, https://substackcdn.com/image/fetch/$s_!9SHS!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb842a3aa-b6a3-4451-84f1-dc4d111ce012_1402x1100.png 848w, https://substackcdn.com/image/fetch/$s_!9SHS!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb842a3aa-b6a3-4451-84f1-dc4d111ce012_1402x1100.png 1272w, https://substackcdn.com/image/fetch/$s_!9SHS!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb842a3aa-b6a3-4451-84f1-dc4d111ce012_1402x1100.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!9SHS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb842a3aa-b6a3-4451-84f1-dc4d111ce012_1402x1100.png" width="1402" height="1100" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/b842a3aa-b6a3-4451-84f1-dc4d111ce012_1402x1100.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1100,&quot;width&quot;:1402,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:532852,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!9SHS!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb842a3aa-b6a3-4451-84f1-dc4d111ce012_1402x1100.png 424w, https://substackcdn.com/image/fetch/$s_!9SHS!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb842a3aa-b6a3-4451-84f1-dc4d111ce012_1402x1100.png 848w, https://substackcdn.com/image/fetch/$s_!9SHS!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb842a3aa-b6a3-4451-84f1-dc4d111ce012_1402x1100.png 1272w, https://substackcdn.com/image/fetch/$s_!9SHS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb842a3aa-b6a3-4451-84f1-dc4d111ce012_1402x1100.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><a href="https://www.youtube.com/watch?v=vwUV2IDLP8Q">Constrained Optimization Introduction</a></figcaption></figure></div><p>Given x, y coordinates, we can walk to that position in the playground and at that location a value, f( x, y ), will be returned to us.</p><p>The returned value is the objective function we&#8217;re trying to maximise. Its value will represent some height above or below the floor. Therefore the height above/below the ground represents our third axis f( x, y ).</p><p>If we had the ability to place a floating tennis ball above/below the ground based on the returned f( x, y ) we could plot out the 3-dimensional graph. </p><p>We would simply walk around the playground note the x &amp; y position and place our tennis ball some point above or below the ground. </p><p>If done at every coordinate this network of floating tennis balls would match the 3-dimensional graph we see in the images above.</p><p>The maximisation question can then be thought of as given a set of tennis balls placed above / below the ground at all playground coordinates (x, y) where x^2 + y^2 = 1 find  the tennis ball that is the highest above the ground.</p><h3>Framing an Optimisation Problem</h3><p>The purpose of this example is it shows us how an optimisation problem is framed. </p><p>We have some objective function which is the thing we want to minimise/maximise and a set of constraints. The importance of this will become apparent when we look at a real-world arbitrage.  </p><p>This is all the context we need for the rest of this article however if you are interested in learning more about this specific problem there are a set of videos that describe the solution. Feel free to come back at the end and run through them to help aid your understanding. </p><ul><li><p><a href="https://www.youtube.com/watch?v=vwUV2IDLP8Q">Constrained Optimization Part 1</a></p></li><li><p><a href="https://www.youtube.com/watch?v=yuqB-d5MjZA&amp;list=PLSQl0a2vh4HC5feHa6Rc5c0wbRTx56nF7&amp;index=93">Constrained Optimization Part 2</a></p></li><li><p><a href="https://www.youtube.com/watch?v=aep6lwPqm6I&amp;list=PLSQl0a2vh4HC5feHa6Rc5c0wbRTx56nF7&amp;index=94">Constrained Optimization Part 3</a></p></li></ul><p>Now let&#8217;s move on to the optimisation specific to CFMM arbitrage, convex optimisation. </p><h2>Convex Optimisation</h2><p>First, let&#8217;s start by looking at what makes our CFMM arbitrage optimisation problem convex. We&#8217;ll be dealing with a lot of what we have already encountered namely the objective function and constraints.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!wACi!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf2188b7-1571-4f8d-9ce6-636fdcf09ced_2040x1086.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!wACi!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf2188b7-1571-4f8d-9ce6-636fdcf09ced_2040x1086.png 424w, https://substackcdn.com/image/fetch/$s_!wACi!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf2188b7-1571-4f8d-9ce6-636fdcf09ced_2040x1086.png 848w, https://substackcdn.com/image/fetch/$s_!wACi!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf2188b7-1571-4f8d-9ce6-636fdcf09ced_2040x1086.png 1272w, https://substackcdn.com/image/fetch/$s_!wACi!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf2188b7-1571-4f8d-9ce6-636fdcf09ced_2040x1086.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!wACi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf2188b7-1571-4f8d-9ce6-636fdcf09ced_2040x1086.png" width="1456" height="775" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/df2188b7-1571-4f8d-9ce6-636fdcf09ced_2040x1086.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:775,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:354543,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!wACi!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf2188b7-1571-4f8d-9ce6-636fdcf09ced_2040x1086.png 424w, https://substackcdn.com/image/fetch/$s_!wACi!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf2188b7-1571-4f8d-9ce6-636fdcf09ced_2040x1086.png 848w, https://substackcdn.com/image/fetch/$s_!wACi!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf2188b7-1571-4f8d-9ce6-636fdcf09ced_2040x1086.png 1272w, https://substackcdn.com/image/fetch/$s_!wACi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf2188b7-1571-4f8d-9ce6-636fdcf09ced_2040x1086.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><a href="https://www.youtube.com/watch?v=d0CF3d5aEGc&amp;t=33s">Convexity &amp; The Principle of Duality</a></figcaption></figure></div><ol><li><p>The objective function must be convex.</p></li><li><p>The equality function can be linear, you may wonder why the equality function can be linear we&#8217;ll see why in [3].</p></li><li><p>The linear equality function can be converted into 2 inequality functions and given the equality function is linear we know that the associated inequality functions will be convex (see <a href="https://youtu.be/d0CF3d5aEGc?t=169">here</a> if you&#8217;d like to learn why).</p></li><li><p>The inequality functions must be convex.</p></li></ol><p>So we&#8217;ve determined we need the objective function and the inequality constraints to be convex but what determines if a function is convex.</p><h3>Convex Functions</h3><p>Let&#8217;s have look at the definition.</p><blockquote><p>In mathematics, a real-valued function is called convex if the line segment between any two points on the graph of the function lies above the graph between the two points </p></blockquote><p>The image below will help clarify what this means.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!eSIm!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F4b14c14e-3dce-43d1-a8b4-d25efcc49700_1670x606.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!eSIm!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F4b14c14e-3dce-43d1-a8b4-d25efcc49700_1670x606.png 424w, https://substackcdn.com/image/fetch/$s_!eSIm!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F4b14c14e-3dce-43d1-a8b4-d25efcc49700_1670x606.png 848w, https://substackcdn.com/image/fetch/$s_!eSIm!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F4b14c14e-3dce-43d1-a8b4-d25efcc49700_1670x606.png 1272w, https://substackcdn.com/image/fetch/$s_!eSIm!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F4b14c14e-3dce-43d1-a8b4-d25efcc49700_1670x606.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!eSIm!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F4b14c14e-3dce-43d1-a8b4-d25efcc49700_1670x606.png" width="1456" height="528" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/4b14c14e-3dce-43d1-a8b4-d25efcc49700_1670x606.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:528,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:214035,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!eSIm!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F4b14c14e-3dce-43d1-a8b4-d25efcc49700_1670x606.png 424w, https://substackcdn.com/image/fetch/$s_!eSIm!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F4b14c14e-3dce-43d1-a8b4-d25efcc49700_1670x606.png 848w, https://substackcdn.com/image/fetch/$s_!eSIm!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F4b14c14e-3dce-43d1-a8b4-d25efcc49700_1670x606.png 1272w, https://substackcdn.com/image/fetch/$s_!eSIm!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F4b14c14e-3dce-43d1-a8b4-d25efcc49700_1670x606.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><a href="https://www.youtube.com/watch?v=7QmGj1_i3MU">Convex vs Non-Convex Functions</a></figcaption></figure></div><p>We can see in the second and third graphs we are able to define a line segment between 2 points on the graph that goes below the graph indicating that they are non-convex. </p><p>If we take a look at Uniswaps (x * y = k) function on a graph we can see it is a convex function.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!j1Xj!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F087eccc5-ce32-4967-9b61-ce75536b8d16_1024x526.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!j1Xj!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F087eccc5-ce32-4967-9b61-ce75536b8d16_1024x526.png 424w, https://substackcdn.com/image/fetch/$s_!j1Xj!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F087eccc5-ce32-4967-9b61-ce75536b8d16_1024x526.png 848w, https://substackcdn.com/image/fetch/$s_!j1Xj!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F087eccc5-ce32-4967-9b61-ce75536b8d16_1024x526.png 1272w, https://substackcdn.com/image/fetch/$s_!j1Xj!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F087eccc5-ce32-4967-9b61-ce75536b8d16_1024x526.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!j1Xj!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F087eccc5-ce32-4967-9b61-ce75536b8d16_1024x526.png" width="1024" height="526" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/087eccc5-ce32-4967-9b61-ce75536b8d16_1024x526.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:526,&quot;width&quot;:1024,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;Uniswap v3 Explained &#8212; All You Need to Know | by Alexander Belobok | Medium&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Uniswap v3 Explained &#8212; All You Need to Know | by Alexander Belobok | Medium" title="Uniswap v3 Explained &#8212; All You Need to Know | by Alexander Belobok | Medium" srcset="https://substackcdn.com/image/fetch/$s_!j1Xj!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F087eccc5-ce32-4967-9b61-ce75536b8d16_1024x526.png 424w, https://substackcdn.com/image/fetch/$s_!j1Xj!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F087eccc5-ce32-4967-9b61-ce75536b8d16_1024x526.png 848w, https://substackcdn.com/image/fetch/$s_!j1Xj!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F087eccc5-ce32-4967-9b61-ce75536b8d16_1024x526.png 1272w, https://substackcdn.com/image/fetch/$s_!j1Xj!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F087eccc5-ce32-4967-9b61-ce75536b8d16_1024x526.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Uniswap (x*y=k) Graph</figcaption></figure></div><p>Again this is all the context we need, we have determined that our objective function and inequality constraints must be convex for a convex optimization problem.</p><p>There is lots more to learn about convex optimisation, the property of duality, the lagrangian, interior point method etc. however they&#8217;re not required to understand the rest of the article.</p><p>I would encourage you to dig into these topics after finishing. You can start with these 3 videos which give a great overview of some of these concepts.</p><ul><li><p><a href="https://www.youtube.com/watch?v=AM6BY4btj-M">Convex Optimisation Part 1</a></p></li><li><p><a href="https://www.youtube.com/watch?v=d0CF3d5aEGc">Convex Optimisation Part 2</a></p></li><li><p><a href="https://www.youtube.com/watch?v=uh1Dk68cfWs">Convex Optimisation Part 3</a></p></li></ul><p>Now that we have determined that our CFMM arbitrage problem is a convex problem we can move on to the implementation.</p><h2>CFMM Arbitrage Routing</h2><p>This section will focus on enhancing your understanding of how an arbitrage problem is framed as a convex optimisation problem and solving that problem via python.</p><p>We will draw heavily from 2 resources, the first a <a href="https://www.youtube.com/watch?v=v9liLt12jN8">talk from Theo Diamandis on convex optimisation</a> and the second <a href="https://github.com/angeris/cfmm-routing-code">a convex optimisation github repo from Guillermo Angeris</a>. Both should be reviewed at the end of the article to solidify your understanding.</p><p>All the code in the rest of the article can be found in <a href="https://gist.github.com/noxx3xxon/11fd224d7b99b78ee1e4a914bf0cbd22">this gist</a> which is a fork of Guillermo&#8217;s arbitrage.py with some additions.</p><p>One caveat to note we will be ignoring gas during these calculations to keep things simple. Note Theo does touch on how to include gas in <a href="https://www.youtube.com/watch?v=v9liLt12jN8">his talk</a> if you are interested. Now let&#8217;s begin. </p><h3>Framing The Problem</h3><p>The problem statement is given a set of CFMM (Constant Function Market Maker)  pools is there an arbitrage opportunity and if so which is optimal?</p><p>This problem is actually a subset of the CFMM routing problem.</p><p>We will see later that the addition of one extra constraint enables us to use this routing problem to solve our arbitrage optimisation.</p><p>So how do we formulate this routing problem into a convex optimisation problem?</p><p>Let&#8217;s run through it step by step taking you through the mathematical approach and its translation into code.</p><h3>Search Space</h3><p>We start by defining the search space. We can either define a set of tokens and find all the pools that contain these tokens or we can define a set of pools in which case the tokens are defined by the pools in our data set.</p><p>Our tokens will be labeled 1 to &#8220;n&#8221; and CFMM pools will be labeled 1 to &#8220;m&#8221;.</p><p>The connection between the tokens and the pools represents the CFMM network and can be viewed using a bipartite graph. </p><p>Let&#8217;s have a look at the bipartite graph along with how the tokens and CFMMs are defined in arbitrage.py (Note in the code we are using labeling 0 to n-1 &amp; 0 to m-1)</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!G_Rm!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5ec33ce-be24-4467-8329-fe2a3f0536e4_2940x1120.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!G_Rm!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5ec33ce-be24-4467-8329-fe2a3f0536e4_2940x1120.png 424w, https://substackcdn.com/image/fetch/$s_!G_Rm!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5ec33ce-be24-4467-8329-fe2a3f0536e4_2940x1120.png 848w, https://substackcdn.com/image/fetch/$s_!G_Rm!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5ec33ce-be24-4467-8329-fe2a3f0536e4_2940x1120.png 1272w, https://substackcdn.com/image/fetch/$s_!G_Rm!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5ec33ce-be24-4467-8329-fe2a3f0536e4_2940x1120.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!G_Rm!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5ec33ce-be24-4467-8329-fe2a3f0536e4_2940x1120.png" width="1200" height="457.4175824175824" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/e5ec33ce-be24-4467-8329-fe2a3f0536e4_2940x1120.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:555,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:230187,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!G_Rm!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5ec33ce-be24-4467-8329-fe2a3f0536e4_2940x1120.png 424w, https://substackcdn.com/image/fetch/$s_!G_Rm!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5ec33ce-be24-4467-8329-fe2a3f0536e4_2940x1120.png 848w, https://substackcdn.com/image/fetch/$s_!G_Rm!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5ec33ce-be24-4467-8329-fe2a3f0536e4_2940x1120.png 1272w, https://substackcdn.com/image/fetch/$s_!G_Rm!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5ec33ce-be24-4467-8329-fe2a3f0536e4_2940x1120.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Bipartite Graph + Defining Search Space in <a href="https://gist.github.com/noxx3xxon/11fd224d7b99b78ee1e4a914bf0cbd22">arbitrage.py</a></figcaption></figure></div><p>This looks complicated but I can assure you it isn&#8217;t.</p><ol><li><p>The CFMM pools are declared in code as an array called &#8220;local_indices&#8221;. Each CFMM pool is itself an array where the items in the array determine which tokens are available within that pool. &#8220;m&#8221; is equal to 5 since we have 5 CFMM pools.</p></li><li><p>The tokens are declared in code as a list [ 0, 1, 2, 3 ] called &#8220;global indices&#8221;. &#8220;n&#8221; is equal to 4 since we have 4 items in the list. The numbers within this list represent tokens ie</p><ol><li><p>0 = TOKEN-0 </p></li><li><p>1 = TOKEN-1</p></li><li><p> While I have labeled these TOKEN-X for simplicity you can imagine these mapping to real tokens such as ETH, DAI, SOL etc.</p></li></ol></li><li><p>The bipartite graph shows the CFMM pools on the left-hand side and the tokens on the right-hand side. A pool node is connected to a token node if that pool contains that token. This gives us a full representation of the network of CFMM pools we are working with. </p></li></ol><p>Let&#8217;s quickly run through each CFMM.</p><ul><li><p>CFMM 0  is a Balancer Pool  (Balancer pools can hold more than 2 tokens)</p><ul><li><p>[ 0, 1, 2, 3 ]</p></li><li><p>Contains TOKENS 0, 1, 2 &amp; 3</p></li></ul></li><li><p>CFMM 1 is a UniswapV2 Pool</p><ul><li><p>[ 0, 1 ]</p></li><li><p>Contains TOKENS 0 &amp; 1</p></li></ul></li><li><p>CFMM 2 is a UniswapV2 Pool</p><ul><li><p>[ 1, 2 ]</p></li><li><p>Contains TOKENS 1 &amp; 2</p></li></ul></li><li><p>CFMM 3 is a UniswapV2 Pool</p><ul><li><p>[ 2, 3 ]</p></li><li><p>Contains TOKENS 2 &amp; 3</p></li></ul></li><li><p>CFMM 4 is a Constant-Sum Pool</p><ul><li><p>[ 2, 3 ]</p></li><li><p>Contains TOKENS 2 &amp; 3</p></li></ul></li></ul><p>At this point, you may be asking why the tokens were defined as &#8220;global_indices&#8221; and the CFMM pools as &#8220;local_indices&#8221;? Let&#8217;s find out.</p><h3>Global vs Local Indices</h3><p>The naming above refers to the global index labeling of tokens which applies to all CFMM pools and the local index labeling of tokens which applies to a specific CFMM pool.</p><p>To give you an example above we have declared the following global indexes.</p><ul><li><p>0 &#8594; TOKEN-0</p></li><li><p>1 &#8594; TOKEN-1</p></li><li><p>2 &#8594; TOKEN-2</p></li><li><p>3 &#8594; TOKEN-3</p></li></ul><p>If we look at the local index for CFMM-2 we have.</p><ul><li><p>0 &#8594; TOKEN-1</p></li><li><p>1 &#8594; TOKEN-2</p></li></ul><p>This means the index mapping of a token can be viewed globally or locally. If we look at the global index 0 we can see it maps to TOKEN-0. If we look at the local index 0 of CFMM-2 we can see it maps to TOKEN-1, a different token to the global index mapping.  </p><p>The local indices can be defined by the notation below,</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!qQCf!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F35964f7d-5af1-4e4b-8c78-6f05c16b321a_450x45.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!qQCf!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F35964f7d-5af1-4e4b-8c78-6f05c16b321a_450x45.png 424w, https://substackcdn.com/image/fetch/$s_!qQCf!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F35964f7d-5af1-4e4b-8c78-6f05c16b321a_450x45.png 848w, https://substackcdn.com/image/fetch/$s_!qQCf!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F35964f7d-5af1-4e4b-8c78-6f05c16b321a_450x45.png 1272w, https://substackcdn.com/image/fetch/$s_!qQCf!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F35964f7d-5af1-4e4b-8c78-6f05c16b321a_450x45.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!qQCf!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F35964f7d-5af1-4e4b-8c78-6f05c16b321a_450x45.png" width="450" height="45" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/35964f7d-5af1-4e4b-8c78-6f05c16b321a_450x45.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:45,&quot;width&quot;:450,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:20600,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!qQCf!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F35964f7d-5af1-4e4b-8c78-6f05c16b321a_450x45.png 424w, https://substackcdn.com/image/fetch/$s_!qQCf!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F35964f7d-5af1-4e4b-8c78-6f05c16b321a_450x45.png 848w, https://substackcdn.com/image/fetch/$s_!qQCf!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F35964f7d-5af1-4e4b-8c78-6f05c16b321a_450x45.png 1272w, https://substackcdn.com/image/fetch/$s_!qQCf!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F35964f7d-5af1-4e4b-8c78-6f05c16b321a_450x45.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>Below is a table showing both the global index and local index of each CFMM.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!QLzb!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0eba5bd-eacb-4462-9013-f348083b06b5_2104x536.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!QLzb!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0eba5bd-eacb-4462-9013-f348083b06b5_2104x536.png 424w, https://substackcdn.com/image/fetch/$s_!QLzb!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0eba5bd-eacb-4462-9013-f348083b06b5_2104x536.png 848w, https://substackcdn.com/image/fetch/$s_!QLzb!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0eba5bd-eacb-4462-9013-f348083b06b5_2104x536.png 1272w, https://substackcdn.com/image/fetch/$s_!QLzb!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0eba5bd-eacb-4462-9013-f348083b06b5_2104x536.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!QLzb!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0eba5bd-eacb-4462-9013-f348083b06b5_2104x536.png" width="728" height="185.5" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/b0eba5bd-eacb-4462-9013-f348083b06b5_2104x536.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;normal&quot;,&quot;height&quot;:371,&quot;width&quot;:1456,&quot;resizeWidth&quot;:728,&quot;bytes&quot;:40650,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!QLzb!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0eba5bd-eacb-4462-9013-f348083b06b5_2104x536.png 424w, https://substackcdn.com/image/fetch/$s_!QLzb!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0eba5bd-eacb-4462-9013-f348083b06b5_2104x536.png 848w, https://substackcdn.com/image/fetch/$s_!QLzb!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0eba5bd-eacb-4462-9013-f348083b06b5_2104x536.png 1272w, https://substackcdn.com/image/fetch/$s_!QLzb!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0eba5bd-eacb-4462-9013-f348083b06b5_2104x536.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption">Global and Local Indexes</figcaption></figure></div><p>Since our global index differs from our local ones we need to have a way of translating local indices to global indices.</p><p>Ultimately we want to see the net gain/loss of each token across the entire network of CFMM Pools after all trades have been completed. This will be viewed in the global index.</p><p>To do this translation from local index to global index a set of matrices is used. </p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!8ja1!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9fa4733b-c0b2-4388-898f-0628f950e1cf_537x32.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!8ja1!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9fa4733b-c0b2-4388-898f-0628f950e1cf_537x32.png 424w, https://substackcdn.com/image/fetch/$s_!8ja1!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9fa4733b-c0b2-4388-898f-0628f950e1cf_537x32.png 848w, https://substackcdn.com/image/fetch/$s_!8ja1!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9fa4733b-c0b2-4388-898f-0628f950e1cf_537x32.png 1272w, https://substackcdn.com/image/fetch/$s_!8ja1!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9fa4733b-c0b2-4388-898f-0628f950e1cf_537x32.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!8ja1!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9fa4733b-c0b2-4388-898f-0628f950e1cf_537x32.png" width="537" height="32" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/9fa4733b-c0b2-4388-898f-0628f950e1cf_537x32.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:32,&quot;width&quot;:537,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:25175,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!8ja1!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9fa4733b-c0b2-4388-898f-0628f950e1cf_537x32.png 424w, https://substackcdn.com/image/fetch/$s_!8ja1!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9fa4733b-c0b2-4388-898f-0628f950e1cf_537x32.png 848w, https://substackcdn.com/image/fetch/$s_!8ja1!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9fa4733b-c0b2-4388-898f-0628f950e1cf_537x32.png 1272w, https://substackcdn.com/image/fetch/$s_!8ja1!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9fa4733b-c0b2-4388-898f-0628f950e1cf_537x32.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>Below is the set of Matrices for our example.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!WFk0!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7eaa020-e7c3-4674-8334-11dfb0c4cb78_1802x1000.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!WFk0!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7eaa020-e7c3-4674-8334-11dfb0c4cb78_1802x1000.png 424w, https://substackcdn.com/image/fetch/$s_!WFk0!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7eaa020-e7c3-4674-8334-11dfb0c4cb78_1802x1000.png 848w, https://substackcdn.com/image/fetch/$s_!WFk0!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7eaa020-e7c3-4674-8334-11dfb0c4cb78_1802x1000.png 1272w, https://substackcdn.com/image/fetch/$s_!WFk0!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7eaa020-e7c3-4674-8334-11dfb0c4cb78_1802x1000.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!WFk0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7eaa020-e7c3-4674-8334-11dfb0c4cb78_1802x1000.png" width="1456" height="808" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/b7eaa020-e7c3-4674-8334-11dfb0c4cb78_1802x1000.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:808,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:71714,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!WFk0!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7eaa020-e7c3-4674-8334-11dfb0c4cb78_1802x1000.png 424w, https://substackcdn.com/image/fetch/$s_!WFk0!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7eaa020-e7c3-4674-8334-11dfb0c4cb78_1802x1000.png 848w, https://substackcdn.com/image/fetch/$s_!WFk0!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7eaa020-e7c3-4674-8334-11dfb0c4cb78_1802x1000.png 1272w, https://substackcdn.com/image/fetch/$s_!WFk0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7eaa020-e7c3-4674-8334-11dfb0c4cb78_1802x1000.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Ai Matrices</figcaption></figure></div><p>Note that A0 maps to CFMM-0, A1 to CFMM-1 etc. These matrices are generated using the following code.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!_kqP!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3ae95ba2-fc52-4205-8b4c-e6c073718a8c_1006x332.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!_kqP!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3ae95ba2-fc52-4205-8b4c-e6c073718a8c_1006x332.png 424w, https://substackcdn.com/image/fetch/$s_!_kqP!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3ae95ba2-fc52-4205-8b4c-e6c073718a8c_1006x332.png 848w, https://substackcdn.com/image/fetch/$s_!_kqP!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3ae95ba2-fc52-4205-8b4c-e6c073718a8c_1006x332.png 1272w, https://substackcdn.com/image/fetch/$s_!_kqP!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3ae95ba2-fc52-4205-8b4c-e6c073718a8c_1006x332.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!_kqP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3ae95ba2-fc52-4205-8b4c-e6c073718a8c_1006x332.png" width="1006" height="332" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/3ae95ba2-fc52-4205-8b4c-e6c073718a8c_1006x332.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:332,&quot;width&quot;:1006,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:40821,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!_kqP!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3ae95ba2-fc52-4205-8b4c-e6c073718a8c_1006x332.png 424w, https://substackcdn.com/image/fetch/$s_!_kqP!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3ae95ba2-fc52-4205-8b4c-e6c073718a8c_1006x332.png 848w, https://substackcdn.com/image/fetch/$s_!_kqP!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3ae95ba2-fc52-4205-8b4c-e6c073718a8c_1006x332.png 1272w, https://substackcdn.com/image/fetch/$s_!_kqP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3ae95ba2-fc52-4205-8b4c-e6c073718a8c_1006x332.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Ai Matrices defined in <a href="https://gist.github.com/noxx3xxon/11fd224d7b99b78ee1e4a914bf0cbd22">arbitrage.py</a></figcaption></figure></div><p>We&#8217;ll see these matrices in action later in the article, for now, you just need to know they exist and that their purpose is mapping local indexes to global ones.</p><p>Next, we need to look at how we define our trades across the CFMMs.</p><h3>Trades - Lambdas &amp; Deltas</h3><p>When making a trade/swap with a pool we have the amount we put into the pool, referred to as &#8220;Delta&#8221; and the amount we take out of the pool, known as &#8220;Lambda&#8221;. </p><p>Each token within the pool has its own &#8220;Delta&#8221; &amp; &#8220;Lambda&#8221; values.</p><p>For example, if we traded against the UniswapV2 pool of ETH/DAI and put in 1 ETH and received 2000 DAI we would have the following values for &#8220;Delta&#8221; &amp; &#8220;Lambda&#8221;.</p><ul><li><p>ETH</p><ul><li><p>Delta = 1</p></li><li><p>Lambda = 0</p></li></ul></li><li><p>DAI</p><ul><li><p>Delta = 0 </p></li><li><p>Lambda = 2000</p></li></ul></li></ul><h3>Trading Function</h3><p>We can define the trade using &#8220;Delta&#8221; &amp; &#8220;Lambda&#8221; and use those values to ensure the trade adheres to the trading function that governs the pool. </p><p>We define the trading function with the greek symbol phi &#966;.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!FUaa!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe264bea0-c619-4cca-92fd-403e0adf89f8_1214x586.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!FUaa!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe264bea0-c619-4cca-92fd-403e0adf89f8_1214x586.png 424w, https://substackcdn.com/image/fetch/$s_!FUaa!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe264bea0-c619-4cca-92fd-403e0adf89f8_1214x586.png 848w, https://substackcdn.com/image/fetch/$s_!FUaa!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe264bea0-c619-4cca-92fd-403e0adf89f8_1214x586.png 1272w, https://substackcdn.com/image/fetch/$s_!FUaa!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe264bea0-c619-4cca-92fd-403e0adf89f8_1214x586.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!FUaa!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe264bea0-c619-4cca-92fd-403e0adf89f8_1214x586.png" width="1214" height="586" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/e264bea0-c619-4cca-92fd-403e0adf89f8_1214x586.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:586,&quot;width&quot;:1214,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:90447,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!FUaa!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe264bea0-c619-4cca-92fd-403e0adf89f8_1214x586.png 424w, https://substackcdn.com/image/fetch/$s_!FUaa!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe264bea0-c619-4cca-92fd-403e0adf89f8_1214x586.png 848w, https://substackcdn.com/image/fetch/$s_!FUaa!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe264bea0-c619-4cca-92fd-403e0adf89f8_1214x586.png 1272w, https://substackcdn.com/image/fetch/$s_!FUaa!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe264bea0-c619-4cca-92fd-403e0adf89f8_1214x586.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Trading Function notation</figcaption></figure></div><ol><li><p>Making a trade of &#8220;Delta i&#8221; &amp; &#8220;Lambda i&#8221; against &#8220;CFMM i&#8221; </p></li><li><p>The trade results in converting the tendered tokens &#8220;Delta&#8221; into the received tokens &#8220;Lambda&#8221;</p></li><li><p> Trade is accepted if it meets the trading function constraint. The trading function phi takes in pool reserves, &#8220;Delta&#8221;, &#8220;Lambda&#8221; and &#8220;Gamma&#8221;. </p><ol><li><p>&#8220;Gamma&#8221; represents the 1 minus the trading fee of the CFMM. This ensures we take into consideration the trading fee (0.3% for UniswapV2) that will be sent to the protocol from our &#8220;Delta&#8221; when we add tokens to the reserves. </p></li><li><p>The trading function ensures that the trade results in a new reserve that is greater than or equal to the trading function result of the old reserves.</p></li></ol></li></ol><p>Let&#8217;s see how we handle this in the code.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!SATT!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3b1c03fd-92d8-4f1a-b2db-b53d6b50424f_2202x1442.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!SATT!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3b1c03fd-92d8-4f1a-b2db-b53d6b50424f_2202x1442.png 424w, https://substackcdn.com/image/fetch/$s_!SATT!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3b1c03fd-92d8-4f1a-b2db-b53d6b50424f_2202x1442.png 848w, https://substackcdn.com/image/fetch/$s_!SATT!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3b1c03fd-92d8-4f1a-b2db-b53d6b50424f_2202x1442.png 1272w, https://substackcdn.com/image/fetch/$s_!SATT!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3b1c03fd-92d8-4f1a-b2db-b53d6b50424f_2202x1442.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!SATT!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3b1c03fd-92d8-4f1a-b2db-b53d6b50424f_2202x1442.png" width="1200" height="785.4395604395604" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/3b1c03fd-92d8-4f1a-b2db-b53d6b50424f_2202x1442.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:953,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:199959,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!SATT!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3b1c03fd-92d8-4f1a-b2db-b53d6b50424f_2202x1442.png 424w, https://substackcdn.com/image/fetch/$s_!SATT!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3b1c03fd-92d8-4f1a-b2db-b53d6b50424f_2202x1442.png 848w, https://substackcdn.com/image/fetch/$s_!SATT!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3b1c03fd-92d8-4f1a-b2db-b53d6b50424f_2202x1442.png 1272w, https://substackcdn.com/image/fetch/$s_!SATT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3b1c03fd-92d8-4f1a-b2db-b53d6b50424f_2202x1442.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Defining trades and reserves in <a href="https://gist.github.com/noxx3xxon/11fd224d7b99b78ee1e4a914bf0cbd22">arbitrage.py</a></figcaption></figure></div><ol><li><p>The reserves are declared for each CFMM pool. In this example these have been hardcoded, in reality, you would be constantly updating these reserves as trades happen on-chain. The first item in the array [ 4, 4, 4, 4 ] represents the Balancer pool and means we have the following within the pool</p><ol><li><p>4 of TOKEN-0</p></li><li><p>4 of TOKEN-1</p></li><li><p>4 of TOKEN-2</p></li><li><p>4 of TOKEN-3</p></li></ol></li><li><p>The trading fees are declared for each CFMM pool. The trading fees are defined as 1 - the actual fee. For example, UniswapV2 is a 0.3% fee so our fee is 0.997.</p></li><li><p>&#8220;Deltas&#8221; &amp; &#8220;Lambdas&#8221; are declared and defined using cp which is the cvxpy library. This library is a convex optimisation solver. At this point in the code &#8220;Delta&#8221; &amp; &#8220;Lambda&#8221; are abstract, they have no real value until we ask cvxpy to solve the optimisation.</p></li><li><p>The new reserves are declared. We can see they are calculated using the equation we saw earlier.</p><ol><li><p>R = Reserves</p></li><li><p>gamma = &#8220;Gamma&#8221;</p></li><li><p>D = &#8220;Delta&#8221;</p></li><li><p>L = &#8220;Lambda&#8221;</p></li></ol></li></ol><p>Now we understand how each individual swap will be defined we can define the &#8220;Net Network Trade&#8221;.</p><h3>Net Network Trade</h3><p>The &#8220;Net Network Trade&#8221; is the +/- of each token in the network, at the end of all the trades, relative to our starting position.</p><p>This is where the &#8220;Ai&#8221; matrices we defined earlier will be used.</p><p>They allow us to take a trade against a CFMM pool which is made up of the tokens on that CFMM at their local indexes and combine it with our &#8220;Ai&#8221; matrix to update a global index array which will represent our &#8220;Net Network Trade&#8221;. </p><p>The &#8220;Net Network Trade&#8221; is represented by the greek letter psi &#936;. Its mathematical notation is seen below.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!-j2U!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa96ca830-2784-483f-bd99-e25e1b5450b0_364x136.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!-j2U!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa96ca830-2784-483f-bd99-e25e1b5450b0_364x136.png 424w, https://substackcdn.com/image/fetch/$s_!-j2U!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa96ca830-2784-483f-bd99-e25e1b5450b0_364x136.png 848w, https://substackcdn.com/image/fetch/$s_!-j2U!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa96ca830-2784-483f-bd99-e25e1b5450b0_364x136.png 1272w, https://substackcdn.com/image/fetch/$s_!-j2U!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa96ca830-2784-483f-bd99-e25e1b5450b0_364x136.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!-j2U!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa96ca830-2784-483f-bd99-e25e1b5450b0_364x136.png" width="364" height="136" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/a96ca830-2784-483f-bd99-e25e1b5450b0_364x136.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:136,&quot;width&quot;:364,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:20191,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!-j2U!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa96ca830-2784-483f-bd99-e25e1b5450b0_364x136.png 424w, https://substackcdn.com/image/fetch/$s_!-j2U!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa96ca830-2784-483f-bd99-e25e1b5450b0_364x136.png 848w, https://substackcdn.com/image/fetch/$s_!-j2U!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa96ca830-2784-483f-bd99-e25e1b5450b0_364x136.png 1272w, https://substackcdn.com/image/fetch/$s_!-j2U!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa96ca830-2784-483f-bd99-e25e1b5450b0_364x136.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption">Net Network Trade notation</figcaption></figure></div><p>The image above states that psi &#936; is the sum of all trades across all CFMMs 1 &#8594; m where a trade is defined by &#8220;Lambda i&#8221; - &#8220;Delta i&#8221; and &#8220;Ai&#8221; is used to convert that trade into its global mapping. </p><p>Again let&#8217;s dive into the code, focusing on CFMM-2 as an example.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!P9GV!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F511c4e6c-192e-453f-8821-a66dfa99ae6c_1178x956.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!P9GV!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F511c4e6c-192e-453f-8821-a66dfa99ae6c_1178x956.png 424w, https://substackcdn.com/image/fetch/$s_!P9GV!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F511c4e6c-192e-453f-8821-a66dfa99ae6c_1178x956.png 848w, https://substackcdn.com/image/fetch/$s_!P9GV!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F511c4e6c-192e-453f-8821-a66dfa99ae6c_1178x956.png 1272w, https://substackcdn.com/image/fetch/$s_!P9GV!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F511c4e6c-192e-453f-8821-a66dfa99ae6c_1178x956.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!P9GV!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F511c4e6c-192e-453f-8821-a66dfa99ae6c_1178x956.png" width="1178" height="956" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/511c4e6c-192e-453f-8821-a66dfa99ae6c_1178x956.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:956,&quot;width&quot;:1178,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:100516,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!P9GV!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F511c4e6c-192e-453f-8821-a66dfa99ae6c_1178x956.png 424w, https://substackcdn.com/image/fetch/$s_!P9GV!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F511c4e6c-192e-453f-8821-a66dfa99ae6c_1178x956.png 848w, https://substackcdn.com/image/fetch/$s_!P9GV!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F511c4e6c-192e-453f-8821-a66dfa99ae6c_1178x956.png 1272w, https://substackcdn.com/image/fetch/$s_!P9GV!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F511c4e6c-192e-453f-8821-a66dfa99ae6c_1178x956.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Dot product of matrices</figcaption></figure></div><ol><li><p>Psi is declared. Once again we&#8217;re using the cvxpy library. The @ symbol when used with matrices means the dot product of the 2 values. We have the A2 matrix which converts the local token indexes to global token indexes and (L-D) which is &#8220;Lambda&#8221; - &#8220;Delta&#8221; ie how much goes in during the swap and how much comes out. We sum the results for the CFMMs to give us our &#8220;Net Network Trade&#8221;.</p></li><li><p>Is a demonstration of the dot product method for those who aren&#8217;t familiar with matrix multiplication. </p></li><li><p>The result of the dot production calculation for CFMM-2 can be seen here. Each row represents the +/- of a token in the trade.  Note the numbers above come from the cvxpy solution to our convex optimsation problem. </p><ol><li><p>(0 * -0.224) + (0 * 0.931) = 0 = TOKEN-0</p></li><li><p>(1 * -0.224) + (0 * 0.931) = -0.224 = TOKEN-1</p></li><li><p>(0 * -0.224) + (1 * 0.931) = 0.931 = TOKEN-2</p></li><li><p>(0 * -0.224) + (0 * 0.931) = 0 = TOKEN-3</p></li></ol></li></ol><p>Above we have looked at CFMM-2 in isolation, now let&#8217;s look at the sum of all the CFMMs to get our &#8220;Net Network Trade&#8221; matrix. </p><p>Note, for this diagram we have rounded to 3 decimal places so the result will differ slightly from the python output in <a href="https://gist.github.com/noxx3xxon/11fd224d7b99b78ee1e4a914bf0cbd22">arbitrage.py</a>.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!4kue!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8261fe50-8920-4aa7-8030-af239e44a256_1588x652.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!4kue!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8261fe50-8920-4aa7-8030-af239e44a256_1588x652.png 424w, https://substackcdn.com/image/fetch/$s_!4kue!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8261fe50-8920-4aa7-8030-af239e44a256_1588x652.png 848w, https://substackcdn.com/image/fetch/$s_!4kue!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8261fe50-8920-4aa7-8030-af239e44a256_1588x652.png 1272w, https://substackcdn.com/image/fetch/$s_!4kue!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8261fe50-8920-4aa7-8030-af239e44a256_1588x652.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!4kue!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8261fe50-8920-4aa7-8030-af239e44a256_1588x652.png" width="1456" height="598" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/8261fe50-8920-4aa7-8030-af239e44a256_1588x652.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:598,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:61499,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!4kue!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8261fe50-8920-4aa7-8030-af239e44a256_1588x652.png 424w, https://substackcdn.com/image/fetch/$s_!4kue!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8261fe50-8920-4aa7-8030-af239e44a256_1588x652.png 848w, https://substackcdn.com/image/fetch/$s_!4kue!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8261fe50-8920-4aa7-8030-af239e44a256_1588x652.png 1272w, https://substackcdn.com/image/fetch/$s_!4kue!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8261fe50-8920-4aa7-8030-af239e44a256_1588x652.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Net Network Trade in <a href="https://gist.github.com/noxx3xxon/11fd224d7b99b78ee1e4a914bf0cbd22">arbitrage.py</a></figcaption></figure></div><p>Each matrix represents the trade that occurred on that specific CFMM with positive values being received tokens and negative values being tendered tokens. By combining all these trades together we get our &#8220;Net Network Trade&#8221;.</p><ul><li><p>CFMM-0 Executes a trade </p><ul><li><p>Tenders 4.234 of TOKEN-0 &amp; 0.131 of TOKEN-2</p></li><li><p>Receives 2.135 of TOKEN-1 &amp; 1.928 of TOKEN-3</p></li></ul></li><li><p>CFMM-1 Executes a trade</p><ul><li><p>Tenders 0.736 of TOKEN-1</p></li><li><p>Receives 4.234 of TOKEN-0</p></li></ul></li><li><p>CFMM-2 Executes a trade</p><ul><li><p>Tenders 0.224 of TOKEN-1</p></li><li><p>Receives 0.931 of TOKEN-2</p></li></ul></li><li><p>CFMM-3 Executes a trade</p><ul><li><p>Tenders 4.646 of TOKEN-2</p></li><li><p>Receives 5.189 of TOKEN-3</p></li></ul></li><li><p>CFMM-4 Executes a trade </p><ul><li><p>Tenders 3.867 of TOKEN-3</p></li><li><p>Receives 3.864 of TOKEN-2</p></li></ul></li><li><p>Net Network Trade</p><ul><li><p>Receives 1.175 TOKEN-1, 0.018 TOKEN-2 &amp; 3.25 TOKEN-3</p></li></ul></li></ul><p>Next, we need to think about what exactly it is we what to maximise. What is our objective function? </p><h3>Objective (Utility) Function</h3><p>The Objective Function sometimes known as the utility function is what we want to maximise.</p><p>So what do we want to maximise?</p><p>We want to maximise the total value we receive at the end of all the trades. To do this we can&#8217;t use the number of each individual token as token prices vary. We need to convert the tokens to a common unit of value, we need to normalise the data. </p><p>We can do this by getting the USD values of the tokens from external markets with deep liquidity.</p><p>This means our objective function is maximising the &#8220;Net Network Trade&#8221; in USD value. Let&#8217;s check out the code.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ocnT!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F104cf283-d2ed-4a1b-83d4-aaca09163533_1214x922.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ocnT!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F104cf283-d2ed-4a1b-83d4-aaca09163533_1214x922.png 424w, https://substackcdn.com/image/fetch/$s_!ocnT!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F104cf283-d2ed-4a1b-83d4-aaca09163533_1214x922.png 848w, https://substackcdn.com/image/fetch/$s_!ocnT!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F104cf283-d2ed-4a1b-83d4-aaca09163533_1214x922.png 1272w, https://substackcdn.com/image/fetch/$s_!ocnT!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F104cf283-d2ed-4a1b-83d4-aaca09163533_1214x922.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ocnT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F104cf283-d2ed-4a1b-83d4-aaca09163533_1214x922.png" width="1214" height="922" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/104cf283-d2ed-4a1b-83d4-aaca09163533_1214x922.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:922,&quot;width&quot;:1214,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:71806,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ocnT!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F104cf283-d2ed-4a1b-83d4-aaca09163533_1214x922.png 424w, https://substackcdn.com/image/fetch/$s_!ocnT!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F104cf283-d2ed-4a1b-83d4-aaca09163533_1214x922.png 848w, https://substackcdn.com/image/fetch/$s_!ocnT!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F104cf283-d2ed-4a1b-83d4-aaca09163533_1214x922.png 1272w, https://substackcdn.com/image/fetch/$s_!ocnT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F104cf283-d2ed-4a1b-83d4-aaca09163533_1214x922.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Objective function in <a href="https://gist.github.com/noxx3xxon/11fd224d7b99b78ee1e4a914bf0cbd22">arbitrage.py</a></figcaption></figure></div><ol><li><p>We define the market_value of each token. Here we use a hardcoded array  but in reality, we would be constantly querying an exchange endpoint to get the latest prices. </p><ol><li><p>$1.50 is the price for TOKEN-0</p></li><li><p>$10 is the price for TOKEN-1</p></li><li><p>etc.</p></li></ol></li><li><p>The objective function is declared as obj. It states we want to maximise &#8220;market_value @ psi&#8221;, as I mentioned previously @ in this context means matrix multiplication. </p></li><li><p>These 2 matrices represent the market value and psi in the real solution. The resulting matrix is the &#8220;Net Network Trade&#8221; in USD value, exactly what we are trying to maximise.</p></li></ol><p>As mentioned the values I&#8217;ve used here are from the actual solution, in reality prior to the convex optimisation being solved these are all abstract values.</p><p>I&#8217;ve included them as I feel seeing the real values rather than symbols can sometimes help with our understanding.</p><blockquote><p>Note sometimes we may want to maximise the value for specific tokens. If we were only interested in acquiring TOKEN-0 &amp; TOKEN-2 we could set the market value of TOKEN-1 &amp; TOKEN-3 to zero. This will make the optimisation maximise the return value only in tokens 0 &amp; 2 rather than across all 4 tokens. </p></blockquote><p>We now have our objective function and can finally move on to the last piece of the puzzle, the constraints of the system. </p><h3>Trading Constraints </h3><p>The convex optimisation problem we have been setting up throughout the article exists within a network of CFMM pools. </p><p>These pools have their own laws that govern them.</p><p>These laws come in the form of trading functions, which we touched on earlier. The most famous being Uniswaps &#8220;x * y = k&#8221;. </p><p>To dig into each CFMM&#8217;s trading function is outside the scope of this article but if you are interested have a read of <a href="https://medium.com/bollinger-investment-group/constant-function-market-makers-defis-zero-to-one-innovation-968f77022159">this article</a>.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!j8FC!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F82ad2368-2567-4676-9730-f21567d9664a_702x580.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!j8FC!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F82ad2368-2567-4676-9730-f21567d9664a_702x580.png 424w, https://substackcdn.com/image/fetch/$s_!j8FC!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F82ad2368-2567-4676-9730-f21567d9664a_702x580.png 848w, https://substackcdn.com/image/fetch/$s_!j8FC!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F82ad2368-2567-4676-9730-f21567d9664a_702x580.png 1272w, https://substackcdn.com/image/fetch/$s_!j8FC!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F82ad2368-2567-4676-9730-f21567d9664a_702x580.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!j8FC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F82ad2368-2567-4676-9730-f21567d9664a_702x580.png" width="702" height="580" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/82ad2368-2567-4676-9730-f21567d9664a_702x580.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:580,&quot;width&quot;:702,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:39547,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!j8FC!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F82ad2368-2567-4676-9730-f21567d9664a_702x580.png 424w, https://substackcdn.com/image/fetch/$s_!j8FC!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F82ad2368-2567-4676-9730-f21567d9664a_702x580.png 848w, https://substackcdn.com/image/fetch/$s_!j8FC!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F82ad2368-2567-4676-9730-f21567d9664a_702x580.png 1272w, https://substackcdn.com/image/fetch/$s_!j8FC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F82ad2368-2567-4676-9730-f21567d9664a_702x580.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Trading Functions</figcaption></figure></div><p>Above are the trading functions for the 3 DEXs we are trading in. Within these 3 DEXs we are trading in 5 CFMM pools. Each pool has an arrow pointing to the trading function it uses.</p><p>Let&#8217;s see these constraints defined in the code.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!WvuM!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F2ebec8ef-321c-490a-b5e0-a10d7fb85353_1484x698.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!WvuM!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F2ebec8ef-321c-490a-b5e0-a10d7fb85353_1484x698.png 424w, https://substackcdn.com/image/fetch/$s_!WvuM!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F2ebec8ef-321c-490a-b5e0-a10d7fb85353_1484x698.png 848w, https://substackcdn.com/image/fetch/$s_!WvuM!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F2ebec8ef-321c-490a-b5e0-a10d7fb85353_1484x698.png 1272w, https://substackcdn.com/image/fetch/$s_!WvuM!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F2ebec8ef-321c-490a-b5e0-a10d7fb85353_1484x698.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!WvuM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F2ebec8ef-321c-490a-b5e0-a10d7fb85353_1484x698.png" width="1456" height="685" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/2ebec8ef-321c-490a-b5e0-a10d7fb85353_1484x698.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:685,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:91403,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!WvuM!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F2ebec8ef-321c-490a-b5e0-a10d7fb85353_1484x698.png 424w, https://substackcdn.com/image/fetch/$s_!WvuM!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F2ebec8ef-321c-490a-b5e0-a10d7fb85353_1484x698.png 848w, https://substackcdn.com/image/fetch/$s_!WvuM!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F2ebec8ef-321c-490a-b5e0-a10d7fb85353_1484x698.png 1272w, https://substackcdn.com/image/fetch/$s_!WvuM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F2ebec8ef-321c-490a-b5e0-a10d7fb85353_1484x698.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Constraints in <a href="https://gist.github.com/noxx3xxon/11fd224d7b99b78ee1e4a914bf0cbd22">arbitrage.py</a></figcaption></figure></div><ol><li><p>The Balancer trading function is a <a href="https://arxiv.org/abs/2006.08806#:~:text=Geometric%20mean%20market%20makers%20(G3Ms,same%20(weighted)%20geometric%20mean.">geometric mean</a> function. It includes the weights of the tokens in the pool and is declared with the cp.geo_mean. The sum of the weights ( [ 4, 3, 2, 1 ] ) adds up to 1 representing all of the assets in the pool.</p><ol><li><p>TOKEN-0 = 40%</p></li><li><p>TOKEN-1 = 30%</p></li><li><p>TOKEN-2 = 20%</p></li><li><p>TOKEN-3 = 10%</p></li></ol></li><li><p>UniswapV2 is also a geometric mean market maker and so a cp.geo_mean can again be used to define the constraints for CFMM 1, 2 &amp; 3.</p></li><li><p>Constant sum uses a different trading function and as such we use cp.sum.</p></li><li><p>This is not a trading function constraint but instead the constraint that turns our routing problem into an arbitrage problem that we discussed earlier. </p><ol><li><p>By stating that at the end of all trades there should be no tokens that need to be tendered we turn the routing problem into an arbitrage problem. </p></li><li><p>This constraint checks that each token in psi (net network trade) is greater or equal to 0. This means at the end of all trades for every token  we have either received that token or at least not had to tender any of that token.</p></li></ol></li></ol><h3>Solving Convex Optimisation Problem</h3><p>Now that we have our objective function and our constraints we can solve the convex optimisation problem. </p><p>The objective function and constraints, which effectively frame the problem, are displayed mathematically in Theo&#8217;s talk using the following notation.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!VCOc!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F347cb8dd-6333-4c99-b9d3-a507d2972740_1502x374.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!VCOc!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F347cb8dd-6333-4c99-b9d3-a507d2972740_1502x374.png 424w, https://substackcdn.com/image/fetch/$s_!VCOc!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F347cb8dd-6333-4c99-b9d3-a507d2972740_1502x374.png 848w, https://substackcdn.com/image/fetch/$s_!VCOc!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F347cb8dd-6333-4c99-b9d3-a507d2972740_1502x374.png 1272w, https://substackcdn.com/image/fetch/$s_!VCOc!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F347cb8dd-6333-4c99-b9d3-a507d2972740_1502x374.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!VCOc!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F347cb8dd-6333-4c99-b9d3-a507d2972740_1502x374.png" width="1456" height="363" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/347cb8dd-6333-4c99-b9d3-a507d2972740_1502x374.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:363,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:126231,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!VCOc!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F347cb8dd-6333-4c99-b9d3-a507d2972740_1502x374.png 424w, https://substackcdn.com/image/fetch/$s_!VCOc!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F347cb8dd-6333-4c99-b9d3-a507d2972740_1502x374.png 848w, https://substackcdn.com/image/fetch/$s_!VCOc!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F347cb8dd-6333-4c99-b9d3-a507d2972740_1502x374.png 1272w, https://substackcdn.com/image/fetch/$s_!VCOc!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F347cb8dd-6333-4c99-b9d3-a507d2972740_1502x374.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption">Framing the arbitrage problem</figcaption></figure></div><ol><li><p>Our Objective Function, which represents the &#8220;Net Network Trade&#8221; in USD</p></li><li><p> Our trading constraints for each CFMM from 1 &#8594; m</p></li><li><p>Valid values for trade amounts across all CFMMs (Note not explicitly defined in the code)</p></li><li><p>Arbitrage constraint, (cT)&#936; is our objective function where (cT) is the market values of the tokens. The arbitrage constraint is an indicator/penalty function. When &#936; &gt;= 0 it will resolve to infinity otherwise it resolves to 0. Since anything where &#936; &gt;= 0 will result in negative infinity, it ensures if this condition isn&#8217;t met that permutation won&#8217;t be selected (see <a href="https://www.youtube.com/watch?v=v9liLt12jN8">Theo&#8217;s talk</a> for more details).</p></li></ol><p>The solution to the optimisation gives us the total value of the net network trade. By looking through the &#8220;Lambdas&#8221; &amp; &#8220;Deltas&#8221; of each CFMM pool we can also see what trades were made to get to this result.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!9qxH!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa12ef13a-d857-4455-8b36-d32d6646c23e_772x262.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!9qxH!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa12ef13a-d857-4455-8b36-d32d6646c23e_772x262.png 424w, https://substackcdn.com/image/fetch/$s_!9qxH!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa12ef13a-d857-4455-8b36-d32d6646c23e_772x262.png 848w, https://substackcdn.com/image/fetch/$s_!9qxH!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa12ef13a-d857-4455-8b36-d32d6646c23e_772x262.png 1272w, https://substackcdn.com/image/fetch/$s_!9qxH!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa12ef13a-d857-4455-8b36-d32d6646c23e_772x262.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!9qxH!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa12ef13a-d857-4455-8b36-d32d6646c23e_772x262.png" width="772" height="262" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/a12ef13a-d857-4455-8b36-d32d6646c23e_772x262.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:262,&quot;width&quot;:772,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:29374,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!9qxH!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa12ef13a-d857-4455-8b36-d32d6646c23e_772x262.png 424w, https://substackcdn.com/image/fetch/$s_!9qxH!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa12ef13a-d857-4455-8b36-d32d6646c23e_772x262.png 848w, https://substackcdn.com/image/fetch/$s_!9qxH!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa12ef13a-d857-4455-8b36-d32d6646c23e_772x262.png 1272w, https://substackcdn.com/image/fetch/$s_!9qxH!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa12ef13a-d857-4455-8b36-d32d6646c23e_772x262.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Solving the optimisation in <a href="https://gist.github.com/noxx3xxon/11fd224d7b99b78ee1e4a914bf0cbd22">arbitrage.py</a></figcaption></figure></div><p>What cvxpy (cp) is doing under the hood to solve this problem would be another entire article so we&#8217;re not going to dive into it here. If you are interested I recommend starting <a href="https://bcc-research.github.io/CFMMRouter.jl/dev/method/">here</a> which is the documentation for CFMMRouter.jl, an optimiser written for this problem in julia.</p><p>For the context of this article what&#8217;s important is that we have a tool that solves convex optimistation problems and we now know how to programmatically frame questions we may want to ask.  </p><p>After calling solve we will be given a list of trades that are known to be the trades that result in the optimal arbitrage. One issue we still have is that we don&#8217;t know in what order to execute them.</p><h3>Execution Ordering</h3><p>To kick-start an arbitrage some capital is required for the first trade (even if it is from a flashloan). As such our goal when ordering the trades should be to minimise the kick-start capital required.</p><p>Here we are going to minimise USD value across all the tokens to kick-start the trade. There may be situations where you have some of token x and so want to weight it towards that token.</p><p>The number of permutations for trade ordering is n! (n factorial) where n is the number of trades. </p><p>In our case we have 5 so we have 5 x 4 x 3 x 2 x 1 = 120 permutations. </p><p>To keep things simple we will brute force the trade orders to determine the best ordering.</p><p>The code is relatively simple and ultimately just loops through each permutation and keeps a tally of how much of each token was required taking into account any tokens that have been received in prior trades.</p><p>You can find the full <a href="https://gist.github.com/noxx3xxon/11fd224d7b99b78ee1e4a914bf0cbd22">arbitrage.py</a> code below and play around with it yourself. I&#8217;ve added a few print statements and comments around the code to aid in your understanding.</p><div class="github-gist" data-attrs="{&quot;innerHTML&quot;:&quot;<div id=\&quot;gist117948369\&quot; class=\&quot;gist\&quot;>\n    <div class=\&quot;gist-file\&quot; translate=\&quot;no\&quot;>\n      <div class=\&quot;gist-data\&quot;>\n        <div class=\&quot;js-gist-file-update-container js-task-list-container file-box\&quot;>\n  <div id=\&quot;file-arbitrage-py\&quot; class=\&quot;file my-2\&quot;>\n    \n    <div itemprop=\&quot;text\&quot; class=\&quot;Box-body p-0 blob-wrapper data type-python  \&quot;>\n\n        \n<div class=\&quot;js-check-bidi js-blob-code-container blob-code-content\&quot;>\n\n  <template class=\&quot;js-file-alert-template\&quot;>\n  <div data-view-component=\&quot;true\&quot; class=\&quot;flash flash-warn flash-full d-flex flex-items-center\&quot;>\n  <svg aria-hidden=\&quot;true\&quot; height=\&quot;16\&quot; viewBox=\&quot;0 0 16 16\&quot; version=\&quot;1.1\&quot; width=\&quot;16\&quot; data-view-component=\&quot;true\&quot; class=\&quot;octicon octicon-alert\&quot;>\n    <path fill-rule=\&quot;evenodd\&quot; d=\&quot;M8.22 1.754a.25.25 0 00-.44 0L1.698 13.132a.25.25 0 00.22.368h12.164a.25.25 0 00.22-.368L8.22 1.754zm-1.763-.707c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0114.082 15H1.918a1.75 1.75 0 01-1.543-2.575L6.457 1.047zM9 11a1 1 0 11-2 0 1 1 0 012 0zm-.25-5.25a.75.75 0 00-1.5 0v2.5a.75.75 0 001.5 0v-2.5z\&quot;></path>\n</svg>\n  \n    <span>\n      This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.\n      <a href=\&quot;https://github.co/hiddenchars\&quot; target=\&quot;_blank\&quot;>Learn more about bidirectional Unicode characters</a>\n    </span>\n\n\n  <div data-view-component=\&quot;true\&quot; class=\&quot;flash-action\&quot;>        <a href=\&quot;{{ revealButtonHref }}\&quot; data-view-component=\&quot;true\&quot; class=\&quot;btn-sm btn\&quot;>    Show hidden characters\n</a>\n</div>\n</div></template>\n<template class=\&quot;js-line-alert-template\&quot;>\n  <span aria-label=\&quot;This line has hidden Unicode characters\&quot; data-view-component=\&quot;true\&quot; class=\&quot;line-alert tooltipped tooltipped-e\&quot;>\n    <svg aria-hidden=\&quot;true\&quot; height=\&quot;16\&quot; viewBox=\&quot;0 0 16 16\&quot; version=\&quot;1.1\&quot; width=\&quot;16\&quot; data-view-component=\&quot;true\&quot; class=\&quot;octicon octicon-alert\&quot;>\n    <path fill-rule=\&quot;evenodd\&quot; d=\&quot;M8.22 1.754a.25.25 0 00-.44 0L1.698 13.132a.25.25 0 00.22.368h12.164a.25.25 0 00.22-.368L8.22 1.754zm-1.763-.707c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0114.082 15H1.918a1.75 1.75 0 01-1.543-2.575L6.457 1.047zM9 11a1 1 0 11-2 0 1 1 0 012 0zm-.25-5.25a.75.75 0 00-1.5 0v2.5a.75.75 0 001.5 0v-2.5z\&quot;></path>\n</svg>\n</span></template>\n\n  <table data-hpc class=\&quot;highlight tab-size js-file-line-container js-code-nav-container js-tagsearch-file\&quot; data-tab-size=\&quot;8\&quot; data-paste-markdown-skip data-tagsearch-lang=\&quot;Python\&quot; data-tagsearch-path=\&quot;arbitrage.py\&quot;>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L1\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;1\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC1\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-k>import</span> <span class=pl-s1>numpy</span> <span class=pl-k>as</span> <span class=pl-s1>np</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L2\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;2\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC2\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-k>import</span> <span class=pl-s1>cvxpy</span> <span class=pl-k>as</span> <span class=pl-s1>cp</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L3\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;3\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC3\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-k>import</span> <span class=pl-s1>itertools</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L4\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;4\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC4\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L5\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;5\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC5\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-c># Problem data</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L6\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;6\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC6\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-s1>global_indices</span> <span class=pl-c1>=</span> <span class=pl-en>list</span>(<span class=pl-en>range</span>(<span class=pl-c1>4</span>))</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L7\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;7\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC7\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L8\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;8\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC8\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-c># 0 = TOKEN-0</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L9\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;9\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC9\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-c># 1 = TOKEN-1</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L10\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;10\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC10\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-c># 2 = TOKEN-2</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L11\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;11\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC11\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-c># 3 = TOKEN-3</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L12\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;12\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC12\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L13\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;13\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC13\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-s1>local_indices</span> <span class=pl-c1>=</span> [</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L14\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;14\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC14\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    [<span class=pl-c1>0</span>, <span class=pl-c1>1</span>, <span class=pl-c1>2</span>, <span class=pl-c1>3</span>], <span class=pl-c># TOKEN-0/TOKEN-1/TOKEN-2/TOKEN-3</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L15\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;15\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC15\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    [<span class=pl-c1>0</span>, <span class=pl-c1>1</span>], <span class=pl-c># TOKEN-0/TOKEN-1</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L16\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;16\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC16\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    [<span class=pl-c1>1</span>, <span class=pl-c1>2</span>], <span class=pl-c># TOKEN-1/TOKEN-2</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L17\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;17\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC17\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    [<span class=pl-c1>2</span>, <span class=pl-c1>3</span>], <span class=pl-c># TOKEN-2/TOKEN-3</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L18\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;18\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC18\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    [<span class=pl-c1>2</span>, <span class=pl-c1>3</span>] <span class=pl-c># TOKEN-2/TOKEN-3</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L19\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;19\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC19\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>]</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L20\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;20\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC20\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L21\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;21\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC21\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-s1>reserves</span> <span class=pl-c1>=</span> <span class=pl-en>list</span>(<span class=pl-en>map</span>(<span class=pl-s1>np</span>.<span class=pl-s1>array</span>, [</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L22\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;22\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC22\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    [<span class=pl-c1>4</span>, <span class=pl-c1>4</span>, <span class=pl-c1>4</span>, <span class=pl-c1>4</span>], <span class=pl-c># balancer with 4 assets in pool TOKEN-0, TOKEN-1, TOKEN-2, TOKEN-3 (4 TOKEN-0, 4 TOKEN-1, 4 TOKEN-2 &amp;amp; 4 TOKEN-3 IN POOL)</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L23\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;23\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC23\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    [<span class=pl-c1>10</span>, <span class=pl-c1>1</span>], <span class=pl-c># uniswapV2 TOKEN-0/TOKEN-1 (10 TOKEN-0 &amp;amp; 1 TOKEN-1 IN POOL)</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L24\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;24\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC24\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    [<span class=pl-c1>1</span>, <span class=pl-c1>5</span>], <span class=pl-c># uniswapV2 TOKEN-1/TOKEN-2 (1 TOKEN-1 &amp;amp; 5 TOKEN-2 IN POOL)</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L25\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;25\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC25\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    [<span class=pl-c1>40</span>, <span class=pl-c1>50</span>], <span class=pl-c># uniswapV2 TOKEN-2/TOKEN-3  (40 TOKEN-2 &amp;amp; 50 TOKEN-3 IN POOL)</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L26\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;26\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC26\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    [<span class=pl-c1>10</span>, <span class=pl-c1>10</span>] <span class=pl-c># constant_sum TOKEN-2/TOKEN-3 (10 TOKEN-2 &amp;amp; 10 TOKEN-3 IN POOL)</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L27\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;27\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC27\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>]))</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L28\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;28\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC28\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L29\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;29\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC29\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-s1>fees</span> <span class=pl-c1>=</span> [</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L30\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;30\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC30\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-c1>.998</span>, <span class=pl-c># balancer fees</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L31\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;31\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC31\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-c1>.997</span>, <span class=pl-c># uniswapV2 fees</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L32\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;32\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC32\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-c1>.997</span>, <span class=pl-c># uniswapV2 fees</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L33\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;33\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC33\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-c1>.997</span>, <span class=pl-c># uniswapV2 fees</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L34\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;34\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC34\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-c1>.999</span> <span class=pl-c># constant_sum fees</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L35\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;35\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC35\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>]</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L36\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;36\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC36\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L37\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;37\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC37\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-c># &amp;quot;Market value&amp;quot; of tokens (say, in a centralized exchange)</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L38\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;38\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC38\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-s1>market_value</span> <span class=pl-c1>=</span> [</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L39\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;39\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC39\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-c1>1.5</span>, <span class=pl-c># TOKEN-0</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L40\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;40\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC40\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-c1>10</span>, <span class=pl-c># TOKEN-1</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L41\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;41\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC41\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-c1>2</span>, <span class=pl-c># TOKEN-2</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L42\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;42\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC42\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-c1>3</span> <span class=pl-c># TOKEN-3</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L43\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;43\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC43\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>] </td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L44\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;44\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC44\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L45\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;45\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC45\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-c># Build local-global matrices</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L46\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;46\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC46\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-s1>n</span> <span class=pl-c1>=</span> <span class=pl-en>len</span>(<span class=pl-s1>global_indices</span>)</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L47\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;47\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC47\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-s1>m</span> <span class=pl-c1>=</span> <span class=pl-en>len</span>(<span class=pl-s1>local_indices</span>)</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L48\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;48\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC48\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L49\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;49\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC49\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-v>A</span> <span class=pl-c1>=</span> []</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L50\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;50\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC50\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-k>for</span> <span class=pl-s1>l</span> <span class=pl-c1>in</span> <span class=pl-s1>local_indices</span>: <span class=pl-c># for each CFMM</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L51\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;51\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC51\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-s1>n_i</span> <span class=pl-c1>=</span> <span class=pl-en>len</span>(<span class=pl-s1>l</span>) <span class=pl-c># n_i = number of tokens avaiable for CFMM i</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L52\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;52\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC52\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-v>A_i</span> <span class=pl-c1>=</span> <span class=pl-s1>np</span>.<span class=pl-en>zeros</span>((<span class=pl-s1>n</span>, <span class=pl-s1>n_i</span>)) <span class=pl-c># Create matrix of 0&amp;#39;s</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L53\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;53\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC53\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-k>for</span> <span class=pl-s1>i</span>, <span class=pl-s1>idx</span> <span class=pl-c1>in</span> <span class=pl-en>enumerate</span>(<span class=pl-s1>l</span>):</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L54\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;54\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC54\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>        <span class=pl-v>A_i</span>[<span class=pl-s1>idx</span>, <span class=pl-s1>i</span>] <span class=pl-c1>=</span> <span class=pl-c1>1</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L55\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;55\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC55\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-v>A</span>.<span class=pl-en>append</span>(<span class=pl-v>A_i</span>)</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L56\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;56\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC56\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L57\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;57\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC57\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-c># Build variables</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L58\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;58\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC58\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L59\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;59\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC59\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-c># tender delta</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L60\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;60\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC60\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-s1>deltas</span> <span class=pl-c1>=</span> [<span class=pl-s1>cp</span>.<span class=pl-v>Variable</span>(<span class=pl-en>len</span>(<span class=pl-s1>l</span>), <span class=pl-s1>nonneg</span><span class=pl-c1>=</span><span class=pl-c1>True</span>) <span class=pl-k>for</span> <span class=pl-s1>l</span> <span class=pl-c1>in</span> <span class=pl-s1>local_indices</span>]</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L61\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;61\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC61\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L62\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;62\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC62\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-c># receive lambda</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L63\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;63\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC63\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-s1>lambdas</span> <span class=pl-c1>=</span> [<span class=pl-s1>cp</span>.<span class=pl-v>Variable</span>(<span class=pl-en>len</span>(<span class=pl-s1>l</span>), <span class=pl-s1>nonneg</span><span class=pl-c1>=</span><span class=pl-c1>True</span>) <span class=pl-k>for</span> <span class=pl-s1>l</span> <span class=pl-c1>in</span> <span class=pl-s1>local_indices</span>]</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L64\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;64\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC64\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L65\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;65\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC65\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-s1>psi</span> <span class=pl-c1>=</span> <span class=pl-s1>cp</span>.<span class=pl-en>sum</span>([<span class=pl-v>A_i</span> @ (<span class=pl-v>L</span> <span class=pl-c1>-</span> <span class=pl-v>D</span>) <span class=pl-k>for</span> <span class=pl-v>A_i</span>, <span class=pl-v>D</span>, <span class=pl-v>L</span> <span class=pl-c1>in</span> <span class=pl-en>zip</span>(<span class=pl-v>A</span>, <span class=pl-s1>deltas</span>, <span class=pl-s1>lambdas</span>)])</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L66\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;66\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC66\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L67\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;67\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC67\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-c># Objective is to maximize &amp;quot;total market value&amp;quot; of coins out</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L68\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;68\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC68\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-s1>obj</span> <span class=pl-c1>=</span> <span class=pl-s1>cp</span>.<span class=pl-v>Maximize</span>(<span class=pl-s1>market_value</span> @ <span class=pl-s1>psi</span>) <span class=pl-c># matrix multiplication</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L69\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;69\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC69\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L70\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;70\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC70\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-c># Reserves after trade</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L71\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;71\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC71\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-s1>new_reserves</span> <span class=pl-c1>=</span> [<span class=pl-v>R</span> <span class=pl-c1>+</span> <span class=pl-s1>gamma_i</span><span class=pl-c1>*</span><span class=pl-v>D</span> <span class=pl-c1>-</span> <span class=pl-v>L</span> <span class=pl-k>for</span> <span class=pl-v>R</span>, <span class=pl-s1>gamma_i</span>, <span class=pl-v>D</span>, <span class=pl-v>L</span> <span class=pl-c1>in</span> <span class=pl-en>zip</span>(<span class=pl-s1>reserves</span>, <span class=pl-s1>fees</span>, <span class=pl-s1>deltas</span>, <span class=pl-s1>lambdas</span>)]</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L72\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;72\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC72\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L73\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;73\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC73\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-c># Trading function constraints</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L74\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;74\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC74\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-s1>cons</span> <span class=pl-c1>=</span> [</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L75\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;75\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC75\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-c># Balancer pool with weights 4, 3, 2, 1</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L76\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;76\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC76\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-s1>cp</span>.<span class=pl-en>geo_mean</span>(<span class=pl-s1>new_reserves</span>[<span class=pl-c1>0</span>], <span class=pl-s1>p</span><span class=pl-c1>=</span><span class=pl-s1>np</span>.<span class=pl-en>array</span>([<span class=pl-c1>4</span>, <span class=pl-c1>3</span>, <span class=pl-c1>2</span>, <span class=pl-c1>1</span>])) <span class=pl-c1>&amp;gt;=</span> <span class=pl-s1>cp</span>.<span class=pl-en>geo_mean</span>(<span class=pl-s1>reserves</span>[<span class=pl-c1>0</span>]),</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L77\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;77\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC77\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L78\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;78\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC78\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-c># Uniswap v2 pools</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L79\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;79\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC79\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-s1>cp</span>.<span class=pl-en>geo_mean</span>(<span class=pl-s1>new_reserves</span>[<span class=pl-c1>1</span>]) <span class=pl-c1>&amp;gt;=</span> <span class=pl-s1>cp</span>.<span class=pl-en>geo_mean</span>(<span class=pl-s1>reserves</span>[<span class=pl-c1>1</span>]),</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L80\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;80\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC80\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-s1>cp</span>.<span class=pl-en>geo_mean</span>(<span class=pl-s1>new_reserves</span>[<span class=pl-c1>2</span>]) <span class=pl-c1>&amp;gt;=</span> <span class=pl-s1>cp</span>.<span class=pl-en>geo_mean</span>(<span class=pl-s1>reserves</span>[<span class=pl-c1>2</span>]),</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L81\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;81\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC81\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-s1>cp</span>.<span class=pl-en>geo_mean</span>(<span class=pl-s1>new_reserves</span>[<span class=pl-c1>3</span>]) <span class=pl-c1>&amp;gt;=</span> <span class=pl-s1>cp</span>.<span class=pl-en>geo_mean</span>(<span class=pl-s1>reserves</span>[<span class=pl-c1>3</span>]),</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L82\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;82\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC82\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L83\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;83\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC83\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-c># Constant sum pool</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L84\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;84\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC84\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-s1>cp</span>.<span class=pl-en>sum</span>(<span class=pl-s1>new_reserves</span>[<span class=pl-c1>4</span>]) <span class=pl-c1>&amp;gt;=</span> <span class=pl-s1>cp</span>.<span class=pl-en>sum</span>(<span class=pl-s1>reserves</span>[<span class=pl-c1>4</span>]),</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L85\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;85\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC85\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-s1>new_reserves</span>[<span class=pl-c1>4</span>] <span class=pl-c1>&amp;gt;=</span> <span class=pl-c1>0</span>,</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L86\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;86\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC86\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L87\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;87\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC87\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-c># Arbitrage constraint</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L88\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;88\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC88\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-s1>psi</span> <span class=pl-c1>&amp;gt;=</span> <span class=pl-c1>0</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L89\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;89\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC89\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>]</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L90\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;90\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC90\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L91\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;91\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC91\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-c># Set up and solve problem</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L92\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;92\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC92\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-s1>prob</span> <span class=pl-c1>=</span> <span class=pl-s1>cp</span>.<span class=pl-v>Problem</span>(<span class=pl-s1>obj</span>, <span class=pl-s1>cons</span>)</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L93\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;93\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC93\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-s1>prob</span>.<span class=pl-en>solve</span>()</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L94\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;94\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC94\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L95\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;95\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC95\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L96\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;96\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC96\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-c># Trade Execution Ordering</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L97\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;97\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC97\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L98\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;98\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC98\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-s1>current_tokens</span> <span class=pl-c1>=</span> [<span class=pl-c1>0</span>, <span class=pl-c1>0</span>, <span class=pl-c1>0</span>, <span class=pl-c1>0</span>]</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L99\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;99\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC99\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-s1>new_current_tokens</span> <span class=pl-c1>=</span> [<span class=pl-c1>0</span>, <span class=pl-c1>0</span>, <span class=pl-c1>0</span>, <span class=pl-c1>0</span>]</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L100\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;100\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC100\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-s1>tokens_required_arr</span> <span class=pl-c1>=</span> []</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L101\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;101\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC101\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-s1>tokens_required_value_arr</span> <span class=pl-c1>=</span> []</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L102\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;102\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC102\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L103\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;103\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC103\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-s1>pool_names</span> <span class=pl-c1>=</span> [<span class=pl-s>&amp;quot;BALANCER 0/1/2/3&amp;quot;</span>, <span class=pl-s>&amp;quot;UNIV2 0/1&amp;quot;</span>, <span class=pl-s>&amp;quot;UNIV2 1/2&amp;quot;</span>, <span class=pl-s>&amp;quot;UNIV2 2/3&amp;quot;</span>, <span class=pl-s>&amp;quot;CONSTANT SUM 2/3&amp;quot;</span>]</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L104\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;104\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC104\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L105\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;105\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC105\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-s1>permutations</span> <span class=pl-c1>=</span> <span class=pl-s1>itertools</span>.<span class=pl-en>permutations</span>(<span class=pl-en>list</span>(<span class=pl-en>range</span>(<span class=pl-en>len</span>(<span class=pl-s1>local_indices</span>))), <span class=pl-en>len</span>(<span class=pl-s1>local_indices</span>))</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L106\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;106\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC106\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-s1>permutations2</span> <span class=pl-c1>=</span> []</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L107\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;107\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC107\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-k>for</span> <span class=pl-s1>permutation</span> <span class=pl-c1>in</span> <span class=pl-s1>permutations</span>:</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L108\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;108\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC108\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-s1>permutations2</span>.<span class=pl-en>append</span>(<span class=pl-s1>permutation</span>)</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L109\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;109\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC109\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-s1>current_tokens</span> <span class=pl-c1>=</span> [<span class=pl-c1>0</span>, <span class=pl-c1>0</span>, <span class=pl-c1>0</span>, <span class=pl-c1>0</span>]</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L110\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;110\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC110\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-s1>new_current_tokens</span> <span class=pl-c1>=</span> [<span class=pl-c1>0</span>, <span class=pl-c1>0</span>, <span class=pl-c1>0</span>, <span class=pl-c1>0</span>]   </td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L111\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;111\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC111\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-s1>tokens_required</span> <span class=pl-c1>=</span> [<span class=pl-c1>0</span>, <span class=pl-c1>0</span>, <span class=pl-c1>0</span>, <span class=pl-c1>0</span>]</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L112\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;112\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC112\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-k>for</span> <span class=pl-s1>pool_id</span> <span class=pl-c1>in</span> <span class=pl-s1>permutation</span>:</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L113\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;113\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC113\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>        <span class=pl-s1>pool</span> <span class=pl-c1>=</span> <span class=pl-s1>local_indices</span>[<span class=pl-s1>pool_id</span>]</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L114\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;114\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC114\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>        <span class=pl-k>for</span> <span class=pl-s1>global_token_id</span> <span class=pl-c1>in</span> <span class=pl-s1>pool</span>:</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L115\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;115\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC115\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>            <span class=pl-s1>local_token_index</span> <span class=pl-c1>=</span> <span class=pl-s1>pool</span>.<span class=pl-en>index</span>(<span class=pl-s1>global_token_id</span>)</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L116\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;116\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC116\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>            <span class=pl-s1>new_current_tokens</span>[<span class=pl-s1>global_token_id</span>] <span class=pl-c1>=</span> <span class=pl-s1>current_tokens</span>[<span class=pl-s1>global_token_id</span>] <span class=pl-c1>+</span> (<span class=pl-s1>lambdas</span>[<span class=pl-s1>pool_id</span>].<span class=pl-s1>value</span>[<span class=pl-s1>local_token_index</span>] <span class=pl-c1>-</span> <span class=pl-s1>deltas</span>[<span class=pl-s1>pool_id</span>].<span class=pl-s1>value</span>[<span class=pl-s1>local_token_index</span>])</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L117\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;117\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC117\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L118\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;118\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC118\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>            <span class=pl-k>if</span> <span class=pl-s1>new_current_tokens</span>[<span class=pl-s1>global_token_id</span>] <span class=pl-c1>&amp;lt;</span> <span class=pl-c1>0</span> <span class=pl-c1>and</span> <span class=pl-s1>new_current_tokens</span>[<span class=pl-s1>global_token_id</span>] <span class=pl-c1>&amp;lt;</span> <span class=pl-s1>current_tokens</span>[<span class=pl-s1>global_token_id</span>]:</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L119\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;119\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC119\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>                <span class=pl-k>if</span> <span class=pl-s1>current_tokens</span>[<span class=pl-s1>global_token_id</span>] <span class=pl-c1>&amp;lt;</span> <span class=pl-c1>0</span>:</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L120\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;120\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC120\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>                    <span class=pl-s1>tokens_required</span>[<span class=pl-s1>global_token_id</span>] <span class=pl-c1>+=</span> (<span class=pl-s1>current_tokens</span>[<span class=pl-s1>global_token_id</span>] <span class=pl-c1>-</span> <span class=pl-s1>new_current_tokens</span>[<span class=pl-s1>global_token_id</span>])</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L121\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;121\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC121\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>                    <span class=pl-s1>new_current_tokens</span>[<span class=pl-s1>global_token_id</span>] <span class=pl-c1>=</span> <span class=pl-c1>0</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L122\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;122\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC122\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>                <span class=pl-k>else</span>:</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L123\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;123\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC123\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>                    <span class=pl-s1>tokens_required</span>[<span class=pl-s1>global_token_id</span>] <span class=pl-c1>+=</span> (<span class=pl-c1>-</span><span class=pl-s1>new_current_tokens</span>[<span class=pl-s1>global_token_id</span>])</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L124\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;124\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC124\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>                    <span class=pl-s1>new_current_tokens</span>[<span class=pl-s1>global_token_id</span>] <span class=pl-c1>=</span> <span class=pl-c1>0</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L125\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;125\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC125\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>            <span class=pl-s1>current_tokens</span>[<span class=pl-s1>global_token_id</span>] <span class=pl-c1>=</span> <span class=pl-s1>new_current_tokens</span>[<span class=pl-s1>global_token_id</span>]</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L126\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;126\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC126\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L127\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;127\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC127\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-s1>tokens_required_value</span> <span class=pl-c1>=</span> []</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L128\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;128\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC128\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-k>for</span> <span class=pl-s1>i1</span>, <span class=pl-s1>i2</span> <span class=pl-c1>in</span> <span class=pl-en>zip</span>(<span class=pl-s1>tokens_required</span>, <span class=pl-s1>market_value</span>):</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L129\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;129\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC129\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>        <span class=pl-s1>tokens_required_value</span>.<span class=pl-en>append</span>(<span class=pl-s1>i1</span><span class=pl-c1>*</span><span class=pl-s1>i2</span>)</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L130\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;130\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC130\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L131\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;131\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC131\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-s1>tokens_required_arr</span>.<span class=pl-en>append</span>(<span class=pl-s1>tokens_required</span>)</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L132\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;132\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC132\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-s1>tokens_required_value_arr</span>.<span class=pl-en>append</span>(<span class=pl-en>sum</span>(<span class=pl-s1>tokens_required_value</span>))</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L133\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;133\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC133\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L134\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;134\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC134\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-s1>min_value</span> <span class=pl-c1>=</span> <span class=pl-en>min</span>(<span class=pl-s1>tokens_required_value_arr</span>)</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L135\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;135\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC135\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-s1>min_value_index</span> <span class=pl-c1>=</span> <span class=pl-s1>tokens_required_value_arr</span>.<span class=pl-en>index</span>(<span class=pl-s1>min_value</span>)</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L136\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;136\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC136\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L137\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;137\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC137\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L138\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;138\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC138\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-en>print</span>(<span class=pl-s>&amp;quot;<span class=pl-cce>\\n</span>-------------------- ARBITRAGE TRADES + EXECUTION ORDER --------------------<span class=pl-cce>\\n</span>&amp;quot;</span>)</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L139\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;139\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC139\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-k>for</span> <span class=pl-s1>pool_id</span> <span class=pl-c1>in</span> <span class=pl-s1>permutations2</span>[<span class=pl-s1>min_value_index</span>]:</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L140\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;140\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC140\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-s1>pool</span> <span class=pl-c1>=</span> <span class=pl-s1>local_indices</span>[<span class=pl-s1>pool_id</span>]</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L141\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;141\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC141\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-en>print</span>(<span class=pl-s>f&amp;quot;<span class=pl-cce>\\n</span>TRADE POOL = <span class=pl-s1><span class=pl-kos>{</span><span class=pl-s1>pool_names</span>[<span class=pl-s1>pool_id</span>]<span class=pl-kos>}</span></span>&amp;quot;</span>)</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L142\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;142\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC142\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L143\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;143\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC143\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-k>for</span> <span class=pl-s1>global_token_id</span> <span class=pl-c1>in</span> <span class=pl-s1>pool</span>:</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L144\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;144\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC144\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>        <span class=pl-s1>local_token_index</span> <span class=pl-c1>=</span> <span class=pl-s1>pool</span>.<span class=pl-en>index</span>(<span class=pl-s1>global_token_id</span>)</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L145\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;145\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC145\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>        <span class=pl-k>if</span> (<span class=pl-s1>lambdas</span>[<span class=pl-s1>pool_id</span>].<span class=pl-s1>value</span>[<span class=pl-s1>local_token_index</span>] <span class=pl-c1>-</span> <span class=pl-s1>deltas</span>[<span class=pl-s1>pool_id</span>].<span class=pl-s1>value</span>[<span class=pl-s1>local_token_index</span>]) <span class=pl-c1>&amp;lt;</span> <span class=pl-c1>0</span>:  </td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L146\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;146\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC146\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>            <span class=pl-en>print</span>(<span class=pl-s>f&amp;quot;<span class=pl-cce>\\t</span>TENDERING <span class=pl-s1><span class=pl-kos>{</span><span class=pl-c1>-</span>(<span class=pl-s1>lambdas</span>[<span class=pl-s1>pool_id</span>].<span class=pl-s1>value</span>[<span class=pl-s1>local_token_index</span>] <span class=pl-c1>-</span> <span class=pl-s1>deltas</span>[<span class=pl-s1>pool_id</span>].<span class=pl-s1>value</span>[<span class=pl-s1>local_token_index</span>])<span class=pl-kos>}</span></span> TOKEN <span class=pl-s1><span class=pl-kos>{</span><span class=pl-s1>global_token_id</span><span class=pl-kos>}</span></span>&amp;quot;</span>)</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L147\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;147\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC147\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    </td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L148\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;148\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC148\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-k>for</span> <span class=pl-s1>global_token_id</span> <span class=pl-c1>in</span> <span class=pl-s1>pool</span>:</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L149\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;149\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC149\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>        <span class=pl-s1>local_token_index</span> <span class=pl-c1>=</span> <span class=pl-s1>pool</span>.<span class=pl-en>index</span>(<span class=pl-s1>global_token_id</span>)</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L150\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;150\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC150\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>        <span class=pl-k>if</span> (<span class=pl-s1>lambdas</span>[<span class=pl-s1>pool_id</span>].<span class=pl-s1>value</span>[<span class=pl-s1>local_token_index</span>] <span class=pl-c1>-</span> <span class=pl-s1>deltas</span>[<span class=pl-s1>pool_id</span>].<span class=pl-s1>value</span>[<span class=pl-s1>local_token_index</span>]) <span class=pl-c1>&amp;gt;=</span> <span class=pl-c1>0</span>:  </td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L151\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;151\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC151\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>            <span class=pl-en>print</span>(<span class=pl-s>f&amp;quot;<span class=pl-cce>\\t</span>RECEIVEING <span class=pl-s1><span class=pl-kos>{</span>(<span class=pl-s1>lambdas</span>[<span class=pl-s1>pool_id</span>].<span class=pl-s1>value</span>[<span class=pl-s1>local_token_index</span>] <span class=pl-c1>-</span> <span class=pl-s1>deltas</span>[<span class=pl-s1>pool_id</span>].<span class=pl-s1>value</span>[<span class=pl-s1>local_token_index</span>])<span class=pl-kos>}</span></span> TOKEN <span class=pl-s1><span class=pl-kos>{</span><span class=pl-s1>global_token_id</span><span class=pl-kos>}</span></span>&amp;quot;</span>)</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L152\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;152\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC152\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L153\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;153\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC153\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L154\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;154\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC154\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-en>print</span>(<span class=pl-s>&amp;quot;<span class=pl-cce>\\n</span>-------------------- REQUIRED TOKENS TO KICK-START ARBITRAGE --------------------<span class=pl-cce>\\n</span>&amp;quot;</span>)</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L155\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;155\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC155\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-en>print</span>(<span class=pl-s>f&amp;quot;TOKEN-0 = <span class=pl-s1><span class=pl-kos>{</span><span class=pl-s1>tokens_required_arr</span>[<span class=pl-s1>min_value_index</span>][<span class=pl-c1>0</span>]<span class=pl-kos>}</span></span>&amp;quot;</span>)</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L156\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;156\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC156\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-en>print</span>(<span class=pl-s>f&amp;quot;TOKEN-1 = <span class=pl-s1><span class=pl-kos>{</span><span class=pl-s1>tokens_required_arr</span>[<span class=pl-s1>min_value_index</span>][<span class=pl-c1>1</span>]<span class=pl-kos>}</span></span>&amp;quot;</span>)</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L157\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;157\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC157\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-en>print</span>(<span class=pl-s>f&amp;quot;TOKEN-2 = <span class=pl-s1><span class=pl-kos>{</span><span class=pl-s1>tokens_required_arr</span>[<span class=pl-s1>min_value_index</span>][<span class=pl-c1>2</span>]<span class=pl-kos>}</span></span>&amp;quot;</span>)</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L158\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;158\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC158\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-en>print</span>(<span class=pl-s>f&amp;quot;TOKEN-3 = <span class=pl-s1><span class=pl-kos>{</span><span class=pl-s1>tokens_required_arr</span>[<span class=pl-s1>min_value_index</span>][<span class=pl-c1>3</span>]<span class=pl-kos>}</span></span>&amp;quot;</span>)</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L159\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;159\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC159\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L160\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;160\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC160\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-en>print</span>(<span class=pl-s>f&amp;quot;<span class=pl-cce>\\n</span>USD VALUE REQUIRED = $<span class=pl-s1><span class=pl-kos>{</span><span class=pl-s1>min_value</span><span class=pl-kos>}</span></span>&amp;quot;</span>)</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L161\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;161\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC161\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L162\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;162\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC162\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-en>print</span>(<span class=pl-s>&amp;quot;<span class=pl-cce>\\n</span>-------------------- TOKENS &amp;amp; VALUE RECEIVED FROM ARBITRAGE --------------------<span class=pl-cce>\\n</span>&amp;quot;</span>)</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L163\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;163\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC163\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-s1>net_network_trade_tokens</span> <span class=pl-c1>=</span> [<span class=pl-c1>0</span>, <span class=pl-c1>0</span>, <span class=pl-c1>0</span>, <span class=pl-c1>0</span>]</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L164\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;164\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC164\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-s1>net_network_trade_value</span> <span class=pl-c1>=</span> [<span class=pl-c1>0</span>, <span class=pl-c1>0</span>, <span class=pl-c1>0</span>, <span class=pl-c1>0</span>]</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L165\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;165\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC165\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L166\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;166\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC166\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-k>for</span> <span class=pl-s1>pool_id</span> <span class=pl-c1>in</span> <span class=pl-s1>permutations2</span>[<span class=pl-s1>min_value_index</span>]:</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L167\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;167\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC167\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-s1>pool</span> <span class=pl-c1>=</span> <span class=pl-s1>local_indices</span>[<span class=pl-s1>pool_id</span>]</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L168\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;168\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC168\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-k>for</span> <span class=pl-s1>global_token_id</span> <span class=pl-c1>in</span> <span class=pl-s1>pool</span>:</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L169\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;169\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC169\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>        <span class=pl-s1>local_token_index</span> <span class=pl-c1>=</span> <span class=pl-s1>pool</span>.<span class=pl-en>index</span>(<span class=pl-s1>global_token_id</span>)</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L170\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;170\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC170\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>        <span class=pl-s1>net_network_trade_tokens</span>[<span class=pl-s1>global_token_id</span>] <span class=pl-c1>+=</span> <span class=pl-s1>lambdas</span>[<span class=pl-s1>pool_id</span>].<span class=pl-s1>value</span>[<span class=pl-s1>local_token_index</span>]</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L171\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;171\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC171\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>        <span class=pl-s1>net_network_trade_tokens</span>[<span class=pl-s1>global_token_id</span>] <span class=pl-c1>-=</span> <span class=pl-s1>deltas</span>[<span class=pl-s1>pool_id</span>].<span class=pl-s1>value</span>[<span class=pl-s1>local_token_index</span>]</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L172\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;172\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC172\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L173\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;173\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC173\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-k>for</span> <span class=pl-s1>i</span> <span class=pl-c1>in</span> <span class=pl-en>range</span>(<span class=pl-c1>0</span>, <span class=pl-en>len</span>(<span class=pl-s1>net_network_trade_tokens</span>)):</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L174\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;174\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC174\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-s1>net_network_trade_value</span>[<span class=pl-s1>i</span>] <span class=pl-c1>=</span> <span class=pl-s1>net_network_trade_tokens</span>[<span class=pl-s1>i</span>] <span class=pl-c1>*</span> <span class=pl-s1>market_value</span>[<span class=pl-s1>i</span>]</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L175\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;175\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC175\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L176\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;176\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC176\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-en>print</span>(<span class=pl-s>f&amp;quot;RECEIVED <span class=pl-s1><span class=pl-kos>{</span><span class=pl-s1>net_network_trade_tokens</span>[<span class=pl-c1>0</span>]<span class=pl-kos>}</span></span> TOKEN-0 = $<span class=pl-s1><span class=pl-kos>{</span><span class=pl-s1>net_network_trade_value</span>[<span class=pl-c1>0</span>]<span class=pl-kos>}</span></span>&amp;quot;</span>)</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L177\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;177\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC177\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-en>print</span>(<span class=pl-s>f&amp;quot;RECEIVED <span class=pl-s1><span class=pl-kos>{</span><span class=pl-s1>net_network_trade_tokens</span>[<span class=pl-c1>1</span>]<span class=pl-kos>}</span></span> TOKEN-1 = $<span class=pl-s1><span class=pl-kos>{</span><span class=pl-s1>net_network_trade_value</span>[<span class=pl-c1>1</span>]<span class=pl-kos>}</span></span>&amp;quot;</span>)</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L178\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;178\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC178\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-en>print</span>(<span class=pl-s>f&amp;quot;RECEIVED <span class=pl-s1><span class=pl-kos>{</span><span class=pl-s1>net_network_trade_tokens</span>[<span class=pl-c1>2</span>]<span class=pl-kos>}</span></span> TOKEN-2 = $<span class=pl-s1><span class=pl-kos>{</span><span class=pl-s1>net_network_trade_value</span>[<span class=pl-c1>2</span>]<span class=pl-kos>}</span></span>&amp;quot;</span>)</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L179\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;179\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC179\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-en>print</span>(<span class=pl-s>f&amp;quot;RECEIVED <span class=pl-s1><span class=pl-kos>{</span><span class=pl-s1>net_network_trade_tokens</span>[<span class=pl-c1>3</span>]<span class=pl-kos>}</span></span> TOKEN-3 = $<span class=pl-s1><span class=pl-kos>{</span><span class=pl-s1>net_network_trade_value</span>[<span class=pl-c1>3</span>]<span class=pl-kos>}</span></span>&amp;quot;</span>)</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L180\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;180\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC180\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L181\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;181\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC181\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-en>print</span>(<span class=pl-s>f&amp;quot;<span class=pl-cce>\\n</span>SUM OF RECEIVED TOKENS USD VALUE = $<span class=pl-s1><span class=pl-kos>{</span><span class=pl-en>sum</span>(<span class=pl-s1>net_network_trade_value</span>)<span class=pl-kos>}</span></span>&amp;quot;</span>)</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-arbitrage-py-L182\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;182\&quot;></td>\n          <td id=\&quot;file-arbitrage-py-LC182\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-en>print</span>(<span class=pl-s>f&amp;quot;CONVEX OPTIMISATION SOLVER RESULT: $<span class=pl-s1><span class=pl-kos>{</span><span class=pl-s1>prob</span>.<span class=pl-s1>value</span><span class=pl-kos>}</span></span><span class=pl-cce>\\n</span>&amp;quot;</span>)</td>\n        </tr>\n  </table>\n</div>\n\n\n    </div>\n\n  </div>\n</div>\n\n      </div>\n      <div class=\&quot;gist-meta\&quot;>\n        <a href=\&quot;https://gist.github.com/noxx3xxon/11fd224d7b99b78ee1e4a914bf0cbd22/raw/376a854183d4f2fc27e06058cfda2c5ea8e32efc/arbitrage.py\&quot; style=\&quot;float:right\&quot;>view raw</a>\n        <a href=\&quot;https://gist.github.com/noxx3xxon/11fd224d7b99b78ee1e4a914bf0cbd22#file-arbitrage-py\&quot;>\n          arbitrage.py\n        </a>\n        hosted with &amp;#10084; by <a href=\&quot;https://github.com\&quot;>GitHub</a>\n      </div>\n    </div>\n</div>\n&quot;,&quot;stylesheet&quot;:&quot;https://github.githubassets.com/assets/gist-embed-d0a825a44f5d.css&quot;}" data-component-name="GitgistToDOM"><link rel="stylesheet" href="https://github.githubassets.com/assets/gist-embed-d0a825a44f5d.css"><div id="gist117948369" class="gist">
    <div class="gist-file">
      <div class="gist-data">
        <div class="js-gist-file-update-container js-task-list-container file-box">
  <div id="file-arbitrage-py" class="file my-2">
    
    <div itemprop="text" class="Box-body p-0 blob-wrapper data type-python  ">

        
<div class="js-check-bidi js-blob-code-container blob-code-content">

  
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  
    

  
    <span>
      This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div>

  <span data-view-component="true" class="line-alert tooltipped tooltipped-e">
    
    

</span>

  <table data-hpc="" class="highlight tab-size js-file-line-container js-code-nav-container js-tagsearch-file" data-tab-size="8" data-paste-markdown-skip="" data-tagsearch-lang="Python" data-tagsearch-path="arbitrage.py">
        <tbody><tr>
          <td id="file-arbitrage-py-L1" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-arbitrage-py-LC1" class="blob-code blob-code-inner js-file-line"><span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L2" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-arbitrage-py-LC2" class="blob-code blob-code-inner js-file-line"><span class="pl-k">import</span> <span class="pl-s1">cvxpy</span> <span class="pl-k">as</span> <span class="pl-s1">cp</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L3" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="3"></td>
          <td id="file-arbitrage-py-LC3" class="blob-code blob-code-inner js-file-line"><span class="pl-k">import</span> <span class="pl-s1">itertools</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L4" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="4"></td>
          <td id="file-arbitrage-py-LC4" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L5" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="5"></td>
          <td id="file-arbitrage-py-LC5" class="blob-code blob-code-inner js-file-line"><span class="pl-c"># Problem data</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L6" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="6"></td>
          <td id="file-arbitrage-py-LC6" class="blob-code blob-code-inner js-file-line"><span class="pl-s1">global_indices</span> <span class="pl-c1">=</span> <span class="pl-en">list</span>(<span class="pl-en">range</span>(<span class="pl-c1">4</span>))</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L7" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="7"></td>
          <td id="file-arbitrage-py-LC7" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L8" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="8"></td>
          <td id="file-arbitrage-py-LC8" class="blob-code blob-code-inner js-file-line"><span class="pl-c"># 0 = TOKEN-0</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L9" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="9"></td>
          <td id="file-arbitrage-py-LC9" class="blob-code blob-code-inner js-file-line"><span class="pl-c"># 1 = TOKEN-1</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L10" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="10"></td>
          <td id="file-arbitrage-py-LC10" class="blob-code blob-code-inner js-file-line"><span class="pl-c"># 2 = TOKEN-2</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L11" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="11"></td>
          <td id="file-arbitrage-py-LC11" class="blob-code blob-code-inner js-file-line"><span class="pl-c"># 3 = TOKEN-3</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L12" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="12"></td>
          <td id="file-arbitrage-py-LC12" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L13" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="13"></td>
          <td id="file-arbitrage-py-LC13" class="blob-code blob-code-inner js-file-line"><span class="pl-s1">local_indices</span> <span class="pl-c1">=</span> [</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L14" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="14"></td>
          <td id="file-arbitrage-py-LC14" class="blob-code blob-code-inner js-file-line">    [<span class="pl-c1">0</span>, <span class="pl-c1">1</span>, <span class="pl-c1">2</span>, <span class="pl-c1">3</span>], <span class="pl-c"># TOKEN-0/TOKEN-1/TOKEN-2/TOKEN-3</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L15" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="15"></td>
          <td id="file-arbitrage-py-LC15" class="blob-code blob-code-inner js-file-line">    [<span class="pl-c1">0</span>, <span class="pl-c1">1</span>], <span class="pl-c"># TOKEN-0/TOKEN-1</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L16" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="16"></td>
          <td id="file-arbitrage-py-LC16" class="blob-code blob-code-inner js-file-line">    [<span class="pl-c1">1</span>, <span class="pl-c1">2</span>], <span class="pl-c"># TOKEN-1/TOKEN-2</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L17" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="17"></td>
          <td id="file-arbitrage-py-LC17" class="blob-code blob-code-inner js-file-line">    [<span class="pl-c1">2</span>, <span class="pl-c1">3</span>], <span class="pl-c"># TOKEN-2/TOKEN-3</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L18" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="18"></td>
          <td id="file-arbitrage-py-LC18" class="blob-code blob-code-inner js-file-line">    [<span class="pl-c1">2</span>, <span class="pl-c1">3</span>] <span class="pl-c"># TOKEN-2/TOKEN-3</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L19" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="19"></td>
          <td id="file-arbitrage-py-LC19" class="blob-code blob-code-inner js-file-line">]</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L20" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="20"></td>
          <td id="file-arbitrage-py-LC20" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L21" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="21"></td>
          <td id="file-arbitrage-py-LC21" class="blob-code blob-code-inner js-file-line"><span class="pl-s1">reserves</span> <span class="pl-c1">=</span> <span class="pl-en">list</span>(<span class="pl-en">map</span>(<span class="pl-s1">np</span>.<span class="pl-s1">array</span>, [</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L22" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="22"></td>
          <td id="file-arbitrage-py-LC22" class="blob-code blob-code-inner js-file-line">    [<span class="pl-c1">4</span>, <span class="pl-c1">4</span>, <span class="pl-c1">4</span>, <span class="pl-c1">4</span>], <span class="pl-c"># balancer with 4 assets in pool TOKEN-0, TOKEN-1, TOKEN-2, TOKEN-3 (4 TOKEN-0, 4 TOKEN-1, 4 TOKEN-2 &amp; 4 TOKEN-3 IN POOL)</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L23" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="23"></td>
          <td id="file-arbitrage-py-LC23" class="blob-code blob-code-inner js-file-line">    [<span class="pl-c1">10</span>, <span class="pl-c1">1</span>], <span class="pl-c"># uniswapV2 TOKEN-0/TOKEN-1 (10 TOKEN-0 &amp; 1 TOKEN-1 IN POOL)</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L24" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="24"></td>
          <td id="file-arbitrage-py-LC24" class="blob-code blob-code-inner js-file-line">    [<span class="pl-c1">1</span>, <span class="pl-c1">5</span>], <span class="pl-c"># uniswapV2 TOKEN-1/TOKEN-2 (1 TOKEN-1 &amp; 5 TOKEN-2 IN POOL)</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L25" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="25"></td>
          <td id="file-arbitrage-py-LC25" class="blob-code blob-code-inner js-file-line">    [<span class="pl-c1">40</span>, <span class="pl-c1">50</span>], <span class="pl-c"># uniswapV2 TOKEN-2/TOKEN-3  (40 TOKEN-2 &amp; 50 TOKEN-3 IN POOL)</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L26" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="26"></td>
          <td id="file-arbitrage-py-LC26" class="blob-code blob-code-inner js-file-line">    [<span class="pl-c1">10</span>, <span class="pl-c1">10</span>] <span class="pl-c"># constant_sum TOKEN-2/TOKEN-3 (10 TOKEN-2 &amp; 10 TOKEN-3 IN POOL)</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L27" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="27"></td>
          <td id="file-arbitrage-py-LC27" class="blob-code blob-code-inner js-file-line">]))</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L28" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="28"></td>
          <td id="file-arbitrage-py-LC28" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L29" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="29"></td>
          <td id="file-arbitrage-py-LC29" class="blob-code blob-code-inner js-file-line"><span class="pl-s1">fees</span> <span class="pl-c1">=</span> [</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L30" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="30"></td>
          <td id="file-arbitrage-py-LC30" class="blob-code blob-code-inner js-file-line">    <span class="pl-c1">.998</span>, <span class="pl-c"># balancer fees</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L31" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="31"></td>
          <td id="file-arbitrage-py-LC31" class="blob-code blob-code-inner js-file-line">    <span class="pl-c1">.997</span>, <span class="pl-c"># uniswapV2 fees</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L32" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="32"></td>
          <td id="file-arbitrage-py-LC32" class="blob-code blob-code-inner js-file-line">    <span class="pl-c1">.997</span>, <span class="pl-c"># uniswapV2 fees</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L33" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="33"></td>
          <td id="file-arbitrage-py-LC33" class="blob-code blob-code-inner js-file-line">    <span class="pl-c1">.997</span>, <span class="pl-c"># uniswapV2 fees</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L34" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="34"></td>
          <td id="file-arbitrage-py-LC34" class="blob-code blob-code-inner js-file-line">    <span class="pl-c1">.999</span> <span class="pl-c"># constant_sum fees</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L35" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="35"></td>
          <td id="file-arbitrage-py-LC35" class="blob-code blob-code-inner js-file-line">]</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L36" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="36"></td>
          <td id="file-arbitrage-py-LC36" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L37" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="37"></td>
          <td id="file-arbitrage-py-LC37" class="blob-code blob-code-inner js-file-line"><span class="pl-c"># "Market value" of tokens (say, in a centralized exchange)</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L38" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="38"></td>
          <td id="file-arbitrage-py-LC38" class="blob-code blob-code-inner js-file-line"><span class="pl-s1">market_value</span> <span class="pl-c1">=</span> [</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L39" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="39"></td>
          <td id="file-arbitrage-py-LC39" class="blob-code blob-code-inner js-file-line">    <span class="pl-c1">1.5</span>, <span class="pl-c"># TOKEN-0</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L40" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="40"></td>
          <td id="file-arbitrage-py-LC40" class="blob-code blob-code-inner js-file-line">    <span class="pl-c1">10</span>, <span class="pl-c"># TOKEN-1</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L41" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="41"></td>
          <td id="file-arbitrage-py-LC41" class="blob-code blob-code-inner js-file-line">    <span class="pl-c1">2</span>, <span class="pl-c"># TOKEN-2</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L42" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="42"></td>
          <td id="file-arbitrage-py-LC42" class="blob-code blob-code-inner js-file-line">    <span class="pl-c1">3</span> <span class="pl-c"># TOKEN-3</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L43" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="43"></td>
          <td id="file-arbitrage-py-LC43" class="blob-code blob-code-inner js-file-line">] </td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L44" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="44"></td>
          <td id="file-arbitrage-py-LC44" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L45" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="45"></td>
          <td id="file-arbitrage-py-LC45" class="blob-code blob-code-inner js-file-line"><span class="pl-c"># Build local-global matrices</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L46" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="46"></td>
          <td id="file-arbitrage-py-LC46" class="blob-code blob-code-inner js-file-line"><span class="pl-s1">n</span> <span class="pl-c1">=</span> <span class="pl-en">len</span>(<span class="pl-s1">global_indices</span>)</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L47" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="47"></td>
          <td id="file-arbitrage-py-LC47" class="blob-code blob-code-inner js-file-line"><span class="pl-s1">m</span> <span class="pl-c1">=</span> <span class="pl-en">len</span>(<span class="pl-s1">local_indices</span>)</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L48" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="48"></td>
          <td id="file-arbitrage-py-LC48" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L49" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="49"></td>
          <td id="file-arbitrage-py-LC49" class="blob-code blob-code-inner js-file-line"><span class="pl-v">A</span> <span class="pl-c1">=</span> []</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L50" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="50"></td>
          <td id="file-arbitrage-py-LC50" class="blob-code blob-code-inner js-file-line"><span class="pl-k">for</span> <span class="pl-s1">l</span> <span class="pl-c1">in</span> <span class="pl-s1">local_indices</span>: <span class="pl-c"># for each CFMM</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L51" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="51"></td>
          <td id="file-arbitrage-py-LC51" class="blob-code blob-code-inner js-file-line">    <span class="pl-s1">n_i</span> <span class="pl-c1">=</span> <span class="pl-en">len</span>(<span class="pl-s1">l</span>) <span class="pl-c"># n_i = number of tokens avaiable for CFMM i</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L52" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="52"></td>
          <td id="file-arbitrage-py-LC52" class="blob-code blob-code-inner js-file-line">    <span class="pl-v">A_i</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">zeros</span>((<span class="pl-s1">n</span>, <span class="pl-s1">n_i</span>)) <span class="pl-c"># Create matrix of 0's</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L53" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="53"></td>
          <td id="file-arbitrage-py-LC53" class="blob-code blob-code-inner js-file-line">    <span class="pl-k">for</span> <span class="pl-s1">i</span>, <span class="pl-s1">idx</span> <span class="pl-c1">in</span> <span class="pl-en">enumerate</span>(<span class="pl-s1">l</span>):</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L54" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="54"></td>
          <td id="file-arbitrage-py-LC54" class="blob-code blob-code-inner js-file-line">        <span class="pl-v">A_i</span>[<span class="pl-s1">idx</span>, <span class="pl-s1">i</span>] <span class="pl-c1">=</span> <span class="pl-c1">1</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L55" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="55"></td>
          <td id="file-arbitrage-py-LC55" class="blob-code blob-code-inner js-file-line">    <span class="pl-v">A</span>.<span class="pl-en">append</span>(<span class="pl-v">A_i</span>)</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L56" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="56"></td>
          <td id="file-arbitrage-py-LC56" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L57" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="57"></td>
          <td id="file-arbitrage-py-LC57" class="blob-code blob-code-inner js-file-line"><span class="pl-c"># Build variables</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L58" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="58"></td>
          <td id="file-arbitrage-py-LC58" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L59" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="59"></td>
          <td id="file-arbitrage-py-LC59" class="blob-code blob-code-inner js-file-line"><span class="pl-c"># tender delta</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L60" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="60"></td>
          <td id="file-arbitrage-py-LC60" class="blob-code blob-code-inner js-file-line"><span class="pl-s1">deltas</span> <span class="pl-c1">=</span> [<span class="pl-s1">cp</span>.<span class="pl-v">Variable</span>(<span class="pl-en">len</span>(<span class="pl-s1">l</span>), <span class="pl-s1">nonneg</span><span class="pl-c1">=</span><span class="pl-c1">True</span>) <span class="pl-k">for</span> <span class="pl-s1">l</span> <span class="pl-c1">in</span> <span class="pl-s1">local_indices</span>]</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L61" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="61"></td>
          <td id="file-arbitrage-py-LC61" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L62" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="62"></td>
          <td id="file-arbitrage-py-LC62" class="blob-code blob-code-inner js-file-line"><span class="pl-c"># receive lambda</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L63" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="63"></td>
          <td id="file-arbitrage-py-LC63" class="blob-code blob-code-inner js-file-line"><span class="pl-s1">lambdas</span> <span class="pl-c1">=</span> [<span class="pl-s1">cp</span>.<span class="pl-v">Variable</span>(<span class="pl-en">len</span>(<span class="pl-s1">l</span>), <span class="pl-s1">nonneg</span><span class="pl-c1">=</span><span class="pl-c1">True</span>) <span class="pl-k">for</span> <span class="pl-s1">l</span> <span class="pl-c1">in</span> <span class="pl-s1">local_indices</span>]</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L64" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="64"></td>
          <td id="file-arbitrage-py-LC64" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L65" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="65"></td>
          <td id="file-arbitrage-py-LC65" class="blob-code blob-code-inner js-file-line"><span class="pl-s1">psi</span> <span class="pl-c1">=</span> <span class="pl-s1">cp</span>.<span class="pl-en">sum</span>([<span class="pl-v">A_i</span> @ (<span class="pl-v">L</span> <span class="pl-c1">-</span> <span class="pl-v">D</span>) <span class="pl-k">for</span> <span class="pl-v">A_i</span>, <span class="pl-v">D</span>, <span class="pl-v">L</span> <span class="pl-c1">in</span> <span class="pl-en">zip</span>(<span class="pl-v">A</span>, <span class="pl-s1">deltas</span>, <span class="pl-s1">lambdas</span>)])</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L66" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="66"></td>
          <td id="file-arbitrage-py-LC66" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L67" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="67"></td>
          <td id="file-arbitrage-py-LC67" class="blob-code blob-code-inner js-file-line"><span class="pl-c"># Objective is to maximize "total market value" of coins out</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L68" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="68"></td>
          <td id="file-arbitrage-py-LC68" class="blob-code blob-code-inner js-file-line"><span class="pl-s1">obj</span> <span class="pl-c1">=</span> <span class="pl-s1">cp</span>.<span class="pl-v">Maximize</span>(<span class="pl-s1">market_value</span> @ <span class="pl-s1">psi</span>) <span class="pl-c"># matrix multiplication</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L69" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="69"></td>
          <td id="file-arbitrage-py-LC69" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L70" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="70"></td>
          <td id="file-arbitrage-py-LC70" class="blob-code blob-code-inner js-file-line"><span class="pl-c"># Reserves after trade</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L71" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="71"></td>
          <td id="file-arbitrage-py-LC71" class="blob-code blob-code-inner js-file-line"><span class="pl-s1">new_reserves</span> <span class="pl-c1">=</span> [<span class="pl-v">R</span> <span class="pl-c1">+</span> <span class="pl-s1">gamma_i</span><span class="pl-c1">*</span><span class="pl-v">D</span> <span class="pl-c1">-</span> <span class="pl-v">L</span> <span class="pl-k">for</span> <span class="pl-v">R</span>, <span class="pl-s1">gamma_i</span>, <span class="pl-v">D</span>, <span class="pl-v">L</span> <span class="pl-c1">in</span> <span class="pl-en">zip</span>(<span class="pl-s1">reserves</span>, <span class="pl-s1">fees</span>, <span class="pl-s1">deltas</span>, <span class="pl-s1">lambdas</span>)]</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L72" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="72"></td>
          <td id="file-arbitrage-py-LC72" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L73" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="73"></td>
          <td id="file-arbitrage-py-LC73" class="blob-code blob-code-inner js-file-line"><span class="pl-c"># Trading function constraints</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L74" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="74"></td>
          <td id="file-arbitrage-py-LC74" class="blob-code blob-code-inner js-file-line"><span class="pl-s1">cons</span> <span class="pl-c1">=</span> [</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L75" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="75"></td>
          <td id="file-arbitrage-py-LC75" class="blob-code blob-code-inner js-file-line">    <span class="pl-c"># Balancer pool with weights 4, 3, 2, 1</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L76" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="76"></td>
          <td id="file-arbitrage-py-LC76" class="blob-code blob-code-inner js-file-line">    <span class="pl-s1">cp</span>.<span class="pl-en">geo_mean</span>(<span class="pl-s1">new_reserves</span>[<span class="pl-c1">0</span>], <span class="pl-s1">p</span><span class="pl-c1">=</span><span class="pl-s1">np</span>.<span class="pl-en">array</span>([<span class="pl-c1">4</span>, <span class="pl-c1">3</span>, <span class="pl-c1">2</span>, <span class="pl-c1">1</span>])) <span class="pl-c1">&gt;=</span> <span class="pl-s1">cp</span>.<span class="pl-en">geo_mean</span>(<span class="pl-s1">reserves</span>[<span class="pl-c1">0</span>]),</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L77" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="77"></td>
          <td id="file-arbitrage-py-LC77" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L78" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="78"></td>
          <td id="file-arbitrage-py-LC78" class="blob-code blob-code-inner js-file-line">    <span class="pl-c"># Uniswap v2 pools</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L79" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="79"></td>
          <td id="file-arbitrage-py-LC79" class="blob-code blob-code-inner js-file-line">    <span class="pl-s1">cp</span>.<span class="pl-en">geo_mean</span>(<span class="pl-s1">new_reserves</span>[<span class="pl-c1">1</span>]) <span class="pl-c1">&gt;=</span> <span class="pl-s1">cp</span>.<span class="pl-en">geo_mean</span>(<span class="pl-s1">reserves</span>[<span class="pl-c1">1</span>]),</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L80" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="80"></td>
          <td id="file-arbitrage-py-LC80" class="blob-code blob-code-inner js-file-line">    <span class="pl-s1">cp</span>.<span class="pl-en">geo_mean</span>(<span class="pl-s1">new_reserves</span>[<span class="pl-c1">2</span>]) <span class="pl-c1">&gt;=</span> <span class="pl-s1">cp</span>.<span class="pl-en">geo_mean</span>(<span class="pl-s1">reserves</span>[<span class="pl-c1">2</span>]),</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L81" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="81"></td>
          <td id="file-arbitrage-py-LC81" class="blob-code blob-code-inner js-file-line">    <span class="pl-s1">cp</span>.<span class="pl-en">geo_mean</span>(<span class="pl-s1">new_reserves</span>[<span class="pl-c1">3</span>]) <span class="pl-c1">&gt;=</span> <span class="pl-s1">cp</span>.<span class="pl-en">geo_mean</span>(<span class="pl-s1">reserves</span>[<span class="pl-c1">3</span>]),</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L82" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="82"></td>
          <td id="file-arbitrage-py-LC82" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L83" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="83"></td>
          <td id="file-arbitrage-py-LC83" class="blob-code blob-code-inner js-file-line">    <span class="pl-c"># Constant sum pool</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L84" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="84"></td>
          <td id="file-arbitrage-py-LC84" class="blob-code blob-code-inner js-file-line">    <span class="pl-s1">cp</span>.<span class="pl-en">sum</span>(<span class="pl-s1">new_reserves</span>[<span class="pl-c1">4</span>]) <span class="pl-c1">&gt;=</span> <span class="pl-s1">cp</span>.<span class="pl-en">sum</span>(<span class="pl-s1">reserves</span>[<span class="pl-c1">4</span>]),</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L85" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="85"></td>
          <td id="file-arbitrage-py-LC85" class="blob-code blob-code-inner js-file-line">    <span class="pl-s1">new_reserves</span>[<span class="pl-c1">4</span>] <span class="pl-c1">&gt;=</span> <span class="pl-c1">0</span>,</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L86" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="86"></td>
          <td id="file-arbitrage-py-LC86" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L87" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="87"></td>
          <td id="file-arbitrage-py-LC87" class="blob-code blob-code-inner js-file-line">    <span class="pl-c"># Arbitrage constraint</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L88" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="88"></td>
          <td id="file-arbitrage-py-LC88" class="blob-code blob-code-inner js-file-line">    <span class="pl-s1">psi</span> <span class="pl-c1">&gt;=</span> <span class="pl-c1">0</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L89" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="89"></td>
          <td id="file-arbitrage-py-LC89" class="blob-code blob-code-inner js-file-line">]</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L90" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="90"></td>
          <td id="file-arbitrage-py-LC90" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L91" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="91"></td>
          <td id="file-arbitrage-py-LC91" class="blob-code blob-code-inner js-file-line"><span class="pl-c"># Set up and solve problem</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L92" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="92"></td>
          <td id="file-arbitrage-py-LC92" class="blob-code blob-code-inner js-file-line"><span class="pl-s1">prob</span> <span class="pl-c1">=</span> <span class="pl-s1">cp</span>.<span class="pl-v">Problem</span>(<span class="pl-s1">obj</span>, <span class="pl-s1">cons</span>)</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L93" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="93"></td>
          <td id="file-arbitrage-py-LC93" class="blob-code blob-code-inner js-file-line"><span class="pl-s1">prob</span>.<span class="pl-en">solve</span>()</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L94" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="94"></td>
          <td id="file-arbitrage-py-LC94" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L95" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="95"></td>
          <td id="file-arbitrage-py-LC95" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L96" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="96"></td>
          <td id="file-arbitrage-py-LC96" class="blob-code blob-code-inner js-file-line"><span class="pl-c"># Trade Execution Ordering</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L97" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="97"></td>
          <td id="file-arbitrage-py-LC97" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L98" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="98"></td>
          <td id="file-arbitrage-py-LC98" class="blob-code blob-code-inner js-file-line"><span class="pl-s1">current_tokens</span> <span class="pl-c1">=</span> [<span class="pl-c1">0</span>, <span class="pl-c1">0</span>, <span class="pl-c1">0</span>, <span class="pl-c1">0</span>]</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L99" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="99"></td>
          <td id="file-arbitrage-py-LC99" class="blob-code blob-code-inner js-file-line"><span class="pl-s1">new_current_tokens</span> <span class="pl-c1">=</span> [<span class="pl-c1">0</span>, <span class="pl-c1">0</span>, <span class="pl-c1">0</span>, <span class="pl-c1">0</span>]</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L100" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="100"></td>
          <td id="file-arbitrage-py-LC100" class="blob-code blob-code-inner js-file-line"><span class="pl-s1">tokens_required_arr</span> <span class="pl-c1">=</span> []</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L101" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="101"></td>
          <td id="file-arbitrage-py-LC101" class="blob-code blob-code-inner js-file-line"><span class="pl-s1">tokens_required_value_arr</span> <span class="pl-c1">=</span> []</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L102" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="102"></td>
          <td id="file-arbitrage-py-LC102" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L103" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="103"></td>
          <td id="file-arbitrage-py-LC103" class="blob-code blob-code-inner js-file-line"><span class="pl-s1">pool_names</span> <span class="pl-c1">=</span> [<span class="pl-s">"BALANCER 0/1/2/3"</span>, <span class="pl-s">"UNIV2 0/1"</span>, <span class="pl-s">"UNIV2 1/2"</span>, <span class="pl-s">"UNIV2 2/3"</span>, <span class="pl-s">"CONSTANT SUM 2/3"</span>]</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L104" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="104"></td>
          <td id="file-arbitrage-py-LC104" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L105" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="105"></td>
          <td id="file-arbitrage-py-LC105" class="blob-code blob-code-inner js-file-line"><span class="pl-s1">permutations</span> <span class="pl-c1">=</span> <span class="pl-s1">itertools</span>.<span class="pl-en">permutations</span>(<span class="pl-en">list</span>(<span class="pl-en">range</span>(<span class="pl-en">len</span>(<span class="pl-s1">local_indices</span>))), <span class="pl-en">len</span>(<span class="pl-s1">local_indices</span>))</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L106" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="106"></td>
          <td id="file-arbitrage-py-LC106" class="blob-code blob-code-inner js-file-line"><span class="pl-s1">permutations2</span> <span class="pl-c1">=</span> []</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L107" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="107"></td>
          <td id="file-arbitrage-py-LC107" class="blob-code blob-code-inner js-file-line"><span class="pl-k">for</span> <span class="pl-s1">permutation</span> <span class="pl-c1">in</span> <span class="pl-s1">permutations</span>:</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L108" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="108"></td>
          <td id="file-arbitrage-py-LC108" class="blob-code blob-code-inner js-file-line">    <span class="pl-s1">permutations2</span>.<span class="pl-en">append</span>(<span class="pl-s1">permutation</span>)</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L109" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="109"></td>
          <td id="file-arbitrage-py-LC109" class="blob-code blob-code-inner js-file-line">    <span class="pl-s1">current_tokens</span> <span class="pl-c1">=</span> [<span class="pl-c1">0</span>, <span class="pl-c1">0</span>, <span class="pl-c1">0</span>, <span class="pl-c1">0</span>]</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L110" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="110"></td>
          <td id="file-arbitrage-py-LC110" class="blob-code blob-code-inner js-file-line">    <span class="pl-s1">new_current_tokens</span> <span class="pl-c1">=</span> [<span class="pl-c1">0</span>, <span class="pl-c1">0</span>, <span class="pl-c1">0</span>, <span class="pl-c1">0</span>]   </td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L111" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="111"></td>
          <td id="file-arbitrage-py-LC111" class="blob-code blob-code-inner js-file-line">    <span class="pl-s1">tokens_required</span> <span class="pl-c1">=</span> [<span class="pl-c1">0</span>, <span class="pl-c1">0</span>, <span class="pl-c1">0</span>, <span class="pl-c1">0</span>]</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L112" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="112"></td>
          <td id="file-arbitrage-py-LC112" class="blob-code blob-code-inner js-file-line">    <span class="pl-k">for</span> <span class="pl-s1">pool_id</span> <span class="pl-c1">in</span> <span class="pl-s1">permutation</span>:</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L113" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="113"></td>
          <td id="file-arbitrage-py-LC113" class="blob-code blob-code-inner js-file-line">        <span class="pl-s1">pool</span> <span class="pl-c1">=</span> <span class="pl-s1">local_indices</span>[<span class="pl-s1">pool_id</span>]</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L114" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="114"></td>
          <td id="file-arbitrage-py-LC114" class="blob-code blob-code-inner js-file-line">        <span class="pl-k">for</span> <span class="pl-s1">global_token_id</span> <span class="pl-c1">in</span> <span class="pl-s1">pool</span>:</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L115" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="115"></td>
          <td id="file-arbitrage-py-LC115" class="blob-code blob-code-inner js-file-line">            <span class="pl-s1">local_token_index</span> <span class="pl-c1">=</span> <span class="pl-s1">pool</span>.<span class="pl-en">index</span>(<span class="pl-s1">global_token_id</span>)</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L116" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="116"></td>
          <td id="file-arbitrage-py-LC116" class="blob-code blob-code-inner js-file-line">            <span class="pl-s1">new_current_tokens</span>[<span class="pl-s1">global_token_id</span>] <span class="pl-c1">=</span> <span class="pl-s1">current_tokens</span>[<span class="pl-s1">global_token_id</span>] <span class="pl-c1">+</span> (<span class="pl-s1">lambdas</span>[<span class="pl-s1">pool_id</span>].<span class="pl-s1">value</span>[<span class="pl-s1">local_token_index</span>] <span class="pl-c1">-</span> <span class="pl-s1">deltas</span>[<span class="pl-s1">pool_id</span>].<span class="pl-s1">value</span>[<span class="pl-s1">local_token_index</span>])</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L117" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="117"></td>
          <td id="file-arbitrage-py-LC117" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L118" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="118"></td>
          <td id="file-arbitrage-py-LC118" class="blob-code blob-code-inner js-file-line">            <span class="pl-k">if</span> <span class="pl-s1">new_current_tokens</span>[<span class="pl-s1">global_token_id</span>] <span class="pl-c1">&lt;</span> <span class="pl-c1">0</span> <span class="pl-c1">and</span> <span class="pl-s1">new_current_tokens</span>[<span class="pl-s1">global_token_id</span>] <span class="pl-c1">&lt;</span> <span class="pl-s1">current_tokens</span>[<span class="pl-s1">global_token_id</span>]:</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L119" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="119"></td>
          <td id="file-arbitrage-py-LC119" class="blob-code blob-code-inner js-file-line">                <span class="pl-k">if</span> <span class="pl-s1">current_tokens</span>[<span class="pl-s1">global_token_id</span>] <span class="pl-c1">&lt;</span> <span class="pl-c1">0</span>:</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L120" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="120"></td>
          <td id="file-arbitrage-py-LC120" class="blob-code blob-code-inner js-file-line">                    <span class="pl-s1">tokens_required</span>[<span class="pl-s1">global_token_id</span>] <span class="pl-c1">+=</span> (<span class="pl-s1">current_tokens</span>[<span class="pl-s1">global_token_id</span>] <span class="pl-c1">-</span> <span class="pl-s1">new_current_tokens</span>[<span class="pl-s1">global_token_id</span>])</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L121" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="121"></td>
          <td id="file-arbitrage-py-LC121" class="blob-code blob-code-inner js-file-line">                    <span class="pl-s1">new_current_tokens</span>[<span class="pl-s1">global_token_id</span>] <span class="pl-c1">=</span> <span class="pl-c1">0</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L122" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="122"></td>
          <td id="file-arbitrage-py-LC122" class="blob-code blob-code-inner js-file-line">                <span class="pl-k">else</span>:</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L123" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="123"></td>
          <td id="file-arbitrage-py-LC123" class="blob-code blob-code-inner js-file-line">                    <span class="pl-s1">tokens_required</span>[<span class="pl-s1">global_token_id</span>] <span class="pl-c1">+=</span> (<span class="pl-c1">-</span><span class="pl-s1">new_current_tokens</span>[<span class="pl-s1">global_token_id</span>])</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L124" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="124"></td>
          <td id="file-arbitrage-py-LC124" class="blob-code blob-code-inner js-file-line">                    <span class="pl-s1">new_current_tokens</span>[<span class="pl-s1">global_token_id</span>] <span class="pl-c1">=</span> <span class="pl-c1">0</span></td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L125" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="125"></td>
          <td id="file-arbitrage-py-LC125" class="blob-code blob-code-inner js-file-line">            <span class="pl-s1">current_tokens</span>[<span class="pl-s1">global_token_id</span>] <span class="pl-c1">=</span> <span class="pl-s1">new_current_tokens</span>[<span class="pl-s1">global_token_id</span>]</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L126" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="126"></td>
          <td id="file-arbitrage-py-LC126" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L127" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="127"></td>
          <td id="file-arbitrage-py-LC127" class="blob-code blob-code-inner js-file-line">    <span class="pl-s1">tokens_required_value</span> <span class="pl-c1">=</span> []</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L128" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="128"></td>
          <td id="file-arbitrage-py-LC128" class="blob-code blob-code-inner js-file-line">    <span class="pl-k">for</span> <span class="pl-s1">i1</span>, <span class="pl-s1">i2</span> <span class="pl-c1">in</span> <span class="pl-en">zip</span>(<span class="pl-s1">tokens_required</span>, <span class="pl-s1">market_value</span>):</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L129" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="129"></td>
          <td id="file-arbitrage-py-LC129" class="blob-code blob-code-inner js-file-line">        <span class="pl-s1">tokens_required_value</span>.<span class="pl-en">append</span>(<span class="pl-s1">i1</span><span class="pl-c1">*</span><span class="pl-s1">i2</span>)</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L130" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="130"></td>
          <td id="file-arbitrage-py-LC130" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L131" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="131"></td>
          <td id="file-arbitrage-py-LC131" class="blob-code blob-code-inner js-file-line">    <span class="pl-s1">tokens_required_arr</span>.<span class="pl-en">append</span>(<span class="pl-s1">tokens_required</span>)</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L132" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="132"></td>
          <td id="file-arbitrage-py-LC132" class="blob-code blob-code-inner js-file-line">    <span class="pl-s1">tokens_required_value_arr</span>.<span class="pl-en">append</span>(<span class="pl-en">sum</span>(<span class="pl-s1">tokens_required_value</span>))</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L133" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="133"></td>
          <td id="file-arbitrage-py-LC133" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L134" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="134"></td>
          <td id="file-arbitrage-py-LC134" class="blob-code blob-code-inner js-file-line"><span class="pl-s1">min_value</span> <span class="pl-c1">=</span> <span class="pl-en">min</span>(<span class="pl-s1">tokens_required_value_arr</span>)</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L135" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="135"></td>
          <td id="file-arbitrage-py-LC135" class="blob-code blob-code-inner js-file-line"><span class="pl-s1">min_value_index</span> <span class="pl-c1">=</span> <span class="pl-s1">tokens_required_value_arr</span>.<span class="pl-en">index</span>(<span class="pl-s1">min_value</span>)</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L136" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="136"></td>
          <td id="file-arbitrage-py-LC136" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L137" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="137"></td>
          <td id="file-arbitrage-py-LC137" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L138" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="138"></td>
          <td id="file-arbitrage-py-LC138" class="blob-code blob-code-inner js-file-line"><span class="pl-en">print</span>(<span class="pl-s">"<span class="pl-cce">\n</span>-------------------- ARBITRAGE TRADES + EXECUTION ORDER --------------------<span class="pl-cce">\n</span>"</span>)</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L139" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="139"></td>
          <td id="file-arbitrage-py-LC139" class="blob-code blob-code-inner js-file-line"><span class="pl-k">for</span> <span class="pl-s1">pool_id</span> <span class="pl-c1">in</span> <span class="pl-s1">permutations2</span>[<span class="pl-s1">min_value_index</span>]:</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L140" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="140"></td>
          <td id="file-arbitrage-py-LC140" class="blob-code blob-code-inner js-file-line">    <span class="pl-s1">pool</span> <span class="pl-c1">=</span> <span class="pl-s1">local_indices</span>[<span class="pl-s1">pool_id</span>]</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L141" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="141"></td>
          <td id="file-arbitrage-py-LC141" class="blob-code blob-code-inner js-file-line">    <span class="pl-en">print</span>(<span class="pl-s">f"<span class="pl-cce">\n</span>TRADE POOL = <span class="pl-s1"><span class="pl-kos">{</span><span class="pl-s1">pool_names</span>[<span class="pl-s1">pool_id</span>]<span class="pl-kos">}</span></span>"</span>)</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L142" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="142"></td>
          <td id="file-arbitrage-py-LC142" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L143" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="143"></td>
          <td id="file-arbitrage-py-LC143" class="blob-code blob-code-inner js-file-line">    <span class="pl-k">for</span> <span class="pl-s1">global_token_id</span> <span class="pl-c1">in</span> <span class="pl-s1">pool</span>:</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L144" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="144"></td>
          <td id="file-arbitrage-py-LC144" class="blob-code blob-code-inner js-file-line">        <span class="pl-s1">local_token_index</span> <span class="pl-c1">=</span> <span class="pl-s1">pool</span>.<span class="pl-en">index</span>(<span class="pl-s1">global_token_id</span>)</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L145" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="145"></td>
          <td id="file-arbitrage-py-LC145" class="blob-code blob-code-inner js-file-line">        <span class="pl-k">if</span> (<span class="pl-s1">lambdas</span>[<span class="pl-s1">pool_id</span>].<span class="pl-s1">value</span>[<span class="pl-s1">local_token_index</span>] <span class="pl-c1">-</span> <span class="pl-s1">deltas</span>[<span class="pl-s1">pool_id</span>].<span class="pl-s1">value</span>[<span class="pl-s1">local_token_index</span>]) <span class="pl-c1">&lt;</span> <span class="pl-c1">0</span>:  </td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L146" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="146"></td>
          <td id="file-arbitrage-py-LC146" class="blob-code blob-code-inner js-file-line">            <span class="pl-en">print</span>(<span class="pl-s">f"<span class="pl-cce">\t</span>TENDERING <span class="pl-s1"><span class="pl-kos">{</span><span class="pl-c1">-</span>(<span class="pl-s1">lambdas</span>[<span class="pl-s1">pool_id</span>].<span class="pl-s1">value</span>[<span class="pl-s1">local_token_index</span>] <span class="pl-c1">-</span> <span class="pl-s1">deltas</span>[<span class="pl-s1">pool_id</span>].<span class="pl-s1">value</span>[<span class="pl-s1">local_token_index</span>])<span class="pl-kos">}</span></span> TOKEN <span class="pl-s1"><span class="pl-kos">{</span><span class="pl-s1">global_token_id</span><span class="pl-kos">}</span></span>"</span>)</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L147" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="147"></td>
          <td id="file-arbitrage-py-LC147" class="blob-code blob-code-inner js-file-line">    </td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L148" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="148"></td>
          <td id="file-arbitrage-py-LC148" class="blob-code blob-code-inner js-file-line">    <span class="pl-k">for</span> <span class="pl-s1">global_token_id</span> <span class="pl-c1">in</span> <span class="pl-s1">pool</span>:</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L149" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="149"></td>
          <td id="file-arbitrage-py-LC149" class="blob-code blob-code-inner js-file-line">        <span class="pl-s1">local_token_index</span> <span class="pl-c1">=</span> <span class="pl-s1">pool</span>.<span class="pl-en">index</span>(<span class="pl-s1">global_token_id</span>)</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L150" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="150"></td>
          <td id="file-arbitrage-py-LC150" class="blob-code blob-code-inner js-file-line">        <span class="pl-k">if</span> (<span class="pl-s1">lambdas</span>[<span class="pl-s1">pool_id</span>].<span class="pl-s1">value</span>[<span class="pl-s1">local_token_index</span>] <span class="pl-c1">-</span> <span class="pl-s1">deltas</span>[<span class="pl-s1">pool_id</span>].<span class="pl-s1">value</span>[<span class="pl-s1">local_token_index</span>]) <span class="pl-c1">&gt;=</span> <span class="pl-c1">0</span>:  </td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L151" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="151"></td>
          <td id="file-arbitrage-py-LC151" class="blob-code blob-code-inner js-file-line">            <span class="pl-en">print</span>(<span class="pl-s">f"<span class="pl-cce">\t</span>RECEIVEING <span class="pl-s1"><span class="pl-kos">{</span>(<span class="pl-s1">lambdas</span>[<span class="pl-s1">pool_id</span>].<span class="pl-s1">value</span>[<span class="pl-s1">local_token_index</span>] <span class="pl-c1">-</span> <span class="pl-s1">deltas</span>[<span class="pl-s1">pool_id</span>].<span class="pl-s1">value</span>[<span class="pl-s1">local_token_index</span>])<span class="pl-kos">}</span></span> TOKEN <span class="pl-s1"><span class="pl-kos">{</span><span class="pl-s1">global_token_id</span><span class="pl-kos">}</span></span>"</span>)</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L152" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="152"></td>
          <td id="file-arbitrage-py-LC152" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L153" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="153"></td>
          <td id="file-arbitrage-py-LC153" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L154" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="154"></td>
          <td id="file-arbitrage-py-LC154" class="blob-code blob-code-inner js-file-line"><span class="pl-en">print</span>(<span class="pl-s">"<span class="pl-cce">\n</span>-------------------- REQUIRED TOKENS TO KICK-START ARBITRAGE --------------------<span class="pl-cce">\n</span>"</span>)</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L155" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="155"></td>
          <td id="file-arbitrage-py-LC155" class="blob-code blob-code-inner js-file-line"><span class="pl-en">print</span>(<span class="pl-s">f"TOKEN-0 = <span class="pl-s1"><span class="pl-kos">{</span><span class="pl-s1">tokens_required_arr</span>[<span class="pl-s1">min_value_index</span>][<span class="pl-c1">0</span>]<span class="pl-kos">}</span></span>"</span>)</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L156" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="156"></td>
          <td id="file-arbitrage-py-LC156" class="blob-code blob-code-inner js-file-line"><span class="pl-en">print</span>(<span class="pl-s">f"TOKEN-1 = <span class="pl-s1"><span class="pl-kos">{</span><span class="pl-s1">tokens_required_arr</span>[<span class="pl-s1">min_value_index</span>][<span class="pl-c1">1</span>]<span class="pl-kos">}</span></span>"</span>)</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L157" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="157"></td>
          <td id="file-arbitrage-py-LC157" class="blob-code blob-code-inner js-file-line"><span class="pl-en">print</span>(<span class="pl-s">f"TOKEN-2 = <span class="pl-s1"><span class="pl-kos">{</span><span class="pl-s1">tokens_required_arr</span>[<span class="pl-s1">min_value_index</span>][<span class="pl-c1">2</span>]<span class="pl-kos">}</span></span>"</span>)</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L158" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="158"></td>
          <td id="file-arbitrage-py-LC158" class="blob-code blob-code-inner js-file-line"><span class="pl-en">print</span>(<span class="pl-s">f"TOKEN-3 = <span class="pl-s1"><span class="pl-kos">{</span><span class="pl-s1">tokens_required_arr</span>[<span class="pl-s1">min_value_index</span>][<span class="pl-c1">3</span>]<span class="pl-kos">}</span></span>"</span>)</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L159" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="159"></td>
          <td id="file-arbitrage-py-LC159" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L160" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="160"></td>
          <td id="file-arbitrage-py-LC160" class="blob-code blob-code-inner js-file-line"><span class="pl-en">print</span>(<span class="pl-s">f"<span class="pl-cce">\n</span>USD VALUE REQUIRED = $<span class="pl-s1"><span class="pl-kos">{</span><span class="pl-s1">min_value</span><span class="pl-kos">}</span></span>"</span>)</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L161" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="161"></td>
          <td id="file-arbitrage-py-LC161" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L162" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="162"></td>
          <td id="file-arbitrage-py-LC162" class="blob-code blob-code-inner js-file-line"><span class="pl-en">print</span>(<span class="pl-s">"<span class="pl-cce">\n</span>-------------------- TOKENS &amp; VALUE RECEIVED FROM ARBITRAGE --------------------<span class="pl-cce">\n</span>"</span>)</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L163" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="163"></td>
          <td id="file-arbitrage-py-LC163" class="blob-code blob-code-inner js-file-line"><span class="pl-s1">net_network_trade_tokens</span> <span class="pl-c1">=</span> [<span class="pl-c1">0</span>, <span class="pl-c1">0</span>, <span class="pl-c1">0</span>, <span class="pl-c1">0</span>]</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L164" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="164"></td>
          <td id="file-arbitrage-py-LC164" class="blob-code blob-code-inner js-file-line"><span class="pl-s1">net_network_trade_value</span> <span class="pl-c1">=</span> [<span class="pl-c1">0</span>, <span class="pl-c1">0</span>, <span class="pl-c1">0</span>, <span class="pl-c1">0</span>]</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L165" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="165"></td>
          <td id="file-arbitrage-py-LC165" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L166" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="166"></td>
          <td id="file-arbitrage-py-LC166" class="blob-code blob-code-inner js-file-line"><span class="pl-k">for</span> <span class="pl-s1">pool_id</span> <span class="pl-c1">in</span> <span class="pl-s1">permutations2</span>[<span class="pl-s1">min_value_index</span>]:</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L167" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="167"></td>
          <td id="file-arbitrage-py-LC167" class="blob-code blob-code-inner js-file-line">    <span class="pl-s1">pool</span> <span class="pl-c1">=</span> <span class="pl-s1">local_indices</span>[<span class="pl-s1">pool_id</span>]</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L168" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="168"></td>
          <td id="file-arbitrage-py-LC168" class="blob-code blob-code-inner js-file-line">    <span class="pl-k">for</span> <span class="pl-s1">global_token_id</span> <span class="pl-c1">in</span> <span class="pl-s1">pool</span>:</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L169" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="169"></td>
          <td id="file-arbitrage-py-LC169" class="blob-code blob-code-inner js-file-line">        <span class="pl-s1">local_token_index</span> <span class="pl-c1">=</span> <span class="pl-s1">pool</span>.<span class="pl-en">index</span>(<span class="pl-s1">global_token_id</span>)</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L170" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="170"></td>
          <td id="file-arbitrage-py-LC170" class="blob-code blob-code-inner js-file-line">        <span class="pl-s1">net_network_trade_tokens</span>[<span class="pl-s1">global_token_id</span>] <span class="pl-c1">+=</span> <span class="pl-s1">lambdas</span>[<span class="pl-s1">pool_id</span>].<span class="pl-s1">value</span>[<span class="pl-s1">local_token_index</span>]</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L171" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="171"></td>
          <td id="file-arbitrage-py-LC171" class="blob-code blob-code-inner js-file-line">        <span class="pl-s1">net_network_trade_tokens</span>[<span class="pl-s1">global_token_id</span>] <span class="pl-c1">-=</span> <span class="pl-s1">deltas</span>[<span class="pl-s1">pool_id</span>].<span class="pl-s1">value</span>[<span class="pl-s1">local_token_index</span>]</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L172" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="172"></td>
          <td id="file-arbitrage-py-LC172" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L173" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="173"></td>
          <td id="file-arbitrage-py-LC173" class="blob-code blob-code-inner js-file-line"><span class="pl-k">for</span> <span class="pl-s1">i</span> <span class="pl-c1">in</span> <span class="pl-en">range</span>(<span class="pl-c1">0</span>, <span class="pl-en">len</span>(<span class="pl-s1">net_network_trade_tokens</span>)):</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L174" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="174"></td>
          <td id="file-arbitrage-py-LC174" class="blob-code blob-code-inner js-file-line">    <span class="pl-s1">net_network_trade_value</span>[<span class="pl-s1">i</span>] <span class="pl-c1">=</span> <span class="pl-s1">net_network_trade_tokens</span>[<span class="pl-s1">i</span>] <span class="pl-c1">*</span> <span class="pl-s1">market_value</span>[<span class="pl-s1">i</span>]</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L175" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="175"></td>
          <td id="file-arbitrage-py-LC175" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L176" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="176"></td>
          <td id="file-arbitrage-py-LC176" class="blob-code blob-code-inner js-file-line"><span class="pl-en">print</span>(<span class="pl-s">f"RECEIVED <span class="pl-s1"><span class="pl-kos">{</span><span class="pl-s1">net_network_trade_tokens</span>[<span class="pl-c1">0</span>]<span class="pl-kos">}</span></span> TOKEN-0 = $<span class="pl-s1"><span class="pl-kos">{</span><span class="pl-s1">net_network_trade_value</span>[<span class="pl-c1">0</span>]<span class="pl-kos">}</span></span>"</span>)</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L177" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="177"></td>
          <td id="file-arbitrage-py-LC177" class="blob-code blob-code-inner js-file-line"><span class="pl-en">print</span>(<span class="pl-s">f"RECEIVED <span class="pl-s1"><span class="pl-kos">{</span><span class="pl-s1">net_network_trade_tokens</span>[<span class="pl-c1">1</span>]<span class="pl-kos">}</span></span> TOKEN-1 = $<span class="pl-s1"><span class="pl-kos">{</span><span class="pl-s1">net_network_trade_value</span>[<span class="pl-c1">1</span>]<span class="pl-kos">}</span></span>"</span>)</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L178" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="178"></td>
          <td id="file-arbitrage-py-LC178" class="blob-code blob-code-inner js-file-line"><span class="pl-en">print</span>(<span class="pl-s">f"RECEIVED <span class="pl-s1"><span class="pl-kos">{</span><span class="pl-s1">net_network_trade_tokens</span>[<span class="pl-c1">2</span>]<span class="pl-kos">}</span></span> TOKEN-2 = $<span class="pl-s1"><span class="pl-kos">{</span><span class="pl-s1">net_network_trade_value</span>[<span class="pl-c1">2</span>]<span class="pl-kos">}</span></span>"</span>)</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L179" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="179"></td>
          <td id="file-arbitrage-py-LC179" class="blob-code blob-code-inner js-file-line"><span class="pl-en">print</span>(<span class="pl-s">f"RECEIVED <span class="pl-s1"><span class="pl-kos">{</span><span class="pl-s1">net_network_trade_tokens</span>[<span class="pl-c1">3</span>]<span class="pl-kos">}</span></span> TOKEN-3 = $<span class="pl-s1"><span class="pl-kos">{</span><span class="pl-s1">net_network_trade_value</span>[<span class="pl-c1">3</span>]<span class="pl-kos">}</span></span>"</span>)</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L180" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="180"></td>
          <td id="file-arbitrage-py-LC180" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L181" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="181"></td>
          <td id="file-arbitrage-py-LC181" class="blob-code blob-code-inner js-file-line"><span class="pl-en">print</span>(<span class="pl-s">f"<span class="pl-cce">\n</span>SUM OF RECEIVED TOKENS USD VALUE = $<span class="pl-s1"><span class="pl-kos">{</span><span class="pl-en">sum</span>(<span class="pl-s1">net_network_trade_value</span>)<span class="pl-kos">}</span></span>"</span>)</td>
        </tr>
        <tr>
          <td id="file-arbitrage-py-L182" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="182"></td>
          <td id="file-arbitrage-py-LC182" class="blob-code blob-code-inner js-file-line"><span class="pl-en">print</span>(<span class="pl-s">f"CONVEX OPTIMISATION SOLVER RESULT: $<span class="pl-s1"><span class="pl-kos">{</span><span class="pl-s1">prob</span>.<span class="pl-s1">value</span><span class="pl-kos">}</span></span><span class="pl-cce">\n</span>"</span>)</td>
        </tr>
  </tbody></table>
</div>


    </div>

  </div>
</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/noxx3xxon/11fd224d7b99b78ee1e4a914bf0cbd22/raw/376a854183d4f2fc27e06058cfda2c5ea8e32efc/arbitrage.py" style="float:right">view raw</a>
        <a href="https://gist.github.com/noxx3xxon/11fd224d7b99b78ee1e4a914bf0cbd22#file-arbitrage-py">
          arbitrage.py
        </a>
        hosted with &#10084; by <a href="https://github.com">GitHub</a>
      </div>
    </div>
</div>
</div><p>Here is the output from the code defining the trades, their execution order, the minimum number of tokens required to kick start the trade and the total tokens received from the arbitrage.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!6brt!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F7bed1f76-5a42-485f-88d9-e4856763c1eb_580x735.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!6brt!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F7bed1f76-5a42-485f-88d9-e4856763c1eb_580x735.png 424w, https://substackcdn.com/image/fetch/$s_!6brt!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F7bed1f76-5a42-485f-88d9-e4856763c1eb_580x735.png 848w, https://substackcdn.com/image/fetch/$s_!6brt!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F7bed1f76-5a42-485f-88d9-e4856763c1eb_580x735.png 1272w, https://substackcdn.com/image/fetch/$s_!6brt!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F7bed1f76-5a42-485f-88d9-e4856763c1eb_580x735.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!6brt!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F7bed1f76-5a42-485f-88d9-e4856763c1eb_580x735.png" width="580" height="735" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/7bed1f76-5a42-485f-88d9-e4856763c1eb_580x735.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:735,&quot;width&quot;:580,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:274432,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!6brt!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F7bed1f76-5a42-485f-88d9-e4856763c1eb_580x735.png 424w, https://substackcdn.com/image/fetch/$s_!6brt!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F7bed1f76-5a42-485f-88d9-e4856763c1eb_580x735.png 848w, https://substackcdn.com/image/fetch/$s_!6brt!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F7bed1f76-5a42-485f-88d9-e4856763c1eb_580x735.png 1272w, https://substackcdn.com/image/fetch/$s_!6brt!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F7bed1f76-5a42-485f-88d9-e4856763c1eb_580x735.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Output from <a href="https://gist.github.com/noxx3xxon/11fd224d7b99b78ee1e4a914bf0cbd22">arbitrage.py</a></figcaption></figure></div><p>There we have it I hope you&#8217;ve learned something new and added a new tool to your arbitrage tool kit.</p><p>Til next time</p><p>noxx</p><p>Follow me on Twitter <a href="https://twitter.com/noxx3xxon">@noxx3xxon</a></p>]]></content:encoded></item><item><title><![CDATA[Flashbots: Kings of The Mempool 🤖]]></title><description><![CDATA[Inside The Mempool, The Evolution of MEV-Geth & A Peek into the Future With MEV Boost]]></description><link>https://noxx.substack.com/p/flashbots-kings-of-the-mempool</link><guid isPermaLink="false">https://noxx.substack.com/p/flashbots-kings-of-the-mempool</guid><dc:creator><![CDATA[noxx]]></dc:creator><pubDate>Mon, 11 Jul 2022 15:26:51 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/h_600,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7996a76-2829-42a6-84ee-dd539de01647_2714x2668.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>So you&#8217;ve heard of the Mempool, Flashbots, Bundles &amp; Searcher Auctions but do you truly understand what these things are and how they work.</p><p>Do you understand their inner mechanics?</p><p>This article looks to provide a deep dive into the Mempool through the lens of the Flashbots auction. </p><p>The reader will be taken on a journey through the development of MEV Geth, a custom Ethereum client that enabled Flashbots to alter the shape &amp; structure of the Mempool.</p><p>MEV Geth along with the Flashbots relay provided the gateway between searchers &amp; miners changing the MEV game forever.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://noxx.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://noxx.substack.com/subscribe?"><span>Subscribe now</span></a></p><h2>The Mempool</h2><p>Let&#8217;s start with the protagonist of this story &#8220;The Mempool&#8221; before the first official MEV Geth even existed.</p><blockquote><p><em>The mempool (memory pool) is a smaller database of unconfirmed or pending transactions which every node keeps. When a transaction is confirmed by being included in a block, it is removed from the mempool.</em></p></blockquote><p>The definition is a great place to get us started. Let&#8217;s cover a few more basics.</p><ul><li><p>A node ie Geth (or MEV Geth) keeps a list of unconfirmed or pending transactions. </p></li><li><p>These transactions are communicated to the nodes through their peers or are received directly through their RPC endpoint. </p></li><li><p>Each node has its own Mempool, there is no such thing as a global Mempool.</p></li><li><p>This means nodes can/do have different Mempools depending on where they are geographically and the peers they are connected to.</p></li><li><p>The nodes limit on the number of transactions they keep in their Mempool this is how they ensure they don&#8217;t get overwhelmed.</p></li></ul><p>The Mempool is represented in a Geth node via a TxPool struct which can be seen below.  </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!2oZW!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9b98512a-21d7-4763-8a07-bba51aab7e08_987x1274.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!2oZW!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9b98512a-21d7-4763-8a07-bba51aab7e08_987x1274.png 424w, https://substackcdn.com/image/fetch/$s_!2oZW!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9b98512a-21d7-4763-8a07-bba51aab7e08_987x1274.png 848w, https://substackcdn.com/image/fetch/$s_!2oZW!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9b98512a-21d7-4763-8a07-bba51aab7e08_987x1274.png 1272w, https://substackcdn.com/image/fetch/$s_!2oZW!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9b98512a-21d7-4763-8a07-bba51aab7e08_987x1274.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!2oZW!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9b98512a-21d7-4763-8a07-bba51aab7e08_987x1274.png" width="987" height="1274" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/9b98512a-21d7-4763-8a07-bba51aab7e08_987x1274.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1274,&quot;width&quot;:987,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:784122,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!2oZW!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9b98512a-21d7-4763-8a07-bba51aab7e08_987x1274.png 424w, https://substackcdn.com/image/fetch/$s_!2oZW!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9b98512a-21d7-4763-8a07-bba51aab7e08_987x1274.png 848w, https://substackcdn.com/image/fetch/$s_!2oZW!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9b98512a-21d7-4763-8a07-bba51aab7e08_987x1274.png 1272w, https://substackcdn.com/image/fetch/$s_!2oZW!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9b98512a-21d7-4763-8a07-bba51aab7e08_987x1274.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><a href="https://github.com/ethereum/go-ethereum/blob/v1.10.1/core/tx_pool.go">tx_pool.go</a></figcaption></figure></div><p>Two items have been highlighted. The first item, &#8220;locals&#8221;, represents a set of addresses. If a transaction in the TxPool (Mempool) is from one of these addresses it will be considered a local transaction (You&#8217;ll see why this is important in the next section).</p><p>The second highlighted item is &#8220;pending&#8221; which represents all currently processable transactions in the TxPool.</p><p>You might also notice the &#8220;queue&#8221; field on line 242, this represents transactions that are in the TxPool but aren&#8217;t processable. An example would be a Tx with a nonce that is not in sequence, indicating there is another transaction from the same address which should be processed first.</p><p>When thinking about MEV the next question you need to ask is how do these transactions find their way from the TxPool into a block.</p><p>Below is the Geth code that executes this functionality. You&#8217;ll see that the transactions are actually split into localTxs and remoteTxs, ordered by their gas price / nonce and that localTxs get priority over remoteTxs.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!hibP!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F838c0955-dc90-4449-8145-53f55a6f21d7_3398x1896.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!hibP!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F838c0955-dc90-4449-8145-53f55a6f21d7_3398x1896.png 424w, https://substackcdn.com/image/fetch/$s_!hibP!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F838c0955-dc90-4449-8145-53f55a6f21d7_3398x1896.png 848w, https://substackcdn.com/image/fetch/$s_!hibP!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F838c0955-dc90-4449-8145-53f55a6f21d7_3398x1896.png 1272w, https://substackcdn.com/image/fetch/$s_!hibP!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F838c0955-dc90-4449-8145-53f55a6f21d7_3398x1896.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!hibP!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F838c0955-dc90-4449-8145-53f55a6f21d7_3398x1896.png" width="1200" height="669.2307692307693" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/838c0955-dc90-4449-8145-53f55a6f21d7_3398x1896.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:812,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:824917,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!hibP!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F838c0955-dc90-4449-8145-53f55a6f21d7_3398x1896.png 424w, https://substackcdn.com/image/fetch/$s_!hibP!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F838c0955-dc90-4449-8145-53f55a6f21d7_3398x1896.png 848w, https://substackcdn.com/image/fetch/$s_!hibP!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F838c0955-dc90-4449-8145-53f55a6f21d7_3398x1896.png 1272w, https://substackcdn.com/image/fetch/$s_!hibP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F838c0955-dc90-4449-8145-53f55a6f21d7_3398x1896.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><a href="https://github.com/ethereum/go-ethereum/blob/v1.10.1/miner/worker.go">worker.go</a> &amp; <a href="https://github.com/ethereum/go-ethereum/blob/v1.10.1/core/types/transaction.go">transaction.go</a></figcaption></figure></div><ol><li><p>The &#8220;commitNewWork&#8221; function does many things including populating the block with transactions from the TxPool </p></li><li><p>This section gets the transactions from the TxPool and splits them into local and remote transactions</p><ol><li><p>On line 954 you can see the comment &#8220;Fill the block with all available pending transactions&#8221; after this line, we set the &#8220;pending&#8221; variable via w.eth.TxPool().Pending(true). This returns a key-value mapping of address to a transaction.</p></li><li><p>Lines 967 - 974 show us using the &#8220;locals&#8221; variable we highlighted earlier to split the transactions into local &amp; remote. We are looping through the locals addresses and checking to see if any of the pending Txs are from a locals address. If it matches, those transactions are removed from remoteTxs and added to localsTxs. (Locals is a set of addresses a node can define in its config, they get priority over remote transactions and won&#8217;t be dropped by the node)</p></li><li><p>The end result is the pending transactions are split into localTxs which have come from &#8220;locals&#8221; addresses and remoteTxs which have come from everyone else.</p></li></ol></li><li><p>This block represents the localTxs being sorted and committed into the block. Since the localTxs are processed first in the code they get priority over the remoteTxs.</p></li><li><p>The transactions are ordered by gas price and nonce via &#8220;NewTransactionsByPriceAndNonce&#8221; and then committed to the block via &#8220;commitTransactions&#8221;</p></li><li><p>&#8220;NewTransactionsByPriceAndNonce&#8221; takes a set of Txs orders them by gas price and ensures account nonces are not out of order. To do this it uses a heap data structure which we can see initialised on line 443. </p><ol><li><p>The heads variable is an array of TxByPriceAndTime which under the hood is just an array of transactions. It implements the heap interface which enables us to use heap calls to sort our data. It represents the &#8220;Next transaction for each unique account (price heap)&#8221;.  To understand more about heaps see <a href="https://www.youtube.com/watch?v=3DYIgTC4T1o">here</a>.</p></li><li><p>Transactions are ordered per account by gas price but nonce order is maintained. This means a high gas-priced transaction with a nonce that isn&#8217;t next may won&#8217;t be at the top of the block. The below printout of a Geth test case demonstrates this.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!4lh6!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb27ba284-88f4-4b77-97bb-463ee0a569b8_547x336.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!4lh6!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb27ba284-88f4-4b77-97bb-463ee0a569b8_547x336.png 424w, https://substackcdn.com/image/fetch/$s_!4lh6!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb27ba284-88f4-4b77-97bb-463ee0a569b8_547x336.png 848w, https://substackcdn.com/image/fetch/$s_!4lh6!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb27ba284-88f4-4b77-97bb-463ee0a569b8_547x336.png 1272w, https://substackcdn.com/image/fetch/$s_!4lh6!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb27ba284-88f4-4b77-97bb-463ee0a569b8_547x336.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!4lh6!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb27ba284-88f4-4b77-97bb-463ee0a569b8_547x336.png" width="547" height="336" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/b27ba284-88f4-4b77-97bb-463ee0a569b8_547x336.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:336,&quot;width&quot;:547,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:62841,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!4lh6!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb27ba284-88f4-4b77-97bb-463ee0a569b8_547x336.png 424w, https://substackcdn.com/image/fetch/$s_!4lh6!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb27ba284-88f4-4b77-97bb-463ee0a569b8_547x336.png 848w, https://substackcdn.com/image/fetch/$s_!4lh6!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb27ba284-88f4-4b77-97bb-463ee0a569b8_547x336.png 1272w, https://substackcdn.com/image/fetch/$s_!4lh6!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb27ba284-88f4-4b77-97bb-463ee0a569b8_547x336.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div></li><li><p>If multiple transactions have the same price, the one that is seen earlier</p><p>is prioritized to avoid network spam attacks aiming for a specific ordering (timestamp is captured <a href="https://github.com/ethereum/go-ethereum/blob/v1.10.1/core/types/transaction.go#L50">here</a> in the code).</p></li></ol></li><li><p>We return the transaction set to be used in the &#8220;commitTransactions&#8221; function to commit the transactions to the block.</p></li><li><p>The exact same process happens with the remote transactions, they are sorted by gas price and nonce and committed to the block. The number of committed transactions is limited by the block gas limit. Remote transactions can be dropped by on the nodes when the number of Txs in the TxPool exceeds the nodes limit.</p></li></ol><p>This takes us back to the times of &#8220;Priority Gas Auctions&#8221; (PGA&#8217;s) where searchers &amp; bots would increase the gas price of their transactions to get to the top of the block. </p><p>Bots would continually bid against each other in the ~13.5 second block time to be first in the block and as a result first to an on-chain opportunity (ie execute a liquidation). </p><p>When you lost an auction you still had to pay the gas price for your highest bid since your transaction would still be executed but it would revert. Bots could lose a lot of money from this.</p><p>When bidding in PGA&#8217;s the bots &amp; searchers had to follow these rules to increase the gas price for a given transaction. </p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ZfbH!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa2a436fa-e9fc-45ba-8da9-3f55d16ddebf_1466x242.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ZfbH!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa2a436fa-e9fc-45ba-8da9-3f55d16ddebf_1466x242.png 424w, https://substackcdn.com/image/fetch/$s_!ZfbH!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa2a436fa-e9fc-45ba-8da9-3f55d16ddebf_1466x242.png 848w, https://substackcdn.com/image/fetch/$s_!ZfbH!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa2a436fa-e9fc-45ba-8da9-3f55d16ddebf_1466x242.png 1272w, https://substackcdn.com/image/fetch/$s_!ZfbH!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa2a436fa-e9fc-45ba-8da9-3f55d16ddebf_1466x242.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ZfbH!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa2a436fa-e9fc-45ba-8da9-3f55d16ddebf_1466x242.png" width="1456" height="240" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/a2a436fa-e9fc-45ba-8da9-3f55d16ddebf_1466x242.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:240,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:40841,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ZfbH!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa2a436fa-e9fc-45ba-8da9-3f55d16ddebf_1466x242.png 424w, https://substackcdn.com/image/fetch/$s_!ZfbH!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa2a436fa-e9fc-45ba-8da9-3f55d16ddebf_1466x242.png 848w, https://substackcdn.com/image/fetch/$s_!ZfbH!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa2a436fa-e9fc-45ba-8da9-3f55d16ddebf_1466x242.png 1272w, https://substackcdn.com/image/fetch/$s_!ZfbH!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa2a436fa-e9fc-45ba-8da9-3f55d16ddebf_1466x242.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption">Source: https://www.blocknative.com/blog/speed-up-transactions</figcaption></figure></div><p>Now we&#8217;ve covered vanilla Geth let&#8217;s take a look at what changed with the first official release of MEV Geth.</p><h2>MEV-Geth v0.1 - 31 Mar 2021</h2><p>For those who don&#8217;t know MEV Geth was a new custom client that enabled miners to receive MEV bundles.</p><p>&#8220;MEV Bundles&#8221; represented bundles of transactions that extracted value and needed to be executed atomically. All transactions within the bundle should be executed or none of them.</p><p>Flashbots provided a relay connecting Searchers with Miners. Searchers would submit the bundles to the Flashbots Relay and the relay would forward them to Miners. The system relied on a level of trust between the parties to function. </p><p>MEV Geth and the Flashbots Relay looked to take PGA&#8217;s off the chain where they were impacting everyday users and move it to the relay.</p><p>The new auction rules were determined and made available to the Searchers. Losing bids wouldn&#8217;t be executed, saving bots their money and Ethereum its block space. </p><p>Bundles also introduced the ability to pay a miner via a <a href="https://docs.flashbots.net/Flashbots-auction/searchers/advanced/coinbase-payment/">coinbase.transfer( )</a> a direct payment to the miner rather than via gas fees.</p><p>For v0.1 only one bundle could be selected for inclusion per block.</p><p>Let&#8217;s take a look at the client alterations that were made to enable these MEV Bundles. </p><h3>Transaction Ordering</h3><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!eloH!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F499b017c-a936-4a42-bc92-c4df064f4136_4242x2172.png" data-component-name="Image2ToDOM"><div class="image2-inset image2-full-screen"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!eloH!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F499b017c-a936-4a42-bc92-c4df064f4136_4242x2172.png 424w, https://substackcdn.com/image/fetch/$s_!eloH!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F499b017c-a936-4a42-bc92-c4df064f4136_4242x2172.png 848w, https://substackcdn.com/image/fetch/$s_!eloH!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F499b017c-a936-4a42-bc92-c4df064f4136_4242x2172.png 1272w, https://substackcdn.com/image/fetch/$s_!eloH!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F499b017c-a936-4a42-bc92-c4df064f4136_4242x2172.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!eloH!,w_5760,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F499b017c-a936-4a42-bc92-c4df064f4136_4242x2172.png" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/499b017c-a936-4a42-bc92-c4df064f4136_4242x2172.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;full&quot;,&quot;height&quot;:746,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1330790,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-fullscreen" alt="" srcset="https://substackcdn.com/image/fetch/$s_!eloH!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F499b017c-a936-4a42-bc92-c4df064f4136_4242x2172.png 424w, https://substackcdn.com/image/fetch/$s_!eloH!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F499b017c-a936-4a42-bc92-c4df064f4136_4242x2172.png 848w, https://substackcdn.com/image/fetch/$s_!eloH!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F499b017c-a936-4a42-bc92-c4df064f4136_4242x2172.png 1272w, https://substackcdn.com/image/fetch/$s_!eloH!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F499b017c-a936-4a42-bc92-c4df064f4136_4242x2172.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><ol><li><p>TxPool Geth&#8217;s implementation of the Mempool which we saw earlier.</p></li><li><p>A new field is added to the TxPool called &#8220;mevBundles&#8221; which is an array of type mevBundle. MevBundle is composed of 4 elements</p><ol><li><p>&#8220;txs&#8221; - A list of transactions in the bundle. Each transaction is signed and RLP-encoded.</p></li><li><p>&#8220;blockNumber&#8221; - The exact block number at which the bundle can be executed</p></li><li><p>&#8220;minTimestamp&#8221; -  Minimum block timestamp (inclusive) at which the bundle can be executed</p></li><li><p>&#8220;maxTimestamp&#8221; - Maximum block timestamp (inclusive) at which the bundle can be executed</p></li></ol></li><li><p>&#8220;CommitNewWork&#8221; is the same function we looked at above which handles filling a block with all pending transactions.</p></li><li><p>The &#8220;Local Transactions&#8221; &amp; &#8220;Remote Transactions&#8221; sections for ordering each group of Txs and committing them to the block. You&#8217;ll notice both are beneath the &#8220;Flashbots Bundles&#8221; section meaning the bundle&#8217;s transactions will have priority and be placed at the top of the block.</p></li><li><p>This section handles the &#8220;mevBundles&#8221; received from Flashbots. Line 1148 grabs all the bundles from the TxPool. The &#8220;findMostProfitableBundle&#8221; function (which we will look at in the next section) is used to determine the most profitable bundle. Bundle info is logged on the node and then the bundle is committed to the block via &#8220;commitBundle&#8221; on Line 1155.</p></li></ol><p>So how did Flashbots determine the most profitable bundle?</p><h3>Most Profitable Bundle </h3><p>Earlier we said, &#8220;The new auction rules were determined and made available to the Searchers&#8221;.</p><p>Below are the new auction rules, it looks complicated but I can assure you it isn&#8217;t.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!hdcy!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F89c68fee-78f2-4767-8f02-2c3e117ac36a_1254x608.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!hdcy!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F89c68fee-78f2-4767-8f02-2c3e117ac36a_1254x608.png 424w, https://substackcdn.com/image/fetch/$s_!hdcy!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F89c68fee-78f2-4767-8f02-2c3e117ac36a_1254x608.png 848w, https://substackcdn.com/image/fetch/$s_!hdcy!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F89c68fee-78f2-4767-8f02-2c3e117ac36a_1254x608.png 1272w, https://substackcdn.com/image/fetch/$s_!hdcy!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F89c68fee-78f2-4767-8f02-2c3e117ac36a_1254x608.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!hdcy!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F89c68fee-78f2-4767-8f02-2c3e117ac36a_1254x608.png" width="1254" height="608" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/89c68fee-78f2-4767-8f02-2c3e117ac36a_1254x608.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:608,&quot;width&quot;:1254,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!hdcy!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F89c68fee-78f2-4767-8f02-2c3e117ac36a_1254x608.png 424w, https://substackcdn.com/image/fetch/$s_!hdcy!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F89c68fee-78f2-4767-8f02-2c3e117ac36a_1254x608.png 848w, https://substackcdn.com/image/fetch/$s_!hdcy!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F89c68fee-78f2-4767-8f02-2c3e117ac36a_1254x608.png 1272w, https://substackcdn.com/image/fetch/$s_!hdcy!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F89c68fee-78f2-4767-8f02-2c3e117ac36a_1254x608.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Source: https://docs.flashbots.net/flashbots-auction/miners/mev-geth-spec/v01</figcaption></figure></div><p>Let&#8217;s run through the diagram.</p><ul><li><p>&#8220;s&#8221; is the &#8220;Adjusted Gas Price&#8221; which is what we what to maximise to win the auction.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!A9zT!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Facda77b5-163b-4b83-90a9-abcd0a427aa6_49x30.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!A9zT!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Facda77b5-163b-4b83-90a9-abcd0a427aa6_49x30.png 424w, https://substackcdn.com/image/fetch/$s_!A9zT!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Facda77b5-163b-4b83-90a9-abcd0a427aa6_49x30.png 848w, https://substackcdn.com/image/fetch/$s_!A9zT!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Facda77b5-163b-4b83-90a9-abcd0a427aa6_49x30.png 1272w, https://substackcdn.com/image/fetch/$s_!A9zT!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Facda77b5-163b-4b83-90a9-abcd0a427aa6_49x30.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!A9zT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Facda77b5-163b-4b83-90a9-abcd0a427aa6_49x30.png" width="49" height="30" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/acda77b5-163b-4b83-90a9-abcd0a427aa6_49x30.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:30,&quot;width&quot;:49,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:2222,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!A9zT!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Facda77b5-163b-4b83-90a9-abcd0a427aa6_49x30.png 424w, https://substackcdn.com/image/fetch/$s_!A9zT!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Facda77b5-163b-4b83-90a9-abcd0a427aa6_49x30.png 848w, https://substackcdn.com/image/fetch/$s_!A9zT!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Facda77b5-163b-4b83-90a9-abcd0a427aa6_49x30.png 1272w, https://substackcdn.com/image/fetch/$s_!A9zT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Facda77b5-163b-4b83-90a9-abcd0a427aa6_49x30.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div></li><li><p>Delta coinbase is the total ETH paid to the miner from the transactions in the bundle via direct payments ie coinbase.transfer( ).</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!vLgk!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd86ef52e-f3bc-4306-82d9-a9ca7bdd7c89_87x30.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!vLgk!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd86ef52e-f3bc-4306-82d9-a9ca7bdd7c89_87x30.png 424w, https://substackcdn.com/image/fetch/$s_!vLgk!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd86ef52e-f3bc-4306-82d9-a9ca7bdd7c89_87x30.png 848w, https://substackcdn.com/image/fetch/$s_!vLgk!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd86ef52e-f3bc-4306-82d9-a9ca7bdd7c89_87x30.png 1272w, https://substackcdn.com/image/fetch/$s_!vLgk!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd86ef52e-f3bc-4306-82d9-a9ca7bdd7c89_87x30.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!vLgk!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd86ef52e-f3bc-4306-82d9-a9ca7bdd7c89_87x30.png" width="87" height="30" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/d86ef52e-f3bc-4306-82d9-a9ca7bdd7c89_87x30.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:30,&quot;width&quot;:87,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:3512,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!vLgk!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd86ef52e-f3bc-4306-82d9-a9ca7bdd7c89_87x30.png 424w, https://substackcdn.com/image/fetch/$s_!vLgk!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd86ef52e-f3bc-4306-82d9-a9ca7bdd7c89_87x30.png 848w, https://substackcdn.com/image/fetch/$s_!vLgk!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd86ef52e-f3bc-4306-82d9-a9ca7bdd7c89_87x30.png 1272w, https://substackcdn.com/image/fetch/$s_!vLgk!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd86ef52e-f3bc-4306-82d9-a9ca7bdd7c89_87x30.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div></li><li><p>The sum of all the bundle&#8217;s transaction gas payments, (gas price * gas used) per transaction, represents the total gas payment for the bundle.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Ipoc!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F7f3e479e-960b-4485-8d27-87953691d1c4_114x32.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Ipoc!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F7f3e479e-960b-4485-8d27-87953691d1c4_114x32.png 424w, https://substackcdn.com/image/fetch/$s_!Ipoc!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F7f3e479e-960b-4485-8d27-87953691d1c4_114x32.png 848w, https://substackcdn.com/image/fetch/$s_!Ipoc!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F7f3e479e-960b-4485-8d27-87953691d1c4_114x32.png 1272w, https://substackcdn.com/image/fetch/$s_!Ipoc!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F7f3e479e-960b-4485-8d27-87953691d1c4_114x32.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Ipoc!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F7f3e479e-960b-4485-8d27-87953691d1c4_114x32.png" width="114" height="32" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/7f3e479e-960b-4485-8d27-87953691d1c4_114x32.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:32,&quot;width&quot;:114,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:4810,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Ipoc!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F7f3e479e-960b-4485-8d27-87953691d1c4_114x32.png 424w, https://substackcdn.com/image/fetch/$s_!Ipoc!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F7f3e479e-960b-4485-8d27-87953691d1c4_114x32.png 848w, https://substackcdn.com/image/fetch/$s_!Ipoc!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F7f3e479e-960b-4485-8d27-87953691d1c4_114x32.png 1272w, https://substackcdn.com/image/fetch/$s_!Ipoc!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F7f3e479e-960b-4485-8d27-87953691d1c4_114x32.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div></li><li><p>The sum of all gas used in the bundle.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!TaKu!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fef19a459-d876-4512-9bbe-b9e1c83bc20a_96x33.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!TaKu!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fef19a459-d876-4512-9bbe-b9e1c83bc20a_96x33.png 424w, https://substackcdn.com/image/fetch/$s_!TaKu!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fef19a459-d876-4512-9bbe-b9e1c83bc20a_96x33.png 848w, https://substackcdn.com/image/fetch/$s_!TaKu!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fef19a459-d876-4512-9bbe-b9e1c83bc20a_96x33.png 1272w, https://substackcdn.com/image/fetch/$s_!TaKu!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fef19a459-d876-4512-9bbe-b9e1c83bc20a_96x33.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!TaKu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fef19a459-d876-4512-9bbe-b9e1c83bc20a_96x33.png" width="96" height="33" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/ef19a459-d876-4512-9bbe-b9e1c83bc20a_96x33.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:33,&quot;width&quot;:96,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:3853,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!TaKu!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fef19a459-d876-4512-9bbe-b9e1c83bc20a_96x33.png 424w, https://substackcdn.com/image/fetch/$s_!TaKu!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fef19a459-d876-4512-9bbe-b9e1c83bc20a_96x33.png 848w, https://substackcdn.com/image/fetch/$s_!TaKu!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fef19a459-d876-4512-9bbe-b9e1c83bc20a_96x33.png 1272w, https://substackcdn.com/image/fetch/$s_!TaKu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fef19a459-d876-4512-9bbe-b9e1c83bc20a_96x33.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div></li><li><p>Dividing these 2 values gives you a ETH price per unit of gas for the bundle (Adjusted Gas Price)</p></li><li><p>To maximise this value we need to either </p><ul><li><p>Increase the numerator by paying the miner more money through coinbase.transfer( ) or Gas. We do this by reducing our profit margin or finding more lucrative oppourtunities</p></li><li><p>Decrease the denominator by reducing the gas used in the bundle, we do this by gas optimising our contracts</p></li></ul></li></ul><p>Ok, let&#8217;s have a look at how this calculation is implemented in MEV Geth. We start where we left off in the previous example with the &#8220;findMostProfitableBundle&#8221; function.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!w_jI!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fef933560-cc8f-4df5-a403-2f9c34fdfa16_4316x2512.png" data-component-name="Image2ToDOM"><div class="image2-inset image2-full-screen"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!w_jI!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fef933560-cc8f-4df5-a403-2f9c34fdfa16_4316x2512.png 424w, https://substackcdn.com/image/fetch/$s_!w_jI!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fef933560-cc8f-4df5-a403-2f9c34fdfa16_4316x2512.png 848w, https://substackcdn.com/image/fetch/$s_!w_jI!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fef933560-cc8f-4df5-a403-2f9c34fdfa16_4316x2512.png 1272w, https://substackcdn.com/image/fetch/$s_!w_jI!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fef933560-cc8f-4df5-a403-2f9c34fdfa16_4316x2512.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!w_jI!,w_5760,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fef933560-cc8f-4df5-a403-2f9c34fdfa16_4316x2512.png" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/ef933560-cc8f-4df5-a403-2f9c34fdfa16_4316x2512.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;full&quot;,&quot;height&quot;:847,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:646088,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-fullscreen" alt="" srcset="https://substackcdn.com/image/fetch/$s_!w_jI!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fef933560-cc8f-4df5-a403-2f9c34fdfa16_4316x2512.png 424w, https://substackcdn.com/image/fetch/$s_!w_jI!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fef933560-cc8f-4df5-a403-2f9c34fdfa16_4316x2512.png 848w, https://substackcdn.com/image/fetch/$s_!w_jI!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fef933560-cc8f-4df5-a403-2f9c34fdfa16_4316x2512.png 1272w, https://substackcdn.com/image/fetch/$s_!w_jI!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fef933560-cc8f-4df5-a403-2f9c34fdfa16_4316x2512.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><ol><li><p>The &#8220;findMostProfitableBundle&#8221; function loops over bundles to determine the &#8220;maxBundle&#8221; (most profitable) and returns this bundle along with some other data that is logged on the Geth node.</p></li><li><p>Each bundle is run through the &#8220;computeBundleGas&#8221; function. This function returns &#8220;totalEth&#8221; which represents the &#8220;coinbaseDiff&#8221; (How much has the miner&#8217;s balance increased by) and &#8220;totalGasUsed&#8221; which is the gas consumed by the transactions within the bundle.</p></li><li><p>We calculate the gas used by looping over the transactions in the bundle and running &#8220;ApplyTransaction&#8221; this allows us to simulate the transaction and get the transaction receipt which contains &#8220;GasUsed&#8221;. By summing the &#8220;GasUsed&#8221; for each transaction we can get the &#8220;totalGasUsed&#8221; of the whole bundle. The updated state is used for the next transaction in the loop so that it is representative of what happens in a real block.</p><ol><li><p>Remember &#8220;totalGasUsed&#8221; represents the denominator (bottom) of our &#8220;Adjusted Gas Price&#8221; calculation which determines the winner of the Flashbots auction.</p></li></ol></li><li><p>On line 1245 we declared a variable &#8220;coinbaseBalanceBefore&#8221; which grabs the balance of the coinbase (miner) address before any transactions are simulated. Line 1254 declares &#8220;coinbaseBalanceAfter&#8221; which grabs the coinbase balance after all the transactions have been applied to the state. Line 1255 sets &#8220;coinbaseDiff&#8221; which subtracts &#8220;coinbaseBalanceBefore&#8221; from &#8220;coinbaseBalanceAfter&#8221; to yield the miner profit. The value is used to set totalEth which is returned.</p><ol><li><p>This value takes into account both coinbase.transfer( ) payments and payments in the form of gas which occurred during the &#8220;ApplyTransaction&#8221; function. This means &#8220;coinbaseDiff&#8221; represents the entire numerator (top)  of our &#8220;Adjusted Gas Price&#8221; calculation.</p></li></ol></li><li><p>The &#8220;Adjusted GasPrice&#8221; given we now have the numerator and denominator we just need to run the calculation. On line 1221 a variable &#8220;mevGasPrice&#8221; is declared, we divide &#8220;totalEth&#8221; by &#8220;totalGasUsed&#8221; which yields our &#8220;Adjusted GasPrice&#8221;.</p></li><li><p>Once the &#8220;Adjusted GasPrice&#8221; is calculated each bundle is compared to the current max value (Lines 1222 - 1227) this ensures the bundle with the highest &#8220;Adjusted GasPrice&#8221; (&#8220;mevGasPrice&#8221;) is returned.</p></li></ol><p>This was the first version of MEV Geth but there was much more to come, over the next few months v0.2 was released. Let&#8217;s see what improvements were made. </p><h2>MEV-Geth v0.2 - 12 Apr 2021 &#8594;16 Jun 2021</h2><p>A number of improvements were made in v0.2 but we&#8217;ll focus on the main 3 changes. A change to the &#8220;Adjusted Gas Price&#8221; calculation, bundle merging and a tweak to the structure of a MevBundle within the client.</p><h3>Adjusted Gas Price</h3><p>The change to the calculation was a small one. It involved removing the gas payments from Txs that were in a bundle but were already in the TxPool. These were represented by</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!eD20!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F451e7c9b-d9cd-4786-b59e-681226d09995_123x26.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!eD20!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F451e7c9b-d9cd-4786-b59e-681226d09995_123x26.png 424w, https://substackcdn.com/image/fetch/$s_!eD20!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F451e7c9b-d9cd-4786-b59e-681226d09995_123x26.png 848w, https://substackcdn.com/image/fetch/$s_!eD20!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F451e7c9b-d9cd-4786-b59e-681226d09995_123x26.png 1272w, https://substackcdn.com/image/fetch/$s_!eD20!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F451e7c9b-d9cd-4786-b59e-681226d09995_123x26.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!eD20!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F451e7c9b-d9cd-4786-b59e-681226d09995_123x26.png" width="123" height="26" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/451e7c9b-d9cd-4786-b59e-681226d09995_123x26.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:26,&quot;width&quot;:123,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:5136,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!eD20!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F451e7c9b-d9cd-4786-b59e-681226d09995_123x26.png 424w, https://substackcdn.com/image/fetch/$s_!eD20!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F451e7c9b-d9cd-4786-b59e-681226d09995_123x26.png 848w, https://substackcdn.com/image/fetch/$s_!eD20!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F451e7c9b-d9cd-4786-b59e-681226d09995_123x26.png 1272w, https://substackcdn.com/image/fetch/$s_!eD20!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F451e7c9b-d9cd-4786-b59e-681226d09995_123x26.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>The purpose of this was to prevent &#8220;bundle stuffing&#8221; where searchers were stuffing their bundles with high gwei Txs from TxPool to inflate their &#8220;Adjusted Gas Price&#8221; score. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!N7HA!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7996a76-2829-42a6-84ee-dd539de01647_2714x2668.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!N7HA!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7996a76-2829-42a6-84ee-dd539de01647_2714x2668.png 424w, https://substackcdn.com/image/fetch/$s_!N7HA!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7996a76-2829-42a6-84ee-dd539de01647_2714x2668.png 848w, https://substackcdn.com/image/fetch/$s_!N7HA!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7996a76-2829-42a6-84ee-dd539de01647_2714x2668.png 1272w, https://substackcdn.com/image/fetch/$s_!N7HA!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7996a76-2829-42a6-84ee-dd539de01647_2714x2668.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!N7HA!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7996a76-2829-42a6-84ee-dd539de01647_2714x2668.png" width="1200" height="1179.3956043956043" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/a7996a76-2829-42a6-84ee-dd539de01647_2714x2668.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:1431,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:1111461,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!N7HA!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7996a76-2829-42a6-84ee-dd539de01647_2714x2668.png 424w, https://substackcdn.com/image/fetch/$s_!N7HA!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7996a76-2829-42a6-84ee-dd539de01647_2714x2668.png 848w, https://substackcdn.com/image/fetch/$s_!N7HA!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7996a76-2829-42a6-84ee-dd539de01647_2714x2668.png 1272w, https://substackcdn.com/image/fetch/$s_!N7HA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7996a76-2829-42a6-84ee-dd539de01647_2714x2668.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><ol><li><p>We revisit &#8220;computeBundleGas&#8221; to see what has changed with the calculation.</p></li><li><p>As before we loop through  the Txs in the bundle running &#8220;ApplyTransaction&#8221; to simulate the Tx and see the change in the state</p></li><li><p>First we set &#8220;txInPendingPool&#8221; to false </p><ol><li><p>Then we get all transactions pending in the TxPool from the address of the bundle Tx</p></li><li><p>Get the nonce of the bundle Tx</p></li><li><p>For each transaction in the TxPool associated with that address check if the nonces match</p></li><li><p>If they do set txInPendingPool to true and break out of the loop</p></li></ol></li><li><p>If the transaction is not in the pending pool add the associated gasFees from that Tx to the gas fees for the entire bundle. (This in effect acts as the minus gas fees from Txs that were present in the TxPool) </p></li><li><p>Comparing the adjusted gas price calculation to the code we can now that  </p><ol><li><p>&#8220;ethSentToCoinbase&#8221; maps to &#8220;delta coinbase&#8221; - payments made to the coinbase address</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!vLgk!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd86ef52e-f3bc-4306-82d9-a9ca7bdd7c89_87x30.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!vLgk!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd86ef52e-f3bc-4306-82d9-a9ca7bdd7c89_87x30.png 424w, https://substackcdn.com/image/fetch/$s_!vLgk!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd86ef52e-f3bc-4306-82d9-a9ca7bdd7c89_87x30.png 848w, https://substackcdn.com/image/fetch/$s_!vLgk!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd86ef52e-f3bc-4306-82d9-a9ca7bdd7c89_87x30.png 1272w, https://substackcdn.com/image/fetch/$s_!vLgk!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd86ef52e-f3bc-4306-82d9-a9ca7bdd7c89_87x30.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!vLgk!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd86ef52e-f3bc-4306-82d9-a9ca7bdd7c89_87x30.png" width="87" height="30" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/d86ef52e-f3bc-4306-82d9-a9ca7bdd7c89_87x30.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:30,&quot;width&quot;:87,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!vLgk!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd86ef52e-f3bc-4306-82d9-a9ca7bdd7c89_87x30.png 424w, https://substackcdn.com/image/fetch/$s_!vLgk!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd86ef52e-f3bc-4306-82d9-a9ca7bdd7c89_87x30.png 848w, https://substackcdn.com/image/fetch/$s_!vLgk!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd86ef52e-f3bc-4306-82d9-a9ca7bdd7c89_87x30.png 1272w, https://substackcdn.com/image/fetch/$s_!vLgk!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd86ef52e-f3bc-4306-82d9-a9ca7bdd7c89_87x30.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div></li><li><p>&#8220;gasFees&#8221; maps the &#8220;total gas fees for the bundle&#8221; - this represents the gas fees for all Txs in the bundle minus the gas fees from Txs that were already in the mempool </p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!8D1q!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F87d6badd-f06f-4b43-8999-a71d18a984dd_243x28.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!8D1q!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F87d6badd-f06f-4b43-8999-a71d18a984dd_243x28.png 424w, https://substackcdn.com/image/fetch/$s_!8D1q!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F87d6badd-f06f-4b43-8999-a71d18a984dd_243x28.png 848w, https://substackcdn.com/image/fetch/$s_!8D1q!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F87d6badd-f06f-4b43-8999-a71d18a984dd_243x28.png 1272w, https://substackcdn.com/image/fetch/$s_!8D1q!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F87d6badd-f06f-4b43-8999-a71d18a984dd_243x28.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!8D1q!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F87d6badd-f06f-4b43-8999-a71d18a984dd_243x28.png" width="243" height="28" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/87d6badd-f06f-4b43-8999-a71d18a984dd_243x28.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:28,&quot;width&quot;:243,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:8378,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!8D1q!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F87d6badd-f06f-4b43-8999-a71d18a984dd_243x28.png 424w, https://substackcdn.com/image/fetch/$s_!8D1q!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F87d6badd-f06f-4b43-8999-a71d18a984dd_243x28.png 848w, https://substackcdn.com/image/fetch/$s_!8D1q!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F87d6badd-f06f-4b43-8999-a71d18a984dd_243x28.png 1272w, https://substackcdn.com/image/fetch/$s_!8D1q!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F87d6badd-f06f-4b43-8999-a71d18a984dd_243x28.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div></li></ol></li></ol><h3>Bundle Merging</h3><p>In v0.1 we could only include 1 bundle per block, with bundle merging in v0.2 we could merge 3 bundles to maximise profit. Let&#8217;s see how.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!UB-u!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb1986be-970d-4935-8d8c-e08951ab3cd7_4102x2966.png" data-component-name="Image2ToDOM"><div class="image2-inset image2-full-screen"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!UB-u!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb1986be-970d-4935-8d8c-e08951ab3cd7_4102x2966.png 424w, https://substackcdn.com/image/fetch/$s_!UB-u!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb1986be-970d-4935-8d8c-e08951ab3cd7_4102x2966.png 848w, https://substackcdn.com/image/fetch/$s_!UB-u!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb1986be-970d-4935-8d8c-e08951ab3cd7_4102x2966.png 1272w, https://substackcdn.com/image/fetch/$s_!UB-u!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb1986be-970d-4935-8d8c-e08951ab3cd7_4102x2966.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!UB-u!,w_5760,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb1986be-970d-4935-8d8c-e08951ab3cd7_4102x2966.png" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/db1986be-970d-4935-8d8c-e08951ab3cd7_4102x2966.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;full&quot;,&quot;height&quot;:1053,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1483328,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-fullscreen" alt="" srcset="https://substackcdn.com/image/fetch/$s_!UB-u!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb1986be-970d-4935-8d8c-e08951ab3cd7_4102x2966.png 424w, https://substackcdn.com/image/fetch/$s_!UB-u!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb1986be-970d-4935-8d8c-e08951ab3cd7_4102x2966.png 848w, https://substackcdn.com/image/fetch/$s_!UB-u!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb1986be-970d-4935-8d8c-e08951ab3cd7_4102x2966.png 1272w, https://substackcdn.com/image/fetch/$s_!UB-u!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb1986be-970d-4935-8d8c-e08951ab3cd7_4102x2966.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><ol><li><p>&#8220;commitNewWork&#8221; which handles filling a block with transactions</p></li><li><p>As before we have our Flashbots bundle section, localTxs section and remoteTxs section. The &#8220;Flashbots Bundle&#8221; section has changed from v0.1, we have a new function called generateFlashbotsBundle.</p></li><li><p>&#8220;generateFlashbotsBundle&#8221; is taking in an array of bundles, simulating them, sorting them and then merging them. Let&#8217;s take a closer look.</p></li><li><p>We have a new struct called &#8220;simulatedBundle&#8221;, which contains the original bundle plus the key data required for our &#8220;Adjusted Gas Price&#8221; calculation, totalGasUsed, totalEth etc. The simulateBundles function on line 1217 takes a bundle and simulates it against the head of the chain to verify it doesn&#8217;t revert and computes the bundle gas.</p></li><li><p>The simulated bundles are sorted based on &#8220;mevGasPrice&#8221; (Adjusted Gas Price)</p></li><li><p>These sorted bundles are passed to the &#8220;mergeBundles&#8221; function. At the start of this function, a variable &#8220;finalBundle&#8221; is declared which represents a set of transactions. The &#8220;mergeBundles&#8221; function is designed to enable more than one bundle to be included in a block.</p></li><li><p>This section occurs inside of a bundle loop, each bundle is again simulated via &#8220;computeBundleGas&#8221; but instead of all being simulated against the chain head  state they are instead simulated against the chain state that the previous bundle has left it in. If &#8220;computeBundleGas&#8221; returns an error or the &#8220;mevGasPrice&#8221; is below the floorGasPrice (this is a new hardcoded lower limit they <a href="https://github.com/flashbots/mev-geth/commit/a172ca123a3c2534573779008fbf8b3c853b4e57">introduced in v0.2</a> ) then the state/gasPool will be reset to what it was in the previous loop. The &#8220;continue&#8221; will put the program back to the start of the loop with the next item meaning the bundle won&#8217;t be included.</p></li><li><p>If the bundle passes the &#8220;computeBundleGas&#8221; check its Txs are added to the &#8220;finalBundle&#8221; and some key/values are updated ie totalGasUsed, totalEth etc. Finally, a counter is incremented, a default maximum of 3 bundles can be merged together. This &#8220;maxMergeBundles&#8221; value balances profit with node performance and can be updated in config.  </p></li><li><p>This section checks the counter and breaks out of the for loop if the counter has reached the &#8220;maxMergeBundles&#8221;</p></li><li><p>A number of variables are returned including &#8220;finalBundle&#8221;, which is returned to generateFlashbotsBundle, inside the &#8220;commitNewWork&#8221; function on line 1146 the &#8220;bundleTxs&#8221; is set which corresponds to &#8220;finalBundle&#8221;. This value is then committed to the block on line 1155.    </p></li></ol><h3>Reverting Tx Hashes</h3><p>One other minor change was to the structure of the MevBundle as defined in the client code. An additional field, RevertingTxHashes, was added which represents a list of Tx hashes that are allowed to return status 0 (revert) on transaction receipts. </p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!IgAQ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1c70e82b-d54f-446b-b2d3-67156bd04dc2_348x141.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!IgAQ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1c70e82b-d54f-446b-b2d3-67156bd04dc2_348x141.png 424w, https://substackcdn.com/image/fetch/$s_!IgAQ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1c70e82b-d54f-446b-b2d3-67156bd04dc2_348x141.png 848w, https://substackcdn.com/image/fetch/$s_!IgAQ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1c70e82b-d54f-446b-b2d3-67156bd04dc2_348x141.png 1272w, https://substackcdn.com/image/fetch/$s_!IgAQ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1c70e82b-d54f-446b-b2d3-67156bd04dc2_348x141.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!IgAQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1c70e82b-d54f-446b-b2d3-67156bd04dc2_348x141.png" width="348" height="141" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/1c70e82b-d54f-446b-b2d3-67156bd04dc2_348x141.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:141,&quot;width&quot;:348,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!IgAQ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1c70e82b-d54f-446b-b2d3-67156bd04dc2_348x141.png 424w, https://substackcdn.com/image/fetch/$s_!IgAQ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1c70e82b-d54f-446b-b2d3-67156bd04dc2_348x141.png 848w, https://substackcdn.com/image/fetch/$s_!IgAQ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1c70e82b-d54f-446b-b2d3-67156bd04dc2_348x141.png 1272w, https://substackcdn.com/image/fetch/$s_!IgAQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1c70e82b-d54f-446b-b2d3-67156bd04dc2_348x141.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>The idea of allowing reverting transactions seems strange. As a searcher you should have simulated the bundle and ensured it can execute at the top of the block so why do we need it.</p><p>This was a valid assumption when we had a single bundle being submitted in v0.1 but we now have bundle merging enabled on the node.</p><p>Suddenly you can&#8217;t be sure of the chain state when your bundle is included. You may want to flag some transactions ie liquidations as allowed to revert in case another bundle was included before you.</p><p>If your bundle can be profitable with a reverting transaction it makes sense to include it in the list. The alternative is the bundle is discarded by the node so the searcher has nothing to lose.</p><p>Strategies that required more than one transaction to execute such as sandwiching likely wouldn&#8217;t be allowed to revert since you may end up in a situation where one leg of the trade reverts but the other doesn&#8217;t which could leave you exposed.</p><p>Next, we&#8217;re going to jump straight to version v0.4 where Megabundles were introduced. For those interested, v0.3 involved making MEV Geth compatible with EIP-1559 you can read more about it <a href="https://docs.flashbots.net/flashbots-auction/miners/mev-geth-spec/v03">here</a>. </p><h2>MEV-Geth v0.4 -  27 Sep 2021 &#8594;10 Jan 2022 </h2><p>If you look at the &#8220;mergeBundles&#8221; implementation in v0.2 you can see that it is quite naive and limiting in how it goes about merging the bundles.</p><p>The reason for this is that you don&#8217;t want to implement a change in the software (MEV Geth) that completely changes the characteristics of the required hardware (CPU MEM). </p><p>Ideally, you want bundle merging to be conducted over the maximum amount of bundles with optimised hardware and algorithms. It doesn&#8217;t make sense to implement this on every node. </p><p>Megabundles enabled this, they are the precursor to the &#8220;full block&#8221; bundles we&#8217;ll see in MEV Boost.</p><blockquote><p>One interesting change that Megabundles introduces was the ability to rank bundles via profitability rather than gas price.<br><br>Imagine 2 bundles one that nets you a profit of 1 ETH with an &#8220;Adjusted Gas Price&#8221; of 100 and another that nets you 10 ETH with an &#8220;Adjusted Gas Price&#8221; of 80. These numbers are exaggerated but as a miner you want the 10 ETH bundle. You don&#8217;t know when you&#8217;ll next get a block so you want to maximise profit when it is your turn. </p></blockquote><p><br>See this Bert Miller Tweet for some more context.</p><div class="twitter-embed" data-attrs="{&quot;url&quot;:&quot;https://twitter.com/bertcmiller/status/1442548307595706369?s=20&amp;t=yW-s4UX94pUWnAwkDEa_ug&quot;,&quot;full_text&quot;:&quot;Today Flashbots is releasing v0.4 and introducing mega bundles to the Flashbots Network\n\nMega bundles will let us innovate faster, support more bundles and will lead to new searcher strategies and more miner revenue\n\nA thread on mega bundles &#129525;&#128071;&#127995;&quot;,&quot;username&quot;:&quot;bertcmiller&quot;,&quot;name&quot;:&quot;@bertcmiller &#9889;&#65039;&#129302; is hiring (jobs.flashbots.net)&quot;,&quot;profile_image_url&quot;:&quot;&quot;,&quot;date&quot;:&quot;Mon Sep 27 17:54:52 +0000 2021&quot;,&quot;photos&quot;:[],&quot;quoted_tweet&quot;:{},&quot;reply_count&quot;:0,&quot;retweet_count&quot;:68,&quot;like_count&quot;:329,&quot;impression_count&quot;:0,&quot;expanded_url&quot;:{},&quot;video_url&quot;:null,&quot;belowTheFold&quot;:true}" data-component-name="Twitter2ToDOM"></div><p><br>The value proposition was simple we&#8217;ll do the computation and find you a bundle that maximises profit.</p><p>Let&#8217;s see how it worked.</p><h3>Megabundles</h3><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!1N5t!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F77f44130-4a47-4895-8a79-147a55a5ce84_2676x2574.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!1N5t!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F77f44130-4a47-4895-8a79-147a55a5ce84_2676x2574.png 424w, https://substackcdn.com/image/fetch/$s_!1N5t!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F77f44130-4a47-4895-8a79-147a55a5ce84_2676x2574.png 848w, https://substackcdn.com/image/fetch/$s_!1N5t!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F77f44130-4a47-4895-8a79-147a55a5ce84_2676x2574.png 1272w, https://substackcdn.com/image/fetch/$s_!1N5t!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F77f44130-4a47-4895-8a79-147a55a5ce84_2676x2574.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!1N5t!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F77f44130-4a47-4895-8a79-147a55a5ce84_2676x2574.png" width="1200" height="1154.6703296703297" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/77f44130-4a47-4895-8a79-147a55a5ce84_2676x2574.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:1401,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:761737,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!1N5t!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F77f44130-4a47-4895-8a79-147a55a5ce84_2676x2574.png 424w, https://substackcdn.com/image/fetch/$s_!1N5t!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F77f44130-4a47-4895-8a79-147a55a5ce84_2676x2574.png 848w, https://substackcdn.com/image/fetch/$s_!1N5t!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F77f44130-4a47-4895-8a79-147a55a5ce84_2676x2574.png 1272w, https://substackcdn.com/image/fetch/$s_!1N5t!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F77f44130-4a47-4895-8a79-147a55a5ce84_2676x2574.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><ol><li><p>Back at the TxPool struct (Mempool) we can see we have a new field called Megabundles which is a mapping of addresses to &#8220;MevBundles&#8221;. The address represents the relay the MevBundle is coming from.</p></li><li><p>The sendMegabundleArgs struct is very similar to the MevBundle struct with one additional field RelaySignature. This is the signature from the relay for that specific Megabundle.</p></li><li><p>RecoverRelayAddress takes the RelaySignature and determines the address that made it. </p></li><li><p>The authorised relay addresses are maintained through the MinerTrustedRelaysFlag. The recovered address from the Megabundle signature will be cross-referenced with the trusted relay addresses to ensure only Megabundles from trusted relays are accepted.</p></li></ol><p>Why do Megabundles have this additional security where the node checks that it is from a trusted relayer. Let&#8217;s look into the implementation of how a Megabundle is committed to the block to help us understand why.</p><h3>Committing a Megabundle</h3><p>The Megabundles are the result of a Relay merging multiple bundles together to maximise profit.</p><p>They are designed to take computation load off the MEV Geth node. They do this by removing the need for the node to merge and order bundles, for the client to not perform these calculations there needs to be a level of trust.</p><p>Let&#8217;s see which steps are skipped when a Megabundle is committed to a block.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!IiY1!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fed9728a8-b992-488b-ac90-83740da6a564_2520x3346.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!IiY1!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fed9728a8-b992-488b-ac90-83740da6a564_2520x3346.png 424w, https://substackcdn.com/image/fetch/$s_!IiY1!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fed9728a8-b992-488b-ac90-83740da6a564_2520x3346.png 848w, https://substackcdn.com/image/fetch/$s_!IiY1!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fed9728a8-b992-488b-ac90-83740da6a564_2520x3346.png 1272w, https://substackcdn.com/image/fetch/$s_!IiY1!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fed9728a8-b992-488b-ac90-83740da6a564_2520x3346.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!IiY1!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fed9728a8-b992-488b-ac90-83740da6a564_2520x3346.png" width="1200" height="1593.131868131868" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/ed9728a8-b992-488b-ac90-83740da6a564_2520x3346.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:1933,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:1051804,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!IiY1!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fed9728a8-b992-488b-ac90-83740da6a564_2520x3346.png 424w, https://substackcdn.com/image/fetch/$s_!IiY1!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fed9728a8-b992-488b-ac90-83740da6a564_2520x3346.png 848w, https://substackcdn.com/image/fetch/$s_!IiY1!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fed9728a8-b992-488b-ac90-83740da6a564_2520x3346.png 1272w, https://substackcdn.com/image/fetch/$s_!IiY1!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fed9728a8-b992-488b-ac90-83740da6a564_2520x3346.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><ol><li><p>&#8220;commitNewWork&#8221; which handles filling a block with transactions</p></li><li><p>There is an if statement checking whether the received bundle is a Megabundle, if it is, the Megabundle is grabbed from the TxPool and committed to the block. Above line 1220 in the codebase is the alternate path for when != Megabundle the node takes the vanilla bundles and merges them as we saw previously.  </p></li><li><p>The Megabundle is committed to the block, note there is no &#8220;simulateBundles&#8221;, &#8220;mergeBundles&#8221; or &#8220;computeBundleGas&#8221;. This reduces the computation the node needs to complete.</p></li><li><p>&#8220;commitBundle&#8221; in turn calls &#8220;commitTransaction&#8221; </p></li><li><p>&#8220;commitTransaction&#8221; applies the transaction to the state, verifying it does not revert, updating the profit and returning the receipt logs.</p></li></ol><p>We can see that the Megabundles implementation focuses on speed &amp; performance.</p><p>In v0.5 the speed &amp; performance was increased even further by processing a Megabundle immediately if it was better than the best-known block so far. You can dig into the code changes <a href="https://github.com/flashbots/mev-geth/commit/f66aad30bcdcae3cf03d4970bc3581bd9b82caa7">here</a>. </p><p>In MEV Geth v0.6, the latest release of the client, private transactions were introduced. This enabled users to send transactions that would be bundled in with the remote transactions but wouldn&#8217;t be emitted to node peers for a period of time. </p><p>These private transactions aren&#8217;t sent to &#8220;the Mempool&#8221; where &#8220;the Mempool&#8221; means the TxPool on other Geth nodes. </p><p>Generalised frontrunners will snipe transactions they see in the Mempool that are profitable. While a Flashbot bundle protects against this since they aren&#8217;t sent to the Mempool they are recorded on the <a href="https://blocks.flashbots.net/">Flashbots API</a>.</p><p>Searchers often look through these recorded transactions for alpha. The searcher may want to disguise their Tx by putting it in with other remote transactions. </p><h3>MEV Boost </h3><p>As I mentioned earlier Megabundles were the precursor to full block builds and MEV Boost.</p><p>In ETH 2 the concept of a block builder has arisen. An entity dedicated to building optimised blocks. </p><p>In this new world Validators (Not Miners) will have the block ordering power and will be able to opt-in to receiving full blocks from builders to submit for their allocated slots.</p><p>These blocks will be blinded - the validator will not be able to see the transactions until the block has been emitted to the network. This prevents validators from stealing MEV for themselves reducing the required trust between parties.</p><p>MEV Boost will be a sidecar container connecting the Validator to Relays which in turn will connect to a network of &#8220;Block Builders&#8221;. <br><br>If you&#8217;d like to learn more <a href="https://github.com/flashbots/mev-boost">here</a> is a good place to start. </p><p>That&#8217;s all for today, hope you enjoyed it.</p><p>noxx</p><p>Follow me on Twitter <a href="https://twitter.com/noxx3xxon">@noxx3xxon</a></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://noxx.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading noxx! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[EVM Deep Dives: The Path to Shadowy Super Coder 🥷 💻 - Part 6]]></title><description><![CDATA[A Treasure Trove of Data - Transaction Receipts & Event Logs]]></description><link>https://noxx.substack.com/p/evm-deep-dives-the-path-to-shadowy-16e</link><guid isPermaLink="false">https://noxx.substack.com/p/evm-deep-dives-the-path-to-shadowy-16e</guid><dc:creator><![CDATA[noxx]]></dc:creator><pubDate>Mon, 27 Jun 2022 14:19:39 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/h_600,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ffbeb4061-3c97-4514-b2ca-205768b46e98_3258x2382.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Navigating on-chain data is an essential skill for anyone looking to build in the Web3 space. Understanding the data structures that make up a blockchain can help you think about new and creative ways to parse that data.</p><p>Today we&#8217;re going to be deep-diving into a key data structure within the EVM, transaction receipts and their associated event logs. If you&#8217;ve coded in Solidity before you&#8217;ve likely emitted event logs yourself, they make up a huge portion of the data available to us on-chain data.</p><p>In this article, we&#8217;ll journey from the block header all the way down to the internals of an event log giving you a comprehensive understanding of what data is available to you and how it was created.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://noxx.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://noxx.substack.com/subscribe?"><span>Subscribe now</span></a></p><h3>Why use Logs</h3><p>Before we begin I want to briefly touch on why we use Event logs as solidity developers.</p><ul><li><p>As a cheaper alternative to data storage, as long as the contract does not need access to it.</p></li><li><p>As a method to trigger web3 applications that are listening out for specific event logs.</p></li></ul><blockquote><p>EVM nodes are not required to keep logs forever and can remove old logs to save space. Contracts cannot access log storage so they are not required for nodes to execute a contract. Contract storage on the other hand is required for execution so cannot be removed.</p></blockquote><h3>Ethereum Block Merkle Roots  </h3><p>In <a href="https://noxx.substack.com/p/evm-deep-dives-the-path-to-shadowy-5a5?utm_source=%2Fprofile%2F80455042-noxx&amp;utm_medium=reader2">Part 4 of the EVM Deep Dives</a>, we dived into the Ethereum architecture  specifically the state Merkle root. The state Merkle root was 1 of 3 Merkle roots contained in the block header. The other 2 were the transaction Merkle root and the transaction receipts Merkle root.</p><p>To frame this deep dive we&#8217;re going to reference <a href="https://etherscan.io/block/15001871">block 15001871</a> on the Ethereum chain which contains 5 transactions, their associated receipts and the event logs that were emitted. This will help us link any concepts we learn back to a real-world example. </p><h3>Block Header</h3><p>We&#8217;ll start with the block header. There are 3 components we&#8217;re interested in, the &#8220;Transaction Root&#8221;,  the &#8220;Receipt Root&#8221; and the &#8220;Logs Bloom&#8221;. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Ey4W!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F035a70a8-abf1-4b66-bb55-811bc07c1bc1_964x462.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Ey4W!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F035a70a8-abf1-4b66-bb55-811bc07c1bc1_964x462.png 424w, https://substackcdn.com/image/fetch/$s_!Ey4W!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F035a70a8-abf1-4b66-bb55-811bc07c1bc1_964x462.png 848w, https://substackcdn.com/image/fetch/$s_!Ey4W!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F035a70a8-abf1-4b66-bb55-811bc07c1bc1_964x462.png 1272w, https://substackcdn.com/image/fetch/$s_!Ey4W!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F035a70a8-abf1-4b66-bb55-811bc07c1bc1_964x462.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Ey4W!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F035a70a8-abf1-4b66-bb55-811bc07c1bc1_964x462.png" width="964" height="462" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/035a70a8-abf1-4b66-bb55-811bc07c1bc1_964x462.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:462,&quot;width&quot;:964,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:95078,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Ey4W!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F035a70a8-abf1-4b66-bb55-811bc07c1bc1_964x462.png 424w, https://substackcdn.com/image/fetch/$s_!Ey4W!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F035a70a8-abf1-4b66-bb55-811bc07c1bc1_964x462.png 848w, https://substackcdn.com/image/fetch/$s_!Ey4W!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F035a70a8-abf1-4b66-bb55-811bc07c1bc1_964x462.png 1272w, https://substackcdn.com/image/fetch/$s_!Ey4W!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F035a70a8-abf1-4b66-bb55-811bc07c1bc1_964x462.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Block Header <a href="https://ethereum.stackexchange.com/questions/268/ethereum-block-architecture/757#757">source</a></figcaption></figure></div><p>Within the Ethereum client underneath the &#8220;Transaction&#8221; and &#8220;Receipt&#8221; roots are Merkle Patricia Tries containing the transaction data &amp; receipt data for all transactions and receipts within that block.</p><p>We won&#8217;t dive into how a Merkle Patricia Trie works. For the purpose of this article understanding that the node has access to all the transactions and receipts is all we need to know.  </p><p>Let&#8217;s take a look at the real block header for <a href="https://etherscan.io/block/15001871">block 15001871</a> by querying an Ethereum node.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!8EGV!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5ad2cc2a-a2f9-4078-b1b6-9654bfadf67c_1416x888.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!8EGV!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5ad2cc2a-a2f9-4078-b1b6-9654bfadf67c_1416x888.png 424w, https://substackcdn.com/image/fetch/$s_!8EGV!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5ad2cc2a-a2f9-4078-b1b6-9654bfadf67c_1416x888.png 848w, https://substackcdn.com/image/fetch/$s_!8EGV!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5ad2cc2a-a2f9-4078-b1b6-9654bfadf67c_1416x888.png 1272w, https://substackcdn.com/image/fetch/$s_!8EGV!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5ad2cc2a-a2f9-4078-b1b6-9654bfadf67c_1416x888.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!8EGV!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5ad2cc2a-a2f9-4078-b1b6-9654bfadf67c_1416x888.png" width="1416" height="888" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/5ad2cc2a-a2f9-4078-b1b6-9654bfadf67c_1416x888.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:888,&quot;width&quot;:1416,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:112132,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!8EGV!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5ad2cc2a-a2f9-4078-b1b6-9654bfadf67c_1416x888.png 424w, https://substackcdn.com/image/fetch/$s_!8EGV!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5ad2cc2a-a2f9-4078-b1b6-9654bfadf67c_1416x888.png 848w, https://substackcdn.com/image/fetch/$s_!8EGV!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5ad2cc2a-a2f9-4078-b1b6-9654bfadf67c_1416x888.png 1272w, https://substackcdn.com/image/fetch/$s_!8EGV!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5ad2cc2a-a2f9-4078-b1b6-9654bfadf67c_1416x888.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Block Header</figcaption></figure></div><p>Take note of the block header logsBloom, it is a key data structure that we will refer back to later in the article.</p><p>For now let&#8217;s start with the data that lies underneath the Transaction Root, the Transaction Trie.</p><h3>Transaction Trie</h3><p>The &#8220;Transaction Trie&#8221;  is the data set that generates the transactionsRoot and records the transaction request vectors. </p><p>Transaction request vectors are the pieces of information required to execute a transaction. </p><p>The fields included in a transaction can be seen below.</p><ul><li><p>Type = The transaction type (LegacyTxType, AccessListTxType, DynamicFeeTxType).</p></li><li><p>ChainId =  The EIP155 chain ID of the transaction.</p></li><li><p>Data = The input data of the transaction</p></li><li><p>AccessList = The access list of the transaction</p></li><li><p>Gas = The gas limit of the transaction</p></li><li><p>GasPrice = The gas price of the transaction</p></li><li><p>GasTipCap = The gasTipCap per gas of the transaction</p></li><li><p>GasFeeCap = The fee cap per gas of the transaction</p></li><li><p>Value = The ether amount of the transaction</p></li><li><p>Nonce = The sender account nonce of the transaction</p></li><li><p>To = The recipient address of the transaction. For contract-creation transactions, To returns nil</p></li><li><p>RawSignatureValues =  The V, R, S signature values of the transaction</p></li></ul><p>Let&#8217;s take a look at some real data from a transaction within <a href="https://etherscan.io/block/15001871">block 15001871</a>. We&#8217;ll use the first <a href="https://etherscan.io/tx/0x311ba3a0affb00510ae3f0a36c5bcd0a48cdb23d803bbc16f128639ffb9e3e58">transaction 0x311ba3a0affb00510ae3f0a36c5bcd0a48cdb23d803bbc16f128639ffb9e3e58</a>. </p><p>Let&#8217;s use Geth&#8217;s ethclient to query a node. Note ChainId and AccessList both have &#8220;omitempty&#8221;, which means if the field is empty it will be omitted from the response.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!uAqL!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F36425f2b-4b1b-4c17-b72a-d1318004f6fb_2960x1036.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!uAqL!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F36425f2b-4b1b-4c17-b72a-d1318004f6fb_2960x1036.png 424w, https://substackcdn.com/image/fetch/$s_!uAqL!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F36425f2b-4b1b-4c17-b72a-d1318004f6fb_2960x1036.png 848w, https://substackcdn.com/image/fetch/$s_!uAqL!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F36425f2b-4b1b-4c17-b72a-d1318004f6fb_2960x1036.png 1272w, https://substackcdn.com/image/fetch/$s_!uAqL!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F36425f2b-4b1b-4c17-b72a-d1318004f6fb_2960x1036.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!uAqL!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F36425f2b-4b1b-4c17-b72a-d1318004f6fb_2960x1036.png" width="1200" height="420.3296703296703" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/36425f2b-4b1b-4c17-b72a-d1318004f6fb_2960x1036.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:510,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:387582,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!uAqL!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F36425f2b-4b1b-4c17-b72a-d1318004f6fb_2960x1036.png 424w, https://substackcdn.com/image/fetch/$s_!uAqL!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F36425f2b-4b1b-4c17-b72a-d1318004f6fb_2960x1036.png 848w, https://substackcdn.com/image/fetch/$s_!uAqL!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F36425f2b-4b1b-4c17-b72a-d1318004f6fb_2960x1036.png 1272w, https://substackcdn.com/image/fetch/$s_!uAqL!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F36425f2b-4b1b-4c17-b72a-d1318004f6fb_2960x1036.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Transaction Data - <a href="https://github.com/ethereum/go-ethereum/blob/c503f98f6d5e80e079c1d8a3601d188af2a899da/core/types/transaction_marshalling.go">transaction_marshalling.go</a></figcaption></figure></div><p>This transaction represents a transfer of USDT to this address <a href="https://etherscan.io/address/0xec23e787ea25230f74a3da0f515825c1d820f47a">0xec23e787ea25230f74a3da0f515825c1d820f47a</a>. </p><p>The to address is the ERC20 USDT contract <a href="https://etherscan.io/address/0xdac17f958d2ee523a2206206994597c13d831ec7">0xdac17f958d2ee523a2206206994597c13d831ec7</a>.</p><p>If we look at the input data we can see the <a href="https://www.4byte.directory/signatures/?bytes4_signature=0xa9059cbb">function signature 0xa9059cbb which corresponds to transfer(address,uint256)</a> , the address to send the USDT to <a href="https://etherscan.io/address/0xec23e787ea25230f74a3da0f515825c1d820f47a">0xec23e787ea25230f74a3da0f515825c1d820f47a</a> and the amount 0x2b279b8 = 45251000 in decimal or $45.251.</p><p>What you might notice about this transaction data structure is that it doesn&#8217;t tell us anything about the outcome of the transaction. Was the transaction successful? How much gas did it use? What event logs were emitted?</p><p>This is where transaction receipts &amp; the &#8220;Receipt Trie<strong>&#8221;</strong> come in.</p><h3>Receipts Trie</h3><p>A shopping receipt records the outcome of a transaction. An object in the Receipt Trie does the same thing for an Ethereum transaction along with some additional details. </p><p>The questions posed above are what a transaction receipt looks to answer. We&#8217;re going to focus on the third question. What event logs were emitted?</p><p>Again I&#8217;ve queried the chain to get some us some real data. We&#8217;re going to look at the transaction receipt for the transaction we looked at above <a href="https://etherscan.io/tx/0x311ba3a0affb00510ae3f0a36c5bcd0a48cdb23d803bbc16f128639ffb9e3e58">0x311ba3a0affb00510ae3f0a36c5bcd0a48cdb23d803bbc16f128639ffb9e3e58</a>.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!vc9r!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe37dd2e5-e2dc-4945-8f6b-096605bfe414_3192x1378.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!vc9r!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe37dd2e5-e2dc-4945-8f6b-096605bfe414_3192x1378.png 424w, https://substackcdn.com/image/fetch/$s_!vc9r!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe37dd2e5-e2dc-4945-8f6b-096605bfe414_3192x1378.png 848w, https://substackcdn.com/image/fetch/$s_!vc9r!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe37dd2e5-e2dc-4945-8f6b-096605bfe414_3192x1378.png 1272w, https://substackcdn.com/image/fetch/$s_!vc9r!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe37dd2e5-e2dc-4945-8f6b-096605bfe414_3192x1378.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!vc9r!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe37dd2e5-e2dc-4945-8f6b-096605bfe414_3192x1378.png" width="1200" height="518.4065934065934" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/e37dd2e5-e2dc-4945-8f6b-096605bfe414_3192x1378.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:629,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:314849,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!vc9r!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe37dd2e5-e2dc-4945-8f6b-096605bfe414_3192x1378.png 424w, https://substackcdn.com/image/fetch/$s_!vc9r!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe37dd2e5-e2dc-4945-8f6b-096605bfe414_3192x1378.png 848w, https://substackcdn.com/image/fetch/$s_!vc9r!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe37dd2e5-e2dc-4945-8f6b-096605bfe414_3192x1378.png 1272w, https://substackcdn.com/image/fetch/$s_!vc9r!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe37dd2e5-e2dc-4945-8f6b-096605bfe414_3192x1378.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Receipt Data - <a href="https://github.com/ethereum/go-ethereum/blob/f5037185aa6ebea0f7e6765b2439a3c8a066e85e/core/types/receipt.go">receipt.go</a></figcaption></figure></div><p>Let&#8217;s run through the fields.</p><ul><li><p>Type = The transaction type (LegacyTxType, AccessListTxType, DynamicFeeTxType).</p></li><li><p>PostState (root) = The StateRoot post-execution of the transaction. You may note it&#8217;s 0x in the query above this is likely due to <a href="https://github.com/ethereum/EIPs/issues/98">EIP-98</a>.</p></li><li><p>CumulativeGasUsed = Sum of gasUsed by this transaction and all preceding transactions in the same block.</p></li><li><p>Bloom (logsBloom) = Bloom filter for event logs (We&#8217;ll dig into this in the next section, remember we saw a logsBloom field in the block header as well)</p></li><li><p>Logs = Array of log objects</p></li><li><p>TxHash = The transaction hash that the receipt is associated with</p></li><li><p>ContractAddress = Address of the deployed contract if the transaction was a contract creation. 0x000&#8230;0 if the transaction isn&#8217;t a contract creation.</p></li><li><p>GasUsed = Gas used by this transaction</p></li><li><p>BlockHash = Hash of the block this transaction occurred in</p></li><li><p>BlockNumber = Block number for the block this transaction occurred in</p></li><li><p>TransactionIndex = Transactions index within the block. The index determines which transaction is executed first. This transaction is at the top of the block and therefore has an index 0.</p></li></ul><p>Now we know what a transaction receipt is composed of we can zoom in on the logsBloom and the log array within the transaction receipt.</p><h3>Event Logs</h3><p>We noted in the transaction section that this transaction is a USDT transfer. I&#8217;ve grabbed a snippet of <a href="https://etherscan.io/address/0xdac17f958d2ee523a2206206994597c13d831ec7#code">code from the USDT contract on Etherscan</a> for us to review.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!uuZQ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdbd03051-6cf4-416d-b5a8-90955e6757ad_1312x1070.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!uuZQ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdbd03051-6cf4-416d-b5a8-90955e6757ad_1312x1070.png 424w, https://substackcdn.com/image/fetch/$s_!uuZQ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdbd03051-6cf4-416d-b5a8-90955e6757ad_1312x1070.png 848w, https://substackcdn.com/image/fetch/$s_!uuZQ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdbd03051-6cf4-416d-b5a8-90955e6757ad_1312x1070.png 1272w, https://substackcdn.com/image/fetch/$s_!uuZQ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdbd03051-6cf4-416d-b5a8-90955e6757ad_1312x1070.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!uuZQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdbd03051-6cf4-416d-b5a8-90955e6757ad_1312x1070.png" width="1312" height="1070" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/dbd03051-6cf4-416d-b5a8-90955e6757ad_1312x1070.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1070,&quot;width&quot;:1312,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:138075,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!uuZQ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdbd03051-6cf4-416d-b5a8-90955e6757ad_1312x1070.png 424w, https://substackcdn.com/image/fetch/$s_!uuZQ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdbd03051-6cf4-416d-b5a8-90955e6757ad_1312x1070.png 848w, https://substackcdn.com/image/fetch/$s_!uuZQ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdbd03051-6cf4-416d-b5a8-90955e6757ad_1312x1070.png 1272w, https://substackcdn.com/image/fetch/$s_!uuZQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdbd03051-6cf4-416d-b5a8-90955e6757ad_1312x1070.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">USDT ERC20 Contract Extract</figcaption></figure></div><p>We can see the Transfer event is declared on line 86 and that 2 of the input parameters have the keyword &#8220;indexed&#8221;. </p><p>You may be wondering what the indexed keyword means. When an event input is &#8220;indexed&#8221; it enables us to do quick look-ups for a log with that input. </p><p>For example with an indexed &#8220;from&#8221; as seen above I can ask the question get me all event logs of type Transfer with a &#8220;from&#8221; address of <a href="https://etherscan.io/address/0x5041ed759dd4afc3a72b8192c143f72f4724081a">0x5041ed759dd4afc3a72b8192c143f72f4724081a</a> between blocks X &amp; Y. How this indexing works under the hood will be covered in the next section.</p><p>We can also see that this event log is emitted when the transfer function is called on line 138. Note this contract was created with an earlier solidity version hence why the emit keyword is missing. </p><p>Again let&#8217;s take a look at the real on-chain data for this transaction. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!CDpz!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F93f8f00c-6f0e-4491-a90a-1dae0dc528c7_3180x1176.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!CDpz!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F93f8f00c-6f0e-4491-a90a-1dae0dc528c7_3180x1176.png 424w, https://substackcdn.com/image/fetch/$s_!CDpz!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F93f8f00c-6f0e-4491-a90a-1dae0dc528c7_3180x1176.png 848w, https://substackcdn.com/image/fetch/$s_!CDpz!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F93f8f00c-6f0e-4491-a90a-1dae0dc528c7_3180x1176.png 1272w, https://substackcdn.com/image/fetch/$s_!CDpz!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F93f8f00c-6f0e-4491-a90a-1dae0dc528c7_3180x1176.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!CDpz!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F93f8f00c-6f0e-4491-a90a-1dae0dc528c7_3180x1176.png" width="1200" height="443.4065934065934" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/93f8f00c-6f0e-4491-a90a-1dae0dc528c7_3180x1176.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:538,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:307703,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!CDpz!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F93f8f00c-6f0e-4491-a90a-1dae0dc528c7_3180x1176.png 424w, https://substackcdn.com/image/fetch/$s_!CDpz!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F93f8f00c-6f0e-4491-a90a-1dae0dc528c7_3180x1176.png 848w, https://substackcdn.com/image/fetch/$s_!CDpz!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F93f8f00c-6f0e-4491-a90a-1dae0dc528c7_3180x1176.png 1272w, https://substackcdn.com/image/fetch/$s_!CDpz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F93f8f00c-6f0e-4491-a90a-1dae0dc528c7_3180x1176.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Log Data - <a href="https://github.com/ethereum/go-ethereum/blob/f5037185aa6ebea0f7e6765b2439a3c8a066e85e/core/types/log.go">log.go</a></figcaption></figure></div><p>If you refer to the comments in the Log struct you&#8217;ll see descriptions for each field. The fields we want to take a closer look at are address, topics and data.</p><h4>Topics</h4><p>Let&#8217;s start with topics. Topics are indexed values. You&#8217;ll notice we have 3 topics in our on-chain query while the Transfer event only has 2 indexed parameters (from &amp; to). This is because the first topic is always the hash of the event signature.</p><p>In this case the event signature is Transfer(address,address,uint256). We keccak256 hash this value to give us ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef. Try it for yourself <a href="https://emn178.github.io/online-tools/keccak_256.html">here</a> (Note input type is text). </p><p>This makes sense, if we look at the question we wanted to ask above we wanted to limit it to event logs of type Transfer only. There may be multiple Events that have a from field so by indexing the event signature as well we are able to filter by event type.</p><p>We can have a maximum of 4 topics and each topic is 32 bytes in size. We can declare 3 indexed parameters given the first is taken by the event signature. </p><p>There is one case when the first topic isn&#8217;t the hashed event signature. This is when we declare an anonymous Event. This opens up the possibility of having 4 indexed parameters rather than 3 but we lose the ability to index on the Event name. One other advantage of anonymous events is that they can be cheaper to deploy since they don&#8217;t force you to use 1 additional topic.</p><p>The other topics are the indexed &#8220;from&#8221; and &#8220;to&#8221; values from the Transfer Event.</p><blockquote><p>If the type of an indexed parameter is larger than 32 bytes (i.e. string and bytes), the actual data isn&#8217;t stored, but rather the keccak256 digest of the data is stored.</p></blockquote><h4>Data</h4><p>The data section contains the remaining (non-indexed) parameters in the event log. In our case this is just &#8220;value&#8221; 0x0000000000000000000000000000000000000000000000000000000002b279b8 which is equal to 45251000 in decimal or $45.251. </p><p>If we had more they would be appended to the data item. Let&#8217;s look at an example where there is more than 1 non-indexed parameter.</p><p></p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!JhhQ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fde0519d3-695e-4e4c-a579-6cf532ac0e33_1376x294.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!JhhQ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fde0519d3-695e-4e4c-a579-6cf532ac0e33_1376x294.png 424w, https://substackcdn.com/image/fetch/$s_!JhhQ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fde0519d3-695e-4e4c-a579-6cf532ac0e33_1376x294.png 848w, https://substackcdn.com/image/fetch/$s_!JhhQ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fde0519d3-695e-4e4c-a579-6cf532ac0e33_1376x294.png 1272w, https://substackcdn.com/image/fetch/$s_!JhhQ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fde0519d3-695e-4e4c-a579-6cf532ac0e33_1376x294.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!JhhQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fde0519d3-695e-4e4c-a579-6cf532ac0e33_1376x294.png" width="1376" height="294" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/de0519d3-695e-4e4c-a579-6cf532ac0e33_1376x294.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:294,&quot;width&quot;:1376,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:52210,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!JhhQ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fde0519d3-695e-4e4c-a579-6cf532ac0e33_1376x294.png 424w, https://substackcdn.com/image/fetch/$s_!JhhQ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fde0519d3-695e-4e4c-a579-6cf532ac0e33_1376x294.png 848w, https://substackcdn.com/image/fetch/$s_!JhhQ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fde0519d3-695e-4e4c-a579-6cf532ac0e33_1376x294.png 1272w, https://substackcdn.com/image/fetch/$s_!JhhQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fde0519d3-695e-4e4c-a579-6cf532ac0e33_1376x294.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>In this example, we add an additional &#8220;tax&#8221; field to the Transfer event. Let&#8217;s assume tax is 20% so our tax value should be 20% of 45251000. This is 9050200 in decimal which is 0x8a1858 in hex, the type is uint = uint256 so we&#8217;ll need to pad the hex value to 32 bytes.</p><p>The resulting data item would be 0x0000000000000000000000000000000000000000000000000000000002b279b800000000000000000000000000000000000000000000000000000000008a1858.</p><h4>Address</h4><p>The address field is the address of the contract that emitted the event. One important note on this field is that it will also be indexed despite it not being included in the topics section.</p><p>Again this makes sense, the Transfer event is part of the ERC20 standard meaning when we filter logs on the ERC20 Transfer event we&#8217;re going to get the transfer events from all ERC20 contracts.</p><p>By indexing the contract address we can narrow down the search to a specific contract/token that we are interested in (USDT).</p><h4>Opcodes </h4><p>Finally, let&#8217;s touch on the LOG opcodes of which there are 5. They go from LOG0 for when no topics are included to LOG4 when 4 topics are included.</p><p>LOG3 is what would have been used in our example. It takes in </p><ul><li><p>offset = memory offset, which represents the start location of the data field input</p></li><li><p>length = length of the data to read in from memory </p></li><li><p>topic1 = value for topic1</p></li><li><p>topic2 = value for topic2</p></li><li><p>topic3 = value for topic3</p></li></ul><p>Offset and length define where in memory the data is located for the data section. </p><p>Now we understand how the log is structured we can finally answer the question of what happens under the hood when a topic is indexed. </p><h3>Bloom Filters</h3><p>The secret to how indexed items enable faster lookup is Bloom filters.<br><br><a href="https://llimllib.github.io/bloomfilter-tutorial/">Llimllib</a> has a great definition of what these data structures are. </p><blockquote><p>A Bloom filter is a data structure designed to tell you, rapidly and memory-efficiently, whether an element is present in a set.</p><p>The price paid for this efficiency is that a Bloom filter is a <strong>probabilistic data structure</strong>: it tells us that the element either <em>definitely is not</em> in the set or <em>may be</em> in the set.</p><p>The base data structure of a Bloom filter is a <strong>Bit Vector</strong>.</p></blockquote><p>Below is an example of a small bit vector. The white cells represent bits with value 0 while the green cells represent bits with value 1.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!rnvl!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe40d3199-efe8-4db5-9414-d303bf8066f3_408x55.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!rnvl!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe40d3199-efe8-4db5-9414-d303bf8066f3_408x55.png 424w, https://substackcdn.com/image/fetch/$s_!rnvl!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe40d3199-efe8-4db5-9414-d303bf8066f3_408x55.png 848w, https://substackcdn.com/image/fetch/$s_!rnvl!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe40d3199-efe8-4db5-9414-d303bf8066f3_408x55.png 1272w, https://substackcdn.com/image/fetch/$s_!rnvl!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe40d3199-efe8-4db5-9414-d303bf8066f3_408x55.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!rnvl!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe40d3199-efe8-4db5-9414-d303bf8066f3_408x55.png" width="408" height="55" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/e40d3199-efe8-4db5-9414-d303bf8066f3_408x55.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:55,&quot;width&quot;:408,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!rnvl!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe40d3199-efe8-4db5-9414-d303bf8066f3_408x55.png 424w, https://substackcdn.com/image/fetch/$s_!rnvl!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe40d3199-efe8-4db5-9414-d303bf8066f3_408x55.png 848w, https://substackcdn.com/image/fetch/$s_!rnvl!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe40d3199-efe8-4db5-9414-d303bf8066f3_408x55.png 1272w, https://substackcdn.com/image/fetch/$s_!rnvl!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe40d3199-efe8-4db5-9414-d303bf8066f3_408x55.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>These bits were set to 1 by taking some input and hashing it. The value of the resulting hash is used as the bit index for which bit should be updated.</p><p>The bit vector above is the result of using 2 different hash functions on the value &#8220;ethereum&#8221; to get 2 bit indexes.</p><p>The hashes represent hexadecimal numbers. To get the index we can take this number and convert it into a value between 0 and 14. There are various ways to do this such as using modulo 14.   </p><p>See <a href="https://llimllib.github.io/bloomfilter-tutorial/">this great site for this example and try it out yourself.</a> </p><h4>Lookup</h4><p>Ok, so we have a bloom filter for a transaction which we now understand to be a bit vector. For Ethereum, the inputs that are hashed to determine which bits to update in the bit vector are the address field and the topics of the event log.</p><p>Let&#8217;s refer back to the logBloom in our transaction receipt. This is the bloom filter for a specific transaction. Remember a transaction can have multiple logs so this represents the addresses/topics of all those logs.</p><pre><code>0x00000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010800000000000000000000000000000000000000000000000000000000000000000100000000000000000000008000080000000000000000000000000000000000000000000002002000000000000000000000000004000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000</code></pre><p>If we refer back to our block header we have another logsBloom. This is the bloom filter for all transactions within the block. This is all the addresses/topics in every log of every transaction. </p><pre><code>0x0100000000000004000000000000100000000000000000000000000000200000000000000000000000000000000001000000000000000000080008000000000040000000002000000000040800000000000000000000200000000000004800000040000002000000000000000000081000000000000040000000001080020000200001000000000000000000040000004000000000000000000000000010000000008000000000000800008000000800000000008000000200000800000000000000200a000000200000000000100002004000000000000000000000000020000000000080000020000000800000000000004000000000000000080000004000</code></pre><p>These bloom filters are represented in hex rather than binary. They are 256 bytes in length which represents a 2048-bit vector.</p><p>If we refer to the <a href="https://llimllib.github.io/bloomfilter-tutorial/">Llimllib</a> example above our bit vector was 15 in length with bit index 2 and 13 flipped. If we convert that to hex let&#8217;s see what we get.</p><pre><code>001000000000010 = 0x1002</code></pre><p>So while the hex may not look like a bit vector remember that it is under the hood.</p><h4>Queries</h4><p>If we remember our earlier query where we asked &#8220;<em>get me all event logs of type Transfer with a &#8220;from&#8221; address of <a href="https://etherscan.io/address/0x5041ed759dd4afc3a72b8192c143f72f4724081a">0x5041ed759dd4afc3a72b8192c143f72f4724081a</a> between blocks X &amp; Y&#8221;</em>.</p><p>We can take the Event signature topic, which represents the type Transfer along with the from value topic (<a href="https://etherscan.io/address/0x5041ed759dd4afc3a72b8192c143f72f4724081a">0x5041ed759dd4afc3a72b8192c143f72f4724081a</a>) and determine which bit indexes in the bloom filter should be set to 1.</p><p>If we use the logsBloom in the block header we can check if any of these bits aren&#8217;t set to 1. If they aren&#8217;t we can know with <strong>certainty</strong> that there are no logs that match that criteria in the block. </p><p>If we find the bits are set we know that a matching log <strong>may</strong> <strong>be</strong> in the block. We don&#8217;t know with certainty because the block header logsBloom is made up of multiple addresses and topics. It&#8217;s possible other event logs have set the matching bits. This is why a bloom filter is a probabilistic data structure.</p><p>The bigger the bit vector the less chance of a bit index collision from other logs.</p><p>Once we have a matching bloom filter we can query the individual receipt logsBloom using the same methodology. When we get a match we can view the actual log entries to retrieve the object. </p><p>By doing this from block X to Y we can quickly find &amp; retrieve all logs that match our criteria.</p><p>That&#8217;s conceptually how the bloom filter works. Let&#8217;s now see the actual implementation used in Ethereum.</p><h3>Geth Implementation - Bloom Filters</h3><p>We understand how a bloom filter works but we want to know the exact steps of how we go from the address/topics to the logsBloom and see it done with a real block.</p><p>Ok no problem, we can start with the definition in the <a href="https://ethereum.github.io/yellowpaper/paper.pdf">yellow paper</a>. Don&#8217;t worry if it makes no sense right now we&#8217;re about to break it down.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!xqoR!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5f60f1f7-67f7-4e9a-b4b8-695703b07a91_596x554.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!xqoR!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5f60f1f7-67f7-4e9a-b4b8-695703b07a91_596x554.png 424w, https://substackcdn.com/image/fetch/$s_!xqoR!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5f60f1f7-67f7-4e9a-b4b8-695703b07a91_596x554.png 848w, https://substackcdn.com/image/fetch/$s_!xqoR!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5f60f1f7-67f7-4e9a-b4b8-695703b07a91_596x554.png 1272w, https://substackcdn.com/image/fetch/$s_!xqoR!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5f60f1f7-67f7-4e9a-b4b8-695703b07a91_596x554.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!xqoR!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5f60f1f7-67f7-4e9a-b4b8-695703b07a91_596x554.png" width="596" height="554" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/5f60f1f7-67f7-4e9a-b4b8-695703b07a91_596x554.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:554,&quot;width&quot;:596,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!xqoR!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5f60f1f7-67f7-4e9a-b4b8-695703b07a91_596x554.png 424w, https://substackcdn.com/image/fetch/$s_!xqoR!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5f60f1f7-67f7-4e9a-b4b8-695703b07a91_596x554.png 848w, https://substackcdn.com/image/fetch/$s_!xqoR!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5f60f1f7-67f7-4e9a-b4b8-695703b07a91_596x554.png 1272w, https://substackcdn.com/image/fetch/$s_!xqoR!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5f60f1f7-67f7-4e9a-b4b8-695703b07a91_596x554.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><a href="https://ethereum.github.io/yellowpaper/paper.pdf">Bloom Filter Yellow Paper</a></figcaption></figure></div><p>The easiest way to show you what this means is to provide an example and reference  back to the Geth client implementation.</p><p>Here&#8217;s the transaction log we looked at above on <a href="https://etherscan.io/tx/0x311ba3a0affb00510ae3f0a36c5bcd0a48cdb23d803bbc16f128639ffb9e3e58#eventlog">Etherscan</a>. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!IdEy!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F562f4d44-59d7-42b4-a398-098aea158763_1834x628.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!IdEy!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F562f4d44-59d7-42b4-a398-098aea158763_1834x628.png 424w, https://substackcdn.com/image/fetch/$s_!IdEy!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F562f4d44-59d7-42b4-a398-098aea158763_1834x628.png 848w, https://substackcdn.com/image/fetch/$s_!IdEy!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F562f4d44-59d7-42b4-a398-098aea158763_1834x628.png 1272w, https://substackcdn.com/image/fetch/$s_!IdEy!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F562f4d44-59d7-42b4-a398-098aea158763_1834x628.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!IdEy!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F562f4d44-59d7-42b4-a398-098aea158763_1834x628.png" width="1456" height="499" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/562f4d44-59d7-42b4-a398-098aea158763_1834x628.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:499,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:92215,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!IdEy!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F562f4d44-59d7-42b4-a398-098aea158763_1834x628.png 424w, https://substackcdn.com/image/fetch/$s_!IdEy!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F562f4d44-59d7-42b4-a398-098aea158763_1834x628.png 848w, https://substackcdn.com/image/fetch/$s_!IdEy!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F562f4d44-59d7-42b4-a398-098aea158763_1834x628.png 1272w, https://substackcdn.com/image/fetch/$s_!IdEy!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F562f4d44-59d7-42b4-a398-098aea158763_1834x628.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>We&#8217;re going to look at the first topic which is the Event signature 0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef and show how this value is converted into which bit indexes should be updated.</p><p>Below is the bloomValues function from the Geth codebase. This is the function takes in data such as the Event signature topic 0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef and give us back the bit indexes that need to be updated in the bloom filter. Let&#8217;s run through it. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!VdWY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ffbeb4061-3c97-4514-b2ca-205768b46e98_3258x2382.png" data-component-name="Image2ToDOM"><div class="image2-inset image2-full-screen"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!VdWY!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ffbeb4061-3c97-4514-b2ca-205768b46e98_3258x2382.png 424w, https://substackcdn.com/image/fetch/$s_!VdWY!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ffbeb4061-3c97-4514-b2ca-205768b46e98_3258x2382.png 848w, https://substackcdn.com/image/fetch/$s_!VdWY!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ffbeb4061-3c97-4514-b2ca-205768b46e98_3258x2382.png 1272w, https://substackcdn.com/image/fetch/$s_!VdWY!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ffbeb4061-3c97-4514-b2ca-205768b46e98_3258x2382.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!VdWY!,w_5760,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ffbeb4061-3c97-4514-b2ca-205768b46e98_3258x2382.png" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/fbeb4061-3c97-4514-b2ca-205768b46e98_3258x2382.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;full&quot;,&quot;height&quot;:1065,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:305456,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-fullscreen" alt="" srcset="https://substackcdn.com/image/fetch/$s_!VdWY!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ffbeb4061-3c97-4514-b2ca-205768b46e98_3258x2382.png 424w, https://substackcdn.com/image/fetch/$s_!VdWY!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ffbeb4061-3c97-4514-b2ca-205768b46e98_3258x2382.png 848w, https://substackcdn.com/image/fetch/$s_!VdWY!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ffbeb4061-3c97-4514-b2ca-205768b46e98_3258x2382.png 1272w, https://substackcdn.com/image/fetch/$s_!VdWY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ffbeb4061-3c97-4514-b2ca-205768b46e98_3258x2382.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Bloom Filter Method - <a href="https://github.com/ethereum/go-ethereum/blob/d8ff53dfb8a516f47db37dbc7fd7ad18a1e8a125/core/types/bloom9.go">bloom9.go</a></figcaption></figure></div><ol><li><p>The function takes in the data ie a topic (In our case the event signature topic) and a hashbuf which is just an empty byte array of length 6. </p><ol><li><p>Refer back to the yellow paper snippet, &#8220;<em>the</em> <em>first three pairs of bytes in a Keccak-256 hash of the byte sequence</em>&#8221;. Three pairs of bytes are equal to 6 bytes which is why our hashbuf is of length 6. </p></li><li><p>The data for our example is 0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef</p></li></ol></li><li><p>The sha commands between lines 140 - 144 hashes the data input and load the output into the hashbuf. </p><ol><li><p>The sha output, which uses keccak256, is ada389e1fc24a8587c776340efb91b36e675792ab631816100d55df0b5cf3cbc. </p></li><li><p>You can verify this using <a href="https://emn178.github.io/online-tools/keccak_256.html">the online keccak 256</a>. (Note make sure you change the input type from text to hex. When using keccak 256 for function signatures the input is of type text whereas here it is of type hex.)</p></li><li><p>The hasbuf now has contents [ad, a3, 89, e1, fc, 24] (in hex). Remember each hexadecimal character represents 4 bits so 2 characters represent an 8-bit byte.</p></li></ol></li><li><p>v1 is calculated.</p><ol><li><p>hashbuf [1] = 0xa3 = 10100011 is used with a bitwise AND against 0x7. 0x7 = 00000111 in binary.</p></li><li><p>A byte is composed of 8 bits, if we want to obtain a bit index we need to ensure the value we get is between 0 and 7 for a zero index array. Using a bitwise AND constrains hashbuf [1] to be a value between 0 and 7. In our case, it is a 3 = 00000011.</p></li><li><p>This bit index value is used with a bit shift operator to create an 8-bit byte with the flipped bit at the correct index, 00001000.</p></li><li><p>v1 is the whole byte rather than the actual bit index because this value will later be used with a bitwise OR on the bloom filter. The OR will ensure all corresponding bits in the bloom filter will also be flipped.</p></li></ol></li><li><p>We now have the byte value but we still need the byte index. Our bloom filter is 256 bytes (2048 bits) in length so we need to know which byte to run the bitwise OR on. The value i1 represents this byte index.</p><ol><li><p>Note we use a big-endian uint16 with our hashbuf, this will constrain it to the first 2 bytes of the array. In our case this represents 0xada3 = 1010110110100011.</p></li><li><p>We use this value with a bitwise AND against 0x7ff = 0000011111111111. If you count the number of bits set to 1 in 0x7ff you&#8217;ll notice there are eleven. From the yellow paper, &#8220;<em>It does this through taking the low-order 11 bits of each of the first three pairs</em>&#8221;. This yields us the value 0000010110100011.</p></li><li><p>This value is then bit shifted down by 3. This turns an 11-bit number into an 8-bit number. We want a byte index and our bloom filter has a byte length of 256 so we need our byte index value to be in that range. An 8-bit number can be any value from 0 to 255. In our case, this value is 180.</p></li><li><p>We calculate our byte index bus using the BloomByteLength which we know is 256 minus our calculated value 180, minus 1. The minus 1 is to keep the result between 0 to 255. This gives us our byte index to update, in this case it&#8217;s byte 75.</p></li></ol></li><li><p>This is telling us to update the bit index 3 (0 index so 4th bit) in the 75th byte of the bloom filter. This can be done by running a bitwise OR of v1 against the 75th byte in the bloom filter. </p><ol><li><p>Note we&#8217;ve only covered the first &#8220;byte pair&#8221; 0xada3, This is done again for &#8220;byte pair&#8221; 2 and 3. Each address/topic will update 3 bits in the 2048-bit vector. From the yellow paper, &#8220;<em>specialised Bloom filter that sets three bits out of 2048&#8221;. </em></p></li><li><p>&#8220;Byte pair&#8221; 2 states update bit index 1 in byte 195</p></li><li><p>&#8220;Byte pair&#8221; 3 states update bit index 4 in byte 123</p></li><li><p>If the bit to be changed has already been flipped by another topic it will stay as is otherwise it will be flipped to a 1.</p></li></ol></li></ol><p>So in conclusion we have determined that the Event signature topic will flip the following bits in the bloom filter.</p><ul><li><p>bit index 3 in byte 75</p></li><li><p>bit index 1 in byte 195</p></li><li><p>bit index 4 in byte 123</p></li></ul><p>Take a look at the logBlooms in the transaction receipt convert it to binary and you can verify those bit indexes are set.</p><p>I&#8217;ve compiled this example into a <a href="https://github.com/noxx3xxon/evm-by-example">Github repo evm-by-example</a> for you to play around with. Check out the bloom folder it will definitely help consolidate what you&#8217;ve learned in the article.</p><p>For those interested in going a little deeper down the rabbit hole have a look at the <a href="https://github.com/zsfelfoldi/go-ethereum/wiki/BloomBits-Trie">BloomBits Trie</a>.</p><p>Till next time.</p><p>noxx</p><p>Twitter <a href="https://twitter.com/noxx3xxon">@noxx3xxon</a></p>]]></content:encoded></item><item><title><![CDATA[MEV Memoirs: Into the Arena - Chapter 1, Part 2 🤖 ]]></title><description><![CDATA[Unearthing Alpha through Decompiled Contracts]]></description><link>https://noxx.substack.com/p/mev-memoirs-into-the-arena-chapter-3e9</link><guid isPermaLink="false">https://noxx.substack.com/p/mev-memoirs-into-the-arena-chapter-3e9</guid><dc:creator><![CDATA[noxx]]></dc:creator><pubDate>Sat, 11 Jun 2022 20:50:12 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/h_600,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F00db9003-17e5-4207-ad37-32e424476fb8_2658x1228.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>So you&#8217;ve heard about the dark forest, now you want to unearth the secrets inside.</p><p>MEV bots are scampering around the forest floor and we want to decipher what those bots are doing under the hood.</p><p>To find these secrets we&#8217;re going to have the learn the tools &amp; techniques related to decompiling contract bytecode. This article is going to run you through them and in the process decompile an unverified MEV bot contract.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!trc_!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F30d29bfd-19f3-4c77-8b5d-bf0fdc1c603a_3456x2304.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!trc_!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F30d29bfd-19f3-4c77-8b5d-bf0fdc1c603a_3456x2304.jpeg 424w, https://substackcdn.com/image/fetch/$s_!trc_!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F30d29bfd-19f3-4c77-8b5d-bf0fdc1c603a_3456x2304.jpeg 848w, https://substackcdn.com/image/fetch/$s_!trc_!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F30d29bfd-19f3-4c77-8b5d-bf0fdc1c603a_3456x2304.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!trc_!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F30d29bfd-19f3-4c77-8b5d-bf0fdc1c603a_3456x2304.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!trc_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F30d29bfd-19f3-4c77-8b5d-bf0fdc1c603a_3456x2304.jpeg" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/30d29bfd-19f3-4c77-8b5d-bf0fdc1c603a_3456x2304.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:762708,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!trc_!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F30d29bfd-19f3-4c77-8b5d-bf0fdc1c603a_3456x2304.jpeg 424w, https://substackcdn.com/image/fetch/$s_!trc_!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F30d29bfd-19f3-4c77-8b5d-bf0fdc1c603a_3456x2304.jpeg 848w, https://substackcdn.com/image/fetch/$s_!trc_!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F30d29bfd-19f3-4c77-8b5d-bf0fdc1c603a_3456x2304.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!trc_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F30d29bfd-19f3-4c77-8b5d-bf0fdc1c603a_3456x2304.jpeg 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This is Part 2 of a series detailing <a href="https://twitter.com/mevalphaleak">MevAlphaLeak&#8217;s</a> first foray into the MEV arena. This article follows directly from <a href="https://noxx.substack.com/p/mev-memoirs-into-the-arena-chapter?s=r">Part 1</a> so if you haven&#8217;t read that yet I recomment you do so before continuing.</p><p>We left off in <a href="https://noxx.substack.com/p/mev-memoirs-into-the-arena-chapter?s=r">Part 1</a> having discovered the encoding of the callInfo variable within the ApeBot Contract and determined that 3 calls are made via the assembly code. These 3 calls were made to 2 unverified contracts.</p><p>Our only way to progress is to decompile these contracts to see whats going on.</p><h2>ApeBot Contract Calldata</h2><p>The image below shows the calldata for the original wfjizxua( ) call and how it is split up to be used as inputs for subsequent calls. This represents the majority of our findings from <a href="https://noxx.substack.com/p/mev-memoirs-into-the-arena-chapter?s=r">Part 1</a>.  </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!qRFX!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb1d9c084-6076-4110-abc3-ab2146abf6c7_3548x2752.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!qRFX!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb1d9c084-6076-4110-abc3-ab2146abf6c7_3548x2752.png 424w, https://substackcdn.com/image/fetch/$s_!qRFX!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb1d9c084-6076-4110-abc3-ab2146abf6c7_3548x2752.png 848w, https://substackcdn.com/image/fetch/$s_!qRFX!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb1d9c084-6076-4110-abc3-ab2146abf6c7_3548x2752.png 1272w, https://substackcdn.com/image/fetch/$s_!qRFX!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb1d9c084-6076-4110-abc3-ab2146abf6c7_3548x2752.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!qRFX!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb1d9c084-6076-4110-abc3-ab2146abf6c7_3548x2752.png" width="1200" height="930.4945054945055" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/b1d9c084-6076-4110-abc3-ab2146abf6c7_3548x2752.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:1129,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:818851,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!qRFX!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb1d9c084-6076-4110-abc3-ab2146abf6c7_3548x2752.png 424w, https://substackcdn.com/image/fetch/$s_!qRFX!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb1d9c084-6076-4110-abc3-ab2146abf6c7_3548x2752.png 848w, https://substackcdn.com/image/fetch/$s_!qRFX!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb1d9c084-6076-4110-abc3-ab2146abf6c7_3548x2752.png 1272w, https://substackcdn.com/image/fetch/$s_!qRFX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb1d9c084-6076-4110-abc3-ab2146abf6c7_3548x2752.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The 3 calls target the addresses <a href="https://etherscan.io/address/0xd380f7e1dc7408aa007744ed3af390f8a47f9b75">0xd380f7e1dc7408aa007744ed3af390f8a47f9b75</a> &amp; <a href="https://etherscan.io/address/0xf4863028b093fdac9cf7fd67c0df6866ac3c7a60">0xf4863028b093fdac9cf7fd67c0df6866ac3c7a60</a>, both of which are unverified contracts.</p><p>Before we begin let&#8217;s quickly clarify what an unverified contract is. On Etherscan when you create a contract you have the ability to verify it by submitting the code that created the contract. </p><p>This enables users to see the solidity code that created the contract. An official project will always do this to be as transparent as possible. When a contract isn&#8217;t verified you will instead see this.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!_2q5!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdbf93a72-73b5-4fe9-815a-16461f38e706_1367x407.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!_2q5!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdbf93a72-73b5-4fe9-815a-16461f38e706_1367x407.png 424w, https://substackcdn.com/image/fetch/$s_!_2q5!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdbf93a72-73b5-4fe9-815a-16461f38e706_1367x407.png 848w, https://substackcdn.com/image/fetch/$s_!_2q5!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdbf93a72-73b5-4fe9-815a-16461f38e706_1367x407.png 1272w, https://substackcdn.com/image/fetch/$s_!_2q5!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdbf93a72-73b5-4fe9-815a-16461f38e706_1367x407.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!_2q5!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdbf93a72-73b5-4fe9-815a-16461f38e706_1367x407.png" width="1200" height="357.2787125091441" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/dbf93a72-73b5-4fe9-815a-16461f38e706_1367x407.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:407,&quot;width&quot;:1367,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:160487,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!_2q5!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdbf93a72-73b5-4fe9-815a-16461f38e706_1367x407.png 424w, https://substackcdn.com/image/fetch/$s_!_2q5!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdbf93a72-73b5-4fe9-815a-16461f38e706_1367x407.png 848w, https://substackcdn.com/image/fetch/$s_!_2q5!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdbf93a72-73b5-4fe9-815a-16461f38e706_1367x407.png 1272w, https://substackcdn.com/image/fetch/$s_!_2q5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdbf93a72-73b5-4fe9-815a-16461f38e706_1367x407.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><a href="https://etherscan.io/address/0xd380f7e1dc7408aa007744ed3af390f8a47f9b75#code">0xd380f7e1dc7408aa007744ed3af390f8a47f9b75 Bytecode</a></figcaption></figure></div><p>You will only have access to the bytecode of the contract which is stored on-chain. You will have no solidity code to reference. As such it can be very hard to understand what is going on within the contract. </p><p>Our task is to decompile the bytecode for the unverified contracts to understand what happened in the ApeBot transaction. </p><h3>Tools &amp; Techniques</h3><p>Our investigation will use the following tools.</p><ul><li><p>Online decompiler</p><ul><li><p><a href="https://library.dedaub.com">https://library.dedaub.com</a> </p></li><li><p>There are a variety of other online decompilers</p><ul><li><p><a href="https://ethervm.io/decompile">https://ethervm.io/decompile</a></p></li><li><p><a href="https://github.com/eveem-org/panoramix">https://github.com/eveem-org/panoramix</a></p></li></ul></li></ul></li><li><p>Cast cli using --debug mode on the transaction hash</p><ul><li><p><a href="https://book.getfoundry.sh/cast/index.html">https://book.getfoundry.sh/cast/index.html</a></p></li><li><p>Useful for identitfying decompiled variable values, calldata input etc.</p></li><li><p><code>cast run --debug --rpc-url $ETH_RPC_URL 0x1126aa5e5b648eebad1c88141e5142cf0a4082e6ccf9fed77d69a190c21724a3</code></p></li></ul></li><li><p>Transaction visualisation</p><ul><li><p>https://versatile.blocksecteam.com/tx/eth/0x0efe3832b85e610fc4ba815f2be3943036a1a1be33cbd8f378a20d20667c1b39</p></li><li><p>The best transaction visualisation tool I&#8217;ve come across</p></li></ul></li><li><p>Etherscan</p><ul><li><p>Easy access to parity traces, contract byte code, sub calls etc.</p></li></ul></li></ul><p>The first step in decompiling the bytecode is to use an online decompiler to get a rough outline of the code. Note whatever the decompiler gives us will likely not be 100% accurate. There are occasions where the decompiler will give you incorrect information. This is why you must cross reference with the opcode dubugger.</p><p>The first task is to run through the decompiled code line by line and attempt to associate values and meaning to any variables declared. Here&#8217;s a list of the things to watch out / do when running through the code,</p><ul><li><p>If we see a variable that represents bytes 32 - 64 of the calldata, find those exact bytes and note it down in a comment next to that line so you know the actual value</p></li><li><p>If we see an address pop up in one of the variables, look it up on Etherscan to see if it&#8217;s a known address, does it have a verified contract etc.</p></li><li><p>If variables are being passed into verified contract functions we should be able to work out what those variables represent. Lookup it&#8217;s inputs in the docs / code.</p></li><li><p>Can we identify what each piece of calldata represents.</p></li><li><p>If a contract is called multiple times we should check a few transactions - sometimes the calldata won&#8217;t make sense till you look at multiple transcations.</p></li><li><p>What other contracts does the contract interact with, are they verified If they&#8217;re not we have repeat all these step on those contracts also. </p></li></ul><p>Now let&#8217;s run through these tools &amp; techniques one by one with the ApeBot example.</p><h3>Decompiler - Dedaub</h3><p>We&#8217;ll start with the decompiler, I&#8217;m using the Dedaub decompiler for this example.  Below is a snippet of the decompiled contract <a href="https://etherscan.io/address/0xd380f7e1dc7408aa007744ed3af390f8a47f9b75">0xd380f7e1dc7408aa007744ed3af390f8a47f9b75</a> that is involved in Call [1] &amp; Call [2]. </p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!VKrT!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1fbdcb5b-07c7-43f3-a533-b15f098f88ba_1554x262.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!VKrT!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1fbdcb5b-07c7-43f3-a533-b15f098f88ba_1554x262.png 424w, https://substackcdn.com/image/fetch/$s_!VKrT!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1fbdcb5b-07c7-43f3-a533-b15f098f88ba_1554x262.png 848w, https://substackcdn.com/image/fetch/$s_!VKrT!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1fbdcb5b-07c7-43f3-a533-b15f098f88ba_1554x262.png 1272w, https://substackcdn.com/image/fetch/$s_!VKrT!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1fbdcb5b-07c7-43f3-a533-b15f098f88ba_1554x262.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!VKrT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1fbdcb5b-07c7-43f3-a533-b15f098f88ba_1554x262.png" width="1456" height="245" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/1fbdcb5b-07c7-43f3-a533-b15f098f88ba_1554x262.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:245,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:64941,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!VKrT!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1fbdcb5b-07c7-43f3-a533-b15f098f88ba_1554x262.png 424w, https://substackcdn.com/image/fetch/$s_!VKrT!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1fbdcb5b-07c7-43f3-a533-b15f098f88ba_1554x262.png 848w, https://substackcdn.com/image/fetch/$s_!VKrT!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1fbdcb5b-07c7-43f3-a533-b15f098f88ba_1554x262.png 1272w, https://substackcdn.com/image/fetch/$s_!VKrT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1fbdcb5b-07c7-43f3-a533-b15f098f88ba_1554x262.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>The Dedaub decompiler uses a function signature lookup table to find any known functions that are called. We can see the address <a href="https://etherscan.io/address/0x8d12a197cb00d4747a1fe03395095ce2a5cc6819">0x8d12a197cb00d4747a1fe03395095ce2a5cc6819</a> has the &#8220;orderFills&#8221; &amp; &#8220;balanceOf&#8221; function. Let&#8217;s have a closer look at it.</p><p>Turns out <a href="https://etherscan.io/address/0x8d12a197cb00d4747a1fe03395095ce2a5cc6819">0x8d12a197cb00d4747a1fe03395095ce2a5cc6819</a> is the EtherDelta contract. It&#8217;s a verified contract which means we can look at the code for each of these functions.</p><p>A huge part of understanding unverified contracts is understanding the entities that they interact with. As such we need to take a quick detour to get an overview of what EtherDelta is and how it works.</p><h3>EtherDelta 2</h3><p>Etherdelta 2 is an on-chain orderbook exchange, this differs from an &#8220;Automated Market Maker&#8221; exchange (AMM).</p><p>An on-chain orderbook exchange acts much like a off-chain orderbook exchange ie Binance. There are Makers &amp; Takers rather than liquidity pools. A Maker order defines a asset to sell/buy for another asset at a certain price. </p><p>In EtherDelta&#8217;s case these &#8220;Maker&#8221; orders can be made on-chain via a transaction or by signing an order transaction on the EtherDelta site. </p><p>Note EtherDelta is no longer functional but I assume these signatures would have been stored in an off-chain DB somewhere and &#8220;Taker&#8221; orders could request the signature of the &#8220;Maker&#8221; order they want to take.</p><p>If a &#8220;Maker&#8221; order is made on-chain an event is emitted. I did a look-up of the event for the associated address with this ApeBot transaction on-chain and found nothing. As a result, we can infer that the &#8220;Maker&#8221; order involve in the ApeBot transaction was made via a signature.</p><p>Now we have a basic understanding of how EtherDelta works lets jump back to the code. Here&#8217;s the <a href="https://library.dedaub.com/contracts/Ethereum/D380f7e1dC7408AA007744ed3aF390F8a47F9b75/decompiled">full decompiled contract</a> for <a href="https://etherscan.io/address/0xd380f7e1dc7408aa007744ed3af390f8a47f9b75">0xd380f7e1dc7408aa007744ed3af390f8a47f9b75</a>. </p><p>Let&#8217;s start with the first call (Call [1]) to to function signature 0x78789120..</p><h3>Function 0x78789120 - Contract 0xd3&#8230;75 - Call [1] </h3><p>Below is the decompiled code for function 0x78789120. Snippets of the the EtherDelta 2 contract have also been included along with the parity traces for the internal calls (orderFills &amp; balanceOf). Let&#8217;s run through it piece by piece.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!eOoG!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd92a976a-ebff-478e-b51b-2bcc9d900ee9_3832x1672.png" data-component-name="Image2ToDOM"><div class="image2-inset image2-full-screen"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!eOoG!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd92a976a-ebff-478e-b51b-2bcc9d900ee9_3832x1672.png 424w, https://substackcdn.com/image/fetch/$s_!eOoG!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd92a976a-ebff-478e-b51b-2bcc9d900ee9_3832x1672.png 848w, https://substackcdn.com/image/fetch/$s_!eOoG!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd92a976a-ebff-478e-b51b-2bcc9d900ee9_3832x1672.png 1272w, https://substackcdn.com/image/fetch/$s_!eOoG!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd92a976a-ebff-478e-b51b-2bcc9d900ee9_3832x1672.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!eOoG!,w_5760,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd92a976a-ebff-478e-b51b-2bcc9d900ee9_3832x1672.png" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/d92a976a-ebff-478e-b51b-2bcc9d900ee9_3832x1672.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;full&quot;,&quot;height&quot;:635,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:652125,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-fullscreen" alt="" srcset="https://substackcdn.com/image/fetch/$s_!eOoG!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd92a976a-ebff-478e-b51b-2bcc9d900ee9_3832x1672.png 424w, https://substackcdn.com/image/fetch/$s_!eOoG!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd92a976a-ebff-478e-b51b-2bcc9d900ee9_3832x1672.png 848w, https://substackcdn.com/image/fetch/$s_!eOoG!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd92a976a-ebff-478e-b51b-2bcc9d900ee9_3832x1672.png 1272w, https://substackcdn.com/image/fetch/$s_!eOoG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd92a976a-ebff-478e-b51b-2bcc9d900ee9_3832x1672.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><ol><li><p>We start with a call to the orderFills function for address <a href="https://etherscan.io/address/0x8d12a197cb00d4747a1fe03395095ce2a5cc6819">0x8d12a197cb00d4747a1fe03395095ce2a5cc6819</a> which we know is the EtherDelta 2. OrderFills takes in varg4, varg5 and returns v0 &amp; v1. v0 represents whether the call was successful and v1 the actual returned result. The require of v0 after this call verifies the call was successful. We&#8217;ll use the parity traces to determinie varg4 &amp; varg5 but we can see they come from the calldata. </p></li><li><p>The Parity trace for this internal call, here we can see the input data (calldata) for this call</p><ol><li><p>0x19774d43</p><ol><li><p>Function Signature </p></li></ol></li><li><p>000000000000000000000000aac971235706aa7b49dd3cc2e42a9695d2060da0</p><ol><li><p>This is varg4 from above, it looks like an Ethereum Address, If we look it up on <a href="https://etherscan.io/address/0xaac971235706aa7b49dd3cc2e42a9695d2060da0">Etherscan</a> we can see it is an EOA address</p></li></ol></li><li><p>6ce7751bd5b9723aa3fa7e4fc4d97865cf912fce880a7797dc0a0da9508f31d9</p><ol><li><p>This is varg5 from above, at the moment we don&#8217;t know what the value represents but since this is an input to a function we can look it up in the verified EtherDelta contract.</p></li></ol></li></ol></li><li><p>If we check the orderFills code in the EtherDelta contract we can see orderFills is a mapping of a mapping. A &#8220;User Account&#8221; is mapped to an &#8220;Order Hash&#8221; which maps to a uint. This uint represents how much of an order has been filled. Using this new information we can determine that <a href="https://etherscan.io/address/0xaac971235706aa7b49dd3cc2e42a9695d2060da0">0xaac971235706aa7b49dd3cc2e42a9695d2060da0</a> is in fact the &#8220;Maker User Account Address&#8221; and that 0x6ce7751bd5b9723aa3fa7e4fc4d97865cf912fce880a7797dc0a0da9508f31d9 is actually the hash of an order on EtherDelta.</p></li><li><p>varg1 - v1, we know from the previous function that v1 represents the amount of an order that has been filled (Note this is in ETH terms). We can&#8217;t be sure yet but it&#8217;s likely that varg1 is the total order amount in ETH. The total order amount minus the filled amount would yield the remaining order amount. varg1 is equal to 0x00000000000000000000000000000000000000000000000000f0142dd1c52700 from our calldata and v1 can be found from the output of parity trace [ 2, 1, 0, 3, 0, 2, 1 ] (orderFills) which equals 0x0000000000000000000000000000000000000000000000000000000000000000</p></li><li><p>Call to EtherDelta 2 address on the balanceOf function, takes in varg2 &amp; varg4. Again we&#8217;ll use the parity traces.</p></li><li><p>Parity trace for this internal call, calldata</p><ol><li><p>0xf7888aec</p><ol><li><p>Function signature</p></li></ol></li><li><p>000000000000000000000000bb49a51ee5a66ca3a8cbe529379ba44ba67e6771</p><ol><li><p>This is varg2, again it appears to be an Ethereum address, looking it up on <a href="https://etherscan.io/address/0xbb49a51ee5a66ca3a8cbe529379ba44ba67e6771">Etherscan</a> shows it&#8217;s the <a href="https://etherscan.io/address/0xbb49a51ee5a66ca3a8cbe529379ba44ba67e6771">CST token address</a>.</p></li></ol></li><li><p>000000000000000000000000aac971235706aa7b49dd3cc2e42a9695d2060da0</p><ol><li><p>This is varg4 which we have already determined is the <a href="https://etherscan.io/address/0xaac971235706aa7b49dd3cc2e42a9695d2060da0">Maker User Account Address</a>.</p></li></ol></li></ol></li><li><p>The balanceOf function in the EtherDelta contract interacts with the tokens mapping. The function returns to us the balance of a specific token for a specific address. So we are checking the CST balance of the &#8220;Maker User Account&#8221;. Looking at the output from the Parity trace we can see that v5 is equal to  0x00000000000000000000000000000000000000000000005b95329a965952e83d</p></li><li><p>We can see some calculation, v5 * varg1 / varg3  = CST Balance * (Trade Amount in ETH / varg3 ). At this stage we are unsure of what varg3 represents. As a result it&#8217;s hard for us to determine what this is trying to calculate.</p><ol><li><p>This is a good example of where the next call (Call [2]) will aid us in detremining this calculation. For now I&#8217;m going fill you in on what varg3 is but watch out for when we discover this piece of info in the next call. In reality we would of had to loop back to this item once we had the new info. </p></li><li><p>varg3 tells us the CST amount on the Maker order. The Maker is willing to swap X CST for Y ETH. varg3 is the X. The calculation above uses X &amp; Y to get the trade ratio 1/2500 ETH/CST. This value is multipled by the Makers CST balance. Therefore the result of this calculation, v5, is the Makers CST balance in ETH terms using the trade ratio as the converstion rate.  </p></li></ol></li><li><p>v3 &lt; v6 = 0xf0142dd1c52700 &lt; 0xf0142dd1dcb4be checks whether the trade amount in ETH is less than the Makers balance in ETH. This ensures the Maker is good to fulfill the trade. In our case this returns True. We can see this If statement has no code inside of it, when looking through the opcodes you can see there is a JUMPI based on this result. It is likely if this condition is not meet the execution will revert. You can confirm this using foundry with --debug &amp; --fork-block-number to create transactions that would explore this section of the code and look at the opcodes that are called in different scenarios. </p></li><li><p>v2 &lt; varg6 = 0xf0142dd1c52700 &lt; 0x05 checks whether the Maker trade amount in ETH is less than the amount we want to trade in ETH. Note that v2 refers to its definition on line 15 rather than line 21 another thing to watch out for with decompiled code. This check makes sense, it&#8217;s verifying the open trade amount it big enough to fill our order. In our case, this returns false so we jump to the else statement. </p></li><li><p>Line 27 is not called in the flow of the ApeBot transaction however it is interesting to note that byte 2 of calldata item [0] in our case 0x4b appears to be some custom encoding. It is used when the balance of the maker trade amount is less than the amount we would like to trade.</p></li><li><p>v2  is set to 4200, unsure of what this value represents. The returned value isn&#8217;t used in any subsequent calls of the ApeBot transaction.</p></li><li><p>Returns 2 values. The 2 v2&#8217;s again represent different values, we can always verify returned values via the parity traces.</p><ol><li><p>v2, v2 = 0x05, 0x1068</p></li></ol></li></ol><p>This shows us this first call (Call [1]) is ultimately about checking the validity of the trade ie is it possible. </p><p>A number of the calldata items weren&#8217;t used in this function call or were used without context ie varg3. As such we weren&#8217;t able to determine their meaning.</p><p>Let&#8217;s now look at Call [2] which calls the same contract <a href="https://etherscan.io/address/0xd380f7e1dc7408aa007744ed3af390f8a47f9b75">0xd380f7e1dc7408aa007744ed3af390f8a47f9b75</a> and see if it can shed any light on these unknown pieces of calldata.</p><h3>Function 0x401687f4 - Contract 0xd3&#8230;75 - Call [2]</h3><p>Let&#8217;s quickly refer back to the calldata from Call [1] and Call [2] at the top of the article. You&#8217;ll notice they are the same. Below is the decompiled code for function 0x401687f4, let&#8217;s dive in.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!llgl!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fff5a8e2f-2535-40ae-b6d0-937266d6ed49_3228x2708.png" data-component-name="Image2ToDOM"><div class="image2-inset image2-full-screen"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!llgl!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fff5a8e2f-2535-40ae-b6d0-937266d6ed49_3228x2708.png 424w, https://substackcdn.com/image/fetch/$s_!llgl!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fff5a8e2f-2535-40ae-b6d0-937266d6ed49_3228x2708.png 848w, https://substackcdn.com/image/fetch/$s_!llgl!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fff5a8e2f-2535-40ae-b6d0-937266d6ed49_3228x2708.png 1272w, https://substackcdn.com/image/fetch/$s_!llgl!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fff5a8e2f-2535-40ae-b6d0-937266d6ed49_3228x2708.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!llgl!,w_5760,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fff5a8e2f-2535-40ae-b6d0-937266d6ed49_3228x2708.png" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/ff5a8e2f-2535-40ae-b6d0-937266d6ed49_3228x2708.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;full&quot;,&quot;height&quot;:1221,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:866049,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-fullscreen" alt="" srcset="https://substackcdn.com/image/fetch/$s_!llgl!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fff5a8e2f-2535-40ae-b6d0-937266d6ed49_3228x2708.png 424w, https://substackcdn.com/image/fetch/$s_!llgl!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fff5a8e2f-2535-40ae-b6d0-937266d6ed49_3228x2708.png 848w, https://substackcdn.com/image/fetch/$s_!llgl!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fff5a8e2f-2535-40ae-b6d0-937266d6ed49_3228x2708.png 1272w, https://substackcdn.com/image/fetch/$s_!llgl!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fff5a8e2f-2535-40ae-b6d0-937266d6ed49_3228x2708.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><ol><li><p>The 0x401687f4 Function call another internal function 0x74 and passes in the value 4</p></li><li><p>v0 - v11 are declared using the calldata in 32 byte chunks all with a 4 byte offset. The offset comes from, varg0 = 4, which represents the 4 byte function signature in the calldata.</p></li><li><p>A check is done to see if there is a non-zero address in the v0 = calldata item [0]. Note the zero address 0x000&#8230;00 represents ETH in EtherDelta so this is effectively asking is the trade in ETH or an ERC20. In our case we have a zero address so we wouldn&#8217;t enter this chunk of code. Let&#8217;s have a look at what it does anyway. After we enter theres a check on the first byte of v0. If it is zero it indicates the token hasn&#8217;t been approved for this contract.  </p></li><li><p>This section calls the ERC20 contract for the given token and checks if EtherDelta (0x8d1&#8230;19) has any allowance using the allowance( ) function. If it doesn&#8217;t it calls the approve( ) function on the ERC20 contract with the EtherDelta address. This allows EtherDelta to move your ERC20 token without any further approval.</p></li><li><p>Next it checks the balance of the ApeBot contract (Since its a delegate call address(this) is the parent caller) for the given ERC20 token by calling balanceOf on the token contract address. It then deposits the full amount (which came from balanceOf) into EtherDelta minus 1 uint of the token. This minus 1 is a gas saving optimisation for future calls. It&#8217;s cheaper to change a slot from a non-zero value to another non-zero value than it is to change from a zero value to a non-zero value. </p></li><li><p>Now we&#8217;re back to the flow the ApeBot transaction took, this also does an EtherDelta deposit however uses v11 (calldata [11]) = 0x05 as the amount to deposit (5 Wei).</p></li><li><p>The EtherDelta trade function, I&#8217;ve included a snippet from the verified contract to show you the function inputs. A lot of the inputs we use are from our calldata, this gives us a chance to work out what each piece of calldata represents. Below is the list of inputs.</p><ol><li><p>TokenGet (calldata [0]) = This is from the makers perspective so the token they would like to get is ETH.</p></li><li><p>AmountGet (calldata [1]) = The amount the maker would like to recieve in ETH for the full trade.</p></li><li><p>TokenGive (calldata [2]) =  Again this is from the makers perspective so the token they would like to give is CST.</p></li><li><p>AmountGive (calldata [3]) = The amount the maker would like to give in CST for the full trade. </p><ol><li><p>At this stage we can work out what the trade value was by looking at the ratio of CST/ETH = 0x5b95329a8d5d289800/0xf0142dd1c52700 = 1689404535900000000000/67576181436000000 = 2500 CST &#8594; 1 ETH</p></li><li><p>This is when we could refer back to varg3 in Call [1] to determine what the calculation was doing</p></li></ol></li><li><p>Expires (calldata [4]) = The block number the order expires in. After this block number, the order can no longer trade.</p></li><li><p>Nonce (calldata [5]) = Used by EtherDelta, the nonce is a number you can include with your order to make it relatively unique. This way, if you want to place two otherwise identical orders, they won't have the same hash.</p></li><li><p>User (calldata [6]) = Maker&#8217;s Ethereum address</p></li><li><p>V (calldata [7]) =  ECDSA signature component - used to verify the proposed trade is legitimate ie has been signed by the Maker</p></li><li><p>R (calldata [8]) = ECDSA signature component -  used to verify the proposed trade is legitimate ie has been signed by the Maker</p></li><li><p>S (calldata [9]) = ECDSA signature component -  used to verify the proposed trade is legitimate ie has been signed by the Maker</p></li><li><p>Amount = Amount the taker would like to trade, note the amount is in amountGet terms which in our case is ETH. The value here is 0x03 which is different from our 0x05 calldata. You&#8217;ll notice 0xde0b6b3a7640000/0xdeb5f2f95b78000 = 1000000000000000000/1003000000000000000 = ~0.997 the taker fee for a trade is 0.3%. The calculation above takes your trade input and multiples it by 0.997 so you have enough left over to pay the 0.3% taker fee. 5 Wei * 0.997 = 4.985 but Ethereum doesn&#8217;t have floating point numbers so we round down to 4. We then minus one to give us 3 Wei (The minus one is again is related to gas savings).  </p></li></ol></li><li><p>Finally after the trade is made we withdraw our funds. WithdrawToken( ) is used for ERC20 withdraws while withdraw( ) is used for ETH. Since v2 is the CST address we&#8217;ll use WithdrawToken( ) again there is a minus one for gas savings.</p></li></ol><p>Now we have gone through both Call [1] and Call [2] we are able to determine what the calldata to the original call represents. Below is that calldata for Call [1] &amp; [2] along with what each value represents.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!WeuQ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F00db9003-17e5-4207-ad37-32e424476fb8_2658x1228.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!WeuQ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F00db9003-17e5-4207-ad37-32e424476fb8_2658x1228.png 424w, https://substackcdn.com/image/fetch/$s_!WeuQ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F00db9003-17e5-4207-ad37-32e424476fb8_2658x1228.png 848w, https://substackcdn.com/image/fetch/$s_!WeuQ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F00db9003-17e5-4207-ad37-32e424476fb8_2658x1228.png 1272w, https://substackcdn.com/image/fetch/$s_!WeuQ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F00db9003-17e5-4207-ad37-32e424476fb8_2658x1228.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!WeuQ!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F00db9003-17e5-4207-ad37-32e424476fb8_2658x1228.png" width="1200" height="554.6703296703297" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/00db9003-17e5-4207-ad37-32e424476fb8_2658x1228.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:673,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:279480,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!WeuQ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F00db9003-17e5-4207-ad37-32e424476fb8_2658x1228.png 424w, https://substackcdn.com/image/fetch/$s_!WeuQ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F00db9003-17e5-4207-ad37-32e424476fb8_2658x1228.png 848w, https://substackcdn.com/image/fetch/$s_!WeuQ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F00db9003-17e5-4207-ad37-32e424476fb8_2658x1228.png 1272w, https://substackcdn.com/image/fetch/$s_!WeuQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F00db9003-17e5-4207-ad37-32e424476fb8_2658x1228.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This concludes the EtherDelta leg of the trade let&#8217;s recap what happened.</p><h4>EtherDelta Leg</h4><ul><li><p>0xd380f7e1dc7408aa007744ed3af390f8a47f9b75 - Call [1]</p><ul><li><p>Check to make sure the trade hasn&#8217;t been filled</p><ul><li><p>0 ETH of the 0.067576181436 ETH trade had been filled</p></li><li><p>Therefore 0.067576181436 ETH of the trade still available</p></li></ul></li><li><p>Check to make sure that the Maker address has sufficent tokens in their EtherDelta balance to make the trade (in ETH terms) </p><ul><li><p>Maker (0xaac971235706aa7b49dd3cc2e42a9695d2060da0) had a balance of 1689.404535938590369853 CST</p></li><li><p>Convert to ETH terms base on trade ratio (1:2500) yields 0.067576181437543614 ETH</p></li><li><p>Open trade in ETH &lt; Maker Balance in ETH</p></li><li><p>0.067576181436 ETH &lt; 0.067576181437543614 ETH = True</p></li><li><p>Therefore Maker is has enough funds to cover the trade</p></li></ul></li><li><p>Check to make sure the proposed trade amount by the taker is less than the open Maker trade amount in ETH </p><ul><li><p>Open trade in ETH &lt; Proposed Taker trade in ETH</p></li><li><p>0.067576181436 ETH &lt; 0.000000000000000005 ETH = False</p></li><li><p>Therefore we will be able to execute the trade for our Taker amount</p></li></ul></li></ul></li><li><p>0xd380f7e1dc7408aa007744ed3af390f8a47f9b75 - Call [2]</p><ul><li><p>Check if the Taker trade is in an ERC20 token or ETH, for us it&#8217;s ETH</p></li><li><p> Desposit ETH into EtherDelta from the MevAlphaLeak contract for the trade</p><ul><li><p>5 Wei deposited</p></li></ul></li><li><p>Excute trade of ETH for CST</p><ul><li><p>Proposed trade amount 5 Wei is used in a calculation to determine the amount put into the trade function</p><ul><li><p>((5 * 1000000000000000000) / 1003000000000000000) - 1</p></li><li><p>Yields 3.9850448654, no floating point numbers so rounds down to 3</p></li></ul></li><li><p>3 Wei is swapped for 0.000000000000075 CST</p></li><li><p>Taker fee is (0.000000000000000003 * 3000000000000000) / 1000000000000000000 this yields 9e-21</p><ul><li><p>1 Wei = 1e-18 ETH, you cannot go lower than 1 Wei &amp; there are no floating point numbers in the EVM so this rounds down to 0</p></li><li><p>Therefore no taker fee is paid</p></li></ul></li></ul></li><li><p>Check CST balance of ApeBot contract on EtherDelta post trade</p><ul><li><p>Balance returns 0.000000000000075 CST</p></li></ul></li><li><p>Withdraw that CST balance from EtherDelta to MevAlphaLeak contract for next leg of the arb</p><ul><li><p>Withdraws 0.00000000000007499 CST</p></li></ul></li></ul></li></ul><p>There we have it, I&#8217;ve taken you through the first 2 calls and shown you the tools &amp; techniques. For those interested in a challenge see if you can decompile the final piece of the puzzle, Call [2] to <a href="https://etherscan.io/address/0xf4863028b093fdac9cf7fd67c0df6866ac3c7a60">0xf4863028b093fdac9cf7fd67c0df6866ac3c7a60</a> which interacts with Uniswap V2. Here&#8217;s the <a href="https://library.dedaub.com/contracts/Ethereum/f4863028b093fdac9cf7fd67c0df6866ac3c7a60/decompiled?line=55">decompiled contract</a>.</p><p>Hope you enjoyed, now go unearth some alpha anon!</p><p>noxx</p><p>Twitter <a href="https://twitter.com/noxx3xxon">@noxx3xxon</a></p>]]></content:encoded></item><item><title><![CDATA[MEV Memoirs: Into the Arena - Chapter 1, Part 1 🤖]]></title><description><![CDATA[From Nothing to Something - How MevAlphaLeak entered the MEV arena]]></description><link>https://noxx.substack.com/p/mev-memoirs-into-the-arena-chapter</link><guid isPermaLink="false">https://noxx.substack.com/p/mev-memoirs-into-the-arena-chapter</guid><dc:creator><![CDATA[noxx]]></dc:creator><pubDate>Sun, 29 May 2022 18:07:03 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/h_600,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F33b1929c-cd1a-4851-8c86-8b64ba4bdbcb_3142x1774.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>It&#8217;s April 2021, MEV Searchers roam the chain looking for their fortunes, snipers are everywhere, the PvP battles are heating up.</p><p>A new entrant is about to enter the fray.</p><p>On block 12342861 a Searcher appeared from the shadows. He came with nothing and took his first shot. </p><p>This is the story of how <a href="https://twitter.com/mevalphaleak">MevAlphaLeak</a> entered the MEV arena. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Tomk!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F711324ce-3f72-452a-a443-be8fa9d53196_2048x1152.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Tomk!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F711324ce-3f72-452a-a443-be8fa9d53196_2048x1152.jpeg 424w, https://substackcdn.com/image/fetch/$s_!Tomk!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F711324ce-3f72-452a-a443-be8fa9d53196_2048x1152.jpeg 848w, https://substackcdn.com/image/fetch/$s_!Tomk!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F711324ce-3f72-452a-a443-be8fa9d53196_2048x1152.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!Tomk!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F711324ce-3f72-452a-a443-be8fa9d53196_2048x1152.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Tomk!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F711324ce-3f72-452a-a443-be8fa9d53196_2048x1152.jpeg" width="1456" height="819" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/711324ce-3f72-452a-a443-be8fa9d53196_2048x1152.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:819,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Tomk!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F711324ce-3f72-452a-a443-be8fa9d53196_2048x1152.jpeg 424w, https://substackcdn.com/image/fetch/$s_!Tomk!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F711324ce-3f72-452a-a443-be8fa9d53196_2048x1152.jpeg 848w, https://substackcdn.com/image/fetch/$s_!Tomk!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F711324ce-3f72-452a-a443-be8fa9d53196_2048x1152.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!Tomk!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F711324ce-3f72-452a-a443-be8fa9d53196_2048x1152.jpeg 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Source: <a href="https://imgur.com/X7lsZet">Two Sides of the Battle</a></figcaption></figure></div><h2>Through The Eyes of a Searcher</h2><p>The exploration of this story and subsequent deep dive gives us a chance to see the world through the Searcher&#8217;s eyes at that point in time. </p><p>Our task is the same as a mechanic who is taking apart a car to see how it works. Through the exercise of reverse-engineering the parts, the mechanic can begin to understand the thought process of the engineer/designer.</p><p>This is far more valuable than simply copying a strategy. As you collect these searcher perspectives you can then build &amp; innovate on the ideas that have come before you.</p><h2>What Happened?</h2><p>We&#8217;ll start with a high-level overview of what happened in block 12342861 and then zoom in on a specific part of the deployed contract for the deep dive.</p><p>MevAlphaLeak&#8217;s first transactions are located at the top of block 12342861, let&#8217;s start there for the investigation.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!D-Wm!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Feddab5b3-8795-4d9d-a18f-43dfc62044e9_3568x3506.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!D-Wm!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Feddab5b3-8795-4d9d-a18f-43dfc62044e9_3568x3506.png 424w, https://substackcdn.com/image/fetch/$s_!D-Wm!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Feddab5b3-8795-4d9d-a18f-43dfc62044e9_3568x3506.png 848w, https://substackcdn.com/image/fetch/$s_!D-Wm!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Feddab5b3-8795-4d9d-a18f-43dfc62044e9_3568x3506.png 1272w, https://substackcdn.com/image/fetch/$s_!D-Wm!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Feddab5b3-8795-4d9d-a18f-43dfc62044e9_3568x3506.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!D-Wm!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Feddab5b3-8795-4d9d-a18f-43dfc62044e9_3568x3506.png" width="1200" height="1179.3956043956043" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/eddab5b3-8795-4d9d-a18f-43dfc62044e9_3568x3506.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:1431,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:698647,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!D-Wm!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Feddab5b3-8795-4d9d-a18f-43dfc62044e9_3568x3506.png 424w, https://substackcdn.com/image/fetch/$s_!D-Wm!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Feddab5b3-8795-4d9d-a18f-43dfc62044e9_3568x3506.png 848w, https://substackcdn.com/image/fetch/$s_!D-Wm!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Feddab5b3-8795-4d9d-a18f-43dfc62044e9_3568x3506.png 1272w, https://substackcdn.com/image/fetch/$s_!D-Wm!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Feddab5b3-8795-4d9d-a18f-43dfc62044e9_3568x3506.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><ol><li><p>This section shows the <a href="https://etherscan.io/txs?block=12342861&amp;p=4">first 2 transactions from block 12342861 on Etherscan</a>. The first creates an ApeBot Contract and the second interacts with that contract. We can see that mevalphaleak.eth is the EOA that initiated the transactions. Note mevalphaleak.eth was a brand new address with 0 ETH in the account.  </p></li><li><p>Shows the created ApeBot Contract on Etherscan. The contract address is <a href="https://etherscan.io/address/0x666f80a198412bCb987c430831B57AD61facB666">0x666f80a198412bCb987c430831B57AD61facB666</a>. Note that the contract source code has been verified so we can see the <a href="https://etherscan.io/address/0x666f80a198412bCb987c430831B57AD61facB666#code">solidity code</a>. Typically searchers don&#8217;t verify their contracts to obfuscate what is going on.</p></li><li><p>The second transaction in the block interacts with the ApeBot contract. Note that the &#8220;Interacted with (To)&#8221; section has the ApeBot contract address <a href="https://etherscan.io/address/0x666f80a198412bCb987c430831B57AD61facB666">0x666f80a198412bCb987c430831B57AD61facB666</a> as its entry point.</p></li><li><p>By looking at tokens transferred we get a rough idea of what happened. A flashloan was taken from dYdX and then used in an arbitrage opportunity between EtherDelta 2 &amp; Uniswap V2 for the CST token. We can see the flashloan is paid back at the end.</p></li><li><p>There are two transfers at the end of the transaction that are interesting. The first to &#8220;Spark Pool&#8221; (<a href="https://etherscan.io/address/0x5A0b54D5dc17e0AadC383d2db43B0a0D3E029c4c">0x5A0b54D5dc17e0AadC383d2db43B0a0D3E029c4c</a>), a big Ethereum Mining Entity for a value of ~ 0.4497 ETH. The second to <a href="https://etherscan.io/address/0x90102a92e8E40561f88be66611E5437FEb339e79">0x90102a92e8E40561f88be66611E5437FEb339e79</a>, which corresponds to the ENS mevalphaleak.eth for ~0.0004 ETH. You&#8217;ll soon find out the relevance of these transactions.</p></li><li><p>The Gas Price of the transaction which is set to zero (The contract creation transaction also had a gas price of zero). At this point you should be asking why any miner would accept a zero gas transaction.    </p></li></ol><p>Why a miner would accept those transactions relates to the transfer of ~ 0.4497 ETH in [ 5 ] to Spark Pool. Let&#8217;s take a look at who was the miner for block 12342861.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!L-G_!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fcf5a640a-1a43-42e9-af93-6e490930eea7_1654x398.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!L-G_!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fcf5a640a-1a43-42e9-af93-6e490930eea7_1654x398.png 424w, https://substackcdn.com/image/fetch/$s_!L-G_!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fcf5a640a-1a43-42e9-af93-6e490930eea7_1654x398.png 848w, https://substackcdn.com/image/fetch/$s_!L-G_!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fcf5a640a-1a43-42e9-af93-6e490930eea7_1654x398.png 1272w, https://substackcdn.com/image/fetch/$s_!L-G_!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fcf5a640a-1a43-42e9-af93-6e490930eea7_1654x398.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!L-G_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fcf5a640a-1a43-42e9-af93-6e490930eea7_1654x398.png" width="1456" height="350" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/cf5a640a-1a43-42e9-af93-6e490930eea7_1654x398.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:350,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:54081,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!L-G_!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fcf5a640a-1a43-42e9-af93-6e490930eea7_1654x398.png 424w, https://substackcdn.com/image/fetch/$s_!L-G_!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fcf5a640a-1a43-42e9-af93-6e490930eea7_1654x398.png 848w, https://substackcdn.com/image/fetch/$s_!L-G_!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fcf5a640a-1a43-42e9-af93-6e490930eea7_1654x398.png 1272w, https://substackcdn.com/image/fetch/$s_!L-G_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fcf5a640a-1a43-42e9-af93-6e490930eea7_1654x398.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>We can see that block 12342862 was mined by &#8220;Spark Pool&#8221;. The same &#8220;Spark Pool&#8221; that was paid out in the second mevalphaleak.eth transaction. What is going on that allows a 0 ETH account to make multiple transactions at 0 zero gas? </p><p>The secret is that these 2 transactions didn&#8217;t go the traditional route of into mempool to be picked up by a miner. Instead, they went via the flashbots relay.</p><h2>Flashbots Relay  </h2><p>I won&#8217;t go too much into the details of the Flashbots Relay however will provide a brief synopsis for the purpose of this article. </p><p>When searchers are executing MEV strategies they often need to execute multiple transactions one after the other and they usually want to be at the top of the block.</p><p>Their search is based on the state of the world at block n-1 where n is the block they want to be included in. An Ethereum block can have hundreds of transactions each of which changes the world state.</p><p>Imagine &#8220;Searcher X&#8221; &amp; &#8220;Searcher Y&#8221; seeing the same arbitrage opportunity and both submitting arbitrage transactions. When the transactions of a block are executed they are executed procedurally (in order). </p><p>If Searcher X&#8217;s transaction is above Searcher Y&#8217;s then when the node gets to computing Searcher Y&#8217;s transaction the Ethereum state will have changed. The arbitrage opportunity will no longer be there and their transaction with either revert or be unprofitable.</p><p>This is why it&#8217;s so important to have control over the order of transactions in a block.</p><p>As I&#8217;m sure you know transaction ordering in a block is an essential component of MEV. Before the Flashbots relay, this was typically done via PGA&#8217;s (Priority Gas Auctions) or by being a miner.</p><p>Flashbots saw this problem and looked to create a solution. They altered the Geth codebase to create mev-geth an Ethereum client that enabled searchers to submit bundles of transactions to the miners via the Flashbots Relay. </p><p>The Miner would look at which proposal gave them the biggest reward and place this bundle at the top of the block. This reward could be paid out in gas or with a &#8220;coinbase.transfer&#8221; which sends the specified amount of ETH to the miner of the block.</p><p>Unlike the mempool the relay is private to everyone except Flashbots. You are able to sign mutliple transactions/bundles and you won&#8217;t pay any gas unless they are executed.  </p><p>A cool feature of the <a href="https://blocks.flashbots.net/">Flashbots Relay is their API</a> which shows us for each block  whether any bundles were included via the relay.</p><p>Let&#8217;s take a look at block 12342861. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Ppl2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff08eb801-bddb-45ac-a0c5-bc1288911b93_2048x1972.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Ppl2!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff08eb801-bddb-45ac-a0c5-bc1288911b93_2048x1972.png 424w, https://substackcdn.com/image/fetch/$s_!Ppl2!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff08eb801-bddb-45ac-a0c5-bc1288911b93_2048x1972.png 848w, https://substackcdn.com/image/fetch/$s_!Ppl2!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff08eb801-bddb-45ac-a0c5-bc1288911b93_2048x1972.png 1272w, https://substackcdn.com/image/fetch/$s_!Ppl2!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff08eb801-bddb-45ac-a0c5-bc1288911b93_2048x1972.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Ppl2!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff08eb801-bddb-45ac-a0c5-bc1288911b93_2048x1972.png" width="1456" height="1402" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/f08eb801-bddb-45ac-a0c5-bc1288911b93_2048x1972.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1402,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:305460,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Ppl2!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff08eb801-bddb-45ac-a0c5-bc1288911b93_2048x1972.png 424w, https://substackcdn.com/image/fetch/$s_!Ppl2!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff08eb801-bddb-45ac-a0c5-bc1288911b93_2048x1972.png 848w, https://substackcdn.com/image/fetch/$s_!Ppl2!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff08eb801-bddb-45ac-a0c5-bc1288911b93_2048x1972.png 1272w, https://substackcdn.com/image/fetch/$s_!Ppl2!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff08eb801-bddb-45ac-a0c5-bc1288911b93_2048x1972.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>We can see that MevAlphaLeaks transactions went via the Flashbots Relay which explains why he was able to use a 0 ETH address and the coinbase.transfer to Spark Pool.</p><p>Ok, let&#8217;s do a quick review of what we&#8217;ve learned so far before the deep dive.</p><ul><li><p>MevAlphaLeak submitted a bundle of transactions via the flashbots relay</p></li><li><p>The first transaction created the ApeBot contract </p></li><li><p>The second transaction interacted with the ApeBot contract to execute an arbitrage</p></li><li><p>The arbitrage did the following (Note I have ommitted the WETH / ETH conversions that take place throughout the arbitrage)</p><ul><li><p>Flashloan From dYdX of WETH</p></li><li><p>Exchange WETH for CST on EtherDelta 2</p></li><li><p>Exchange CST for ETH on Uniswap V2</p></li><li><p>Pay back WETH from dYdX Flashloan</p></li></ul></li><li><p>At the end of the second transaction, MevAlphaLeak pays the miner via the conbase.transfer method 0<strong>.</strong>449293093766000097 ETH</p></li><li><p>MevAlphaLeak leaves with a contract deployed for free and &nbsp;0<strong>.</strong>000449742836602602 ETH</p></li></ul><p>Now let&#8217;s take a deep dive into the ApeBot contract.</p><p><em>A warning, the next section is heavy on the code side and assumes a fair amount of EVM knowledge from the reader.</em></p><h2>ApeBot Contract </h2><p>Now we will investigate the ApeBot Contract. We&#8217;ll briefly touch on the entry point of the contract and then zoom in on the block of assembly code that executes the arbitrage.</p><p>We can start with a high-level overview of the contract flow and identify where the assembly code we&#8217;d like to inspect is located. Note all the code snippets come from the <a href="https://etherscan.io/address/0x666f80a198412bcb987c430831b57ad61facb666#code">verified contract on Etherscan</a> so the line numbers should match up with that.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!PIjw!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff2f918a2-552d-4098-82a8-1c614dc93fb5_1802x1548.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!PIjw!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff2f918a2-552d-4098-82a8-1c614dc93fb5_1802x1548.png 424w, https://substackcdn.com/image/fetch/$s_!PIjw!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff2f918a2-552d-4098-82a8-1c614dc93fb5_1802x1548.png 848w, https://substackcdn.com/image/fetch/$s_!PIjw!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff2f918a2-552d-4098-82a8-1c614dc93fb5_1802x1548.png 1272w, https://substackcdn.com/image/fetch/$s_!PIjw!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff2f918a2-552d-4098-82a8-1c614dc93fb5_1802x1548.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!PIjw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff2f918a2-552d-4098-82a8-1c614dc93fb5_1802x1548.png" width="1456" height="1251" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/f2f918a2-552d-4098-82a8-1c614dc93fb5_1802x1548.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1251,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:161377,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!PIjw!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff2f918a2-552d-4098-82a8-1c614dc93fb5_1802x1548.png 424w, https://substackcdn.com/image/fetch/$s_!PIjw!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff2f918a2-552d-4098-82a8-1c614dc93fb5_1802x1548.png 848w, https://substackcdn.com/image/fetch/$s_!PIjw!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff2f918a2-552d-4098-82a8-1c614dc93fb5_1802x1548.png 1272w, https://substackcdn.com/image/fetch/$s_!PIjw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff2f918a2-552d-4098-82a8-1c614dc93fb5_1802x1548.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><ol><li><p>We start by calling wfjizxua(uint256,uint256[]). If you&#8217;re wondering what is with the strange name try hashing this to get its function signature. It returns the signature 0x00000000 which is more gas efficient than having a function signature that is non zero (A zero byte in calldata costs 4 gas whereas a non-zero byte costs 16 gas). We pass in actionFlags &amp; actionData to the function.</p></li><li><p>This data (actionFlags &amp; actionData) is in turn passed to ape(uint256,uint256[]). This is a permissionless function that allows a user to execute any arbitrary logic. This means you can have a single contract and target various DEX&#8217;s based on your call data rather than writing custom contracts for each. You&#8217;ll see how this function is generalised later in the article.</p></li><li><p>The &#8220;data&#8221; variable in the ape function which corresponds to the &#8220;actionData&#8221; from the original wfjizxua call is looped over. Some assembly code is executed on each loop. Let&#8217;s see what happens.</p></li></ol><p>First, we need to look at the calldata for the ApeBot contract call.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!VAvG!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf63e0e2-28b8-4424-84c6-8cd37087c5ed_1954x1924.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!VAvG!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf63e0e2-28b8-4424-84c6-8cd37087c5ed_1954x1924.png 424w, https://substackcdn.com/image/fetch/$s_!VAvG!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf63e0e2-28b8-4424-84c6-8cd37087c5ed_1954x1924.png 848w, https://substackcdn.com/image/fetch/$s_!VAvG!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf63e0e2-28b8-4424-84c6-8cd37087c5ed_1954x1924.png 1272w, https://substackcdn.com/image/fetch/$s_!VAvG!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf63e0e2-28b8-4424-84c6-8cd37087c5ed_1954x1924.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!VAvG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf63e0e2-28b8-4424-84c6-8cd37087c5ed_1954x1924.png" width="1456" height="1434" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/df63e0e2-28b8-4424-84c6-8cd37087c5ed_1954x1924.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1434,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:385469,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!VAvG!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf63e0e2-28b8-4424-84c6-8cd37087c5ed_1954x1924.png 424w, https://substackcdn.com/image/fetch/$s_!VAvG!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf63e0e2-28b8-4424-84c6-8cd37087c5ed_1954x1924.png 848w, https://substackcdn.com/image/fetch/$s_!VAvG!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf63e0e2-28b8-4424-84c6-8cd37087c5ed_1954x1924.png 1272w, https://substackcdn.com/image/fetch/$s_!VAvG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf63e0e2-28b8-4424-84c6-8cd37087c5ed_1954x1924.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>How are we to decipher what this data means? We can start with the function signature that tells us a uint256 &amp; unit256[ ] is passed in. </p><p>A unit256[ ] is a dynamic type. The <a href="https://docs.soliditylang.org/en/develop/abi-spec.html#use-of-dynamic-types">encoding for dynamic types is different from static types</a>. For static types like &#8220;uint256 actionFlags&#8221; we pass the values directly into the calldata. We can see this value at calldata item [0] in the image above.</p><p>For dynamic types like &#8220;unit256[] actionData&#8221;, we instead pass in the offset in bytes to the start of their data area, measured from the start of the value encoding. In our case, this can be seen at calldata item [1] and has a value 0x40. This is equal to 64 in decimal and tells us the data area for the array starts 64 bytes into our calldata. </p><p>An offset of 64 bytes means calldata item [2] signals the start of the data area. The first item in this data area [2] declares the length of the array, 0x25 = 37 decimal. </p><p>After this, we have 37 items from [3] to [39]. These items are the actual values in the uint256[ ] array.</p><p>These groupings are useful but we still have no idea how this calldata is used. To determine this we need to look at the assembly code and work backward from there.</p><p>I&#8217;ve highlighted in green the first item in the actual array, calldata [3], it will be apparent why when we look into the assembly code.</p><h3>Assembly Code</h3><p>What better place to start than the first line of the assembly code. The image below highlights this (line 219) along with some excerpts from the <a href="https://github.com/foundry-rs/foundry">foundry</a> debugger.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!jzUE!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F23449bad-9c3f-4b24-806d-1f66b6e737f8_1804x1450.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!jzUE!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F23449bad-9c3f-4b24-806d-1f66b6e737f8_1804x1450.png 424w, https://substackcdn.com/image/fetch/$s_!jzUE!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F23449bad-9c3f-4b24-806d-1f66b6e737f8_1804x1450.png 848w, https://substackcdn.com/image/fetch/$s_!jzUE!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F23449bad-9c3f-4b24-806d-1f66b6e737f8_1804x1450.png 1272w, https://substackcdn.com/image/fetch/$s_!jzUE!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F23449bad-9c3f-4b24-806d-1f66b6e737f8_1804x1450.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!jzUE!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F23449bad-9c3f-4b24-806d-1f66b6e737f8_1804x1450.png" width="1456" height="1170" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/23449bad-9c3f-4b24-806d-1f66b6e737f8_1804x1450.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1170,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:141931,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!jzUE!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F23449bad-9c3f-4b24-806d-1f66b6e737f8_1804x1450.png 424w, https://substackcdn.com/image/fetch/$s_!jzUE!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F23449bad-9c3f-4b24-806d-1f66b6e737f8_1804x1450.png 848w, https://substackcdn.com/image/fetch/$s_!jzUE!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F23449bad-9c3f-4b24-806d-1f66b6e737f8_1804x1450.png 1272w, https://substackcdn.com/image/fetch/$s_!jzUE!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F23449bad-9c3f-4b24-806d-1f66b6e737f8_1804x1450.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>At the beginning of the assembly, a variable callInfo is created by running an MLOAD on memory location 0xa0. The data at this memory location, 100000000c78789120020674d380f7e1dc7408aa007744ed3af390f8a47f9b75,  is the same as the calldata [3] item highlighted in green in the previous section.</p><p>Let&#8217;s now look at where this callInfo variable is used in the rest of the code.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!HWTn!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F66d50e26-b915-4fba-9aed-0b43bdca12ce_2218x2066.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!HWTn!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F66d50e26-b915-4fba-9aed-0b43bdca12ce_2218x2066.png 424w, https://substackcdn.com/image/fetch/$s_!HWTn!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F66d50e26-b915-4fba-9aed-0b43bdca12ce_2218x2066.png 848w, https://substackcdn.com/image/fetch/$s_!HWTn!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F66d50e26-b915-4fba-9aed-0b43bdca12ce_2218x2066.png 1272w, https://substackcdn.com/image/fetch/$s_!HWTn!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F66d50e26-b915-4fba-9aed-0b43bdca12ce_2218x2066.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!HWTn!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F66d50e26-b915-4fba-9aed-0b43bdca12ce_2218x2066.png" width="1200" height="1117.5824175824175" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/66d50e26-b915-4fba-9aed-0b43bdca12ce_2218x2066.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:1356,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:398429,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!HWTn!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F66d50e26-b915-4fba-9aed-0b43bdca12ce_2218x2066.png 424w, https://substackcdn.com/image/fetch/$s_!HWTn!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F66d50e26-b915-4fba-9aed-0b43bdca12ce_2218x2066.png 848w, https://substackcdn.com/image/fetch/$s_!HWTn!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F66d50e26-b915-4fba-9aed-0b43bdca12ce_2218x2066.png 1272w, https://substackcdn.com/image/fetch/$s_!HWTn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F66d50e26-b915-4fba-9aed-0b43bdca12ce_2218x2066.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Above is the full  &#8220;Assembly code&#8221;. The highlighted boxes in the assembly code show where callInfo is being used. We can see that there are a number of bitmasks being used on callInfo to extract values and execute logic based on what is returned. If you&#8217;re not aware of how bitmasks work check out <a href="https://noxx.substack.com/p/evm-deep-dives-the-path-to-shadowy-3ea?utm_source=%2Fprofile%2F80455042-noxx&amp;utm_medium=reader2&amp;s=r">Part 3 of the EVM Deep Dives</a> which looks at how this technique is used in  storage slot packing.</p><p>By looking at the assembly we can determine that this value, 100000000c78789120020674d380f7e1dc7408aa007744ed3af390f8a47f9b75, is actually a custom encoded set of 32 bytes. </p><p>Below is the decoding of it. Note the numbers ( [1] etc.) in the assembly code image above correspond to the numbers in the image below so you can cross-reference.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!kscX!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F33b1929c-cd1a-4851-8c86-8b64ba4bdbcb_3142x1774.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!kscX!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F33b1929c-cd1a-4851-8c86-8b64ba4bdbcb_3142x1774.png 424w, https://substackcdn.com/image/fetch/$s_!kscX!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F33b1929c-cd1a-4851-8c86-8b64ba4bdbcb_3142x1774.png 848w, https://substackcdn.com/image/fetch/$s_!kscX!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F33b1929c-cd1a-4851-8c86-8b64ba4bdbcb_3142x1774.png 1272w, https://substackcdn.com/image/fetch/$s_!kscX!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F33b1929c-cd1a-4851-8c86-8b64ba4bdbcb_3142x1774.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!kscX!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F33b1929c-cd1a-4851-8c86-8b64ba4bdbcb_3142x1774.png" width="1200" height="677.4725274725274" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/33b1929c-cd1a-4851-8c86-8b64ba4bdbcb_3142x1774.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:822,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:319827,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!kscX!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F33b1929c-cd1a-4851-8c86-8b64ba4bdbcb_3142x1774.png 424w, https://substackcdn.com/image/fetch/$s_!kscX!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F33b1929c-cd1a-4851-8c86-8b64ba4bdbcb_3142x1774.png 848w, https://substackcdn.com/image/fetch/$s_!kscX!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F33b1929c-cd1a-4851-8c86-8b64ba4bdbcb_3142x1774.png 1272w, https://substackcdn.com/image/fetch/$s_!kscX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F33b1929c-cd1a-4851-8c86-8b64ba4bdbcb_3142x1774.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><ol><li><p>callLength - encoded in bytes 4 - 5, the number of items to ingest from the calldata for the subsequent delegatecall / call</p><ol><li><p>0x000c = 12 decimal</p></li></ol></li><li><p>Function signature - encoded in bytes 6 - 9, the function signature for the subsequent delegatecall / call</p><ol><li><p>0x78789120, we can look this up in the <a href="https://www.4byte.directory/signatures/?bytes4_signature=0x78789120">Ethereum Signature Database</a>. It returns nothing, this indicates the associated contract may be unverified</p></li></ol></li><li><p>callContract - encoded in bytes 13 - 32, the contract address for the subsequent delegatecall / call</p><ol><li><p>&nbsp;<a href="https://etherscan.io/address/0xd380f7e1dc7408aa007744ed3af390f8a47f9b75">0xd380f7e1dc7408aa007744ed3af390f8a47f9b75</a>, we can look at this address on Etherscan. The contract hasn&#8217;t been verified so we&#8217;ll have to work with the <a href="https://etherscan.io/address/0xd380f7e1dc7408aa007744ed3af390f8a47f9b75#code">contract bytecode</a>.</p></li></ol></li><li><p>Delegate Call or Call - encoded in byte 1, whether the subsequent call should be a DELEGATECALL or a CALL</p><ol><li><p>0x10, execute as a DELEGATECALL</p></li></ol></li><li><p>Gas Amount for Call - encoded in bytes 10 - 12, how much gas is allocated for the subsequent delegatecall / call</p><ol><li><p>0x020674 = 132724 decimal </p></li></ol></li><li><p>Should the returned data be used in the next call - encoded in byte 3, whether the returned data is needed for the next loop</p><ol><li><p>0x00, the return data is not needed  </p></li></ol></li></ol><p>To reinforce our understanding of how these encoded values are used we&#8217;ll quickly run through the logic that uses them and determines the execution path.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!rYkW!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F46579050-f758-4403-bb7a-078fdb9ade40_2218x2066.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!rYkW!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F46579050-f758-4403-bb7a-078fdb9ade40_2218x2066.png 424w, https://substackcdn.com/image/fetch/$s_!rYkW!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F46579050-f758-4403-bb7a-078fdb9ade40_2218x2066.png 848w, https://substackcdn.com/image/fetch/$s_!rYkW!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F46579050-f758-4403-bb7a-078fdb9ade40_2218x2066.png 1272w, https://substackcdn.com/image/fetch/$s_!rYkW!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F46579050-f758-4403-bb7a-078fdb9ade40_2218x2066.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!rYkW!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F46579050-f758-4403-bb7a-078fdb9ade40_2218x2066.png" width="1200" height="1117.5824175824175" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/46579050-f758-4403-bb7a-078fdb9ade40_2218x2066.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:1356,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:397766,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!rYkW!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F46579050-f758-4403-bb7a-078fdb9ade40_2218x2066.png 424w, https://substackcdn.com/image/fetch/$s_!rYkW!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F46579050-f758-4403-bb7a-078fdb9ade40_2218x2066.png 848w, https://substackcdn.com/image/fetch/$s_!rYkW!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F46579050-f758-4403-bb7a-078fdb9ade40_2218x2066.png 1272w, https://substackcdn.com/image/fetch/$s_!rYkW!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F46579050-f758-4403-bb7a-078fdb9ade40_2218x2066.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><ol><li><p>This section of the code checks for a non-zero function signature. Note how lines 226 &amp; 234 differ. Both update the callLength but when the function signature is non-zero an extra 4 is added to the callLength. This is for the 4-byte function signature. The same applies to lines 228 &amp; 236, both are for loops that loop over the data but the for loop on line 236 starts at 4 while the loop on 228 starts at 0. Again this is to account for the 4-byte function signature.</p></li><li><p>This section deals with the callLength. When you make a DELEGATECALL or CALL you have to specify where in memory the calldata/input for that call is located. This for loop is storing the calldata items in memory in anticipation of that call. It loads in the number of items from the calldata specified in the encoded data. If you&#8217;re unsure of how the DELEGATECALL opcode works take a look at <a href="https://noxx.substack.com/p/evm-deep-dives-the-path-to-shadowy-a5f?utm_source=%2Fprofile%2F80455042-noxx&amp;utm_medium=reader2&amp;s=r">Part 5 of the EVM Deep Dives</a> which does an in-depth review. </p></li><li><p>This section looks at the DELEGATECALL/CALL byte, if the byte is 0x10 it executes a delegatecall on the &#8220;callContract&#8221; and for anything else, a call is made.</p></li><li><p>This looks at byte 3 and determines whether the returned data should be used in the next loop. For it to be used in the next loop it needs to be stored somewhere. If byte 3 is non-zero an MSTORE is used to save it in memory so it can be accessed later.</p></li></ol><p>Now that we understand callInfo&#8217;s encoding and where it&#8217;s located in the calldata we can revisit the full calldata for the arbitrage transaction.</p><p>We&#8217;re going to organise the calldata into sections based on any encoded callInfos we can find.</p><p>We&#8217;re also going to link these sections to the <a href="https://etherscan.io/vmtrace?txhash=0x0efe3832b85e610fc4ba815f2be3943036a1a1be33cbd8f378a20d20667c1b39&amp;type=parity">parity traces</a> of the transaction to determine which calldata is used for which call.</p><p><a href="https://openethereum.github.io/JSONRPC-trace-module">Parity traces</a> enable us to see all the internal contract calls for a given transaction. This is extremely useful when debugging &amp; attempting to piece together what happened on-chain.</p><p>The traces have a traceAddress. TraceAddresses have the notation [ 2, 1, 0, 3, 0, 2 ] where the number of items in the array shows you the depth of the call and the value of each item shows you the index of that call at that depth (ie its order position relative to other calls at that depth).       </p><p>Here&#8217;s a quick summary of the notation from the OpenEthereum (Parity) website.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!oTig!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5339fb96-93a2-4a72-a8f3-24d84c17c465_1928x680.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!oTig!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5339fb96-93a2-4a72-a8f3-24d84c17c465_1928x680.png 424w, https://substackcdn.com/image/fetch/$s_!oTig!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5339fb96-93a2-4a72-a8f3-24d84c17c465_1928x680.png 848w, https://substackcdn.com/image/fetch/$s_!oTig!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5339fb96-93a2-4a72-a8f3-24d84c17c465_1928x680.png 1272w, https://substackcdn.com/image/fetch/$s_!oTig!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5339fb96-93a2-4a72-a8f3-24d84c17c465_1928x680.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!oTig!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5339fb96-93a2-4a72-a8f3-24d84c17c465_1928x680.png" width="1456" height="514" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/5339fb96-93a2-4a72-a8f3-24d84c17c465_1928x680.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:514,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:61570,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!oTig!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5339fb96-93a2-4a72-a8f3-24d84c17c465_1928x680.png 424w, https://substackcdn.com/image/fetch/$s_!oTig!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5339fb96-93a2-4a72-a8f3-24d84c17c465_1928x680.png 848w, https://substackcdn.com/image/fetch/$s_!oTig!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5339fb96-93a2-4a72-a8f3-24d84c17c465_1928x680.png 1272w, https://substackcdn.com/image/fetch/$s_!oTig!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5339fb96-93a2-4a72-a8f3-24d84c17c465_1928x680.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Now let&#8217;s dig into the calldata and the associated parity traces. There&#8217;s a lot going on in the image below but I promise it&#8217;s not too complicated.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!idy_!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F0ded9bf9-9e2f-4cca-90ae-26e87de425d7_4370x2752.png" data-component-name="Image2ToDOM"><div class="image2-inset image2-full-screen"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!idy_!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F0ded9bf9-9e2f-4cca-90ae-26e87de425d7_4370x2752.png 424w, https://substackcdn.com/image/fetch/$s_!idy_!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F0ded9bf9-9e2f-4cca-90ae-26e87de425d7_4370x2752.png 848w, https://substackcdn.com/image/fetch/$s_!idy_!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F0ded9bf9-9e2f-4cca-90ae-26e87de425d7_4370x2752.png 1272w, https://substackcdn.com/image/fetch/$s_!idy_!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F0ded9bf9-9e2f-4cca-90ae-26e87de425d7_4370x2752.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!idy_!,w_5760,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F0ded9bf9-9e2f-4cca-90ae-26e87de425d7_4370x2752.png" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/0ded9bf9-9e2f-4cca-90ae-26e87de425d7_4370x2752.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;full&quot;,&quot;height&quot;:917,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1310599,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-fullscreen" alt="" srcset="https://substackcdn.com/image/fetch/$s_!idy_!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F0ded9bf9-9e2f-4cca-90ae-26e87de425d7_4370x2752.png 424w, https://substackcdn.com/image/fetch/$s_!idy_!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F0ded9bf9-9e2f-4cca-90ae-26e87de425d7_4370x2752.png 848w, https://substackcdn.com/image/fetch/$s_!idy_!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F0ded9bf9-9e2f-4cca-90ae-26e87de425d7_4370x2752.png 1272w, https://substackcdn.com/image/fetch/$s_!idy_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F0ded9bf9-9e2f-4cca-90ae-26e87de425d7_4370x2752.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The 3 sections in the parity traces above represent the 3 calls that execute the arbitrage between EtherDelta 2 and Uniswap V2.</p><p>The calldata has been grouped to show you what calldata was passed to the top call in each of parity trace groupings ([ 2, 1, 0, 3, 0, 2 ], [ 2, 1, 0, 3, 0, 3 ] &amp; [ 2, 1, 0, 3, 0, 4 ]). </p><p>At the top of each calldata section is the custom encoded &#8220;callInfo&#8221; value, see calldata items [3], [17] &amp; [31]. The groupings were determined by looking at the callInfo data along with the assembly code.</p><p>The parity traces show us that multiple subsequent calls are being made in each section. Two in the first call, five in the second call and ten in the third call.</p><p>Let&#8217;s have a closer look at the 3 core calls. </p><ol><li><p>The first call ([ 2, 1, 0, 3, 0, 2 ]) uses calldata item [3] as its encoded callInfo. We looked at this exact callInfo earlier. The encoding tells us to make a DELEGATECALL to address <a href="https://etherscan.io/address/0xd380f7e1dc7408aa007744ed3af390f8a47f9b75">0xd380f7e1dc7408aa007744ed3af390f8a47f9b75</a> and take the next 12 items from the calldata as input.</p><ol><li><p>Note the first item after the callInfo in the calldata ([4]) is 0x000&#8230;0. This represents the Wei amount for the call. You won&#8217;t see it being used in the delegatecall flow of the assembly since the DELEGATECALL opcode doesn&#8217;t take in a value. You can however see it being accessed in the call flow on line 260 of the assembly code. Note this &#8220;Wei value&#8221; is not included in the &#8220;next 12 items&#8221; from the calldata. Therefore the &#8220;next 12 items&#8221; runs from [5] to [16]. This applies to all 3 core calls. </p></li></ol></li><li><p>If we inspect the <a href="https://etherscan.io/vmtrace?txhash=0x0efe3832b85e610fc4ba815f2be3943036a1a1be33cbd8f378a20d20667c1b39&amp;type=parity">parity traces of the transaction on Etherscan</a>, specifically, Action [17] TraceAddress [ 2, 1, 0, 3, 0, 2 ] we can see that the values encoded in the callInfo match what is in the trace. </p><ol><li><p>The &#8220;To&#8221; address = <a href="https://etherscan.io/address/0xd380f7e1dc7408aa007744ed3af390f8a47f9b75">0xd380f7e1dc7408aa007744ed3af390f8a47f9b75</a></p></li><li><p>CallType = delegatecall</p></li><li><p>Gas =  132724 (Allocated Gas in decimal)</p></li><li><p>Input Data matches the 12 calldata items highlighted with a prefix of the encoded function signature</p></li></ol></li><li><p>The second call ([ 2, 1, 0, 3, 0, 3 ]) uses calldata item [17] as its callInfo. It makes a DELEGATECALL to the same address <a href="https://etherscan.io/address/0xd380f7e1dc7408aa007744ed3af390f8a47f9b75">0xd380f7e1dc7408aa007744ed3af390f8a47f9b75</a> but with a different function signature 0x401687f4. Again the next 12 items from the calldata are used as input along with the function signature.</p></li><li><p>The third call ([ 2, 1, 0, 3, 0, 4 ]) uses calldata item [31] as its callInfo. It makes a DELEGATECALL to a different address <a href="https://etherscan.io/address/0xf4863028b093fdac9cf7fd67c0df6866ac3c7a60">0xf4863028b093fdac9cf7fd67c0df6866ac3c7a60</a> with the function signature  0x0fd72adb. The encoded callLength is 7 so the next 7 items from the calldata are used as the input along with function signature.   </p></li></ol><p>These 3 calls represent 3 loops of our assembly code. The first 2 calls handle the EtherDelta leg of the arb while the third executes the Uniswap section.</p><p>The 2 contracts the calls interact with  <a href="https://etherscan.io/address/0xd380f7e1dc7408aa007744ed3af390f8a47f9b75">0xd380f7e1dc7408aa007744ed3af390f8a47f9b75</a> &amp; <a href="https://etherscan.io/address/0xf4863028b093fdac9cf7fd67c0df6866ac3c7a60">0xf4863028b093fdac9cf7fd67c0df6866ac3c7a60</a> are both unverified contracts.</p><p>This means we&#8217;re going to need to decompile their bytecode and debug via foundry at the opcode level to understand what is going on.</p><p>But that one is for next time.</p><p>Hope you enjoyed!</p><p>noxx</p><p>Twitter <a href="https://twitter.com/noxx3xxon">@noxx3xxon</a></p>]]></content:encoded></item><item><title><![CDATA[EVM Deep Dives: The Path to Shadowy Super Coder 🥷 💻 - Part 5]]></title><description><![CDATA[Another Weapon in the EVM Hackers Arsenal - The Power of Delegate Call]]></description><link>https://noxx.substack.com/p/evm-deep-dives-the-path-to-shadowy-a5f</link><guid isPermaLink="false">https://noxx.substack.com/p/evm-deep-dives-the-path-to-shadowy-a5f</guid><dc:creator><![CDATA[noxx]]></dc:creator><pubDate>Sun, 15 May 2022 22:57:12 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F39945796-94b6-4d6b-8bf6-52b29ac22559_3404x1334.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>If you&#8217;re interested in EVM Hacking you&#8217;re going to enjoy this installment of the &#8220;EVM Deep Dives&#8221; series.</p><p>Today we&#8217;re going to take a close look at the opcodes CALL &amp; DELEGATECALL. This episode builds on concepts covered in <a href="https://noxx.substack.com/p/evm-deep-dives-the-path-to-shadowy-d6b?utm_source=%2Fprofile%2F80455042-noxx&amp;utm_medium=reader2&amp;s=r">Part 2</a>, <a href="https://noxx.substack.com/p/evm-deep-dives-the-path-to-shadowy-3ea?utm_source=%2Fprofile%2F80455042-noxx&amp;utm_medium=reader2&amp;s=r">Part 3</a> &amp; <a href="https://noxx.substack.com/p/evm-deep-dives-the-path-to-shadowy-5a5?utm_source=%2Fprofile%2F80455042-noxx&amp;utm_medium=reader2&amp;s=r">Part 4</a>, I recommend reading those if you haven&#8217;t yet.</p><p>We&#8217;re going to cover how these opcodes work at the solidity level, the EVM level and the Geth client level to give you a complete understanding.   </p><p>Before we dig into each of these we need to first understand the concept of a contract execution context. </p><h3>Execution Context </h3><p><em>When the EVM executes a smart contract, a <a href="https://www.evm.codes/about">context</a> is created for it. The context consists of the following.</em></p><ul><li><p>The Code</p><ul><li><p>The contract bytecode which is immutable, it is stored on-chain and referenced using a contract address. </p></li></ul></li><li><p>The Stack</p><ul><li><p>The call stack, an empty stack is initialised for each EVM contract execution. </p></li></ul></li><li><p>The Memory </p><ul><li><p>The contract memory, a clean memory is initialised for each EVM contract execution. </p></li></ul></li><li><p>The Storage</p><ul><li><p>The contract storage which is persisted across executions, it is stored on-chain and is referenced via a contract address and its storage slot.</p></li></ul></li><li><p>The Call Data</p><ul><li><p>The input data for a transaction.</p></li></ul></li><li><p>The Return Data</p><ul><li><p>The data returned from a contract function call. </p></li></ul></li></ul><p>Keep these items in your mind as we proceed through the article.</p><p>We will start with a DELEGATECALL Solidity example from the <a href="https://www.youtube.com/watch?v=uawCDnxFJ-0">Smart Contract Programmer</a> and reference it throughout.</p><h3>Solidity Example</h3><p>The diagram below shows the execution of two function calls on the same contract, one which uses DELEGATECALL and the other which uses CALL. </p><p>We&#8217;ll run through both and compare how they differ. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ibxa!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fbd68e45d-6238-483a-bf8f-393db30ac39c_2650x1572.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ibxa!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fbd68e45d-6238-483a-bf8f-393db30ac39c_2650x1572.png 424w, https://substackcdn.com/image/fetch/$s_!ibxa!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fbd68e45d-6238-483a-bf8f-393db30ac39c_2650x1572.png 848w, https://substackcdn.com/image/fetch/$s_!ibxa!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fbd68e45d-6238-483a-bf8f-393db30ac39c_2650x1572.png 1272w, https://substackcdn.com/image/fetch/$s_!ibxa!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fbd68e45d-6238-483a-bf8f-393db30ac39c_2650x1572.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ibxa!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fbd68e45d-6238-483a-bf8f-393db30ac39c_2650x1572.png" width="1200" height="712.0879120879121" data-attrs="{&quot;src&quot;:&quot;https://substackcdn.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fbd68e45d-6238-483a-bf8f-393db30ac39c_2650x1572.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:864,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:235075,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ibxa!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fbd68e45d-6238-483a-bf8f-393db30ac39c_2650x1572.png 424w, https://substackcdn.com/image/fetch/$s_!ibxa!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fbd68e45d-6238-483a-bf8f-393db30ac39c_2650x1572.png 848w, https://substackcdn.com/image/fetch/$s_!ibxa!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fbd68e45d-6238-483a-bf8f-393db30ac39c_2650x1572.png 1272w, https://substackcdn.com/image/fetch/$s_!ibxa!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fbd68e45d-6238-483a-bf8f-393db30ac39c_2650x1572.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Let&#8217;s start by noting the constants in this interaction (Note if you recreate in remix your addresses will likely be different.)</p><p>We have two contracts, Contract A &amp; B and an EOA. </p><ul><li><p>EOA Address = 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4</p></li><li><p> Contract A Address = 0x7b96aF9Bd211cBf6BA5b0dd53aa61Dc5806b6AcE</p></li><li><p>Contract B Address = 0x3328358128832A260C76A4141e19E2A943CD4B6D</p></li></ul><p>We&#8217;re going to call the 2 functions in Contract A, setVarsDelegateCall &amp; setVarsCall. </p><p>We will pass in the parameters Contract B Address, a uint of 12 and a Wei value of 1000000000000000000 (1 ETH).</p><p><em><strong>Delegate Call</strong></em></p><ol><li><p>An EOA address calls Contract A&#8217;s setVarsDelegateCall with Contract B&#8217;s address, uint 12 and value 1000000000000000000 Wei. This in turn makes a delegatecall to Contract B&#8217;s setVars(uint256) function with uint 12.  </p></li><li><p>The delegatecall executes the setVars(uint256) code from Contract B but updates Contract A&#8217;s storage. The execution has the same storage, msg.sender &amp; msg.value as its parent call setVarsDelegateCall.  </p></li><li><p>The values are set in Contract A&#8217;s storage, 12 for num, 0x5b38&#8230;c4 for sender (EOA Address) &amp; 1000000000000000000 for value. Despite setVars(uint256) being called by Contract A with no value when we check msg.sender &amp; msg.value we get the values from the original setVarsDelegateCall.</p></li></ol><p>After the execution of this function we can check the num, sender &amp; value state items of Contract A &amp; B. We will see that none of the values are initialised in Contract B while all are set in Contract A.  </p><p><em><strong>Call</strong></em></p><ol><li><p>An EOA address calls Contract A&#8217;s setVarsCall with Contract B&#8217;s address, uint 12 and value 1000000000000000000 Wei. This in turn makes a call to Contract B&#8217;s setVars(uint256) function with uint 12.</p></li><li><p>The standard call executes the setVars(uint256) code from Contract B with no alterations to storage, msg.sender, msg.value.   </p></li><li><p>The values are set in Contract B&#8217;s storage, 12 for num, 0x7b96&#8230;ce for sender (Contract A Address) &amp; 0 for value. These values correspond with what we expect since setVars(uint256) was called from Contract A and no Wei value was passed into the setVars(uint256) (the 1000000000000000000 Wei was passed into the parent call setVarsCall)</p></li></ol><p>Again after the execution of this function we can check the num, sender &amp; value state items of Contract A &amp; B. We see that the reverse is true this time, none of the values are initialised in Contract A while all are set in Contract B.</p><p>Conceptually a &#8220;Delegate Call&#8221; effectively allows you to copy and paste a function from another contract  into your contract. It will be run as if it were executed by your contract and will have access to the same storage, msg.sender &amp; msg.value. </p><h3>Delegate Call &amp; Storage Layout</h3><p>In the above example, you may have noticed the comment in the code for Contract B on line 5 stating <em>&#8220;NOTE: storage layout must be the same as contract A&#8221;</em>. </p><p>Remember a function in a contract maps to some static bytecode that is calculated at compile time. </p><p>When we look at solidity code we think in terms of variables. We see the state variables num, sender &amp; value. </p><p>The compiled bytecode doesn&#8217;t see these variables it instead sees storage slots. Declared state variables are mapped to storage slots (If you&#8217;re unsure how this works check out <a href="https://noxx.substack.com/p/evm-deep-dives-the-path-to-shadowy-3ea?utm_source=%2Fprofile%2F80455042-noxx&amp;utm_medium=reader2&amp;s=r">Part 3</a>). </p><p>If we look at Contract B setVars(uint256) specifically &#8220;num = _num&#8221; this is saying store value _num into storage slot 0.</p><p>When we look at contracts involved in a DELEGATECALL don&#8217;t think about the  mapping of num &#8594; num, sender &#8594; sender. That&#8217;s not how it works at the bytecode level. </p><p>We need to think in terms of mapping slot 0 &#8594; slot 0, slot 1 &#8594; slot 1</p><p>The diagram below shows this mapping, along with the corresponding variable names.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!dNAA!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fcf64c77b-f60f-4720-96bb-575dabef8917_1006x306.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!dNAA!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fcf64c77b-f60f-4720-96bb-575dabef8917_1006x306.png 424w, https://substackcdn.com/image/fetch/$s_!dNAA!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fcf64c77b-f60f-4720-96bb-575dabef8917_1006x306.png 848w, https://substackcdn.com/image/fetch/$s_!dNAA!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fcf64c77b-f60f-4720-96bb-575dabef8917_1006x306.png 1272w, https://substackcdn.com/image/fetch/$s_!dNAA!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fcf64c77b-f60f-4720-96bb-575dabef8917_1006x306.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!dNAA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fcf64c77b-f60f-4720-96bb-575dabef8917_1006x306.png" width="1006" height="306" data-attrs="{&quot;src&quot;:&quot;https://substackcdn.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fcf64c77b-f60f-4720-96bb-575dabef8917_1006x306.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:306,&quot;width&quot;:1006,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:49861,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!dNAA!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fcf64c77b-f60f-4720-96bb-575dabef8917_1006x306.png 424w, https://substackcdn.com/image/fetch/$s_!dNAA!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fcf64c77b-f60f-4720-96bb-575dabef8917_1006x306.png 848w, https://substackcdn.com/image/fetch/$s_!dNAA!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fcf64c77b-f60f-4720-96bb-575dabef8917_1006x306.png 1272w, https://substackcdn.com/image/fetch/$s_!dNAA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fcf64c77b-f60f-4720-96bb-575dabef8917_1006x306.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Contract A &amp; B storage slot mapping</figcaption></figure></div><p>Think what would happen if we were to change the order in which our state variables are defined.</p><p>It would change their storage slot positions and subsequently the bytecode associated with the setVars(uint256) function.</p><p>If we updated Contract B by switching lines 6 and 8  we would be declaring the &#8220;value&#8221; state variable first and the &#8220;num&#8221; state variable last.</p><p>This means line 11 &#8220;num = _num&#8221;  in setVars(uint256) would now say store value _num into storage slot 2. Line 13 &#8220;value = msg.value&#8221; would now say store msg.value in storage slot 0. </p><p>This means our variable mappings between Contract A &amp; B would no longer match relative to their storage slots.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!133M!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F0f24b3ce-a2ed-4bbe-b87d-6d36b9148640_1006x306.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!133M!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F0f24b3ce-a2ed-4bbe-b87d-6d36b9148640_1006x306.png 424w, https://substackcdn.com/image/fetch/$s_!133M!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F0f24b3ce-a2ed-4bbe-b87d-6d36b9148640_1006x306.png 848w, https://substackcdn.com/image/fetch/$s_!133M!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F0f24b3ce-a2ed-4bbe-b87d-6d36b9148640_1006x306.png 1272w, https://substackcdn.com/image/fetch/$s_!133M!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F0f24b3ce-a2ed-4bbe-b87d-6d36b9148640_1006x306.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!133M!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F0f24b3ce-a2ed-4bbe-b87d-6d36b9148640_1006x306.png" width="1006" height="306" data-attrs="{&quot;src&quot;:&quot;https://substackcdn.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F0f24b3ce-a2ed-4bbe-b87d-6d36b9148640_1006x306.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:306,&quot;width&quot;:1006,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:50554,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!133M!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F0f24b3ce-a2ed-4bbe-b87d-6d36b9148640_1006x306.png 424w, https://substackcdn.com/image/fetch/$s_!133M!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F0f24b3ce-a2ed-4bbe-b87d-6d36b9148640_1006x306.png 848w, https://substackcdn.com/image/fetch/$s_!133M!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F0f24b3ce-a2ed-4bbe-b87d-6d36b9148640_1006x306.png 1272w, https://substackcdn.com/image/fetch/$s_!133M!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F0f24b3ce-a2ed-4bbe-b87d-6d36b9148640_1006x306.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Contract A &amp; B with different storage layouts</figcaption></figure></div><p>When we run DELEGATECALL the &#8220;num&#8221; value is going to be stored in storage slot 2 for Contract A which maps to the &#8220;value&#8221; state variable. The same applies for when &#8220;value&#8221; is stored its going to update slot 0 which maps to the &#8220;num&#8221; state variable.</p><p>This is one of the reasons DELEGATECALL can be dangerous.</p><p>Above we&#8217;ve accidentally replaced the &#8220;num&#8221; state variable with the &#8220;value&#8221; state variable and vice versa. </p><p>A Hacker won&#8217;t be accidentally changing your state variables. They will be conducting a targeted attack.</p><p>Imagine we have a contract that makes an open delegatecall and we know the slot location where the owner of that contract is stored.</p><p>We would be able to construct a contract with a state variable layout and function that allows us to update the &#8220;owner&#8221; slot location with a different address. This would enable us to claim ownership of that contract.</p><p>If you&#8217;re interested in how these hacks work, take a look at these 2 Ethernaut problems, you have the required knowledge to solve them.</p><ul><li><p><a href="https://ethernaut.openzeppelin.com/level/0x9451961b7Aea1Df57bc20CC68D72f662241b5493">Ethernaut Level 6 - Delegation</a></p></li><li><p><a href="https://ethernaut.openzeppelin.com/level/0x97E982a15FbB1C28F6B8ee971BEc15C78b3d263F">Ethernaut Level 16 - Preservation</a> </p></li></ul><p>Now let&#8217;s jump into the opcodes.</p><h2>Opcodes</h2><p>We have a rough idea of how DELEGATECALL works so let&#8217;s have a look at the opcodes for DELEGATECALL &amp; CALL. </p><p>For DELEGATECALL we have the following input variables;</p><ul><li><p><code>gas</code>: amount of gas to send to the sub <a href="https://www.evm.codes/about">context</a> to execute. The gas that is not used by the sub context is returned to this one.</p></li><li><p><code>address</code>: the account which <a href="https://www.evm.codes/about">context</a> to execute.</p></li><li><p><code>argsOffset</code>: byte offset in the <a href="https://www.evm.codes/about">memory</a> in bytes, the <a href="https://www.evm.codes/about">calldata</a> of the sub <a href="https://www.evm.codes/about">context</a>.</p></li><li><p><code>argsSize</code>: byte size to copy (size of the <a href="https://www.evm.codes/about">calldata</a>).</p></li><li><p><code>retOffset</code>: byte offset in the <a href="https://www.evm.codes/about">memory</a> in bytes, where to store the <a href="https://www.evm.codes/about">return data</a> of the sub <a href="https://www.evm.codes/about">context</a>.</p></li><li><p><code>retSize</code>: byte size to copy (size of the <a href="https://www.evm.codes/about">return data</a>).</p></li></ul><p>CALL has exactly the same input variables with one additional value. </p><ul><li><p><code>value</code>: <a href="https://www.evm.codes/#34">value</a> in <a href="https://www.investopedia.com/terms/w/wei.asp">wei</a> to send to the account. (CALL only)</p></li></ul><p>Delegate call doesn&#8217;t require a value input as it is inherited from its parent call. Recall when we mentioned that the execution context has the same storage, msg.sender &amp; msg.value as its parent call.</p><p>Both have one output variable &#8220;success&#8221; which is 0 if the sub context reverted otherwise it returns 1.</p><blockquote><p><em>Delegatecall will return success &#8216;True&#8217; if it is called on an address that is not a contract and so has no code. This can cause bugs if code expects delegatecall functions to return `False` when they can&#8217;t execute.</em></p></blockquote><p>To understand the opcode let&#8217;s inspect how DELEGATECALL was executed for the earlier example with Contracts A &amp; B.</p><h3>DELEGATECALL Opcode Inspection With Remix    </h3><p>Below is a snapshot from the Remix IDE as the DELEGATECALL opcode was called. It corresponds to lines 24 - 26 in the earlier code snippet.</p><p>We will look at the items on the stack &amp; memory and see how these values determine the call data that is passed into the DELEGATECALL. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!T_ku!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F39945796-94b6-4d6b-8bf6-52b29ac22559_3404x1334.png" data-component-name="Image2ToDOM"><div class="image2-inset image2-full-screen"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!T_ku!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F39945796-94b6-4d6b-8bf6-52b29ac22559_3404x1334.png 424w, https://substackcdn.com/image/fetch/$s_!T_ku!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F39945796-94b6-4d6b-8bf6-52b29ac22559_3404x1334.png 848w, https://substackcdn.com/image/fetch/$s_!T_ku!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F39945796-94b6-4d6b-8bf6-52b29ac22559_3404x1334.png 1272w, https://substackcdn.com/image/fetch/$s_!T_ku!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F39945796-94b6-4d6b-8bf6-52b29ac22559_3404x1334.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!T_ku!,w_5760,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F39945796-94b6-4d6b-8bf6-52b29ac22559_3404x1334.png" data-attrs="{&quot;src&quot;:&quot;https://substackcdn.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F39945796-94b6-4d6b-8bf6-52b29ac22559_3404x1334.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;full&quot;,&quot;height&quot;:571,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:597844,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-fullscreen" alt="" srcset="https://substackcdn.com/image/fetch/$s_!T_ku!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F39945796-94b6-4d6b-8bf6-52b29ac22559_3404x1334.png 424w, https://substackcdn.com/image/fetch/$s_!T_ku!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F39945796-94b6-4d6b-8bf6-52b29ac22559_3404x1334.png 848w, https://substackcdn.com/image/fetch/$s_!T_ku!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F39945796-94b6-4d6b-8bf6-52b29ac22559_3404x1334.png 1272w, https://substackcdn.com/image/fetch/$s_!T_ku!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F39945796-94b6-4d6b-8bf6-52b29ac22559_3404x1334.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>We&#8217;ll work our way from the opcode &#8594; stack &#8594; memory &#8594; calldata.</p><p>Let&#8217;s run through this</p><ol><li><p>On line 24 of the Solidity code, a &#8220;delegatecall&#8221; is made to Contract B setVars(unit256) with a value of 12. This results in the DELEGATECALL opcode being executed.</p></li><li><p>The DELEGATECALL opcode takes 6 inputs, gas, address, argsOffset, argsSize, retOffset &amp; retSize which are taken off the stack.</p><ol><li><p>Gas = 0x45eb</p></li><li><p>Address =  0x3328358128832A260C76A4141e19E2A943CD4B6D (Address for Contract B)</p></li><li><p>ArgsOffset = 0xc4</p></li><li><p>ArgsSize = 0x24</p></li><li><p>RetOffset = 0xc4</p></li><li><p>RetSize = 0x00</p></li></ol></li><li><p>Let&#8217;s focus on the argsOffset &amp; argsSize which are the calldata that will be passed to Contract B. These 2 values order us to go to memory location 0xc4 and copy the next 0x24 (36 in Decimal) bytes to get our calldata.</p></li><li><p>This yields 0x6466414b000000000000000000000000000000000000000000000000000000000000000c which can be split into 0x6466414b which is the function signature for setVars(uint256) and 0x000000000000000000000000000000000000000000000000000000000000000c which is 12 in decimal and represents our input value for num.</p></li><li><p>This value maps to what is produced by line 25 in the Solidity code abi.encodeWithSignature("setVars(uint256)", _num)</p></li></ol><p>Note the retSize is equal to 0 since setVars(uint256) doesn&#8217;t return anything. If it did the retSize value would be updated and the returned value would be stored at the retOffset. </p><p>This should give you a good idea of what the opcode is doing under the hood and enable you to connect it back to a real Solidity example.</p><p>Now let&#8217;s look at the Geth Client Implementation.</p><p><em>A warning, the next section is heavy on the code side.</em></p><h3>Geth Implementation</h3><p>I&#8217;m going to focus on a specific part of DELEGATECALL within Geth. </p><p>The goal is to show you how the DELEGATECALL opcode differs from the CALL opcode at the storage scope level and how this relates to the SLOAD opcode.</p><p>The diagram below looks intimidating but we will run through it step by step. By the end, you&#8217;ll understand the subtle implementation  differences between DELEGATECALL and CALL in Geth.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!S-8h!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F60abd9bd-475f-4694-8bce-aa4c4b43fadd_4102x3874.png" data-component-name="Image2ToDOM"><div class="image2-inset image2-full-screen"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!S-8h!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F60abd9bd-475f-4694-8bce-aa4c4b43fadd_4102x3874.png 424w, https://substackcdn.com/image/fetch/$s_!S-8h!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F60abd9bd-475f-4694-8bce-aa4c4b43fadd_4102x3874.png 848w, https://substackcdn.com/image/fetch/$s_!S-8h!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F60abd9bd-475f-4694-8bce-aa4c4b43fadd_4102x3874.png 1272w, https://substackcdn.com/image/fetch/$s_!S-8h!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F60abd9bd-475f-4694-8bce-aa4c4b43fadd_4102x3874.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!S-8h!,w_5760,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F60abd9bd-475f-4694-8bce-aa4c4b43fadd_4102x3874.png" data-attrs="{&quot;src&quot;:&quot;https://substackcdn.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F60abd9bd-475f-4694-8bce-aa4c4b43fadd_4102x3874.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;full&quot;,&quot;height&quot;:1375,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1110770,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-fullscreen" alt="" srcset="https://substackcdn.com/image/fetch/$s_!S-8h!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F60abd9bd-475f-4694-8bce-aa4c4b43fadd_4102x3874.png 424w, https://substackcdn.com/image/fetch/$s_!S-8h!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F60abd9bd-475f-4694-8bce-aa4c4b43fadd_4102x3874.png 848w, https://substackcdn.com/image/fetch/$s_!S-8h!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F60abd9bd-475f-4694-8bce-aa4c4b43fadd_4102x3874.png 1272w, https://substackcdn.com/image/fetch/$s_!S-8h!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F60abd9bd-475f-4694-8bce-aa4c4b43fadd_4102x3874.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>We have the DELEGATECALL &amp; CALL opcodes labeled on the left-hand side and the SLOAD opcode labeled bottom right. Let&#8217;s see how they&#8217;re connected. </p><ol><li><p>Note there are two [ 1 ] &#8216;s on the diagram. These are the Geth functions for the opcodes DELEGATECALL &amp; CALL found in <a href="https://github.com/ethereum/go-ethereum/blob/d4d288e3f1cebb183fce9137829a76ddf7c6d12a/core/vm/instructions.go">instructions.go</a>. We can see the values we discussed earlier being popped off the stack into variables. Later in the function, we see that interpreter.evm.DeleagteCall and interpreter.evm.Call are called with the values off the stack, the &#8220;to address&#8221; and the current contract scope.</p></li><li><p>Note there are two [ 2 ] &#8216;s on the diagram. Both the evm.DelegateCall &amp; evm.Call functions are executed (found in <a href="https://github.com/ethereum/go-ethereum/blob/d4d288e3f1cebb183fce9137829a76ddf7c6d12a/core/vm/evm.go">evm.go</a>). I&#8217;ve omitted sections of the functions to focus on the NewContract function call which is creating a new contract context for us to execute in.</p></li><li><p>Note there are two [ 3 ] &#8216;s on the diagram. The NewContract function call for evm.DelegateCall &amp; evm.Call are very similar except for 2 items. </p><ol><li><p>In DelegateCall the value parameter is set to nil, remember it inherits its value from its parent context so doesn&#8217;t take in this parameter. </p></li><li><p>The second input into the NewContract functions are different. In evm.DelegateCall caller.Address( ) is passed in (Contract A address). In evm.Call addrCopy is passed in which is equal to the toAddr from the opCall function (Contract B address). This difference will be very important later. Note both are of type AccountRef.</p></li></ol></li><li><p>DelegateCall&#8217;s NewContract will return a Contract struct. The AsDelegate( ) function is called (found in <a href="https://github.com/ethereum/go-ethereum/blob/d4d288e3f1cebb183fce9137829a76ddf7c6d12a/core/vm/contract.go">contract.go</a>). It sets the msg.sender &amp; msg.value to that of the original call (EOA address &amp; 1000000000000000000 Wei). This is not done on the Call implementation.</p></li><li><p> Both evm.DelegateCall &amp; evm.Call execute the NewContract function (found in <a href="https://github.com/ethereum/go-ethereum/blob/d4d288e3f1cebb183fce9137829a76ddf7c6d12a/core/vm/contract.go">contract.go</a>). Note &#8220;object ContractRef&#8221; is the second input variable for NewContract which maps to the AccountRef&#8217;s we discussed in [ 3 ]</p></li><li><p>This &#8220;object ContractRef&#8221; is used along with a number of other values to initialise a Contract. The &#8220;object ContractRef&#8221; is mapped to &#8220;self&#8221; in the Contract struct.</p></li><li><p>The Contract struct (found in <a href="https://github.com/ethereum/go-ethereum/blob/d4d288e3f1cebb183fce9137829a76ddf7c6d12a/core/vm/contract.go">contract.go</a>) has the field &#8220;self&#8221; which is what we are interested in. You can see some of the other fields that relate to items we discussed earlier when talking about contract execution context.</p></li><li><p>We are now jumping to the SLOAD opcode implementation in Geth (found in <a href="https://github.com/ethereum/go-ethereum/blob/d4d288e3f1cebb183fce9137829a76ddf7c6d12a/core/vm/instructions.go">instructions.go</a>). It runs GetState on scope.Contract.Address( ). The &#8220;Contract&#8221; within this statement refers to the Contract struct in [ 7 ].</p></li><li><p>The implementation of Address( ) for a Contract object (found in <a href="https://github.com/ethereum/go-ethereum/blob/d4d288e3f1cebb183fce9137829a76ddf7c6d12a/core/vm/contract.go">contract.go</a>). It in turn calls self.Address( ).</p></li><li><p>Self is of type ContractRef so type ContractRef must have an Address( ) function.</p></li><li><p>ContractRef is an interface (found in <a href="https://github.com/ethereum/go-ethereum/blob/d4d288e3f1cebb183fce9137829a76ddf7c6d12a/core/vm/contract.go">contract.go</a>) and defines that for something to be a ContractRef it must implement an Address( ) function that returns a common.Address (common.Address is defined as a byte array of length 20, the length of an Ethereum address).</p></li><li><p>If we refer back to section [ 3 ] we discussed the different AccountRef values in evm.DelegateCall and evm.Call which became &#8220;self&#8221; on the Contract objects. We can see that AccountRef is actually just a common.Address but it implements an Address( ) function. AccountRef, therefore, meets the ContractRef interface requirements.</p></li><li><p>The Address( ) function for a AccountRef just casts the AccountRef to an common.Address which in our  case would be Contract A&#8217;s address for evm.DelegateCall &amp; Contract B&#8217;s address for evm.Call. This means the SLOAD opcode we looked at in [ 8 ] is looking at Contract A&#8217;s storage for the DELEGATECALL opcode and Contract B&#8217;s storage for the CALL opcode.</p></li></ol><p>Seeing the Geth implementation shows you how the storage, msg.sender &amp; msg.value are altered for a DelegateCall. You should now have a comprehensive understanding of the DELEGATECALL opcode.</p><p>You made it congrats! Until next time.</p><p>noxx</p><p>Twitter <a href="https://twitter.com/noxx3xxon">@noxx3xxon</a></p>]]></content:encoded></item><item><title><![CDATA[EVM Deep Dives: The Path to Shadowy Super Coder 🥷 💻 - Part 4]]></title><description><![CDATA[Under The Hood - Opcodes In the Go Ethereum (Geth) Client]]></description><link>https://noxx.substack.com/p/evm-deep-dives-the-path-to-shadowy-5a5</link><guid isPermaLink="false">https://noxx.substack.com/p/evm-deep-dives-the-path-to-shadowy-5a5</guid><dc:creator><![CDATA[noxx]]></dc:creator><pubDate>Fri, 06 May 2022 10:09:43 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/h_600,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F35765b7d-a78d-4350-86b7-02925e25b35a_3226x1178.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>This is the fourth installment in the &#8220;EVM Deep Dives&#8221; series. In <a href="https://noxx.substack.com/p/evm-deep-dives-the-path-to-shadowy-3ea?utm_source=%2Fprofile%2F80455042-noxx&amp;utm_medium=reader2&amp;s=r">Part 3</a> we did a deep dive into contract storage, I now want to show you how an individual contract&#8217;s storage fits into the wider &#8220;World State&#8221; of the Ethereum chain. </p><p>To do this we&#8217;re going to inspect the Ethereum chain architecture, its data structures and peer inside the internals of the &#8220;Go Ethereum&#8221; (Geth) client. </p><p>We&#8217;ll start with the data contained in an Ethereum block and work backward to a specific contract&#8217;s storage. We&#8217;ll finish by taking a look at the SSTORE &amp; SLOAD opcodes implementations in Geth.</p><p>This will serve multiple purposes. It will introduce you to the Geth codebase, teach you about the Ethereum &#8220;World State&#8221; and further your overall understanding of the EVM.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://noxx.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://noxx.substack.com/subscribe?"><span>Subscribe now</span></a></p><h2>Ethereum Architecture</h2><p>We&#8217;ll start with the image below. Do not be intimidated, by the end of this article you&#8217;ll understand exactly how this all fits together. This represents the Ethereum architecture and the data contained within the Ethereum chain. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!YQMl!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6f48734-3cd7-42a0-a2dd-8f09fdd8a90e_1048x728.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!YQMl!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6f48734-3cd7-42a0-a2dd-8f09fdd8a90e_1048x728.png 424w, https://substackcdn.com/image/fetch/$s_!YQMl!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6f48734-3cd7-42a0-a2dd-8f09fdd8a90e_1048x728.png 848w, https://substackcdn.com/image/fetch/$s_!YQMl!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6f48734-3cd7-42a0-a2dd-8f09fdd8a90e_1048x728.png 1272w, https://substackcdn.com/image/fetch/$s_!YQMl!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6f48734-3cd7-42a0-a2dd-8f09fdd8a90e_1048x728.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!YQMl!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6f48734-3cd7-42a0-a2dd-8f09fdd8a90e_1048x728.png" width="1200" height="833.587786259542" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/f6f48734-3cd7-42a0-a2dd-8f09fdd8a90e_1048x728.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:728,&quot;width&quot;:1048,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!YQMl!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6f48734-3cd7-42a0-a2dd-8f09fdd8a90e_1048x728.png 424w, https://substackcdn.com/image/fetch/$s_!YQMl!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6f48734-3cd7-42a0-a2dd-8f09fdd8a90e_1048x728.png 848w, https://substackcdn.com/image/fetch/$s_!YQMl!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6f48734-3cd7-42a0-a2dd-8f09fdd8a90e_1048x728.png 1272w, https://substackcdn.com/image/fetch/$s_!YQMl!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6f48734-3cd7-42a0-a2dd-8f09fdd8a90e_1048x728.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Ethereum Architecture - Source <a href="https://ethereum.stackexchange.com/questions/268/ethereum-block-architecture">Zanzu</a></figcaption></figure></div><p>Rather than look at the diagram as a whole we&#8217;ll analyse it piece by piece. For now, let&#8217;s focus on the &#8220;Block N Header&#8221; and the fields it contains.</p><h3>Block Header</h3><p>The Block Header contains the key information about an Ethereum block. Below is the &#8220;Block N Header&#8221; snippet along with its data fields. Take a look at this block <a href="https://etherscan.io/block/14698834">14698834</a> on etherscan and see if you can see some of the fields in the diagram.   </p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!g8TZ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1e081473-12c5-4b01-ab0b-5f1ec1db106d_482x231.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!g8TZ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1e081473-12c5-4b01-ab0b-5f1ec1db106d_482x231.png 424w, https://substackcdn.com/image/fetch/$s_!g8TZ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1e081473-12c5-4b01-ab0b-5f1ec1db106d_482x231.png 848w, https://substackcdn.com/image/fetch/$s_!g8TZ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1e081473-12c5-4b01-ab0b-5f1ec1db106d_482x231.png 1272w, https://substackcdn.com/image/fetch/$s_!g8TZ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1e081473-12c5-4b01-ab0b-5f1ec1db106d_482x231.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!g8TZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1e081473-12c5-4b01-ab0b-5f1ec1db106d_482x231.png" width="482" height="231" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/1e081473-12c5-4b01-ab0b-5f1ec1db106d_482x231.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:231,&quot;width&quot;:482,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:76516,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!g8TZ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1e081473-12c5-4b01-ab0b-5f1ec1db106d_482x231.png 424w, https://substackcdn.com/image/fetch/$s_!g8TZ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1e081473-12c5-4b01-ab0b-5f1ec1db106d_482x231.png 848w, https://substackcdn.com/image/fetch/$s_!g8TZ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1e081473-12c5-4b01-ab0b-5f1ec1db106d_482x231.png 1272w, https://substackcdn.com/image/fetch/$s_!g8TZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1e081473-12c5-4b01-ab0b-5f1ec1db106d_482x231.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption">Block N Header from Ethereum Architecture Diagram</figcaption></figure></div><p>The block header contains the following fields:</p><ul><li><p>Prev Hash - Keccak hash of the parent block </p></li><li><p>Nonce -  Used in proof of work  computation</p></li><li><p>Timestamp - Scale value of the output of UNIX time( ) </p></li><li><p>Uncles Hash - Keccak hash for uncled blocks </p></li><li><p>Beneficiary - Beneficiary address, mining fee recipient </p></li><li><p>LogsBloom - Bloom filter of two fields, log address &amp; log topic in the receipts</p></li><li><p>Difficulty - Scalar value of the difficulty of the previous block</p></li><li><p>Extra Data - 32 byte data relevant to this block</p></li><li><p>Block Num - Scalar value of the number of ancestor blocks</p></li><li><p>Gas Limit - Scalar value of the current limit of gas usage per block</p></li><li><p>Gas Used - Scalar value of the total gas spent on transactions in this block</p></li><li><p>Mix Hash - 256-bit value used with a nonce to prove proof of work computation</p></li><li><p><em><strong>State Root - Keccak Hash of the root node of state trie (post-execution)</strong></em></p></li><li><p>Transaction Root - Keccak Hash of the root node of transaction trie</p></li><li><p>Receipt Root - Keccak Hash of the root node of receipt trie</p></li></ul><p>Let&#8217;s see how these fields map to what is in the Geth client codebase. We&#8217;ll look at the &#8220;Header&#8221; struct defined in <a href="https://github.com/ethereum/go-ethereum/blob/d4d288e3f1cebb183fce9137829a76ddf7c6d12a/core/types/block.go#L70">block.go</a> which represents a block header.  </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!_rqj!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F35765b7d-a78d-4350-86b7-02925e25b35a_3226x1178.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!_rqj!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F35765b7d-a78d-4350-86b7-02925e25b35a_3226x1178.png 424w, https://substackcdn.com/image/fetch/$s_!_rqj!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F35765b7d-a78d-4350-86b7-02925e25b35a_3226x1178.png 848w, https://substackcdn.com/image/fetch/$s_!_rqj!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F35765b7d-a78d-4350-86b7-02925e25b35a_3226x1178.png 1272w, https://substackcdn.com/image/fetch/$s_!_rqj!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F35765b7d-a78d-4350-86b7-02925e25b35a_3226x1178.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!_rqj!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F35765b7d-a78d-4350-86b7-02925e25b35a_3226x1178.png" width="1200" height="438.46153846153845" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/35765b7d-a78d-4350-86b7-02925e25b35a_3226x1178.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:532,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:328863,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!_rqj!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F35765b7d-a78d-4350-86b7-02925e25b35a_3226x1178.png 424w, https://substackcdn.com/image/fetch/$s_!_rqj!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F35765b7d-a78d-4350-86b7-02925e25b35a_3226x1178.png 848w, https://substackcdn.com/image/fetch/$s_!_rqj!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F35765b7d-a78d-4350-86b7-02925e25b35a_3226x1178.png 1272w, https://substackcdn.com/image/fetch/$s_!_rqj!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F35765b7d-a78d-4350-86b7-02925e25b35a_3226x1178.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Code at <a href="https://github.com/ethereum/go-ethereum/blob/d4d288e3f1cebb183fce9137829a76ddf7c6d12a/core/types/block.go">go-ethereum/core/types/block.go</a></figcaption></figure></div><p>We can see the values stated within the codebase match our conceptual diagram. Our goal is to get from the block header down to the storage of an individual contract. </p><p>To do so we need to focus on the &#8220;State Root&#8221; field of the block header highlighted in red.</p><h3>State Root</h3><p>The &#8220;State Root&#8221; acts like a <a href="https://www.investopedia.com/terms/m/merkle-root-cryptocurrency.asp#:~:text=A%20Merkle%20root%20is%20a,whole%2C%20undamaged%2C%20and%20unaltered.">merkle root</a> in that it is a hash that is dependent on all the pieces of data that lie underneath it. If any piece of data changes the root will also change.</p><p>The data structure underneath the &#8220;State Root&#8221; is a Merkle Patricia Trie which stores a key-value pair for every Ethereum account on the network, where the key is an Ethereum address and the value is the Ethereum account object. </p><p><em>In actuality, the key is the hash of the Ethereum address and the value is the RLP encoded Ethereum account however we can ignore this for now. </em></p><p>Below is the section of the &#8220;Ethereum Architecture&#8221; diagram that represents the Merkel Patricia Trie for the &#8220;State Root&#8221;.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!V3x1!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd1cf36d8-9880-4aa1-9b7b-a4176273d210_178x258.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!V3x1!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd1cf36d8-9880-4aa1-9b7b-a4176273d210_178x258.png 424w, https://substackcdn.com/image/fetch/$s_!V3x1!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd1cf36d8-9880-4aa1-9b7b-a4176273d210_178x258.png 848w, https://substackcdn.com/image/fetch/$s_!V3x1!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd1cf36d8-9880-4aa1-9b7b-a4176273d210_178x258.png 1272w, https://substackcdn.com/image/fetch/$s_!V3x1!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd1cf36d8-9880-4aa1-9b7b-a4176273d210_178x258.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!V3x1!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd1cf36d8-9880-4aa1-9b7b-a4176273d210_178x258.png" width="178" height="258" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/d1cf36d8-9880-4aa1-9b7b-a4176273d210_178x258.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:258,&quot;width&quot;:178,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:27369,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!V3x1!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd1cf36d8-9880-4aa1-9b7b-a4176273d210_178x258.png 424w, https://substackcdn.com/image/fetch/$s_!V3x1!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd1cf36d8-9880-4aa1-9b7b-a4176273d210_178x258.png 848w, https://substackcdn.com/image/fetch/$s_!V3x1!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd1cf36d8-9880-4aa1-9b7b-a4176273d210_178x258.png 1272w, https://substackcdn.com/image/fetch/$s_!V3x1!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd1cf36d8-9880-4aa1-9b7b-a4176273d210_178x258.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Merkle Patricia Trie underneath the State Root - Snippet from Ethereum Architecture Diagram</figcaption></figure></div><p>The Merkle Patricia Trie is a non-trival data structure so we won&#8217;t deep dive into it in this article. Instead, we can instead keep the key-value mapping model of address to the Ethereum account.</p><p>If you are interested in the Merkle Patricia Trie I recommend checking out <a href="https://medium.com/shyft-network-media/understanding-trie-databases-in-ethereum-9f03d2c3325d">this</a> excellent introductory article.</p><p>Next, let&#8217;s inspect the Ethereum account value that the Ethereum addresses are mapping to. </p><h3>Ethereum Account</h3><p>The Ethereum account is the consensus representation for an Ethereum address. It is  made up of 4 items.</p><ul><li><p>Nonce - Number of transactions made by the account </p></li><li><p>Balance - Balance of the account in Wei</p></li><li><p>Code Hash - Hash of the bytecode stored in the contract/account</p></li><li><p>Storage Root - Keccak Hash of the root node of storage trie (post-execution) </p></li></ul><p>We see these in this snippet from the original Ethereum architecture image.   </p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!-cHC!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F0f0b898c-8fb1-469a-9fb1-0346ccbae601_409x57.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!-cHC!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F0f0b898c-8fb1-469a-9fb1-0346ccbae601_409x57.png 424w, https://substackcdn.com/image/fetch/$s_!-cHC!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F0f0b898c-8fb1-469a-9fb1-0346ccbae601_409x57.png 848w, https://substackcdn.com/image/fetch/$s_!-cHC!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F0f0b898c-8fb1-469a-9fb1-0346ccbae601_409x57.png 1272w, https://substackcdn.com/image/fetch/$s_!-cHC!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F0f0b898c-8fb1-469a-9fb1-0346ccbae601_409x57.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!-cHC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F0f0b898c-8fb1-469a-9fb1-0346ccbae601_409x57.png" width="409" height="57" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/0f0b898c-8fb1-469a-9fb1-0346ccbae601_409x57.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:57,&quot;width&quot;:409,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:19971,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!-cHC!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F0f0b898c-8fb1-469a-9fb1-0346ccbae601_409x57.png 424w, https://substackcdn.com/image/fetch/$s_!-cHC!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F0f0b898c-8fb1-469a-9fb1-0346ccbae601_409x57.png 848w, https://substackcdn.com/image/fetch/$s_!-cHC!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F0f0b898c-8fb1-469a-9fb1-0346ccbae601_409x57.png 1272w, https://substackcdn.com/image/fetch/$s_!-cHC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F0f0b898c-8fb1-469a-9fb1-0346ccbae601_409x57.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption">Ethereum Account and its fields - Snippet from Ethereum Architecture Diagram </figcaption></figure></div><p>Again we can jump into the Geth codebase and find the corresponding file <a href="https://github.com/ethereum/go-ethereum/blob/b1e72f7ea998ad662166bcf23705ca59cf81e925/core/types/state_account.go#L27">state_account.go</a> and the struct that defines an &#8220;Ethereum account&#8221; referred to as a StateAccount. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!WgSu!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F630df44e-bdab-41b9-8894-f09be4a09ea7_2122x574.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!WgSu!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F630df44e-bdab-41b9-8894-f09be4a09ea7_2122x574.png 424w, https://substackcdn.com/image/fetch/$s_!WgSu!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F630df44e-bdab-41b9-8894-f09be4a09ea7_2122x574.png 848w, https://substackcdn.com/image/fetch/$s_!WgSu!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F630df44e-bdab-41b9-8894-f09be4a09ea7_2122x574.png 1272w, https://substackcdn.com/image/fetch/$s_!WgSu!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F630df44e-bdab-41b9-8894-f09be4a09ea7_2122x574.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!WgSu!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F630df44e-bdab-41b9-8894-f09be4a09ea7_2122x574.png" width="1200" height="324.72527472527474" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/630df44e-bdab-41b9-8894-f09be4a09ea7_2122x574.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:394,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:114617,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!WgSu!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F630df44e-bdab-41b9-8894-f09be4a09ea7_2122x574.png 424w, https://substackcdn.com/image/fetch/$s_!WgSu!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F630df44e-bdab-41b9-8894-f09be4a09ea7_2122x574.png 848w, https://substackcdn.com/image/fetch/$s_!WgSu!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F630df44e-bdab-41b9-8894-f09be4a09ea7_2122x574.png 1272w, https://substackcdn.com/image/fetch/$s_!WgSu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F630df44e-bdab-41b9-8894-f09be4a09ea7_2122x574.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Code at <a href="https://github.com/ethereum/go-ethereum/blob/b1e72f7ea998ad662166bcf23705ca59cf81e925/core/types/state_account.go">go-ethereum/core/types/state_account.go</a></figcaption></figure></div><p>Again we can see the values stated within the codebase match our conceptual diagram.</p><p>Next, we need to zoom in on the &#8220;Storage Root&#8221; field within the Ethereum account.</p><h3>Storage Root</h3><p>The storage root is much like the state root in that underneath it is another Merkle Patricia trie.</p><p>The difference is that this time the keys are the storage slots and the values are the data in each slot. </p><p><em>Again in actuality, there is RLP encoding of the values &amp; hashing of the keys that goes on as part of this process.</em></p><p>Below is the section of the &#8220;Ethereum Architecture&#8221; diagram that represents the Merkel Patricia Trie for the &#8220;Storage Root&#8221;.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!3Zsb!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F18d30b84-5853-4505-9466-a8c2804be40e_196x260.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!3Zsb!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F18d30b84-5853-4505-9466-a8c2804be40e_196x260.png 424w, https://substackcdn.com/image/fetch/$s_!3Zsb!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F18d30b84-5853-4505-9466-a8c2804be40e_196x260.png 848w, https://substackcdn.com/image/fetch/$s_!3Zsb!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F18d30b84-5853-4505-9466-a8c2804be40e_196x260.png 1272w, https://substackcdn.com/image/fetch/$s_!3Zsb!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F18d30b84-5853-4505-9466-a8c2804be40e_196x260.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!3Zsb!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F18d30b84-5853-4505-9466-a8c2804be40e_196x260.png" width="196" height="260" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/18d30b84-5853-4505-9466-a8c2804be40e_196x260.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:260,&quot;width&quot;:196,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:23445,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!3Zsb!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F18d30b84-5853-4505-9466-a8c2804be40e_196x260.png 424w, https://substackcdn.com/image/fetch/$s_!3Zsb!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F18d30b84-5853-4505-9466-a8c2804be40e_196x260.png 848w, https://substackcdn.com/image/fetch/$s_!3Zsb!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F18d30b84-5853-4505-9466-a8c2804be40e_196x260.png 1272w, https://substackcdn.com/image/fetch/$s_!3Zsb!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F18d30b84-5853-4505-9466-a8c2804be40e_196x260.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Merkle Patricia Trie underneath the Storage Root - Snippet from Ethereum Architecture Diagram</figcaption></figure></div><p>As before the &#8220;Storage Root&#8221; is a <a href="https://www.investopedia.com/terms/m/merkle-root-cryptocurrency.asp#:~:text=A%20Merkle%20root%20is%20a,whole%2C%20undamaged%2C%20and%20unaltered.">merkle root</a> hash that will be impacted if any of the underlying data (contract storage) changes. </p><p>Any change in contract storage will impact the &#8220;Storage Root&#8221; which in turn will impact the &#8220;State Root&#8221; which in turn will impact the &#8220;Block Header&#8221;.</p><p>At this stage of the article, we&#8217;ve achieved our goal of taking you from an Ethereum block down to an individual contract&#8217;s storage. </p><p>The next part of the article is a deep dive into the Geth codebase. We will look briefly at how the contract storage is initialised and what happens when the SSTORE &amp; SLOAD opcodes are called.</p><p>This will help you make the mental connections from what we&#8217;ve discussed so far back to your solidity code and the underlying storage opcodes.</p><p><em>A warning, the next section is heavy on the code side and assumes the ability to read code </em></p><div><hr></div><h3>StateDB &#8594; stateObject &#8594; StateAccount</h3><p>To get us started we need a brand new contract. A brand new contract means a brand new StateAccount.</p><p>Before we start there are 3 structures we&#8217;re going to be interfacing with:</p><ul><li><p> StateAccount </p><ul><li><p>StateAccount is the Ethereum consensus representation of &#8220;<strong>Ethereum accounts</strong>&#8221;.</p></li></ul></li><li><p>stateObject</p><ul><li><p>stateObject represents an &#8220;<strong>Ethereum account</strong>&#8221; that is being modified. </p></li></ul></li><li><p>StateDB</p><ul><li><p>StateDB structs within the Ethereum protocol are used to store anything within the merkle trie. It's the general query interface to retrieve: Contracts &amp; <strong>Ethereum Accounts</strong></p></li></ul></li></ul><p>Let&#8217;s look at how these 3 items are interconnected and how they relate back to what we have been discussing.  </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!tS33!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc0e579c8-adb2-4679-9744-03c4bd3c1182_2002x1572.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!tS33!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc0e579c8-adb2-4679-9744-03c4bd3c1182_2002x1572.png 424w, https://substackcdn.com/image/fetch/$s_!tS33!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc0e579c8-adb2-4679-9744-03c4bd3c1182_2002x1572.png 848w, https://substackcdn.com/image/fetch/$s_!tS33!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc0e579c8-adb2-4679-9744-03c4bd3c1182_2002x1572.png 1272w, https://substackcdn.com/image/fetch/$s_!tS33!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc0e579c8-adb2-4679-9744-03c4bd3c1182_2002x1572.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!tS33!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc0e579c8-adb2-4679-9744-03c4bd3c1182_2002x1572.png" width="728" height="571.5" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/c0e579c8-adb2-4679-9744-03c4bd3c1182_2002x1572.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;normal&quot;,&quot;height&quot;:1143,&quot;width&quot;:1456,&quot;resizeWidth&quot;:728,&quot;bytes&quot;:270250,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!tS33!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc0e579c8-adb2-4679-9744-03c4bd3c1182_2002x1572.png 424w, https://substackcdn.com/image/fetch/$s_!tS33!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc0e579c8-adb2-4679-9744-03c4bd3c1182_2002x1572.png 848w, https://substackcdn.com/image/fetch/$s_!tS33!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc0e579c8-adb2-4679-9744-03c4bd3c1182_2002x1572.png 1272w, https://substackcdn.com/image/fetch/$s_!tS33!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc0e579c8-adb2-4679-9744-03c4bd3c1182_2002x1572.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">StateDB &#8594; stateObject &#8594; StateAccount </figcaption></figure></div><ol><li><p><a href="https://github.com/ethereum/go-ethereum/blob/d4d288e3f1cebb183fce9137829a76ddf7c6d12a/core/state/statedb.go#L64">StateDB struct</a>, we can see it has a stateObjects field which is a mapping of addresses to stateObjects (Remember the &#8220;State Root&#8221; Merkle Patricia Trie was a mapping of Ethereum addresses to Ethereum accounts and that a stateObject is an Ethereum account that is being modified)</p></li><li><p><a href="https://github.com/ethereum/go-ethereum/blob/d4d288e3f1cebb183fce9137829a76ddf7c6d12a/core/state/state_object.go#L66">stateObject struct</a>, we can see it has a data field which is of type StateAccount (Remember that earlier in the article we mapped an Ethereum account to StateAccount in Geth)</p></li><li><p><a href="https://github.com/ethereum/go-ethereum/blob/d4d288e3f1cebb183fce9137829a76ddf7c6d12a/core/types/state_account.go#L29">StateAccount struct</a>, we have seen this struct already, it represents an Ethereum account and the Root field represents the &#8220;Storage Root&#8221; we discussed earlier</p></li></ol><p>At this stage some pieces of the puzzle are starting to fit together. Now we have the context to see how a new &#8220;Ethereum account&#8221; (StateAccount) is initialised.</p><h3>Initalising A New Ethereum Account (StateAccount)</h3><p>To create a new StateAccount we&#8217;ll need to interact with the <a href="https://github.com/ethereum/go-ethereum/blob/d4d288e3f1cebb183fce9137829a76ddf7c6d12a/core/state/statedb.go">statedb.go</a> file and the StateDB struct. </p><p>StateDB has a createObject function which creates a new stateObject and passes a blank StateAccount into it. This is effectively creating a blank &#8220;Ethereum account&#8221;.</p><p>The below diagram details the code flow.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!IGLH!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5b2cc945-1634-4315-9adc-f5a2a147406e_3296x1104.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!IGLH!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5b2cc945-1634-4315-9adc-f5a2a147406e_3296x1104.png 424w, https://substackcdn.com/image/fetch/$s_!IGLH!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5b2cc945-1634-4315-9adc-f5a2a147406e_3296x1104.png 848w, https://substackcdn.com/image/fetch/$s_!IGLH!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5b2cc945-1634-4315-9adc-f5a2a147406e_3296x1104.png 1272w, https://substackcdn.com/image/fetch/$s_!IGLH!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5b2cc945-1634-4315-9adc-f5a2a147406e_3296x1104.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!IGLH!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5b2cc945-1634-4315-9adc-f5a2a147406e_3296x1104.png" width="1200" height="402.1978021978022" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/5b2cc945-1634-4315-9adc-f5a2a147406e_3296x1104.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:488,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:422591,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!IGLH!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5b2cc945-1634-4315-9adc-f5a2a147406e_3296x1104.png 424w, https://substackcdn.com/image/fetch/$s_!IGLH!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5b2cc945-1634-4315-9adc-f5a2a147406e_3296x1104.png 848w, https://substackcdn.com/image/fetch/$s_!IGLH!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5b2cc945-1634-4315-9adc-f5a2a147406e_3296x1104.png 1272w, https://substackcdn.com/image/fetch/$s_!IGLH!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5b2cc945-1634-4315-9adc-f5a2a147406e_3296x1104.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">StateAccount initialisation</figcaption></figure></div><ol><li><p>StateDB has a <a href="https://github.com/ethereum/go-ethereum/blob/d4d288e3f1cebb183fce9137829a76ddf7c6d12a/core/state/statedb.go#L575">createObject function</a> which takes in a Ethereum address and returns a stateObject (Remember a stateObject represents an Ethereum account that is being modified.)</p></li><li><p>The createObject function calls the <a href="https://github.com/ethereum/go-ethereum/blob/d4d288e3f1cebb183fce9137829a76ddf7c6d12a/core/state/state_object.go#L102">newObject function</a> passing in the stateDB, the address and an empty StateAccount (Remember a StateAccount =  Ethereum account) it returns a stateObject</p></li><li><p>The return statement of the newObject function, we can see there are a number of fields associated with the stateObject, address, data, dirtyStorage etc.</p></li><li><p>The stateObject data field maps to the empty StateAccount input in the function - Note nil values are replaced in the StateAccount in lines 103 - 111 </p></li><li><p>The stateObject created that contains the initialised StateAccount as a data field is returned</p></li></ol><p>Ok, so we have an empty stateAccount, what do we want to do next? </p><p>We want to store some data and to do that we need to use the SSTORE opcode.  </p><h2>SSTORE</h2><p>Before we dive into the SSTORE implementation in Geth let&#8217;s quickly remind ourselves of what SSTORE does. </p><p>It pops 2 values off the stack, first the 32-byte key second the 32-byte value and stores that value at the specified storage slot defined by the key.</p><p>Below is the Geth code flow for the SSTORE opcode let&#8217;s take a look at what it does.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!EKCq!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8b9e61f6-7a74-48db-9c07-b2a3717bd3b5_2882x1762.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!EKCq!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8b9e61f6-7a74-48db-9c07-b2a3717bd3b5_2882x1762.png 424w, https://substackcdn.com/image/fetch/$s_!EKCq!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8b9e61f6-7a74-48db-9c07-b2a3717bd3b5_2882x1762.png 848w, https://substackcdn.com/image/fetch/$s_!EKCq!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8b9e61f6-7a74-48db-9c07-b2a3717bd3b5_2882x1762.png 1272w, https://substackcdn.com/image/fetch/$s_!EKCq!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8b9e61f6-7a74-48db-9c07-b2a3717bd3b5_2882x1762.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!EKCq!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8b9e61f6-7a74-48db-9c07-b2a3717bd3b5_2882x1762.png" width="1200" height="733.5164835164835" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/8b9e61f6-7a74-48db-9c07-b2a3717bd3b5_2882x1762.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:890,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:326297,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!EKCq!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8b9e61f6-7a74-48db-9c07-b2a3717bd3b5_2882x1762.png 424w, https://substackcdn.com/image/fetch/$s_!EKCq!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8b9e61f6-7a74-48db-9c07-b2a3717bd3b5_2882x1762.png 848w, https://substackcdn.com/image/fetch/$s_!EKCq!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8b9e61f6-7a74-48db-9c07-b2a3717bd3b5_2882x1762.png 1272w, https://substackcdn.com/image/fetch/$s_!EKCq!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8b9e61f6-7a74-48db-9c07-b2a3717bd3b5_2882x1762.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">SSTORE opcode Geth Implementation</figcaption></figure></div><ol><li><p>We start with the <a href="https://github.com/ethereum/go-ethereum/blob/d4d288e3f1cebb183fce9137829a76ddf7c6d12a/core/vm/instructions.go#L524">instructions.go file</a> which defines all the EVM opcodes. Within this file, we find the &#8220;opSstore&#8221; function.</p></li><li><p>The scope variable which is passed into the function contains the contract context such as the stack, memory etc. We pop 2 values off the stack and label them loc (short for location) and val (short for value).</p></li><li><p>The 2 values popped off the stack are then used as inputs along with the contract address for the <a href="https://github.com/ethereum/go-ethereum/blob/d4d288e3f1cebb183fce9137829a76ddf7c6d12a/core/state/statedb.go#L414">SetState function</a> associated with the StateDB. The SetState function uses the contract address to check if a stateObject exists for that contract, if it doesn&#8217;t it will create one. It then calls SetState on that stateObject passing in the StateDB db, the key and the value.</p></li><li><p>The stateObject <a href="https://github.com/ethereum/go-ethereum/blob/d4d288e3f1cebb183fce9137829a76ddf7c6d12a/core/state/state_object.go#L245">SetState function</a> does some checks on fake storage and whether the value has changed and then runs a journal append.</p></li><li><p>If you look at the code comment about the <a href="https://github.com/ethereum/go-ethereum/blob/d4d288e3f1cebb183fce9137829a76ddf7c6d12a/core/state/journal.go#L38">journal struct</a> you&#8217;ll see that the journal is used to track state modifications so that they can be reverted in the case of an execution exception or request for reversal.</p></li><li><p>After the journal is updated the storageObject <a href="https://github.com/ethereum/go-ethereum/blob/d4d288e3f1cebb183fce9137829a76ddf7c6d12a/core/state/state_object.go#L283">setState function</a> is called with the key and value. This updates the storageObjects dirtyStorage.</p></li></ol><p>Ok, so we have updated the stateObject&#8217;s dirtyStorage with the key and value. What does that actually mean and how does it relate to everything we&#8217;ve learned so far. </p><p>Let&#8217;s start with the dirtyStorage definition within the code.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!CsjD!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe62db976-b29d-4803-af46-22bc577d5755_2210x1794.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!CsjD!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe62db976-b29d-4803-af46-22bc577d5755_2210x1794.png 424w, https://substackcdn.com/image/fetch/$s_!CsjD!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe62db976-b29d-4803-af46-22bc577d5755_2210x1794.png 848w, https://substackcdn.com/image/fetch/$s_!CsjD!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe62db976-b29d-4803-af46-22bc577d5755_2210x1794.png 1272w, https://substackcdn.com/image/fetch/$s_!CsjD!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe62db976-b29d-4803-af46-22bc577d5755_2210x1794.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!CsjD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe62db976-b29d-4803-af46-22bc577d5755_2210x1794.png" width="728" height="591" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/e62db976-b29d-4803-af46-22bc577d5755_2210x1794.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;normal&quot;,&quot;height&quot;:1182,&quot;width&quot;:1456,&quot;resizeWidth&quot;:728,&quot;bytes&quot;:269555,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!CsjD!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe62db976-b29d-4803-af46-22bc577d5755_2210x1794.png 424w, https://substackcdn.com/image/fetch/$s_!CsjD!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe62db976-b29d-4803-af46-22bc577d5755_2210x1794.png 848w, https://substackcdn.com/image/fetch/$s_!CsjD!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe62db976-b29d-4803-af46-22bc577d5755_2210x1794.png 1272w, https://substackcdn.com/image/fetch/$s_!CsjD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe62db976-b29d-4803-af46-22bc577d5755_2210x1794.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">dirtyStorage &#8594; Storage &#8594; Hash &#8594; 32-byte</figcaption></figure></div><ol><li><p>dirtyStorage is defined within the <a href="https://github.com/ethereum/go-ethereum/blob/d4d288e3f1cebb183fce9137829a76ddf7c6d12a/core/state/state_object.go#L66">stateObject struct</a>, it is of type Storage and is described as &#8220;Storage entries that have been modified in the current transaction execution&#8221;</p></li><li><p>The <a href="https://github.com/ethereum/go-ethereum/blob/d4d288e3f1cebb183fce9137829a76ddf7c6d12a/core/state/state_object.go#L41">Storage type</a> which corresponds to dirtyStorage is a simple mapping of common.Hash to common.Hash</p></li><li><p>The <a href="https://github.com/ethereum/go-ethereum/blob/d4d288e3f1cebb183fce9137829a76ddf7c6d12a/common/types.go#L49">Hash type</a> is simply a byte array of length HashLength</p></li><li><p><a href="https://github.com/ethereum/go-ethereum/blob/d4d288e3f1cebb183fce9137829a76ddf7c6d12a/common/types.go#L36">HashLength</a> is a constant defined as 32</p></li></ol><p>This should sound familiar to you a 32-byte key mapping to a 32-byte value. It is exactly how we conceptually viewed contract storage in <a href="https://noxx.substack.com/p/evm-deep-dives-the-path-to-shadowy-3ea?utm_source=%2Fprofile%2F80455042-noxx&amp;utm_medium=reader2&amp;s=r">Part 3 of the EVM Deep Dives</a>.  </p><p>You may have noticed pendingStorage &amp; originStorage in the stateObject just above the dirtyStorage field. They are all related, during finalisation dirtyStorage is copied across to pendingStorage which in turn is copied across to originStorage when the trie is updated.</p><p>After the trie is updated the &#8220;Storage Root&#8221; of the StateAccount will also be updated during a StateDB &#8220;Commit&#8221;. This writes the new state to the underlying in-memory trie database. </p><p>Now onto the last piece of the puzzle, SLOAD.</p><h2>SLOAD  </h2><p>Again let&#8217;s quickly remind ourselves of what SLOAD does. </p><p>It pops 1 value off the stack, the 32-byte key, which represents the storage slot and returns the 32-byte value stored there.</p><p>Below is the Geth code flow for the SLOAD opcode let&#8217;s take a look at what it does</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!XokZ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F991e7cec-54bd-41b2-afe4-247144fea091_3430x1316.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!XokZ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F991e7cec-54bd-41b2-afe4-247144fea091_3430x1316.png 424w, https://substackcdn.com/image/fetch/$s_!XokZ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F991e7cec-54bd-41b2-afe4-247144fea091_3430x1316.png 848w, https://substackcdn.com/image/fetch/$s_!XokZ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F991e7cec-54bd-41b2-afe4-247144fea091_3430x1316.png 1272w, https://substackcdn.com/image/fetch/$s_!XokZ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F991e7cec-54bd-41b2-afe4-247144fea091_3430x1316.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!XokZ!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F991e7cec-54bd-41b2-afe4-247144fea091_3430x1316.png" width="1200" height="460.7142857142857" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/991e7cec-54bd-41b2-afe4-247144fea091_3430x1316.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:559,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:327497,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!XokZ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F991e7cec-54bd-41b2-afe4-247144fea091_3430x1316.png 424w, https://substackcdn.com/image/fetch/$s_!XokZ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F991e7cec-54bd-41b2-afe4-247144fea091_3430x1316.png 848w, https://substackcdn.com/image/fetch/$s_!XokZ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F991e7cec-54bd-41b2-afe4-247144fea091_3430x1316.png 1272w, https://substackcdn.com/image/fetch/$s_!XokZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F991e7cec-54bd-41b2-afe4-247144fea091_3430x1316.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">SLOAD opcode Geth Implementation</figcaption></figure></div><ol><li><p>Again we start with the <a href="https://github.com/ethereum/go-ethereum/blob/d4d288e3f1cebb183fce9137829a76ddf7c6d12a/core/vm/instructions.go#L516">instructions.go file</a> where we can find the &#8220;opSload&#8221; function. We grab the location (storage slot) for the SLOAD from the top of the stack using peek.</p></li><li><p>We call the <a href="https://github.com/ethereum/go-ethereum/blob/d4d288e3f1cebb183fce9137829a76ddf7c6d12a/core/state/statedb.go#L308">GetState function</a> on the StateDB passing in the contract address and the storage location. GetState gets the stateObject associated with that contract address. If the stateObject is not nil it calls GetState on that stateObject.</p></li><li><p>The <a href="https://github.com/ethereum/go-ethereum/blob/d4d288e3f1cebb183fce9137829a76ddf7c6d12a/core/state/state_object.go#L172">GetState function</a> on the stateObject does a check on fakeStorage and then does a check on dirtyStorage</p></li><li><p>If dirtyStorage exists return the value at the key location in the dirtyStorage mapping. (dirtyStorage represents the most up-to-date state of the contract which is why we try to return that first)</p></li><li><p>Otherwise call the <a href="https://github.com/ethereum/go-ethereum/blob/d4d288e3f1cebb183fce9137829a76ddf7c6d12a/core/state/state_object.go#L187">GetCommitedState function</a> to look up the value in the storage trie. Again fakeStorage is checked for.</p></li><li><p>If pendingStorage exists return the value at the key location in the pendingStorage mapping. </p></li><li><p>If none of the above have returned go to the originStorage and retrieve &amp; return the value from there.</p></li></ol><p>You&#8217;ll have noticed that the function attempted to return dirtyStorage first, then pendingStorage, then originStorage. This makes sense, during execution dirtyStorage is the most up-to-date storage mapping followed by pending and then the originStorage.</p><p>One transaction can manipulate a single storage slot multiple times so we must ensure we have the most recent value.</p><p>Let&#8217;s imagine an SSTORE that happens before an SLOAD on the same slot and in the same transaction. In this situation the dirtyStorage would be updated in the SSTORE and in the SLOAD it would be returned. </p><p>There you have it you now have an understanding of how SSTORE &amp; SLOAD are implemented at the Geth client level. How they interact with state &amp; storage objects and how updating a storage slot relates to the wider Ethereum &#8220;World State&#8221;.</p><p>That was intense but you made it. My guess is this article has left you with more questions than you had before you started but that&#8217;s part of the fun of crypto.</p><p>Keep grinding anon.</p><p>noxx</p><p>Twitter <a href="https://twitter.com/noxx3xxon">@noxx3xxon</a></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://noxx.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading noxx! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[EVM Deep Dives: The Path to Shadowy Super Coder 🥷 💻 - Part 3]]></title><description><![CDATA[Demystifying Storage Slot Packing - Key Knowledge for the EVM Hackers]]></description><link>https://noxx.substack.com/p/evm-deep-dives-the-path-to-shadowy-3ea</link><guid isPermaLink="false">https://noxx.substack.com/p/evm-deep-dives-the-path-to-shadowy-3ea</guid><dc:creator><![CDATA[noxx]]></dc:creator><pubDate>Sun, 27 Mar 2022 20:01:40 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!cjex!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F13728261-0af4-4581-b03f-04057bdbb5dc_744x370.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>This is the third installment in the &#8220;EVM Deep Dives&#8221; series, this article will build on the knowledge gained from <a href="https://noxx.substack.com/p/evm-deep-dives-the-path-to-shadowy?utm_source=url&amp;s=r">Part 1</a> &amp; <a href="https://noxx.substack.com/p/evm-deep-dives-the-path-to-shadowy-d6b?s=r">Part 2</a> so if you haven&#8217;t read these yet I encourage you to do so.</p><p>In this episode, we&#8217;ll dig into how contract storage works, provide some mental models to aid your understanding and deep dive into storage slot packing. If the term &#8220;slot packing&#8221; is foreign to you don&#8217;t worry, knowledge of slot packing is vital for EVM hackers and you&#8217;ll have a deep understanding of it by the end of the article. </p><p>If you&#8217;ve ever attempted the <a href="https://ethernaut.openzeppelin.com/">Ethernaut Solidity Wargame Series</a> or other Solidity &#8220;Capture The Flag&#8221; type games you&#8217;ll know slot packing knowledge is often the key to cracking the puzzles/hacks.</p><h2>Storage Fundamentals</h2><p>A high-level overview of storage fundamentals has been done brilliantly in <a href="https://programtheblockchain.com/posts/2018/03/09/understanding-ethereum-smart-contract-storage/">this</a> <a href="https://programtheblockchain.com/posts/2018/03/09/understanding-ethereum-smart-contract-storage/">&#8220;Program the Blockchain&#8221;</a> post. I&#8217;ll provide a refresher of the key points required for this article however I highly recommend reading their full article.</p><h4>Data Structure</h4><p>We&#8217;ll start with the data structure of contract storage, this provides us a solid foundation upon which the rest of our knowledge will sit. </p><p>Contract storage is simply a key to value mapping. It maps a 32-byte key to a 32-byte value. Given our key is 32 bytes in size we can have a maximum of (2^256)-1 keys. </p><p>32 bytes is equal to 256 bits which gives us (2^256)-1 binary numbers to choose from for the key.</p><p>All values are initialised as 0 and zeros are not explicitly stored. This makes sense since 2^256 is approximately the number of atoms in the known, observable universe.</p><p>No computer can hold that much data. This is also the reason setting a storage value to zero refunds you some gas as that key value no longer needs to be stored by the nodes on the network. </p><p>Conceptually storage can be viewed as an astronomically large array. Our first key with binary value 0 represents item 0 in the array, the key with binary value 1 represents item 1 in the array etc.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!cjex!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F13728261-0af4-4581-b03f-04057bdbb5dc_744x370.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!cjex!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F13728261-0af4-4581-b03f-04057bdbb5dc_744x370.png 424w, https://substackcdn.com/image/fetch/$s_!cjex!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F13728261-0af4-4581-b03f-04057bdbb5dc_744x370.png 848w, https://substackcdn.com/image/fetch/$s_!cjex!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F13728261-0af4-4581-b03f-04057bdbb5dc_744x370.png 1272w, https://substackcdn.com/image/fetch/$s_!cjex!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F13728261-0af4-4581-b03f-04057bdbb5dc_744x370.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!cjex!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F13728261-0af4-4581-b03f-04057bdbb5dc_744x370.png" width="744" height="370" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/13728261-0af4-4581-b03f-04057bdbb5dc_744x370.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:370,&quot;width&quot;:744,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:21297,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!cjex!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F13728261-0af4-4581-b03f-04057bdbb5dc_744x370.png 424w, https://substackcdn.com/image/fetch/$s_!cjex!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F13728261-0af4-4581-b03f-04057bdbb5dc_744x370.png 848w, https://substackcdn.com/image/fetch/$s_!cjex!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F13728261-0af4-4581-b03f-04057bdbb5dc_744x370.png 1272w, https://substackcdn.com/image/fetch/$s_!cjex!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F13728261-0af4-4581-b03f-04057bdbb5dc_744x370.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h4>Fixed Size Variables</h4><p>Contract variables that are declared as storage variables can be split into 2 camps fixed-size and dynamic-size. We&#8217;re going to focus on fixed-size variables and how the EVM can pack multiple variables into a single 32-byte storage slot. </p><p>To learn more about dynamic size variables refer to the <a href="https://programtheblockchain.com/posts/2018/03/09/understanding-ethereum-smart-contract-storage/">&#8220;Program the Blockchain&#8221;</a> <a href="https://programtheblockchain.com/posts/2018/03/09/understanding-ethereum-smart-contract-storage/">post</a>.</p><p>Now that we know storage is a key-value mapping, the next question is how are the keys assigned to the variables. Say we have the following solidity code.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!8wfB!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F211a6f5d-9a81-41b9-9643-e31c141d0827_335x298.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!8wfB!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F211a6f5d-9a81-41b9-9643-e31c141d0827_335x298.png 424w, https://substackcdn.com/image/fetch/$s_!8wfB!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F211a6f5d-9a81-41b9-9643-e31c141d0827_335x298.png 848w, https://substackcdn.com/image/fetch/$s_!8wfB!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F211a6f5d-9a81-41b9-9643-e31c141d0827_335x298.png 1272w, https://substackcdn.com/image/fetch/$s_!8wfB!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F211a6f5d-9a81-41b9-9643-e31c141d0827_335x298.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!8wfB!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F211a6f5d-9a81-41b9-9643-e31c141d0827_335x298.png" width="335" height="298" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/211a6f5d-9a81-41b9-9643-e31c141d0827_335x298.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:298,&quot;width&quot;:335,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:31869,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!8wfB!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F211a6f5d-9a81-41b9-9643-e31c141d0827_335x298.png 424w, https://substackcdn.com/image/fetch/$s_!8wfB!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F211a6f5d-9a81-41b9-9643-e31c141d0827_335x298.png 848w, https://substackcdn.com/image/fetch/$s_!8wfB!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F211a6f5d-9a81-41b9-9643-e31c141d0827_335x298.png 1272w, https://substackcdn.com/image/fetch/$s_!8wfB!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F211a6f5d-9a81-41b9-9643-e31c141d0827_335x298.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Given all these variables are fixed size the EVM can use reserved storage locations (keys) starting from slot 0 (key of binary value 0) and moving linearly forward to slot 1, 2 etc. </p><p>It will do this based on the order the variables are declared in the contract. The first declared storage variable will be stored at slot 0.</p><p>In this example slot 0 will hold variable &#8220;value1&#8221;, variable &#8220;value2&#8221; is a fixed-sized array of 2 so will take up slots 1 &amp; 2 and finally, slot 3 will hold variable &#8220;value3&#8221;. The diagram below shows this.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!t2LY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F46352903-1395-49d0-a3a8-36b1bf5a3000_744x288.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!t2LY!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F46352903-1395-49d0-a3a8-36b1bf5a3000_744x288.png 424w, https://substackcdn.com/image/fetch/$s_!t2LY!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F46352903-1395-49d0-a3a8-36b1bf5a3000_744x288.png 848w, https://substackcdn.com/image/fetch/$s_!t2LY!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F46352903-1395-49d0-a3a8-36b1bf5a3000_744x288.png 1272w, https://substackcdn.com/image/fetch/$s_!t2LY!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F46352903-1395-49d0-a3a8-36b1bf5a3000_744x288.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!t2LY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F46352903-1395-49d0-a3a8-36b1bf5a3000_744x288.png" width="744" height="288" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/46352903-1395-49d0-a3a8-36b1bf5a3000_744x288.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:288,&quot;width&quot;:744,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:14190,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!t2LY!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F46352903-1395-49d0-a3a8-36b1bf5a3000_744x288.png 424w, https://substackcdn.com/image/fetch/$s_!t2LY!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F46352903-1395-49d0-a3a8-36b1bf5a3000_744x288.png 848w, https://substackcdn.com/image/fetch/$s_!t2LY!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F46352903-1395-49d0-a3a8-36b1bf5a3000_744x288.png 1272w, https://substackcdn.com/image/fetch/$s_!t2LY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F46352903-1395-49d0-a3a8-36b1bf5a3000_744x288.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Now let&#8217;s take a look at a similar contract  and inspect how the variables are stored in this scenario.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!snzA!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F48a5cb06-c390-4b92-8a20-39053f40d54c_335x316.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!snzA!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F48a5cb06-c390-4b92-8a20-39053f40d54c_335x316.png 424w, https://substackcdn.com/image/fetch/$s_!snzA!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F48a5cb06-c390-4b92-8a20-39053f40d54c_335x316.png 848w, https://substackcdn.com/image/fetch/$s_!snzA!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F48a5cb06-c390-4b92-8a20-39053f40d54c_335x316.png 1272w, https://substackcdn.com/image/fetch/$s_!snzA!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F48a5cb06-c390-4b92-8a20-39053f40d54c_335x316.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!snzA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F48a5cb06-c390-4b92-8a20-39053f40d54c_335x316.png" width="335" height="316" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/48a5cb06-c390-4b92-8a20-39053f40d54c_335x316.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:316,&quot;width&quot;:335,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:32269,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!snzA!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F48a5cb06-c390-4b92-8a20-39053f40d54c_335x316.png 424w, https://substackcdn.com/image/fetch/$s_!snzA!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F48a5cb06-c390-4b92-8a20-39053f40d54c_335x316.png 848w, https://substackcdn.com/image/fetch/$s_!snzA!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F48a5cb06-c390-4b92-8a20-39053f40d54c_335x316.png 1272w, https://substackcdn.com/image/fetch/$s_!snzA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F48a5cb06-c390-4b92-8a20-39053f40d54c_335x316.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Note the variable types are not uint256</figcaption></figure></div><p>You might expect this to take up slots 0 to 3 as in the previous example. We had 4 values to store in our previous example (taking into consideration the array of size 2) and we have 4 values to store in this example.</p><p>You may be surprised to hear that in this example only storage slot 0 is used. The key difference is the unit types that are used for the variables. </p><p>Before all variables were of type uint256 which represents 32 bytes of data. Here we use uint32, uint64 &amp; uint128 which represent 4, 8 and 16 bytes of data respectively.</p><h4>Slot Packing</h4><p>This is where the term slot packing arises. The solidity compiler knows it can store 32 bytes of data at a storage slot. As a result, when &#8220;uint32 value1&#8221;, which only takes up 4 bytes, is stored at slot 0 when the next variable is read in the compiler will see if it can be packed into the current storage slot. </p><p>Given slot 0 had 32 bytes of space and value1 only took up 4 of them as long as the next variable is less than 28 bytes in size it will also be packed into slot 0.</p><p>For the above example we start with 32 bytes at slot 0;</p><ul><li><p>value1 is stored at slot 0 which takes up 4 bytes</p></li><li><p>slot 0 has 28 bytes remaining</p></li><li><p>value2 is 4 bytes which is &lt;= 28 therefore it can be stored at slot 0</p></li><li><p>slot 0 has 24 bytes remaining </p></li><li><p>value3 is 8 bytes which is &lt;= 24 therefore it can be stored at slot 0</p></li><li><p>slot 0 has 16 bytes remaining</p></li><li><p>value4 is 16 bytes which is &lt;= 16 therefore it can be stored at slot 0 </p></li><li><p>slot 0 has 0 bytes remaining </p></li></ul><blockquote><p><em>Note that uint8 is the smallest solidity type, therefore packing cannot be smaller than 1 byte (8 bits)</em> </p></blockquote><p>The image below shows how the 32 bytes of data in slot 0 hold all 4 variables. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!JcZr!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdeb707da-60db-465d-a7c2-e45befefe3ff_744x434.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!JcZr!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdeb707da-60db-465d-a7c2-e45befefe3ff_744x434.png 424w, https://substackcdn.com/image/fetch/$s_!JcZr!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdeb707da-60db-465d-a7c2-e45befefe3ff_744x434.png 848w, https://substackcdn.com/image/fetch/$s_!JcZr!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdeb707da-60db-465d-a7c2-e45befefe3ff_744x434.png 1272w, https://substackcdn.com/image/fetch/$s_!JcZr!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdeb707da-60db-465d-a7c2-e45befefe3ff_744x434.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!JcZr!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdeb707da-60db-465d-a7c2-e45befefe3ff_744x434.png" width="744" height="434" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/deb707da-60db-465d-a7c2-e45befefe3ff_744x434.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:434,&quot;width&quot;:744,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:22249,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!JcZr!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdeb707da-60db-465d-a7c2-e45befefe3ff_744x434.png 424w, https://substackcdn.com/image/fetch/$s_!JcZr!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdeb707da-60db-465d-a7c2-e45befefe3ff_744x434.png 848w, https://substackcdn.com/image/fetch/$s_!JcZr!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdeb707da-60db-465d-a7c2-e45befefe3ff_744x434.png 1272w, https://substackcdn.com/image/fetch/$s_!JcZr!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdeb707da-60db-465d-a7c2-e45befefe3ff_744x434.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h4>EVM Storage Opcodes</h4><p>Now we understand the data structure of storage and the concept of slot packing let&#8217;s take a quick look at the 2 storage opcodes SSTORE &amp; SLOAD.</p><h5>SSTORE</h5><p>We&#8217;ll start with SSTORE it takes in a 32-byte key and a 32-byte value from the call stack and stores that 32-byte value at that 32-byte key location. Check out <a href="https://www.evm.codes/playground?unit=Wei&amp;codeType=Mnemonic&amp;code=%27z1uFFv1%200w~z2uy8965w%27~%5Cnz%2F%2F%20Example%20yv2%20w~SSTORE~v~PUSHuy0xFF%01uvwyz~_">this</a> EVM playground to see how it works.</p><h5>SLOAD</h5><p>Next, we have SLOAD which takes in a 32-byte key from the call stack and pushes the 32-byte value stored at that 32-byte key location onto the call stack. Check out <a href="https://www.evm.codes/playground?unit=Wei&amp;codeType=Mnemonic&amp;code=%27wSet%20up%20thrstatez46z0~SSTOREy1z0vy2z1v~%27~%5Cnz~PUSH1%20y~~wExamplrw%2F%2F%20v~SLOADre%20%01rvwyz~_">this</a> EVM playground to see how it works.</p><p>The question you should be asking yourself at this stage is if SSTORE and SLOAD only deal in 32-byte values how can you extract a variable that has been packed into a 32-byte slot. </p><p>If you take our example above when we run SLOAD on slot 0 we&#8217;re going to get the full 32-byte value stored at that location. </p><p>This value will include the data for value1, value2, value3 &amp; value4. How does the EVM extract the specific bytes in that 32-byte slot to return the value that we need?</p><p>The same goes for when we run SSTORE if we&#8217;re storing 32 bytes every time how does the EVM ensure when we store value2 it doesn&#8217;t overwrite value1. When we store value3 it doesn&#8217;t overwrite value2 etc.</p><p>These are the questions we&#8217;ll look to answer next.  </p><h2>Storing &amp; Retrieving Packed Variables </h2><p>Below is a simple contract that imitates the example we looked at above. The only addition is a store function that sets the variable values and has to read one variable to perform some arithmetic.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!GSgD!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F53bfa82f-b889-40c8-a407-3157f2e31385_457x452.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!GSgD!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F53bfa82f-b889-40c8-a407-3157f2e31385_457x452.png 424w, https://substackcdn.com/image/fetch/$s_!GSgD!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F53bfa82f-b889-40c8-a407-3157f2e31385_457x452.png 848w, https://substackcdn.com/image/fetch/$s_!GSgD!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F53bfa82f-b889-40c8-a407-3157f2e31385_457x452.png 1272w, https://substackcdn.com/image/fetch/$s_!GSgD!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F53bfa82f-b889-40c8-a407-3157f2e31385_457x452.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!GSgD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F53bfa82f-b889-40c8-a407-3157f2e31385_457x452.png" width="457" height="452" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/53bfa82f-b889-40c8-a407-3157f2e31385_457x452.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:452,&quot;width&quot;:457,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:53324,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!GSgD!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F53bfa82f-b889-40c8-a407-3157f2e31385_457x452.png 424w, https://substackcdn.com/image/fetch/$s_!GSgD!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F53bfa82f-b889-40c8-a407-3157f2e31385_457x452.png 848w, https://substackcdn.com/image/fetch/$s_!GSgD!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F53bfa82f-b889-40c8-a407-3157f2e31385_457x452.png 1272w, https://substackcdn.com/image/fetch/$s_!GSgD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F53bfa82f-b889-40c8-a407-3157f2e31385_457x452.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The store() function in the above solidity will perform the exact operations we had questions about. </p><p>Storing multiple variables in a single slot without overwriting existing data and retrieving a variable&#8217;s specific bytes from a 32-byte slot.</p><p>Let&#8217;s start by looking at the end state of slot 0 and work backward from there. Below is both the binary and hexadecimal representation of slot 0.</p><p>Remember that hexadecimal numbers are ultimately seen as binary numbers by the machine. This is important as a number of bitwise operations are used in slot packing.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!kvNp!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3ce409fd-b942-42d7-b7ea-02c18c4a8993_782x338.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!kvNp!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3ce409fd-b942-42d7-b7ea-02c18c4a8993_782x338.png 424w, https://substackcdn.com/image/fetch/$s_!kvNp!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3ce409fd-b942-42d7-b7ea-02c18c4a8993_782x338.png 848w, https://substackcdn.com/image/fetch/$s_!kvNp!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3ce409fd-b942-42d7-b7ea-02c18c4a8993_782x338.png 1272w, https://substackcdn.com/image/fetch/$s_!kvNp!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3ce409fd-b942-42d7-b7ea-02c18c4a8993_782x338.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!kvNp!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3ce409fd-b942-42d7-b7ea-02c18c4a8993_782x338.png" width="782" height="338" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/3ce409fd-b942-42d7-b7ea-02c18c4a8993_782x338.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:338,&quot;width&quot;:782,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!kvNp!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3ce409fd-b942-42d7-b7ea-02c18c4a8993_782x338.png 424w, https://substackcdn.com/image/fetch/$s_!kvNp!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3ce409fd-b942-42d7-b7ea-02c18c4a8993_782x338.png 848w, https://substackcdn.com/image/fetch/$s_!kvNp!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3ce409fd-b942-42d7-b7ea-02c18c4a8993_782x338.png 1272w, https://substackcdn.com/image/fetch/$s_!kvNp!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3ce409fd-b942-42d7-b7ea-02c18c4a8993_782x338.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Note the values you can see in the hexadecimal, 0x115c which is equal to 4444 in decimal, 0x14d = 333, 0x16 = 22 &amp; 0x01 = 1. These correspond to what we see in our solidity code. One slot holds 32 bytes of data which is 64 hexadecimal characters or 256 bits.</p><h4>Bitwise Operations</h4><p>Slot packing makes use of 3 bitwise operations, AND, OR &amp; NOT. These correspond to 3  EVM opcodes with the same naming. Let&#8217;s take a quick look at each one.</p><h4>AND </h4><p>Below we have two 8 bit binary numbers. In an AND operation the 1st bit in the first number is compared with the 1st bit in the second number. </p><p>If both values are a 1 then the AND statement returns true and the first bit of our result will equal 1 otherwise the statement returns false and the bit will equal 0. </p><p>This continues ie the 2nd bit of our first number is compared with the 2nd bit of our second number etc.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!eNO6!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F50496d7f-3e74-404c-b71c-472cf2440c2c_466x302.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!eNO6!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F50496d7f-3e74-404c-b71c-472cf2440c2c_466x302.png 424w, https://substackcdn.com/image/fetch/$s_!eNO6!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F50496d7f-3e74-404c-b71c-472cf2440c2c_466x302.png 848w, https://substackcdn.com/image/fetch/$s_!eNO6!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F50496d7f-3e74-404c-b71c-472cf2440c2c_466x302.png 1272w, https://substackcdn.com/image/fetch/$s_!eNO6!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F50496d7f-3e74-404c-b71c-472cf2440c2c_466x302.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!eNO6!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F50496d7f-3e74-404c-b71c-472cf2440c2c_466x302.png" width="466" height="302" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/50496d7f-3e74-404c-b71c-472cf2440c2c_466x302.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:302,&quot;width&quot;:466,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:9411,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!eNO6!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F50496d7f-3e74-404c-b71c-472cf2440c2c_466x302.png 424w, https://substackcdn.com/image/fetch/$s_!eNO6!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F50496d7f-3e74-404c-b71c-472cf2440c2c_466x302.png 848w, https://substackcdn.com/image/fetch/$s_!eNO6!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F50496d7f-3e74-404c-b71c-472cf2440c2c_466x302.png 1272w, https://substackcdn.com/image/fetch/$s_!eNO6!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F50496d7f-3e74-404c-b71c-472cf2440c2c_466x302.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h4>OR</h4><p>In the OR operation, only one of the values needs to have a value of 1 for the statement to return true. Using the same inputs as above we get a completely different output.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!31TM!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F42a806e1-3da7-4a11-901d-47d40a85ba3f_466x302.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!31TM!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F42a806e1-3da7-4a11-901d-47d40a85ba3f_466x302.png 424w, https://substackcdn.com/image/fetch/$s_!31TM!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F42a806e1-3da7-4a11-901d-47d40a85ba3f_466x302.png 848w, https://substackcdn.com/image/fetch/$s_!31TM!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F42a806e1-3da7-4a11-901d-47d40a85ba3f_466x302.png 1272w, https://substackcdn.com/image/fetch/$s_!31TM!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F42a806e1-3da7-4a11-901d-47d40a85ba3f_466x302.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!31TM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F42a806e1-3da7-4a11-901d-47d40a85ba3f_466x302.png" width="466" height="302" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/42a806e1-3da7-4a11-901d-47d40a85ba3f_466x302.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:302,&quot;width&quot;:466,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:9207,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!31TM!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F42a806e1-3da7-4a11-901d-47d40a85ba3f_466x302.png 424w, https://substackcdn.com/image/fetch/$s_!31TM!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F42a806e1-3da7-4a11-901d-47d40a85ba3f_466x302.png 848w, https://substackcdn.com/image/fetch/$s_!31TM!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F42a806e1-3da7-4a11-901d-47d40a85ba3f_466x302.png 1272w, https://substackcdn.com/image/fetch/$s_!31TM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F42a806e1-3da7-4a11-901d-47d40a85ba3f_466x302.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h4>NOT</h4><p>NOT is slightly different in that it only takes in one value rather than performing a comparison on 2 . NOT performs logical negation on each bit. Bits that are 0 become 1, and those that are 1 become 0. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!4ta4!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fed511be0-14bd-4f0e-adf5-e464e8cd7b4d_466x276.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!4ta4!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fed511be0-14bd-4f0e-adf5-e464e8cd7b4d_466x276.png 424w, https://substackcdn.com/image/fetch/$s_!4ta4!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fed511be0-14bd-4f0e-adf5-e464e8cd7b4d_466x276.png 848w, https://substackcdn.com/image/fetch/$s_!4ta4!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fed511be0-14bd-4f0e-adf5-e464e8cd7b4d_466x276.png 1272w, https://substackcdn.com/image/fetch/$s_!4ta4!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fed511be0-14bd-4f0e-adf5-e464e8cd7b4d_466x276.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!4ta4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fed511be0-14bd-4f0e-adf5-e464e8cd7b4d_466x276.png" width="466" height="276" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/ed511be0-14bd-4f0e-adf5-e464e8cd7b4d_466x276.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:276,&quot;width&quot;:466,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:8397,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!4ta4!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fed511be0-14bd-4f0e-adf5-e464e8cd7b4d_466x276.png 424w, https://substackcdn.com/image/fetch/$s_!4ta4!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fed511be0-14bd-4f0e-adf5-e464e8cd7b4d_466x276.png 848w, https://substackcdn.com/image/fetch/$s_!4ta4!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fed511be0-14bd-4f0e-adf5-e464e8cd7b4d_466x276.png 1272w, https://substackcdn.com/image/fetch/$s_!4ta4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fed511be0-14bd-4f0e-adf5-e464e8cd7b4d_466x276.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Let&#8217;s now jump into how these are used in the above solidity example</p><h4>Slot Manipulation - Slot Packing SSTORE </h4><p>We&#8217;re going to focus on solidity line 18. </p><pre><code>value2 = 22;</code></pre><p>At this stage some data, value1, has been stored in slot 0, we now need to pack some additional data into the same slot.</p><p>All the logic we see for this example is the same as when value3 &amp; value4 are stored. We&#8217;ll look at how this is done conceptually and an EVM playground will be provided for you to explore further.</p><p>We start with the following values. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!joyU!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F4fdcbc53-b6a6-4d10-892e-ff2d3b159f53_778x244.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!joyU!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F4fdcbc53-b6a6-4d10-892e-ff2d3b159f53_778x244.png 424w, https://substackcdn.com/image/fetch/$s_!joyU!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F4fdcbc53-b6a6-4d10-892e-ff2d3b159f53_778x244.png 848w, https://substackcdn.com/image/fetch/$s_!joyU!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F4fdcbc53-b6a6-4d10-892e-ff2d3b159f53_778x244.png 1272w, https://substackcdn.com/image/fetch/$s_!joyU!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F4fdcbc53-b6a6-4d10-892e-ff2d3b159f53_778x244.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!joyU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F4fdcbc53-b6a6-4d10-892e-ff2d3b159f53_778x244.png" width="778" height="244" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/4fdcbc53-b6a6-4d10-892e-ff2d3b159f53_778x244.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:244,&quot;width&quot;:778,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:31267,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!joyU!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F4fdcbc53-b6a6-4d10-892e-ff2d3b159f53_778x244.png 424w, https://substackcdn.com/image/fetch/$s_!joyU!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F4fdcbc53-b6a6-4d10-892e-ff2d3b159f53_778x244.png 848w, https://substackcdn.com/image/fetch/$s_!joyU!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F4fdcbc53-b6a6-4d10-892e-ff2d3b159f53_778x244.png 1272w, https://substackcdn.com/image/fetch/$s_!joyU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F4fdcbc53-b6a6-4d10-892e-ff2d3b159f53_778x244.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Note &#8220;0xffffffff&#8221; is equal to &#8220;11111111111111111111111111111111&#8221; in binary.</p><p>The first thing the EVM does is use the EXP opcode which takes in a base integer and an exponent and returns the value. </p><p>Here we use 0x0100 as the base integer which represents a 1-byte offset and raise it to exponent 0x04 which is the start position for &#8220;value2&#8221;. The image below shows why the returned value is useful.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!CCBT!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff33571de-7414-4e29-9681-d2f9f5693550_1058x284.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!CCBT!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff33571de-7414-4e29-9681-d2f9f5693550_1058x284.png 424w, https://substackcdn.com/image/fetch/$s_!CCBT!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff33571de-7414-4e29-9681-d2f9f5693550_1058x284.png 848w, https://substackcdn.com/image/fetch/$s_!CCBT!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff33571de-7414-4e29-9681-d2f9f5693550_1058x284.png 1272w, https://substackcdn.com/image/fetch/$s_!CCBT!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff33571de-7414-4e29-9681-d2f9f5693550_1058x284.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!CCBT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff33571de-7414-4e29-9681-d2f9f5693550_1058x284.png" width="1058" height="284" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/f33571de-7414-4e29-9681-d2f9f5693550_1058x284.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:284,&quot;width&quot;:1058,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:30131,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!CCBT!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff33571de-7414-4e29-9681-d2f9f5693550_1058x284.png 424w, https://substackcdn.com/image/fetch/$s_!CCBT!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff33571de-7414-4e29-9681-d2f9f5693550_1058x284.png 848w, https://substackcdn.com/image/fetch/$s_!CCBT!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff33571de-7414-4e29-9681-d2f9f5693550_1058x284.png 1272w, https://substackcdn.com/image/fetch/$s_!CCBT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff33571de-7414-4e29-9681-d2f9f5693550_1058x284.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>We can see that the result of the EXP function enables us to insert our data 0x16 at the correct position (4-byte offset). </p><p>We can&#8217;t use this value however as it would overwrite value1 which has already been stored. This is where bitmasks are utilised.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!bfgu!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F71f004c0-6d1d-40b9-a83c-3355832e3138_1058x706.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!bfgu!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F71f004c0-6d1d-40b9-a83c-3355832e3138_1058x706.png 424w, https://substackcdn.com/image/fetch/$s_!bfgu!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F71f004c0-6d1d-40b9-a83c-3355832e3138_1058x706.png 848w, https://substackcdn.com/image/fetch/$s_!bfgu!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F71f004c0-6d1d-40b9-a83c-3355832e3138_1058x706.png 1272w, https://substackcdn.com/image/fetch/$s_!bfgu!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F71f004c0-6d1d-40b9-a83c-3355832e3138_1058x706.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!bfgu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F71f004c0-6d1d-40b9-a83c-3355832e3138_1058x706.png" width="1058" height="706" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/71f004c0-6d1d-40b9-a83c-3355832e3138_1058x706.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:706,&quot;width&quot;:1058,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:82598,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!bfgu!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F71f004c0-6d1d-40b9-a83c-3355832e3138_1058x706.png 424w, https://substackcdn.com/image/fetch/$s_!bfgu!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F71f004c0-6d1d-40b9-a83c-3355832e3138_1058x706.png 848w, https://substackcdn.com/image/fetch/$s_!bfgu!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F71f004c0-6d1d-40b9-a83c-3355832e3138_1058x706.png 1272w, https://substackcdn.com/image/fetch/$s_!bfgu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F71f004c0-6d1d-40b9-a83c-3355832e3138_1058x706.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The above image shows how a bitmask can be utilised to get all data from a slot except for the bytes you are looking to overwrite. In this case, value2&#8217;s bytes were already set to 0 however if they had not been we would have seen this data wiped.</p><p>Here&#8217;s another example to crystalise what is happening. This is the same process but looking at what would happen if all 4 values had already been stored and we wanted to update value2 from 22 to 99. Look out for the existing 0x016 value being zeroed out.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!IEQr!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5c1522f7-9467-4116-9c3a-a076d0bc0fcd_1058x414.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!IEQr!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5c1522f7-9467-4116-9c3a-a076d0bc0fcd_1058x414.png 424w, https://substackcdn.com/image/fetch/$s_!IEQr!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5c1522f7-9467-4116-9c3a-a076d0bc0fcd_1058x414.png 848w, https://substackcdn.com/image/fetch/$s_!IEQr!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5c1522f7-9467-4116-9c3a-a076d0bc0fcd_1058x414.png 1272w, https://substackcdn.com/image/fetch/$s_!IEQr!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5c1522f7-9467-4116-9c3a-a076d0bc0fcd_1058x414.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!IEQr!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5c1522f7-9467-4116-9c3a-a076d0bc0fcd_1058x414.png" width="1058" height="414" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/5c1522f7-9467-4116-9c3a-a076d0bc0fcd_1058x414.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:414,&quot;width&quot;:1058,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:46868,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!IEQr!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5c1522f7-9467-4116-9c3a-a076d0bc0fcd_1058x414.png 424w, https://substackcdn.com/image/fetch/$s_!IEQr!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5c1522f7-9467-4116-9c3a-a076d0bc0fcd_1058x414.png 848w, https://substackcdn.com/image/fetch/$s_!IEQr!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5c1522f7-9467-4116-9c3a-a076d0bc0fcd_1058x414.png 1272w, https://substackcdn.com/image/fetch/$s_!IEQr!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5c1522f7-9467-4116-9c3a-a076d0bc0fcd_1058x414.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>You may already be thinking about how a bitwise OR could help us combine the values we have. The image below shows the next steps.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!M7EB!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9ff9609-0488-4684-848b-5d4245b0d966_1058x880.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!M7EB!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9ff9609-0488-4684-848b-5d4245b0d966_1058x880.png 424w, https://substackcdn.com/image/fetch/$s_!M7EB!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9ff9609-0488-4684-848b-5d4245b0d966_1058x880.png 848w, https://substackcdn.com/image/fetch/$s_!M7EB!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9ff9609-0488-4684-848b-5d4245b0d966_1058x880.png 1272w, https://substackcdn.com/image/fetch/$s_!M7EB!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9ff9609-0488-4684-848b-5d4245b0d966_1058x880.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!M7EB!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9ff9609-0488-4684-848b-5d4245b0d966_1058x880.png" width="1058" height="880" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/b9ff9609-0488-4684-848b-5d4245b0d966_1058x880.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:880,&quot;width&quot;:1058,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!M7EB!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9ff9609-0488-4684-848b-5d4245b0d966_1058x880.png 424w, https://substackcdn.com/image/fetch/$s_!M7EB!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9ff9609-0488-4684-848b-5d4245b0d966_1058x880.png 848w, https://substackcdn.com/image/fetch/$s_!M7EB!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9ff9609-0488-4684-848b-5d4245b0d966_1058x880.png 1272w, https://substackcdn.com/image/fetch/$s_!M7EB!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9ff9609-0488-4684-848b-5d4245b0d966_1058x880.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>We can now use SSTORE on this 32-byte value at slot 0 which contains the data for both value1 &amp; value2 at the correct byte positions.</p><h4>Slot Manipulation - Retrieving a Packed Variable SLOAD</h4><p>For retrieval, we will focus on solidity line 22.</p><pre><code>uint96 value5 = value3 + uint32(666)</code></pre><p>We&#8217;re not interested in the arithmetic, we&#8217;re interested in value3 being retrieved to perform the calculation.</p><p>We have a slightly different set of starting values.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!l3B7!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc8dffd1d-bcd2-461d-90c7-97287d7f0d2e_1148x204.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!l3B7!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc8dffd1d-bcd2-461d-90c7-97287d7f0d2e_1148x204.png 424w, https://substackcdn.com/image/fetch/$s_!l3B7!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc8dffd1d-bcd2-461d-90c7-97287d7f0d2e_1148x204.png 848w, https://substackcdn.com/image/fetch/$s_!l3B7!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc8dffd1d-bcd2-461d-90c7-97287d7f0d2e_1148x204.png 1272w, https://substackcdn.com/image/fetch/$s_!l3B7!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc8dffd1d-bcd2-461d-90c7-97287d7f0d2e_1148x204.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!l3B7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc8dffd1d-bcd2-461d-90c7-97287d7f0d2e_1148x204.png" width="1148" height="204" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/c8dffd1d-bcd2-461d-90c7-97287d7f0d2e_1148x204.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:204,&quot;width&quot;:1148,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:35928,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!l3B7!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc8dffd1d-bcd2-461d-90c7-97287d7f0d2e_1148x204.png 424w, https://substackcdn.com/image/fetch/$s_!l3B7!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc8dffd1d-bcd2-461d-90c7-97287d7f0d2e_1148x204.png 848w, https://substackcdn.com/image/fetch/$s_!l3B7!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc8dffd1d-bcd2-461d-90c7-97287d7f0d2e_1148x204.png 1272w, https://substackcdn.com/image/fetch/$s_!l3B7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc8dffd1d-bcd2-461d-90c7-97287d7f0d2e_1148x204.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>Much of what we have seen already will be reused for retrieval with some modifications.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!XoJM!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff55711ab-e05f-46a3-9291-c472409a7276_1058x672.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!XoJM!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff55711ab-e05f-46a3-9291-c472409a7276_1058x672.png 424w, https://substackcdn.com/image/fetch/$s_!XoJM!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff55711ab-e05f-46a3-9291-c472409a7276_1058x672.png 848w, https://substackcdn.com/image/fetch/$s_!XoJM!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff55711ab-e05f-46a3-9291-c472409a7276_1058x672.png 1272w, https://substackcdn.com/image/fetch/$s_!XoJM!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff55711ab-e05f-46a3-9291-c472409a7276_1058x672.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!XoJM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff55711ab-e05f-46a3-9291-c472409a7276_1058x672.png" width="1058" height="672" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/f55711ab-e05f-46a3-9291-c472409a7276_1058x672.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:672,&quot;width&quot;:1058,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!XoJM!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff55711ab-e05f-46a3-9291-c472409a7276_1058x672.png 424w, https://substackcdn.com/image/fetch/$s_!XoJM!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff55711ab-e05f-46a3-9291-c472409a7276_1058x672.png 848w, https://substackcdn.com/image/fetch/$s_!XoJM!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff55711ab-e05f-46a3-9291-c472409a7276_1058x672.png 1272w, https://substackcdn.com/image/fetch/$s_!XoJM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff55711ab-e05f-46a3-9291-c472409a7276_1058x672.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>We have retrieved value3 from our packed slot 0. Hexadecimal 0x14d  is equal to 333 which is what we set in the solidity code above. </p><p>Again bitmasks and bitwise operations are used to help extract specific bytes from the 32-byte slot. This value is now on the stack and can then be used by the EVM to calculate &#8220;value3 + uint32(666)&#8221;.</p><h2>EVM Playground</h2><p>I&#8217;ve taken all the opcodes executed in the store() function we just explored and put them into an <a href="https://www.evm.codes/playground?unit=Wei&amp;codeType=Mnemonic&amp;code='qjjjj.17%20-F1)%201%3C~J1_01J1_00~DUP1%224_%244_*jjjj-.18%20-F2)%2022%3C-~~J1_16Gvalue2)%2022%20decimal)_16%20i(hex%C2%81storagQlocatio(forF2%5C'~J1_04G4%602%5C'~J2_0100G0x100%20i(hex)%20256%20i(decimal%2C%202%23_04)_1%3F%5D%5D%20~%5D%20~DUP2Gduplicate_00C%3A%40DUP2GduplicatQ%7F_04)_1%3F%5B%5D%5D~~MULGmultiplyCo%20geB%25for%3E~~NOTGNOT%20operationCo%20geB%25for%20allYexcept%3E~~ANDGAND%20of%20%25and%20sloB0!Qto%20zero%20ouBvalues%20in%3E%20and%20retai(all%20other!es~~%7C1%3F%C2%80DUP4GduplicatQvalue2!Q%3D%2022)_16%5B%20~~ANDGANDCo%20ensurQthQvaluQis%20no%20morQtha(4Yi(length~~MULGreturns!e2%20atChQcorrecBpositio(-%204Yin~~ORGOR%20with%20previous!QandChQvaluQAND%20yielded%20o(linQ38%20gives%20usChQ32YthaBneedCo%20bQstored~~SWAP1GsloB0%C2%80SSTOREGstorQthQ32%20bytQvaluQaBsloB0~~POPGpop_16%20offChQstack~~qjjjj--.19%20-F3)%20333%3C--~J2_014dJ1_00J1_08%228_K%248_K*jjjj---.20%20-F4)%204444%3C---~J2_115cJ1_00J1_10%2216_KKK%2416_KKK*jjjjjjj--.22%20-%20%5C'uint64!e5)!e3%20%2B%20%5E%3Cjjj--~~J1_00~J2_029aG%5E%5B~~ANDGensurQ%5E%20does%20noBexceed%208%20bytes%2CCrim%20if%20iBdoes%20%C2%81locatio(of!e3~J1_08G8%603%5C'~~%7C00C%3A%20for%20SLOAD%20of%20sloB0%40%7C08C%3A%20for%20EXP~J2_0100G2%23_08)_1%3F%3F~~SWAP1GgeBsloB0!Qt%3A~~DIVGDIV%20of%20sloB0!Qwith_1%3F%3F%20removQbottom%208Y%20~J8_KKG%258Ylength%20~~ANDGZero%20outYoutsidQofChQ8%20bytQmaskCo%20retur(variablQ%5C'value3%5C'~~qTo%20seQthQresBofChQopcodes%20forChis%20calculatio(recreatQthQcontracBi(remix%20and%20enter%20debugging%20mode'~%5Cnq%2F%2F%20j--------_%200xY%20bytes%20Qe%20KffffffffJ~PUSHG%20qF%20%5C'valueC%20tBt%20.~qSolidity%20LinQ*K~AND~MUL~OR~SWAP1~SSTORE~POP~~q)%20%3D(n%20!%20valu%22J2_0100~EXP~DUP2~SLOAD~DUP2J%2356%20bits%20i(1%20bytQ~~EXPG%7F%24K~MUL~NOT~AND~SWAP1~DUP4J%25bitmask%20%3AoCop%20of%20stack%3C%3B%5C'~qjjjj%3EChQ8YassignedCoF2%5C'%3F00000000%40~~SLOADGload%20data%20aBsloB0~~%5B~J4_KG%254Ylength%5D%20%20%20%5Euint32%7B666%7D%60Yi(-%20starBpositio(forF%7CSWAP1Gbring_%7FexponenBof_0100%20%26%C2%80CoCop%20ofChQstack~~%C2%81~J1_00GsloB0%20-%20%01%C2%81%C2%80%7F%7C%60%5E%5D%5B%40%3F%3E%3C%3A%25%24%23%22!()*.BCFGJKQY_jq~_">EVM playground</a>. Here you&#8217;ll be able to interactively play with the opcodes that are used and see how the call stack and contract storage change as you jump through them.<br><br>I&#8217;ve left comments next to the opcodes on the 2 sections we explored (solidity lines 18 &amp; 22). I highly encourage you to check it out and jump through the opcodes it will greatly enhance your understanding.<br><br>Check it out <a href="https://www.evm.codes/playground?unit=Wei&amp;codeType=Mnemonic&amp;code='qjjjj.17%20-F1)%201%3C~J1_01J1_00~DUP1%224_%244_*jjjj-.18%20-F2)%2022%3C-~~J1_16Gvalue2)%2022%20decimal)_16%20i(hex%C2%81storagQlocatio(forF2%5C'~J1_04G4%602%5C'~J2_0100G0x100%20i(hex)%20256%20i(decimal%2C%202%23_04)_1%3F%5D%5D%20~%5D%20~DUP2Gduplicate_00C%3A%40DUP2GduplicatQ%7F_04)_1%3F%5B%5D%5D~~MULGmultiplyCo%20geB%25for%3E~~NOTGNOT%20operationCo%20geB%25for%20allYexcept%3E~~ANDGAND%20of%20%25and%20sloB0!Qto%20zero%20ouBvalues%20in%3E%20and%20retai(all%20other!es~~%7C1%3F%C2%80DUP4GduplicatQvalue2!Q%3D%2022)_16%5B%20~~ANDGANDCo%20ensurQthQvaluQis%20no%20morQtha(4Yi(length~~MULGreturns!e2%20atChQcorrecBpositio(-%204Yin~~ORGOR%20with%20previous!QandChQvaluQAND%20yielded%20o(linQ38%20gives%20usChQ32YthaBneedCo%20bQstored~~SWAP1GsloB0%C2%80SSTOREGstorQthQ32%20bytQvaluQaBsloB0~~POPGpop_16%20offChQstack~~qjjjj--.19%20-F3)%20333%3C--~J2_014dJ1_00J1_08%228_K%248_K*jjjj---.20%20-F4)%204444%3C---~J2_115cJ1_00J1_10%2216_KKK%2416_KKK*jjjjjjj--.22%20-%20%5C'uint64!e5)!e3%20%2B%20%5E%3Cjjj--~~J1_00~J2_029aG%5E%5B~~ANDGensurQ%5E%20does%20noBexceed%208%20bytes%2CCrim%20if%20iBdoes%20%C2%81locatio(of!e3~J1_08G8%603%5C'~~%7C00C%3A%20for%20SLOAD%20of%20sloB0%40%7C08C%3A%20for%20EXP~J2_0100G2%23_08)_1%3F%3F~~SWAP1GgeBsloB0!Qt%3A~~DIVGDIV%20of%20sloB0!Qwith_1%3F%3F%20removQbottom%208Y%20~J8_KKG%258Ylength%20~~ANDGZero%20outYoutsidQofChQ8%20bytQmaskCo%20retur(variablQ%5C'value3%5C'~~qTo%20seQthQresBofChQopcodes%20forChis%20calculatio(recreatQthQcontracBi(remix%20and%20enter%20debugging%20mode'~%5Cnq%2F%2F%20j--------_%200xY%20bytes%20Qe%20KffffffffJ~PUSHG%20qF%20%5C'valueC%20tBt%20.~qSolidity%20LinQ*K~AND~MUL~OR~SWAP1~SSTORE~POP~~q)%20%3D(n%20!%20valu%22J2_0100~EXP~DUP2~SLOAD~DUP2J%2356%20bits%20i(1%20bytQ~~EXPG%7F%24K~MUL~NOT~AND~SWAP1~DUP4J%25bitmask%20%3AoCop%20of%20stack%3C%3B%5C'~qjjjj%3EChQ8YassignedCoF2%5C'%3F00000000%40~~SLOADGload%20data%20aBsloB0~~%5B~J4_KG%254Ylength%5D%20%20%20%5Euint32%7B666%7D%60Yi(-%20starBpositio(forF%7CSWAP1Gbring_%7FexponenBof_0100%20%26%C2%80CoCop%20ofChQstack~~%C2%81~J1_00GsloB0%20-%20%01%C2%81%C2%80%7F%7C%60%5E%5D%5B%40%3F%3E%3C%3A%25%24%23%22!()*.BCFGJKQY_jq~_">here</a>.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!IGNn!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F2f0f0e5d-fd10-41d6-ad63-aa2b719f98d5_1497x847.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!IGNn!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F2f0f0e5d-fd10-41d6-ad63-aa2b719f98d5_1497x847.png 424w, https://substackcdn.com/image/fetch/$s_!IGNn!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F2f0f0e5d-fd10-41d6-ad63-aa2b719f98d5_1497x847.png 848w, https://substackcdn.com/image/fetch/$s_!IGNn!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F2f0f0e5d-fd10-41d6-ad63-aa2b719f98d5_1497x847.png 1272w, https://substackcdn.com/image/fetch/$s_!IGNn!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F2f0f0e5d-fd10-41d6-ad63-aa2b719f98d5_1497x847.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!IGNn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F2f0f0e5d-fd10-41d6-ad63-aa2b719f98d5_1497x847.png" width="1456" height="824" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/2f0f0e5d-fd10-41d6-ad63-aa2b719f98d5_1497x847.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:824,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:153779,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!IGNn!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F2f0f0e5d-fd10-41d6-ad63-aa2b719f98d5_1497x847.png 424w, https://substackcdn.com/image/fetch/$s_!IGNn!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F2f0f0e5d-fd10-41d6-ad63-aa2b719f98d5_1497x847.png 848w, https://substackcdn.com/image/fetch/$s_!IGNn!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F2f0f0e5d-fd10-41d6-ad63-aa2b719f98d5_1497x847.png 1272w, https://substackcdn.com/image/fetch/$s_!IGNn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F2f0f0e5d-fd10-41d6-ad63-aa2b719f98d5_1497x847.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>You should now have an in-depth understanding of how storage slot packing works and how the EVM is able to retrieve and store bytes in specific locations in a 32-byte slot. </p><p>Although the EVM opcodes SLOAD &amp; SSTORE only deal in 32-byte chunks we can use bitwise operations and bitmasks to store and load the data we want.</p><p>In <a href="https://noxx.substack.com/p/evm-deep-dives-the-path-to-shadowy-5a5?s=r">Part 4</a> of the series, we take a look under the hood of the Go Ethereum (Geth) client to see how it implements the SSTORE &amp; SLOAD opcodes.</p><p>Hope you enjoyed the article.</p><p>noxx</p><p>Twitter <a href="https://twitter.com/noxx3xxon">@noxx3xxon</a></p>]]></content:encoded></item><item><title><![CDATA[EVM Deep Dives: The Path to Shadowy Super Coder 🥷 💻 - Part 2]]></title><description><![CDATA[Let's take a trip down memory lane]]></description><link>https://noxx.substack.com/p/evm-deep-dives-the-path-to-shadowy-d6b</link><guid isPermaLink="false">https://noxx.substack.com/p/evm-deep-dives-the-path-to-shadowy-d6b</guid><dc:creator><![CDATA[noxx]]></dc:creator><pubDate>Sat, 05 Mar 2022 21:25:33 GMT</pubDate><enclosure url="https://cdn.substack.com/image/fetch/h_600,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff45435da-8528-4e99-8a43-bab75decbf06_1066x630.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>This is the second installment in a series of articles that will deep dive into the EVM and build the foundational knowledge needed to become a &#8220;shadowy super coder&#8221;. This article will build on the knowledge gained from <a href="https://noxx.substack.com/p/evm-deep-dives-the-path-to-shadowy?utm_source=url&amp;s=r">Part 1</a> so if you haven&#8217;t read it yet I encourage you to do so.</p><p>In <a href="https://noxx.substack.com/p/evm-deep-dives-the-path-to-shadowy?utm_source=url&amp;s=r">Part 1</a> we explored how the EVM knows which bytecode to run depending on which contract function is called. This helped us build an understanding of the call stack, calldata, function signatures &amp; the EVM opcodes instructions.</p><p>In Part 2 we&#8217;ll take a trip down &#8220;memory&#8221; lane and provide a comprehensive review of what contract memory is and how it works under the EVM hood.  </p><h2>A Trip Down Memory Lane</h2><p>As you will recall in <a href="https://noxx.substack.com/p/evm-deep-dives-the-path-to-shadowy?utm_source=url&amp;s=r">Part 1</a> we took a look at the default 1_Storage.sol contract from <a href="https://remix.ethereum.org/">remix</a>.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!fFEU!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3400bba6-f870-4b68-8ba8-118562b08aef_489x538.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!fFEU!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3400bba6-f870-4b68-8ba8-118562b08aef_489x538.png 424w, https://substackcdn.com/image/fetch/$s_!fFEU!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3400bba6-f870-4b68-8ba8-118562b08aef_489x538.png 848w, https://substackcdn.com/image/fetch/$s_!fFEU!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3400bba6-f870-4b68-8ba8-118562b08aef_489x538.png 1272w, https://substackcdn.com/image/fetch/$s_!fFEU!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3400bba6-f870-4b68-8ba8-118562b08aef_489x538.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!fFEU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3400bba6-f870-4b68-8ba8-118562b08aef_489x538.png" width="489" height="538" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/3400bba6-f870-4b68-8ba8-118562b08aef_489x538.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:538,&quot;width&quot;:489,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!fFEU!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3400bba6-f870-4b68-8ba8-118562b08aef_489x538.png 424w, https://substackcdn.com/image/fetch/$s_!fFEU!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3400bba6-f870-4b68-8ba8-118562b08aef_489x538.png 848w, https://substackcdn.com/image/fetch/$s_!fFEU!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3400bba6-f870-4b68-8ba8-118562b08aef_489x538.png 1272w, https://substackcdn.com/image/fetch/$s_!fFEU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3400bba6-f870-4b68-8ba8-118562b08aef_489x538.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>We then generated the byte code and zoomed in on the part relating to function selection. In this article, we going to focus on the first 5 bytes of the contract runtime bytecode.</p><pre><code>6080604052</code></pre><pre><code>60 80                       =   PUSH1 0x80
60 40                       =   PUSH1 0x40
52                          =   MSTORE </code></pre><p>These 5 bytes represent the initialisation of the &#8220;free memory pointer&#8221;. To fully understand what that means and what these bytes do we must first build your understanding of the data structures that govern contract memory.</p><h3>Memory Data Structure</h3><p>Contract memory is a simple byte array, where data can be stored in 32 bytes (256 bit) or 1 byte (8 bit) chunks and read in 32 bytes (256 bit) chunks. The image below illustrates this structure along with the read/write functionality of contract memory. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!47Y7!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F33d7994b-a4b5-4268-8d53-85f214944599_717x437.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!47Y7!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F33d7994b-a4b5-4268-8d53-85f214944599_717x437.png 424w, https://substackcdn.com/image/fetch/$s_!47Y7!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F33d7994b-a4b5-4268-8d53-85f214944599_717x437.png 848w, https://substackcdn.com/image/fetch/$s_!47Y7!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F33d7994b-a4b5-4268-8d53-85f214944599_717x437.png 1272w, https://substackcdn.com/image/fetch/$s_!47Y7!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F33d7994b-a4b5-4268-8d53-85f214944599_717x437.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!47Y7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F33d7994b-a4b5-4268-8d53-85f214944599_717x437.png" width="717" height="437" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/33d7994b-a4b5-4268-8d53-85f214944599_717x437.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:437,&quot;width&quot;:717,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:26504,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!47Y7!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F33d7994b-a4b5-4268-8d53-85f214944599_717x437.png 424w, https://substackcdn.com/image/fetch/$s_!47Y7!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F33d7994b-a4b5-4268-8d53-85f214944599_717x437.png 848w, https://substackcdn.com/image/fetch/$s_!47Y7!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F33d7994b-a4b5-4268-8d53-85f214944599_717x437.png 1272w, https://substackcdn.com/image/fetch/$s_!47Y7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F33d7994b-a4b5-4268-8d53-85f214944599_717x437.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">source: https://takenobu-hs.github.io/downloads/ethereum_evm_illustrated.pdf</figcaption></figure></div><p>This functionality is determined by the 3 opcodes that operate on memory. </p><ul><li><p>MSTORE (x, y) - Store a 32 byte (256-bit) value &#8220;y&#8221; starting at memory location &#8220;x&#8221;</p></li><li><p>MLOAD (x) - Load 32 bytes (256-bit) starting at memory location &#8220;x&#8221; onto the call stack</p></li><li><p>MSTORE8 (x, y) - Store a 1 byte (8-bit) value &#8220;y&#8221; at memory location &#8220;x&#8221; (the least significant byte of the 32-byte stack value).</p></li></ul><p>You can think of the memory location as simply the array index of where to start writing/reading the data. If you want to write/read more than 1 byte of data you simply continue writing or reading from the next array index. </p><h3>EVM Playground</h3><p>This <a href="https://www.evm.codes/playground?unit=Wei&amp;codeType=Mnemonic&amp;code='Vg*(_I...1W0GJ_!!!!z00FK22WJQ0Y22z20F8K33W33Q1Y33z21F8d(v0Z0-Jq00Xd(vJZJ-64q20Xdv33Z33-65q21Xpp'~N%20locatioCzG1_wppVv7o7hBcall%20stack%20from~uIIIIq(%20ofNzp%5Cnj%20bytegSTOREdw)*_%200xZ9BY9Chex%7DzXpM)W%20at~V%2F%2F%20MQ%20%7B0x2N%20memoryKwg8%201j_J32I11GpPUSHFpMgCn%20Be%209%20i7%20t*%20J)LOAD(js!uu%01!()*79BCFGIJKNQVWXYZ_dgjpquvwz~_">EVM playground</a> will help solidify your understanding of what these 3 opcodes do and how memory locations work. Click Run and the curled arrow at the top right to jump through the opcodes and see how the stack and memory are altered. (There are comments above the opcodes to describe what each section does)</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Y1is!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3e29126-2954-40e3-bc1b-7ca5e780fd1c_1500x850.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Y1is!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3e29126-2954-40e3-bc1b-7ca5e780fd1c_1500x850.png 424w, https://substackcdn.com/image/fetch/$s_!Y1is!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3e29126-2954-40e3-bc1b-7ca5e780fd1c_1500x850.png 848w, https://substackcdn.com/image/fetch/$s_!Y1is!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3e29126-2954-40e3-bc1b-7ca5e780fd1c_1500x850.png 1272w, https://substackcdn.com/image/fetch/$s_!Y1is!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3e29126-2954-40e3-bc1b-7ca5e780fd1c_1500x850.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Y1is!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3e29126-2954-40e3-bc1b-7ca5e780fd1c_1500x850.png" width="1456" height="825" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/e3e29126-2954-40e3-bc1b-7ca5e780fd1c_1500x850.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:825,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!Y1is!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3e29126-2954-40e3-bc1b-7ca5e780fd1c_1500x850.png 424w, https://substackcdn.com/image/fetch/$s_!Y1is!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3e29126-2954-40e3-bc1b-7ca5e780fd1c_1500x850.png 848w, https://substackcdn.com/image/fetch/$s_!Y1is!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3e29126-2954-40e3-bc1b-7ca5e780fd1c_1500x850.png 1272w, https://substackcdn.com/image/fetch/$s_!Y1is!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3e29126-2954-40e3-bc1b-7ca5e780fd1c_1500x850.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>While walking through the EVM playground above you may have noticed a few strange occurrences. First, when we wrote a single byte 0x22 using MSTORE8 to memory location 32 (0x20) the memory changed from </p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!6Jly!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F954d54e6-0bce-4de3-a61d-dd41fdae49c7_836x152.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!6Jly!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F954d54e6-0bce-4de3-a61d-dd41fdae49c7_836x152.png 424w, https://substackcdn.com/image/fetch/$s_!6Jly!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F954d54e6-0bce-4de3-a61d-dd41fdae49c7_836x152.png 848w, https://substackcdn.com/image/fetch/$s_!6Jly!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F954d54e6-0bce-4de3-a61d-dd41fdae49c7_836x152.png 1272w, https://substackcdn.com/image/fetch/$s_!6Jly!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F954d54e6-0bce-4de3-a61d-dd41fdae49c7_836x152.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!6Jly!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F954d54e6-0bce-4de3-a61d-dd41fdae49c7_836x152.png" width="836" height="152" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/954d54e6-0bce-4de3-a61d-dd41fdae49c7_836x152.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:152,&quot;width&quot;:836,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:14440,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!6Jly!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F954d54e6-0bce-4de3-a61d-dd41fdae49c7_836x152.png 424w, https://substackcdn.com/image/fetch/$s_!6Jly!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F954d54e6-0bce-4de3-a61d-dd41fdae49c7_836x152.png 848w, https://substackcdn.com/image/fetch/$s_!6Jly!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F954d54e6-0bce-4de3-a61d-dd41fdae49c7_836x152.png 1272w, https://substackcdn.com/image/fetch/$s_!6Jly!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F954d54e6-0bce-4de3-a61d-dd41fdae49c7_836x152.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>to</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Yqcp!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F225fde5a-09bf-4a24-a702-88ad5010951e_836x176.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Yqcp!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F225fde5a-09bf-4a24-a702-88ad5010951e_836x176.png 424w, https://substackcdn.com/image/fetch/$s_!Yqcp!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F225fde5a-09bf-4a24-a702-88ad5010951e_836x176.png 848w, https://substackcdn.com/image/fetch/$s_!Yqcp!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F225fde5a-09bf-4a24-a702-88ad5010951e_836x176.png 1272w, https://substackcdn.com/image/fetch/$s_!Yqcp!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F225fde5a-09bf-4a24-a702-88ad5010951e_836x176.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Yqcp!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F225fde5a-09bf-4a24-a702-88ad5010951e_836x176.png" width="836" height="176" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/225fde5a-09bf-4a24-a702-88ad5010951e_836x176.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:176,&quot;width&quot;:836,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:17619,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Yqcp!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F225fde5a-09bf-4a24-a702-88ad5010951e_836x176.png 424w, https://substackcdn.com/image/fetch/$s_!Yqcp!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F225fde5a-09bf-4a24-a702-88ad5010951e_836x176.png 848w, https://substackcdn.com/image/fetch/$s_!Yqcp!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F225fde5a-09bf-4a24-a702-88ad5010951e_836x176.png 1272w, https://substackcdn.com/image/fetch/$s_!Yqcp!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F225fde5a-09bf-4a24-a702-88ad5010951e_836x176.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>You may ask the question, what&#8217;s with all the additional zeros we only added 1 byte?</p><h4>Memory Expansion</h4><p>When your contract writes to memory, you have to pay for the number of bytes written. If you are writing to an area of memory that hasn't been written to before, there is an additional memory expansion cost for using it for the first time.</p><p>Memory is expanded in 32 bytes (256-bit) increments when writing to previously untouched memory space.</p><blockquote><p>Memory expansion costs scale linearly for the first 724 bytes and quadratically after that.</p></blockquote><p>Above our memory was 32 bytes before we wrote 1 byte at location 32. At this point we began writing into untouched memory, as a result, the memory was expanded by another 32-byte increment to 64 bytes. </p><p>Note that all locations in memory are well-defined initially as zero which is why we see 2200000000000000000000000000000000000000000000000000000000000000 added to our memory.</p><h4>Remember Memory is a Byte Array </h4><p>The second thing you may have noticed occurred when we ran an MLOAD from memory location 33 (0x21). We returned the following value to the call stack.</p><pre><code>3300000000000000000000000000000000000000000000000000000000000000</code></pre><p>We were able to start our read from a non 32 factor. </p><p>Remember memory is a byte array meaning we can start our reads (and our writes) from any memory location. We are not constrained to multiples of 32. Memory is linear and can be addressed at the byte level.</p><blockquote><p>Memory can only be newly created in a function. It can either be newly instantiated complex types like array/struct (e.g. via <code>new int[...]</code>) or copied from a <code>storage</code> referenced variable.</p></blockquote><p>Now we have an understanding of the data structures let&#8217;s return to the free memory pointer.</p><h3>Free Memory Pointer </h3><p>The free memory pointer is simply a pointer to the location where free memory starts. It ensures smart contracts keep track of which memory locations have been written to and which haven&#8217;t. </p><p>This protects against a contract overwriting some memory that has been allocated to another variable.</p><p>When a variable is written to memory the contract will first reference the free memory pointer to determine where the data should be stored. </p><p>It then updates the free memory pointer by noting how much data is to be written to the new location. A simple addition of these 2 values will yield where the new free memory will start.</p><pre><code>freeMemoryPointer + dataSizeBytes = newFreeMemoryPointer </code></pre><p></p><h4>Bytecode</h4><p>As mentioned before the free memory pointer is defined at the start of the runtime bytecode through these 5 opcodes.</p><pre><code>60 80                       =   PUSH1 0x80
60 40                       =   PUSH1 0x40
52                          =   MSTORE  </code></pre><p>These effectively state that the free memory pointer is located in memory at byte 0x40 (64 in decimal) and has a value of 0x80 (128 in decimal).</p><p>The immediate questions you may have are why the values 0x40 &amp; 0x80 are used above. The answer to this can be found in the following statement.</p><blockquote><p>Solidity&#8217;s memory layout reserves four 32-byte slots:</p><ul><li><p><code>0x00</code> - <code>0x3f</code> (64 bytes): scratch space </p></li><li><p><code>0x40</code> - <code>0x5f</code> (32 bytes):  free memory pointer</p></li><li><p><code>0x60</code> - <code>0x7f</code> (32 bytes): zero slot</p></li></ul></blockquote><p>We can see that 0x40 is the predefined location by solidity for the free memory pointer. The value 0x80 is merely the first memory byte that is available to write to after the 4 reserved  32-byte slots.</p><p>We&#8217;ll quickly run through what each reserved section does.</p><ul><li><p>Scratch space, can be used between statements i.e. within inline assembly and for hashing methods. </p></li><li><p>Free memory pointer, currently allocated memory size, start location of free memory, 0x80 initially.</p></li><li><p>The zero slot, is used as an initial value for dynamic memory arrays and should never be written to.</p></li></ul><h3>Memory in a Real Contract</h3><p>To consolidate what we&#8217;ve learned so far we&#8217;re going to look at how memory and the free memory pointer update within real solidity code.</p><p>I&#8217;ve created a MemoryLane Contract and intentionally kept it extremely simple. It has a single function that merely defines two arrays of lengths 5 &amp; 2 and then assigns b[0] a value of 1. Despite the simplicity, there is a lot that goes on when these 3 lines of code are executed.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!yHSw!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F45614789-b470-43be-9bb5-f7e0aabf35b5_393x276.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!yHSw!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F45614789-b470-43be-9bb5-f7e0aabf35b5_393x276.png 424w, https://substackcdn.com/image/fetch/$s_!yHSw!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F45614789-b470-43be-9bb5-f7e0aabf35b5_393x276.png 848w, https://substackcdn.com/image/fetch/$s_!yHSw!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F45614789-b470-43be-9bb5-f7e0aabf35b5_393x276.png 1272w, https://substackcdn.com/image/fetch/$s_!yHSw!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F45614789-b470-43be-9bb5-f7e0aabf35b5_393x276.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!yHSw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F45614789-b470-43be-9bb5-f7e0aabf35b5_393x276.png" width="393" height="276" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/45614789-b470-43be-9bb5-f7e0aabf35b5_393x276.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:276,&quot;width&quot;:393,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:30631,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!yHSw!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F45614789-b470-43be-9bb5-f7e0aabf35b5_393x276.png 424w, https://substackcdn.com/image/fetch/$s_!yHSw!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F45614789-b470-43be-9bb5-f7e0aabf35b5_393x276.png 848w, https://substackcdn.com/image/fetch/$s_!yHSw!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F45614789-b470-43be-9bb5-f7e0aabf35b5_393x276.png 1272w, https://substackcdn.com/image/fetch/$s_!yHSw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F45614789-b470-43be-9bb5-f7e0aabf35b5_393x276.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>To view the details of  how this solidity code executes within the EVM it can be copied into a <a href="https://remix.ethereum.org/">remix</a> IDE. After it&#8217;s copied you can compile the code, deploy it, run the memoryLane() function and then enter debugging mode to step through the opcodes (See <a href="https://remix-ide.readthedocs.io/en/latest/tutorial_debug.html">here</a> for instructions on how to do this). I have extracted a simplified version into an <a href="https://www.evm.codes/playground?unit=Wei&amp;callData=0xd275ca72&amp;codeType=Mnemonic&amp;code='BBQFreXM%3APointer%20%C2%8B~_BB_QvaluXto%20storXfor%20NG80)128%3F%2480Qlocatio9for%20NG40)64%3F%C2%8EQjump!9%7Brequired%20to%20prevent%20stack%20underflow%7D%20_PUSH2Gffff___BBB~~~%C2%83a%7F%2F_BBB~~~_Qload%20N%2440_MLOAD_%23N_DUP1Q0xa0)160%3F%2C%2032%20*%205)160%20first%C2%8C5%24a0QN(0x8%C2%82a0%7D)new%20N_ADDQSavXthis%20new%20valuX0x120%20to%20thXfreXmemory!n%C2%8E_BB%2FQM%3A%C2%8BVaraiblX%E2%80%9Ca%E2%80%9D%20~%2F_BB%2F_%230x80_DUP1QpushG05)5%3F(array%20length%7D%2405QSwap%20thXtop%202-tems%20%C2%86andG80_SWAP1QpushG20)32%C2%88e%7D%2420QDuplicatXthX3rd%3B%C2%86to%20thX%C2%84_DUP3Q0x05%20*G20)5%20*%2032-9decmial)160%20o9%C2%84(sizXof%20array-9bytes%7D_MULQDuplicatX0xa0)160%3F_DUP1QReturns%20sizXof%20calldata-9bytes%20currently%20just%C2%90%3DG04%20or%204-9decmial%C2%89stack(0x80%7D_DUP4Q0x80(%7CXcopie%40Ga%C2%8A_Qthis%20offsets%20thX4%20bytes-9our%20call%20data%20with%20a%20sizXofGa0%20which%20yeild%20a%20160%20bit%20set%20of%200%22s%20to%20bXstored%20at%20thXN!n_Qthis%20effectively-nitialises%20our%20array-9m%3A%C2%87%60%C2%80%25eded_%230xa0_DUP1%230x80_DUP3Qnew%20N%20as%20before_ADDQswap%201st(0x120%7D%3Bo9%3E%20and%203rd(0x80%7D_SWAP2K80%7D_POPKa0%7D_POPQS%C2%81120%20%26G05_SWAP1K05%7D_POPK120%7D_POPQs%C2%8180%20%26Gb6(jump!n%7D_SWAP1%C2%8D%5E%C2%85%C2%85%C2%8D%2C%5E_BBB~~%2F%C2%83b%7F_BBB~~%2F_QN%20load-9%2440_MLOAD%23N(0x120%7D_DUP1Q0x40)64%3F%2C%2032%20*%202)64%20second%C2%8C2%2440QN(0x12%C2%8240%7D)new%20N_ADDQsavXnew%20N%20valuXat%20freXmemory!nG40%C2%8EBBQM%3A%C2%8BVariablX%E2%80%9Cb%E2%80%9D%20~_BB_%230x120(m%3Astart!9for%20variablX%5C'b%5C'%7D_DUP1Q0x02)2%3F)array%20length%2402Qs%C2%8102%20%26G120_SWAP1Q0x20)32%C2%88Xi9bytes%7D%2420%233rd%3Bo9%3EG02_DUP3Q0x02%20*G20%20%3DG40)64(amount%20of%20bytes-9m%3Ato-nitialise%7D_MUL%230x40(N!n%7D_DUP1QsamXas%20beforX4%20bytes%20for%C2%900x04%C2%89%3E%20%3DG120_DUP4Q0x120(%7CXcopie%40G4%C2%8A%C2%87%60%C2%80%25eded__~duplicatXthX%C2%84G40_DUP1%233rd%3Bo9%3EG120_DUP3Qadd%20together%20yields%20N%20value_ADDQswapG160%20%26G120_SWAP2K120%7D_POPK40%7D_POPQswapG160%20%26G02_SWAP1K02%7D_POPK160%7D_POPQjump!9to%20%C2%84Gbe_SWAP1QsimulatXjump%20pop%20jump!9off%20stack_POP__B~~~QAssig9ValuXto%20b%5B0%5D%20~_B~~~_QpushG01%2C%20valuXto%20add%20b%5B0%5D%2401QpushG00%2400Qleft%20shift%20operatio9no%20shift%2C%20first-nput-s%200%20_SHL%C2%91120%7D_DUP2QpushG00)%5B0%5D%20wherXi9thXarray%20should%20this%3Bgo%2400QpushG20)64%20bytes%20thXlength%20of%20thXarray%20%2402%C2%9100%7D_DUP2Q0x00%20%3CG20)%20truX%3DG01(check%20thXuser-s%20not%20trying%20to%20storXa%20valuXat%20a!9that%20doesn%22t%20exist-9thXarray%7D_LTQjump!n_PUSH2G00d7Q2%20POPs%20sincXthis-s%20a%20JUMPI(checking-f%20LT%20returned%20truXor%20false%7D%C2%8F%C2%8F_QpushG20(32%20bytes%20aray%3Bsize%7D%2420Q0x20%20*G00%20%3DG00)0%C2%88X*-ndex%20to%20determinXbytXoffset%7D_MULQ0x00%20%2BG120_ADD%232nd%20o9stackG01(valuXfor%20b%5B0%5D%7D_DUP2%232nd%20o9stackG120(memory!9for%20b%5B%5D%7D_DUP2QstorX0x01%20at%20memory!nG120_MSTOREQclea9up%20stack_POP_POP_POP_POP_'~%2F%2F_%5CnXe%20Q_~%20NfreXm%3ApointerKQpop%20top%3Boff%20stack(0xG%200xB~~~~~~~~~~9n%20-%20i)%20%3D%20(%20%7B!%20locatio%23QduplicatX%24_PUSH1G%25%5C'a%5C'%20and%20removes%20any-tems%20that%20arXno%20longer%20ne%3Aemory%20%3B-tem%20%3EthXstack%3F-9decimal%40d.%7D%2CG04(bytXoffset-9thXcalldata%20to%20copy.%7D%2C%5E%20removXthXtop%3Boff%20stack%20with%20POP_POP_%60ng%20lines-9this%20sectio9manipulatX%3E%20to%20%7CbytXoffset-9thXm%3AwherXthXresult%20will%20b%7F%E2%80%9D%20%26%20FreXM%3APointer%20UpdatX~%C2%80ensurXwXhavXthXmemory!9of%20variablX%C2%81wap%20top%202-temsG%C2%820%7D%20%2B%20spacXfor%20array(0x%C2%83QM%3AAllocatio9VaribalX%E2%80%9C%C2%84top%20of%20%3E%C2%85QSimulated%20jump!n_PUSH2Gffff%C2%86o9%3E-9this%20casX0x05%20%C2%87_CALLDATACOPY_QThXremaini%C2%88%3F(array%3Bsiz%C2%89_CALLDATASIZE%234th%3Bo9%C2%8A0(bytXsizXto%20copy.%7D%C2%8BInitialisatio9%C2%8C%20array-s%20length%20%C2%8DQsimulating%20a%20JUMP%C2%8E%2440_MSTORE__%C2%8FQsimulatXJUMPI%20_POP%C2%90%20functio9signaturX%C2%91%232nd%3Bo9stack(0x%01%C2%91%C2%90%C2%8F%C2%8E%C2%8D%C2%8C%C2%8B%C2%8A%C2%89%C2%88%C2%87%C2%86%C2%85%C2%84%C2%83%C2%82%C2%81%C2%80%7F%7C%60%5E%40%3F%3E%3B%3A%25%24%23!()-9BGKNQX_~_">EVM Playground</a> and will run through it below. </p><p>The simplified version organises the opcodes sequentially removing any JUMP&#8217;s and any code that isn&#8217;t relevant to memory manipulation. Comments have been added to the code to provide context to what is being done. The code is split into 6 distinct sections which we will delve into. </p><p>I cannot stress enough how important it is to use the <a href="https://www.evm.codes/playground?unit=Wei&amp;callData=0xd275ca72&amp;codeType=Mnemonic&amp;code='BBQFreXM%3APointer%20%C2%8B~_BB_QvaluXto%20storXfor%20NG80)128%3F%2480Qlocatio9for%20NG40)64%3F%C2%8EQjump!9%7Brequired%20to%20prevent%20stack%20underflow%7D%20_PUSH2Gffff___BBB~~~%C2%83a%7F%2F_BBB~~~_Qload%20N%2440_MLOAD_%23N_DUP1Q0xa0)160%3F%2C%2032%20*%205)160%20first%C2%8C5%24a0QN(0x8%C2%82a0%7D)new%20N_ADDQSavXthis%20new%20valuX0x120%20to%20thXfreXmemory!n%C2%8E_BB%2FQM%3A%C2%8BVaraiblX%E2%80%9Ca%E2%80%9D%20~%2F_BB%2F_%230x80_DUP1QpushG05)5%3F(array%20length%7D%2405QSwap%20thXtop%202-tems%20%C2%86andG80_SWAP1QpushG20)32%C2%88e%7D%2420QDuplicatXthX3rd%3B%C2%86to%20thX%C2%84_DUP3Q0x05%20*G20)5%20*%2032-9decmial)160%20o9%C2%84(sizXof%20array-9bytes%7D_MULQDuplicatX0xa0)160%3F_DUP1QReturns%20sizXof%20calldata-9bytes%20currently%20just%C2%90%3DG04%20or%204-9decmial%C2%89stack(0x80%7D_DUP4Q0x80(%7CXcopie%40Ga%C2%8A_Qthis%20offsets%20thX4%20bytes-9our%20call%20data%20with%20a%20sizXofGa0%20which%20yeild%20a%20160%20bit%20set%20of%200%22s%20to%20bXstored%20at%20thXN!n_Qthis%20effectively-nitialises%20our%20array-9m%3A%C2%87%60%C2%80%25eded_%230xa0_DUP1%230x80_DUP3Qnew%20N%20as%20before_ADDQswap%201st(0x120%7D%3Bo9%3E%20and%203rd(0x80%7D_SWAP2K80%7D_POPKa0%7D_POPQS%C2%81120%20%26G05_SWAP1K05%7D_POPK120%7D_POPQs%C2%8180%20%26Gb6(jump!n%7D_SWAP1%C2%8D%5E%C2%85%C2%85%C2%8D%2C%5E_BBB~~%2F%C2%83b%7F_BBB~~%2F_QN%20load-9%2440_MLOAD%23N(0x120%7D_DUP1Q0x40)64%3F%2C%2032%20*%202)64%20second%C2%8C2%2440QN(0x12%C2%8240%7D)new%20N_ADDQsavXnew%20N%20valuXat%20freXmemory!nG40%C2%8EBBQM%3A%C2%8BVariablX%E2%80%9Cb%E2%80%9D%20~_BB_%230x120(m%3Astart!9for%20variablX%5C'b%5C'%7D_DUP1Q0x02)2%3F)array%20length%2402Qs%C2%8102%20%26G120_SWAP1Q0x20)32%C2%88Xi9bytes%7D%2420%233rd%3Bo9%3EG02_DUP3Q0x02%20*G20%20%3DG40)64(amount%20of%20bytes-9m%3Ato-nitialise%7D_MUL%230x40(N!n%7D_DUP1QsamXas%20beforX4%20bytes%20for%C2%900x04%C2%89%3E%20%3DG120_DUP4Q0x120(%7CXcopie%40G4%C2%8A%C2%87%60%C2%80%25eded__~duplicatXthX%C2%84G40_DUP1%233rd%3Bo9%3EG120_DUP3Qadd%20together%20yields%20N%20value_ADDQswapG160%20%26G120_SWAP2K120%7D_POPK40%7D_POPQswapG160%20%26G02_SWAP1K02%7D_POPK160%7D_POPQjump!9to%20%C2%84Gbe_SWAP1QsimulatXjump%20pop%20jump!9off%20stack_POP__B~~~QAssig9ValuXto%20b%5B0%5D%20~_B~~~_QpushG01%2C%20valuXto%20add%20b%5B0%5D%2401QpushG00%2400Qleft%20shift%20operatio9no%20shift%2C%20first-nput-s%200%20_SHL%C2%91120%7D_DUP2QpushG00)%5B0%5D%20wherXi9thXarray%20should%20this%3Bgo%2400QpushG20)64%20bytes%20thXlength%20of%20thXarray%20%2402%C2%9100%7D_DUP2Q0x00%20%3CG20)%20truX%3DG01(check%20thXuser-s%20not%20trying%20to%20storXa%20valuXat%20a!9that%20doesn%22t%20exist-9thXarray%7D_LTQjump!n_PUSH2G00d7Q2%20POPs%20sincXthis-s%20a%20JUMPI(checking-f%20LT%20returned%20truXor%20false%7D%C2%8F%C2%8F_QpushG20(32%20bytes%20aray%3Bsize%7D%2420Q0x20%20*G00%20%3DG00)0%C2%88X*-ndex%20to%20determinXbytXoffset%7D_MULQ0x00%20%2BG120_ADD%232nd%20o9stackG01(valuXfor%20b%5B0%5D%7D_DUP2%232nd%20o9stackG120(memory!9for%20b%5B%5D%7D_DUP2QstorX0x01%20at%20memory!nG120_MSTOREQclea9up%20stack_POP_POP_POP_POP_'~%2F%2F_%5CnXe%20Q_~%20NfreXm%3ApointerKQpop%20top%3Boff%20stack(0xG%200xB~~~~~~~~~~9n%20-%20i)%20%3D%20(%20%7B!%20locatio%23QduplicatX%24_PUSH1G%25%5C'a%5C'%20and%20removes%20any-tems%20that%20arXno%20longer%20ne%3Aemory%20%3B-tem%20%3EthXstack%3F-9decimal%40d.%7D%2CG04(bytXoffset-9thXcalldata%20to%20copy.%7D%2C%5E%20removXthXtop%3Boff%20stack%20with%20POP_POP_%60ng%20lines-9this%20sectio9manipulatX%3E%20to%20%7CbytXoffset-9thXm%3AwherXthXresult%20will%20b%7F%E2%80%9D%20%26%20FreXM%3APointer%20UpdatX~%C2%80ensurXwXhavXthXmemory!9of%20variablX%C2%81wap%20top%202-temsG%C2%820%7D%20%2B%20spacXfor%20array(0x%C2%83QM%3AAllocatio9VaribalX%E2%80%9C%C2%84top%20of%20%3E%C2%85QSimulated%20jump!n_PUSH2Gffff%C2%86o9%3E-9this%20casX0x05%20%C2%87_CALLDATACOPY_QThXremaini%C2%88%3F(array%3Bsiz%C2%89_CALLDATASIZE%234th%3Bo9%C2%8A0(bytXsizXto%20copy.%7D%C2%8BInitialisatio9%C2%8C%20array-s%20length%20%C2%8DQsimulating%20a%20JUMP%C2%8E%2440_MSTORE__%C2%8FQsimulatXJUMPI%20_POP%C2%90%20functio9signaturX%C2%91%232nd%3Bo9stack(0x%01%C2%91%C2%90%C2%8F%C2%8E%C2%8D%C2%8C%C2%8B%C2%8A%C2%89%C2%88%C2%87%C2%86%C2%85%C2%84%C2%83%C2%82%C2%81%C2%80%7F%7C%60%5E%40%3F%3E%3B%3A%25%24%23!()-9BGKNQX_~_">playground</a> and step through the opcodes yourself. This will greatly enhance your learning. Now let&#8217;s dig into the 6 sections.</p><h4>Free Memory Pointer Initialisation (EVM Playground Lines 1-15)</h4><p>First, we have &#8220;free memory pointer initialisation&#8221; which we have discussed above. A value of 0x80 (128 in decimal) is pushed onto the stack. This is the value of the free memory pointer and is determined by Solidity&#8217;s memory layout. At this stage, we have nothing in memory.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!0xfJ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F6437f902-7104-4009-8858-230d73a765a8_1066x198.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!0xfJ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F6437f902-7104-4009-8858-230d73a765a8_1066x198.png 424w, https://substackcdn.com/image/fetch/$s_!0xfJ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F6437f902-7104-4009-8858-230d73a765a8_1066x198.png 848w, https://substackcdn.com/image/fetch/$s_!0xfJ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F6437f902-7104-4009-8858-230d73a765a8_1066x198.png 1272w, https://substackcdn.com/image/fetch/$s_!0xfJ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F6437f902-7104-4009-8858-230d73a765a8_1066x198.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!0xfJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F6437f902-7104-4009-8858-230d73a765a8_1066x198.png" width="1066" height="198" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/6437f902-7104-4009-8858-230d73a765a8_1066x198.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:198,&quot;width&quot;:1066,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:29985,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!0xfJ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F6437f902-7104-4009-8858-230d73a765a8_1066x198.png 424w, https://substackcdn.com/image/fetch/$s_!0xfJ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F6437f902-7104-4009-8858-230d73a765a8_1066x198.png 848w, https://substackcdn.com/image/fetch/$s_!0xfJ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F6437f902-7104-4009-8858-230d73a765a8_1066x198.png 1272w, https://substackcdn.com/image/fetch/$s_!0xfJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F6437f902-7104-4009-8858-230d73a765a8_1066x198.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>Next, we push the free memory pointer location 0x40 (64 in decimal) again determined by Solidity&#8217;s memory layout. </p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Xqed!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F61258407-3f1c-44ff-a81f-982dc17a174e_1066x256.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Xqed!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F61258407-3f1c-44ff-a81f-982dc17a174e_1066x256.png 424w, https://substackcdn.com/image/fetch/$s_!Xqed!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F61258407-3f1c-44ff-a81f-982dc17a174e_1066x256.png 848w, https://substackcdn.com/image/fetch/$s_!Xqed!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F61258407-3f1c-44ff-a81f-982dc17a174e_1066x256.png 1272w, https://substackcdn.com/image/fetch/$s_!Xqed!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F61258407-3f1c-44ff-a81f-982dc17a174e_1066x256.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Xqed!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F61258407-3f1c-44ff-a81f-982dc17a174e_1066x256.png" width="1066" height="256" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/61258407-3f1c-44ff-a81f-982dc17a174e_1066x256.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:256,&quot;width&quot;:1066,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:29907,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Xqed!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F61258407-3f1c-44ff-a81f-982dc17a174e_1066x256.png 424w, https://substackcdn.com/image/fetch/$s_!Xqed!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F61258407-3f1c-44ff-a81f-982dc17a174e_1066x256.png 848w, https://substackcdn.com/image/fetch/$s_!Xqed!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F61258407-3f1c-44ff-a81f-982dc17a174e_1066x256.png 1272w, https://substackcdn.com/image/fetch/$s_!Xqed!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F61258407-3f1c-44ff-a81f-982dc17a174e_1066x256.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>Finally, we call MSTORE which pops the first item off the stack 0x40 to determine where to write to in memory and the second value 0x80 as what to write. </p><p>This leaves us with an empty stack but we have now populated some memory. This memory representation is in hexadecimal where each character represents 4 bits.</p><p>We have 192 hexadecimal characters in memory which means we have 96 bytes (1 byte = 8 bits = 2 hexadecimal characters). </p><p>If we refer back to Solidity&#8217;s memory layout we were told the first 64 bytes would be allocated as scratch space and the next 32 would be for the free memory pointer.</p><p>Thats exactly what we have below.       </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!unYm!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F62ee6b98-6d06-4b91-a12a-45cc86a9ab5c_1066x304.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!unYm!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F62ee6b98-6d06-4b91-a12a-45cc86a9ab5c_1066x304.png 424w, https://substackcdn.com/image/fetch/$s_!unYm!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F62ee6b98-6d06-4b91-a12a-45cc86a9ab5c_1066x304.png 848w, https://substackcdn.com/image/fetch/$s_!unYm!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F62ee6b98-6d06-4b91-a12a-45cc86a9ab5c_1066x304.png 1272w, https://substackcdn.com/image/fetch/$s_!unYm!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F62ee6b98-6d06-4b91-a12a-45cc86a9ab5c_1066x304.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!unYm!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F62ee6b98-6d06-4b91-a12a-45cc86a9ab5c_1066x304.png" width="1066" height="304" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/62ee6b98-6d06-4b91-a12a-45cc86a9ab5c_1066x304.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:304,&quot;width&quot;:1066,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:32434,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!unYm!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F62ee6b98-6d06-4b91-a12a-45cc86a9ab5c_1066x304.png 424w, https://substackcdn.com/image/fetch/$s_!unYm!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F62ee6b98-6d06-4b91-a12a-45cc86a9ab5c_1066x304.png 848w, https://substackcdn.com/image/fetch/$s_!unYm!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F62ee6b98-6d06-4b91-a12a-45cc86a9ab5c_1066x304.png 1272w, https://substackcdn.com/image/fetch/$s_!unYm!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F62ee6b98-6d06-4b91-a12a-45cc86a9ab5c_1066x304.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h4>Memory Allocation Variable &#8220;a&#8221; &amp; Free Memory Pointer Update (EVM Playground Lines 16-34)</h4><p>For the remaining sections, we&#8217;re going to skip to the end state of each section and give a high-level overview of what happened for brevity. The individual opcode steps can be seen via the <a href="https://www.evm.codes/playground?unit=Wei&amp;callData=0xd275ca72&amp;codeType=Mnemonic&amp;code='BBQFreXM%3APointer%20%C2%8B~_BB_QvaluXto%20storXfor%20NG80)128%3F%2480Qlocatio9for%20NG40)64%3F%C2%8EQjump!9%7Brequired%20to%20prevent%20stack%20underflow%7D%20_PUSH2Gffff___BBB~~~%C2%83a%7F%2F_BBB~~~_Qload%20N%2440_MLOAD_%23N_DUP1Q0xa0)160%3F%2C%2032%20*%205)160%20first%C2%8C5%24a0QN(0x8%C2%82a0%7D)new%20N_ADDQSavXthis%20new%20valuX0x120%20to%20thXfreXmemory!n%C2%8E_BB%2FQM%3A%C2%8BVaraiblX%E2%80%9Ca%E2%80%9D%20~%2F_BB%2F_%230x80_DUP1QpushG05)5%3F(array%20length%7D%2405QSwap%20thXtop%202-tems%20%C2%86andG80_SWAP1QpushG20)32%C2%88e%7D%2420QDuplicatXthX3rd%3B%C2%86to%20thX%C2%84_DUP3Q0x05%20*G20)5%20*%2032-9decmial)160%20o9%C2%84(sizXof%20array-9bytes%7D_MULQDuplicatX0xa0)160%3F_DUP1QReturns%20sizXof%20calldata-9bytes%20currently%20just%C2%90%3DG04%20or%204-9decmial%C2%89stack(0x80%7D_DUP4Q0x80(%7CXcopie%40Ga%C2%8A_Qthis%20offsets%20thX4%20bytes-9our%20call%20data%20with%20a%20sizXofGa0%20which%20yeild%20a%20160%20bit%20set%20of%200%22s%20to%20bXstored%20at%20thXN!n_Qthis%20effectively-nitialises%20our%20array-9m%3A%C2%87%60%C2%80%25eded_%230xa0_DUP1%230x80_DUP3Qnew%20N%20as%20before_ADDQswap%201st(0x120%7D%3Bo9%3E%20and%203rd(0x80%7D_SWAP2K80%7D_POPKa0%7D_POPQS%C2%81120%20%26G05_SWAP1K05%7D_POPK120%7D_POPQs%C2%8180%20%26Gb6(jump!n%7D_SWAP1%C2%8D%5E%C2%85%C2%85%C2%8D%2C%5E_BBB~~%2F%C2%83b%7F_BBB~~%2F_QN%20load-9%2440_MLOAD%23N(0x120%7D_DUP1Q0x40)64%3F%2C%2032%20*%202)64%20second%C2%8C2%2440QN(0x12%C2%8240%7D)new%20N_ADDQsavXnew%20N%20valuXat%20freXmemory!nG40%C2%8EBBQM%3A%C2%8BVariablX%E2%80%9Cb%E2%80%9D%20~_BB_%230x120(m%3Astart!9for%20variablX%5C'b%5C'%7D_DUP1Q0x02)2%3F)array%20length%2402Qs%C2%8102%20%26G120_SWAP1Q0x20)32%C2%88Xi9bytes%7D%2420%233rd%3Bo9%3EG02_DUP3Q0x02%20*G20%20%3DG40)64(amount%20of%20bytes-9m%3Ato-nitialise%7D_MUL%230x40(N!n%7D_DUP1QsamXas%20beforX4%20bytes%20for%C2%900x04%C2%89%3E%20%3DG120_DUP4Q0x120(%7CXcopie%40G4%C2%8A%C2%87%60%C2%80%25eded__~duplicatXthX%C2%84G40_DUP1%233rd%3Bo9%3EG120_DUP3Qadd%20together%20yields%20N%20value_ADDQswapG160%20%26G120_SWAP2K120%7D_POPK40%7D_POPQswapG160%20%26G02_SWAP1K02%7D_POPK160%7D_POPQjump!9to%20%C2%84Gbe_SWAP1QsimulatXjump%20pop%20jump!9off%20stack_POP__B~~~QAssig9ValuXto%20b%5B0%5D%20~_B~~~_QpushG01%2C%20valuXto%20add%20b%5B0%5D%2401QpushG00%2400Qleft%20shift%20operatio9no%20shift%2C%20first-nput-s%200%20_SHL%C2%91120%7D_DUP2QpushG00)%5B0%5D%20wherXi9thXarray%20should%20this%3Bgo%2400QpushG20)64%20bytes%20thXlength%20of%20thXarray%20%2402%C2%9100%7D_DUP2Q0x00%20%3CG20)%20truX%3DG01(check%20thXuser-s%20not%20trying%20to%20storXa%20valuXat%20a!9that%20doesn%22t%20exist-9thXarray%7D_LTQjump!n_PUSH2G00d7Q2%20POPs%20sincXthis-s%20a%20JUMPI(checking-f%20LT%20returned%20truXor%20false%7D%C2%8F%C2%8F_QpushG20(32%20bytes%20aray%3Bsize%7D%2420Q0x20%20*G00%20%3DG00)0%C2%88X*-ndex%20to%20determinXbytXoffset%7D_MULQ0x00%20%2BG120_ADD%232nd%20o9stackG01(valuXfor%20b%5B0%5D%7D_DUP2%232nd%20o9stackG120(memory!9for%20b%5B%5D%7D_DUP2QstorX0x01%20at%20memory!nG120_MSTOREQclea9up%20stack_POP_POP_POP_POP_'~%2F%2F_%5CnXe%20Q_~%20NfreXm%3ApointerKQpop%20top%3Boff%20stack(0xG%200xB~~~~~~~~~~9n%20-%20i)%20%3D%20(%20%7B!%20locatio%23QduplicatX%24_PUSH1G%25%5C'a%5C'%20and%20removes%20any-tems%20that%20arXno%20longer%20ne%3Aemory%20%3B-tem%20%3EthXstack%3F-9decimal%40d.%7D%2CG04(bytXoffset-9thXcalldata%20to%20copy.%7D%2C%5E%20removXthXtop%3Boff%20stack%20with%20POP_POP_%60ng%20lines-9this%20sectio9manipulatX%3E%20to%20%7CbytXoffset-9thXm%3AwherXthXresult%20will%20b%7F%E2%80%9D%20%26%20FreXM%3APointer%20UpdatX~%C2%80ensurXwXhavXthXmemory!9of%20variablX%C2%81wap%20top%202-temsG%C2%820%7D%20%2B%20spacXfor%20array(0x%C2%83QM%3AAllocatio9VaribalX%E2%80%9C%C2%84top%20of%20%3E%C2%85QSimulated%20jump!n_PUSH2Gffff%C2%86o9%3E-9this%20casX0x05%20%C2%87_CALLDATACOPY_QThXremaini%C2%88%3F(array%3Bsiz%C2%89_CALLDATASIZE%234th%3Bo9%C2%8A0(bytXsizXto%20copy.%7D%C2%8BInitialisatio9%C2%8C%20array-s%20length%20%C2%8DQsimulating%20a%20JUMP%C2%8E%2440_MSTORE__%C2%8FQsimulatXJUMPI%20_POP%C2%90%20functio9signaturX%C2%91%232nd%3Bo9stack(0x%01%C2%91%C2%90%C2%8F%C2%8E%C2%8D%C2%8C%C2%8B%C2%8A%C2%89%C2%88%C2%87%C2%86%C2%85%C2%84%C2%83%C2%82%C2%81%C2%80%7F%7C%60%5E%40%3F%3E%3B%3A%25%24%23!()-9BGKNQX_~_">EVM playground</a>.</p><p>Next memory is allocated for variable &#8220;a&#8221; <code>(bytes32[5])</code> and the free memory pointer is updated. </p><p>The compiler will have determined how much space is required through the array size and the default array element size.</p><blockquote><p>Remember elements in memory arrays in Solidity always occupy multiples of 32 bytes (this is even true for <code>bytes1[]</code>, but not for <code>bytes</code> and <code>string</code>) </p></blockquote><p>The size of the array multiplied by 32 bytes tells us how much memory we need to allocate. </p><p>In this case that calculation 5  * 32 yields 160 or 0xa0 in hex. We can see this being pushed onto the stack and added to the current free memory pointer 0x80 (128 in decimal) to get the new free memory pointer value. </p><p>This returns 0x120 (288 in decimal) which we can see has been written to the free memory pointer location.</p><p>The call stack keeps the memory location of the variable &#8220;a&#8221; on the stack 0x80 so it can reference it later if needed. 0xffff represents a JUMP location and can be ignored since it isn&#8217;t relevant to memory manipulation.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!A4XM!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf90605a-11c5-4d15-a055-b45dc6f93d9f_1066x378.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!A4XM!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf90605a-11c5-4d15-a055-b45dc6f93d9f_1066x378.png 424w, https://substackcdn.com/image/fetch/$s_!A4XM!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf90605a-11c5-4d15-a055-b45dc6f93d9f_1066x378.png 848w, https://substackcdn.com/image/fetch/$s_!A4XM!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf90605a-11c5-4d15-a055-b45dc6f93d9f_1066x378.png 1272w, https://substackcdn.com/image/fetch/$s_!A4XM!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf90605a-11c5-4d15-a055-b45dc6f93d9f_1066x378.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!A4XM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf90605a-11c5-4d15-a055-b45dc6f93d9f_1066x378.png" width="1066" height="378" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/df90605a-11c5-4d15-a055-b45dc6f93d9f_1066x378.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:378,&quot;width&quot;:1066,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:41360,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!A4XM!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf90605a-11c5-4d15-a055-b45dc6f93d9f_1066x378.png 424w, https://substackcdn.com/image/fetch/$s_!A4XM!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf90605a-11c5-4d15-a055-b45dc6f93d9f_1066x378.png 848w, https://substackcdn.com/image/fetch/$s_!A4XM!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf90605a-11c5-4d15-a055-b45dc6f93d9f_1066x378.png 1272w, https://substackcdn.com/image/fetch/$s_!A4XM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf90605a-11c5-4d15-a055-b45dc6f93d9f_1066x378.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h4>Memory Initialisation Variable &#8220;a&#8221; (EVM Playground Lines 35-95)</h4><p>Now that the memory has been allocated and the free memory pointer updated we need to initialise the memory space for variable &#8220;a&#8221;. Since the variable is just declared and not assigned it will be initialised with the zero value.</p><p>To do this write the EVM uses CALLDATACOPY which takes in 3 variables.</p><ul><li><p>memoryOffset (which memory location to copy the data to) </p></li><li><p>calldataOffset (byte offset in the calldata to copy)</p></li><li><p>size (byte size to copy)</p></li></ul><p>In our case, the memoryOffset is the memory location for variable &#8220;a&#8221; (0x80). The calldataOffset is the actual size of our calldata since we don&#8217;t want to copy any of the calldata, we want to initialise the memory with the zero value. Finally, the size is 0xa0 or 160 bytes since that is the size of the variable.</p><p>We can see our memory has expanded to 288 bytes (this includes the zero slot) and the stack again holds the memory location of the variable and a JUMP location on the call stack. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!8M2h!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F77921922-6699-4ef7-a248-5b96667ac1cc_1066x548.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!8M2h!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F77921922-6699-4ef7-a248-5b96667ac1cc_1066x548.png 424w, https://substackcdn.com/image/fetch/$s_!8M2h!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F77921922-6699-4ef7-a248-5b96667ac1cc_1066x548.png 848w, https://substackcdn.com/image/fetch/$s_!8M2h!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F77921922-6699-4ef7-a248-5b96667ac1cc_1066x548.png 1272w, https://substackcdn.com/image/fetch/$s_!8M2h!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F77921922-6699-4ef7-a248-5b96667ac1cc_1066x548.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!8M2h!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F77921922-6699-4ef7-a248-5b96667ac1cc_1066x548.png" width="1066" height="548" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/77921922-6699-4ef7-a248-5b96667ac1cc_1066x548.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:548,&quot;width&quot;:1066,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:54824,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!8M2h!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F77921922-6699-4ef7-a248-5b96667ac1cc_1066x548.png 424w, https://substackcdn.com/image/fetch/$s_!8M2h!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F77921922-6699-4ef7-a248-5b96667ac1cc_1066x548.png 848w, https://substackcdn.com/image/fetch/$s_!8M2h!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F77921922-6699-4ef7-a248-5b96667ac1cc_1066x548.png 1272w, https://substackcdn.com/image/fetch/$s_!8M2h!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F77921922-6699-4ef7-a248-5b96667ac1cc_1066x548.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h4>Memory Allocation Variable &#8220;b&#8221; &amp; Free Memory Pointer Update (EVM Playground Lines 96-112)</h4><p>This is the same as the memory allocation and free memory pointer update for variable &#8220;a&#8221;<code> </code>except this time it is for <code>&#8220;bytes32[2] memory b&#8221;</code>. </p><p>The memory pointer is updated to 0x160 (352 in decimal) which is equal to the previous free memory pointer 288 plus the size of the new variable in bytes 64.</p><p>Note that the free memory pointer has updated in memory to 0x160 and we now have the memory location for variable &#8220;b&#8221; (0x120) on the stack. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!-zv6!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F6d38d4d0-f02f-4f85-a16e-11a3687ae095_1066x610.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!-zv6!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F6d38d4d0-f02f-4f85-a16e-11a3687ae095_1066x610.png 424w, https://substackcdn.com/image/fetch/$s_!-zv6!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F6d38d4d0-f02f-4f85-a16e-11a3687ae095_1066x610.png 848w, https://substackcdn.com/image/fetch/$s_!-zv6!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F6d38d4d0-f02f-4f85-a16e-11a3687ae095_1066x610.png 1272w, https://substackcdn.com/image/fetch/$s_!-zv6!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F6d38d4d0-f02f-4f85-a16e-11a3687ae095_1066x610.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!-zv6!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F6d38d4d0-f02f-4f85-a16e-11a3687ae095_1066x610.png" width="1066" height="610" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/6d38d4d0-f02f-4f85-a16e-11a3687ae095_1066x610.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:610,&quot;width&quot;:1066,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:58701,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!-zv6!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F6d38d4d0-f02f-4f85-a16e-11a3687ae095_1066x610.png 424w, https://substackcdn.com/image/fetch/$s_!-zv6!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F6d38d4d0-f02f-4f85-a16e-11a3687ae095_1066x610.png 848w, https://substackcdn.com/image/fetch/$s_!-zv6!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F6d38d4d0-f02f-4f85-a16e-11a3687ae095_1066x610.png 1272w, https://substackcdn.com/image/fetch/$s_!-zv6!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F6d38d4d0-f02f-4f85-a16e-11a3687ae095_1066x610.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h4>Memory Initialisation Variable &#8220;b&#8221; (EVM Playground Lines 113-162)</h4><p>Same as memory initialisation of variable &#8220;a&#8221;.  </p><p>Note that memory has increased to 352 bytes. The stack still holds memory locations for the 2 variables.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ilfD!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff45435da-8528-4e99-8a43-bab75decbf06_1066x630.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ilfD!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff45435da-8528-4e99-8a43-bab75decbf06_1066x630.png 424w, https://substackcdn.com/image/fetch/$s_!ilfD!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff45435da-8528-4e99-8a43-bab75decbf06_1066x630.png 848w, https://substackcdn.com/image/fetch/$s_!ilfD!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff45435da-8528-4e99-8a43-bab75decbf06_1066x630.png 1272w, https://substackcdn.com/image/fetch/$s_!ilfD!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff45435da-8528-4e99-8a43-bab75decbf06_1066x630.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ilfD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff45435da-8528-4e99-8a43-bab75decbf06_1066x630.png" width="1066" height="630" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/f45435da-8528-4e99-8a43-bab75decbf06_1066x630.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:630,&quot;width&quot;:1066,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:56146,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ilfD!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff45435da-8528-4e99-8a43-bab75decbf06_1066x630.png 424w, https://substackcdn.com/image/fetch/$s_!ilfD!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff45435da-8528-4e99-8a43-bab75decbf06_1066x630.png 848w, https://substackcdn.com/image/fetch/$s_!ilfD!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff45435da-8528-4e99-8a43-bab75decbf06_1066x630.png 1272w, https://substackcdn.com/image/fetch/$s_!ilfD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff45435da-8528-4e99-8a43-bab75decbf06_1066x630.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h4>Assign Value to b[0] (EVM Playground Lines 163-207)</h4><p>Finally, we get to assigning a value to array &#8220;b&#8221; index 0. The code states that b[0] should have a value of 1. </p><p>This value is pushed onto the stack 0x01. A bit shift left occurs next however the input for the bit shift is 0 meaning our value doesn&#8217;t change.</p><p>Next, the array index position to be written to 0x00 is pushed to the stack and a check is done to verify this value is less than the length of the array 0x02. If it isn&#8217;t the execution jumps to a different part of the bytecode which handles this error state. </p><p>The MUL (multiply) &amp; ADD opcodes are used to determine where in memory the value needs to be written for it to correspond to the correct array index. </p><pre><code>0x20 (32 in decimal) * 0x00 (0 in decimal) = 0x00</code></pre><p>Remember memory arrays are 32-byte elements so this value represents the start location of an array index. Given we are writing to index 0 we have no offset. </p><pre><code>0x00 + 0x120 = 0x120 (288 in decimal)</code></pre><p>ADD is used to add this offset value to the memory location for variable &#8220;b&#8221;. Given our offset was 0 we will write our data straight to the assigned memory location.</p><p>Finally, an MSTORE stores the value 0x01 to this memory location 0x120.</p><p>The image below shows the system state at the end of the function execution. All the stack items have been popped off. </p><p><em><strong>Note in actuality in remix there are a few items left on the stack, a JUMP location and the function signature however they are not relevant to memory manipulation and therefore have been omitted in the EVM playground.</strong></em></p><p>Our memory has been updated to include the b[0] = 1 assignment, on the third last line of our memory a 0 value has turned into a 1. </p><p>You can verify the value is at the correct memory location,  b[0] should occupy locations 0x120 - 0x13f (bytes 289 - 320).</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!N1JE!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa5516282-632f-4af3-8e0f-40b07497a9d7_1066x578.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!N1JE!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa5516282-632f-4af3-8e0f-40b07497a9d7_1066x578.png 424w, https://substackcdn.com/image/fetch/$s_!N1JE!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa5516282-632f-4af3-8e0f-40b07497a9d7_1066x578.png 848w, https://substackcdn.com/image/fetch/$s_!N1JE!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa5516282-632f-4af3-8e0f-40b07497a9d7_1066x578.png 1272w, https://substackcdn.com/image/fetch/$s_!N1JE!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa5516282-632f-4af3-8e0f-40b07497a9d7_1066x578.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!N1JE!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa5516282-632f-4af3-8e0f-40b07497a9d7_1066x578.png" width="1066" height="578" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/a5516282-632f-4af3-8e0f-40b07497a9d7_1066x578.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:578,&quot;width&quot;:1066,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:51439,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!N1JE!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa5516282-632f-4af3-8e0f-40b07497a9d7_1066x578.png 424w, https://substackcdn.com/image/fetch/$s_!N1JE!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa5516282-632f-4af3-8e0f-40b07497a9d7_1066x578.png 848w, https://substackcdn.com/image/fetch/$s_!N1JE!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa5516282-632f-4af3-8e0f-40b07497a9d7_1066x578.png 1272w, https://substackcdn.com/image/fetch/$s_!N1JE!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa5516282-632f-4af3-8e0f-40b07497a9d7_1066x578.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>There we have it &#127881; , that was a lot of information to take in but we now have a solid understanding of how contract memory works. This will serve us well the next time we need to write some solidity code. </p><p>When you&#8217;re jumping through some contract opcodes and see certain memory locations that keep popping up (0x40)  you&#8217;ll now know exactly what they mean.</p><p>Next, in the series, we &#8220;Demystify Storage Slot Packing&#8221; in <a href="https://noxx.substack.com/p/evm-deep-dives-the-path-to-shadowy-3ea?s=r">EVM Deep Dives - Part 3</a>.</p><p>Until next time.</p><p>noxx</p><p>Twitter <a href="https://twitter.com/noxx3xxon">@noxx3xxon</a></p>]]></content:encoded></item><item><title><![CDATA[EVM Deep Dives: The Path to Shadowy Super Coder 🥷 💻 - Part 1 ]]></title><description><![CDATA[Digging deep into the EVM mechanics during contract function calls]]></description><link>https://noxx.substack.com/p/evm-deep-dives-the-path-to-shadowy</link><guid isPermaLink="false">https://noxx.substack.com/p/evm-deep-dives-the-path-to-shadowy</guid><dc:creator><![CDATA[noxx]]></dc:creator><pubDate>Sun, 27 Feb 2022 18:51:08 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!fFEU!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3400bba6-f870-4b68-8ba8-118562b08aef_489x538.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>First principles thinking is a term we often hear. It focuses on deeply understanding the foundational concepts of a subject to enable better thinking in the design space of the components which are built on top.</p><p>In the smart contract world, the &#8220;Ethereum Virtual Machine&#8221; along with its algorithms and data structures are the first principles. Solidity and the smart contracts we create are the components built on top of this foundation. To be a great Solidity dev one must have a deep understanding of the EVM.</p><p>This is the first in a series of articles that will deep dive into the EVM and build that foundational knowledge needed to become a &#8220;shadowy super coder&#8221;. </p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://noxx.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://noxx.substack.com/subscribe?"><span>Subscribe now</span></a></p><h2>The Basics: Solidity &#8594; Bytecode &#8594; Opcode</h2><p>Before we begin, this article assumes some basic knowledge of Solidity and how it&#8217;s deployed to the Ethereum chain. We&#8217;ll briefly touch on these subjects however if you&#8217;d like a refresher see this article <a href="https://medium.com/@eiki1212/explaining-ethereum-contract-abi-evm-bytecode-6afa6e917c3b">here</a>.</p><p>As you know your Solidity code needs to be compiled into bytecode prior to being deployed to the Ethereum network. This bytecode corresponds to a series of opcode instructions that the EVM interprets.</p><p>This series will focus on specific parts of the compiled bytecode and illuminate how they work. By the end of each article, you should have a much clearer understanding of how each component functions. Along the way, you will learn lots of the foundational concepts relating to the EVM.</p><p>Today we&#8217;re going to look at a basic Solidity contract along with an excerpt of its bytecode/opcodes to demonstrate how the EVM selects functions. </p><p>The runtime bytecode created from Solidity contracts is a representation of the entire contract. Within the contract, you may have multiple functions that can be called once it is deployed. </p><p>A common question is how does the EVM know what bytecode to execute depending on which function of the contract is called. This is the first question we&#8217;ll use to help understand the underlying mechanics of the EVM and how this particular case is handled.</p><h3>1_Storage.sol Breakdown</h3><p>For our demo, we will use the 1_Storage.sol contract, which is one of the default contracts in the online Solidity IDE <a href="https://remix.ethereum.org/">Remix</a>.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!fFEU!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3400bba6-f870-4b68-8ba8-118562b08aef_489x538.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!fFEU!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3400bba6-f870-4b68-8ba8-118562b08aef_489x538.png 424w, https://substackcdn.com/image/fetch/$s_!fFEU!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3400bba6-f870-4b68-8ba8-118562b08aef_489x538.png 848w, https://substackcdn.com/image/fetch/$s_!fFEU!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3400bba6-f870-4b68-8ba8-118562b08aef_489x538.png 1272w, https://substackcdn.com/image/fetch/$s_!fFEU!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3400bba6-f870-4b68-8ba8-118562b08aef_489x538.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!fFEU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3400bba6-f870-4b68-8ba8-118562b08aef_489x538.png" width="489" height="538" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/3400bba6-f870-4b68-8ba8-118562b08aef_489x538.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:538,&quot;width&quot;:489,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:63359,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!fFEU!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3400bba6-f870-4b68-8ba8-118562b08aef_489x538.png 424w, https://substackcdn.com/image/fetch/$s_!fFEU!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3400bba6-f870-4b68-8ba8-118562b08aef_489x538.png 848w, https://substackcdn.com/image/fetch/$s_!fFEU!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3400bba6-f870-4b68-8ba8-118562b08aef_489x538.png 1272w, https://substackcdn.com/image/fetch/$s_!fFEU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3400bba6-f870-4b68-8ba8-118562b08aef_489x538.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The contract has 2 functions store(uint256) and retrieve() that the EVM will have to decide between when a function call comes in. Below is the compiled runtime bytecode of the entire contract. </p><pre><code>608060405234801561001057600080fd5b50600436106100365760003560e01c80632e64cec11461003b5780636057361d14610059575b600080fd5b610043610075565b60405161005091906100d9565b60405180910390f35b610073600480360381019061006e919061009d565b61007e565b005b60008054905090565b8060008190555050565b60008135905061009781610103565b92915050565b6000602082840312156100b3576100b26100fe565b5b60006100c184828501610088565b91505092915050565b6100d3816100f4565b82525050565b60006020820190506100ee60008301846100ca565b92915050565b6000819050919050565b600080fd5b61010c816100f4565b811461011757600080fd5b5056fea2646970667358221220404e37f487a89a932dca5e77faaf6ca2de3b991f93d230604b1b8daaef64766264736f6c63430008070033 </code></pre><p>We are going to focus on the snippet of bytecode below. This snippet represents the function selector logic. Run &#8220;ctrl f&#8221; on the snippet to verify it is in the above bytecode.</p><pre><code>60003560e01c80632e64cec11461003b5780636057361d1461005957</code></pre><p>This bytecode corresponds to a set of EVM opcodes and their input values. You can check out the list of EVM opcodes <a href="https://www.ethervm.io/">here</a>. </p><p>Opcodes are 1 byte in length leading to 256 different possible opcodes. The EVM only uses 140 unique opcodes. </p><p>The below shows the bytecode snippet broken into its corresponding opcode commands. These are run sequentially on the call stack by the EVM. You can navigate to the link above to verify the opcode number 60 = PUSH1 etc. By the end of the article, you&#8217;ll have a full understanding of what these do. </p><pre><code>60 00                       =   PUSH1 0x00 
35                          =   CALLDATALOAD
60 e0                       =   PUSH1 0xe0
1c                          =   SHR
80                          =   DUP1  
63 2e64cec1                 =   PUSH4 0x2e64cec1
14                          =   EQ
61 003b                     =   PUSH2 0x003b
57                          =   JUMPI
80                          =   DUP1 
63 6057361d                 =   PUSH4 0x6057361d     
14                          =   EQ
61 0059                     =   PUSH2 0x0059
57                          =   JUMPI  </code></pre><h3>Smart Contract Function Calls &amp; Calldata</h3><p>Before diving deep into opcodes we need to quickly run through how we call a contract function.</p><p>When we call a contract function we include some calldata that specifies the function signature we are calling and any arguments that need to be passed in.</p><p>This can be done in Solidity with the following code.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ZCEc!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa9957ce1-945b-4afa-a395-c9d2563d2094_1614x670.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ZCEc!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa9957ce1-945b-4afa-a395-c9d2563d2094_1614x670.png 424w, https://substackcdn.com/image/fetch/$s_!ZCEc!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa9957ce1-945b-4afa-a395-c9d2563d2094_1614x670.png 848w, https://substackcdn.com/image/fetch/$s_!ZCEc!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa9957ce1-945b-4afa-a395-c9d2563d2094_1614x670.png 1272w, https://substackcdn.com/image/fetch/$s_!ZCEc!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa9957ce1-945b-4afa-a395-c9d2563d2094_1614x670.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ZCEc!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa9957ce1-945b-4afa-a395-c9d2563d2094_1614x670.png" width="1456" height="604" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/a9957ce1-945b-4afa-a395-c9d2563d2094_1614x670.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:604,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:148665,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ZCEc!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa9957ce1-945b-4afa-a395-c9d2563d2094_1614x670.png 424w, https://substackcdn.com/image/fetch/$s_!ZCEc!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa9957ce1-945b-4afa-a395-c9d2563d2094_1614x670.png 848w, https://substackcdn.com/image/fetch/$s_!ZCEc!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa9957ce1-945b-4afa-a395-c9d2563d2094_1614x670.png 1272w, https://substackcdn.com/image/fetch/$s_!ZCEc!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa9957ce1-945b-4afa-a395-c9d2563d2094_1614x670.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Here we are making a contract call to the store function with argument 10. We use abi.encodeWithSignature() to get the calldata in the desired format. The emit logs our calldata for testing.</p><pre><code>0x6057361d000000000000000000000000000000000000000000000000000000000000000a</code></pre><p>The above is what abi.encodeWithSignature("store(uint256)", 10) returns.</p><p>Earlier I mentioned function signatures, now let&#8217;s take a closer look at what they are.</p><blockquote><p>Function signatures are defined as the first four bytes of the Keccak hash of the canonical representation of the function signature. </p></blockquote><p>The canonical representation of the function signatures is the function name along with the function argument types ie. &#8220;store(uint256)&#8221; &amp; &#8220;retrieve()&#8221;. Below are the function signatures of the 1_Storage.sol contract, try hashing store(uint256) yourself to verify the results <a href="https://emn178.github.io/online-tools/keccak_256.html">here</a>.</p><pre><code>keccak256(&#8220;store(uint256)&#8221;) &#8594;  first 4 bytes = 6057361d

keccak256(&#8220;retrieve()&#8221;) &#8594; first 4 bytes = 2e64cec1</code></pre><p>Looking at our calldata above we can see that we have 36 bytes of calldata, the first 4 bytes of our calldata correspond to the function selector we just computed for the store(uint256) function.</p><p>The remaining 32 bytes correspond to our uint256 input argument. We have a hex value of &#8220;a&#8221; which is equal to 10 in decimal.</p><pre><code>6057361d = function signature (4 bytes)

000000000000000000000000000000000000000000000000000000000000000a = uint256 input (32 bytes)</code></pre><p>If we take the function signature 6057361d and refer back to the opcode section, run &#8220;ctrl f&#8221; on this value and see if you can find it. </p><h3>Opcodes &amp; The Call Stack </h3><p>We now have everything we need to commence our deep dive into what goes on at the EVM level during function selection.</p><p>We are going to run through each of the opcode commands what they do and how they affect the call stack.</p><p>If you&#8217;re unfamiliar with how a stack data structure works watch this quick <a href="https://www.youtube.com/watch?v=FNZ5o9S9prU">video</a> as a primer.</p><p>We start with PUSH1 which tells the EVM to push the next 1 byte of data, 0x00 (0 in decimal), to the call stack. Why we do this will become apparent with the next opcode.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!0Owu!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F52e45eff-44b3-4028-a075-9f5591fd2e7e_900x151.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!0Owu!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F52e45eff-44b3-4028-a075-9f5591fd2e7e_900x151.png 424w, https://substackcdn.com/image/fetch/$s_!0Owu!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F52e45eff-44b3-4028-a075-9f5591fd2e7e_900x151.png 848w, https://substackcdn.com/image/fetch/$s_!0Owu!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F52e45eff-44b3-4028-a075-9f5591fd2e7e_900x151.png 1272w, https://substackcdn.com/image/fetch/$s_!0Owu!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F52e45eff-44b3-4028-a075-9f5591fd2e7e_900x151.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!0Owu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F52e45eff-44b3-4028-a075-9f5591fd2e7e_900x151.png" width="900" height="151" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/52e45eff-44b3-4028-a075-9f5591fd2e7e_900x151.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:151,&quot;width&quot;:900,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:8660,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!0Owu!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F52e45eff-44b3-4028-a075-9f5591fd2e7e_900x151.png 424w, https://substackcdn.com/image/fetch/$s_!0Owu!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F52e45eff-44b3-4028-a075-9f5591fd2e7e_900x151.png 848w, https://substackcdn.com/image/fetch/$s_!0Owu!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F52e45eff-44b3-4028-a075-9f5591fd2e7e_900x151.png 1272w, https://substackcdn.com/image/fetch/$s_!0Owu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F52e45eff-44b3-4028-a075-9f5591fd2e7e_900x151.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>Next, we have CALLDATALOAD which pops off the first value on the stack (0) as input. </p><p>This opcode loads in the calldata to the stack using the &#8220;input&#8221; as an offset. Stack items are 32 bytes in size but our calldata is 36 bytes. The pushed value is msg.data[i:i+32] where &#8220;i&#8221; is this input. This ensures only 32 bytes are pushed to the stack but enables us to access any part of the calldata. </p><p>In this case, we have no offset (the value popped off of the stack was 0 from the previous PUSH1) so we push the first 32 bytes of the calldata to the call stack.</p><p>Remember earlier we logged our call data via an emit which equalled  &#8220;0x6057361d000000000000000000000000000000000000000000000000000000000000000a&#8221;.</p><p>This means the trailing 4 bytes (&#8220;0000000a&#8221;) are lost. If we had wanted to access the uint256 variable we would have used an offset of 4 to omit the function signature but include the full variable.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!9fC_!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6f79343-c4c4-4ee6-a29f-f1923fea5b9e_901x150.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!9fC_!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6f79343-c4c4-4ee6-a29f-f1923fea5b9e_901x150.png 424w, https://substackcdn.com/image/fetch/$s_!9fC_!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6f79343-c4c4-4ee6-a29f-f1923fea5b9e_901x150.png 848w, https://substackcdn.com/image/fetch/$s_!9fC_!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6f79343-c4c4-4ee6-a29f-f1923fea5b9e_901x150.png 1272w, https://substackcdn.com/image/fetch/$s_!9fC_!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6f79343-c4c4-4ee6-a29f-f1923fea5b9e_901x150.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!9fC_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6f79343-c4c4-4ee6-a29f-f1923fea5b9e_901x150.png" width="901" height="150" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/e6f79343-c4c4-4ee6-a29f-f1923fea5b9e_901x150.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:150,&quot;width&quot;:901,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:13228,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!9fC_!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6f79343-c4c4-4ee6-a29f-f1923fea5b9e_901x150.png 424w, https://substackcdn.com/image/fetch/$s_!9fC_!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6f79343-c4c4-4ee6-a29f-f1923fea5b9e_901x150.png 848w, https://substackcdn.com/image/fetch/$s_!9fC_!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6f79343-c4c4-4ee6-a29f-f1923fea5b9e_901x150.png 1272w, https://substackcdn.com/image/fetch/$s_!9fC_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6f79343-c4c4-4ee6-a29f-f1923fea5b9e_901x150.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>Another PUSH1 this time with the hex value 0xe0 which has a decimal value of 224. Remember function signatures are 4 bytes long or 32 bits. Our loaded calldata is 32 bytes long or 256 bits. 256 - 32 = 224 you may see where this is going.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!x3cv!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F161dea9b-d35b-4eb1-aac5-7adecb6cc17d_901x149.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!x3cv!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F161dea9b-d35b-4eb1-aac5-7adecb6cc17d_901x149.png 424w, https://substackcdn.com/image/fetch/$s_!x3cv!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F161dea9b-d35b-4eb1-aac5-7adecb6cc17d_901x149.png 848w, https://substackcdn.com/image/fetch/$s_!x3cv!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F161dea9b-d35b-4eb1-aac5-7adecb6cc17d_901x149.png 1272w, https://substackcdn.com/image/fetch/$s_!x3cv!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F161dea9b-d35b-4eb1-aac5-7adecb6cc17d_901x149.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!x3cv!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F161dea9b-d35b-4eb1-aac5-7adecb6cc17d_901x149.png" width="901" height="149" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/161dea9b-d35b-4eb1-aac5-7adecb6cc17d_901x149.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:149,&quot;width&quot;:901,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:15286,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!x3cv!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F161dea9b-d35b-4eb1-aac5-7adecb6cc17d_901x149.png 424w, https://substackcdn.com/image/fetch/$s_!x3cv!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F161dea9b-d35b-4eb1-aac5-7adecb6cc17d_901x149.png 848w, https://substackcdn.com/image/fetch/$s_!x3cv!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F161dea9b-d35b-4eb1-aac5-7adecb6cc17d_901x149.png 1272w, https://substackcdn.com/image/fetch/$s_!x3cv!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F161dea9b-d35b-4eb1-aac5-7adecb6cc17d_901x149.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>Next, we have SHR which is a bit shift right. It takes the first item off the stack (224) as an input of how much to shift by and the second item off the stack (0x6057361d0&#8230;00) represents what needs to be shifted. We can see after this operation we have our 4-byte function selector on the call stack.</p><p>If you are unfamiliar with how bit shifts work see <a href="https://youtu.be/fDKUq38H2jk?t=176">this</a> short video.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!WSF6!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8db5bd19-2271-44b3-99ed-0eec2731be5c_893x144.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!WSF6!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8db5bd19-2271-44b3-99ed-0eec2731be5c_893x144.png 424w, https://substackcdn.com/image/fetch/$s_!WSF6!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8db5bd19-2271-44b3-99ed-0eec2731be5c_893x144.png 848w, https://substackcdn.com/image/fetch/$s_!WSF6!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8db5bd19-2271-44b3-99ed-0eec2731be5c_893x144.png 1272w, https://substackcdn.com/image/fetch/$s_!WSF6!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8db5bd19-2271-44b3-99ed-0eec2731be5c_893x144.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!WSF6!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8db5bd19-2271-44b3-99ed-0eec2731be5c_893x144.png" width="893" height="144" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/8db5bd19-2271-44b3-99ed-0eec2731be5c_893x144.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:144,&quot;width&quot;:893,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:9586,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!WSF6!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8db5bd19-2271-44b3-99ed-0eec2731be5c_893x144.png 424w, https://substackcdn.com/image/fetch/$s_!WSF6!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8db5bd19-2271-44b3-99ed-0eec2731be5c_893x144.png 848w, https://substackcdn.com/image/fetch/$s_!WSF6!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8db5bd19-2271-44b3-99ed-0eec2731be5c_893x144.png 1272w, https://substackcdn.com/image/fetch/$s_!WSF6!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8db5bd19-2271-44b3-99ed-0eec2731be5c_893x144.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>Next is DUP1, a simple opcode that takes the value on the top of the stack and duplicates it.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!0zzf!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F40b72e9d-6e80-4232-9099-8718604542a8_896x146.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!0zzf!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F40b72e9d-6e80-4232-9099-8718604542a8_896x146.png 424w, https://substackcdn.com/image/fetch/$s_!0zzf!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F40b72e9d-6e80-4232-9099-8718604542a8_896x146.png 848w, https://substackcdn.com/image/fetch/$s_!0zzf!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F40b72e9d-6e80-4232-9099-8718604542a8_896x146.png 1272w, https://substackcdn.com/image/fetch/$s_!0zzf!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F40b72e9d-6e80-4232-9099-8718604542a8_896x146.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!0zzf!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F40b72e9d-6e80-4232-9099-8718604542a8_896x146.png" width="896" height="146" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/40b72e9d-6e80-4232-9099-8718604542a8_896x146.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:146,&quot;width&quot;:896,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:14244,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!0zzf!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F40b72e9d-6e80-4232-9099-8718604542a8_896x146.png 424w, https://substackcdn.com/image/fetch/$s_!0zzf!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F40b72e9d-6e80-4232-9099-8718604542a8_896x146.png 848w, https://substackcdn.com/image/fetch/$s_!0zzf!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F40b72e9d-6e80-4232-9099-8718604542a8_896x146.png 1272w, https://substackcdn.com/image/fetch/$s_!0zzf!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F40b72e9d-6e80-4232-9099-8718604542a8_896x146.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>PUSH4 pushes the 4 byte function signature of retrieve() (0x2e64cec1) onto the call stack. </p><p>In case you&#8217;re wondering how it knows this value, remember this is in the bytecode that was compiled from the solidity code. The compiler, therefore, had information on all function names and argument types. </p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!yeIM!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F23dc9994-e360-4205-bf5d-af92aaba42e5_899x189.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!yeIM!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F23dc9994-e360-4205-bf5d-af92aaba42e5_899x189.png 424w, https://substackcdn.com/image/fetch/$s_!yeIM!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F23dc9994-e360-4205-bf5d-af92aaba42e5_899x189.png 848w, https://substackcdn.com/image/fetch/$s_!yeIM!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F23dc9994-e360-4205-bf5d-af92aaba42e5_899x189.png 1272w, https://substackcdn.com/image/fetch/$s_!yeIM!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F23dc9994-e360-4205-bf5d-af92aaba42e5_899x189.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!yeIM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F23dc9994-e360-4205-bf5d-af92aaba42e5_899x189.png" width="899" height="189" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/23dc9994-e360-4205-bf5d-af92aaba42e5_899x189.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:189,&quot;width&quot;:899,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:24631,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!yeIM!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F23dc9994-e360-4205-bf5d-af92aaba42e5_899x189.png 424w, https://substackcdn.com/image/fetch/$s_!yeIM!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F23dc9994-e360-4205-bf5d-af92aaba42e5_899x189.png 848w, https://substackcdn.com/image/fetch/$s_!yeIM!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F23dc9994-e360-4205-bf5d-af92aaba42e5_899x189.png 1272w, https://substackcdn.com/image/fetch/$s_!yeIM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F23dc9994-e360-4205-bf5d-af92aaba42e5_899x189.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>EQ pops 2 values off of the stack, in this case, 0x2e64cec1 &amp; 0x6057361d and checks if they&#8217;re equal. If they are it pushes a 1 back to the stack, if not a 0.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!IHuW!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F7cfc3f88-d5eb-4b03-b4c3-eb605bdeb283_895x144.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!IHuW!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F7cfc3f88-d5eb-4b03-b4c3-eb605bdeb283_895x144.png 424w, https://substackcdn.com/image/fetch/$s_!IHuW!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F7cfc3f88-d5eb-4b03-b4c3-eb605bdeb283_895x144.png 848w, https://substackcdn.com/image/fetch/$s_!IHuW!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F7cfc3f88-d5eb-4b03-b4c3-eb605bdeb283_895x144.png 1272w, https://substackcdn.com/image/fetch/$s_!IHuW!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F7cfc3f88-d5eb-4b03-b4c3-eb605bdeb283_895x144.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!IHuW!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F7cfc3f88-d5eb-4b03-b4c3-eb605bdeb283_895x144.png" width="895" height="144" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/7cfc3f88-d5eb-4b03-b4c3-eb605bdeb283_895x144.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:144,&quot;width&quot;:895,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:10100,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!IHuW!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F7cfc3f88-d5eb-4b03-b4c3-eb605bdeb283_895x144.png 424w, https://substackcdn.com/image/fetch/$s_!IHuW!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F7cfc3f88-d5eb-4b03-b4c3-eb605bdeb283_895x144.png 848w, https://substackcdn.com/image/fetch/$s_!IHuW!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F7cfc3f88-d5eb-4b03-b4c3-eb605bdeb283_895x144.png 1272w, https://substackcdn.com/image/fetch/$s_!IHuW!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F7cfc3f88-d5eb-4b03-b4c3-eb605bdeb283_895x144.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>PUSH2 pushes 2 bytes of data onto the call stack 0x003b in hex which is equal to 59 in decimal. </p><p>The call stack has something called a program counter which specifies where in the bytecode the next execution command is. Here we set 59 because that is the location for the start of the retrieve() bytecode. (Note the EVM Playground section below will help crystallize how this works)</p><p>You can view the program counter location in a similar way to a line number location in your Solidity code. If the function is defined on line 59 you can use the line number as a way to tell the machine where to find the code for that function. </p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!g3tG!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F67596d4e-054d-4cd7-b516-64b4789ee01f_900x190.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!g3tG!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F67596d4e-054d-4cd7-b516-64b4789ee01f_900x190.png 424w, https://substackcdn.com/image/fetch/$s_!g3tG!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F67596d4e-054d-4cd7-b516-64b4789ee01f_900x190.png 848w, https://substackcdn.com/image/fetch/$s_!g3tG!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F67596d4e-054d-4cd7-b516-64b4789ee01f_900x190.png 1272w, https://substackcdn.com/image/fetch/$s_!g3tG!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F67596d4e-054d-4cd7-b516-64b4789ee01f_900x190.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!g3tG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F67596d4e-054d-4cd7-b516-64b4789ee01f_900x190.png" width="900" height="190" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/67596d4e-054d-4cd7-b516-64b4789ee01f_900x190.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:190,&quot;width&quot;:900,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:17122,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!g3tG!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F67596d4e-054d-4cd7-b516-64b4789ee01f_900x190.png 424w, https://substackcdn.com/image/fetch/$s_!g3tG!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F67596d4e-054d-4cd7-b516-64b4789ee01f_900x190.png 848w, https://substackcdn.com/image/fetch/$s_!g3tG!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F67596d4e-054d-4cd7-b516-64b4789ee01f_900x190.png 1272w, https://substackcdn.com/image/fetch/$s_!g3tG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F67596d4e-054d-4cd7-b516-64b4789ee01f_900x190.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>JUMPI stands for &#8220;jump if&#8221;. It pops 2 values off of the stack as input, the first (59) is the jump location and the second (0) is the bool value for whether this jump should be executed. Where 1 = true and 0 = false. </p><p>If it is true the program counter will be updated and the execution will jump to that location. In our case it is false, the program counter is not altered and the execution continues as normal.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!sytv!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1357763b-4150-4e14-8a8c-583ee74572aa_896x146.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!sytv!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1357763b-4150-4e14-8a8c-583ee74572aa_896x146.png 424w, https://substackcdn.com/image/fetch/$s_!sytv!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1357763b-4150-4e14-8a8c-583ee74572aa_896x146.png 848w, https://substackcdn.com/image/fetch/$s_!sytv!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1357763b-4150-4e14-8a8c-583ee74572aa_896x146.png 1272w, https://substackcdn.com/image/fetch/$s_!sytv!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1357763b-4150-4e14-8a8c-583ee74572aa_896x146.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!sytv!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1357763b-4150-4e14-8a8c-583ee74572aa_896x146.png" width="896" height="146" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/1357763b-4150-4e14-8a8c-583ee74572aa_896x146.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:146,&quot;width&quot;:896,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:9965,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!sytv!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1357763b-4150-4e14-8a8c-583ee74572aa_896x146.png 424w, https://substackcdn.com/image/fetch/$s_!sytv!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1357763b-4150-4e14-8a8c-583ee74572aa_896x146.png 848w, https://substackcdn.com/image/fetch/$s_!sytv!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1357763b-4150-4e14-8a8c-583ee74572aa_896x146.png 1272w, https://substackcdn.com/image/fetch/$s_!sytv!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1357763b-4150-4e14-8a8c-583ee74572aa_896x146.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>DUP1 again.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!0zzf!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F40b72e9d-6e80-4232-9099-8718604542a8_896x146.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!0zzf!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F40b72e9d-6e80-4232-9099-8718604542a8_896x146.png 424w, https://substackcdn.com/image/fetch/$s_!0zzf!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F40b72e9d-6e80-4232-9099-8718604542a8_896x146.png 848w, https://substackcdn.com/image/fetch/$s_!0zzf!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F40b72e9d-6e80-4232-9099-8718604542a8_896x146.png 1272w, https://substackcdn.com/image/fetch/$s_!0zzf!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F40b72e9d-6e80-4232-9099-8718604542a8_896x146.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!0zzf!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F40b72e9d-6e80-4232-9099-8718604542a8_896x146.png" width="896" height="146" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/40b72e9d-6e80-4232-9099-8718604542a8_896x146.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:146,&quot;width&quot;:896,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!0zzf!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F40b72e9d-6e80-4232-9099-8718604542a8_896x146.png 424w, https://substackcdn.com/image/fetch/$s_!0zzf!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F40b72e9d-6e80-4232-9099-8718604542a8_896x146.png 848w, https://substackcdn.com/image/fetch/$s_!0zzf!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F40b72e9d-6e80-4232-9099-8718604542a8_896x146.png 1272w, https://substackcdn.com/image/fetch/$s_!0zzf!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F40b72e9d-6e80-4232-9099-8718604542a8_896x146.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>PUSH4 pushes the 4 byte function signature of store(uint256) (0x6057361d) onto the call stack.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!H9yO!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff937657d-dbb3-4133-95bb-1b3f5b8117cd_897x188.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!H9yO!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff937657d-dbb3-4133-95bb-1b3f5b8117cd_897x188.png 424w, https://substackcdn.com/image/fetch/$s_!H9yO!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff937657d-dbb3-4133-95bb-1b3f5b8117cd_897x188.png 848w, https://substackcdn.com/image/fetch/$s_!H9yO!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff937657d-dbb3-4133-95bb-1b3f5b8117cd_897x188.png 1272w, https://substackcdn.com/image/fetch/$s_!H9yO!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff937657d-dbb3-4133-95bb-1b3f5b8117cd_897x188.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!H9yO!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff937657d-dbb3-4133-95bb-1b3f5b8117cd_897x188.png" width="897" height="188" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/f937657d-dbb3-4133-95bb-1b3f5b8117cd_897x188.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:188,&quot;width&quot;:897,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!H9yO!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff937657d-dbb3-4133-95bb-1b3f5b8117cd_897x188.png 424w, https://substackcdn.com/image/fetch/$s_!H9yO!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff937657d-dbb3-4133-95bb-1b3f5b8117cd_897x188.png 848w, https://substackcdn.com/image/fetch/$s_!H9yO!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff937657d-dbb3-4133-95bb-1b3f5b8117cd_897x188.png 1272w, https://substackcdn.com/image/fetch/$s_!H9yO!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff937657d-dbb3-4133-95bb-1b3f5b8117cd_897x188.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>EQ again however this time the result is true as the function signatures match.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!X9bB!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9c0617f7-2181-427e-9dca-917be7847f0a_898x145.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!X9bB!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9c0617f7-2181-427e-9dca-917be7847f0a_898x145.png 424w, https://substackcdn.com/image/fetch/$s_!X9bB!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9c0617f7-2181-427e-9dca-917be7847f0a_898x145.png 848w, https://substackcdn.com/image/fetch/$s_!X9bB!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9c0617f7-2181-427e-9dca-917be7847f0a_898x145.png 1272w, https://substackcdn.com/image/fetch/$s_!X9bB!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9c0617f7-2181-427e-9dca-917be7847f0a_898x145.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!X9bB!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9c0617f7-2181-427e-9dca-917be7847f0a_898x145.png" width="898" height="145" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/9c0617f7-2181-427e-9dca-917be7847f0a_898x145.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:145,&quot;width&quot;:898,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:9598,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!X9bB!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9c0617f7-2181-427e-9dca-917be7847f0a_898x145.png 424w, https://substackcdn.com/image/fetch/$s_!X9bB!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9c0617f7-2181-427e-9dca-917be7847f0a_898x145.png 848w, https://substackcdn.com/image/fetch/$s_!X9bB!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9c0617f7-2181-427e-9dca-917be7847f0a_898x145.png 1272w, https://substackcdn.com/image/fetch/$s_!X9bB!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9c0617f7-2181-427e-9dca-917be7847f0a_898x145.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>PUSH2, push the program counter location for the store(uint256) bytecode, 0x0059 in hex which is equal to 89 in decimal.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!gOEv!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F44dea0e0-9e0c-4459-bd1e-4af814c89203_898x186.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!gOEv!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F44dea0e0-9e0c-4459-bd1e-4af814c89203_898x186.png 424w, https://substackcdn.com/image/fetch/$s_!gOEv!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F44dea0e0-9e0c-4459-bd1e-4af814c89203_898x186.png 848w, https://substackcdn.com/image/fetch/$s_!gOEv!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F44dea0e0-9e0c-4459-bd1e-4af814c89203_898x186.png 1272w, https://substackcdn.com/image/fetch/$s_!gOEv!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F44dea0e0-9e0c-4459-bd1e-4af814c89203_898x186.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!gOEv!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F44dea0e0-9e0c-4459-bd1e-4af814c89203_898x186.png" width="898" height="186" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/44dea0e0-9e0c-4459-bd1e-4af814c89203_898x186.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:186,&quot;width&quot;:898,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:16831,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!gOEv!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F44dea0e0-9e0c-4459-bd1e-4af814c89203_898x186.png 424w, https://substackcdn.com/image/fetch/$s_!gOEv!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F44dea0e0-9e0c-4459-bd1e-4af814c89203_898x186.png 848w, https://substackcdn.com/image/fetch/$s_!gOEv!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F44dea0e0-9e0c-4459-bd1e-4af814c89203_898x186.png 1272w, https://substackcdn.com/image/fetch/$s_!gOEv!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F44dea0e0-9e0c-4459-bd1e-4af814c89203_898x186.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>JUMPI, this time the bool check is true meaning the jump executes. This updates the program counter to 89 which will move the execution to a different part of the bytecode. </p><p>At this location, there will be a JUMPDEST opcode, without this opcode at the destination the JUMPI will fail.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!P6pI!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9f3a7f3c-a5f6-4e29-888f-60708e8863dc_896x146.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!P6pI!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9f3a7f3c-a5f6-4e29-888f-60708e8863dc_896x146.png 424w, https://substackcdn.com/image/fetch/$s_!P6pI!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9f3a7f3c-a5f6-4e29-888f-60708e8863dc_896x146.png 848w, https://substackcdn.com/image/fetch/$s_!P6pI!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9f3a7f3c-a5f6-4e29-888f-60708e8863dc_896x146.png 1272w, https://substackcdn.com/image/fetch/$s_!P6pI!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9f3a7f3c-a5f6-4e29-888f-60708e8863dc_896x146.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!P6pI!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9f3a7f3c-a5f6-4e29-888f-60708e8863dc_896x146.png" width="896" height="146" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/9f3a7f3c-a5f6-4e29-888f-60708e8863dc_896x146.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:146,&quot;width&quot;:896,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:9965,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!P6pI!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9f3a7f3c-a5f6-4e29-888f-60708e8863dc_896x146.png 424w, https://substackcdn.com/image/fetch/$s_!P6pI!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9f3a7f3c-a5f6-4e29-888f-60708e8863dc_896x146.png 848w, https://substackcdn.com/image/fetch/$s_!P6pI!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9f3a7f3c-a5f6-4e29-888f-60708e8863dc_896x146.png 1272w, https://substackcdn.com/image/fetch/$s_!P6pI!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9f3a7f3c-a5f6-4e29-888f-60708e8863dc_896x146.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>There we have it, after this opcode executes you&#8217;ll be taken to the location of the store(uint156) bytecode and the execution of the function will continue as normal.</p><p>While this contract only had 2 functions the same principles apply to a contract with 20+ functions.</p><p>You now know how the  EVM determines the location of the function bytecode that it needs to execute based on a contract function call. It&#8217;s actually just a simple set of &#8220;if statements&#8221; for each function in your contract along with their jump locations.</p><h3>EVM Playground</h3><p>I highly recommend visiting this <a href="https://www.evm.codes/playground?unit=Wei&amp;callData=0x6057361d000000000000000000000000000000000000000000000000000000000000000a&amp;codeType=Mnemonic&amp;code='!0~0KCALLDATALOAD~2z2qw!E0~3KSHR~5z2qwDUP1~6(X4_2E64CEC1~7KEQ~12z5qwX2_3B~13(*I~16z3qwDUP1~17KX4_6057361D~18KEQ~23z5qwX2_59~24K*I~27z3qwkY%20wX30_0~28KwZGV59z31q!1~60%20%7BG%7DW%7DKwkYwX26_0~62z2qKZstore%7Buint256V89z27q!0%20ZContinueW.KK'~%20ZOffset%20z%20%7Bprevious%20instruFoccupies%20w%5Cnq)s%7DwkZThes-ar-just%20paddingNenabl-usNgetN_%200xZ%2F%2F%20Yprogram%20counter%2059%20%26%2089XPUSHW%20funFexecution...V%7D)codew*DEST~N%20to(wwGretrieve%7BFction%20-e%20*JUMP)%20byte(%20K!X1_%01!()*-FGKNVWXYZ_kqwz~_">link</a>, it&#8217;s an EVM playground where I have set up the exact bytecode we have just run through. You&#8217;ll be able to interactively see the stack changing and I&#8217;ve included the JUMPDEST so you can see what happens after the final JUMPI.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!hV5W!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F86591a6b-ee71-4590-8462-4ebb38f5cb80_1503x887.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!hV5W!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F86591a6b-ee71-4590-8462-4ebb38f5cb80_1503x887.png 424w, https://substackcdn.com/image/fetch/$s_!hV5W!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F86591a6b-ee71-4590-8462-4ebb38f5cb80_1503x887.png 848w, https://substackcdn.com/image/fetch/$s_!hV5W!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F86591a6b-ee71-4590-8462-4ebb38f5cb80_1503x887.png 1272w, https://substackcdn.com/image/fetch/$s_!hV5W!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F86591a6b-ee71-4590-8462-4ebb38f5cb80_1503x887.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!hV5W!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F86591a6b-ee71-4590-8462-4ebb38f5cb80_1503x887.png" width="1456" height="859" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/86591a6b-ee71-4590-8462-4ebb38f5cb80_1503x887.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:859,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:155064,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!hV5W!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F86591a6b-ee71-4590-8462-4ebb38f5cb80_1503x887.png 424w, https://substackcdn.com/image/fetch/$s_!hV5W!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F86591a6b-ee71-4590-8462-4ebb38f5cb80_1503x887.png 848w, https://substackcdn.com/image/fetch/$s_!hV5W!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F86591a6b-ee71-4590-8462-4ebb38f5cb80_1503x887.png 1272w, https://substackcdn.com/image/fetch/$s_!hV5W!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F86591a6b-ee71-4590-8462-4ebb38f5cb80_1503x887.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The EVM playground will also help with your understanding of the program counter, in the code, you&#8217;ll see comments next to each command with its offset which represents its program counter location.</p><p>You&#8217;ll also see the calldata input to the left of the Run button, try changing this to the retrieve() call data 0x2e64cec1 to see how the execution changes. Just click Run and then the &#8220;step into&#8221; (curled arrow) button at the top right to jump through each opcode one by one.</p><p>Next, in the series, we take a trip down &#8220;Memory&#8221; lane in <a href="https://noxx.substack.com/p/evm-deep-dives-the-path-to-shadowy-d6b?s=r">EVM Deep Dives - Part 2</a>.</p><p>See you soon.</p><p>noxx</p><p>Twitter <a href="https://twitter.com/noxx3xxon">@noxx3xxon</a></p>]]></content:encoded></item></channel></rss>