Token Bonding Curves, the Movie

How to make a DEX using a single contract.

Token bonding curves (TBCs) made their debut in 2018 and immediately began inspiring blockchain developers to move away from creating monolithic smart contracts with multiple external dependencies to building self-contained self-referential building blocks. TBCs are token pricing contracts that don’t rely on external oracles or order books. Instead their pricing is algorithmically controlled. When first introduced, I suspected they’d make a nice alternative to ICOs since they align the incentives of buyer and developer more carefully but since then it became clear that in addition to providing self referential pricing, they provide their own liquidity. And as Uniswap and Kyber Network have demonstrated, contracts that bring their own liquidity are the foundation of decentralized exchange in DeFi. In this article we’ll build a DEX from scratch using token bonding curves. Then we’ll end by introducing a real decentralized token exchange powered by token bonding curves that is live on main net, the Behodler.

A token bonding curve is a contract that mints an output token in response to receiving an input token. The ratio of output produced to input received (the price) is set by an internal algorithm. The simplest incarnation is a contract that accepts Ether and produces some token T according to a linear price growth equation P=Q. You can either print T by sending in Ether or get Ether by returning T to the contract. Here the price of T in Ether is set by the existing quantity of T in circulation. Eg. Suppose the total supply of T is 12. This means that if I feed 1 T into the contract, I’ll receive 12 Eth. If, instead I want a 13th T to be minted, I’ll have to pay 13 Eth to the contract.

The area A is the total holding of Eth. Q is the quantity of the printed bonding curve token, T.

We can generalize the relationship between Eth and T by using calculus. Simply put, for any level of T, the amount of Eth required is the area under the curve P=Q. The area is given by the integral,

So if we want to produce 10 T, we’ll need 1/2(1⁰²) = 50 Eth.

As you can see by the quadratic relationship, the required Eth grows faster than the supply of T. This is the secret sauce required for liquidity. Another way of looking at this relationship is to say that if we feed in 1 Eth to the contract, the more Eth there already is in the contract, the less T that Eth will create and consequently the less the price of T will change (remember P=Q).

For instance, suppose we have 2 T in existence. This means we’ve fed in 1/2(²²) = 2 Eth. Now let’s say we want to see what happens to T when we feed in just 1 more Eth. This takes the total to 3 Eth. Using the above integral, we have

This means an addition of 1 Eth increases the supply of T by

Since P=Q, this also means the price of T increases by about 0.45 from 2 to 2.45 Eth. This amounts to about 22.5% movement in price.

Now let’s repeat this experiment but this time let’s make the preexisting supply of T 1000. This means the contract contains

Now let’s add 1 Eth and see what happens to the price of T.

The amount T printed by 1 Eth is now 0.001

As you can see, the amount of T produced by 1 Eth is now only 0.001. The price also moved from 1000 Eth to 1000.001 Eth, a movement of 0.00001%

We can actually treat this contract as a simple 2-Token decentralized exchange (DEX). As we add more Eth to it, the slippage in price for each additional Eth declines. Liquidity is jargon for how much a particular trade affects the market price. The less the market price “notices” a given trade, the more liquidity the market is said to have. Or to put it another way, liquid markets can absorb larger trades than illiquid markets. There still seems to be a missing part to the solution. It’s true that Eth liquidity has increased by adding more Eth but the price of T is becoming very high in the process. As we’ll see below, the trick to constructing a high liquidity DEX is to not think of the native bonding curve token as a token anyone “wants” but more as a medium of exchange to facilitate trades between tokens people actually want. Then the actual price of T is arbitrary.

Let’s take two well know and existing tokens, Dai and Eth, and take a stab at constructing a decentralized exchange using token bonding curves. The first approach is to deploy two TBC contracts, one that accepts Dai and the other that accepts Eth. The one that accepts Dai prints a token T and the one that accepts Eth prints a token called R.

To swap Eth for Dai, we deposit Eth and get some R. We then have to find an exchange where we can trade R for T and then deposit T into the Dai bonding curve to get Dai. Oh wait… we’ve just substituted the problem of swapping Dai and Eth for the problem of swapping their bonding curve tokens, T and R.

The solution is to have one token that both bonding curves print independently. That means that if I deposit 1 Eth, I’ll get 1 T AND if I deposit 1 Dai I’ll get 1 T. Now if I want to swap, all I need to do is deposit Eth, get some T then deposit T and get some Dai (assuming the contract has a Dai balance).

What about pricing?

Well lets assume the current market price of Eth is $200. This means 1 Eth should give me 200 Dai on any active exchange.

Let’s seed our TBC exchange with 50 Dai and 50 Eth. (For completeness, you should be able to figure out using the equations above that such a seeding will print 10 T for each deposit which means total circulating supply of T will be 20.)

Now someone comes along and wants to use this exchange to buy Eth with Dai. They deposit 10 Dai. This takes the total T printed by the Dai curve to

Dai increased from 50 to 60, T increased from 10 to 10.95

