Skip to content

This project demonstrates how to use Hardhat and Solidity to deploy an arbitrage scanner to scrape data from multiple decentralized exchanges in a single RPC call.

Notifications You must be signed in to change notification settings

XDapps/defi-tools

Repository files navigation

Defi Arbitrage Scanner Demo

This project demonstrates how to use Hardhat and Solidity to create and deploy an arbitrage scanner to scrape data from multiple decentralized exchanges in a single RPC call. This gives you the ability to scrape and store large amounts of data allowing for arbitrage route analysis with minimal RPC calls.

Installation

Please see .example.env file and recreate a .env in the same format.

Note

The project will not run properly without a valid Infura API key and seed phrase in .env file. Seed phrase wallet can be random, it does NOT need a balance.

yarn

npx hardhat test

Contracts

  1. ArbUtils Library: manages decoding of exchange data. Deployment

  2. DecimalUtils Library: manages token decimal calculations and normalizing exchange rates. Deployment

  3. SlippageUtils Library: manages calculation of slippage. Deployment

  4. QuickswapV3Utils Library: manages interactions with QuickswapV3 contracts. Deployment

  5. UniswapV2Utils Library: manages interactions with UniswapV2 contracts. Deployment

  6. UniswapV3Utils Library: manages interactions with UniswapV3 contracts. Deployment

  7. ArbitrageScanner: contract that we interact with. Deployment

How to Use

You may interact with these contracts already deployed on Polygon, or you may extend these contracts by adding additional exchange protocols and re-deploying them on the EVM network(s) of your choice.

Interacting with Contracts

Try running some of the following tasks:

Get UniswapV2 pair counts, pair addresses, token addresses

npx hardhat run scripts/scrapers/scrapeV2PairCounts.ts --network polygon
npx hardhat run scripts/scrapers/scrapeV2PairAddresses.ts --network polygon 

Get normalized exchange rate(s) using a base asset and an array of potential pairs.

npx hh run scripts/exchange-rates/getExchangeRate.ts --network polygon 
npx hh run scripts/exchange-rates/getMultipleExchangeRates.ts --network polygon 

Validate prospective tokens by checking their liquidity vs other assets of your choice.

npx hh run scripts/validator/validateTokens.ts --network polygon 

Simulate the results of a multi-hop, multi-exchange trade path.

npx hh run scripts/simulate-trade/simulateTrade.ts --network polygon 

Exchanges

The scanner contract allows for multiple exchanges to be added. At deployment, the following exchanges were loaded.

DexId Exchange Fee
0 QuickswapV2 25
1 QuickswapV3 3000*
2 UniswapV3 500, 3000, 10000

Note

QuickswapV3 has dynamic fees. Number used is a placeholder.

Adding Exchanges

To add an exchange, you'd simply call this method on your contract:

    function setDexValues(
        uint256[] memory _dexIds,
        address[] memory _factories,
        address[] memory _routers,
        uint8[] memory _protocols
    ) public onlyOwner {
        for (uint256 i = 0; i < _dexIds.length; i++) {
            factories[_dexIds[i]] = _factories[i];
            routers[_dexIds[i]] = _routers[i];
            protocol[_dexIds[i]] = _protocols[i];
        }
    }

You could also deploy additional libraries to support additional exchange protocols and then add those protocols to the ArbitrageScanner contract.

About

This project demonstrates how to use Hardhat and Solidity to deploy an arbitrage scanner to scrape data from multiple decentralized exchanges in a single RPC call.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published