Skip to content
This repository has been archived by the owner on Jul 24, 2020. It is now read-only.

Commit

Permalink
Add ContractRegistry contract
Browse files Browse the repository at this point in the history
Event parameters should be named: trufflesuite/truffle#494 (comment)
  • Loading branch information
davidyuk committed Nov 30, 2017
1 parent 678c4a1 commit 39f0bb4
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
36 changes: 36 additions & 0 deletions truffle/contracts/ContractRegistry.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
pragma solidity ^0.4.15;

contract ContractRegistry {
address[] public contracts;
address owner;
address recorder;

modifier onlyOwner {
require(msg.sender == owner);
_;
}

modifier onlyRecorder {
require(msg.sender == recorder);
_;
}

event Added(address contractAddress);

function getContractsCount() constant returns (uint) {
return contracts.length;
}

function ContractRegistry() {
owner = msg.sender;
}

function setRecorder(address _recorder) onlyOwner {
recorder = _recorder;
}

function add(address _contract) onlyRecorder {
contracts.push(_contract);
Added(_contract);
}
}
46 changes: 46 additions & 0 deletions truffle/test/ContractRegistry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* global artifacts contract it assert */

const { assertError } = require('./common');

const ContractRegistry = artifacts.require('ContractRegistry');

const testContractAddress = '0x7e8a8a78e0938cde6fd89ce47c1319b82d5e4022';

contract('ContractRegistry', (accounts) => {
it('can\'t set recorder by not the owner', () =>
ContractRegistry.deployed()
.then(instance => instance.setRecorder(accounts[1], { from: accounts[1] }))
.then(assert.fail, assertError));

it('set recorder', () =>
ContractRegistry.deployed()
.then(instance => instance.setRecorder(accounts[1])));

it('can\'t add contract by not the recorder', () =>
ContractRegistry.deployed()
.then(instance => instance.add(testContractAddress))
.then(assert.fail, assertError));

it('add contract', () =>
ContractRegistry.deployed()
.then(registry =>
registry.setRecorder(accounts[1])
.then(() => registry.add(testContractAddress, { from: accounts[1] }))
.then(() => registry.getContractsCount.call())
.then(contractsCount => assert.equal(contractsCount, 1))
.then(() => registry.contracts.call(0))
.then(contract => assert.equal(contract, testContractAddress))));

it('add contract invokes Added event', () =>
ContractRegistry.deployed()
.then((registry) => {
const filter = registry.Added();
return registry.setRecorder(accounts[0])
.then(() => registry.add(testContractAddress))
.then(() => {
const events = filter.get();
assert.equal(events.length, 1);
assert.equal(events[0].args.contractAddress, testContractAddress);
});
}));
});
6 changes: 6 additions & 0 deletions truffle/test/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* global assert */

module.exports = {
assertError: error =>
assert.equal(error.message, 'VM Exception while processing transaction: invalid opcode'),
};

0 comments on commit 39f0bb4

Please sign in to comment.