This means the T supply has increased by 0.95. In other words the trader receives 0.95 T for their deposit. Since T is simply an intermediary, a medium of exchange, they want to take their 0.95 T and see how much Eth they’d get. Depositing 0.95 T is the same as reducing the supply by 0.95 T to 9.05 T so if we take the Eth bonding curve and substitute in the new Token values to figure out the final Eth balance in the contract, we’ll have

Remember the initial Eth balance was 50

This means the contract paid out 9.04875 Eth. So for 10 Dai the user received (9.04875x200=)$1809.75 worth of Eth. Good deal! The trader, seeing dollar signs, continues to deposit batches of 10 Dai, hoping to make infinite profit.

What happens when they deposit 10 Dai again?

The Dai contract now produces a total T of

The total T supply has increased by 11.83–10.95 = 0.882.

Notice already that as programmed, the bonding curve has produced less T than before.

Now let’s see how much Eth we get for 0.882 T. Depositing 0.882 T into the Eth bonding curve contract reduces the supply from 9.05 to 8.17 T. That means the Eth left in the contract should go to

This means the contract paid the trader (40.95125–33.37445=) 7.5768 Eth.

At first, 10 Dai produced 9.04 Eth. Now 10 Dai only produces 7.5768 Eth. The terms of trade are changing! In fact they’re changing as you’d expect: the more of one token you trade, the lower its price slips relative to the output token.

Note in the example above that one of the reasons the terms of trade fell is because the second batch of Dai produced less T which was then used to produce less Eth. What happens if for some magical reason the second set of Dai produced the exact same amount of T as the first transaction? That is, instead of having to use 0.882T, let’s use 0.95 T. Plugging that in, the supply of T in the Eth bonding curve drops from 9.05 to 8.1 T. This takes the total Eth in the contract down to

This means the user is paid (40.95125–32.805=) 8.14625 Eth. Clearly more Eth. The implied price of Dai:Eth hasn’t fallen as much in this scenario. We could say the liquidity is higher. So how would we get T printed by 10 Dai to not drop? Well it’s a bonding curve so the best way to do this would be to deposit more Dai. We know that as we deposit more Dai, the impact each unit of Dai has on the T produced declines. Of course, to keep the terms of trade the same, we’d have to deposit more Eth as well. And since we’re depositing more Eth, we also know the Eth liquidity will rise. Putting this all together gives us the complete picture: a decentralized token exchange built on token bonding curves where each token has its own independent bonding curve but each curve prints the same universal token, the medium of exchange: T.

This is how the Behodler token exchange works. Instead of T, the medium of exchange token printed by all the bonding curves is called Scarcity (SCX). To increase liquidity for Dai, you simply purchase Scarcity with Dai. This is the equivalent of getting the liquidity token on a Uniswap exchange contract. Trades between tokens that aren’t Scarcity (such as WBTC and Dai) use Scarcity in the background as described above. To use Behodler, go to the Wei Dai dapp, connect your Metamask wallet and click on the ‘Swap tokens’ menu option on the left. You can either add liquidity by toggling the “Add Liquidity” switch and supplying tokens in the ratio you desire OR you can simply buy Scarcity with a token of your choice. If you want to withdraw liquidity, set Scarcity to your input token and sell it back into Behodler for a desired output token.

Suppose I deposit 10 Dai and 10 Eth into a TBC exchange like Behodler and I get given 20 T. Now I accidentally go and lose my private keys. For all intents and purposes, the 20 T is lost and the exchange now has 10 Dai and 10 Eth stuck in it. What’s the only way to get the Dai and Eth out? With more Dai or Eth of course. So if I bring some Dai to buy Eth, not only will I get Eth but I’ll be doing it on an exchange pre-seeded with liquidity (due to my accidental key loss).

Suppose in addition to the key loss above, someone else comes along and deposits another 10 Dai and 10 Eth and also loses their keys. Now we have an exchange with 20 Dai and 20 Eth with no corresponding T. Again, the only way to get at the Eth or Dai is to deposit even more Eth or Dai and again. The mistake of losing keys has inadvertently and permanently increased the liquidity of the exchange. If you think about it, another strange phenomenon has occured: you can never drain the exchange of all its tokens, no matter what. If you try to buy out all the Dai, you’d have to deposit Eth (since no T exists). But now you have an exchange full of Eth. If you deposit all the T in existence created by this Eth deposit, it wouldn’t be enough to redeem all the Eth. By losing T, you have inadvertently created a permanent token sink!

Behodler simulates this key loss phenomenon by burning a small portion of Scarcity on every trade. Essentially the Scarcity is being leaked away with regular trade, meaning that Behodler becomes a bigger and bigger sink with every trade. This also means that the liquidity of every token will increase over time. Whatever trades on Behodler will become increasingly drawn in like quick sand. This black hole sink effect is what puts the HODL into Behodler.

Besides acting as yet another DEX, Behodler provides a service to the Ethereum community by acting as a permanent sink for Eth (amongst other tokens). Eth sinks are necessary to ensure the security of the Ethereum network by providing price support for the payment to miners.

--

--

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