Flash Loans on Anchor

I just implemented a PoC of flash loans last week. My work is a general implementation for any chain with CosmWasm support. I have designed the PoC to maximize safety of the lenders.

My solution includes:

  • borrowers
    • smart contracts executing, for example arbitrage
  • gateway
    • a single smart contract
    • has security mechanisms guaranteeing atomic transactions
    • is matching flash loan requests from borrowers with lenders available
    • charges relayer’s fee for providing atomic transaction execution layer
  • lenders
    • smart contracts providing liquidity to all requests coming through the gateway
    • have a way to perform the last action during a flash loan transaction and revert it altogether if somethings not right
    • charges liquidity provider fee (can be different based on the lender’s settings)

So far, the PoC allows using native coins (Luna + all Terra stable coins). However, the goal for the PoC was to prove the atomic transactions execution capability. After testing, I think this goal was reached.

Further idea is to also use IBC in later stages to allow for more arbitrage opportunities.

Let me know if you’re interested integrating this PoC on testnet with liquidity of the Anchor Protocol.


I think the community is definitely interested in this! Thank you for bringing it to attention. Could you describe for us more on how your implementation addresses potential flash loan security concerns (im not well versed in atomic transactions). Would love to see a testnet deployment, if you could provide a demo (video or guide otherwise) on the implementation to post here it will help the community visualize what flashloans are and how they can help the protocol.

If you can also show how they can be used to implement @bitn8 idea here then I think that would be what we need to raise community interest to get this going.

Hello @atebites.

Flashloans security

Security concerns that I’m aware of are:

  1. For lender: make sure you can stop transaction from being completed if the borrower didn’t pay the loan and interest back.
  2. For borrower: make sure you make some profit from the loan, as it’s not free to access the funds.

In both cases the actor has to have a way to revert. Borrower would do that if they made less than repayment (loan + fee). Lender has to have a final say, as it would check their balances right before the transaction is confirmed on chain.


  1. Smart contracts can contain bugs, letting the attacker to borrow funds and never return them back. Or even access the lender’s vault directly and withdraw the funds.

What flashloans are

I recommend reading Sneak peak at Flash Loans that was published back in 2020 in Aave Blog.

There’s also a great video by Finematics that is suitable for anyone, even if they never heard of flash loans before.

How flash loans can help the Anchor Protocol

Low-risk stream of revenue

First and foremost, provide additional, low-risk stream of revenue.

The low risk comes from using CosmWasm technology in a way, that give the Lenders a right to revert a flash loan transaction. The funds transferred during a flash loan leave the Lender’s wallet for a brief moment, and are repaid back with all the interest before the transaction is completed.

In short, any idle pool of crypto assets can be still used to make profit in a safe way.

Risk management

Flash loans can serve as a great tool to provide liquidation protection (@bitn8 mentioned that). Let’s see what the options are.


This is simply following the rule: only risk funds that you can afford to loose.

Let’s say a person has a debt on Anchor, and it turns out that it’s too large for them to feel comfortable. They would like to manage their risk levels by decreasing the size of the current Anchor debts.

The use case could well-served by risk management bots.

Liquidation protection

Let’s say market conditions are getting less and less favourable. It could lead to a cascade of debt liquidations. However, people could use a flash loan to close their near-liquidation positions. It would save them some funds that otherwise would be dedicated for an incentive to liquidators.


Arbitrage is quite demanding task, but if anyone has a capability to notice inefficiencies between markets, they can move some liquidity from one market to another and keep the change.


Flash loans can be a low risk revenue stream for liquidity providers. They can also enable users of lending platforms to manage their personal risk levels. Arbitrage is another use case, especially interesting when it comes to accessing other markets via IBC protocol.

1 Like

This sounds great. Let’s hold off on the prop though. I need to get Anchor internal team support and then stakeholder support. This will harder to pass than the bETH LTV.

Let’s just keep this one going. No need to make two.

We can get it BtBlock who is now auditing Anchor contracts every quarter. So this would fall in the q3 stack, or alternatively, there is a bit of ANC left over from the first two audits, we could get Vini from SCV to it. I would be a simple and quick audit that should not cost much.


