Tutorials – Finematics https://finematics.com decentralized finance education Mon, 12 Oct 2020 14:47:09 +0000 en-GB hourly 1 https://wordpress.org/?v=5.8.1 https://finematics.com/wp-content/uploads/2017/09/cropped-favicon-32x32.png Tutorials – Finematics https://finematics.com 32 32 How To Use Furucombo https://finematics.com/how-to-use-furucombo/?utm_source=rss&utm_medium=rss&utm_campaign=how-to-use-furucombo&utm_source=rss&utm_medium=rss&utm_campaign=how-to-use-furucombo https://finematics.com/how-to-use-furucombo/#respond Mon, 12 Oct 2020 14:38:47 +0000 https://finematics.com/?p=1007 So have you always wanted to try using some of the more advanced DeFi constructs such as flash loans but didn’t want to get too deep into coding? If the answer is yes, Furucombo may be an ideal tool for you. 

Let’s start with what Furucombo actually is. 

Furucombo

Furucombo is a tool that allows you to construct an Ethereum transaction using a simple drag-and-drop user interface. It can visualise complex DeFi transaction by showing a chain of interactions displayed as individual “cubes”. Each cube represents one specific interaction, for example, taking a flash loan or swapping coins on Uniswap. 

How To Take A Flash Loan

Okay, now let’s try to take a flash loan in Furucombo. If you want to learn more about flash loans you can check out our other article here.

  1. Go to https://furucombo.app/combo
  2. Click on the cube with the + sign to add a new cube
  3. Search for Aave and Flashloan 
  4. Select a token that you want to borrow and the borrowed amount 
  5. Click the Set button
  6. You’ll see 2 cubes added to the combo. Flashloan cube is a special case as every flash loan has to be repaid within the same Ethereum transaction. Because of that Furucombo creates 2 instead of 1 cube for you
  7. Now, you’re ready to add some arbitrary steps between the 2 flash loan cubes
  8. In our example, after we borrow 1,000 DAI from Aave via a flash loan we swap borrowed DAI for USDC on Uniswap and swap USDC back to DAI on Curve. 
  9. At the end of all the steps, we have to repay the initial loan + 0.09% fee
  10. Before you’ll able to execute your transaction you have to connect your wallet such as Metamask in the last step of your combo 

  11. After the wallet is connected you can Approve and execute your transaction

In our example, depending on the current price of USDC/DAI pairs on Curve and Uniswap, you’ll most likely end up with having to pay more than the amount that was borrowed. 

This is, of course, because there is usually no arbitrage opportunity in USDC/DAI between these 2 most popular decentralized exchanges. On top of that, you’ll be paying extra 0.09% of the borrowed amount in fees for being able to take a flash loan on Aave.

Next Steps 

After you make yourself familiar with the Furucombo interface you can try adding more cubes to the example. 

You can, for example, borrow funds from Compound, create a new vault in MakerDAO or add liquidity to a liquidity pool on Uniswap. 

Furucombo is an ideal tool for people who want to be able to create more complex Ethereum transactions. One thing that we have to remember is that you’ll be always limited to what combos you can create by the available cubes. For example, currently, there is no way to use some of the less popular DeFi protocols such as SushiSwap or Cream. 

If you’re ready to dive a little bit deeper and start writing your own smart contracts you can check out our guide on how to code your own flash loan with Aave here.

If you enjoyed this tutorial you can also check out Finematics on Youtube and Twitter.

]]>
https://finematics.com/how-to-use-furucombo/feed/ 0
How To Code A Flash Loan With Aave https://finematics.com/how-to-code-a-flash-loan-with-aave/?utm_source=rss&utm_medium=rss&utm_campaign=how-to-code-a-flash-loan-with-aave&utm_source=rss&utm_medium=rss&utm_campaign=how-to-code-a-flash-loan-with-aave https://finematics.com/how-to-code-a-flash-loan-with-aave/#respond Mon, 12 Oct 2020 14:30:27 +0000 https://finematics.com/?p=1022 So how can you code a flash loan with a few easy steps? And how can you interact with Aave’s smart contracts? You’ll find answers to these questions in this tutorial. 

What are Flash Loans? 

A flash loan is a feature that allows you to borrow any available amount of assets from a designated smart contract pool with no collateral. Flash loans are useful building blocks in DeFi as they can be used for things like arbitrage, swapping collateral and self-liquidation.

Flash loans, although initially introduced by the Marble protocol, were popularised by Aave and Dydx. 

So, what’s the catch? 

A flash loan has to be borrowed and repaid within the same blockchain transaction. 

If you’d like to learn more about flash loans check out our article on flash loans here

Before we start 

This guide requires a basic understanding of Solidity smart contracts and how to use Remix. You can find Remix documentation here

It also requires having your Metamask wallet installed and switched to the Kovan testnet. 

To execute any smart contracts on Ethereum you’ll also have to have some ETH in your wallet. You can get some Kovan testnet ETH by going to faucet.kovan.network and logging in with your Github account. You can request 1 testnet ETH once per day. 

We’ll also need some DAI. To request DAI go to testnet.aave.com/faucet and click on DAI, than hit the “Submit” button. 

Okay, the last step is to set up your Remix environment. Go to remix.ethereum.org and select 0.6.6+ version of the Solidity compiler. 

Flash Loan Smart Contract

Great, time to start working on our flash loan. Let’s start with creating a basic smart contract file. 

Here is the first piece of code we need.

pragma solidity ^0.6.6;

