Of Compound, Recursion, and Mythical Creatures

Today, I’d like to share with you a myth that I’ve recently debunked:

“Interacting with DeFi protocols directly via their smart contracts is hard. You need to be considerably familiar with the protocol in question.”

Turns out this is completely false. And overcoming this myth is quite powerful indeed. I’m not going to tell you why. I’m just gonna show you.

But before we do that, take a look at this.

An Eagleopard.

Which will totallly make sense soon.


A Quick Experiment

Let’s head over to Compound. Once there, switch your Metamask extension to the Rinkeby network. You should see this:

If you see red stuff, then you need to find some Rinkeby ETH.

Now, let’s supply some DAI to Compound. Click on “Dai” on the left side, and then on “enable”. You should see a Metamask pop up asking you to give Compound allowance for your DAI (don’t worry if you don’t have any yet). Before you sign the transaction, just make sure that you’re on Rinkeby. Better be safe than sorry!

Now, we need some Rinkeby DAI. Fortunately, once the tx above succeeds, you should see a little “faucet” button. Click on it and approve that tx as well.

Your Metamask wallet should now show 100 DAI in its balance.

Cool! Now, in the supply pop up for DAI (click on the “Dai” market again if it closed), you should be able to click on “MAX” and see your DAI balance of 100 staged for supply. Go ahead and click on the “Supply” button and approve the tx.

Once the tx succeeds, close the pop up and go back to the Compound dashboard. You should see this:

The UI changed, now showing you that you’re supplying 100 DAI. A less obvious change is that DAI no longer shows up on the right side! Hmm… that’s strange.

Actually, it makes sense that once you lend DAI, you can’t borrow DAI back. Otherwise we could just deposit 100 DAI, borrow 75 DAI, then deposit the 75 DAI again, borrow more DAI, ad-infinitum… right?

If you go to Compound’s docs you should be able to find a “Network” section that lists the addresses of all the deployed contracts of the protocol (if only everyone did this <3).

cDAI’s address in Rinkeby is 0x6d7f0754ffeb405d23c51ce938289d4835be3b14.

You can now go to Etherscan in Rinkeby, and search for this address. In the “Contract” tab, you’ll be able to see the token’s code. The contract’s name is “CErc20”. You should also see the “Read Contract” and “Write Contract” buttons. Let’s go ahead and click on the latter and write to the cDAI contract. Once in that page, click on the “Connect web3” link, just below these buttons.

Now, search in the page for “borrowAmount” and in that box enter the number 1 (yes, just 1) in the field, click write, and sign the tx. We may have just lent 1 weiDAI to Compound with the same address that we used to lend 100 DAI.

Once this tx succeeds, go back to Compound…

Voila!

See how we’re also borrowing DAI now? Turns out that we can lend AND borrow DAI at the same time. Sneaky UI you!

Go ahead and lend/borrow a few more times. Get greedy. You can do everything from Compound now (no need to return to Etherscan). Oddly enough, our initial 100 DAI, starts becoming somehow “inflated”.


How far could we take this?

=> Deposit 100 DAI
Supplied: 100 DAI, Borrowed: 0 DAI

=> Borrow 75% of that, 75 DAI
Supplied: 100 DAI, Borrowed: 75 DAI

=> Deposit the borrowed 75 DAI
Supplied: 175 DAI, Borrowed: 75 DAI

=> Borrow 75% of that, 56 DAI
Supplied: 175 DAI, Borrowed: 131 DAI

=> Deposit the 56 DAI
Supplied: 231 DAI, Borrowed: 131 DAI

=> Borrow 75% of that, 42 DAI
Supplied: 231 DAI, Borrowed: 173 DAI

=> Etc…

So, in each iteration, you’d be borrowing 75% of the added collateral, and recycling it into your supply. I’m no mathematician, but I can code, so I spun up this little script to see where this would end up. The maths could be totally wrong, but I don’t care, it’s just to get the gist of it:

let supply = 0
let borrow = 0

let count = 0

function iterate(deltaSupply) {
  supply += deltaSupply

  let deltaBorrow = 0.75 * deltaSupply
  borrow += deltaBorrow

  count++

  console.log(supply, borrow)

  if (count < 100) {
    iterate(deltaBorrow)
  }
}

iterate(100)

The first few results look like this:

100 75
175 131.25
231.25 173.4375
273.4375 205.078125
305.078125 228.80859375
328.80859375 246.6064453125
346.6064453125 259.954833984375
359.954833984375 269.96612548828125
369.96612548828125 277.47459411621094
377.47459411621094 283.1059455871582
...
399.9999999998716 299.99999999990365

There is a clear tendency towards 400 DAI being supplied, and 300 DAI being borrowed. That’s 4x for lending and 3x for borrowing!

As Ryan from Bankless points out in his article Yield Farming Games (totally recommended read), this sort of “recycling” is not only possible, but actually very common.

But.. why in the world would anyone ever do this? Well, due to the recent COMP incentives, borrowing is subsidized in such a way that it can actually produce positive yields. Lending and borrowing produces a higher net APY than just lending once you consider COMP rewards, and doing so recursively can result in pretty extreme leverage. Hell, Instadapp’s Maximize COMP Mining lets you do all this by just pressing a button. How? Let Defi Dad show you.

Now, such not-so-natural incentives inevitably produce rather bizarre outcomes.

As Ryan puts it:

“DAI recycling is now so popular that the amount supplied to Compound is triple the amount of total DAI in existence…”

That’s right.

DAI locked in Compound = 3 times all real DAI

Take a moment, and let that sink in..

WTF right?


Strange Creatures Lurking in the Chain

I’m a complete newb to finance, but as I learn along with DeFi, I’m beginning to get the impression that the financial universe-narrative thing, be it in Ethereum or in meatspace, is not made up of the regular farm animals we learnt about in school, but creatures rather more exotic, like our little Eagleoapard back there (hey kitty kitty). And I’m sure that reality easily surpasses our imaginations in this game quite easily!

Let’s see what other creatures may be lurking on chain.

A Tigerphant.

An adult, male Buttermouse.

What seems to be a Humming Pegasoflamingo… Deerish-ont?

I don’t know what the fuck that is.

Anyways, our order of business today is not mythical zoology, but the fact that we, as developers, know how to interact with contracts, and so we should. What we shouldn’t do, is make assumptions about a protocol just because its UI says so.

My advice is that you make a habit of interacting with your favorite protocols’ contracts. You won’t regret it. First, you need to find the contract addresses. A bit of googling should take care of that. Then tinker with the contracts in Etherscan.

Something that I’ve found extremely useful lately, and of which I hope to write about soon, is Money Legos. It’s not only awesome to code stuff with this, but the documentation’s examples are soooo good, that I often end up there to get a feel of how to interact with a particular protocol.

Check it out. Take a look at Money Lego’s examples for Compound.

Enjoy, but beware of the strange things that you may encounter along the way in the DeFi bestiary!