White Whale Releases Details on Flash Loan Architecture

White Whale
4 min readNov 20, 2021

--

Let’s break this down. A flash loan is, as the name implies, a loan that is opened and closed in a very short amount of time. More specifically, it is opened and closed in the same transaction. Flash loans are used to execute atomic trades that either capitalize off of inefficiencies in the market, or provide some other functionality to the borrower. Atomic trades are trades that can be finalized in one transaction and almost all on-chain arbitrage falls under this category. By now you hopefully realize how powerful this financial instrument can be. But with this power comes responsibility.

While White Whale will presumably be the first to roll out flash loans on Terra (and the whole Cosmos!), a number of protocols already offer it on Ethereum. The sometimes bad connotation linked to flash loans is caused by a number of exploits that happened on Ethereum in which hackers used this tool to do so-called re-entrancy attacks. Luckily for us, CosmWasm (the smart contract language of Terra) is designed to avoid this kind of attack. When comparing CosmWasm to Ethereum’s smart contract language the developers state:

“A big difference is that we avoid all reentrancy attacks by design. This point deserves an article by itself, but in short a large class of exploits in Ethereum is based on this trick .”

And,

“Cosmwasm avoids this completely by preventing any contract from calling another one directly.”

These precautions enable us to provide this service without having to worry too much about other protocols being exploited using our tools.

To really understand how all this works I’ll briefly unpack our flagship vault. The UST vault allows users to deposit UST into the vault in a simple, one step process. The total liquidity in the UST vault then acts as a general purpose liquidity pool with a series of in-house arbitrage strategies. The first of which is all about keeping the peg. But there are also other strategies in the works, such as exploiting price inefficiencies among multiple exchanges, as well as automated liquidations on Mars and Levana. All of these strategies make the ecosystem more stable and efficient, and they all use the liquidity of the UST vault.

These strategies bring us back to flash loans: when any of our bots detect a profitable arbitrage opportunity, the smart contract linked to that strategy will ask for a flash loan from the UST vault. The UST vault will then withdraw the UST from Anchor protocol (where it is yielding a comfy 19.5% while idle) and provide the contract with borrowed money to execute the arbitrage. After the arbitrage all funds are returned to the vault. This is how we plan to provide Anchor+ yields.

But what if the trade didn’t make a profit? Or what if the borrower just doesn’t repay the loan!?
Yeah, we thought about that too.

To ensure our depositors don’t get rugged we added a mechanism that adds two steps to this whole process. First, when a flash loan is requested, we save the total value of the vault. We then send the funds out to the borrower (i.e. the arb bot) and let them do whatever with it (i.e. execute an arb trade). Only there’s one important catch. We added a callback at the end of the program which can not be altered and is guaranteed to execute.

After whatever the borrower wanted to do, that callback is executed. The callback is essentially step two. It re-calculates the value of the vault and compares it with the initial value before the loan. If this amount is smaller than the initial amount it throws an ERROR and reverts the whole transaction, aka the flash loan. The transaction then fails and its like nothing ever happened. Effectively this means that the flash loan will only execute if it pre-determines a profitable result, otherwise it will cancel itself.

When you begin to think about the possibilities of how this architecture can be used in the future it gets quite exciting. At first the whitelisted strategies being utilized by the vaults will be those developed by us, the White Whale team. However, it would be a shame to keep this incredibly powerful infrastructure to ourselves. Instead, we intend to open it up and offer flash loans as a service.

Eventually, there will be a whitelisting process for community built bots to onboard through on-chain governance and utilize our flash loan architecture. The White Whale community will be able decide which bots will benefit and secure the ecosystem and vote to whitelist them. Of course we will take a small fee (the borrowing cost of the loan) which will automatically get distributed to our depositors. That’s right, you heard correctly, for a fraction of a second and a small fee, White Whale will enable you to become a true whale!

In order to ensure security for this complex and innovative architecture, we have already scheduled multiple audits with some of the most highly respected auditors in the industry. The foundation has already been built and we’re excited to get these tools into the hands of our community, empowering them to protect the peg and stabilize the ecosystem.

More details will be released soon. Our CTO (@TheCyberHoward) will be doing an interview to explain further and answer technical questions regarding the architecture on the Danku_r YouTube channel this week. (https://www.youtube.com/c/dankur)

--

--