Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
O
openzeppelin-contracts-upgradeable
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
俞永鹏
openzeppelin-contracts-upgradeable
Commits
77dfcb6e
Commit
77dfcb6e
authored
Aug 06, 2017
by
Jakub Wojciechowski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Change crowdsales to use timestamps instead of block numbers #350
parent
287b873a
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
27 additions
and
20 deletions
+27
-20
Crowdsale.sol
contracts/crowdsale/Crowdsale.sol
+11
-12
Crowdsale.js
test/Crowdsale.js
+16
-8
No files found.
contracts/crowdsale/Crowdsale.sol
View file @
77dfcb6e
...
@@ -6,7 +6,7 @@ import '../math/SafeMath.sol';
...
@@ -6,7 +6,7 @@ import '../math/SafeMath.sol';
/**
/**
* @title Crowdsale
* @title Crowdsale
* @dev Crowdsale is a base contract for managing a token crowdsale.
* @dev Crowdsale is a base contract for managing a token crowdsale.
* Crowdsales have a start and end
block
, where investors can make
* Crowdsales have a start and end
timestamps
, where investors can make
* token purchases and the crowdsale will assign them tokens based
* token purchases and the crowdsale will assign them tokens based
* on a token per ETH rate. Funds collected are forwarded to a wallet
* on a token per ETH rate. Funds collected are forwarded to a wallet
* as they arrive.
* as they arrive.
...
@@ -17,9 +17,9 @@ contract Crowdsale {
...
@@ -17,9 +17,9 @@ contract Crowdsale {
// The token being sold
// The token being sold
MintableToken public token;
MintableToken public token;
// start and end
block
where investments are allowed (both inclusive)
// start and end
timestamps
where investments are allowed (both inclusive)
uint256 public start
Block
;
uint256 public start
Time
;
uint256 public end
Block
;
uint256 public end
Time
;
// address where funds are collected
// address where funds are collected
address public wallet;
address public wallet;
...
@@ -40,15 +40,15 @@ contract Crowdsale {
...
@@ -40,15 +40,15 @@ contract Crowdsale {
event TokenPurchase(address indexed purchaser, address indexed beneficiary, uint256 value, uint256 amount);
event TokenPurchase(address indexed purchaser, address indexed beneficiary, uint256 value, uint256 amount);
function Crowdsale(uint256 _start
Block, uint256 _endBlock
, uint256 _rate, address _wallet) {
function Crowdsale(uint256 _start
Time, uint256 _endTime
, uint256 _rate, address _wallet) {
require(_start
Block >= block.number
);
require(_start
Time >= now
);
require(_end
Block >= _startBlock
);
require(_end
Time >= _startTime
);
require(_rate > 0);
require(_rate > 0);
require(_wallet != 0x0);
require(_wallet != 0x0);
token = createTokenContract();
token = createTokenContract();
start
Block = _startBlock
;
start
Time = _startTime
;
end
Block = _endBlock
;
end
Time = _endTime
;
rate = _rate;
rate = _rate;
wallet = _wallet;
wallet = _wallet;
}
}
...
@@ -92,15 +92,14 @@ contract Crowdsale {
...
@@ -92,15 +92,14 @@ contract Crowdsale {
// @return true if the transaction can buy tokens
// @return true if the transaction can buy tokens
function validPurchase() internal constant returns (bool) {
function validPurchase() internal constant returns (bool) {
uint256 current = block.number;
bool withinPeriod = now >= startTime && now <= endTime;
bool withinPeriod = current >= startBlock && current <= endBlock;
bool nonZeroPurchase = msg.value != 0;
bool nonZeroPurchase = msg.value != 0;
return withinPeriod && nonZeroPurchase;
return withinPeriod && nonZeroPurchase;
}
}
// @return true if crowdsale event has ended
// @return true if crowdsale event has ended
function hasEnded() public constant returns (bool) {
function hasEnded() public constant returns (bool) {
return
block.number > endBlock
;
return
now > endTime
;
}
}
...
...
test/Crowdsale.js
View file @
77dfcb6e
import
moment
from
'moment'
import
ether
from
'./helpers/ether'
import
ether
from
'./helpers/ether'
import
advanceToBlock
from
'./helpers/advanceToBlock'
import
advanceToBlock
from
'./helpers/advanceToBlock'
import
increaseTime
from
'./helpers/increaseTime'
import
latestTime
from
'./helpers/latestTime'
import
EVMThrow
from
'./helpers/EVMThrow'
import
EVMThrow
from
'./helpers/EVMThrow'
const
BigNumber
=
web3
.
BigNumber
const
BigNumber
=
web3
.
BigNumber
...
@@ -19,11 +22,16 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) {
...
@@ -19,11 +22,16 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) {
const
expectedTokenAmount
=
rate
.
mul
(
value
)
const
expectedTokenAmount
=
rate
.
mul
(
value
)
before
(
async
function
()
{
//Advance to the next block to correctly read time in the solidity "now" function interpreted by testrpc
await
advanceToBlock
(
web3
.
eth
.
getBlock
(
'latest'
).
number
+
1
)
})
beforeEach
(
async
function
()
{
beforeEach
(
async
function
()
{
this
.
start
Block
=
web3
.
eth
.
blockNumber
+
10
this
.
start
Time
=
latestTime
().
unix
()
+
moment
.
duration
(
1
,
'week'
).
asSeconds
();
this
.
end
Block
=
web3
.
eth
.
blockNumber
+
20
this
.
end
Time
=
latestTime
().
unix
()
+
moment
.
duration
(
2
,
'week'
).
asSeconds
();
this
.
crowdsale
=
await
Crowdsale
.
new
(
this
.
start
Block
,
this
.
endBlock
,
rate
,
wallet
)
this
.
crowdsale
=
await
Crowdsale
.
new
(
this
.
start
Time
,
this
.
endTime
,
rate
,
wallet
)
this
.
token
=
MintableToken
.
at
(
await
this
.
crowdsale
.
token
())
this
.
token
=
MintableToken
.
at
(
await
this
.
crowdsale
.
token
())
})
})
...
@@ -36,7 +44,7 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) {
...
@@ -36,7 +44,7 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) {
it
(
'should be ended only after end'
,
async
function
()
{
it
(
'should be ended only after end'
,
async
function
()
{
let
ended
=
await
this
.
crowdsale
.
hasEnded
()
let
ended
=
await
this
.
crowdsale
.
hasEnded
()
ended
.
should
.
equal
(
false
)
ended
.
should
.
equal
(
false
)
await
advanceToBlock
(
this
.
endBlock
+
1
)
await
increaseTime
(
moment
.
duration
(
2.1
,
'week'
)
)
ended
=
await
this
.
crowdsale
.
hasEnded
()
ended
=
await
this
.
crowdsale
.
hasEnded
()
ended
.
should
.
equal
(
true
)
ended
.
should
.
equal
(
true
)
})
})
...
@@ -49,13 +57,13 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) {
...
@@ -49,13 +57,13 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) {
})
})
it
(
'should accept payments after start'
,
async
function
()
{
it
(
'should accept payments after start'
,
async
function
()
{
await
advanceToBlock
(
this
.
startBlock
-
1
)
await
increaseTime
(
moment
.
duration
(
1
,
'week'
)
)
await
this
.
crowdsale
.
send
(
value
).
should
.
be
.
fulfilled
await
this
.
crowdsale
.
send
(
value
).
should
.
be
.
fulfilled
await
this
.
crowdsale
.
buyTokens
(
investor
,
{
value
:
value
,
from
:
purchaser
}).
should
.
be
.
fulfilled
await
this
.
crowdsale
.
buyTokens
(
investor
,
{
value
:
value
,
from
:
purchaser
}).
should
.
be
.
fulfilled
})
})
it
(
'should reject payments after end'
,
async
function
()
{
it
(
'should reject payments after end'
,
async
function
()
{
await
advanceToBlock
(
this
.
endBlock
)
await
increaseTime
(
moment
.
duration
(
2.1
,
'week'
)
)
await
this
.
crowdsale
.
send
(
value
).
should
.
be
.
rejectedWith
(
EVMThrow
)
await
this
.
crowdsale
.
send
(
value
).
should
.
be
.
rejectedWith
(
EVMThrow
)
await
this
.
crowdsale
.
buyTokens
(
investor
,
{
value
:
value
,
from
:
purchaser
}).
should
.
be
.
rejectedWith
(
EVMThrow
)
await
this
.
crowdsale
.
buyTokens
(
investor
,
{
value
:
value
,
from
:
purchaser
}).
should
.
be
.
rejectedWith
(
EVMThrow
)
})
})
...
@@ -65,7 +73,7 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) {
...
@@ -65,7 +73,7 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) {
describe
(
'high-level purchase'
,
function
()
{
describe
(
'high-level purchase'
,
function
()
{
beforeEach
(
async
function
()
{
beforeEach
(
async
function
()
{
await
advanceToBlock
(
this
.
startBlock
)
await
increaseTime
(
moment
.
duration
(
1
,
'week'
)
)
})
})
it
(
'should log purchase'
,
async
function
()
{
it
(
'should log purchase'
,
async
function
()
{
...
@@ -104,7 +112,7 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) {
...
@@ -104,7 +112,7 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) {
describe
(
'low-level purchase'
,
function
()
{
describe
(
'low-level purchase'
,
function
()
{
beforeEach
(
async
function
()
{
beforeEach
(
async
function
()
{
await
advanceToBlock
(
this
.
startBlock
)
await
increaseTime
(
moment
.
duration
(
1
,
'week'
)
)
})
})
it
(
'should log purchase'
,
async
function
()
{
it
(
'should log purchase'
,
async
function
()
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment