Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add contracts for token and crowdsale, and solidity tests #5

Merged
merged 4 commits into from
Apr 18, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions contracts/SampleToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,13 @@ contract SampleToken is MintableToken {
return super.mint(_to, _amount);
}

function transfer(address _to, uint256 _value) public returns (bool) {
require(jurisdiction.hasAttribute(_to, 'VALID'));
return super.transfer(_to, _value);
}

function transferFrom(address _from, address _to, uint256 _value) public returns (bool) {
require(jurisdiction.hasAttribute(_to, 'VALID'));
return super.transferFrom(_from, _to, _value);
}
}
5 changes: 1 addition & 4 deletions test/contracts/SampleCrowdsale.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ contract('SampleCrowdsale', function ([owner, wallet, investor]) {

beforeEach(async function () {
this.jurisdiction = await Jurisdiction.new();
this.jurisdiction.addValidator(owner);
await this.jurisdiction.addValidator(owner);

this.token = await SampleToken.new(this.jurisdiction.address);
this.crowdsale = await SampleCrowdsale.new(
Expand All @@ -24,9 +24,6 @@ contract('SampleCrowdsale', function ([owner, wallet, investor]) {
});

it('should create crowdsale with correct parameters', async function () {
this.crowdsale.should.exist;
this.token.should.exist;

(await this.crowdsale.rate()).should.be.bignumber.equal(RATE);
(await this.crowdsale.wallet()).should.be.equal(wallet);
});
Expand Down
129 changes: 129 additions & 0 deletions test/contracts/SampleToken.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
const Jurisdiction = artifacts.require('Jurisdiction');
const SampleToken = artifacts.require('SampleToken');

const BigNumber = web3.BigNumber;

var should = require('chai')
.use(require('chai-as-promised'))
.use(require('chai-bignumber')(BigNumber))
.should();

contract('SampleToken', function ([owner, investor1, investor2]) {

beforeEach(async function () {
this.jurisdiction = await Jurisdiction.new();
await this.jurisdiction.addValidator(owner);

this.token = await SampleToken.new(this.jurisdiction.address);
await this.token.transferOwnership(owner);
});

it('should not accept not validated minting', async function () {
const investmentAmount = new BigNumber(web3.toWei(1, 'ether'));
await this.token.mint(
investor1, investmentAmount,
{ from: investor1 }
).should.be.rejectedWith('revert');
(await this.token.balanceOf(investor1))
.should.be.bignumber.equal(0);
(await this.token.totalSupply())
.should.be.bignumber.equal(0);
});

it('should accept validated minting', async function () {
await this.jurisdiction.addAttribute(investor1, 'VALID', 1);

const investmentAmount = new BigNumber(web3.toWei(1, 'ether'));
await this.token.mint(
investor1, investmentAmount,
{ from: owner }
).should.be.fulfilled;
(await this.token.balanceOf(investor1))
.should.be.bignumber.equal(investmentAmount);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you're using chai-as-promised you should be able to write

await this.token.balanceOf(investor1).should.eventually.be.bignumber.equal(investmentAmount)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think that reads better? It's a lot longer than adding two parentheses.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It probably doesn't read better. No strong feelings about this.

(await this.token.totalSupply())
.should.be.bignumber.equal(investmentAmount);
});

it('should not accept not validated transfer', async function () {
await this.jurisdiction.addAttribute(investor1, 'VALID', 1);
const investmentAmount = new BigNumber(web3.toWei(1, 'ether'));
await this.token.mint(
investor1, investmentAmount,
{ from: owner }
).should.be.fulfilled;

await this.token.transfer(
investor2, investmentAmount,
{ from: investor1 }
).should.be.rejectedWith('revert');
(await this.token.balanceOf(investor1))
.should.be.bignumber.equal(investmentAmount);
(await this.token.balanceOf(investor2))
.should.be.bignumber.equal(0);
});

it('should accept validated transfer', async function () {
await this.jurisdiction.addAttribute(investor1, 'VALID', 1);
const investmentAmount = new BigNumber(web3.toWei(1, 'ether'));
await this.token.mint(
investor1, investmentAmount,
{ from: owner }
).should.be.fulfilled;

await this.jurisdiction.addAttribute(investor2, 'VALID', 1);
await this.token.transfer(
investor2, investmentAmount,
{ from: investor1 }
).should.be.fulfilled;
(await this.token.balanceOf(investor1))
.should.be.bignumber.equal(0);
(await this.token.balanceOf(investor2))
.should.be.bignumber.equal(investmentAmount);
});

it('should not accept not validated transferFrom', async function () {
await this.jurisdiction.addAttribute(investor1, 'VALID', 1);
const investmentAmount = new BigNumber(web3.toWei(1, 'ether'));
await this.token.mint(
investor1, investmentAmount,
{ from: owner }
).should.be.fulfilled;
await this.token.approve(
owner, investmentAmount,
{ from: investor1 }
).should.be.fulfilled;

await this.token.transferFrom(
investor1, investor2, investmentAmount,
{ from: owner }
).should.be.rejectedWith('revert');
(await this.token.balanceOf(investor1))
.should.be.bignumber.equal(investmentAmount);
(await this.token.balanceOf(investor2))
.should.be.bignumber.equal(0);
});

it('should accept validated transferFrom', async function () {
await this.jurisdiction.addAttribute(investor1, 'VALID', 1);
const investmentAmount = new BigNumber(web3.toWei(1, 'ether'));
await this.token.mint(
investor1, investmentAmount,
{ from: owner }
).should.be.fulfilled;
await this.token.approve(
owner, investmentAmount,
{ from: investor1 }
).should.be.fulfilled;

await this.jurisdiction.addAttribute(investor2, 'VALID', 1);
await this.token.transferFrom(
investor1, investor2, investmentAmount,
{ from: owner }
).should.be.fulfilled;
(await this.token.balanceOf(investor1))
.should.be.bignumber.equal(0);
(await this.token.balanceOf(investor2))
.should.be.bignumber.equal(investmentAmount);
});

});