Skip to content

Commit

Permalink
Merge pull request #982 from JoinColony/maint/meta-extensions
Browse files Browse the repository at this point in the history
Add ColonyExtensionMeta
  • Loading branch information
kronosapiens authored Oct 15, 2021
2 parents 05ffecb + 3a92c8e commit 48dda78
Show file tree
Hide file tree
Showing 15 changed files with 161 additions and 28 deletions.
7 changes: 4 additions & 3 deletions contracts/extensions/CoinMachine.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ pragma solidity 0.7.3;
pragma experimental ABIEncoderV2;

import "./../../lib/dappsys/erc20.sol";
import "./../common/BasicMetaTransaction.sol";
import "./ColonyExtension.sol";
import "./Whitelist.sol";

// ignore-file-swc-108


contract CoinMachine is ColonyExtension {
contract CoinMachine is ColonyExtension, BasicMetaTransaction {

// Events

Expand Down Expand Up @@ -209,7 +210,7 @@ contract CoinMachine is ColonyExtension {
"coin-machine-unauthorised"
);

uint256 maxPurchase = getMaxPurchase(msg.sender);
uint256 maxPurchase = getMaxPurchase(msgSender());
uint256 numTokens = min(maxPurchase, _numTokens);
uint256 totalCost = wmul(numTokens, activePrice);

Expand All @@ -223,7 +224,7 @@ contract CoinMachine is ColonyExtension {
// Do userLimitFraction bookkeeping (only if needed)
if (userLimitFraction < WAD) {
soldTotal = add(soldTotal, numTokens);
soldUser[msg.sender] = add(soldUser[msg.sender], numTokens);
soldUser[msgSender()] = add(soldUser[msgSender()], numTokens);
}

// Check if we've sold out
Expand Down
4 changes: 2 additions & 2 deletions contracts/extensions/ColonyExtension.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import "./../../lib/dappsys/math.sol";
import "./../common/EtherRouter.sol";
import "./../colony/IColony.sol";
import "./../colony/ColonyDataTypes.sol";
import "./../common/BasicMetaTransaction.sol";

abstract contract ColonyExtension is DSAuth, DSMath, BasicMetaTransaction {

abstract contract ColonyExtension is DSAuth, DSMath {

uint256 constant UINT256_MAX = 2**256 - 1;

Expand Down
68 changes: 68 additions & 0 deletions contracts/extensions/ColonyExtensionMeta.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
This file is part of The Colony Network.
The Colony Network is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
The Colony Network is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with The Colony Network. If not, see <http://www.gnu.org/licenses/>.
*/

pragma solidity 0.7.3;
pragma experimental ABIEncoderV2;

import "./../common/BasicMetaTransaction.sol";
import "./ColonyExtension.sol";

abstract contract ColonyExtensionMeta is ColonyExtension, BasicMetaTransaction {

mapping (address => uint256) metatransactionNonces;

function getMetatransactionNonce(address _user) override public view returns (uint256 nonce) {
return metatransactionNonces[_user];
}

function incrementMetatransactionNonce(address _user) override internal {
metatransactionNonces[_user] += 1;
}

uint256 placeholder00;
uint256 placeholder01;
uint256 placeholder02;
uint256 placeholder03;
uint256 placeholder04;
uint256 placeholder05;
uint256 placeholder06;
uint256 placeholder07;
uint256 placeholder08;
uint256 placeholder09;
uint256 placeholder10;
uint256 placeholder11;
uint256 placeholder12;
uint256 placeholder13;
uint256 placeholder14;
uint256 placeholder15;
uint256 placeholder16;
uint256 placeholder17;
uint256 placeholder18;
uint256 placeholder19;
uint256 placeholder20;
uint256 placeholder21;
uint256 placeholder22;
uint256 placeholder23;
uint256 placeholder24;
uint256 placeholder25;
uint256 placeholder26;
uint256 placeholder27;
uint256 placeholder28;
uint256 placeholder29;
uint256 placeholder30;
uint256 placeholder31;
}
5 changes: 3 additions & 2 deletions contracts/extensions/EvaluatedExpenditure.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ pragma solidity 0.7.3;
pragma experimental ABIEncoderV2;

import "./ColonyExtension.sol";
import "./../common/BasicMetaTransaction.sol";

// ignore-file-swc-108


contract EvaluatedExpenditure is ColonyExtension {
contract EvaluatedExpenditure is ColonyExtension, BasicMetaTransaction {

uint256 constant EXPENDITURESLOTS_SLOT = 26;
uint256 constant PAYOUT_MODIFIER_OFFSET = 2;
Expand Down Expand Up @@ -86,7 +87,7 @@ contract EvaluatedExpenditure is ColonyExtension {
public
{
require(_slots.length == _payoutModifiers.length, "evaluated-expenditure-bad-slots");
require(colony.getExpenditure(_id).owner == msg.sender, "evaluated-expenditure-not-owner");
require(colony.getExpenditure(_id).owner == msgSender(), "evaluated-expenditure-not-owner");

bool[] memory mask = new bool[](2);
bytes32[] memory keys = new bytes32[](2);
Expand Down
5 changes: 3 additions & 2 deletions contracts/extensions/FundingQueue.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@
pragma solidity 0.7.3;
pragma experimental ABIEncoderV2;

import "./../colony/ColonyDataTypes.sol";
import "./../colonyNetwork/IColonyNetwork.sol";
import "./../common/BasicMetaTransaction.sol";
import "./../common/ERC20Extended.sol";
import "./../patriciaTree/PatriciaTreeProofs.sol";
import "./../tokenLocking/ITokenLocking.sol";
import "./ColonyExtension.sol";
import "./../colony/ColonyDataTypes.sol";

contract FundingQueue is ColonyExtension, PatriciaTreeProofs {
contract FundingQueue is ColonyExtension, PatriciaTreeProofs, BasicMetaTransaction {

// Events
event ProposalCreated(uint256 id, uint256 indexed fromPot, uint256 indexed toPot, address indexed token, uint256 amount);
Expand Down
3 changes: 2 additions & 1 deletion contracts/extensions/OneTxPayment.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ pragma solidity 0.7.3;
pragma experimental ABIEncoderV2;

import "./ColonyExtension.sol";
import "./../common/BasicMetaTransaction.sol";

// ignore-file-swc-108


contract OneTxPayment is ColonyExtension {
contract OneTxPayment is ColonyExtension, BasicMetaTransaction {
event OneTxPaymentMade(address agent, uint256 fundamentalId, uint256 nPayouts);

ColonyDataTypes.ColonyRole constant ADMINISTRATION = ColonyDataTypes.ColonyRole.Administration;
Expand Down
5 changes: 4 additions & 1 deletion contracts/extensions/TokenSupplier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
pragma solidity 0.7.3;
pragma experimental ABIEncoderV2;

import "./../common/BasicMetaTransaction.sol";
import "./../common/ERC20Extended.sol";
import "./ColonyExtension.sol";


contract TokenSupplier is ColonyExtension {
contract TokenSupplier is ColonyExtension, BasicMetaTransaction {

uint256 constant ISSUANCE_PERIOD = 1 days;

Expand All @@ -39,7 +40,9 @@ contract TokenSupplier is ColonyExtension {
uint256 tokenIssuanceRate;
uint256 lastIssue;
uint256 lastRateUpdate;

mapping(address => uint256) metatransactionNonces;

function getMetatransactionNonce(address userAddress) override public view returns (uint256 nonce){
return metatransactionNonces[userAddress];
}
Expand Down
5 changes: 3 additions & 2 deletions contracts/extensions/VotingReputation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@
pragma solidity 0.7.3;
pragma experimental ABIEncoderV2;

import "./../colony/ColonyRoles.sol";
import "./../colonyNetwork/IColonyNetwork.sol";
import "./../colony/ColonyRoles.sol";
import "./../common/BasicMetaTransaction.sol";
import "./../common/ERC20Extended.sol";
import "./../patriciaTree/PatriciaTreeProofs.sol";
import "./../tokenLocking/ITokenLocking.sol";
import "./ColonyExtension.sol";


contract VotingReputation is ColonyExtension, PatriciaTreeProofs {
contract VotingReputation is ColonyExtension, PatriciaTreeProofs, BasicMetaTransaction {

// Events
event MotionCreated(uint256 indexed motionId, address creator, uint256 indexed domainId);
Expand Down
3 changes: 2 additions & 1 deletion contracts/extensions/Whitelist.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
pragma solidity 0.7.3;
pragma experimental ABIEncoderV2;

import "./../common/BasicMetaTransaction.sol";
import "./ColonyExtension.sol";

// ignore-file-swc-108


contract Whitelist is ColonyExtension {
contract Whitelist is ColonyExtension, BasicMetaTransaction {

// Events

Expand Down
14 changes: 2 additions & 12 deletions contracts/testHelpers/TestExtensions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
pragma solidity 0.7.3;
pragma experimental ABIEncoderV2;

import "../extensions/ColonyExtension.sol";
import "../extensions/ColonyExtensionMeta.sol";


abstract contract TestExtension is ColonyExtension {
abstract contract TestExtension is ColonyExtensionMeta {
function install(address _colony) public override auth {
require(address(colony) == address(0x0), "extension-already-installed");

Expand All @@ -37,16 +37,6 @@ abstract contract TestExtension is ColonyExtension {
function uninstall() public override auth {
selfdestruct(address(uint160(address(colony))));
}

mapping(address => uint256) metatransactionNonces;
function getMetatransactionNonce(address userAddress) override public view returns (uint256 nonce){
return metatransactionNonces[userAddress];
}

function incrementMetatransactionNonce(address user) override internal {
metatransactionNonces[user] = add(metatransactionNonces[user], 1);
}

}


Expand Down
1 change: 1 addition & 0 deletions scripts/check-recovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ walkSync("./contracts/").forEach((contractName) => {
"contracts/ens/ENSRegistry.sol",
"contracts/extensions/CoinMachine.sol",
"contracts/extensions/ColonyExtension.sol",
"contracts/extensions/ColonyExtensionMeta.sol",
"contracts/extensions/EvaluatedExpenditure.sol",
"contracts/extensions/FundingQueue.sol",
"contracts/extensions/OneTxPayment.sol",
Expand Down
1 change: 1 addition & 0 deletions scripts/check-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ walkSync("./contracts/").forEach((contractName) => {
"contracts/extensions/EvaluatedExpenditure.sol",
"contracts/extensions/FundingQueue.sol",
"contracts/extensions/ColonyExtension.sol",
"contracts/extensions/ColonyExtensionMeta.sol",
"contracts/extensions/OneTxPayment.sol",
"contracts/extensions/TokenSupplier.sol",
"contracts/extensions/VotingReputation.sol",
Expand Down
27 changes: 26 additions & 1 deletion test/contracts-network/colony-network-extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ import { soliditySha3 } from "web3-utils";

import { checkErrorRevert, web3GetBalance, encodeTxData } from "../../helpers/test-helper";
import { setupEtherRouter } from "../../helpers/upgradable-contracts";
import { setupColonyNetwork, setupMetaColonyWithLockedCLNYToken, setupRandomColony } from "../../helpers/test-data-generator";
import { UINT256_MAX } from "../../helpers/constants";

import {
setupColonyNetwork,
setupMetaColonyWithLockedCLNYToken,
setupRandomColony,
getMetaTransactionParameters,
} from "../../helpers/test-data-generator";

const { expect } = chai;
chai.use(bnChai(web3.utils.BN));

Expand Down Expand Up @@ -355,5 +361,24 @@ contract("Colony Network Extensions", (accounts) => {
const action = await encodeTxData(tokenLocking, "unlockTokenForUser", [token.address, USER, lockId]);
await checkErrorRevert(otherColonyExecuteCall.executeCall(tokenLocking.address, action), "colony-token-locking-not-locker");
});

it("allows extensions to use metatransactions", async () => {
await metaColony.addExtensionToNetwork(TEST_EXTENSION, testExtension1Resolver.address);
await colony.installExtension(TEST_EXTENSION, 1, { from: ROOT });

const extensionAddress = await colonyNetwork.getExtensionInstallation(TEST_EXTENSION, colony.address);
const extension = await TestExtension1.at(extensionAddress);

let nonce;
nonce = await extension.getMetatransactionNonce(ROOT);
expect(nonce).to.be.zero;

const txData = await extension.contract.methods.foo().encodeABI();
const { r, s, v } = await getMetaTransactionParameters(txData, ROOT, extension.address);
await extension.executeMetaTransaction(ROOT, txData, r, s, v, { from: USER });

nonce = await extension.getMetatransactionNonce(ROOT);
expect(nonce).to.eq.BN(1);
});
});
});
25 changes: 24 additions & 1 deletion test/extensions/evaluated-expenditures.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ import { soliditySha3 } from "web3-utils";
import { UINT256_MAX, WAD } from "../../helpers/constants";
import { setupEtherRouter } from "../../helpers/upgradable-contracts";
import { checkErrorRevert, web3GetCode } from "../../helpers/test-helper";
import { setupColonyNetwork, setupRandomColony, setupMetaColonyWithLockedCLNYToken } from "../../helpers/test-data-generator";

import {
setupColonyNetwork,
setupRandomColony,
setupMetaColonyWithLockedCLNYToken,
getMetaTransactionParameters,
} from "../../helpers/test-data-generator";

const { expect } = chai;
chai.use(bnChai(web3.utils.BN));
Expand Down Expand Up @@ -127,5 +133,22 @@ contract("EvaluatedExpenditure", (accounts) => {
"evaluated-expenditure-not-owner"
);
});

it("can set the payout modifier via metatransaction", async () => {
const txData = await evaluatedExpenditure.contract.methods
.setExpenditurePayoutModifiers(1, UINT256_MAX.toString(), expenditureId.toString(), [0], [WAD.toString()])
.encodeABI();

const { r, s, v } = await getMetaTransactionParameters(txData, USER0, evaluatedExpenditure.address);

let expenditureSlot;
expenditureSlot = await colony.getExpenditureSlot(expenditureId, 0);
expect(expenditureSlot.payoutModifier).to.be.zero;

await evaluatedExpenditure.executeMetaTransaction(USER0, txData, r, s, v, { from: USER1 });

expenditureSlot = await colony.getExpenditureSlot(expenditureId, 0);
expect(expenditureSlot.payoutModifier).to.eq.BN(WAD);
});
});
});
16 changes: 16 additions & 0 deletions test/reputation-system/client-core-functionality.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,14 @@ process.env.SOLIDITY_COVERAGE
expect(addresses[1]).to.equal(MINER1.toLowerCase());
});

it("should correctly respond to a request for users that have a particular reputation in a colony that has an invalid address", async () => {
const url = `http://127.0.0.1:3000/0x0000/NotAValidAddress/1`;
const res = await request(url);
expect(res.statusCode).to.equal(400);
expect(res.statusCode).to.equal(400);
expect(JSON.parse(res.body).message).to.equal("One of the parameters was incorrect");
});

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 setupFinalizedTask({ colonyNetwork, colony: metaColony, token: clnyToken, worker: MINER1, manager: accounts[6] });
Expand Down Expand Up @@ -251,5 +259,13 @@ process.env.SOLIDITY_COVERAGE
({ reputations } = JSON.parse(res.body));
expect(reputations.length).to.equal(3);
});

it("should correctly respond to a request for all reputation a single user has in a colony that has an invalid address", async () => {
const url = `http://127.0.0.1:3000/0x0000/NotAValidAddress/1/all`;
const res = await request(url);
expect(res.statusCode).to.equal(400);
expect(res.statusCode).to.equal(400);
expect(JSON.parse(res.body).message).to.equal("One of the parameters was incorrect");
});
});
});

0 comments on commit 48dda78

Please sign in to comment.