The basic idea behind a bonding curve is that the price of the token is determined by a mathematical function that relates the number of tokens in circulation to the amount tokens that is used to buy and sell the tokens.
The bonding curve model is often used for new tokens that are created and distributed through an initial coin offering (ICO). In a traditional ICO, a fixed amount of tokens is created and sold at a fixed price. However, with a bonding curve, the price of the token is determined by the supply and demand for the token.
Logic
The bonding curve algorithm uses a mathematical function to determine the price of the token at any given point in time. As more tokens are bought, the price of the token increases. Conversely, as more tokens are sold, the price of the token decreases. The price of the token is adjusting to the market demand for the token.
Types
There are different types of bonding curves that can be used, but one common type is the exponential curve. Where the price adjustment is exponential.
Example Contract
pragma solidity ^0.8.0;
contract BondingCurve {
uint256 public reserveBalance;
uint256 public totalSupply;
uint256 public initialPrice;
uint256 public reserveRatio;
address public tokenAddress;
constructor(uint256 _initialPrice, uint256 _reserveRatio, address _tokenAddress) {
initialPrice = _initialPrice;
reserveRatio = _reserveRatio;
tokenAddress = _tokenAddress;
}
function buy() public payable {
require(msg.value > 0, "Must send ether to buy tokens.");
uint256 tokensToMint = calculatePurchaseAmount(msg.value);
require(tokensToMint > 0, "Must purchase a non-zero number of tokens.");
totalSupply += tokensToMint;
reserveBalance += msg.value;
require(IERC20(tokenAddress).mint(msg.sender, tokensToMint), "Token minting failed.");
}
function sell(uint256 tokensToBurn) public {
require(tokensToBurn > 0, "Must burn a non-zero number of tokens.");
require(totalSupply >= tokensToBurn, "Not enough tokens in circulation.");
uint256 etherToReturn = calculateSaleAmount(tokensToBurn);
require(etherToReturn > 0, "Must sell at least one wei worth of tokens.");
totalSupply -= tokensToBurn;
reserveBalance -= etherToReturn;
(bool success, ) = msg.sender.call{value: etherToReturn}("");
require(success, "Ether transfer failed.");
require(IERC20(tokenAddress).burn(msg.sender, tokensToBurn), "Token burning failed.");
}
function calculatePurchaseAmount(uint256 etherToSpend) public view returns (uint256) {
uint256 numerator = etherToSpend * totalSupply;
uint256 denominator = reserveBalance + etherToSpend;
return numerator / denominator;
}
function calculateSaleAmount(uint256 tokensToBurn) public view returns (uint256) {
uint256 numerator = reserveBalance * tokensToBurn;
uint256 denominator = totalSupply;
return numerator / denominator;
}
}
In the example implementation, the formula for the determining the amount of tokens to mint is:
mintAmount = etherToSpend * totalSupply / reserveBalance + etherToSpend
This is based on the Bancor Formula, which uses a logarithmic curve to determine the price of the token. As more tokens are minted and added to the total supply, the price of the token increases logarithmically. This means that early buyers can purchase the token at a lower price, while later buyers will have to pay more as the demand for the token increases.
The curve looks like this:
Conclusion
In conclusion, a bonding curve is a mathematical formula that determines the price of a token as the supply and demand of the token changes. It is used to create a market for a new token by providing a mechanism for buying and selling the token at a price that is determined by the market.
In the context of cryptocurrencies, bonding curves have gained popularity as a way to launch new tokens with a fair and transparent pricing mechanism. The use of smart contracts to implement bonding curves provides a secure and reliable way to manage the token supply and price.
The example smart contract presented earlier demonstrates how a simple bonding curve can be implemented using a logarithmic pricing curve, where the price of the token increases logarithmically as more tokens are purchased. There are many other possible bonding curve designs and mathematical formulas that can be used to achieve different goals and outcomes.
Overall, bonding curves are a powerful tool for creating decentralized markets and managing the supply and demand of new tokens in a transparent and secure way.