import "https://github.com/aave/flashloan-box/blob/Remix/contracts/aave/FlashLoanReceiverBase.sol";
import "https://github.com/aave/flashloan-box/blob/Remix/contracts/aave/ILendingPoolAddressesProvider.sol";
import "https://github.com/aave/flashloan-box/blob/Remix/contracts/aave/ILendingPool.sol";

contract Flashloan is FlashLoanReceiverBase {
    constructor(address _addressProvider) FlashLoanReceiverBase(_addressProvider) public {}
}

This will import all the necessary dependencies and create a basic FlashLoan.sol smart contract that inherits from FlashLoanReceiverBase which is an abstract contract that provides a few useful things such as a way of repaying the flash loan.

The Flashloan.sol constructor accepts the address of one of the Aave’s lending pool providers. We’ll get to this later.

You can now hit the compile button. 

This should download all the relevant dependencies and result in an error as we haven’t implemented all of the necessary methods from the FlashLoanReceiverBase just yet.

Time to add 2 missing functions. First one is our actual flashLoan function that will be used to trigger a flash loan. The other one is the missing method from FlashLoanReceiverBaseexecuteOperation that will be called after the flash loan method is triggered. 

Flash Loan Function

Let’s add the flashLoan function first. 

Here is the code snippet. 

function flashloan(address _asset) public onlyOwner {
        bytes memory data = "";
        uint amount = 1000000000000000000;

        ILendingPool lendingPool = ILendingPool(addressesProvider.getLendingPool());
        lendingPool.flashLoan(address(this), _asset, amount, data);
    }

Time to make sure we understand all the relevant parts. 

The flashLoan function parameter _asset is an address of an asset that we want to borrow using a flash loan, for example, ETH or DAI.

We define our function as onlyOwner, so only the owner of the contract can call the function.

uint amount = 1000000000000000000;

Here, we’re defining the borrowed amount in the minor unit – wei that is 10^18, so this value is equal to 1. If we pass the DAI address as _asset we’ll be borrowing 1 DAI, if we pass the ETH address we’ll be borrowing 1 ETH.

Now we can use a ILendingPool interface provided by Aave and call flashLoan function with all the required parameters such as the asset that we want to borrow, the amount of that asset and an extra data parameter.

Even after adding our flashLoan function, the code will still not compile as we have to add the missing executeOperation function.

executeOperation Function

Time to implement the last missing bit before we can try triggering our flash loan function.

executeOperation function will be called by the LendingPool contract after a valid amount of asset is requested in a flash loan.

Here is a code snippet for the executeOperation function.

function executeOperation(
        address _reserve,
        uint256 _amount,
        uint256 _fee,
        bytes calldata _params
    )
        external
        override
    {
        require(_amount <= getBalanceInternal(address(this), _reserve), "Invalid balance, was the flashLoan successful?");

        // Your logic goes here.

        uint totalDebt = _amount.add(_fee);
        transferFundsBackToPoolInternal(_reserve, totalDebt);
    }

All the parameters for the executeOperation function will be passed automatically after a valid flash loan is triggered using the flashLoan function.

The require check is used to validate that we received a correct amount from a flash loan.

Next, we can insert any arbitrary logic that we want to execute. At this step we have all the funds from a flash loan available, so we can, for example, try to execute an arbitrage opportunity.

After we made use of a flash loan, it’s time to repay it.

uint totalDebt = _amount.add(_fee);

Here, we are calculating how much we have to repay which is the borrowed amount + 0.09% of the borrowed amount.

The last step is to call transferFundsBackToPoolInternal to pay back our flash loan.

Now, we can try to compile our code again. This time everything should compile just fine.

Time to trigger our flashLoan function.

Running The Code

Great, our flash loan smart contract is compiled and ready to be deployed.

Let’s start with preparing 2 necessary things:

  • LendingPoolAddressesProvider – to deploy our smart contract we need to find the address of the Aave’s lending smart contract on the Kovan testnet. You can find all the addresses here. The Kovan address is 0x506B0B2CF20FAA8f38a4E2B524EE43e1f4458Cc5
  • DAI address – we need a contract address of DAI (or any other asset you want to borrow) on the Kovan testnet. The DAI Kovan address is 0xFf795577d9AC8bD7D90Ee22b6C1703490b6512FD

Time to deploy our smart contract.

  1. Go to “Deploy & Run Transactions” on Remix
  2. Select “Injected Web3” (Make sure your Metamask is switched to Kovan)
  3. Select a contract to be deployed – FlashLoan.sol
  4. Pass the LendingPoolAddressesProvider that we found a few steps earlier to the field next to the “Deploy” button
  5. Hit “Deploy” button
  6. Confirm your Ethereum transaction via Metamask

If everything went ok – great! We now have our FlashLoan.sol smart contract deployed on the Ethereum testnet – Kovan.

Here is the crucial step in running our smart contract. We have to send some DAI into the smart contract we just created to be able to pay for the flash loan fee.

You can find your smart contract address by checking the Metamask transaction that you previously approved. The transaction for creating a smart contract should look similar to this one here. From that transaction, you can extract the smart contract address (0x27016b23BEE0553A4aAa89b25Be58b93Fe647BBe in our case).

Time to trigger our flashLoan function from the deployed contract. Remember about passing a correct address of the asset you want to borrow. In our case it’s DAI address on the Kovan testnet.

After triggering the flashLoan function and accepting an Ethereum transaction via Metamask, you should see a similar transaction to this one here.

Congrats! You just made your first flash loan!

If you enjoyed this tutorial you can also check out Finematics on Youtube and Twitter.

]]>
https://finematics.com/how-to-code-a-flash-loan-with-aave/feed/ 0