Flash Loans on Anchor

Hey guys,

This is probably for the technical people in the crowd but what are peoples opinions on flash loans? They can definitely be a valuable revenue stream but have security risks. What are the risks? How much potential revenue could they provide? What kind of resources does Anchor have to contribute towards them? What could governance do to implement them?

It would take a while to implement but would also open up a lot of idle capital relatively efficiently with little to no risk (apart from the additional security risks).

I know we have some security professionals on the forums so it would be interesting to hear their take on flash loans as well.

3 Likes

I believe this will be needed in the medium to long term, to add another revenue stream to Anchor’s idle reserves, risks (if we ignore the obvious smart contract risks) could be divided into own protocol attacks and other protocols attacks.

If we do allow for flash loans, one of the easiest way to protect Anchor from a recursive attack would be to make sure that after a flash loan that address could not interact with any other Anchor smart contract within the same block, therefore kiling any avenue for recursiveness. There’s also the potential issue of the funds being used to drive prices down on collateral and being used to drive liquidations and purchase of the collateral at a discount, but with the 10 min activation window on bids it would require more than a flash loan to pull this off.

As for the risk of it being used to attack some other protocol, there’s nothing we could do imo.

I’m no expert but this is my current thoughts on the matter.

2 Likes

Yes. I am putting pressure to get this done q2 - no promises though. We need community support and votes.

I have been told by Devs that it won’t take long at all.

The main appeal in my mind is making a scrip that is easy for users to deploy to their wallet that provides liquidation protection. This could help drive more borrowing too if it was an option, solving one of the major pain points.

Correct, from what I have seen oracle risk is one of the main ones. So really having several oracles (pyth, band, link) and using a mean shorting function will help with manipulation and stale prices.

3 Likes

This could be highly appealing to novice borrowers. Vulnerabilities we expose to other protocols is not really our problem but could become something controversial with the community so it’s good to know there’s safeguards we can put in place. (We can test it on test net anyways from my understanding).

It seems like low hanging fruit in terms of additional revenue. I know they charge a small percentage fee for one taken out. There’s a wide gap between peoples education on them and their potential as they are primarily a developer tool. There is also that flashloan attack C.R.E.A.M had which destroyed their protocol (remarkably they’re still churning though but a fraction of what they were). So there’s going to be some push back.

From what I’ve seen on Anchor discord it looks like White Whale already does flash loans on Terra. I’m not sure if people are looking to offer a competitor offering but it does have value just being there.

Flash loans can provide a really useful tool for a plethora of different Dao’s and protocols to take advantage of Anchor deposits with relatively little risk and no need to negotiate anything between protocols themselves.

If this is running ontop of xAnchor such that any blockchain can pull a flash loan from Anchor natively that would be something else I imagine? Regardless I think we’re just waiting for a time for it to come into conversation where people are more aware of what they are and what do they do but it’s pretty hard to explain and advertise this kind of tool to the public.

1 Like

Flash loans done correctly would be a great value add to the protocol for both users and as an additional revenue source. Fully support this initiative.

2 Likes

Am I an only one who warry about flash loan attack on protocols?

2 Likes

You’re not the only one, as you can read it’s been mentioned before, but there are ways to allow for this without putting Anchor at risk, other protocols should be aware of their own vulnerabilities

1 Like

Flash loan attacks on other protocols will happen, and I consider that a good thing. It helps highlighting reckless implementations and once one protocols gets successfully attacked, that will motivate others fix their issues (such as incorrect reward computations). For now there is no incentive for protocols to think through their implementations, instead many just ship as fast as possible to profit from the market conditions.
The implementation of flash loans is rather trivial, that can be easily done in a day (even faster with copy pasta).

1 Like

Once I get bETH LTV up I’ll get a friend to help me make a pull request for flashloans on git and we’ll poll this next (assuming I don’t lose all my anchor from failing to meet quorum for the bETH one lol).

I agree with your stance on flashloan attack on other protocols, once it’s not in my back yard its not my problem. Apart from being easy to implement it would require an audit correct?

It’ll be a new forum post for the prop, I think depositors would be happy to see more revenue from it. Sure white whale’s got their own going on but that only further solidify’s the reason to have it on anchor.

How does audit work, we would pull and build the flash loans component, stage it on testnet then get it audited? Simultaneously we can poll a prop for their approval and that should wrap up the work.

1 Like

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.

2 Likes

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.

Risks

  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.

Self-hedging

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

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.

Summary

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.

2 Likes

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.

2 Likes

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.

2 Likes

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