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

Use the TPLToken #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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);
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, because the Registry doesn't have addAttribute, just hasAttribute.
Maybe, the idea is to have a checker that just checks hasAttribute, and a validator that calls addAttribute.
Or maybe we should include addAttribute to Registry, so it's nicer to do both on the same validator.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Registry seems to be the read-only interface of Jurisdiction. 🤷‍♂️

It looks good to me like this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What's weird is that now the validator has a variable called registry and another called jurisdiction, and they point to the same instance.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah, right. I don't see a way out of this. We can't cast the registry variable safely because we don't know if TransactionChecker ever changes it to be something different.

}

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));
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is not clear in the TPL spec, but I think we should be checking transferAllowed(0x0, _to, _amount), i.e. from = 0x0 which is usually how minting is thought of.

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