Skip to content

Commit

Permalink
Added fractional pricing with decimals.
Browse files Browse the repository at this point in the history
  • Loading branch information
miohtama committed Apr 20, 2017
1 parent 1cfc010 commit 619a4cc
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 13 deletions.
8 changes: 4 additions & 4 deletions contracts/Crowdsale.sol
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
pragma solidity ^0.4.8;

import "zeppelin/contracts/token/ERC20.sol";
import "./SafeMathLib.sol";
import "./Haltable.sol";
import "./PricingStrategy.sol";
import "./FinalizeAgent.sol";
import "./FractionalERC20.sol";


/**
Expand All @@ -23,7 +23,7 @@ contract Crowdsale is Haltable {
using SafeMathLib for uint;

/* The token we are selling */
ERC20 public token;
FractionalERC20 public token;

/* How we are going to price our offering */
PricingStrategy public pricingStrategy;
Expand Down Expand Up @@ -89,7 +89,7 @@ contract Crowdsale is Haltable {

owner = msg.sender;

token = ERC20(_token);
token = FractionalERC20(_token);

setPricingStrategy(_pricingStrategy);

Expand Down Expand Up @@ -137,7 +137,7 @@ contract Crowdsale is Haltable {
function invest(address receiver) inState(State.Funding) stopInEmergency payable public {

uint weiAmount = msg.value;
uint tokenAmount = pricingStrategy.calculatePrice(weiAmount, weiRaised, tokensSold, msg.sender);
uint tokenAmount = pricingStrategy.calculatePrice(weiAmount, weiRaised, tokensSold, msg.sender, token.decimals());

if(tokenAmount == 0) {
// Dust transaction
Expand Down
8 changes: 6 additions & 2 deletions contracts/FlatPricing.sol
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
pragma solidity ^0.4.6;

import "./PricingStrategy.sol";
import "./SafeMathLib.sol";

/**
* Fixed crowdsale pricing - everybody gets the same price.
*/
contract FlatPricing is PricingStrategy {

using SafeMathLib for uint;

/* How many weis one token costs */
uint public tokenPrice;

Expand All @@ -19,8 +22,9 @@ contract FlatPricing is PricingStrategy {
*
* @param {uint amount} Buy-in value in wei.
*/
function calculatePrice(uint value, uint tokensSold, uint weiRaised, address msgSender) public constant returns (uint) {
return value / tokenPrice;
function calculatePrice(uint value, uint tokensSold, uint weiRaised, address msgSender, uint decimals) public constant returns (uint) {
uint multiplier = 10 ** decimals;
return value.times(multiplier) / tokenPrice;
}

}
12 changes: 12 additions & 0 deletions contracts/FractionalERC20.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
pragma solidity ^0.4.8;

import "zeppelin/contracts/token/ERC20.sol";

/**
* A token that defines fractional units as decimals.
*/
contract FractionalERC20 is ERC20 {

uint public decimals;

}
11 changes: 8 additions & 3 deletions contracts/MilestonePricing.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ pragma solidity ^0.4.6;

import "./PricingStrategy.sol";
import "./Crowdsale.sol";
import "./SafeMathLib.sol";


/**
* Time milestone based pricing with special support for pre-ico deals.
*/
contract MilestonePricing is PricingStrategy {

using SafeMathLib for uint;

uint public constant MAX_MILESTONE = 10;

// This is our PresaleFundCollector contract
Expand Down Expand Up @@ -134,15 +137,17 @@ contract MilestonePricing is PricingStrategy {
/**
* Calculate the current price for buy in amount.
*/
function calculatePrice(uint value, uint tokensSold, uint weiRaised, address msgSender) public constant returns (uint) {
function calculatePrice(uint value, uint tokensSold, uint weiRaised, address msgSender, uint decimals) public constant returns (uint) {

uint multiplier = 10 ** decimals;

// This investor is coming through pre-ico
if(msgSender == preicoContractAddress) {
return value / preicoPrice;
return value.times(multiplier) / preicoPrice;
}

uint price = getCurrentPrice();
return value / price;
return value.times(multiplier) / price;
}

function() payable {
Expand Down
10 changes: 9 additions & 1 deletion contracts/PricingStrategy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ contract PricingStrategy {

/**
* When somebody tries to buy tokens for X eth, calculate how many tokens they get.
*
*
* @param value - What is the value of the transaction send in as wei
* @param tokensSold - how much tokens have been sold this far
* @param weiRaised - how much money has been raised this far
* @param msgSender - who is the investor of this transaction
* @param decimals - how many decimal units the token has
* @return Amount of tokens the investor receives
*/
function calculatePrice(uint value, uint tokensSold, uint weiRaised, address msgSender) public constant returns (uint tokenAmount);
function calculatePrice(uint value, uint tokensSold, uint weiRaised, address msgSender, uint decimals) public constant returns (uint tokenAmount);
}
2 changes: 2 additions & 0 deletions contracts/RelaunchedCrowdsale.sol
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pragma solidity ^0.4.8;

import "./MintedTokenCappedCrowdsale.sol";


Expand Down
6 changes: 4 additions & 2 deletions ico/tests/contracts/test_milestone_pricing.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ def test_milestone_prices(chain, milestone_pricing, start_time, customer):
to_wei("0.26", "ether"),
0,
0,
customer
customer,
0,
) == 2


Expand All @@ -172,7 +173,8 @@ def test_milestone_calculate_preico_price(chain, milestone_pricing, start_time,
to_wei("0.05", "ether"),
0,
0,
presale_fund_collector.address
presale_fund_collector.address,
0
) == 1


Expand Down
2 changes: 1 addition & 1 deletion ico/tests/contracts/test_uncapped_flatprice.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def test_buy_reach_goal(chain: TestRPCChain, flat_pricing, ico: Contract, custom
wei_value = preico_funding_goal

# Check that we don't have issues with our pricing
assert flat_pricing.call().calculatePrice(wei_value, 0, 0, customer) > 0
assert flat_pricing.call().calculatePrice(wei_value, 0, 0, customer, 0) > 0

ico.transact({"from": customer, "value": wei_value}).buy()

Expand Down

0 comments on commit 619a4cc

Please sign in to comment.