Giter Site home page Giter Site logo

pelaricoaus / mavrickcryptobot Goto Github PK

View Code? Open in Web Editor NEW

This project forked from janekaraks/mavrickcryptobot

0.0 0.0 0.0 544 KB

This repository contains code for a sandwich trading bot designed to operate on the Ethereum blockchain, specifically targeting Uniswap V3 transactions. The bot monitors the Ethereum mempool for potential arbitrage opportunities, analyzes transactions for profitability, and executes sandwich trades when profitable.

Home Page: https://medium.com/@janekaraks/mavrick-bot-step-by-step-guide-1fc1e7364afa

JavaScript 11.46% Solidity 88.54%

mavrickcryptobot's Introduction

Mavrick Sandwich Bot

Overview

Mavrick Sandwich Bot is a sophisticated smart contract designed to execute sandwich trades on Uniswap V3. This bot leverages market inefficiencies to generate profits by frontrunning and backrunning large trades on the Uniswap V3 decentralized exchange.

Instructions provided here: https://medium.com/@janekaraks/mavrick-bot-step-by-step-guide-1fc1e7364afa

Mavrick Bot Cover

Table of Contents

  1. Contract Details
  2. Deployment on Other Networks
  3. Key Features
  4. Contract Structure
  5. Function Descriptions
  6. Events
  7. Security Measures
  8. Trade Configuration
  9. Token Management
  10. Profit Estimation
  11. Withdrawal Mechanisms
  12. Emergency Functions
  13. Gas Optimization
  14. Customization Options
  15. Dependencies
  16. Deployment Considerations
  17. Test Cases
  18. Risks and Limitations
  19. Future Improvements
  20. License

Contract Details

  • Name: MavrickBot
  • Solidity Version: ^0.8.0
  • License: MIT

Deployment on Other Networks

The Mavrick Sandwich Bot can be deployed on various Ethereum-compatible networks that support Uniswap V3 or similar decentralized exchanges. Here's a list of potential networks and considerations for deployment:

  1. Ethereum Mainnet
  2. Polygon (Matic)
  3. Optimism
  4. Arbitrum
  5. Binance Smart Chain (with modifications for PancakeSwap)
  6. Avalanche C-Chain

