|
| 1 | +import ether from './helpers/ether' |
| 2 | +import advanceToBlock from './helpers/advanceToBlock' |
| 3 | +import EVMThrow from './helpers/EVMThrow' |
| 4 | + |
| 5 | +const BigNumber = web3.BigNumber; |
| 6 | + |
| 7 | +const should = require('chai') |
| 8 | + .use(require('chai-as-promised')) |
| 9 | + .use(require('chai-bignumber')(BigNumber)) |
| 10 | + .should(); |
| 11 | + |
| 12 | +const SampleCrowdsale = artifacts.require('SampleCrowdsale'); |
| 13 | +const SampleCrowdsaleToken = artifacts.require('SampleCrowdsaleToken'); |
| 14 | + |
| 15 | +contract('Crowdsale', function ([owner, wallet, investor]) { |
| 16 | + |
| 17 | + const RATE = new BigNumber(10); |
| 18 | + const GOAL = ether(10); |
| 19 | + const CAP = ether(20); |
| 20 | + |
| 21 | + beforeEach(async function () { |
| 22 | + this.startBlock = web3.eth.blockNumber + 10; |
| 23 | + this.endBlock = web3.eth.blockNumber + 20; |
| 24 | + |
| 25 | + this.crowdsale = await SampleCrowdsale.new(this.startBlock, this.endBlock, RATE, GOAL, CAP, wallet); |
| 26 | + this.token = SampleCrowdsaleToken.at(await this.crowdsale.token()); |
| 27 | + }); |
| 28 | + |
| 29 | + |
| 30 | + it('should create crowdsale with correct parameters', async function () { |
| 31 | + this.crowdsale.should.exist; |
| 32 | + this.token.should.exist; |
| 33 | + |
| 34 | + (await this.crowdsale.startBlock()).should.be.bignumber.equal(this.startBlock); |
| 35 | + (await this.crowdsale.endBlock()).should.be.bignumber.equal(this.endBlock); |
| 36 | + (await this.crowdsale.rate()).should.be.bignumber.equal(RATE); |
| 37 | + (await this.crowdsale.wallet()).should.be.equal(wallet); |
| 38 | + (await this.crowdsale.goal()).should.be.bignumber.equal(GOAL); |
| 39 | + (await this.crowdsale.cap()).should.be.bignumber.equal(CAP); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should not accept payments before start', async function () { |
| 43 | + await this.crowdsale.send(ether(1)).should.be.rejectedWith(EVMThrow); |
| 44 | + await this.crowdsale.buyTokens(investor, {from: investor, value: ether(1)}).should.be.rejectedWith(EVMThrow); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should accept payments during the sale', async function () { |
| 48 | + const investmentAmount = ether(1); |
| 49 | + const expectedTokenAmount = RATE.mul(investmentAmount); |
| 50 | + |
| 51 | + await advanceToBlock(this.startBlock - 1); |
| 52 | + await this.crowdsale.buyTokens(investor, {value: investmentAmount, from: investor}).should.be.fulfilled; |
| 53 | + |
| 54 | + (await this.token.balanceOf(investor)).should.be.bignumber.equal(expectedTokenAmount); |
| 55 | + (await this.token.totalSupply()).should.be.bignumber.equal(expectedTokenAmount); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should reject payments after end', async function () { |
| 59 | + await advanceToBlock(this.endBlock); |
| 60 | + await this.crowdsale.send(ether(1)).should.be.rejectedWith(EVMThrow); |
| 61 | + await this.crowdsale.buyTokens(investor, {value: ether(1), from: investor}).should.be.rejectedWith(EVMThrow); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should reject payments over cap', async function () { |
| 65 | + await advanceToBlock(this.startBlock - 1); |
| 66 | + await this.crowdsale.send(CAP); |
| 67 | + await this.crowdsale.send(1).should.be.rejectedWith(EVMThrow); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should allow finalization and transfer funds to wallet if the goal is reached', async function () { |
| 71 | + await advanceToBlock(this.endBlock - 1); |
| 72 | + await this.crowdsale.send(GOAL); |
| 73 | + |
| 74 | + const beforeFinalization = web3.eth.getBalance(wallet); |
| 75 | + await this.crowdsale.finalize({from: owner}); |
| 76 | + const afterFinalization = web3.eth.getBalance(wallet); |
| 77 | + |
| 78 | + afterFinalization.minus(beforeFinalization).should.be.bignumber.equal(GOAL); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should allow refunds if the goal is not reached', async function () { |
| 82 | + const balanceBeforeInvestment = web3.eth.getBalance(investor); |
| 83 | + |
| 84 | + await advanceToBlock(this.startBlock - 1); |
| 85 | + await this.crowdsale.sendTransaction({value: ether(1), from: investor, gasPrice: 0}); |
| 86 | + await advanceToBlock(this.endBlock); |
| 87 | + |
| 88 | + await this.crowdsale.finalize({from: owner}); |
| 89 | + await this.crowdsale.claimRefund({from: investor, gasPrice: 0}).should.be.fulfilled; |
| 90 | + |
| 91 | + const balanceAfterRefund = web3.eth.getBalance(investor); |
| 92 | + balanceBeforeInvestment.should.be.bignumber.equal(balanceAfterRefund); |
| 93 | + }); |
| 94 | + |
| 95 | +}); |
0 commit comments