In traditional finance, a borrower must deposit collateral and pass credit checks before receiving a loan. However, with Defi, borrowers can access loans without providing collateral. This lending option is called a flash loan.
A flash loan is an uncollateralized, short-term loan available in the DeFi space. Flash loans are usually high-risk activities that allow users to borrow crypto with no upfront collateral. These loans are executed using smart contracts and are available for a very short time.
If you what to learn more about flashloan read this article learn-how-to-perform-flashloan-using-aave
So, without further a do, let us begin.
1.1. We are going to develop a simple smart contract that will borrow USDC
from the Aave Protocol and then repay it along with the premium required (that is to say the commission).
1.2. Following is the sample contract that we will be going through (available over here đ GitHub Link):
1.3. Diving into the code:
createFlashLoan()
function:
USDC
(0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174)address(this)
since we will want the borrowed funds to be sent to the address of the contract that we will just deploy and then repay our loan from the same contractaddress(this)
for further execution. In a complicated transaction, this data is passed as a parameter to the createFlashLoan
functionflashLoanSimple
function with the receiver address, asset address, amount, params, and referral code as the parameters.executeOperation()
function:
Now as per the âsetâ practices, you would be expected to deploy the above contract on either a local blockchain node (eg: Hardhat or Truffle) or a public Testnet like Goreli Testnet. While that is okay, except for the following pain points:
In relation to the Goreli Testnet:
In relation to the local node:
localhost
for a reason! So forget about it. đ2.1. That is where BuildBear comes in. It kind-a marries the benefits of both the world for you:
Have a look over here: Where Localhost Fails and Win Web3 Hackathons, using BuildBear Testnetâs analytics
2.2. Visit the BuildBear App. Once you login with your Github or Google Account, you will see a page similar to the image added below:
BuildBear dashboard
2.3. Create your private node forking from the Polygon Mainnet: The reason to choose Polygon Mainnet is that AaveV3 (at the time of writing this article) is not deployed on the Ethereum Mainnet.
As per the latest Governance vote by the Aave Community, AaveV3 will be deployed on the Ethereum Mainnet as well
2.4. Add your private testnet to your MetaMask wallet by using the âAdd to Metamaskâ button:
2.5. Mint your Native and USDC from the Faucet (using the Faucet URL):
2.5.1. Minting the Tokens
3.1. Navigate to the Remix Online IDE website and accept the terms and conditions.
Remix
3.2. Create a new contract and paste the Smart Contract Code, available over here đ GitHub Link
3.3. Compile the Smart Contract, with Solidity Version 0.8.10, by using the following icon and details:
Please note that the ABI of the Contract is available only once the Contract is compiled. You can see in the above image the ABI which can be copied. Please keep this handy. We will need it momentarily.
3.4. Select the âDeploy and Run Transactionsâ tab: and deploy the contract as follows:
0xa97684ead0e402dC232d5A977953DF7ECBaB3CDb
3.5. Click on the âtransactâ button to deploy the contract to your private node network; once done, you will see something similar to the following:
3.6. Copy the address of the Contract and visit the Blockchain Explorer for your personal forked testnet (link available on the dashboard page at home.buildbear.io) and locate your contract.
3.6.1. Submitting the ABI of the contract for ease of interaction
You can visit your contract page on BuildBearâs Blockchain Explorer and then visit the Contract tab. You should see something similar to the following:
Submit the ABI that we copied from Step 3.3; once done, you should see the Read and Write Contract buttons made available to you on the Contract Page:
6.1. Visit the Write Contract
tab and connect your wallet:
6.2. You MUST ONLY call the createFlashLoan
6.3. Enter the token address you want to borrow (make sure that this token is listed on Aave) and the amount you want to borrow in the âArgumentsâ field of createFlashLoan. Since we minted USDC from the Faucet, we will use the USDC Address and borrow 10,000,000.000000 [Ten Million] (Please note, USDC has 6 decimals and hence be mindful of the number of Zeros)
6.3. Upon clicking the button, MetaMask should appear, prompting you to approve the transaction. After confirming the transaction, you should see a success message. To verify the transaction on the blockchain, go to your Buildbear dashboard and click on âOpen Explorer.â From there, click on âShow all transactionsâ and you should see your transaction listed.
We were successfully able to write the smart contract for a Flash Loan! We were able to borrow USDC from the pool, pay the Flash Loan fee, and then repay the borrowed amount all in a single transaction. You just borrowed money with no collateral!
This only took around 15 mins
If you appreciate what we are doing, please follow us on Twitter, LinkedIn and Join the Telegram group if you havenât done yet.
And please give us a clap đ if you like our work.