yuichiroaoki / poly-flashloan-bot

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

INSUFFICIENT_OUTPUT_AMOUNT error #9

Open yuichiroaoki opened 2 years ago

yuichiroaoki commented 2 years ago

I don't know how this error happened, but some people got this error when they change the config.

trancee commented 2 years ago

Do you know what config was changed? Because I am not sure about that. But what I did is to make sure the arbitrage opportunity is higher before executing the transaction. I guess that the opportunity is changing quite quickly and while the transaction is running, the opportunity was already lost because it was too slow to execute.

yuichiroaoki commented 2 years ago

I thought this error happened when you fail to borrow a loan token from dodo.

it("should be reverted with `INSUFFICIENT_INPUT_AMOUNT` when the bot set a wrong dodo pool and the contract can't borrow tokens.", async () => {
    await expect(
        Flashloan.dodoFlashLoan({
            flashLoanPool: dodoV2Pool.WMATIC_USDT,
            loanAmount: getBigNumber(1, 6),
            firstRoutes: [{
                path: [erc20Address.USDC, erc20Address.DAI],
                router: uniswapRouter.quickswap,
            }],
            secondRoutes: [{
                path: [erc20Address.DAI, erc20Address.USDC],
                router: uniswapRouter.quickswap
            }],
        }, { gasLimit: 1000000 })
    ).to.be.revertedWith("UniswapV2Library: INSUFFICIENT_INPUT_AMOUNT");
});

https://github.com/yuichiroaoki/poly-flash/blob/6d29dac77bedf2871eda23ab875b9f4843a993ab/test/errorMsg.test.ts#L37-L53

In this scenario, the contract will try to borrow USDC, but it can't borrow USDC from dodoV2Pool.WMATIC_USDT. The contract checks if the loan token is a base token of the dodo pool, and if not it borrow a quote token of the pool, so the contract would borrow USDT (quote token of dodoV2Pool.WMATIC_USDT).

I should have checked if the quote token is the loan token as well.

https://github.com/yuichiroaoki/poly-flash/blob/6d29dac77bedf2871eda23ab875b9f4843a993ab/contracts/Flashloan.sol#L51-L66

But it could be a different reason, I'm just guessing.

I will update the contract to check the dodo pool before executing flashloan and to make it clear.

ref. https://docs.uniswap.org/protocol/concepts/V3-overview/swaps#safety-checks https://ethereum.stackexchange.com/questions/90241/why-insufficient-output-amount

trancee commented 2 years ago

Thank you for looking into this issue. What you have tested is the INPUT amount, but my issue was about the OUTPUT amount. Can you make sure both are tested?

yuichiroaoki commented 2 years ago

Sure. I opened an issue on that.

https://github.com/yuichiroaoki/poly-flash/issues/4