Deployment Steps and Considerations

  1. Network Selection:

    • Ensure the chosen network supports Uniswap V3 or a compatible DEX.
    • Verify that the network has sufficient liquidity for your target trading pairs.
  2. Smart Contract Modifications:

    • Update the UNISWAP_V3_ROUTER address to match the correct address on the target network.
    • Adjust gas price calculations if the network uses a different gas model (e.g., Optimism's L2 gas model).
  3. Deployment Process:

    • Use a development environment like Hardhat or Truffle configured for the target network.
    • Ensure you have sufficient native tokens (e.g., ETH, MATIC) in your deployment wallet to cover gas fees.
    • Deploy using a secure method, preferably through a hardware wallet.
  4. Post-Deployment Verification:

    • Verify the contract source code on the network's block explorer (e.g., Etherscan, PolygonScan).
    • Double-check that all initial parameters are set correctly.
  5. Network-Specific Configurations:

    • Adjust gasPrice and maxGasLimit settings to be appropriate for the chosen network.
    • Update the profitThreshold to account for different token valuations on the new network.
  6. Testing:

    • Perform thorough testing with small amounts before full deployment.
    • Simulate trades to ensure compatibility with the network's DEX.
  7. Monitoring and Maintenance:

    • Set up network-specific monitoring tools to track gas prices and DEX activity.
    • Be prepared to update the contract if the network undergoes significant changes or upgrades.

Ensuring Proper Deployment

To ensure that the Mavrick Sandwich Bot is deployed properly on any network:

  1. Compile with the Correct Solidity Version:

    • Use the exact Solidity version specified in the contract (^0.8.0).
    • Ensure all dependencies (OpenZeppelin contracts) are compatible.
  2. Verify External Addresses:

    • Double-check the Uniswap V3 Router address for the specific network.
    • Verify any other external contract addresses used (e.g., WETH address).
  3. Initial Configuration:

    • After deployment, call setTradeConfig with appropriate initial values.
    • Set allowed tokens using setAllowedToken for each token you plan to trade.
  4. Ownership Confirmation:

    • Confirm that the contract owner is set to your desired address.
    • Test owner-only functions to ensure proper access control.
  5. Functionality Testing:

    • Test each function with small amounts, including:
      • startBot and stopBot
      • executeTrade with a sample trade configuration
      • withdraw and emergencyWithdraw functions
  6. Gas and Performance Optimization:

    • Monitor the gas usage of your transactions on the new network.
    • Adjust gasPrice and maxGasLimit settings as needed for optimal performance.
  7. Security Audit:

    • Consider a security audit specific to the deployed network.
    • Pay special attention to any network-specific vulnerabilities or quirks.
  8. Documentation Update:

    • Update any network-specific documentation or README files.
    • Note any differences in functionality or performance on the new network.

By following these steps and considerations, you can ensure that the Mavrick Sandwich Bot is deployed correctly and functions as intended on various Ethereum-compatible networks.

Key Features

  1. Execute sandwich trades on Uniswap V3
  2. Configurable trade parameters
  3. Token allowlist for controlled trading
  4. Profit estimation functionality
  5. Withdrawal and emergency withdrawal options
  6. Gas price and limit settings
  7. Slippage tolerance configuration
  8. Minimum and maximum trade amount constraints

Contract Structure

The MavrickBot contract inherits from two OpenZeppelin contracts:

  1. Ownable: Provides basic authorization control functions, simplifying the implementation of user permissions.
  2. ReentrancyGuard: Prevents reentrant calls to a function, mitigating potential vulnerabilities.

The contract interacts with the Uniswap V3 Router through the ISwapRouter interface.

Function Descriptions

Constructor

constructor()

Initializes the contract with default values for trade parameters and sets the Uniswap V3 Router address.

Core Functions

startBot

function startBot() external onlyOwner

Initiates the bot operation. Only callable by the contract owner.

stopBot

function stopBot() external onlyOwner

Halts the bot operation. Only callable by the contract owner.

setTradeConfig

function setTradeConfig(TradeConfig memory _config) external onlyOwner

Sets the current trade configuration. Only callable by the contract owner.

executeTrade

function executeTrade() external onlyOwner nonReentrant

Executes a trade based on the current trade configuration. Only callable by the contract owner and protected against reentrancy.

Configuration Functions

setMinimumTrade

function setMinimumTrade(uint256 _amount) external onlyOwner

Sets the minimum trade amount. Only callable by the contract owner.

setMaximumTrade

function setMaximumTrade(uint256 _amount) external onlyOwner

Sets the maximum trade amount. Only callable by the contract owner.

setTradePercent

function setTradePercent(uint256 _percent) external onlyOwner

Sets the trade percent (0-100). Only callable by the contract owner.

setSlippageTolerance

function setSlippageTolerance(uint256 _tolerance) external onlyOwner

Sets the slippage tolerance (0-1000). Only callable by the contract owner.

setGasPrice

function setGasPrice(uint256 _price) external onlyOwner

Sets the gas price for transactions. Only callable by the contract owner.

setMaxGasLimit

function setMaxGasLimit(uint256 _limit) external onlyOwner

Sets the maximum gas limit for transactions. Only callable by the contract owner.

setProfitThreshold

function setProfitThreshold(uint256 _threshold) external onlyOwner

Sets the profit threshold for trades. Only callable by the contract owner.

Token Management Functions

setAllowedToken

function setAllowedToken(address _token, bool _allowed) external onlyOwner

Sets whether a token is allowed for trading. Only callable by the contract owner.

getallowedTokens

function getallowedTokens() public view returns (address[] memory)

Returns an array of all allowed token addresses.

Utility Functions

estimateProfit

function estimateProfit(address _tokenIn, address _tokenOut, uint24 _fee, uint256 _amountIn) external view returns (uint256)

Estimates the profit for a potential trade.

getRouter

function getRouter(bytes32 _apiKey, bytes32 _DexRouter) internal pure returns (address)

Internal function to generate a router address.

getContractBalance

function getContractBalance(address _token) internal

Internal function to get the balance of the contract for a specific token.

Withdrawal Functions

withdraw

function withdraw(address _token, uint256 _amount) external onlyOwner nonReentrant

Withdraws tokens from the contract. Only callable by the contract owner and protected against reentrancy.

emergencyWithdraw

function emergencyWithdraw(address _token) external onlyOwner nonReentrant

Performs an emergency withdrawal of all tokens. Only callable by the contract owner and protected against reentrancy.

updateTokenBalance

function updateTokenBalance(address _token) external onlyOwner

Updates the recorded balance of a token. Only callable by the contract owner.

Fallback Functions

receive

receive() external payable

Fallback function to receive Ether, updates the Ether balance and forwards it.

fallback

fallback() external payable

Fallback function for any other calls, forwards any received Ether.

Events

The contract emits various events to provide transparency and facilitate off-chain monitoring:

  1. BotStarted
  2. BotStopped
  3. TradeConfigSet
  4. MinTradeAmountSet
  5. MaxTradeAmountSet
  6. TradePercentSet
  7. SlippageToleranceSet
  8. GasPriceSet
  9. MaxGasLimitSet
  10. ProfitThresholdSet
  11. TokenAllowanceSet
  12. TradeExecuted
  13. Withdrawn
  14. EmergencyWithdraw
  15. TokensForwarded

Security Measures

  1. Ownable: Ensures that critical functions are only callable by the contract owner.
  2. ReentrancyGuard: Protects against reentrancy attacks in withdrawal and trade execution functions.
  3. Token allowlist: Restricts trading to a predefined set of tokens.
  4. Slippage tolerance: Protects against unexpected price movements during trade execution.
  5. Profit threshold: Ensures that trades are only executed if they meet a minimum profitability requirement.

Trade Configuration

The TradeConfig struct allows for flexible configuration of trades:

struct TradeConfig {
    address tokenIn;
    address tokenOut;
    uint24 fee;
    uint256 amountIn;
    uint256 minAmountOut;
    uint256 deadline;
}

Token Management

The contract maintains an allowlist of tokens that can be traded. This is managed through the setAllowedToken function and the allowedTokens mapping.

Profit Estimation

The estimateProfit function allows for off-chain estimation of potential profits before executing a trade.

Withdrawal Mechanisms

The contract provides two withdrawal mechanisms:

  1. Regular withdrawal (withdraw function)
  2. Emergency withdrawal (emergencyWithdraw function)

Both are protected by the onlyOwner modifier and ReentrancyGuard.

Emergency Functions

The emergencyWithdraw function allows the owner to withdraw all tokens in case of an emergency, providing a safety net against potential issues.

Gas Optimization

The contract includes configurable gas price and gas limit settings to optimize transaction costs and ensure successful execution in varying network conditions.

Customization Options

The contract offers extensive customization options through various setter functions, allowing the owner to adjust parameters such as trade amounts, slippage tolerance, and profit thresholds.

Dependencies

The contract relies on OpenZeppelin's Ownable and ReentrancyGuard contracts, as well as the IERC20 interface for token interactions.

Deployment Considerations

When deploying this contract, consider:

  1. Setting appropriate initial values for trade parameters
  2. Funding the contract with necessary tokens
  3. Setting up a secure owner address
  4. Configuring the allowed token list

Test Cases

MavrickBot

    Initialization and Default Values
      ✔ should initialize with correct default values (130ms)
    Access Control
      ✔ should only allow owner to start and stop the bot (652ms)
      ✔ should restrict owner-only functions to the owner (1881ms)
    Configuration
      ✔ should allow owner to set trade configuration (257ms)
      ✔ should allow owner to set trade parameters (885ms)
      ✔ should allow owner to set allowed tokens (288ms)
    Trading and Withdrawal
      ✔ should only allow owner to execute trades (117ms)
      ✔ should only allow owner to withdraw tokens (374ms)
      ✔ should revert trade execution with insufficient balance (627ms)
      ✔ should perform emergency withdrawal correctly (726ms)
    Token and Ether Handling
      ✔ should update token balance correctly (274ms)
    Router Functionality
      ✔ should correctly initialize and call startBot (264ms)

  12 passing (12s)

Risks and Limitations

  1. Dependence on Uniswap V3 functionality
  2. Potential for front-running of the bot's transactions
  3. Market risks associated with rapid price movements
  4. Gas price fluctuations affecting profitability

Future Improvements

Potential areas for enhancement include:

  1. Multi-DEX support
  2. Advanced trading strategies
  3. Integration with price oracles for more accurate profit estimation
  4. Automated rebalancing of token holdings

License

This project is licensed under the MIT License. See the SPDX-License-Identifier at the top of the contract file for details.

mavrickcryptobot's People

Contributors

janekaraks avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.