Possible reasons for "Not enough amount to return loan" error #36
Replies: 7 comments 12 replies
-
Well I was looking into 0x api some days ago, you can check that out. They also route their orders through multiple dexes and are available on polygon. They also have a front end using that api : matcha.xyz |
Beta Was this translation helpful? Give feedback.
-
Maybe furucombo.app might be some help here? AFAIK some people where using it for poly and eth Flash loan arbitrage trades Maybe sim investigation into their app might shed some light To add to proro's comments they also cover multiple chains, that sounds like a great feature to implement when the polygon bot is working correctly |
Beta Was this translation helpful? Give feedback.
-
Hi please take a look at this source, this bot is using 0x Api, maybe there is something in the source code that might help you snippet from githubThe author describes the basic operation of an arbitrage bot. For example, to arbitrage the pair WETH/DAI: The bot will query the 0x API looking for WETH/DAI pair limit orders The 0x API can get the limit orders for a currency pair from every exchange that uses the 0x protocol. Example API url for orders buying WETH with DAI: https://api.0x.org/sra/v3/orders?page=1&perPage=1000&makerAssetProxyId=0xf47261b0&takerAssetProxyId=0xf47261b0&makerAssetAddress=0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2&takerAssetAddress=0x6b175474e89094c44da98b954eedeac495271d0f Parameters the maker token’s contract address (WETH) the taker token’s contract address (DAI) Sample response from the above request |512x215 However, the above opportunity may not actually exist in practice, because we would need both limit orders to exist at the same time for the arbitrage to work. The author thus proposes to perform the arbitrage using a flash loan. Steps Get a flash loan DAI from DyDx exchange Buy WETH from 0x using the DAI borrowed with the flash loan Use 1inch to find the best exchange to sell the WETH acquired in step 2 Pay back the flash loan DAI and keep the remainder as profit The author provides the code and explains how the arbitrage smart contract works The contract inherits the DyDxFloashLoan smart contract. Functions Swap Perform the trade getExpectedReturn Get the current asset price getWeth Turn any ETH sent into WETH approveWeth Approve the 0x proxy to collect WETH as the fee for using 0x. getFlashloan Called whenever a profitable arbitrage is found by the client. All of the parameters for this function will be constructed and passed in from the client script. callFunction Has to be deployed in our smart contract to receive a flash loan from dYdX. _arb Arbitrage function that is called when the loan is successful. Tracks the balance of our smart contract before and after the trade. If the end balance is not greater than the start balance, the operation will revert. Method The prerequisite for the arbitrage bot is to have a browser with the Metamask extension installed. The programming language used is NodeJS. Project structure: only two files index.js a node.js server that continuously fetches crypto prices on exchanges looking for arbitrage opportunities, trying to guarantee that the trade is possible before even attempting to execute it. TradingBot.sol a smart contract that gets called by the node app only when a profitable arbitrage is found, it will borrow funds with a flash loan and execute trades on DEXes. Detailed setup Install Metamask browser extension Create an Ethereum Mainnet account with some ETH for paying gas fees. Don’t use your personal account for this, create a new account for the bot in order to limit accidental losses. Go to Remix online IDE and paste the smart contract solidity code Compile the code using compiler version 0.5.17. Deploy with an initial 100 wei, which is enough for 100 flash loans on dYdX. Environment setup By cloning the project’s code repository, users will find a file called .env.example inside the /src folder Fill in the fields: RPC_URL : the public address of an Ethereum node, the easiest one to set up is the Infura RPC provider, register an account in order to get an API key. ADDRESS and PRIVATE_KEY: fill in the public Ethereum address of the bot account, and its corresponding private key. CONTRACT_ADDRESS : paste in the smart contract’s address that was returned from Remix after the deployment step. GAS_LIMIT : how much gas the contract is allowed to use, leave as 3000000 or decrease to 2000000 which should be fine GAS_PRICE : change this depending on how fast you want the transaction to be mined, see https://ethgasstation.info/ for info. ESTIMATED_GAS : leave as is Running the bot Execute the command from the project’s root directory. node src/index.jshttps://github.com/ExtropyIO/defi-bot Also take a look here https://www.smartcontractresearch.org/t/research-summary-coding-a-defi-arbitrage-bot/282 |
Beta Was this translation helpful? Give feedback.
-
Hi guys, as I can see from Flashloan.sol contract should emit event SwapFinished just before compare , but I can't see that event posted, any comments?
|
Beta Was this translation helpful? Give feedback.
-
hello! great work. |
Beta Was this translation helpful? Give feedback.
-
i'm looking on it, but can't modify the code to call paraswap.io if someone could try ? I'm not a dev (but surely I will become one ^^ learning typescript since I found Yuichi repo) https://developers.paraswap.network/api/master Also since Yuichi is drinking pina colada with his hard-earned money, does someone forked and modified the bot to use uniswap ? |
Beta Was this translation helpful? Give feedback.
-
Hello guys, here another one for the team! @yuichiroaoki why not instead HTTPS use Websocket WS connection. This will increase speed a lot to execute faster trades.. You can use Moralis WS.. please add me to the private repo so I can help. Good Work! |
Beta Was this translation helpful? Give feedback.
-
"Not enough amount to return loan"
The transaction will be reverted when there's no opportunities and the contract cannot return the token.
https://github.com/yuichiroaoki/poly-flash/blob/03c5501aa00b3d6a4a134059a3f0397c24e9e12b/contracts/Flashloan.sol#L91
ref.
I think of some possible reasons for this.
1. Cannot tell how the 1inch trade entirely from their API
1inch api doesn't return what ratio each route takes when there are multiple routes.
sample query
https://api.1inch.exchange/v4.0/137/quote?fromTokenAddress=0x8f3cf7ad23cd3cadbd9735aff958023239c6a063&toTokenAddress=0x7ceb23fd6bc0add59e62ac25578270cff1b9f619&amount=1000000000000000000000000&mainRouteParts=50&protocols=POLYGON_SUSHISWAP,POLYGON_QUICKSWAP,POLYGON_APESWAP,POLYGON_JETSWAP,POLYGON_WAULTSWAP,POLYGON_UNISWAP_V3
result
You can tell each ratio from their web ui like the followings:
Also, 1inch doesn't have pool information on each swap. You can see they swap USDC for WMATIC on different uniswap V3 pools from the picture above. Since uniswap V3 has often 2 pools for the same pair like one is 0.05 % fee pool and the other is 0.3%, this kind of trades happens. However, 1inch API doesn't return anything about pools while again web ui does.
2. Exchange rate changes after you swap tokens with the same pool
When the bot tries to detect an opportunity between USDC and WMATIC for example, it gets price data of USDC->WMATIC and WMATIC->USDC with 1inch API. If the bot trades on the same pool on both directions, the price of that pool changes and the bot cannot trade as it expected. That is because prices on dexes are determined by the pool state.
Possible solutions
What do you guys think? One possible solution for 1 would be detecting prices besides 1inch API (#33).
Beta Was this translation helpful? Give feedback.
All reactions