forked from OpenZeppelin/openzeppelin-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RefundableCrowdsale.test.js
98 lines (80 loc) · 3.47 KB
/
RefundableCrowdsale.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const { balance, BN, ether, shouldFail, time } = require('openzeppelin-test-helpers');
const RefundableCrowdsaleImpl = artifacts.require('RefundableCrowdsaleImpl');
const SimpleToken = artifacts.require('SimpleToken');
contract('RefundableCrowdsale', function ([_, wallet, investor, purchaser, other]) {
const rate = new BN(1);
const goal = ether('50');
const lessThanGoal = ether('45');
const tokenSupply = new BN('10').pow(new BN('22'));
before(async function () {
// Advance to the next block to correctly read time in the solidity "now" function interpreted by ganache
await time.advanceBlock();
});
beforeEach(async function () {
this.openingTime = (await time.latest()).add(time.duration.weeks(1));
this.closingTime = this.openingTime.add(time.duration.weeks(1));
this.afterClosingTime = this.closingTime.add(time.duration.seconds(1));
this.preWalletBalance = await balance.current(wallet);
this.token = await SimpleToken.new();
});
it('rejects a goal of zero', async function () {
await shouldFail.reverting(
RefundableCrowdsaleImpl.new(this.openingTime, this.closingTime, rate, wallet, this.token.address, 0)
);
});
context('with crowdsale', function () {
beforeEach(async function () {
this.crowdsale = await RefundableCrowdsaleImpl.new(
this.openingTime, this.closingTime, rate, wallet, this.token.address, goal
);
await this.token.transfer(this.crowdsale.address, tokenSupply);
});
context('before opening time', function () {
it('denies refunds', async function () {
await shouldFail.reverting(this.crowdsale.claimRefund(investor));
});
});
context('after opening time', function () {
beforeEach(async function () {
await time.increaseTo(this.openingTime);
});
it('denies refunds', async function () {
await shouldFail.reverting(this.crowdsale.claimRefund(investor));
});
context('with unreached goal', function () {
beforeEach(async function () {
await this.crowdsale.sendTransaction({ value: lessThanGoal, from: investor });
});
context('after closing time and finalization', function () {
beforeEach(async function () {
await time.increaseTo(this.afterClosingTime);
await this.crowdsale.finalize({ from: other });
});
it('refunds', async function () {
const balanceTracker = await balance.tracker(investor);
await this.crowdsale.claimRefund(investor, { gasPrice: 0 });
(await balanceTracker.delta()).should.be.bignumber.equal(lessThanGoal);
});
});
});
context('with reached goal', function () {
beforeEach(async function () {
await this.crowdsale.sendTransaction({ value: goal, from: investor });
});
context('after closing time and finalization', function () {
beforeEach(async function () {
await time.increaseTo(this.afterClosingTime);
await this.crowdsale.finalize({ from: other });
});
it('denies refunds', async function () {
await shouldFail.reverting(this.crowdsale.claimRefund(investor));
});
it('forwards funds to wallet', async function () {
const postWalletBalance = await balance.current(wallet);
postWalletBalance.sub(this.preWalletBalance).should.be.bignumber.equal(goal);
});
});
});
});
});
});