Flash loans are a great thing, they could be a very valuable revenue stream, but I don’t think they are for anchor, they are more of Fields of Mars thing.

Risks are the same as regular loans, if the code is compromised assets are compromised.
Well, it’s hard to tell, regular users don’t use flash loans, only coders, and depending on uses cases currently, there are only a few:

  1. Arbitrage between exchanges
  2. Liquidations for the mirror, anchor, and other protocols
  3. Changing loans between money markets

Again it’s very hard to tell how much it will generate revenue for protocol, bcs it depends on people who are willing to code smart contracts that use them, and then on how many opportunities are there to use them.

Personally, I don’t think anchor should have flash loans, they are on the technical side, and that is Mars Protocol thing, I think anchor should be better focusing on a stable interest rate of 20%.

1 Like

To believe the only use cases for the flash loans are the ones you’re already aware of is a bit close minded, don’t you think? You presented some very valid use cases, there are others, @bitn8 has mentioned one use case that would be excelent for Anchor, closing down loans automatically by flash loan of the amount, selling collateral to pay back and give the user the remainder. It’s also one more revenue stream for Anchor to be able to achieve the target APY…

If we have the idle capital, why would we not do something with it?

Mars can become a competitor if they wish, but we should enter the game.


Of course there are many more ways of using flash loans, the example you said I said as 2, but these are current ways I see flash loans getting used.

From what people tell be point of anchor is to attract users to terra chain, most of new users would find it confusing.

1 Like

You bring up some good points. @tko had mentioned (before his post got removed for whatever reason) his implementation uses atomic transactions (I believe) to help reduce security concerns? but this tech is so new and it takes users a lot of learning to wrap their heads around it.

Even with the risks flash loans provide a competitive edge in terms of collecting interest on deposits. Not a lot of defi protocols have or support them (for the plethora of reasons, mainly security) that we’re discussing. And trad.fi could never offer it. Given that Anchor has literal billions of idle UST the double edged sword analogy really does present itself here.

We also have successful (AAVE) and unsuccessful (CREAM) implementations of flash loans we can study. We can probably figure out how many users and utilized usd is from flash loans on those protocols and weigh the cost benefit of the tool.

I think staging a demo on testnet, that users can interact with (particularly devs), and obviously the audits bring the idea to the community in a digestible fashion.

I am currently making a flash loan arbitrage bot, so I am in support of this tech, what I am saying is that anchor should be simple and clean, to attract people to terra chain, 1 click to deposit and get 20% APR, and people who understand crypto better put up collateral to borrow money. You borrow UST and use a few collateral options.

Then you have MARS, a money market, not a bank, with many collateral options, with many coins to borrow, EDGE Protocol has potential too, but MARS is more complete, with governance and Fields of Mars, so I am supporting new tech like flash loans, but I don’t think anchor should do it. I have to main reasons for it:

  1. As I said before, anchor should be simple and clean, so anyone can understand it and use it, it shouldn’t have complex interface and complex parts, it should be as simple as possible.
  2. Anchor shouldn’t risk with flash loans, if anchor get’s fucked bcs of flash loans whole chain goes to shit, that is why smaller protocols should do it.
  3. To outflow some of UST from anchor, explained bellow.

Currently there are 3 places where you can borrow on terra, anchor protocol, edge protocol and mars protocol, and they give APY on UST of 20%, 15%, and 1%, anchor protocol is giving this rate due to bail out by terra fundation, edge protocol is giving this naturaly, mostly due to aUST being able to used for collateral, which creates degen box, and Mars protocol gives this rate with limit on fileds of mars and only LUNA and UST, used for borrowing/lending. Edge protcol has 10M of UST while Mars has 140.04M, most of that UST borrowed is used on Fileds of Mars, which is contract to contract lending which is excaly for what flash loans are used, if these places didn’t exist people would most likley put that UST in anchor, which would drain reserve even faster. Edge protocol currently gives rate of 15%, with bumps to 25% for a day a week, while when Mars Reserve puts more collateral and removes caps on fileds of Mars would be able to give even better rate. If Mars gave rate of 10-20%, while being able to used as collateral people that understand crypto would most likley put their money in Mars Protocol to do strategies insted of anchor, this would help to distribute unused UST on anchor to smaller protocols.


