Discover more from Degen Code
Introduction and Index
I’ve written a lot of very dense content, and I’m starting to see similar questions in the comments and direct messages.
It’s overwhelming for new subscribers, so this will be pinned to the top of the list and will serve as your guidepost to all the content here. I will edit it after publishing new articles, so check in occasionally to make sure you haven’t missed something.
Where Should I Start?
The absolute best way to work through the lessons here is by starting at the very first post and working through each one after, in order.
I wrote the lessons assuming that you have zero knowledge at the beginning, other than some basic technical knowledge. Each successive lesson assumes that you have read the ones before it. I will often spend a lot of time explaining some idea or technique when I introduce it, but afterwards will provide a link back to it or mention it briefly.
How Should I Use the Code?
I encourage you to use the projects that I publish as a general guide. Each will provide code for a working bot that uses the techniques I have previously covered.
The downside of publishing an easily copied bot to a large technical audience is that the specific strategy will become very crowded.
As an example, after I published the sSPELL-SPELL bot, the particular liquidity pool quickly became very active with very familiar looking bots. If you rely solely on my project bots, you will have a bad time as you compete with hundreds of other readers.
Instead of attempting to squeeze profit from the project bot, I want you to explore its structure, understand the strategy, and think about other ways you could use it with other tokens, pools, and blockchains.
You can join the Degen Code Discord HERE. Most readers prefer to ask questions there instead of through the Substack comment system, which is fairly limited.
I also use the #files channel to post source code for various lessons.
These lessons are not necessarily in order. They are divided into sections based on a theme, and I’ve written a very brief summary of what each lesson covers.
Development Environment and Package Setup
Your Development Environment: Setting up Linux, Python, Brownie, and other Python tools
Introduction to Brownie: Connecting Brownie to a blockchain network, generating an account address and private key, accessing the Brownie console to interact with the network.
Loading a Token Smart Contract: Fetching a smart contract from a block explorer, loading it into Brownie, and exploring the built-in methods that Brownie provides to interact with the contract on the console.
Setting Up a Dedicated Bot VPS: Setting up a Linux VPS in the cloud to improve uptime and latency.
Interacting with the Blockchain Using the Console
Router Contracts — Exploring getAmountsOut(): Loading a UniswapV2-style router contract, sending queries to determine swap rates for arbitrary tokens.
Project: (un)Stablecoin Watcher: Build a very simple bot to watch the swap rates between various stablecoin pairs and print a message when the swap is profitable.
Router Contracts — Swaps and Approvals: Set ERC-20 token allowances for future swapping via the router contract.
Your First Console Swap: Trade a small balance of one stablecoin for another on the console via the router contract.
Your First Automated Swap: Build a more complex bot that automates token approval, continuously monitored swap rates between two stablecoins, and attempts to swap at a profit via the router.
Using a 3rd Party RPC: Signing up with a 3rd party RPC instead of the public RPC for better performance.
Working with ABIs and Unverified Contracts: Loading a contract with an ABI when the source is not published on a block explorer.
Local Forking With Ganache: Launch a local fork, which allows you to test your bot against a local blockchain copy without spending real money or broadcasting public transactions.
Automated Market Makers and the Role of the Arbitrageur: A high level discussion of the positive contribution of an abitrageur.
Lessons Learned from a Successful Arbitrage: A history of my first arbitrage bot and its return on investment.
Bot Building — Structure and Helper Functions: A discussion of various helper functions to simplify repetitive botting tasks.
Bot Building — Staking Rate Watcher: A simple implementation to track on-chain data for use in an arbitrage strategy.
Bot Building — Setup, Main Loop and Swapping Logic: The complete sSPELL-SPELL bot source code.
NOTE: the sSPELL-SPELL arbitrage has dried up and the token swapping activity is very low now. You should not expect to make any money with this bot.
Fun With Pool Reserves: Calculating swaps directly via formulae, instead of querying
Pool Reserves — Part II: Partial Swaps, Max Profits: Improving the calculation to support partial swaps at a target ratio.
Pool Reserves — Part III: Swap and Quote Function Upgrades: Implementing direct swap calculation instead of
getAmountsOutin a working sSPELL-SPELL bot.
Get a Pair: Querying a UniswapV2 factory contract for liquidity pool addresses, instead of looking them up via the web frontend.
Pool Contract: Review the foundational core contract of UniswapV3. Investigate liquidity, ticks, square root price and pool state functions.
Router Contract: Review and test the four types of swaps performed by the UniswapV3 router.
TickLens Contract: Calculate and retrieve the initialized status of multiple tick indices for a known UniswapV3 pool by hand and using the TickLens accessory contract.
Quoter Contract: Calculate gas use and amounts for exactInput and exactOutput swaps using the V3 Quoter accessory contract.
Multicall: Learn how to encode and decode multiple transaction inputs to any V3 contract with built-in multicall functionality.
Swap Prediction — Part I: Predict and verify the behavior of a single pool single tick swap.
Swap Prediction — Part II: Predict and verify the behavior of a single pool multi-tick swap.
Swap Prediction — Part III: Predict and verify the behavior of a multi-pool, multi-tick swap.
Project: Mempool Watcher: Watch and decode mempool transactions sent to the V3 Router contracts, including multicall transactions.
LP Helper — Part I: Develop the basic skeleton of a V3-focused liquidity pool helper class for object-oriented programming in Python.
LP Helper — Part II: Extend the helper to support tick and liquidity fetching from the on-chain contract
Cycle Arbitrage Helper — Part I: Develop the basic skeleton of a V3-compatible cycle arbitrage helper class for object-oriented programming in Python.
Cycle Arbitrage Helper — Part II: Extend the helper to support tracking and updating pool states, swap calculation across multiple pools, and swap input generation using the ported
swapfunction from the Solidity contract.
Cycle Arbitrage Helper — Part III: Extend the helper to generate arbitrage payload generation for arbitrage execution via generic address/calldata payloads.
Project: Uniswap V2 & V3 2-Pool Arbitrage Bot: Build an automated bot that searches and submits 2-pool cycle arbitrage transactions between V2 and V3 pools via Flashbots Auction.
General EVM and Blockchain
Fighting and Winning Gas Wars: Legacy vs. EIP-1559 gas
Chainlink Price Feeds, Calculating Trade EV, Automatically Scaling Gas Fees: Getting on-chain token prices, adjusting trades based on expected value.
EV-Based Gas Scaling: Implementing EV scaling in a working sSPELL-SPELL bot.
Event Listeners: Accessing the built-in “logs” feature to read events emitted when certain blockchain actions occur.
Asynchronous Websocket Listeners: Introduction to asyncio, Python’s concurrent code implementation, and how to monitor multiple websockets for passive monitoring of events, pending transactions, and new blocks.
Multicall Requests: Batch multiple read-only requests using Brownie, instead of sending individual calls. Use the multicall feature to retrieve all known liquidity pools on three Avalanche DEXes.
Encoding and Decoding Calldata with eth_abi: Directly encode and decode byte values for inclusion in calldata (and other things) without using a middleman wrapper function.
Avoiding Trap Tokens With eth_call: Learn how to identify fee/tax on transfer tokens, override storage, balance, and code states for eth_call, and execute arbitrary function calls against a live blockchain without deploying the contract.
Save Gas With Packed Calldata: Reduce gas use by close-packing and decoding calldata inputs to your contract instead of using default padded ABI encoding.
The Great Object-Oriented Rewrite: Transforming the monolithic bot structure into reusable Python classes, shared via Github.
Project: Limit Order Bot: Using the degenbot classes to implement a generic “limit order” bot that will wait for favorable ratios and execute one-time trades.
JSON Data Exchange: Read and write JSON to load data between running programs and take advantage of more human-readable data structures.
Smart Contract Arbitrage
Smart Contract Arbitrage — Introduction and Overview: A high level overview of how atomic smart contract swaps work.
Smart Contract Arbitrage — Toolkit and A Simple Contract: Introduction to Vyper and using Brownie to deploy and test smart contracts.
Smart Contract Arbitrage — Interfaces: Using interfaces to access other smart contracts from our smart contract.
Smart Contract Arbitrage — The Flash Borrow Callback: Introduction to the callback feature of the UniswapV2 pair contract which enables flash borrowing.
Smart Contract Arbitrage — Two-Pool Flash Swap: A simple implementation of a Vyper contract that performs two-pool atomic flash swap arbitrage.
Smart Contract Arbitrage — Optimizing the Input: Introduction to SciPy, a module that quickly calculates optimal inputs for the non-linear flash borrow / LP swap arbitrage strategy.
Smart Contract Arbitrage — Towards A Smarter Contract: Implementing better security checks, eliminate the use of the router in favor of swapping directly at the LP contract, and reduce gas use.
Smart Contract Arbitrage — Bot: Build an example bot that drives a two-pool arbitrage smart contract.
Smart Contract Arbitrage — Calldata Pass-through, ABI Encode / Decode, Flexible Fallback: Develop a method to pass arbitrary payloads through the UniswapV2 callback feature, which eliminates the need to use storage variables.
Generalized Vyper Smart Contracts: Introduce the concept of generalized EVM bytecode payloads, which can be executed without using an interface or specific logic for a given external smart contract.
Vyper Bundle Executor: Develop a general-purpose Vyper contract for arbitrary execution of EVM calldata.
How I Learned to Stop Worrying and Love the Mempool: Watching pending transactions on Fantom before they are confirmed and recorded to the blockchain.
Snowsight — Getting Started: Introduction to Snowsight, a mempool subscription service for Avalanche, which does not provide public access to the mempool.
Using the Snowsight Transaction Propagator: Use the Snowsight transaction propagator to broadcast trades directly to validators (miners) on the Snowsight network.
Project: Snowsight Arbitrage Bot: Build a bot that uses Snowsight to monitor pending transactions and submit trades to the transaction propagator.
Updates to the Snowsight Bot: Modify the bot to support the changes to the Snowsight after the close of their beta program.
Mempool Backrunning: Extend the Snowsight bot to perform mempool transaction backrunning for a simple two-pool arbitrage.
Predicting The Effect of Mempool Transactions: Generalize the future reserve calculation approach to support multiple token swap types.
Mempool Arbitrage Improvements — Multi-pool Prediction: Extend the generalized future reserve calculation to support overriding the reserves for any LP in a general arbitrage path.
Find Arbitrage Paths Using Graph Theory and NetworkX: Use NetworkX to automatically build arbitrage pathways from available LP information.
Automatic Arbitrage Path Building: Generalize and automate the arbitrage helper generation from the NetworkX pathways, which eliminates the need to manually enter arbitrage paths.
Running a Local Ethereum Node: Use Docker to run a standalone local node instead of using an external RPC.
Flashbots — Setup and Identity: Introduction to Flashbots, the private mining pool for MEV searchers, and the Flashbots Auction.
Flashbots — Bundle and Simulate: Learn how to construct a Flashbots-compatible bundle of transactions for submission through the Flashbots relay.
Flashbots — Smart Contract Bribe: Implement the miner bribe, a direct ETH payment, to build more competitive bundles.
Flashbots — Project: Two-Pool UniswapV2 Arbitrage Bot: Build an onchain and mempool backrunning arbitrage bot for two-pool UniswapV2 style opportunities. Uses Flashbots relay for execution.
Flashbots — Project Improvements: Bundle Submission Hotfix, Multipool Arbitrage, Filtering Mempool Transactions with eth_call, Dropping Failing Arbs: Quality-of-life improvements for the two-pool UniswapV2 example.
Degen Code is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.