📈

# ERC20 Bonding Curve

Constant Function Market Maker
The ERC20BondingCurve is the token contract for GBT, but unlike regular ERC20 tokens, it is extended to have a bonding curve built directly into the token contract. The bonding curve is a constant function market maker with two token reserves. The reserves of the bonding curve consist of a base token (eg. ETH) and GBT. The bonding curve equation is:
K = rETH * rGBT
Where K is a constant, rETH is ETH in the bonding curve reserve, and rGBT is GBT in the bonding curve reserve.
The reserve GBT (rGBT) is always equal to the actual GBT (aGBT) in the bonding curve. The reserve ETH (rETH) is broken up into three parts: virtual ETH (vETH), borrowed ETH (bETH), and actual ETH (aETH). GumBall contracts utilize vETH and bETH, which are both placeholders for ETH in the bonding curve reserve. vETH is used to set the initial amount of ETH in the reserve. Since vETH does not actually exist, it can never be used as active liquidity, but it does concentrate the liquidity of actual ETH by setting a price floor. Using vETH instead of real ETH to set the floor price increases the capital efficiency of the prptocol. bETH is used to maintain the amount of aETH in the reserve when ETH is borrowed from the bonding curve. Lastly, aETH and aGBT represent liquid ETH and GBT, respectively, in the reserves. As a result, the formula of the constant K can be arranged as follows:
K = (vETH + bETH + aETH) * (aGBT)

### Initial State

The initial state of the bonding curve is determined by vETH and total supply of GBT (tsGBT). For example, vETH is set to 100 and tsGBT is 100. The reserves would look like:
K = 100 rETH * 100 rGBT
K = (100 vETH + 0 bETH + 0 aETH) * (100 aGBT)
Initial GBT Price = 100 vETH / 100 aGBT = 1 ETH/GBT
Initially there is no actual ETH liquidity, but this is not an issue, as there is no GBT circulating to be sold into the bonding curve. Actual ETH liquidity comes in once GBT is purchased from the bonding curve. For example, after a 1 GBT purchase the reserves would look like:
K = (100 vETH + 0 bETH + 1.01 aETH) * (99 aGBT)
GBT Price = (100 vETH + 1.01 aETH) / 99 aGBT = 1.02 ETH/GBT
Now there is 1 GBT circulating, if this 1 GBT was sold back to the bonding curve there would be actual ETH liquidity to provide the seller. The final circulating GBT sold to the bonding curve would be sold at the floor price. The price of GBT can never go under this floor price and there is always sufficient ETH liquidity for GBT to be sold into the bonding curve above the floor price.

### Swap

Each time GBT is bought from the bonding curve, the price of GBT increases. The amount of GBT stored in the bonding curve, also known as reserve GBT (rGBT), decreases when GBT is bought. Conversely, the amount of rETH stored in the bonding curve contract increases.
In a Buy the amount of ETH deposited into the bonding curve by a buyer is represented as inETH. The amount of GBT returned to the buyer, outGBT , is calculated based on the following formula:
outGBT = (aGBT * inETH) / (vETH + bETH + aETH + inETH)
When GBT is sold to the bonding curve, the price of GBT decreases because rGBT goes up and rETH goes down. When GBT is sold, it is sent into the bonding curve as inGBT. The amount of ETH returned to the seller, outETH, is calculated based on the following formula:
outETH = ((vETH + bETH + aETH) * inGBT) / (aGBT + inGBT)

### Swap Fees

There is a 2.5% fee for every swap on the bonding curve. This fee is taken in the input token and stored in the treasury (outside the bonding curve reserves) where they accumulate until a function is called to withdraw the tokens to their appropriate addresses/contracts.

In the below diagram, user inputs 51.28 ETH into the bonding curve. 1.28 of this ETH (2.5%) is sent to the treasury. The remaining 50 ETH is sent to the bonding curve reserve. The user receives 10 GBT in return. Example of a user buying GBT from the bonding curve
When the 50 ETH is sent to the bonding curve, 10 rGBT are removed from the reserve and become circulating GBT.
GBT price increases from 4 ETH/GBT to 6.25 ETH/GBT as rGBT supply contracts. Visualization of bonding curve for buying GBT

### Sell Example

In the below diagram, user inputs 10.26 GBT into the bonding curve. 0.26 of this GBT (2.5%) is sent to the treasury. The remaining 10 GBT is sent to the bonding curve reserve. The user receives 33.33 ETH in return. Example of a user selling GBT to the bonding curve
When the 10 GBT is returned to the bonding curve reserve GBT price decreases from 4 ETH/GBT to 2.78 ETH/GBT as rGBT supply increases. Visualization of bonding curve for selling GBT