yuichiroaoki / poly-flashloan-bot

Flashloan Bot on Polygon
https://yuichiroaoki.medium.com/flashloan-bot-on-polygon-part-2-3eae0ac33986?sk=80821da4ca1d0462c7c9ae617df56bdd
MIT License
302 stars 192 forks source link

Trade array doesn't read string of protocols - messes up router setup! #103

Closed LordReya closed 1 year ago

LordReya commented 1 year ago

So I have been continuing to make the bot profitable. It's finally working, however it still has some flaws. The config.ts says which of the protocols from my map should be used:

export const tradingRoutes: ITrade[] = [ { path: [ ERC20Token.USDC, ERC20Token.DAI, ERC20Token.USDC, ERC20Token.MATIC, ERC20Token.USDT, ERC20Token.WBTC, ERC20Token.LINK, ERC20Token.WMATIC, ERC20Token.WETH, ERC20Token.UNI, ERC20Token.AAVE, ERC20Token.CRV, ERC20Token.QI, ERC20Token.MANA, ERC20Token.BNB, ERC20Token.GRT, ], //add tokens! protocols: [1, 3, 6], //not working correctly. Only does Quoter and 2nd protocol amountIn: getBigNumber(20, ERC20Token.USDC.decimals), },

The trading finder uses these protocols to find arbitrage oppertunities:

export const findOpp = async (trade: ITrade) => {
  let amountOut = trade.amountIn;
  for (const [i, protocol] of trade.protocols.entries()) {
    switch (protocol) {

      case 1:
        try {
          amountOut = await getPriceOnSushi(
            trade.path[i].address,
            trade.path[i + 1].address,
            amountOut,
            findRouterFromProtocol(1)
          );
          break;
        } catch (e) {
          logError(e);
          amountOut = getBigNumber(0);
          break;
        }
      case 3:
        try {
          amountOut = await getPriceOnApeswap(
            trade.path[i].address,
            trade.path[i + 1].address,
            amountOut,
            findRouterFromProtocol(3) //exclude or change!
          );
          break;
        } catch (e) {
          logError(e);
          amountOut = getBigNumber(0);
          break;
        }
      case 6:
        try {
          amountOut = await getPriceonCat(
            trade.path[i].address,
            trade.path[i + 1].address,
            amountOut,
            findRouterFromProtocol(6)
          );
          break;
        } catch (e) {
          logError(e);
          amountOut = getBigNumber(0);
          break;
        }
      // uniswap v2

      default:
        try {
          amountOut = await getPriceOnUniV2(
            trade.path[i].address,
            trade.path[i + 1].address,
            amountOut,
            findRouterFromProtocol(protocol)
          );
          break;
        } catch (e) {
          logError(e);
          amountOut = getBigNumber(0);
          break;
        }

    }
  }

That's the findRouterFromProtocol function:export const findRouterFromProtocol = (protocol: number) => { return uniswapRouter[Object.keys(uniswapRouter)[protocol]]; }

The protocols are read from the address file that was created here:

type RouterMap = { [protocol: string]: string };

export const uniswapRouter: RouterMap = {
  //these are all the routers!
  POLYGON_UNISWAP_V3: "0xE592427A0AEce92De3Edee1F18E0157C05861564",
  POLYGON_SUSHISWAP: "0x1b02dA8Cb0d097eB8D57A175b88c7D8b47997506",
  POLYGON_QUICKSWAP: "0xa5E0829CaCEd8fFDD4De3c43696c57F7D7A678ff",
  POLYGON_APESWAP: "0xC0788A3aD43d79aa53B09c2EaCc313A787d1d607",
  POLYGON_BALANCER_V2: "0x8E9aa87E45e92bad84D5F8DD1bff34Fb92637dE9",
  POLYGON_JETSWAP: "0x5C6EC38fb0e2609672BDf628B1fD605A523E5923",
  POLYGON_POLYCAT: "0x94930a328162957FF1dd48900aF67B5439336cBD",
  POLYGON_WAULTSWAP: "0x3a1D87f206D12415f5b0A33E786967680AAb4f6d",
};

I don't know what exactly I did wrong, cause the only routers it actually pings from the protocol are the UniswapV3 and Quickswap router. Even after commenting them out, it still only calls these routers.

Any ideas how to call these Dexes in a better way maybe?

LordReya commented 1 year ago

Solved it. I forgot to use yarn build again

tdev00 commented 1 year ago

is there a reason why you have multiple dexes in the findOpp function? The original code works as intended i.e. maps protocols to the uniswapV3 or uniswapV2. So for example, if UniswapV3 is protocol 0, it will be case 0, etc.