Skip to content

Commit

Permalink
Merge pull request #2 from TokenMarketNet/feat/multiple-preicos
Browse files Browse the repository at this point in the history
Feat/multiple preicos
  • Loading branch information
miohtama committed May 13, 2017
2 parents 0fa6cc8 + 8bf7672 commit e34622d
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 60 deletions.
72 changes: 29 additions & 43 deletions contracts/MilestonePricing.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,18 @@ pragma solidity ^0.4.6;
import "./PricingStrategy.sol";
import "./Crowdsale.sol";
import "./SafeMathLib.sol";
import "zeppelin/contracts/ownership/Ownable.sol";


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

using SafeMathLib for uint;

uint public constant MAX_MILESTONE = 10;

// This is our PresaleFundCollector contract
address public preicoContractAddress;

// Price for presale investors weis per toke
uint public preicoPrice;
// This contains all pre-ICO addresses, and their prices (weis per token)
mapping (address => uint) public preicoAddresses;

/**
* Define pricing schedule using milestones.
Expand All @@ -40,16 +36,9 @@ contract MilestonePricing is PricingStrategy {
// How many active milestones we have
uint public milestoneCount;

/**
* @param _preicoContractAddress PresaleFundCollector address
* @param _preicoPrice How many weis one token cost for pre-ico investors
* @param _milestones uint[] miletones Pairs of (time, price)
*/
function MilestonePricing(address _preicoContractAddress, uint _preicoPrice, uint[] _milestones) {

preicoContractAddress = _preicoContractAddress;
preicoPrice = _preicoPrice;

/// @dev Contruction, creating a list of milestones
/// @param _milestones uint[] milestones Pairs of (time, price)
function MilestonePricing(uint[] _milestones) {
// Need to have tuples, length check
if(_milestones.length % 2 == 1 || _milestones.length >= MAX_MILESTONE*2) {
throw;
Expand Down Expand Up @@ -77,15 +66,21 @@ contract MilestonePricing is PricingStrategy {
}
}

/**
* Iterate through milestones.
*
* You reach end of milestones when price = 0
*
* @return tuple (time, price)
*/
/// @dev This is invoked once for every pre-ICO address, set pricePerToken
/// to 0 to disable
/// @param preicoAddress PresaleFundCollector address
/// @param pricePerToken How many weis one token cost for pre-ico investors
function setPreicoAddress(address preicoAddress, uint pricePerToken)
public
onlyOwner
{
preicoAddresses[preicoAddress] = pricePerToken;
}

/// @dev Iterate through milestones. You reach end of milestones when price = 0
/// @return tuple (time, price)
function getMilestone(uint n) public constant returns (uint, uint) {
return (milestones[n].time, milestones[n].price);
return (milestones[n].time, milestones[n].price);
}

function getFirstMilestone() private constant returns (Milestone) {
Expand All @@ -109,14 +104,10 @@ contract MilestonePricing is PricingStrategy {
return crowdsale.startsAt() == getPricingStartsAt() && crowdsale.endsAt() == getPricingEndsAt();
}

/**
* Get the current milestone or bail out if we are not in the milestone periods.
*
* @return {[type]} [description]
*/
/// @dev Get the current milestone or bail out if we are not in the milestone periods.
/// @return {[type]} [description]
function getCurrentMilestone() private constant returns (Milestone) {
uint i;
uint price;

for(i=0; i<milestones.length; i++) {
if(now < milestones[i].time) {
Expand All @@ -125,25 +116,20 @@ contract MilestonePricing is PricingStrategy {
}
}

/**
* Get the current price.
*
* @return The current price or 0 if we are outside milestone period
*/
/// @dev Get the current price.
/// @return The current price or 0 if we are outside milestone period
function getCurrentPrice() public constant returns (uint result) {
return getCurrentMilestone().price;
}

/**
* Calculate the current price for buy in amount.
*/
/// @dev Calculate the current price for buy in amount.
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.times(multiplier) / preicoPrice;
if(preicoAddresses[msgSender] > 0) {
return value.times(multiplier) / preicoAddresses[msgSender];
}

uint price = getCurrentPrice();
Expand Down
6 changes: 3 additions & 3 deletions crowdsales/unit-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ unit_test:
contract_name: MilestonePricing
contract_file: MilestonePricing.sol
arguments:
_preicoPrice: "{{ to_wei('0.008', 'ether') }}"
_preicoContractAddress: "{{contracts.preico.address}}"
_milestones:
- 1492272000
- "{{ to_wei('0.010', 'ether') }}"
Expand Down Expand Up @@ -161,6 +159,9 @@ unit_test:
# Set token upgrade master
confirm_tx(token.transact({"from": deploy_address}).setUpgradeMaster(team_multisig.address))
# Set a pre-ICO address here
confirm_tx(pricing_strategy.transact({"from": deploy_address}).setPreicoAddress(preico.address, 123456))
# Make sure that everything we have deployed all contracts in good state
# and their internal state is sane
verify_actions: |
Expand All @@ -176,4 +177,3 @@ unit_test:
# To be used with the deposit instructions for contributors
sig_data = crowdsale._prepare_transaction("buy")
print("Crowdsale.buy() data payload is", sig_data["data"])
9 changes: 5 additions & 4 deletions ico/tests/contracts/test_milestone_pricing.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,11 @@ def fractional_token(chain, token_name, token_symbol, team_multisig) -> Contract


@pytest.fixture
def milestone_pricing(chain, presale_fund_collector, start_time, end_time):
def milestone_pricing(chain, presale_fund_collector, start_time, end_time, team_multisig):

week = 24*3600*7

args = [
presale_fund_collector.address,
to_wei("0.05", "ether"),
[
start_time + 0, to_wei("0.10", "ether"),
start_time + week*1, to_wei("0.12", "ether"),
Expand All @@ -78,9 +76,12 @@ def milestone_pricing(chain, presale_fund_collector, start_time, end_time):
]

tx = {
"gas": 4000000
"gas": 4000000,
"from": team_multisig
}
contract, hash = chain.provider.deploy_contract('MilestonePricing', deploy_args=args, deploy_transaction=tx)

contract.transact({"from": team_multisig}).setPreicoAddress(presale_fund_collector.address, to_wei("0.05", "ether"))
return contract


Expand Down
4 changes: 1 addition & 3 deletions ico/tests/contracts/test_minted_capped.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,13 @@ def milestone_pricing(chain, start_time, end_time):
week = 24*3600 * 7

args = [
"0x0000000000000000000000000000000000000000",
to_wei("0.05", "ether"),
[
start_time + 0, to_wei("0.10", "ether"),
start_time + week*1, to_wei("0.11", "ether"),
start_time + week*2, to_wei("0.12", "ether"),
start_time + week*3, to_wei("0.13", "ether"),
end_time, 0,
],
]
]

tx = {
Expand Down
5 changes: 0 additions & 5 deletions ico/tests/contracts/test_relaunch_with_new_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,6 @@ def milestone_pricing(chain, start_time, end_time):
week = 24*3600 * 7

args = [
"0x0000000000000000000000000000000000000000",
to_wei("0.00001", "ether"),
[
start_time + 0, to_wei("0.0009", "ether"),
start_time + week*1, to_wei("0.0009", "ether"),
Expand Down Expand Up @@ -299,6 +297,3 @@ def test_rebuild_success_crowdsale_with_new_token(chain, new_token, relaunched_c
assert new_token.call().totalSupply() == int(before_final * 1.20)

assert new_token.call().released()



2 changes: 0 additions & 2 deletions ico/tests/contracts/test_relaunch_with_old_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ def milestone_pricing(chain, start_time, end_time):
week = 24*3600 * 7

args = [
"0x0000000000000000000000000000000000000000",
to_wei("0.00001", "ether"),
[
start_time + 0, to_wei("0.0009", "ether"),
start_time + week*1, to_wei("0.0009", "ether"),
Expand Down

0 comments on commit e34622d

Please sign in to comment.