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 all 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
17 changes: 17 additions & 0 deletions contracts/SampleCrowdsale.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
pragma solidity ^0.4.18;

import "zeppelin-solidity/contracts/crowdsale/Crowdsale.sol";
import "zeppelin-solidity/contracts/crowdsale/emission/MintedCrowdsale.sol";
import "zeppelin-solidity/contracts/token/ERC20/MintableToken.sol";

contract SampleCrowdsale is MintedCrowdsale {
function SampleCrowdsale(
uint256 _rate,
address _wallet,
MintableToken _token
)
public
Crowdsale(_rate, _wallet, _token)
{
}
}
42 changes: 42 additions & 0 deletions contracts/SampleToken.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
pragma solidity ^0.4.18;

import "zeppelin-solidity/contracts/token/ERC20/MintableToken.sol";
import { Jurisdiction } from "./tpl-contracts/Jurisdiction.sol";

/**
* @title SampleToken
* @dev Mintable ERC20 Token.
*/
contract SampleToken is MintableToken {

Jurisdiction jurisdiction;

string public constant name = "SampleToken";
string public constant symbol = "TPL";
uint8 public constant decimals = 18;

function SampleToken(Jurisdiction _jurisdiction) public {
jurisdiction = _jurisdiction;
totalSupply_ = 0;
}

function mint(address _to, uint256 _amount)
onlyOwner
canMint
public
returns (bool)
{
require(jurisdiction.hasAttribute(_to, 'VALID'));
Copy link
Collaborator

Choose a reason for hiding this comment

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

This needs to be checked in transfer and transferFrom too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for catching this!
I added the overwrites, and tests specific to the token.

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);
}
}
13 changes: 0 additions & 13 deletions migrations/2_jurisdiction.js

This file was deleted.

15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
},
"devDependencies": {
"chai": "^4.1.2",
"chai-as-promised": "^7.1.1",
"chai-bignumber": "^2.0.2",
"enzyme": "^3.3.0",
"enzyme-adapter-react-16": "^1.1.1",
"jsdom": "^11.7.0",
Expand All @@ -31,7 +33,9 @@
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "mocha --require babel-core/register --require test/setup.js",
"test": "npm run test:react && npm run test:contracts",
"test:contracts": "truffle test test/contracts/* --network unexisting",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I found this weird. If I didn't set network, or set it to some existing network, it would say that the contract couldn't be deployed because of gas.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think that by default the test network will be the development network which has a gasLimit. Did you try a higher one?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. But then I had plenty of problems with deploying the contracts to geth, and I read something about a wrong constructor. I made many changes back and forth, until it started working, so I'm not sure what was wrong.
I reported #7, to investigate this.

"test:react": "mocha test/react --require babel-core/register --require test/react/setup.js",
"eject": "react-scripts eject"
}
}
18 changes: 18 additions & 0 deletions test/contracts/DummyValidator.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const Jurisdiction = artifacts.require('Jurisdiction');
const DummyValidator = artifacts.require('DummyValidator');

var should = require('chai').should();

contract('DummyValidator', function ([investor]) {

beforeEach(async function () {
this.jurisdiction = await Jurisdiction.new();
this.dummyValidator = await DummyValidator.new(this.jurisdiction.address);
await this.jurisdiction.addValidator(this.dummyValidator.address);
});

it('should validate', async function () {
await this.dummyValidator.validate({from: investor});
(await this.jurisdiction.hasAttribute(investor, 'VALID')).should.be.true;
});
});
56 changes: 56 additions & 0 deletions test/contracts/SampleCrowdsale.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const Jurisdiction = artifacts.require('Jurisdiction');
const SampleToken = artifacts.require('SampleToken');
const SampleCrowdsale = artifacts.require('SampleCrowdsale');

const BigNumber = web3.BigNumber;

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

contract('SampleCrowdsale', function ([owner, wallet, investor]) {

const RATE = new BigNumber(1);

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

this.token = await SampleToken.new(this.jurisdiction.address);
this.crowdsale = await SampleCrowdsale.new(
RATE, wallet, this.token.address);
await this.token.transferOwnership(this.crowdsale.address);
});

it('should create crowdsale with correct parameters', async function () {
(await this.crowdsale.rate()).should.be.bignumber.equal(RATE);
(await this.crowdsale.wallet()).should.be.equal(wallet);
});

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

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

const investmentAmount = new BigNumber(web3.toWei(1, 'ether'));
await this.crowdsale.buyTokens(
investor,
{ value: investmentAmount, from: investor }
).should.be.fulfilled;
(await this.token.balanceOf(investor))
.should.be.bignumber.equal(investmentAmount);
(await this.token.totalSupply())
.should.be.bignumber.equal(investmentAmount);
});
});
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);
});

});
File renamed without changes.
2 changes: 1 addition & 1 deletion test/test.js → test/react/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { expect } from 'chai';
import { shallow } from 'enzyme';
import { spy } from 'sinon';
import { createMockStore } from 'redux-test-utils';
import ValidatorsList from '../src/components/ValidatorsList.react';
import ValidatorsList from '../../src/components/ValidatorsList.react';

spy(ValidatorsList.prototype, 'componentDidMount');

Expand Down