Skip to content

Commit

Permalink
feat: move out some test suites to external set
Browse files Browse the repository at this point in the history
  • Loading branch information
Hokid committed Oct 20, 2018
1 parent b1334f0 commit 45a96be
Show file tree
Hide file tree
Showing 4 changed files with 314 additions and 341 deletions.
215 changes: 1 addition & 214 deletions test/W12Crowdsale.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ contract('W12Crowdsale', async (accounts) => {
oneOriginToken = new BigNumber(10).pow(await firstCrowdsaleOriginToken.decimals());
});

describe('with discounts stages', async () => {
describe('buy', async () => {
beforeEach(async () => {
discountStages = defaultStagesGenerator([
{
Expand Down Expand Up @@ -648,164 +648,6 @@ contract('W12Crowdsale', async (accounts) => {
endDate = discountStages[2].dates[1];
});

describe('should successful sell token on each crowdsale', async () => {
let stage;
const buyAmount = 10;
const buyPrice = buyAmount * price;

beforeEach(async () => {
stage = discountStages[0];

await utils.time.increaseTimeTo(stage.dates[0] + 10);
});

let counter = 0;
for (const {crowdsale} of crowdsales) {
it(`crowdsale#${counter}`, async () => {
await crowdsale.buyTokens({ value: buyPrice, from: buyer })
.should.be.fulfilled;
});
}
});

describe('purchase steps', async () => {
const buyAmount = 10;
const saleFeeAmount = buyAmount * (saleFee / 100);
const buyPrice = buyAmount * price;
const serviceFeeAmount = buyPrice * (serviceFee / 100);

let stage;
let crowdsale;
let crowdsaleFund;
let wtoken;
let originToken;
let oneWToken;
let oneOriginToken;

beforeEach(async () => {
stage = discountStages[0];
crowdsale = crowdsales[0].crowdsale;
crowdsaleFund = crowdsales[0].fund;
wtoken = crowdsales[0].wtoken;
originToken = crowdsales[0].originToken;
oneWToken = new BigNumber(10).pow(await wtoken.decimals());
oneOriginToken = new BigNumber(10).pow(await originToken.decimals());

await utils.time.increaseTimeTo(stage.dates[0] + 10);
});

it('should fill buyer balance', async () => {
const before = await wtoken.balanceOf(buyer);
const expected = before.plus(oneWToken.mul(buyAmount));

await crowdsale.buyTokens({ value: buyPrice, from: buyer });

(await wtoken.balanceOf(buyer))
.should.bignumber.equal(expected);
});

it('should send sale fee to service wallet', async () => {
const before = await originToken.balanceOf(serviceWallet);
const expected = before.plus(oneOriginToken.mul(saleFeeAmount));

await crowdsale.buyTokens({value: buyPrice, from: buyer});

(await originToken.balanceOf(serviceWallet))
.should.bignumber.equal(expected);
});

it('should spend sale fee from swap', async () => {
const before = await originToken.balanceOf(swap);
const expected = before.minus(oneOriginToken.mul(saleFeeAmount));

await crowdsale.buyTokens({value: buyPrice, from: buyer});

(await originToken.balanceOf(swap))
.should.bignumber.equal(expected);
});

it('should send wtokens from crowdsale address', async () => {
const before = await wtoken.balanceOf(crowdsale.address);
const expected = before
// commission
.minus(oneWToken.mul(saleFeeAmount))
// purchase amount
.minus(oneWToken.mul(buyAmount))

await crowdsale.buyTokens({value: buyPrice, from: buyer});

(await wtoken.balanceOf(crowdsale.address))
.should.bignumber.equal(expected);
});

it('should send fee to swap', async () => {
const before = await wtoken.balanceOf(swap);
const expected = before.plus(oneWToken.mul(saleFeeAmount));

await crowdsale.buyTokens({value: buyPrice, from: buyer});

(await wtoken.balanceOf(swap))
.should.bignumber.equal(expected);
});

it('should fill fund', async () => {
const before = await web3.eth.getBalance(crowdsaleFund.address);
const expected = before
.plus(buyPrice)
.minus(serviceFeeAmount);

await crowdsale.buyTokens({value: buyPrice, from: buyer});

(await web3.eth.getBalance(crowdsaleFund.address))
.should.bignumber.equal(expected);
});

it('should send fee in wei to service wallet', async () => {
const before = await web3.eth.getBalance(serviceWallet);
const expected = before
.plus(serviceFeeAmount);

await crowdsale.buyTokens({value: buyPrice, from: buyer});

(await web3.eth.getBalance(serviceWallet))
.should.bignumber.equal(expected);
});
});

it('should return change', async () => {
const stage = discountStages[0];
const buyTokenAmount = 200;
const buyValueWei = new BigNumber(price * buyTokenAmount);
const expectedChange = buyValueWei.div(2);
const expectedWBalance = oneWToken.mul(100);
const buyerBalanceBefore = await web3.eth.getBalance(buyer);

await utils.time.increaseTimeTo(stage.dates[0] + 10);

await firstCrowdsale._outTokens(
notBuyer,
new BigNumber(mint).minus(expectedWBalance),
{from: owner}
)
.should.be.fulfilled;

await firstCrowdsale._setState(0, {from: owner})
.should.be.fulfilled;

const receipt = await firstCrowdsale.buyTokens({value: buyValueWei, from: buyer}).should.be.fulfilled;
const cost = await utils.getTransactionCost(receipt);
const expectedBalance = buyerBalanceBefore
.minus(cost)
.minus(buyValueWei)
.plus(expectedChange);

(await firstCrowdsaleWToken.balanceOf(buyer))
.should.bignumber.equal(expectedWBalance);

(await web3.eth.getBalance(buyer))
.should.bignumber.equal(expectedBalance);
});

it('should not sell some tokens if sale is not active', async () => {
const someStage1 = discountStages[1];
const someStage2 = discountStages[2];
Expand Down Expand Up @@ -880,60 +722,5 @@ contract('W12Crowdsale', async (accounts) => {
});
});
});

describe('volume bonuses', async () => {
const expected = [
// boundary, bonus, testWei
[new BigNumber(0), new BigNumber(utils.toInternalPercent(0)), new BigNumber(10000)], // no bonus

[new BigNumber(10000000), new BigNumber(utils.toInternalPercent(5)), new BigNumber(10000000)],
[new BigNumber(100000000), new BigNumber(utils.toInternalPercent(10)), new BigNumber(100000001)]
];
let stage;

beforeEach(async () => {
const stages = defaultStagesGenerator({
dates: [
startDate + utils.time.duration.minutes(80),
startDate + utils.time.duration.minutes(90)
],
volumeBoundaries: expected.slice(1).map(item => item[0]),
volumeBonuses: expected.slice(1).map(item => item[1])
});
stage = stages[0];

const params = utils.packSetupCrowdsaleParameters(stages, milestonesDefaultFixture(startDate));

await firstCrowdsale.setup(...params, {from: owner})
.should.be.fulfilled;

await utils.time.increaseTimeTo(stage.dates[0]);
});

for (const itemIdx in expected) {
const item = expected[+itemIdx];
const volume = item[0];
const percent = item[1];
const wei = item[2];

const nextVolume = +itemIdx != (expected.length - 1) ? expected[+itemIdx + 1][0] : 'Infinity';

it(`should sell tokens for volume in [${volume.toString()}, ${nextVolume.toString()}) with bonus ${utils.fromInternalPercent(percent.toNumber())}%`, async () => {
const tokens = utils.calculatePurchase(
wei
, price
, BigNumber.Zero
, percent
, await firstCrowdsaleWToken.decimals()
);

await firstCrowdsale.buyTokens({ value: wei, from: buyer })
.should.be.fulfilled;

(await firstCrowdsaleWToken.balanceOf(buyer))
.should.bignumber.equal(tokens);
});
}
});
});
});
Loading

0 comments on commit 45a96be

Please sign in to comment.