Skip to content
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
allow custom rate function in crowdsales
  • Loading branch information
frangio committed Jul 18, 2017
commit 5c2e866e998e43f414e6695ad698e0aad4b57c9c
12 changes: 4 additions & 8 deletions contracts/crowdsale/Crowdsale.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ contract Crowdsale {
// address where funds are collected
address public wallet;

// how many token units a buyer gets per wei
uint256 public rate;

// amount of raised money in wei
uint256 public weiRaised;

Expand All @@ -40,26 +37,25 @@ contract Crowdsale {
event TokenPurchase(address indexed purchaser, address indexed beneficiary, uint256 value, uint256 amount);


function Crowdsale(uint256 _startBlock, uint256 _endBlock, uint256 _rate, address _wallet) {
function Crowdsale(uint256 _startBlock, uint256 _endBlock, address _wallet) {
require(_startBlock >= block.number);
require(_endBlock >= _startBlock);
require(_rate > 0);
require(_wallet != 0x0);

token = createTokenContract();
startBlock = _startBlock;
endBlock = _endBlock;
rate = _rate;
wallet = _wallet;
}

function getRate() returns (uint256);

// creates the token to be sold.
// override this method to have crowdsale of a specific mintable token.
function createTokenContract() internal returns (MintableToken) {
return new MintableToken();
}


// fallback function can be used to buy tokens
function () payable {
buyTokens(msg.sender);
Expand All @@ -73,7 +69,7 @@ contract Crowdsale {
uint256 weiAmount = msg.value;

// calculate token amount to be created
uint256 tokens = weiAmount.mul(rate);
uint256 tokens = weiAmount.mul(getRate());

// update state
weiRaised = weiRaised.add(weiAmount);
Expand Down
15 changes: 15 additions & 0 deletions contracts/crowdsale/FixedRate.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
pragma solidity ^0.4.11;

import './Crowdsale.sol';

contract FixedRate is Crowdsale {
uint256 rate;

function FixedRate(uint256 _rate) {
rate = _rate;
}

function getRate() returns (uint256) {
return rate;
}
}
2 changes: 1 addition & 1 deletion test/Crowdsale.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const should = require('chai')
.use(require('chai-bignumber')(BigNumber))
.should()

const Crowdsale = artifacts.require('Crowdsale')
const Crowdsale = artifacts.require('./helpers/CrowdsaleImpl.sol')
const MintableToken = artifacts.require('MintableToken')

contract('Crowdsale', function ([_, investor, wallet, purchaser]) {
Expand Down
6 changes: 4 additions & 2 deletions test/helpers/CappedCrowdsaleImpl.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ pragma solidity ^0.4.11;


import '../../contracts/crowdsale/CappedCrowdsale.sol';
import '../../contracts/crowdsale/FixedRate.sol';


contract CappedCrowdsaleImpl is CappedCrowdsale {
contract CappedCrowdsaleImpl is CappedCrowdsale, FixedRate {

function CappedCrowdsaleImpl (
uint256 _startBlock,
Expand All @@ -13,8 +14,9 @@ contract CappedCrowdsaleImpl is CappedCrowdsale {
address _wallet,
uint256 _cap
)
Crowdsale(_startBlock, _endBlock, _rate, _wallet)
Crowdsale(_startBlock, _endBlock, _wallet)
CappedCrowdsale(_cap)
FixedRate(_rate)
{
}

Expand Down
19 changes: 19 additions & 0 deletions test/helpers/CrowdsaleImpl.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
pragma solidity ^0.4.11;

import '../../contracts/crowdsale/Crowdsale.sol';
import '../../contracts/crowdsale/FixedRate.sol';

contract CrowdsaleImpl is Crowdsale, FixedRate {

function CrowdsaleImpl (
uint256 _startBlock,
uint256 _endBlock,
uint256 _rate,
address _wallet
)
Crowdsale(_startBlock, _endBlock, _wallet)
FixedRate(_rate)
{
}

}
6 changes: 4 additions & 2 deletions test/helpers/FinalizableCrowdsaleImpl.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ pragma solidity ^0.4.11;


import '../../contracts/crowdsale/FinalizableCrowdsale.sol';
import '../../contracts/crowdsale/FixedRate.sol';


contract FinalizableCrowdsaleImpl is FinalizableCrowdsale {
contract FinalizableCrowdsaleImpl is FinalizableCrowdsale, FixedRate {

function FinalizableCrowdsaleImpl (
uint256 _startBlock,
uint256 _endBlock,
uint256 _rate,
address _wallet
)
Crowdsale(_startBlock, _endBlock, _rate, _wallet)
Crowdsale(_startBlock, _endBlock, _wallet)
FinalizableCrowdsale()
FixedRate(_rate)
{
}

Expand Down
6 changes: 4 additions & 2 deletions test/helpers/RefundableCrowdsaleImpl.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ pragma solidity ^0.4.11;


import '../../contracts/crowdsale/RefundableCrowdsale.sol';
import '../../contracts/crowdsale/FixedRate.sol';


contract RefundableCrowdsaleImpl is RefundableCrowdsale {
contract RefundableCrowdsaleImpl is RefundableCrowdsale, FixedRate {

function RefundableCrowdsaleImpl (
uint256 _startBlock,
Expand All @@ -13,8 +14,9 @@ contract RefundableCrowdsaleImpl is RefundableCrowdsale {
address _wallet,
uint256 _goal
)
Crowdsale(_startBlock, _endBlock, _rate, _wallet)
Crowdsale(_startBlock, _endBlock, _wallet)
RefundableCrowdsale(_goal)
FixedRate(_rate)
{
}

Expand Down