Skip to content

Commit

Permalink
edits based on review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
balasan committed Mar 22, 2018
1 parent 61391d7 commit d07663b
Show file tree
Hide file tree
Showing 16 changed files with 171 additions and 150 deletions.
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
77 changes: 41 additions & 36 deletions contracts/curation-markets/BancorFormula.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import "../math/SafeMath.sol";


/**
* Bancor formula by Bancor
* @title Bancor formula by Bancor
* @dev Modified from the original by Slava Balasanov
* https://github.com/bancorprotocol/contracts
* Modified from the original by Slava Balasanov
* 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. "
Expand All @@ -18,18 +18,18 @@ contract BancorFormula is Power {
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
* @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,
Expand All @@ -41,36 +41,38 @@ contract BancorFormula is Power {
require(_supply > 0 && _connectorBalance > 0 && _connectorWeight > 0 && _connectorWeight <= MAX_WEIGHT);

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

// special case if the weight = 100%
if (_connectorWeight == MAX_WEIGHT)
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 temp = _supply.mul(result) >> precision;
return temp - _supply;
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
* @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,
Expand All @@ -82,25 +84,28 @@ contract BancorFormula is Power {
require(_supply > 0 && _connectorBalance > 0 && _connectorWeight > 0 && _connectorWeight <= MAX_WEIGHT && _sellAmount <= _supply);

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

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

// special case if the weight = 100%
if (_connectorWeight == MAX_WEIGHT)
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 temp1 = _connectorBalance.mul(result);
uint256 temp2 = _connectorBalance << precision;
return temp1.sub(temp2).div(result);
uint256 oldBalance = _connectorBalance.mul(result);
uint256 newBalance = _connectorBalance << precision;
return oldBalance.sub(newBalance).div(result);
}
}
29 changes: 16 additions & 13 deletions contracts/curation-markets/BondingCurve.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,27 @@ import "./BancorFormula.sol";
* 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;

/*
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
* @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
* - 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

Expand Down Expand Up @@ -81,8 +84,8 @@ contract BondingCurve is StandardToken, BancorFormula, Ownable {
}

/**
@dev Allows the owner to update the gas price limit
@param _gasPrice The new gas price limit
* @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);
Expand Down
Loading

0 comments on commit d07663b

Please sign in to comment.