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:

Simple flash-loan description

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

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:

  1. Deploy an ERC-20 token on Fuji testnet

Flash-loan implementation:

The basic logic of the Flash Loan smart contract is:

  1. Call a Lending Pool requesting some amount of ERC20 compatible assets

For V-Cred prototype a a simpler and safer design pattern was adopted:

  1. Re-entrancy guard by having a separate lending pool i.e. deposit pool contract and borrower contract.

(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.

V-Cred token deployment

Safer flash-loan:

  1. 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

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.

C-Chain flash 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:

  1. Use the ability to do cross-chain transfers, and increased interoperability to perform arbitrage and reduce inefficiencies.

Code for the prototype is available at: https://github.com/vijayengineer/V-Cred-Avalanche

Founder | V-Cred Full stack Developer: DeFi, ML

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store