My post is back now :slightly_smiling_face:

I’ll be testing my technology out anyway, regardless of the chats in here.

I understand that security is the most important thing. I won’t be pushing anyone to onboard the flash loans feature on their platform.

My goal was to make sure the Anchor Protocol community has visibility that someone is working on creating flash loans technology for CosmWasm.

Feel free to reach out if you, as a community, would like to leverage this piece of tech.

1 Like

I will DM you, I would love to leverage it, I am only saying that it Mars Protocol would probably be better place for this, most of their users understand crypto better than anchor users.

1 Like

This is a point I can relate with, someone flash loans borrows a few bills and breaks something. pretty large systemic risk and probably the main reason why people would not be on board with them. worse is like what happened to CREAM, that would 100% kill Anchor on the spot.

It’s definitely something that even though we can do it the systemic risk factors need to be throughly worked out. With that said the potential revenue they offer looks to be uncertain, and there are other protocols that can do it first for longer before Anchor takes it’s jab at it.

I like your analogy with mars protocol, they have them but they’re deployed with specified use cases intended. This is the same with white whale. It goes to show just having flash loans open for general use cases is not always the best of ideas. If we focused them for liquidation insurance only it can make it more of a borrowing feature and leaves the door open to future general use much further down the line when we have more evidence of their use in markets, while reducing the risks of malicious attacks.

I think eventually all money markets will come to offering similar features especially on the borrowing side since that is where income stems from. Look at what happened with concentrated liquidity pools, first curve than about every dex and their mothers.


We should be wary here. As @paletas mentions here there are some risks.

It’s a great idea to prevent recursive attacks against Anchor. However, the biggest one I see here is the ability for a flashloan to borrow a shit load of UST and use it to attack the UST peg and put things into a death spiral or snag the BTC reserve that is coming into place.

Right now, the inability to do a flashloan of UST on Ethereum is actually a blessing in disguise. There isn’t enough liquidity for UST on Ethereum. If there was you could borrow there, execute multiple transactions on faster chains and then revert.

With great power comes great responsibility. Flashloans really only used for arbs and exploits. Not real world usage (currently).

I think it should stay out of Anchor personally. Could potentially be combined with an aUST xaUST proposal and then another protocol can leverage the more programmable xaUST to do shit like this.


This is interesting - and an appropriate time to be having this conversation.

Seems like flashloans could contribute additional use cases and revenue to Anchor. When I think of flashloans, I think of apps such as Furocombo which allows users to pair multiple transactions.

It bridges the more technical side and allows folks to visualize the routing - leading to more sophisticated series of transaction and a willingness to use them.

Is there something similar for Terra? I wonder if it would be worth giving them a grant and investing in a tool which could spur adoption and have Anchor as a prominent choice

1 Like

being against flashloans because they may expose some risk doesn’t make sense since flash loans already exists in terra, so whatever risk you think might come with flash loans are already here. what flash loans don’t do is bring risk to the lender because its fairly easy to make sure you were repaid you entire amount atomically in the same tx, and anchor right now is just giving away market share to white whale

Don’t think there is something similar on terra yet. But I know a project that is looking to launch on terra that might have this in mind! I think I finally convinced them to build on Terra.


I think Mars Protocol is trying to build something like this, since they have whole contract to contract part

White whale has also just passed a proposal to open up flash loans from their vaults

See https://app.whitewhale.money/gov/poll/4

Flash loans would address multiple community suggestions with 1 shot:




I think there is very strong community incentives to work on this.

I don’t think its a good idea to enable flash loan in Anchor it’s just enabling more attack surfaces to exploit terra ecosystem. Currently the main use-case of flash loan in Ethereum is to exploit a protocol.