From 7958ff907a08a8e37f5970bf7682d56508aae174 Mon Sep 17 00:00:00 2001 From: benk10 Date: Tue, 7 Jan 2020 13:48:09 +0200 Subject: [PATCH 1/4] Added AuthorizedMintRep scheme Fix #703 --- contracts/schemes/AuthorizedMintRep.sol | 75 +++++++++ package-lock.json | 41 +++-- test/authorizedmintrep.js | 196 ++++++++++++++++++++++++ 3 files changed, 301 insertions(+), 11 deletions(-) create mode 100644 contracts/schemes/AuthorizedMintRep.sol create mode 100644 test/authorizedmintrep.js diff --git a/contracts/schemes/AuthorizedMintRep.sol b/contracts/schemes/AuthorizedMintRep.sol new file mode 100644 index 00000000..262bb7b0 --- /dev/null +++ b/contracts/schemes/AuthorizedMintRep.sol @@ -0,0 +1,75 @@ +pragma solidity 0.5.13; + +import "openzeppelin-solidity/contracts/math/SafeMath.sol"; +import "../controller/Controller.sol"; + +/** + * @title A scheme for reputation allocation according to token balances + * This contract is assuming that the token contract is paused, and one cannot transfer its tokens. + */ + +contract AuthorizedMintRep { + using SafeMath for uint256; + + Avatar public avatar; + uint256 public activationStartTime; + uint256 public activationEndTime; + uint256 public repRewardLeft; + address public authorizedAddress; + bool public limitRepReward; + + /** + * @dev Throws if called by an unauthorized account. + */ + modifier onlyAuthorized() { + require(msg.sender == authorizedAddress, "Caller is not authorized"); + _; + } + + /** + * @dev initialize + * @param _avatar the avatar to mint reputation from + * @param _activationStartTime start time for allowing minting + * @param _activationEndTime end time for allowing minting + * @param _maxRepReward maximum reputation mintable by this scheme + * @param _authorizedAddress address authorized for minting reputation + */ + function initialize( + Avatar _avatar, + uint256 _activationStartTime, + uint256 _activationEndTime, + uint256 _maxRepReward, + address _authorizedAddress + ) external { + require(avatar == Avatar(0), "can be called only one time"); + require(_avatar != Avatar(0), "avatar cannot be zero"); + require(_activationStartTime < _activationEndTime, "_activationStartTime < _activationEndTime"); + avatar = _avatar; + activationStartTime = _activationStartTime; + activationEndTime = _activationEndTime; + repRewardLeft = _maxRepReward; + authorizedAddress = _authorizedAddress; + limitRepReward = _maxRepReward != 0; + } + + /** + * @dev reputationMint function + * @param _beneficiary the beneficiary address to redeem for + * @param _amount the agreementHash hash + */ + function reputationMint(address _beneficiary, uint256 _amount) external onlyAuthorized { + // solhint-disable-next-line not-rely-on-time + require(now >= activationStartTime, "Minting period did not start yet"); + // solhint-disable-next-line not-rely-on-time + require(now < activationEndTime, "Minting period ended."); + + if (limitRepReward) { + repRewardLeft = repRewardLeft.sub(_amount); + } + + require( + Controller(avatar.owner()).mintReputation(_amount, _beneficiary, address(avatar)), + "Minting reputation should succeed" + ); + } +} diff --git a/package-lock.json b/package-lock.json index 530c8578..2ef71205 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2716,7 +2716,8 @@ "ansi-regex": { "version": "2.1.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "aproba": { "version": "1.2.0", @@ -2737,12 +2738,14 @@ "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, + "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -2757,17 +2760,20 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "core-util-is": { "version": "1.0.2", @@ -2884,7 +2890,8 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "ini": { "version": "1.3.5", @@ -2896,6 +2903,7 @@ "version": "1.0.0", "bundled": true, "dev": true, + "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -2910,6 +2918,7 @@ "version": "3.0.4", "bundled": true, "dev": true, + "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -2917,12 +2926,14 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "minipass": { "version": "2.3.5", "bundled": true, "dev": true, + "optional": true, "requires": { "safe-buffer": "^5.1.2", "yallist": "^3.0.0" @@ -2941,6 +2952,7 @@ "version": "0.5.1", "bundled": true, "dev": true, + "optional": true, "requires": { "minimist": "0.0.8" } @@ -3021,7 +3033,8 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "object-assign": { "version": "4.1.1", @@ -3033,6 +3046,7 @@ "version": "1.4.0", "bundled": true, "dev": true, + "optional": true, "requires": { "wrappy": "1" } @@ -3118,7 +3132,8 @@ "safe-buffer": { "version": "5.1.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "safer-buffer": { "version": "2.1.2", @@ -3154,6 +3169,7 @@ "version": "1.0.2", "bundled": true, "dev": true, + "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -3173,6 +3189,7 @@ "version": "3.0.1", "bundled": true, "dev": true, + "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -3216,12 +3233,14 @@ "wrappy": { "version": "1.0.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "yallist": { "version": "3.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true } } }, diff --git a/test/authorizedmintrep.js b/test/authorizedmintrep.js new file mode 100644 index 00000000..5ab0f10d --- /dev/null +++ b/test/authorizedmintrep.js @@ -0,0 +1,196 @@ +const helpers = require('./helpers'); +const DaoCreator = artifacts.require('./DaoCreator.sol'); +const ControllerCreator = artifacts.require('./ControllerCreator.sol'); +const DAOTracker = artifacts.require('./DAOTracker.sol'); +const constants = require('./constants'); +var AuthorizedMintRep = artifacts.require('./AuthorizedMintRep.sol'); + +const setup = async function( + accounts, + _activationStartTime = 0, + _activationEndTime = 3000, + _maxRepReward = 100, + _initialize = true +) { + var testSetup = new helpers.TestSetup(); + var controllerCreator = await ControllerCreator.new({ + gas: constants.ARC_GAS_LIMIT + }); + var daoTracker = await DAOTracker.new({ gas: constants.ARC_GAS_LIMIT }); + testSetup.daoCreator = await DaoCreator.new( + controllerCreator.address, + daoTracker.address, + { gas: constants.ARC_GAS_LIMIT } + ); + + testSetup.org = await helpers.setupOrganization( + testSetup.daoCreator, + accounts[0], + 1000, + 1000 + ); + testSetup.activationStartTime = + (await web3.eth.getBlock('latest')).timestamp + _activationStartTime; + testSetup.activationEndTime = + (await web3.eth.getBlock('latest')).timestamp + _activationEndTime; + testSetup.maxRepReward = _maxRepReward; + testSetup.authorizedMintRep = await AuthorizedMintRep.new(); + if (_initialize === true) { + await testSetup.authorizedMintRep.initialize( + testSetup.org.avatar.address, + testSetup.activationStartTime, + testSetup.activationEndTime, + testSetup.maxRepReward, + accounts[1], + { gas: constants.ARC_GAS_LIMIT } + ); + } + + var permissions = '0x00000000'; + await testSetup.daoCreator.setSchemes( + testSetup.org.avatar.address, + [testSetup.authorizedMintRep.address], + [web3.utils.asciiToHex('0')], + [permissions], + 'metaData' + ); + + return testSetup; +}; + +contract('AuthorizedMintRep', accounts => { + it('initialize', async () => { + let testSetup = await setup(accounts); + + assert.equal( + await testSetup.authorizedMintRep.repRewardLeft(), + testSetup.maxRepReward + ); + assert.equal( + await testSetup.authorizedMintRep.activationStartTime(), + testSetup.activationStartTime + ); + assert.equal( + await testSetup.authorizedMintRep.activationEndTime(), + testSetup.activationEndTime + ); + assert.equal( + await testSetup.authorizedMintRep.authorizedAddress(), + accounts[1] + ); + }); + + it('initialize _activationStartTime >= activationEndTime is not allowed', async () => { + let testSetup = await setup(accounts); + let authorizedMintRep = await AuthorizedMintRep.new(); + try { + await authorizedMintRep.initialize( + testSetup.org.avatar.address, + testSetup.activationStartTime, + testSetup.activationStartTime - 1, + testSetup.maxRepReward, + accounts[1], + { gas: constants.ARC_GAS_LIMIT } + ); + assert(false, '_redeemEnableTime < auctionsEndTime is not allowed'); + } catch (error) { + helpers.assertVMException(error); + } + }); + + it('mint reputation', async () => { + let testSetup = await setup(accounts); + await testSetup.authorizedMintRep.reputationMint(accounts[2], 1, { + from: accounts[1] + }); + + assert.equal(await testSetup.org.reputation.balanceOf(accounts[2]), 1); + }); + + it('mint reputation by unauthorized account should fail', async () => { + let testSetup = await setup(accounts); + try { + await testSetup.authorizedMintRep.reputationMint(accounts[2], 1, { + from: accounts[0] + }); + assert(false, 'mint reputation by unauthorized account should fail'); + } catch (error) { + helpers.assertVMException(error); + } + }); + + it('mint without initialize should fail', async () => { + let testSetup = await setup(accounts, 0, 3000, 100, false); + try { + await testSetup.authorizedMintRep.reputationMint(accounts[2], 1, { + from: accounts[1] + }); + assert(false, 'mint without initialize should fail'); + } catch (error) { + helpers.assertVMException(error); + } + }); + + it('mint before _activationStartTime should fail', async () => { + let testSetup = await setup(accounts, 2000, 3000, 100, true); + try { + await testSetup.authorizedMintRep.reputationMint(accounts[2], 1, { + from: accounts[1] + }); + assert(false, 'mint before _activationStartTime should fail'); + } catch (error) { + helpers.assertVMException(error); + } + }); + + it('mint after _activationEndTime should revert', async () => { + let testSetup = await setup(accounts); + await helpers.increaseTime(3001); + try { + await testSetup.authorizedMintRep.reputationMint(accounts[2], 1, { + from: accounts[1] + }); + assert(false, 'mint after _activationEndTime should revert'); + } catch (error) { + helpers.assertVMException(error); + } + }); + + it('mint more than _maxRepReward should fail', async () => { + let testSetup = await setup(accounts); + try { + await testSetup.authorizedMintRep.reputationMint(accounts[2], 101, { + from: accounts[1] + }); + assert(false, 'mint more than _maxRepReward should fail'); + } catch (error) { + helpers.assertVMException(error); + } + }); + + it('mint reputation with 0 _maxRepReward should allow any amount', async () => { + let testSetup = await setup(accounts, 0, 3000, 0, true); + await testSetup.authorizedMintRep.reputationMint(accounts[2], 1000, { + from: accounts[1] + }); + + assert.equal(await testSetup.org.reputation.balanceOf(accounts[2]), 1000); + }); + + it('cannot initialize twice', async () => { + let testSetup = await setup(accounts); + try { + await testSetup.authorizedMintRep.initialize( + testSetup.org.avatar.address, + testSetup.activationStartTime, + testSetup.activationEndTime, + testSetup.maxRepReward, + accounts[1], + { gas: constants.ARC_GAS_LIMIT } + ); + assert(false, 'cannot initialize twice'); + } catch (error) { + helpers.assertVMException(error); + } + }); +}); From 842d5917a859133ffb1bb7ad694eae5ddb5cf0b5 Mon Sep 17 00:00:00 2001 From: benk10 Date: Tue, 7 Jan 2020 13:52:14 +0200 Subject: [PATCH 2/4] fix comment --- contracts/schemes/AuthorizedMintRep.sol | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/contracts/schemes/AuthorizedMintRep.sol b/contracts/schemes/AuthorizedMintRep.sol index 262bb7b0..3ef3794d 100644 --- a/contracts/schemes/AuthorizedMintRep.sol +++ b/contracts/schemes/AuthorizedMintRep.sol @@ -4,8 +4,7 @@ import "openzeppelin-solidity/contracts/math/SafeMath.sol"; import "../controller/Controller.sol"; /** - * @title A scheme for reputation allocation according to token balances - * This contract is assuming that the token contract is paused, and one cannot transfer its tokens. + * @title A scheme for reputation allocation by an authorized account */ contract AuthorizedMintRep { From ebe3c4c67409cb6cee1923575bb439691b3ffb7f Mon Sep 17 00:00:00 2001 From: benk10 Date: Tue, 7 Jan 2020 14:18:59 +0200 Subject: [PATCH 3/4] Fix --- contracts/schemes/AuthorizedMintRep.sol | 25 +++++------------- test/authorizedmintrep.js | 34 ++++++------------------- 2 files changed, 15 insertions(+), 44 deletions(-) diff --git a/contracts/schemes/AuthorizedMintRep.sol b/contracts/schemes/AuthorizedMintRep.sol index 3ef3794d..47d05c16 100644 --- a/contracts/schemes/AuthorizedMintRep.sol +++ b/contracts/schemes/AuthorizedMintRep.sol @@ -1,5 +1,6 @@ pragma solidity 0.5.13; +import "openzeppelin-solidity/contracts/ownership/Ownable.sol"; import "openzeppelin-solidity/contracts/math/SafeMath.sol"; import "../controller/Controller.sol"; @@ -7,39 +8,28 @@ import "../controller/Controller.sol"; * @title A scheme for reputation allocation by an authorized account */ -contract AuthorizedMintRep { +contract AuthorizedMintRep is Ownable { using SafeMath for uint256; Avatar public avatar; uint256 public activationStartTime; uint256 public activationEndTime; uint256 public repRewardLeft; - address public authorizedAddress; bool public limitRepReward; - /** - * @dev Throws if called by an unauthorized account. - */ - modifier onlyAuthorized() { - require(msg.sender == authorizedAddress, "Caller is not authorized"); - _; - } - /** * @dev initialize * @param _avatar the avatar to mint reputation from * @param _activationStartTime start time for allowing minting * @param _activationEndTime end time for allowing minting * @param _maxRepReward maximum reputation mintable by this scheme - * @param _authorizedAddress address authorized for minting reputation */ function initialize( Avatar _avatar, uint256 _activationStartTime, uint256 _activationEndTime, - uint256 _maxRepReward, - address _authorizedAddress - ) external { + uint256 _maxRepReward + ) external onlyOwner { require(avatar == Avatar(0), "can be called only one time"); require(_avatar != Avatar(0), "avatar cannot be zero"); require(_activationStartTime < _activationEndTime, "_activationStartTime < _activationEndTime"); @@ -47,16 +37,15 @@ contract AuthorizedMintRep { activationStartTime = _activationStartTime; activationEndTime = _activationEndTime; repRewardLeft = _maxRepReward; - authorizedAddress = _authorizedAddress; limitRepReward = _maxRepReward != 0; } /** * @dev reputationMint function - * @param _beneficiary the beneficiary address to redeem for - * @param _amount the agreementHash hash + * @param _beneficiary the beneficiary address to mint reputation for + * @param _amount the amount of reputation to mint the the beneficirary */ - function reputationMint(address _beneficiary, uint256 _amount) external onlyAuthorized { + function reputationMint(address _beneficiary, uint256 _amount) external onlyOwner { // solhint-disable-next-line not-rely-on-time require(now >= activationStartTime, "Minting period did not start yet"); // solhint-disable-next-line not-rely-on-time diff --git a/test/authorizedmintrep.js b/test/authorizedmintrep.js index 5ab0f10d..1f00a1f2 100644 --- a/test/authorizedmintrep.js +++ b/test/authorizedmintrep.js @@ -41,7 +41,6 @@ const setup = async function( testSetup.activationStartTime, testSetup.activationEndTime, testSetup.maxRepReward, - accounts[1], { gas: constants.ARC_GAS_LIMIT } ); } @@ -74,10 +73,7 @@ contract('AuthorizedMintRep', accounts => { await testSetup.authorizedMintRep.activationEndTime(), testSetup.activationEndTime ); - assert.equal( - await testSetup.authorizedMintRep.authorizedAddress(), - accounts[1] - ); + assert.equal(await testSetup.authorizedMintRep.owner(), accounts[0]); }); it('initialize _activationStartTime >= activationEndTime is not allowed', async () => { @@ -89,7 +85,6 @@ contract('AuthorizedMintRep', accounts => { testSetup.activationStartTime, testSetup.activationStartTime - 1, testSetup.maxRepReward, - accounts[1], { gas: constants.ARC_GAS_LIMIT } ); assert(false, '_redeemEnableTime < auctionsEndTime is not allowed'); @@ -100,9 +95,7 @@ contract('AuthorizedMintRep', accounts => { it('mint reputation', async () => { let testSetup = await setup(accounts); - await testSetup.authorizedMintRep.reputationMint(accounts[2], 1, { - from: accounts[1] - }); + await testSetup.authorizedMintRep.reputationMint(accounts[2], 1); assert.equal(await testSetup.org.reputation.balanceOf(accounts[2]), 1); }); @@ -111,7 +104,7 @@ contract('AuthorizedMintRep', accounts => { let testSetup = await setup(accounts); try { await testSetup.authorizedMintRep.reputationMint(accounts[2], 1, { - from: accounts[0] + from: accounts[1] }); assert(false, 'mint reputation by unauthorized account should fail'); } catch (error) { @@ -122,9 +115,7 @@ contract('AuthorizedMintRep', accounts => { it('mint without initialize should fail', async () => { let testSetup = await setup(accounts, 0, 3000, 100, false); try { - await testSetup.authorizedMintRep.reputationMint(accounts[2], 1, { - from: accounts[1] - }); + await testSetup.authorizedMintRep.reputationMint(accounts[2], 1); assert(false, 'mint without initialize should fail'); } catch (error) { helpers.assertVMException(error); @@ -134,9 +125,7 @@ contract('AuthorizedMintRep', accounts => { it('mint before _activationStartTime should fail', async () => { let testSetup = await setup(accounts, 2000, 3000, 100, true); try { - await testSetup.authorizedMintRep.reputationMint(accounts[2], 1, { - from: accounts[1] - }); + await testSetup.authorizedMintRep.reputationMint(accounts[2], 1); assert(false, 'mint before _activationStartTime should fail'); } catch (error) { helpers.assertVMException(error); @@ -147,9 +136,7 @@ contract('AuthorizedMintRep', accounts => { let testSetup = await setup(accounts); await helpers.increaseTime(3001); try { - await testSetup.authorizedMintRep.reputationMint(accounts[2], 1, { - from: accounts[1] - }); + await testSetup.authorizedMintRep.reputationMint(accounts[2], 1); assert(false, 'mint after _activationEndTime should revert'); } catch (error) { helpers.assertVMException(error); @@ -159,9 +146,7 @@ contract('AuthorizedMintRep', accounts => { it('mint more than _maxRepReward should fail', async () => { let testSetup = await setup(accounts); try { - await testSetup.authorizedMintRep.reputationMint(accounts[2], 101, { - from: accounts[1] - }); + await testSetup.authorizedMintRep.reputationMint(accounts[2], 101); assert(false, 'mint more than _maxRepReward should fail'); } catch (error) { helpers.assertVMException(error); @@ -170,9 +155,7 @@ contract('AuthorizedMintRep', accounts => { it('mint reputation with 0 _maxRepReward should allow any amount', async () => { let testSetup = await setup(accounts, 0, 3000, 0, true); - await testSetup.authorizedMintRep.reputationMint(accounts[2], 1000, { - from: accounts[1] - }); + await testSetup.authorizedMintRep.reputationMint(accounts[2], 1000); assert.equal(await testSetup.org.reputation.balanceOf(accounts[2]), 1000); }); @@ -185,7 +168,6 @@ contract('AuthorizedMintRep', accounts => { testSetup.activationStartTime, testSetup.activationEndTime, testSetup.maxRepReward, - accounts[1], { gas: constants.ARC_GAS_LIMIT } ); assert(false, 'cannot initialize twice'); From 1b27b579e47714e1aaa340323e7d97e7c2e8d137 Mon Sep 17 00:00:00 2001 From: benk10 Date: Wed, 8 Jan 2020 10:03:13 +0200 Subject: [PATCH 4/4] bump version --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2ef71205..fb435c28 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@daostack/arc", - "version": "0.0.1-rc.36", + "version": "0.0.1-rc.37", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 90abcb78..cfa449be 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@daostack/arc", - "version": "0.0.1-rc.36", + "version": "0.0.1-rc.37", "description": "A platform for building DAOs", "files": [ "contracts/",