Skip to content

Commit

Permalink
Refactor Org related code
Browse files Browse the repository at this point in the history
  • Loading branch information
mds1 committed Aug 6, 2020
1 parent 1ddf568 commit 7a18125
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 153 deletions.
24 changes: 13 additions & 11 deletions contracts/Org.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import "./Administratable.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";

interface IOrgFactory {
function endaomentAdmin() external view returns (address);
}

//ORG CONTRACT
/**
* @title Org
Expand All @@ -28,6 +32,7 @@ contract Org is Administratable {
bool filesSubmitted;
}

IOrgFactory public orgFactoryContract;
uint256 public taxId;
address public orgWallet;
Claim[] public claims;
Expand All @@ -41,14 +46,13 @@ contract Org is Administratable {
/**
* @notice Create new Organization Contract
* @param ein The U.S. Tax Identification Number for the Organization
* @param adminContractAddress Contract Address for Endaoment Admin
* @param orgFactory Address of the Factory contract.
*/
constructor(uint256 ein, address adminContractAddress)
public
onlyAdminOrRole(adminContractAddress, IEndaomentAdmin.Role.ORG_FACTORY)
{
constructor(uint256 ein, address orgFactory) public {
require(ein >= 10000000 && ein <= 999999999, "Org: Must provide a valid EIN");
require(orgFactory != address(0), "Org: Factory cannot be null address.");
taxId = ein;
orgFactoryContract = IOrgFactory(orgFactory);
}

// ========== Org Management & Info ==========
Expand Down Expand Up @@ -84,11 +88,10 @@ contract Org is Administratable {
/**
* @notice Approving Organization Claim
* @param index Index value of Claim.
* @param adminContractAddress Contract Address for Endaoment Admin
*/
function approveClaim(uint256 index, address adminContractAddress)
function approveClaim(uint256 index)
public
onlyAdminOrRole(adminContractAddress, IEndaomentAdmin.Role.REVIEWER)
onlyAdminOrRole(orgFactoryContract.endaomentAdmin(), IEndaomentAdmin.Role.REVIEWER)
{
require(index < claims.length, "Org: Index out of range");
Claim storage claim = claims[index];
Expand All @@ -99,11 +102,10 @@ contract Org is Administratable {
/**
* @notice Cashing out Organization Contract
* @param tokenAddress Stablecoin address of desired token withdrawal
* @param adminContractAddress Contract Address for Endaoment Admin
*/
function cashOutOrg(address tokenAddress, address adminContractAddress)
function cashOutOrg(address tokenAddress)
public
onlyAdminOrRole(adminContractAddress, IEndaomentAdmin.Role.ACCOUNTANT)
onlyAdminOrRole(orgFactoryContract.endaomentAdmin(), IEndaomentAdmin.Role.ACCOUNTANT)
{
require(tokenAddress != address(0), "Org: Token address cannot be the zero address");
IERC20 tokenContract = IERC20(tokenAddress);
Expand Down
29 changes: 24 additions & 5 deletions contracts/OrgFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,47 @@ import "./Org.sol";
*/
contract OrgFactory is Administratable {
// ========== STATE VARIABLES==========
address public endaomentAdmin;
Org[] public deployedOrgs;
mapping(address => bool) public allowedOrgs;

event EndaomentAdminChanged(address indexed oldAddress, address indexed newAddress);
event OrgCreated(address indexed newAddress);

// ========== CONSTRUCTOR ==========
/**
* @notice Create new Org Factory
* @param adminContractAddress Address of EndaomentAdmin contract.
*/
constructor(address adminContractAddress) public onlyAdmin(adminContractAddress) {}
constructor(address adminContractAddress) public {
endaomentAdmin = adminContractAddress;
emit EndaomentAdminChanged(address(0), adminContractAddress);
}

/**
* @notice Update address of the endaomentAdmin contract
* @param newAdmin New address of the endaomentAdmin contract
*/
function updateEndaomentAdmin(address newAdmin) public onlyAdmin(endaomentAdmin) {
// Validate that contract has a valid admin address set
EndaomentAdmin endaomentAdminContract = EndaomentAdmin(newAdmin);
address admin = endaomentAdminContract.getRoleAddress(IEndaomentAdmin.Role.ADMIN);
require(admin != address(0), "OrgFactory: Admin cannot be the zero address");

emit EndaomentAdminChanged(endaomentAdmin, newAdmin);
endaomentAdmin = newAdmin;
}

// ========== Org Creation & Management ==========
/**
* @notice Create new Org Contract
* @param ein The U.S. Tax Identification Number for the Organization
* @param adminContractAddress Contract address for Endaoment Admin
*/
function createOrg(uint256 ein, address adminContractAddress)
function createOrg(uint256 ein)
public
onlyAdminOrRole(adminContractAddress, IEndaomentAdmin.Role.ACCOUNTANT)
onlyAdminOrRole(endaomentAdmin, IEndaomentAdmin.Role.ACCOUNTANT)
{
Org newOrg = new Org(ein, adminContractAddress);
Org newOrg = new Org(ein, address(this));
deployedOrgs.push(newOrg);
allowedOrgs[address(newOrg)] = true;
emit OrgCreated(address(newOrg));
Expand Down
36 changes: 6 additions & 30 deletions test/Fund.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,7 @@ describe("Fund", function () {
await this.endaomentAdmin.setRole(5, orgFactory.address, { from: admin });

//Deploy an org contract using the OrgFactory
const org = await orgFactory.createOrg(
123456789,
this.endaomentAdmin.address,
{ from: accountant }
);
const org = await orgFactory.createOrg(123456789, {from: accountant});

//Check that the new org's address passes the checkRecipient() function on the Fund contract
const orgChecked = await this.fund.checkRecipient(
Expand Down Expand Up @@ -194,11 +190,7 @@ describe("Fund", function () {
});

//Deploy an org contract using the OrgFactory
const org = await orgFactory.createOrg(
123456789,
this.endaomentAdmin.address,
{ from: accountant }
);
const org = await orgFactory.createOrg(123456789, {from: accountant});

//Create new grant on the Fund contract
const createGrantReceipt = await this.fund.createGrant(
Expand Down Expand Up @@ -255,11 +247,7 @@ describe("Fund", function () {
const orgFactory = await OrgFactory.new(this.endaomentAdmin.address, { from: admin });
await this.endaomentAdmin.setRole(5, orgFactory.address, { from: admin });

const org = await orgFactory.createOrg(
123456789,
this.endaomentAdmin.address,
{ from: accountant }
);
const org = await orgFactory.createOrg(123456789, {from: accountant});

await this.fund.createGrant(
"test grant",
Expand All @@ -278,11 +266,7 @@ describe("Fund", function () {
const orgFactory = await OrgFactory.new(this.endaomentAdmin.address, { from: admin });
await this.endaomentAdmin.setRole(5, orgFactory.address, { from: admin });

const org = await orgFactory.createOrg(
123456789,
this.endaomentAdmin.address,
{ from: accountant }
);
const org = await orgFactory.createOrg(123456789, {from: accountant});

await this.token.transfer(fund.address, 100, { from: initHolder });

Expand Down Expand Up @@ -317,11 +301,7 @@ describe("Fund", function () {
const orgFactory = await OrgFactory.new(this.endaomentAdmin.address, { from: admin });
await this.endaomentAdmin.setRole(5, orgFactory.address, { from: admin });

const org = await orgFactory.createOrg(
123456789,
this.endaomentAdmin.address,
{ from: accountant }
);
const org = await orgFactory.createOrg(123456789, {from: accountant});

await this.token.transfer(fund.address, 100, { from: initHolder });

Expand Down Expand Up @@ -356,11 +336,7 @@ describe("Fund", function () {
const orgFactory = await OrgFactory.new(this.endaomentAdmin.address, { from: admin });
await this.endaomentAdmin.setRole(5, orgFactory.address, { from: admin });

const org = await orgFactory.createOrg(
123456789,
this.endaomentAdmin.address,
{ from: accountant }
);
const org = await orgFactory.createOrg(123456789, {from: accountant});

await this.token.transfer(fund.address, 100, { from: initHolder });

Expand Down
Loading

0 comments on commit 7a18125

Please sign in to comment.