added factory tests, ability to remove bounties

This commit is contained in:
mbeylin 2017-08-30 18:39:10 -04:00
parent 95f6ca42ea
commit 43ec48cabf
3 changed files with 197 additions and 1 deletions

View File

@ -8,7 +8,27 @@ import "./StandardBounty.sol";
/// @author Gonçalo <goncalo.sa@consensys.net> /// @author Gonçalo <goncalo.sa@consensys.net>
contract BountyFactory is Factory { contract BountyFactory is Factory {
address[] public instances; address[] public instances;
address public owner;
modifier onlyOwner(){
require(msg.sender == owner);
_;
}
modifier correctId(uint _bountyId, address _bountyAddress){
require(instances[_bountyId] == _bountyAddress);
_;
}
modifier correctUser(uint _userId, address _userAddress, address _bountyAddress){
require(instantiations[_userAddress][_userId] == _bountyAddress);
_;
}
/// @dev constructor for the factory
function BountyFactory(){
owner = msg.sender;
}
/// @dev Allows multiple creations of bounties /// @dev Allows multiple creations of bounties
/// @param _deadline the unix timestamp after which fulfillments will no longer be accepted /// @param _deadline the unix timestamp after which fulfillments will no longer be accepted
@ -52,4 +72,29 @@ contract BountyFactory is Factory {
return instances.length; return instances.length;
} }
/// @dev Enables the creator of the factory to remove unwanted bounties
/// @param _bountyId the ID of the bounty
/// @param _bountyAddress the address of the bounty
/// @param _userId the index of the bounty in the user's array
/// @param _userAddress the address of the original bounty
function remove(uint _bountyId, address _bountyAddress, uint _userId, address _userAddress)
public
onlyOwner
correctId(_bountyId, _bountyAddress)
correctUser(_userId, _userAddress, _bountyAddress)
{
delete instances[_bountyId];
isInstantiation[_bountyAddress] = false;
delete instantiations[_userAddress][_userId];
}
/// @dev Enables the creator of the factory transfer ownership
/// @param _newOwner the new address of the owner
function transferOwner(address _newOwner)
public
onlyOwner
{
owner = _newOwner;
}
} }

151
test/bountyFactory.js Normal file
View File

@ -0,0 +1,151 @@
const BountyFactory = artifacts.require("../contracts/BountyFactory.sol");
const StandardBounty = artifacts.require("../contracts/StandardBounty.sol");
const utils = require('./helpers/Utils');
const BN = require(`bn.js`);
contract('BountyFactory', function(accounts) {
it("verifies that the owner is instantiated correctly", async () => {
let manager = await BountyFactory.new({from: accounts[0]});
let owner = await manager.owner();
assert (owner == accounts[0]);
});
it("verifies that ownership can be transferred properly", async () => {
let manager = await BountyFactory.new({from: accounts[0]});
let owner = await manager.owner();
assert (owner == accounts[0]);
await manager.transferOwner(accounts[1], {from: accounts[0]});
owner = await manager.owner();
assert (owner == accounts[1]);
});
it("verifies that only the owner can transfer ownership ", async () => {
let manager = await BountyFactory.new({from: accounts[0]});
let owner = await manager.owner();
assert (owner == accounts[0]);
try {
await manager.transferOwner(accounts[1], {from: accounts[1]});
} catch(error){
return utils.ensureException(error);
}
});
it("verifies that bounty creation works", async () => {
let manager = await BountyFactory.new({from: accounts[0]});
let owner = await manager.owner();
assert (owner == accounts[0]);
let bounty = await manager.create(2528821098,"","",[1000,1000,1000],3000,3,0x0, {from: accounts[0]});
let bounty0 = await manager.instances(0);
assert (bounty.logs[0].args.instantiation == bounty0);
});
it("verifies that bounty creation with incorrect args fails", async () => {
let manager = await BountyFactory.new({from: accounts[0]});
let owner = await manager.owner();
assert (owner == accounts[0]);
try {
await manager.create(2528821098,"","",[0,1000,1000],3000,3,0x0, {from: accounts[0]});
} catch(error){
return utils.ensureException(error);
}
});
it("verifies that bounty removal works", async () => {
let manager = await BountyFactory.new({from: accounts[0]});
let owner = await manager.owner();
assert (owner == accounts[0]);
let bounty = await manager.create(2528821098,"","",[1000,1000,1000],3000,3,0x0, {from: accounts[1]});
let bounty2 = await manager.create(2528821098,"","",[1000,1000,1000],3000,3,0x0, {from: accounts[1]});
let bounty0 = await manager.instances(0);
assert (bounty.logs[0].args.instantiation == bounty0);
let isInstantiation = await manager.isInstantiation(bounty0);
assert (isInstantiation == true);
await manager.remove(0, bounty0, 0, accounts[1]);
isInstantiation = await manager.isInstantiation(bounty0);
assert(isInstantiation == false);
let instance = await manager.instances(0);
assert(instance == "0x0000000000000000000000000000000000000000");
let instantiation = await manager.instantiations(accounts[1], 0);
assert(instantiation == "0x0000000000000000000000000000000000000000");
});
it("verifies that bounty removal fails for anyone but the owner", async () => {
let manager = await BountyFactory.new({from: accounts[0]});
let owner = await manager.owner();
assert (owner == accounts[0]);
let bounty = await manager.create(2528821098,"","",[1000,1000,1000],3000,3,0x0, {from: accounts[1]});
let bounty2 = await manager.create(2528821098,"","",[1000,1000,1000],3000,3,0x0, {from: accounts[1]});
let bounty0 = await manager.instances(0);
assert (bounty.logs[0].args.instantiation == bounty0);
let isInstantiation = await manager.isInstantiation(bounty0);
assert (isInstantiation == true);
try {
await manager.remove(0, bounty0, 0, accounts[1], {from: accounts[1]});
} catch(error){
return utils.ensureException(error);
}
});
it("verifies that bounty removal with wrong IDs fails", async () => {
let manager = await BountyFactory.new({from: accounts[0]});
let owner = await manager.owner();
assert (owner == accounts[0]);
let bounty = await manager.create(2528821098,"","",[1000,1000,1000],3000,3,0x0, {from: accounts[1]});
let bounty2 = await manager.create(2528821098,"","",[1000,1000,1000],3000,3,0x0, {from: accounts[1]});
let bounty0 = await manager.instances(0);
assert (bounty.logs[0].args.instantiation == bounty0);
let isInstantiation = await manager.isInstantiation(bounty0);
assert (isInstantiation == true);
try {
await manager.remove(1, bounty0, 1, accounts[1], {from: accounts[0]});
} catch(error){
return utils.ensureException(error);
}
});
});

View File

@ -2,7 +2,7 @@
function isException(error) { function isException(error) {
let strError = error.toString(); let strError = error.toString();
return strError.includes('invalid opcode') || strError.includes('invalid JUMP'); return strError.includes('invalid opcode') || strError.includes('invalid JUMP') || strError.includes('out of gas');
} }
function ensureException(error) { function ensureException(error) {