You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
144 lines
5.3 KiB
144 lines
5.3 KiB
7 years ago
|
import ether from './helpers/ether';
|
||
|
import { advanceBlock } from './helpers/advanceToBlock';
|
||
|
import { increaseTimeTo, duration } from './helpers/increaseTime';
|
||
|
import latestTime from './helpers/latestTime';
|
||
|
import EVMRevert from './helpers/EVMRevert';
|
||
8 years ago
|
|
||
7 years ago
|
const BigNumber = web3.BigNumber;
|
||
8 years ago
|
|
||
|
const should = require('chai')
|
||
|
.use(require('chai-as-promised'))
|
||
|
.use(require('chai-bignumber')(BigNumber))
|
||
7 years ago
|
.should();
|
||
8 years ago
|
|
||
7 years ago
|
const Crowdsale = artifacts.require('Crowdsale');
|
||
|
const MintableToken = artifacts.require('MintableToken');
|
||
8 years ago
|
|
||
|
contract('Crowdsale', function ([_, investor, wallet, purchaser]) {
|
||
7 years ago
|
const rate = new BigNumber(1000);
|
||
|
const value = ether(42);
|
||
8 years ago
|
|
||
7 years ago
|
const expectedTokenAmount = rate.mul(value);
|
||
8 years ago
|
|
||
7 years ago
|
before(async function () {
|
||
|
// Advance to the next block to correctly read time in the solidity "now" function interpreted by testrpc
|
||
|
await advanceBlock();
|
||
|
});
|
||
8 years ago
|
|
||
8 years ago
|
beforeEach(async function () {
|
||
8 years ago
|
this.startTime = latestTime() + duration.weeks(1);
|
||
7 years ago
|
this.endTime = this.startTime + duration.weeks(1);
|
||
|
this.afterEndTime = this.endTime + duration.seconds(1);
|
||
8 years ago
|
|
||
7 years ago
|
this.crowdsale = await Crowdsale.new(this.startTime, this.endTime, rate, wallet);
|
||
8 years ago
|
|
||
7 years ago
|
this.token = MintableToken.at(await this.crowdsale.token());
|
||
|
});
|
||
8 years ago
|
|
||
|
it('should be token owner', async function () {
|
||
7 years ago
|
const owner = await this.token.owner();
|
||
|
owner.should.equal(this.crowdsale.address);
|
||
|
});
|
||
8 years ago
|
|
||
|
it('should be ended only after end', async function () {
|
||
7 years ago
|
let ended = await this.crowdsale.hasEnded();
|
||
|
ended.should.equal(false);
|
||
|
await increaseTimeTo(this.afterEndTime);
|
||
|
ended = await this.crowdsale.hasEnded();
|
||
|
ended.should.equal(true);
|
||
|
});
|
||
8 years ago
|
|
||
|
describe('accepting payments', function () {
|
||
|
it('should reject payments before start', async function () {
|
||
7 years ago
|
await this.crowdsale.send(value).should.be.rejectedWith(EVMRevert);
|
||
|
await this.crowdsale.buyTokens(investor, { from: purchaser, value: value }).should.be.rejectedWith(EVMRevert);
|
||
|
});
|
||
8 years ago
|
|
||
|
it('should accept payments after start', async function () {
|
||
7 years ago
|
await increaseTimeTo(this.startTime);
|
||
|
await this.crowdsale.send(value).should.be.fulfilled;
|
||
|
await this.crowdsale.buyTokens(investor, { value: value, from: purchaser }).should.be.fulfilled;
|
||
|
});
|
||
8 years ago
|
|
||
|
it('should reject payments after end', async function () {
|
||
7 years ago
|
await increaseTimeTo(this.afterEndTime);
|
||
|
await this.crowdsale.send(value).should.be.rejectedWith(EVMRevert);
|
||
|
await this.crowdsale.buyTokens(investor, { value: value, from: purchaser }).should.be.rejectedWith(EVMRevert);
|
||
|
});
|
||
|
});
|
||
8 years ago
|
|
||
|
describe('high-level purchase', function () {
|
||
7 years ago
|
beforeEach(async function () {
|
||
|
await increaseTimeTo(this.startTime);
|
||
|
});
|
||
8 years ago
|
|
||
|
it('should log purchase', async function () {
|
||
7 years ago
|
const { logs } = await this.crowdsale.sendTransaction({ value: value, from: investor });
|
||
8 years ago
|
|
||
7 years ago
|
const event = logs.find(e => e.event === 'TokenPurchase');
|
||
8 years ago
|
|
||
7 years ago
|
should.exist(event);
|
||
|
event.args.purchaser.should.equal(investor);
|
||
|
event.args.beneficiary.should.equal(investor);
|
||
|
event.args.value.should.be.bignumber.equal(value);
|
||
|
event.args.amount.should.be.bignumber.equal(expectedTokenAmount);
|
||
|
});
|
||
8 years ago
|
|
||
|
it('should increase totalSupply', async function () {
|
||
7 years ago
|
await this.crowdsale.send(value);
|
||
|
const totalSupply = await this.token.totalSupply();
|
||
|
totalSupply.should.be.bignumber.equal(expectedTokenAmount);
|
||
|
});
|
||
8 years ago
|
|
||
|
it('should assign tokens to sender', async function () {
|
||
7 years ago
|
await this.crowdsale.sendTransaction({ value: value, from: investor });
|
||
8 years ago
|
let balance = await this.token.balanceOf(investor);
|
||
7 years ago
|
balance.should.be.bignumber.equal(expectedTokenAmount);
|
||
|
});
|
||
8 years ago
|
|
||
|
it('should forward funds to wallet', async function () {
|
||
7 years ago
|
const pre = web3.eth.getBalance(wallet);
|
||
|
await this.crowdsale.sendTransaction({ value, from: investor });
|
||
|
const post = web3.eth.getBalance(wallet);
|
||
|
post.minus(pre).should.be.bignumber.equal(value);
|
||
|
});
|
||
|
});
|
||
8 years ago
|
|
||
|
describe('low-level purchase', function () {
|
||
7 years ago
|
beforeEach(async function () {
|
||
|
await increaseTimeTo(this.startTime);
|
||
|
});
|
||
7 years ago
|
|
||
8 years ago
|
it('should log purchase', async function () {
|
||
7 years ago
|
const { logs } = await this.crowdsale.buyTokens(investor, { value: value, from: purchaser });
|
||
7 years ago
|
|
||
7 years ago
|
const event = logs.find(e => e.event === 'TokenPurchase');
|
||
7 years ago
|
|
||
7 years ago
|
should.exist(event);
|
||
|
event.args.purchaser.should.equal(purchaser);
|
||
|
event.args.beneficiary.should.equal(investor);
|
||
|
event.args.value.should.be.bignumber.equal(value);
|
||
|
event.args.amount.should.be.bignumber.equal(expectedTokenAmount);
|
||
|
});
|
||
7 years ago
|
|
||
8 years ago
|
it('should increase totalSupply', async function () {
|
||
7 years ago
|
await this.crowdsale.buyTokens(investor, { value, from: purchaser });
|
||
|
const totalSupply = await this.token.totalSupply();
|
||
|
totalSupply.should.be.bignumber.equal(expectedTokenAmount);
|
||
|
});
|
||
7 years ago
|
|
||
8 years ago
|
it('should assign tokens to beneficiary', async function () {
|
||
7 years ago
|
await this.crowdsale.buyTokens(investor, { value, from: purchaser });
|
||
|
const balance = await this.token.balanceOf(investor);
|
||
|
balance.should.be.bignumber.equal(expectedTokenAmount);
|
||
|
});
|
||
7 years ago
|
|
||
8 years ago
|
it('should forward funds to wallet', async function () {
|
||
7 years ago
|
const pre = web3.eth.getBalance(wallet);
|
||
|
await this.crowdsale.buyTokens(investor, { value, from: purchaser });
|
||
|
const post = web3.eth.getBalance(wallet);
|
||
|
post.minus(pre).should.be.bignumber.equal(value);
|
||
|
});
|
||
|
});
|
||
|
});
|