require chai use require chai-as-promised use require chai-bignumber w

  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
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
require('chai')
.use(require('chai-as-promised'))
.use(require('chai-bignumber')(web3.BigNumber))
.should();
let data = require('./data.js');
let big = require('./util/bigNum.js').big;
let {etherUsedForTx} = require('./util/gas.js');
let RecereumToken = artifacts.require('RecereumToken');
let RecereumPreSaleMock = artifacts.require('RecereumPreSaleMock');
let {deployTestContracts} = require('./util/deploy.js');
contract('Crowdsale [Basic Features]', function(accounts) {
let {tokenContract, saleContract, fundsWallet} = {};
beforeEach(async () => {
({tokenContract, saleContract, fundsWallet} = await deployTestContracts(accounts));
});
it('Owner wallet should allow sale contract to transfer up to hard cap tokens', async () => {
data.PRESALE_TOKEN_CAP.should.be.bignumber.equal(
await tokenContract.allowance.call(accounts[0], saleContract.address)
);
});
it('Fail to purchase tokens with ether amount less than minimal purhcase limit', async () => {
await saleContract.setTime(data.PRESALE_START_DATE);
await saleContract.sendTransaction({value: data.MINIMAL_PURCHASE_WEI.sub(1)})
.should.be.rejectedWith(': revert');
});
it('Buy some tokens with minimal allowed ether', async () => {
let buyerWallet = accounts[2];
await saleContract.setTime(data.PRESALE_START_DATE);
// initial balances
let fundsBalance = await web3.eth.getBalance(fundsWallet);
let buyerBalance = await web3.eth.getBalance(buyerWallet);
let tokenBalance = await tokenContract.balanceOf.call(buyerWallet);
// Spend minimal allowed ether
let weiAmount = data.MINIMAL_PURCHASE_WEI.mul(1);
let res = await saleContract.sendTransaction({value: weiAmount, from: buyerWallet});
// calculate expected values
let tokenAmount = weiAmount.divToInt(data.TOKEN_PRICE_WEI).mul(10**data.DECIMALS);
let etherUsed = etherUsedForTx(res);
// Do checks
tokenBalance.add(tokenAmount).should.be.bignumber.equal(
await tokenContract.balanceOf.call(buyerWallet)
);
fundsBalance.add(weiAmount).should.be.bignumber.equal(
await web3.eth.getBalance(fundsWallet)
);
});
it('Try to buy more tokens than token cap', async () => {
let buyerWallet = accounts[2];
await saleContract.setTime(data.PRESALE_START_DATE);
// initial balances
let fundsBalance = await web3.eth.getBalance(fundsWallet);
let buyerBalance = await web3.eth.getBalance(buyerWallet);
let tokenBalance = await tokenContract.balanceOf.call(buyerWallet);
// Spend minimal allowed ether
let weiAmount = data.PRESALE_TOKEN_CAP.divToInt(10**data.DECIMALS)
.mul(data.TOKEN_PRICE_WEI).add(data.ETHER);
let res = await saleContract.sendTransaction({value: weiAmount, from: buyerWallet});
// calculate expected values
let tokenAmount = weiAmount.divToInt(data.TOKEN_PRICE_WEI).mul(10**data.DECIMALS);
let acceptedTokens = data.PRESALE_TOKEN_CAP;
let acceptedWei = acceptedTokens.divToInt(10**data.DECIMALS).mul(data.TOKEN_PRICE_WEI);
let etherUsed = etherUsedForTx(res);
// Do checks
tokenBalance.add(acceptedTokens).should.be.bignumber.equal(
await tokenContract.balanceOf.call(buyerWallet)
);
fundsBalance.add(acceptedWei).should.be.bignumber.equal(
await web3.eth.getBalance(fundsWallet)
);
buyerBalance.sub(acceptedWei).sub(etherUsed).should.be.bignumber.equal(
await web3.eth.getBalance(buyerWallet)
);
});
it('Fail to buy token before presale started', async () => {
let buyerWallet = accounts[2];
await saleContract.setTime(data.PRESALE_START_DATE - 1);
let weiAmount = data.MINIMAL_PURCHASE_WEI.mul(1);
await saleContract.sendTransaction({value: weiAmount, from: buyerWallet})
.should.be.rejectedWith(': revert');
});
it('Fail to buy token after presale ended', async () => {
let buyerWallet = accounts[2];
await saleContract.setTime(data.PRESALE_END_DATE);
let weiAmount = data.MINIMAL_PURCHASE_WEI.mul(1);
await saleContract.sendTransaction({value: weiAmount, from: buyerWallet})
.should.be.rejectedWith(': revert');
});
it('Buy token, then change date and fail to buy', async () => {
let buyerWallet = accounts[2];
await saleContract.setTime(data.PRESALE_START_DATE);
let weiAmount = data.MINIMAL_PURCHASE_WEI.mul(1);
await saleContract.sendTransaction({value: weiAmount, from: buyerWallet})
await saleContract.setTime(data.PRESALE_END_DATE);
await saleContract.sendTransaction({value: weiAmount, from: buyerWallet})
.should.be.rejectedWith(': revert');
});
it('Just another purchase test', async () => {
let tokenContract = await RecereumToken.new();
let fundsWallet = accounts[1];
let buyerWallet = accounts[2];
let tokenCap = 500 * (10**data.DECIMALS);
let saleContract = await RecereumPreSaleMock.new(
tokenContract.address,
fundsWallet,
data.PRESALE_START_DATE,
data.PRESALE_END_DATE,
tokenCap
);
await tokenContract.approve(
saleContract.address,
tokenCap,
{from: accounts[0]}
);
await saleContract.setTime(data.PRESALE_START_DATE);
// initial balances
let fundsBalance = await web3.eth.getBalance(fundsWallet);
let buyerBalance = await web3.eth.getBalance(buyerWallet);
let tokenBalance = await tokenContract.balanceOf.call(buyerWallet);
0..should.be.bignumber.equal(tokenBalance);
// Spend 1 ether, buy 420 tokens
let weiAmount = data.ETHER.mul(1)
let res = await saleContract.sendTransaction({value: weiAmount, from: buyerWallet});
// calculate expected values
let tokenAmount = weiAmount.divToInt(data.TOKEN_PRICE_WEI).mul(10**data.DECIMALS);
let etherUsed = etherUsedForTx(res);
// Do checks
tokenBalance.add(tokenAmount).should.be.bignumber.equal(
await tokenContract.balanceOf.call(buyerWallet)
);
fundsBalance.add(weiAmount).should.be.bignumber.equal(
await web3.eth.getBalance(fundsWallet)
);
buyerBalance.sub(weiAmount).sub(etherUsed).should.be.bignumber.equal(
await web3.eth.getBalance(buyerWallet)
);
// Spend 1 ether more, buy 80 tokens because of hard cap of 500 tokens
let res2 = await saleContract.sendTransaction({
value: data.ETHER.mul(1),
from: buyerWallet
});
// calculate expected values
let newTokenAmount = 80 * (10**data.DECIMALS);
let etherUsed2 = etherUsedForTx(res2);
let extraFundsAmount = data.TOKEN_PRICE_WEI.mul(80);
// Do checks
tokenBalance.add(tokenAmount).add(newTokenAmount).should.be.bignumber.equal(
await tokenContract.balanceOf.call(buyerWallet)
);
fundsBalance.add(weiAmount).add(extraFundsAmount).should.be.bignumber.equal(
await web3.eth.getBalance(fundsWallet)
);
buyerBalance.sub(weiAmount).sub(extraFundsAmount).sub(etherUsed).sub(etherUsed2).should.be.bignumber.equal(
await web3.eth.getBalance(buyerWallet)
);
// Spend 1 ether more, get exception
let res3 = await saleContract.sendTransaction({
value: data.ETHER.mul(1),
from: buyerWallet
}).should.be.rejectedWith(': revert');
});
});