V-Cred: Enabling flash-loans in Avalanche
TL;DR This project is the first to bring flash-loans to Avalanche C-Chain. Avalanche has an innovative consensus mechanism, and is an incredibly lightweight protocol, so it was fun implementing flash-loans on it.
The code submitted for Encode hackathon: a working prototype is available here: https://github.com/vijayengineer/V-Cred-Avalanche. A video demonstrating the prototype: https://www.youtube.com/watch?v=6q6sB2VPbQI
Let us start by describing what is a flash loan:
Flash Loan, is an emerging service in the decentralised finance ecosystem which allows traders to request a non-collateral loan as long as the debt is repaid within a single transaction. If the transaction reverts fees are not paid, and just gas fees are incurred by borrower.
Avalanche C-Chain:
Avalanche features 3 built-in blockchains: Exchange Chain (X-Chain), Platform Chain (P-Chain), and Contract Chain (C-Chain). A detailed overview of Avalanche platform is provided here. A C-Chain is an instance of the EVM running on avalanche. A detailed description of their consensus algorithm is provided here.
Thus avalanche C-Chain provides an easy on-ramp to incorporate smart contracts and development tools for the DeFi developer. Let us examine the standard use cases for flash loans:
- Arbitrage between decentralised exchanges: Looking at differences in pricing or inefficiencies between exchanges
- Liquidation of crypto loans on lending platforms like Compound, dYdX, etc.: The main purpose is to prevent any liquidation fees
- Refinancing Crypto Loans or Maker Vaults: interest rate swaps to exploit better interest rates for refinancing.
In the case of avalanche the ability to transfer assets between C-Chain and X-Chain providing interesting use cases on financial primitives. Availability of potentially 1000x leverage in one transaction with the speed of avalanche network will provide disruptive use cases.
Scope:
To demonstrate the capability of flash loans, and to have the capability in the future to work across different chains a new token CRED will be used. This will act as a flash loan fee payment, and liquidity token enabling flash loans across different chains. As a first step the tokens and contracts will be deployed on Fuji testnet.
At this point in time, there is no Oracle implemented on C-Chain, however once the Ethereum Bridge is complete there is a lot of interesting possibilities. This limits the scope of the prototype, thus the prototype flash-loan will essentially be a template without performing the actual use case for example arbitrage.
Goals are:
- Deploy an ERC-20 token on Fuji testnet
- Develop and deploy smart contracts necessary for flash loan.
- Perform a flash loan which will repay the fees to demonstrate the concept.
Flash-loan implementation:
The basic logic of the Flash Loan smart contract is:
- Call a Lending Pool requesting some amount of ERC20 compatible assets
- Lending Pool transfers the requested amount
- User adds their logic to the smart contract
- The smart contract checks the final balance after execution. If the balance checks out the transaction are accepted and if not — reverted
For V-Cred prototype a a simpler and safer design pattern was adopted:
- Re-entrancy guard by having a separate lending pool i.e. deposit pool contract and borrower contract.
- Lender deposits on the deposit pool contract, and can only withdraw from lending pool contract
- Borrower checks borrow fee or rate, and then performs the flash borrow eliminating interest rate threat.
- Loan repayment is performed i.e. request is recorded before the transaction is sent out.
(This is however an MVP so please do not use this in production. )
MVP:
Cred token:
As discussed in the scope the first step involved deployment of an ERC-20 token Cred on Fuji testnet. This is fairly straightforward, using truffle and Openzeppelin tokenminter. Please follow this tutorial.
In my case since there was already a raspberry Pi connected to Fuji testnet, I could use it to deploy the contracts directly on test-net. About 1 million Cred tokens were minted with a majority going to a whale or liquidity provider address, and small amounts of tokens to addresses mimicking the real world.
Safer flash-loan:
- As outlined in the previous section the first component is a deposit pool contract. Liquidity providers will deposit their Cred tokens in this contract to earn risk free interest. This is as close to a definition of risk-free interest as it gets, since the loan must be repaid for the lending transaction to succeed. This is deployed on the fuji testnet on the C-chain at 0xa1Ba1E7dD7AD2C72A25650ceECaa617319D5Cffb
- The flash module contract is the second component where the hackers/traders will deposit their fees to perform leveraged trading. The flashloan contract is deployed on the Fuji testnet at: 0x19AB2715b478dc39A16A8CdCA0dD78304c3eE528
To start the process, as shown in the diagram below, the liquidity provider deposits about 250000 Cred into the deposit pool. To demonstrate this capability a 1000x leveraged borrow was done. So a trader deposited 250 Cred to take a 250000 Cred loan.
This transaction recorded on Fuji-testnet is to my knowledge the first flash loan transaction on Avalanche C-Chain: 0xd64a36f7b25956d5f6e45b6acab0d794de5cb5b41ff329f7c33dfda62ddc28e1
It was interesting to see the rapidity of the transaction, and is demonstrated here: https://www.youtube.com/watch?v=6q6sB2VPbQI
Next Steps:
With the launch of oracles, and Ethereum Bridge there are several avenues to take V-Cred further:
- Use the ability to do cross-chain transfers, and increased interoperability to perform arbitrage and reduce inefficiencies.
- Generate new synthetic assets, exchanging them for ERC-20 tokens like Cred to perform delegated credits using flash loans.
- Use flash-loans to short synthetic assets with stable-coins providing the necessary collateral.
Code for the prototype is available at: https://github.com/vijayengineer/V-Cred-Avalanche