Skip to content

Commit

Permalink
Use the TPLToken
Browse files Browse the repository at this point in the history
  • Loading branch information
come-maiz committed Jun 2, 2018
1 parent 75a8d32 commit 87a34c3
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 34 deletions.
20 changes: 18 additions & 2 deletions contracts/DummyValidator.sol
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
pragma solidity ^0.4.21;

import { Jurisdiction } from "./tpl-contracts/Jurisdiction.sol";
import { TransactionChecker } from "./tpl-contracts/checks/TransactionChecker.sol";


contract DummyValidator {
contract DummyValidator is TransactionChecker {

Jurisdiction jurisdiction;

function DummyValidator(Jurisdiction _jurisdiction) public {
function DummyValidator(Jurisdiction _jurisdiction)
TransactionChecker(_jurisdiction)
public
{
jurisdiction = _jurisdiction;
}

function validate() public {
jurisdiction.addAttribute(msg.sender, "VALID", 1);
}

function transferAllowed(
address _from,
address _to,
uint256 _value
)
public
view
returns (bool)
{
return registry.hasAttribute(_to, "VALID");
}

}
24 changes: 8 additions & 16 deletions contracts/SampleToken.sol
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
pragma solidity ^0.4.18;

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


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

Jurisdiction jurisdiction;
contract SampleToken is TPLToken, MintableToken {

string public constant name = "SampleToken"; // solium-disable-line uppercase
string public constant symbol = "TPL"; // solium-disable-line uppercase
uint8 public constant decimals = 18; // solium-disable-line uppercase

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

Expand All @@ -27,17 +28,8 @@ contract SampleToken is MintableToken {
public
returns (bool)
{
require(jurisdiction.hasAttribute(_to, "VALID"));
require(validator.transferAllowed(msg.sender, _to, _amount));
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);
}
}
2 changes: 1 addition & 1 deletion scripts/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async function main() {
await jurisdiction.addValidator(validator.address);

const SampleToken = getContract('SampleToken', 2000000);
const sampleToken = await SampleToken.new(jurisdiction.address);
const sampleToken = await SampleToken.new(validator.address);
console.log("SampleToken\t" + sampleToken.address);

const SampleCrowdsale = getContract('SampleCrowdsale', 2000000);
Expand Down
17 changes: 14 additions & 3 deletions test/contracts/DummyValidator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const DummyValidator = artifacts.require('DummyValidator');

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

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

beforeEach(async function () {
this.jurisdiction = await Jurisdiction.new();
Expand All @@ -12,7 +12,18 @@ contract('DummyValidator', function ([investor]) {
});

it('should validate', async function () {
await this.dummyValidator.validate({from: investor});
(await this.jurisdiction.hasAttribute(investor, 'VALID')).should.be.true;
await this.dummyValidator.validate({from: investor1});
(await this.jurisdiction.hasAttribute(investor1, 'VALID')).should.be.true;
});

it('should reject not validated transfer', async function() {
(await this.dummyValidator.transferAllowed(
investor1, investor2, 10)).should.be.false;
});

it('should allow validated transfer', async function() {
await this.dummyValidator.validate({from: investor2});
(await this.dummyValidator.transferAllowed(
investor1, investor2, 10)).should.be.true;
});
});
9 changes: 6 additions & 3 deletions test/contracts/SampleCrowdsale.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const DummyValidator = artifacts.require('DummyValidator');
const Jurisdiction = artifacts.require('Jurisdiction');
const SampleToken = artifacts.require('SampleToken');
const SampleCrowdsale = artifacts.require('SampleCrowdsale');
Expand All @@ -15,9 +16,11 @@ contract('SampleCrowdsale', function ([owner, wallet, investor]) {

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

this.token = await SampleToken.new(this.jurisdiction.address);
this.validator = await DummyValidator.new(this.jurisdiction.address);
await this.jurisdiction.addValidator(this.validator.address);

this.token = await SampleToken.new(this.validator.address);
this.crowdsale = await SampleCrowdsale.new(
RATE, wallet, this.token.address);
await this.token.transferOwnership(this.crowdsale.address);
Expand All @@ -41,7 +44,7 @@ contract('SampleCrowdsale', function ([owner, wallet, investor]) {
});

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

const investmentAmount = new BigNumber(web3.toWei(1, 'ether'));
await this.crowdsale.buyTokens(
Expand Down
21 changes: 12 additions & 9 deletions test/contracts/SampleToken.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const DummyValidator = artifacts.require('DummyValidator');
const Jurisdiction = artifacts.require('Jurisdiction');
const SampleToken = artifacts.require('SampleToken');

Expand All @@ -12,9 +13,11 @@ 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);
this.validator = await DummyValidator.new(this.jurisdiction.address);
await this.jurisdiction.addValidator(this.validator.address);

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

Expand All @@ -31,7 +34,7 @@ contract('SampleToken', function ([owner, investor1, investor2]) {
});

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

const investmentAmount = new BigNumber(web3.toWei(1, 'ether'));
await this.token.mint(
Expand All @@ -45,7 +48,7 @@ contract('SampleToken', function ([owner, investor1, investor2]) {
});

it('should not accept not validated transfer', async function () {
await this.jurisdiction.addAttribute(investor1, 'VALID', 1);
await this.validator.validate({from: investor1});
const investmentAmount = new BigNumber(web3.toWei(1, 'ether'));
await this.token.mint(
investor1, investmentAmount,
Expand All @@ -63,14 +66,14 @@ contract('SampleToken', function ([owner, investor1, investor2]) {
});

it('should accept validated transfer', async function () {
await this.jurisdiction.addAttribute(investor1, 'VALID', 1);
await this.validator.validate({from: investor1});
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.validator.validate({from: investor2});
await this.token.transfer(
investor2, investmentAmount,
{ from: investor1 }
Expand All @@ -82,7 +85,7 @@ contract('SampleToken', function ([owner, investor1, investor2]) {
});

it('should not accept not validated transferFrom', async function () {
await this.jurisdiction.addAttribute(investor1, 'VALID', 1);
await this.validator.validate({from: investor1});
const investmentAmount = new BigNumber(web3.toWei(1, 'ether'));
await this.token.mint(
investor1, investmentAmount,
Expand All @@ -104,7 +107,7 @@ contract('SampleToken', function ([owner, investor1, investor2]) {
});

it('should accept validated transferFrom', async function () {
await this.jurisdiction.addAttribute(investor1, 'VALID', 1);
await this.validator.validate({from: investor1});
const investmentAmount = new BigNumber(web3.toWei(1, 'ether'));
await this.token.mint(
investor1, investmentAmount,
Expand All @@ -115,7 +118,7 @@ contract('SampleToken', function ([owner, investor1, investor2]) {
{ from: investor1 }
).should.be.fulfilled;

await this.jurisdiction.addAttribute(investor2, 'VALID', 1);
await this.validator.validate({from: investor2});
await this.token.transferFrom(
investor1, investor2, investmentAmount,
{ from: owner }
Expand Down

0 comments on commit 87a34c3

Please sign in to comment.