Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/bonding curve #827

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions contracts/crowdsale/Crowdsale.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import "../math/SafeMath.sol";
* behavior.
*/


contract Crowdsale {
using SafeMath for uint256;

Expand Down
1 change: 1 addition & 0 deletions contracts/crowdsale/distribution/FinalizableCrowdsale.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import "../../math/SafeMath.sol";
import "../../ownership/Ownable.sol";
import "../validation/TimedCrowdsale.sol";


/**
* @title FinalizableCrowdsale
* @dev Extension of Crowdsale where an owner can do extra work
Expand Down
1 change: 1 addition & 0 deletions contracts/crowdsale/distribution/PostDeliveryCrowdsale.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import "../validation/TimedCrowdsale.sol";
import "../../token/ERC20/ERC20.sol";
import "../../math/SafeMath.sol";


/**
* @title PostDeliveryCrowdsale
* @dev Crowdsale that locks tokens from withdrawal until it ends.
Expand Down
1 change: 1 addition & 0 deletions contracts/crowdsale/price/IncreasingPriceCrowdsale.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pragma solidity ^0.4.18;
import "../validation/TimedCrowdsale.sol";
import "../../math/SafeMath.sol";


/**
* @title IncreasingPriceCrowdsale
* @dev Extension of Crowdsale contract that increases the price of tokens linearly in time.
Expand Down
111 changes: 111 additions & 0 deletions contracts/curation-markets/BancorFormula.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
pragma solidity ^0.4.18;
import "../math/Power.sol";
import "../math/SafeMath.sol";


/**
* @title Bancor formula by Bancor
* @dev Modified from the original by Slava Balasanov
* https://github.com/bancorprotocol/contracts
* Split Power.sol out from BancorFormula.sol and replace SafeMath formulas with zeppelin's SafeMath
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements;
* and to You under the Apache License, Version 2.0. "
*/
contract BancorFormula is Power {
using SafeMath for uint256;

string public version = "0.3";
uint32 private constant MAX_WEIGHT = 1000000;

/**
* @dev given a token supply, connector balance, weight and a deposit amount (in the connector token),
* calculates the return for a given conversion (in the main token)
*
* Formula:
* Return = _supply * ((1 + _depositAmount / _connectorBalance) ^ (_connectorWeight / 1000000) - 1)
*
* @param _supply token total supply
* @param _connectorBalance total connector balance
* @param _connectorWeight connector weight, represented in ppm, 1-1000000
* @param _depositAmount deposit amount, in connector token
*
* @return purchase return amount
*/
function calculatePurchaseReturn(
uint256 _supply,
uint256 _connectorBalance,
uint32 _connectorWeight,
uint256 _depositAmount) public constant returns (uint256)
{
// validate input
require(_supply > 0 && _connectorBalance > 0 && _connectorWeight > 0 && _connectorWeight <= MAX_WEIGHT);

// special case for 0 deposit amount
if (_depositAmount == 0) {
return 0;
}

// special case if the weight = 100%
if (_connectorWeight == MAX_WEIGHT) {
return _supply.mul(_depositAmount).div(_connectorBalance);
}

uint256 result;
uint8 precision;
uint256 baseN = _depositAmount.add(_connectorBalance);
(result, precision) = power(
baseN, _connectorBalance, _connectorWeight, MAX_WEIGHT
);
uint256 newTokenSupply = _supply.mul(result) >> precision;
return newTokenSupply - _supply;
}

/**
* @dev given a token supply, connector balance, weight and a sell amount (in the main token),
* calculates the return for a given conversion (in the connector token)
*
* Formula:
* Return = _connectorBalance * (1 - (1 - _sellAmount / _supply) ^ (1 / (_connectorWeight / 1000000)))
*
* @param _supply token total supply
* @param _connectorBalance total connector
* @param _connectorWeight constant connector Weight, represented in ppm, 1-1000000
* @param _sellAmount sell amount, in the token itself
*
* @return sale return amount
*/
function calculateSaleReturn(
uint256 _supply,
uint256 _connectorBalance,
uint32 _connectorWeight,
uint256 _sellAmount) public constant returns (uint256)
{
// validate input
require(_supply > 0 && _connectorBalance > 0 && _connectorWeight > 0 && _connectorWeight <= MAX_WEIGHT && _sellAmount <= _supply);

// special case for 0 sell amount
if (_sellAmount == 0) {
return 0;
}

// special case for selling the entire supply
if (_sellAmount == _supply) {
return _connectorBalance;
}

// special case if the weight = 100%
if (_connectorWeight == MAX_WEIGHT) {
return _connectorBalance.mul(_sellAmount).div(_supply);
}

uint256 result;
uint8 precision;
uint256 baseD = _supply - _sellAmount;
(result, precision) = power(
_supply, baseD, MAX_WEIGHT, _connectorWeight
);
uint256 oldBalance = _connectorBalance.mul(result);
uint256 newBalance = _connectorBalance << precision;
return oldBalance.sub(newBalance).div(result);
}
}
99 changes: 99 additions & 0 deletions contracts/curation-markets/BondingCurve.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
pragma solidity ^0.4.18;

import "../token/ERC20/StandardToken.sol";
import "../ownership/Ownable.sol";
import "./BancorFormula.sol";


/**
* @title Bonding Curve
* @dev Bonding curve contract based on Bacor formula
* inspired by bancor protocol and simondlr
* https://github.com/bancorprotocol/contracts
* https://github.com/ConsenSys/curationmarkets/blob/master/CurationMarkets.sol
*/
contract BondingCurve is StandardToken, BancorFormula, Ownable {
/**
* @dev Available balance of reserve token in contract
*/
uint256 public poolBalance;

/*
* @dev reserve ratio, represented in ppm, 1-1000000
* 1/3 corresponds to y= multiple * x^2
* 1/2 corresponds to y= multiple * x
* 2/3 corresponds to y= multiple * x^1/2
* multiple will depends on contract initialization,
* specificallytotalAmount and poolBalance parameters
* we might want to add an 'initialize' function that will allow
* the owner to send ether to the contract and mint a given amount of tokens
*/
uint32 public reserveRatio;

/*
* - Front-running attacks are currently mitigated by the following mechanisms:
* TODO - minimum return argument for each conversion provides a way to define a minimum/maximum price for the transaction
* - gas price limit prevents users from having control over the order of execution
*/
uint256 public gasPrice = 0 wei; // maximum gas price for bancor transactions

/**
* @dev default function
* gas ~ 91645
*/
function() public payable {
buy();
}

/**
* @dev Buy tokens
* gas ~ 77825
* TODO implement maxAmount that helps prevent miner front-running
*/
function buy() validGasPrice public payable returns(bool) {
require(msg.value > 0);
uint256 tokensToMint = calculatePurchaseReturn(totalSupply_, poolBalance, reserveRatio, msg.value);
totalSupply_ = totalSupply_.add(tokensToMint);
balances[msg.sender] = balances[msg.sender].add(tokensToMint);
poolBalance = poolBalance.add(msg.value);
LogMint(tokensToMint, msg.value);
return true;
}

/**
* @dev Sell tokens
* gas ~ 86936
* @param sellAmount Amount of tokens to withdraw
* TODO implement maxAmount that helps prevent miner front-running
*/
function sell(uint256 sellAmount) validGasPrice public returns(bool) {
require(sellAmount > 0 && balances[msg.sender] >= sellAmount);
uint256 ethAmount = calculateSaleReturn(totalSupply_, poolBalance, reserveRatio, sellAmount);
msg.sender.transfer(ethAmount);
poolBalance = poolBalance.sub(ethAmount);
balances[msg.sender] = balances[msg.sender].sub(sellAmount);
totalSupply_ = totalSupply_.sub(sellAmount);
LogWithdraw(sellAmount, ethAmount);
return true;
}

// verifies that the gas price is lower than the universal limit
modifier validGasPrice() {
assert(tx.gasprice <= gasPrice);
_;
}

/**
* @dev Allows the owner to update the gas price limit
* @param _gasPrice The new gas price limit
*/
function setGasPrice(uint256 _gasPrice) onlyOwner public {
require(_gasPrice > 0);
gasPrice = _gasPrice;
}

event LogMint(uint256 amountMinted, uint256 totalCost);
event LogWithdraw(uint256 amountWithdrawn, uint256 reward);
event LogBondingCurve(string logString, uint256 value);
}

Loading