Multiplier Flashloan Introduction ~Aave compatible~ Part.1
In the previous article we tested an arbitrage smart contract and a monitoring script for BSC(Binance Smart Chain) how we can consider DEXes and unleash the power of flash swap in Uniswap-forked DEX such as PancakeSwap and BakerySwap(ApeSwap). In here, I’d like to leverage Aave-compatible protocol to borrow initial funds from the pool and use it for flashloans to do our arbitrage strategy. A defect in the previous flashloan strategy was that prices do not deviate substantially from fair value for long periods of time across target exchanges. 
This implies that price deviations will not last long on those PancakeSwap and BakerySwap exchanges when we find an opportunity for rational actors who have set a similar script or system in the networks already. By using Aave(Multiplier in this article) you will be able to borrow funds from the pool directly not by invoking flashswap (actual swap operation under the hood) but simply taking a large amount of tokens to do a swap and reimburse the borrowed tokens back to the lending pool afterwards.
This article is not either an investment advice or a recommendation or solicitation to buy or sell any investment and should not be used in the evaluation of the merits of making any investment decision. It should not be relied upon for accounting, legal or tax advice or investment recommendations. The contents reflected herein are subject to change without being updated.
As introduced by Julien, there are some protocols that support flashloan such as Aave, dYdX and Uniswap in Ethereum network natively. These services have implemented unique Flashloan feature in each protocol and a bunch of Uniswap forked AMM protocols were released in 2020 and 2021. The same trend has been repeated in BSC(Binance Smart Chain) as well as Ethereum. As you might already notice PancakeSwap got a lot of tractions after its launch. You can check this article pros and cons between the protocols and how these flashloans differ. 
Comparison betwen Flashloan providers: Aave vs dYdX vs Uniswap
Julien Klepatch If you follow DeFi, you probably have heard of Flashloans. With Flashloans, you can borrow massive…
Aave is the service that originally came up with the concept of Flashloan. Multiplier is an Aave-forked project in BSC(Binance Smart Chain) and this would be only a choice for BSC users to copy lending pool flashloans from Ethereum to BSC currently. As Aave evolved and launched its V2, Multipler also released the forked version of Aave V2. There are 2 versions of the protocol Multipler V1 and V2 but I’ll use the older version V1 in this article. 
Introduction to Multiplier V2
Multiplier V2 is Non-Custodial Lending Protocol on Binance Smart Chain. Multiplier V2 is a non-custodial lending…
Why do I use the older version V1 instead of V2? There are some reasons I had to take account of.
- V2 has rich features like batch flashloans, but with its complexity I prefer to use V1 for simple flashloan to execute arbitrage strategy
- A sample flashloan demo is given with the V1 code base in github 
- Actually V1 has more liquidity compared to V2 (it is significant)
Flashloan fees are different between V1 and V2 in Multipler. V1 charges 0.06% while V2 charges you only 0.02% fee. But batch flashloan code in V2 gets more complicated and we can get puzzled by that feature. I prefer to use a simple V1 flashloan code and refer to the example that was provided by Multipler team in github.
Currently TVL in both V1 & V2 is around 2.3 M USD in total, but when I checked both dashboards of V1 and V2, the newer V2 has got only 500 K USD while V1 still holds more than 1.7 M USD in the pools. The example code is intended to borrow BNB from the one of the lending pools and do arbitrary tasks in it and reimburse.
The first application I wanted to develop was just to invoke a flashloan transaction from JS code and run an empty flashloan to borrow and pay the funds back to the pool in BNB. In this part.1 article, I’ll cover only this application on purpose. I’ll improve our code in the next article for considering actual arbitrage swaps between the DEX exchanges with the borrowed funds from Multipler. As flashloans are differen between protocols such as Aave, dYdX and Uniswap it’s better to understand how Aave renders flashloan feature briefly. We can refer to Aave V1 documentation. 
- Your contract calls Aave
LendingPoolcontract , requesting a Flashloan of a certain
LendingPooltransfers the requested
reserveto your contract, then calls
executeOperationfunction on your contract (or another contract that you specify as
- The contract holds the Flashloaned
amount, executed any arbitrary operation in its code. When your code has finished, the contract needs to transfer the Flashloaned
reserveback to the
LendingPoolcontract compared the balance of the
reservebefore and after the contract execution, ensuring that the balance of the
reserveis exactly the same as before plus the taken fee (0.06%)
- All of the above happens in 1 transaction (hence in a single block)
If something goes wrong borrowing funds or in the middle of process, this transaction fails and the entire transaction gets reverted as if nothing had happend. Your operation in
executeOperation function or the balance mismatch in
reserve could cause a transaction failure. Next let’s take a look at the tweaked version of the official example code of MLC-FlashloanDemo.
flashloanBnB function takes only
amount as a variable and invoke a flashloan in the lending pool for BNB and with the fund receiver set to this contract address itself. As described earlier, the lending pool lends
amount of BNB and calls
executeOperation function on
receiver address to get reimbursed on the contract address here. It is simple but we should complete a few tasks to run this flashloan example.
What should we do with this example ?
- A solidity contract should be deployed in a network to invoke flashloan
- Write a JS script to call this contract with the contract holding BNB
As explained in the previous article, running a deployment from Truffle does not work properly and an error occurred. Instead of Truffle I used Remix to compile and deploy a contract. Here’s a screenshot what I ported for Remix. Remix is an online IDE for Solidity language to write, compile and debug smart contract codes. It supports connecting your wallet and deploy an app in specific network and invoke functions of application from Web UI.
I deployed a smart contract from Remix and transfer 0.01 BNB to that smart contract. To reimburse the borrowed funds, the contract needs to have relevant fee on the contract address. Otherwise a flashloan call gets failed and a transaction will get reverted as a result. Let’s say we want to borrow 10 BNB ($5,300 at the current rate) from the lending pool of Multiplier V1 then the fee should be 10 BNB times 0.06%, so the fee price will be 0.006 BNB (around $3.2). Having 0.01 BNB in the smart contract address looks fine for testing.
# test smart contract address
Now let’s write a simple JS script to run your smart contract from your wallet. You can adjust gas and gasPrice if you want. Please note that the configuration values in this snippet might not be optimal.
I ran this script and got this transaction hash as a result. The deployed smart contract renders the empty flashloan without doing any arbitrary codes inside but just borrow and pay back the funds with the required fee.
$ npm run test
Transaction hash: 0x1775b0d58bc3b4f79a8848c1baf2bc246811787e33b06438f3ba4ab3fa1f61fe
If we confirm the transaction details in bscscan.com you will see the lending pool takes 0.06% fee and split the fee into 3 portions. Then it transfers 3 separated fees to 3 different addresses. It distributes the borrowed fee to the liquidity providers, the governance pool and the reserve pool as documented in the official page as below.
Here’s the repo I put all codes for your reference. In the next article, I’ll add arbitrage strategy in the flashloan contract and a monitoring script that can search an arbitrage opportunity. Rather than using flashswap in Uniswap-forked DEX to borrow initial funds, we can now use the lending pool (Aave, Multiplier) to get funds and execute our strategy with little fee.