Skip to content

Commit

Permalink
Migrate makeTask helper functions to Expenditures III
Browse files Browse the repository at this point in the history
  • Loading branch information
kronosapiens committed Aug 11, 2023
1 parent c5d8c54 commit dd94d84
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 102 deletions.
101 changes: 34 additions & 67 deletions helpers/test-data-generator.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* globals artifacts */
const BN = require("bn.js");
const { ethers } = require("ethers");

const { UINT256_MAX, MANAGER_PAYOUT, EVALUATOR_PAYOUT, WORKER_PAYOUT, INITIAL_FUNDING } = require("./constants");
const { UINT256_MAX, GLOBAL_SKILL_ID, MANAGER_PAYOUT, EVALUATOR_PAYOUT, WORKER_PAYOUT, INITIAL_FUNDING } = require("./constants");
const { getTokenArgs, web3GetAccounts, getChildSkillIndex, web3SignTypedData } = require("./test-helper");

const IColony = artifacts.require("IColony");
Expand All @@ -17,139 +16,107 @@ const Resolver = artifacts.require("Resolver");
const MetaTxToken = artifacts.require("MetaTxToken");
const IColonyNetwork = artifacts.require("IColonyNetwork");

exports.makeExpenditure = async function makeExpenditure({ colonyNetwork, colony, domainId = 1, owner }) {
const accounts = await web3GetAccounts();
owner = owner || accounts[0]; // eslint-disable-line no-param-reassign

// Only Colony admins are allowed to make Expenditures, make the account an admin
let networkAddress;
exports.makeExpenditure = async function makeExpenditure({
colonyNetwork,
colony,
domainId = 1,
skillId = GLOBAL_SKILL_ID,
manager,
evaluator,
worker,
}) {
if (colonyNetwork === undefined) {
networkAddress = await colony.getColonyNetwork();
const networkAddress = await colony.getColonyNetwork();
colonyNetwork = await IColonyNetwork.at(networkAddress); // eslint-disable-line no-param-reassign
}
const childSkillIndex = await getChildSkillIndex(colonyNetwork, colony, 1, domainId);
await colony.setAdministrationRole(1, childSkillIndex, owner, domainId, true);

const { logs } = await colony.makeExpenditure(1, childSkillIndex, domainId, { from: owner });
// Reading the ID out of the event triggered by our transaction will allow us to make multiple tasks in parallel in the future.
return logs.filter((log) => log.event === "ExpenditureAdded")[0].args.expenditureId;
};

exports.setupAssignedExpenditure = async function setupAssignedExpenditure({ colonyNetwork, colony, domainId, manager, evaluator, worker }) {
const accounts = await web3GetAccounts();
manager = manager || accounts[0]; // eslint-disable-line no-param-reassign
evaluator = evaluator || manager; // eslint-disable-line no-param-reassign
worker = worker || accounts[2]; // eslint-disable-line no-param-reassign

const expenditureId = await exports.makeExpenditure({ colonyNetwork, colony, domainId, manager });
await colony.setExpenditureRecipients(expenditureId, [0, 1, 2], [manager, evaluator, worker]);
// Only Colony admins are allowed to make Expenditures, make the account an admin
const childSkillIndex = await getChildSkillIndex(colonyNetwork, colony, 1, domainId);
await colony.setAdministrationRole(1, childSkillIndex, manager, domainId, true);

const { logs } = await colony.makeExpenditure(1, childSkillIndex, domainId, { from: manager });
const { expenditureId } = logs.filter((log) => log.event === "ExpenditureAdded")[0].args;

await colony.setExpenditureRecipients(expenditureId, [0, 1, 2], [manager, evaluator, worker], { from: manager });
await colony.setExpenditureSkills(expenditureId, [2], [skillId], { from: manager });

return expenditureId;
};

exports.setupFundedExpenditure = async function setupFundedExpenditure({
colonyNetwork,
colony,
token,
domainId,
skillId,
manager,
evaluator,
worker,
tokenAddress,
managerPayout = MANAGER_PAYOUT,
evaluatorPayout = EVALUATOR_PAYOUT,
workerPayout = WORKER_PAYOUT,
}) {
const accounts = await web3GetAccounts();
manager = manager || accounts[0]; // eslint-disable-line no-param-reassign
evaluator = evaluator || manager; // eslint-disable-line no-param-reassign
worker = worker || accounts[2]; // eslint-disable-line no-param-reassign

let tokenAddress;
if (token === undefined) {
tokenAddress = await colony.getToken();
} else {
tokenAddress = token === ethers.constants.AddressZero ? ethers.constants.AddressZero : token.address;
if (tokenAddress === undefined) {
tokenAddress = await colony.getToken(); // eslint-disable-line no-param-reassign
}

const expenditureId = await exports.makeExpenditure({ colonyNetwork, colony, domainId, manager });
await colony.setExpenditureRecipients(expenditureId, [0, 1, 2], [manager, evaluator, worker]);
const expenditureId = await exports.makeExpenditure({ colonyNetwork, colony, domainId, skillId, manager, evaluator, worker });

const expenditure = await colony.getExpenditure(expenditureId);
const childSkillIndex = await getChildSkillIndex(colonyNetwork, colony, 1, expenditure.domainId);
const moveFundsBetweenPots = colony.methods["moveFundsBetweenPots(uint256,uint256,uint256,uint256,uint256,uint256,uint256,uint256,address)"];

await colony.setFundingRole(1, UINT256_MAX, manager, 1, true);
const totalPayouts = new BN(managerPayout).add(new BN(evaluatorPayout)).add(new BN(workerPayout));

await colony.setFundingRole(1, UINT256_MAX, manager, 1, true);
await moveFundsBetweenPots(1, UINT256_MAX, 1, UINT256_MAX, childSkillIndex, 1, expenditure.fundingPotId, totalPayouts, tokenAddress, {
from: manager,
});

await colony.setExpenditurePayouts(expenditureId, [0, 1, 2], tokenAddress, [managerPayout, evaluatorPayout, workerPayout], { from: manager });
return expenditureId;
};

exports.setupFinalizedExpenditure = async function setupFinalizedExpenditure({
colonyNetwork,
colony,
token,
domainId,
manager,
evaluator,
worker,
managerPayout,
evaluatorPayout,
workerPayout,
}) {
const expenditureId = await exports.setupFundedExpenditure({
colonyNetwork,
colony,
token,
domainId,
manager,
evaluator,
worker,
managerPayout,
evaluatorPayout,
workerPayout,
});

await colony.finalizeExpenditure(expenditureId);
return expenditureId;
};

exports.setupClaimedExpenditure = async function setupClaimedExpenditure({
colonyNetwork,
colony,
token,
domainId,
skillId,
manager,
evaluator,
worker,
tokenAddress,
managerPayout,
evaluatorPayout,
workerPayout,
}) {
let tokenAddress;
if (token === undefined) {
tokenAddress = await colony.getToken();
} else {
tokenAddress = token === ethers.constants.AddressZero ? ethers.constants.AddressZero : token.address;
if (tokenAddress === undefined) {
tokenAddress = await colony.getToken(); // eslint-disable-line no-param-reassign
}

const expenditureId = await exports.setupFinalizedExpenditure({
const expenditureId = await exports.setupFundedExpenditure({
colonyNetwork,
colony,
token,
domainId,
skillId,
manager,
evaluator,
worker,
tokenAddress,
managerPayout,
evaluatorPayout,
workerPayout,
});

await colony.finalizeExpenditure(expenditureId, { from: manager });
await colony.claimExpenditurePayout(expenditureId, 0, tokenAddress);
await colony.claimExpenditurePayout(expenditureId, 1, tokenAddress);
await colony.claimExpenditurePayout(expenditureId, 2, tokenAddress);
Expand Down
11 changes: 7 additions & 4 deletions test/contracts-network/colony-funding.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { ethers } = require("ethers");

const { UINT256_MAX, WAD, MANAGER_PAYOUT, EVALUATOR_PAYOUT, WORKER_PAYOUT, INITIAL_FUNDING } = require("../../helpers/constants");

const { fundColonyWithTokens, setupRandomColony, makeExpenditure, setupFinalizedExpenditure } = require("../../helpers/test-data-generator");
const { fundColonyWithTokens, setupRandomColony, makeExpenditure, setupFundedExpenditure } = require("../../helpers/test-data-generator");
const { getTokenArgs, checkErrorRevert, web3GetBalance, removeSubdomainLimit } = require("../../helpers/test-helper");

const { expect } = chai;
Expand Down Expand Up @@ -374,7 +374,8 @@ contract("Colony Funding", (accounts) => {

it("should not allow funds to be removed from a expenditure with payouts to go", async () => {
await fundColonyWithTokens(colony, otherToken, INITIAL_FUNDING);
const expenditureId = await setupFinalizedExpenditure({ colonyNetwork, colony, token: otherToken });
const expenditureId = await setupFundedExpenditure({ colonyNetwork, colony, tokenAddress: otherToken.address });
await colony.finalizeExpenditure(expenditureId);
const expenditure = await colony.getExpenditure(expenditureId);

await checkErrorRevert(
Expand All @@ -388,7 +389,8 @@ contract("Colony Funding", (accounts) => {

it("should automatically return surplus funds to the domain", async () => {
await fundColonyWithTokens(colony, otherToken, WAD.muln(500));
const expenditureId = await setupFinalizedExpenditure({ colonyNetwork, colony, token: otherToken });
const expenditureId = await setupFundedExpenditure({ colonyNetwork, colony, tokenAddress: otherToken.address });
await colony.finalizeExpenditure(expenditureId);
const expenditure = await colony.getExpenditure(expenditureId);

// Add an extra WAD of funding
Expand All @@ -408,7 +410,8 @@ contract("Colony Funding", (accounts) => {

await metaColony.setNetworkFeeInverse(1); // 100% to fees

const expenditureId = await setupFinalizedExpenditure({ colonyNetwork, colony, token });
const expenditureId = await setupFundedExpenditure({ colonyNetwork, colony });
await colony.finalizeExpenditure(expenditureId);

const networkBalanceBefore = await token.balanceOf(colonyNetwork.address);
await colony.claimExpenditurePayout(expenditureId, 0, token.address);
Expand Down
2 changes: 1 addition & 1 deletion test/contracts-network/colony-reward-payouts.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ contract("Colony Reward Payouts", (accounts) => {
domain = await newColony.getDomain(1);
const rootDomainSkill = domain.skillId;

await setupClaimedExpenditure({ colonyNetwork, colony: newColony, token: newToken, domainId: domainCount });
await setupClaimedExpenditure({ colonyNetwork, colony: newColony, tokenAddress: newToken.address, domainId: domainCount });

await advanceMiningCycleNoContest({ colonyNetwork, client, test: this });
await advanceMiningCycleNoContest({ colonyNetwork, client, test: this });
Expand Down
15 changes: 8 additions & 7 deletions test/contracts-network/reputation-update.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ contract("Reputation Updates", (accounts) => {

describe("when added", () => {
it("should be readable", async () => {
await setupClaimedExpenditure({ colonyNetwork, colony: metaColony, token: clnyToken });
await setupClaimedExpenditure({ colonyNetwork, colony: metaColony });

const repLogEntryManager = await inactiveReputationMiningCycle.getReputationUpdateLogEntry(1);
expect(repLogEntryManager.user).to.equal(MANAGER);
Expand Down Expand Up @@ -109,14 +109,14 @@ contract("Reputation Updates", (accounts) => {

it("should populate nPreviousUpdates correctly", async () => {
const initialRepLogLength = await inactiveReputationMiningCycle.getReputationUpdateLogLength();
await setupClaimedExpenditure({ colonyNetwork, colony: metaColony, token: clnyToken });
await setupClaimedExpenditure({ colonyNetwork, colony: metaColony });

let repLogEntry = await inactiveReputationMiningCycle.getReputationUpdateLogEntry(initialRepLogLength.addn(1));
const nPrevious = new BN(repLogEntry.nPreviousUpdates);
repLogEntry = await inactiveReputationMiningCycle.getReputationUpdateLogEntry(initialRepLogLength.addn(2));
expect(repLogEntry.nPreviousUpdates).to.eq.BN(nPrevious.addn(2));

await setupClaimedExpenditure({ colonyNetwork, colony: metaColony, token: clnyToken });
await setupClaimedExpenditure({ colonyNetwork, colony: metaColony });
repLogEntry = await inactiveReputationMiningCycle.getReputationUpdateLogEntry(initialRepLogLength.addn(3));
expect(repLogEntry.nPreviousUpdates).to.eq.BN(nPrevious.addn(4));
});
Expand All @@ -127,17 +127,18 @@ contract("Reputation Updates", (accounts) => {
await metaColony.addDomain(1, 1, 2);
await metaColony.addDomain(1, 2, 3);
await metaColony.addDomain(1, 3, 4);
await setupClaimedExpenditure({ colonyNetwork, colony: metaColony, token: clnyToken, domainId: 3 });
// 1 => 2 => 3 => 4 => 5

await setupClaimedExpenditure({ colonyNetwork, colony: metaColony, domainId: 3 });

let repLogEntryWorker = await inactiveReputationMiningCycle.getReputationUpdateLogEntry(1);
expect(repLogEntryWorker.amount).to.eq.BN(MANAGER_PAYOUT);
expect(repLogEntryWorker.nUpdates).to.eq.BN(6);

await metaColony.emitDomainReputationPenalty(1, 3, 4, WORKER, WORKER_PAYOUT.neg(), { from: MANAGER });

// (Parents + 1) * 2 + Children * 2 updates
repLogEntryWorker = await inactiveReputationMiningCycle.getReputationUpdateLogEntry(4);
repLogEntryWorker = await inactiveReputationMiningCycle.getReputationUpdateLogEntry(5);
expect(repLogEntryWorker.amount).to.eq.BN(WORKER_PAYOUT.neg());
expect(repLogEntryWorker.nUpdates).to.eq.BN(10); // Negative reputation change means children change as well.
});
Expand All @@ -160,7 +161,7 @@ contract("Reputation Updates", (accounts) => {

it("should not make zero-valued reputation updates", async () => {
await fundColonyWithTokens(metaColony, clnyToken, INITIAL_FUNDING);
await setupClaimedExpenditure({ colonyNetwork, colony: metaColony, token: clnyToken, workerPayout: 0 });
await setupClaimedExpenditure({ colonyNetwork, colony: metaColony, workerPayout: 0 });

// Entries for manager and evaluator only + 1 for miner reward
const numUpdates = await inactiveReputationMiningCycle.getReputationUpdateLogLength();
Expand Down Expand Up @@ -209,7 +210,7 @@ contract("Reputation Updates", (accounts) => {
await otherToken.unlock();
await fundColonyWithTokens(metaColony, otherToken, WAD.muln(500));

await setupClaimedExpenditure({ colonyNetwork, colony: metaColony, token: otherToken });
await setupClaimedExpenditure({ colonyNetwork, colony: metaColony, tokenAddress: otherToken.address });

const reputationUpdateLogLength = await inactiveReputationMiningCycle.getReputationUpdateLogLength();
expect(reputationUpdateLogLength).to.eq.BN(1); // Just the miner reward
Expand Down
14 changes: 7 additions & 7 deletions test/reputation-system/client-core-functionality.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ process.env.SOLIDITY_COVERAGE
const startingBlock = await currentBlock();
const startingBlockNumber = startingBlock.number;
await fundColonyWithTokens(metaColony, clnyToken, INITIAL_FUNDING.muln(100));
await setupClaimedExpenditure({ colonyNetwork, colony: metaColony, token: clnyToken, worker: MINER1, manager: accounts[6] });
await setupClaimedExpenditure({ colonyNetwork, colony: metaColony, worker: MINER1, manager: accounts[6] });
await advanceMiningCycleNoContest({ colonyNetwork, client: reputationMiner, test: this });
await advanceMiningCycleNoContest({ colonyNetwork, client: reputationMiner, test: this });

Expand Down Expand Up @@ -187,7 +187,7 @@ process.env.SOLIDITY_COVERAGE
const startingBlock = await currentBlock();
const startingBlockNumber = startingBlock.number;
await fundColonyWithTokens(metaColony, clnyToken, INITIAL_FUNDING.muln(100));
await setupClaimedExpenditure({ colonyNetwork, colony: metaColony, token: clnyToken, worker: MINER1, manager: accounts[6] });
await setupClaimedExpenditure({ colonyNetwork, colony: metaColony, worker: MINER1, manager: accounts[6] });
await advanceMiningCycleNoContest({ colonyNetwork, client: reputationMiner, test: this });
await advanceMiningCycleNoContest({ colonyNetwork, client: reputationMiner, test: this });

Expand Down Expand Up @@ -278,7 +278,7 @@ process.env.SOLIDITY_COVERAGE

it("should correctly respond to a request for users that have a particular reputation in a colony", async () => {
await fundColonyWithTokens(metaColony, clnyToken, INITIAL_FUNDING.muln(100));
await setupClaimedExpenditure({ colonyNetwork, colony: metaColony, token: clnyToken, worker: MINER1, manager: accounts[6] });
await setupClaimedExpenditure({ colonyNetwork, colony: metaColony, worker: MINER1, manager: accounts[6] });

await advanceMiningCycleNoContest({ colonyNetwork, client: reputationMiner, test: this });
await advanceMiningCycleNoContest({ colonyNetwork, client: reputationMiner, test: this });
Expand All @@ -299,8 +299,8 @@ process.env.SOLIDITY_COVERAGE
expect(reputations.length).to.equal(2);

// Let's check that once accounts[6] has more reputation again, it's listed first.
await setupClaimedExpenditure({ colonyNetwork, colony: metaColony, token: clnyToken, worker: accounts[6], manager: accounts[6] });
await setupClaimedExpenditure({ colonyNetwork, colony: metaColony, token: clnyToken, worker: accounts[6], manager: accounts[6] });
await setupClaimedExpenditure({ colonyNetwork, colony: metaColony, worker: accounts[6], manager: accounts[6] });
await setupClaimedExpenditure({ colonyNetwork, colony: metaColony, worker: accounts[6], manager: accounts[6] });
await advanceMiningCycleNoContest({ colonyNetwork, client: reputationMiner, test: this });
await advanceMiningCycleNoContest({ colonyNetwork, client: reputationMiner, test: this });
rootHash = await reputationMiner.reputationTree.getRootHash();
Expand Down Expand Up @@ -328,7 +328,7 @@ process.env.SOLIDITY_COVERAGE

it("should correctly respond to a request for all reputation a single user has in a colony", async () => {
await fundColonyWithTokens(metaColony, clnyToken, INITIAL_FUNDING.muln(100));
await setupClaimedExpenditure({ colonyNetwork, colony: metaColony, token: clnyToken, worker: MINER1, manager: accounts[6] });
await setupClaimedExpenditure({ colonyNetwork, colony: metaColony, worker: MINER1, manager: accounts[6] });

await advanceMiningCycleNoContest({ colonyNetwork, client: reputationMiner, test: this });
await advanceMiningCycleNoContest({ colonyNetwork, client: reputationMiner, test: this });
Expand All @@ -343,7 +343,7 @@ process.env.SOLIDITY_COVERAGE
expect(reputations.length).to.equal(3);

// More people get reputation doesn't change anything
await setupClaimedExpenditure({ colonyNetwork, colony: metaColony, token: clnyToken, worker: accounts[6], manager: accounts[6] });
await setupClaimedExpenditure({ colonyNetwork, colony: metaColony, worker: accounts[6], manager: accounts[6] });
await advanceMiningCycleNoContest({ colonyNetwork, client: reputationMiner, test: this });
await advanceMiningCycleNoContest({ colonyNetwork, client: reputationMiner, test: this });
rootHash = await reputationMiner.reputationTree.getRootHash();
Expand Down
Loading

0 comments on commit dd94d84

Please sign in to comment.