diff --git a/apps/finance/contracts/Finance.sol b/apps/finance/contracts/Finance.sol index 75645b42a1..bc8bef65d2 100644 --- a/apps/finance/contracts/Finance.sol +++ b/apps/finance/contracts/Finance.sol @@ -61,7 +61,6 @@ contract Finance is EtherTokenConstant, IsContract, AragonApp { uint64 interval; uint64 maxRepeats; uint64 repeats; - string reference; } // Order optimized for storage @@ -74,7 +73,6 @@ contract Finance is EtherTokenConstant, IsContract, AragonApp { uint64 paymentRepeatNumber; uint64 date; uint64 periodId; - string reference; } struct TokenStatement { @@ -114,8 +112,8 @@ contract Finance is EtherTokenConstant, IsContract, AragonApp { event NewPeriod(uint64 indexed periodId, uint64 periodStarts, uint64 periodEnds); event SetBudget(address indexed token, uint256 amount, bool hasBudget); - event NewPayment(uint256 indexed paymentId, address indexed recipient, uint64 maxRepeats); - event NewTransaction(uint256 indexed transactionId, bool incoming, address indexed entity, uint256 amount); + event NewPayment(uint256 indexed paymentId, address indexed recipient, uint64 maxRepeats, string reference); + event NewTransaction(uint256 indexed transactionId, bool incoming, address indexed entity, uint256 amount, string reference); event ChangePaymentState(uint256 indexed paymentId, bool inactive); event ChangePeriodDuration(uint64 newDuration); event PaymentFailure(uint256 paymentId); @@ -243,7 +241,7 @@ contract Finance is EtherTokenConstant, IsContract, AragonApp { require(settings.budgets[_token] >= _amount || !settings.hasBudget[_token], ERROR_BUDGET); paymentId = paymentsNextIndex++; - emit NewPayment(paymentId, _receiver, _maxRepeats); + emit NewPayment(paymentId, _receiver, _maxRepeats, _reference); Payment storage payment = payments[paymentId]; payment.token = _token; @@ -252,7 +250,6 @@ contract Finance is EtherTokenConstant, IsContract, AragonApp { payment.initialPaymentTime = _initialPaymentTime; payment.interval = _interval; payment.maxRepeats = _maxRepeats; - payment.reference = _reference; payment.createdBy = msg.sender; if (nextPaymentTime(paymentId) <= getTimestamp64()) { @@ -405,7 +402,6 @@ contract Finance is EtherTokenConstant, IsContract, AragonApp { uint64 initialPaymentTime, uint64 interval, uint64 maxRepeats, - string reference, bool inactive, uint64 repeats, address createdBy @@ -421,7 +417,6 @@ contract Finance is EtherTokenConstant, IsContract, AragonApp { maxRepeats = payment.maxRepeats; repeats = payment.repeats; inactive = payment.inactive; - reference = payment.reference; createdBy = payment.createdBy; } @@ -437,8 +432,7 @@ contract Finance is EtherTokenConstant, IsContract, AragonApp { address token, address entity, bool isIncoming, - uint64 date, - string reference + uint64 date ) { Transaction storage transaction = transactions[_transactionId]; @@ -451,7 +445,6 @@ contract Finance is EtherTokenConstant, IsContract, AragonApp { amount = transaction.amount; paymentId = transaction.paymentId; paymentRepeatNumber = transaction.paymentRepeatNumber; - reference = transaction.reference; } function getPeriod(uint64 _periodId) @@ -597,7 +590,7 @@ contract Finance is EtherTokenConstant, IsContract, AragonApp { payment.amount, _paymentId, payment.repeats, - "" // since paymentId is saved, the payment reference can be fetched + "" ); } } @@ -666,22 +659,21 @@ contract Finance is EtherTokenConstant, IsContract, AragonApp { uint256 transactionId = transactionsNextIndex++; Transaction storage transaction = transactions[transactionId]; - transaction.periodId = periodId; + transaction.token = _token; + transaction.entity = _entity; + transaction.isIncoming = _incoming; transaction.amount = _amount; transaction.paymentId = _paymentId; transaction.paymentRepeatNumber = _paymentRepeatNumber; - transaction.isIncoming = _incoming; - transaction.token = _token; - transaction.entity = _entity; transaction.date = getTimestamp64(); - transaction.reference = _reference; + transaction.periodId = periodId; Period storage period = periods[periodId]; if (period.firstTransactionId == NO_TRANSACTION) { period.firstTransactionId = transactionId; } - emit NewTransaction(transactionId, _incoming, _entity, _amount); + emit NewTransaction(transactionId, _incoming, _entity, _amount, _reference); } function _tryTransitionAccountingPeriod(uint256 _maxTransitions) internal returns (bool success) { diff --git a/apps/finance/test/finance.js b/apps/finance/test/finance.js index 6463d66108..41aedbc3ec 100644 --- a/apps/finance/test/finance.js +++ b/apps/finance/test/finance.js @@ -7,6 +7,8 @@ const MiniMeToken = artifacts.require('MiniMeToken') const getContract = name => artifacts.require(name) +const getEventData = (receipt, event, arg) => receipt.logs.filter(log => log.event == event)[0].args[arg] + contract('Finance App', accounts => { let daoFact, financeBase, finance, vaultBase, vault, token1, token2, executionTarget, etherToken = {} @@ -162,9 +164,9 @@ contract('Finance App', accounts => { it('records ERC20 deposits', async () => { await token1.approve(finance.address, 5) - await finance.deposit(token1.address, 5, 'ref') + const receipt = await finance.deposit(token1.address, 5, 'ref') - const [periodId, amount, paymentId, paymentRepeatNumber, token, entity, incoming, date, ref] = await finance.getTransaction(1) + const [periodId, amount, paymentId, paymentRepeatNumber, token, entity, incoming, date] = await finance.getTransaction(1) // vault has 100 token1 initially assert.equal((await token1.balanceOf(vault.address)).toString(), 100 + 5, 'deposited tokens must be in vault') @@ -176,7 +178,7 @@ contract('Finance App', accounts => { assert.equal(entity, accounts[0], 'entity should be correct') assert.isTrue(incoming, 'tx should be incoming') assert.equal(date, 1, 'date should be correct') - assert.equal(ref, 'ref', 'ref should be correct') + assert.equal(getEventData(receipt, 'NewTransaction', 'reference'), 'ref', 'ref should be correct') }) it('fails on no value ERC20 deposits', async () => { @@ -192,7 +194,7 @@ contract('Finance App', accounts => { const transactionId = receipt.logs.filter(log => log.event == 'NewTransaction')[0].args.transactionId - const [periodId, amount, paymentId, paymentRepeatNumber, token, entity, incoming, date, ref] = await finance.getTransaction(transactionId) + const [periodId, amount, paymentId, paymentRepeatNumber, token, entity, incoming, date] = await finance.getTransaction(transactionId) assert.equal(await vault.balance(ETH), VAULT_INITIAL_ETH_BALANCE + sentWei, 'deposited ETH must be in vault') assert.equal(periodId, 0, 'period id should be correct') @@ -203,7 +205,7 @@ contract('Finance App', accounts => { assert.equal(entity, accounts[0], 'entity should be correct') assert.isTrue(incoming, 'tx should be incoming') assert.equal(date, 1, 'date should be correct') - assert.equal(ref, reference, 'ref should be correct') + assert.equal(getEventData(receipt, 'NewTransaction', 'reference'), reference, 'ref should be correct') }) it('records ETH deposits using fallback', async () => { @@ -211,7 +213,7 @@ contract('Finance App', accounts => { const receipt = await finance.send(sentWei) const transactionId = receipt.logs.filter(log => log.event == 'NewTransaction')[0].args.transactionId - const [periodId, amount, paymentId, paymentRepeatNumber, token, entity, incoming, date, ref] = await finance.getTransaction(transactionId) + const [periodId, amount, paymentId, paymentRepeatNumber, token, entity, incoming, date] = await finance.getTransaction(transactionId) assert.equal(await vault.balance(ETH), VAULT_INITIAL_ETH_BALANCE + sentWei, 'deposited ETH must be in vault') assert.equal(periodId, 0, 'period id should be correct') @@ -222,7 +224,7 @@ contract('Finance App', accounts => { assert.equal(entity, accounts[0], 'entity should be correct') assert.isTrue(incoming, 'tx should be incoming') assert.equal(date, 1, 'date should be correct') - assert.equal(ref, 'Ether transfer to Finance app', 'ref should be correct') + assert.equal(getEventData(receipt, 'NewTransaction', 'reference'), 'Ether transfer to Finance app', 'ref should be correct') }) context('locked tokens', () => { @@ -239,9 +241,9 @@ contract('Finance App', accounts => { }) it('are recovered using Finance#recoverToVault', async () => { - await finance.recoverToVault(token1.address) + const receipt = await finance.recoverToVault(token1.address) - const [periodId, amount, paymentId, paymentRepeatNumber, token, entity, incoming, date, ref] = await finance.getTransaction(1) + const [periodId, amount, paymentId, paymentRepeatNumber, token, entity, incoming, date] = await finance.getTransaction(1) let finalBalance = await token1.balanceOf(vault.address) assert.equal(finalBalance.toString(), initialBalance.plus(5).toString(), 'deposited tokens must be in vault') @@ -254,7 +256,7 @@ contract('Finance App', accounts => { assert.equal(entity, finance.address, 'entity should be correct') assert.isTrue(incoming, 'tx should be incoming') assert.equal(date, 1, 'date should be correct') - assert.equal(ref, 'Recover to Vault', 'ref should be correct') + assert.equal(getEventData(receipt, 'NewTransaction', 'reference'), 'Recover to Vault', 'ref should be correct') }) it('fail to be recovered using AragonApp#transferToVault', async () => { @@ -284,9 +286,9 @@ contract('Finance App', accounts => { }) it('is recovered using Finance#recoverToVault', async () => { - await finance.recoverToVault(ETH) + const receipt = await finance.recoverToVault(ETH) - const [periodId, amount, paymentId, paymentRepeatNumber, token, entity, incoming, date, ref] = await finance.getTransaction(1) + const [periodId, amount, paymentId, paymentRepeatNumber, token, entity, incoming, date] = await finance.getTransaction(1) assert.equal(await vault.balance(ETH), VAULT_INITIAL_ETH_BALANCE + lockedETH, 'recovered ETH must be in vault') assert.equal((await getBalance(finance.address)).toNumber(), 0, 'finance shouldn\'t have ETH') @@ -298,7 +300,7 @@ contract('Finance App', accounts => { assert.equal(entity, finance.address, 'entity should be correct') assert.isTrue(incoming, 'tx should be incoming') assert.equal(date, 1, 'date should be correct') - assert.equal(ref, 'Recover to Vault', 'ref should be correct') + assert.equal(getEventData(receipt, 'NewTransaction', 'reference'), 'Recover to Vault', 'ref should be correct') }) it('fails to be recovered using AragonApp#transferToVault', async () => { @@ -370,9 +372,9 @@ contract('Finance App', accounts => { it('records payment', async () => { const amount = 10 // repeats up to 10 times every 2 seconds - await finance.newPayment(token1.address, recipient, amount, time, 2, 10, 'ref') + const receipt = await finance.newPayment(token1.address, recipient, amount, time, 2, 10, 'ref') - const [token, receiver, txAmount, initialTime, interval, maxRepeats, ref, disabled, repeats, createdBy] = await finance.getPayment(1) + const [token, receiver, txAmount, initialTime, interval, maxRepeats, disabled, repeats, createdBy] = await finance.getPayment(1) assert.equal(token, token1.address, 'token address should match') assert.equal(receiver, recipient, 'receiver should match') @@ -380,7 +382,7 @@ contract('Finance App', accounts => { assert.equal(initialTime, time, 'time should match') assert.equal(interval, 2, 'interval should match') assert.equal(maxRepeats, 10, 'max repeats should match') - assert.equal(ref, 'ref', 'ref should match') + assert.equal(getEventData(receipt, 'NewPayment', 'reference'), 'ref', 'ref should match') assert.isFalse(disabled, 'should be enabled') assert.equal(repeats, 1, 'should be on repeat 1') assert.equal(createdBy, accounts[0], 'should have correct creator') @@ -413,11 +415,11 @@ contract('Finance App', accounts => { const amount = 10 // interval 0, repeat 1 (single payment) - await finance.newPayment(token1.address, recipient, amount, time, 0, 1, 'ref') + const receipt = await finance.newPayment(token1.address, recipient, amount, time, 0, 1, 'ref') assert.equal((await token1.balanceOf(recipient)).toString(), amount, 'recipient should have received tokens') - const [periodId, txAmount, paymentId, paymentRepeatNumber, token, entity, isIncoming, date, ref] = await finance.getTransaction(1) + const [periodId, txAmount, paymentId, paymentRepeatNumber, token, entity, isIncoming, date] = await finance.getTransaction(1) assert.equal(periodId, 0, 'period id should be correct') assert.equal(txAmount, amount, 'amount should match') assert.equal(paymentId, 0, 'payment id should be 0 for single payment') @@ -426,7 +428,7 @@ contract('Finance App', accounts => { assert.equal(entity, recipient, 'receiver should match') assert.isFalse(isIncoming, 'single payment should be outgoing') assert.equal(date.toNumber(), time, 'date should be correct') - assert.equal(ref, 'ref', 'ref should match') + assert.equal(getEventData(receipt, 'NewTransaction', 'reference'), 'ref', 'ref should match') }) it('can decrease budget after spending', async () => { @@ -473,7 +475,7 @@ contract('Finance App', accounts => { const repeatNum = index + 1 const transactionId = receipt.logs.filter(log => log.event == 'NewTransaction')[0].args.transactionId - const [periodId, txAmount, paymentId, paymentRepeatNumber, token, entity, incoming, date, ref] = await finance.getTransaction(transactionId) + const [periodId, txAmount, paymentId, paymentRepeatNumber, token, entity, incoming, date] = await finance.getTransaction(transactionId) assert.equal(txAmount, amount, 'amount should be correct') assert.equal(paymentId, 1, 'payment id should be 1') @@ -584,7 +586,7 @@ contract('Finance App', accounts => { const receipt = await finance.send(sentWei, { gas: 3e5 }) const transactionId = receipt.logs.filter(log => log.event == 'NewTransaction')[0].args.transactionId - const [periodId, amount, paymentId, paymentRepeatNumber, token, entity, incoming, date, ref] = await finance.getTransaction(transactionId) + const [periodId, amount, paymentId, paymentRepeatNumber, token, entity, incoming, date] = await finance.getTransaction(transactionId) assert.equal(amount, sentWei, 'app should have received ETH and sent it to vault') assert.equal((await getBalance(vault.address)).toNumber(), prevVaultBalance + sentWei, 'app should have received ETH and sent it to vault') diff --git a/apps/survey/contracts/Survey.sol b/apps/survey/contracts/Survey.sol index 5e4182d6e2..0ee634ac54 100644 --- a/apps/survey/contracts/Survey.sol +++ b/apps/survey/contracts/Survey.sol @@ -50,14 +50,12 @@ contract Survey is AragonApp { } struct SurveyStruct { - address creator; uint64 startDate; uint64 snapshotBlock; uint64 minParticipationPct; uint256 options; uint256 votingPower; // total tokens that can cast a vote uint256 participation; // tokens that casted a vote - string metadata; // Note that option IDs are from 1 to `options`, due to ABSTAIN_VOTE taking 0 mapping (uint256 => uint256) optionPower; // option ID -> voting power for option @@ -72,7 +70,7 @@ contract Survey is AragonApp { mapping (uint256 => SurveyStruct) internal surveys; uint256 public surveysLength; - event StartSurvey(uint256 indexed surveyId); + event StartSurvey(uint256 indexed surveyId, address indexed creator, string metadata); event CastVote(uint256 indexed surveyId, address indexed voter, uint256 option, uint256 stake, uint256 optionPower); event ResetVote(uint256 indexed surveyId, address indexed voter, uint256 option, uint256 previousStake, uint256 optionPower); event ChangeMinParticipation(uint64 minParticipationPct); @@ -130,18 +128,18 @@ contract Survey is AragonApp { * @return surveyId id for newly created survey */ function newSurvey(string _metadata, uint256 _options) external auth(CREATE_SURVEYS_ROLE) returns (uint256 surveyId) { + uint256 votingPower = token.totalSupplyAt(survey.snapshotBlock); + require(votingPower > 0, ERROR_NO_VOTING_POWER); + surveyId = surveysLength++; SurveyStruct storage survey = surveys[surveyId]; - survey.creator = msg.sender; survey.startDate = getTimestamp64(); - survey.options = _options; - survey.metadata = _metadata; survey.snapshotBlock = getBlockNumber64() - 1; // avoid double voting in this very block - survey.votingPower = token.totalSupplyAt(survey.snapshotBlock); - require(survey.votingPower > 0, ERROR_NO_VOTING_POWER); survey.minParticipationPct = minParticipationPct; + survey.options = _options; + survey.votingPower = votingPower; - emit StartSurvey(surveyId); + emit StartSurvey(surveyId, msg.sender, _metadata); } /** @@ -262,7 +260,6 @@ contract Survey is AragonApp { surveyExists(_surveyId) returns ( bool _open, - address _creator, uint64 _startDate, uint64 _snapshotBlock, uint64 _minParticipationPct, @@ -274,7 +271,6 @@ contract Survey is AragonApp { SurveyStruct storage survey = surveys[_surveyId]; _open = _isSurveyOpen(survey); - _creator = survey.creator; _startDate = survey.startDate; _snapshotBlock = survey.snapshotBlock; _minParticipationPct = survey.minParticipationPct; @@ -283,10 +279,6 @@ contract Survey is AragonApp { _options = survey.options; } - function getSurveyMetadata(uint256 _surveyId) public view surveyExists(_surveyId) returns (string) { - return surveys[_surveyId].metadata; - } - /* solium-disable-next-line function-order */ function getVoterState(uint256 _surveyId, address _voter) external diff --git a/apps/survey/test/survey.js b/apps/survey/test/survey.js index 935f50c8c8..dcf910bddf 100644 --- a/apps/survey/test/survey.js +++ b/apps/survey/test/survey.js @@ -4,7 +4,8 @@ const timeTravel = require('@aragon/test-helpers/timeTravel')(web3) const getContract = name => artifacts.require(name) const pct16 = x => new web3.BigNumber(x).times(new web3.BigNumber(10).toPower(16)) -const createdSurveyId = receipt => receipt.logs.filter(x => x.event == 'StartSurvey')[0].args.surveyId +const surveyEvent = receipt => receipt.logs.filter(x => x.event == 'StartSurvey')[0].args +const createdSurveyId = receipt => surveyEvent(receipt).surveyId contract('Survey app', accounts => { let daoFact, surveyBase, survey @@ -101,11 +102,14 @@ contract('Survey app', accounts => { let optionsCount = 10e9 // lots of options beforeEach(async () => { - surveyId = createdSurveyId(await survey.newSurvey('metadata', optionsCount, { from: nonHolder })) + createdEvent = surveyEvent(await survey.newSurvey('metadata', optionsCount, { from: nonHolder })) + surveyId = createdEvent.surveyId + creator = createdEvent.creator + metadata = createdEvent.metadata }) it('has correct state', async () => { - const [isOpen, creator, startDate, snapshotBlock, minParticipationPct, totalVoters, participation, options] = await survey.getSurvey(surveyId) + const [isOpen, startDate, snapshotBlock, minParticipationPct, totalVoters, participation, options] = await survey.getSurvey(surveyId) assert.isTrue(isOpen, 'survey should be open') assert.equal(creator, nonHolder, 'creator should be correct') @@ -114,7 +118,7 @@ contract('Survey app', accounts => { assert.equal(totalVoters, 100, 'total voters should be 100') assert.equal(participation, 0, 'initial participation should be 0') // didn't vote even though creator was holder assert.equal(options, optionsCount, 'number of options should be correct') - assert.equal(await survey.getSurveyMetadata(surveyId), 'metadata', 'should have returned correct metadata') + assert.equal(metadata, 'metadata', 'should have returned correct metadata') const voterState = await survey.getVoterState(surveyId, nonHolder) assert.equal(voterState[0].length, 0, 'nonHolder should not have voted (options)') assert.equal(voterState[1].length, 0, 'nonHolder should not have voted (stakes)') @@ -152,7 +156,7 @@ contract('Survey app', accounts => { const state = await survey.getSurvey(surveyId) - assert.equal(state[6], 100, 'participation should be 100') + assert.equal(state[5], 100, 'participation should be 100') }) @@ -207,7 +211,7 @@ contract('Survey app', accounts => { await timeTravel(surveyTime + 1) const state = await survey.getSurvey(surveyId) - assert.deepEqual(state[4], minimumAcceptanceParticipationPct, 'acceptance participation in survey should stay equal') + assert.deepEqual(state[3], minimumAcceptanceParticipationPct, 'acceptance participation in survey should stay equal') }) it('token transfers dont affect voting', async () => { diff --git a/apps/voting/contracts/Voting.sol b/apps/voting/contracts/Voting.sol index 69ac013f5e..4a8c4cf863 100644 --- a/apps/voting/contracts/Voting.sol +++ b/apps/voting/contracts/Voting.sol @@ -37,7 +37,6 @@ contract Voting is IForwarder, AragonApp { enum VoterState { Absent, Yea, Nay } struct Vote { - address creator; bool executed; uint64 startDate; uint64 snapshotBlock; @@ -46,7 +45,6 @@ contract Voting is IForwarder, AragonApp { uint256 yea; uint256 nay; uint256 totalVoters; - string metadata; bytes executionScript; mapping (address => VoterState) voters; } @@ -60,7 +58,7 @@ contract Voting is IForwarder, AragonApp { mapping (uint256 => Vote) internal votes; uint256 public votesLength; - event StartVote(uint256 indexed voteId); + event StartVote(uint256 indexed voteId, address indexed creator, string metadata); event CastVote(uint256 indexed voteId, address indexed voter, bool supports, uint256 stake); event ExecuteVote(uint256 indexed voteId); event ChangeSupportRequired(uint64 supportRequiredPct); @@ -239,7 +237,6 @@ contract Voting is IForwarder, AragonApp { returns ( bool open, bool executed, - address creator, uint64 startDate, uint64 snapshotBlock, uint64 supportRequired, @@ -254,7 +251,6 @@ contract Voting is IForwarder, AragonApp { open = _isVoteOpen(vote_); executed = vote_.executed; - creator = vote_.creator; startDate = vote_.startDate; snapshotBlock = vote_.snapshotBlock; supportRequired = vote_.supportRequiredPct; @@ -265,10 +261,6 @@ contract Voting is IForwarder, AragonApp { script = vote_.executionScript; } - function getVoteMetadata(uint256 _voteId) public view voteExists(_voteId) returns (string) { - return votes[_voteId].metadata; - } - function getVoterState(uint256 _voteId, address _voter) public view voteExists(_voteId) returns (VoterState) { return votes[_voteId].voters[_voter]; } @@ -277,19 +269,19 @@ contract Voting is IForwarder, AragonApp { internal returns (uint256 voteId) { + uint256 totalVoters = token.totalSupplyAt(vote_.snapshotBlock); + require(totalVoters > 0, ERROR_NO_VOTING_POWER); + voteId = votesLength++; Vote storage vote_ = votes[voteId]; - vote_.executionScript = _executionScript; - vote_.creator = msg.sender; vote_.startDate = getTimestamp64(); - vote_.metadata = _metadata; vote_.snapshotBlock = getBlockNumber64() - 1; // avoid double voting in this very block - vote_.totalVoters = token.totalSupplyAt(vote_.snapshotBlock); - require(vote_.totalVoters > 0, ERROR_NO_VOTING_POWER); vote_.supportRequiredPct = supportRequiredPct; vote_.minAcceptQuorumPct = minAcceptQuorumPct; + vote_.totalVoters = totalVoters; + vote_.executionScript = _executionScript; - emit StartVote(voteId); + emit StartVote(voteId, msg.sender, _metadata); if (_castVote && canVote(voteId, msg.sender)) { _vote(voteId, true, msg.sender, _executesIfDecided); diff --git a/apps/voting/contracts/test/mocks/VotingMock.sol b/apps/voting/contracts/test/mocks/VotingMock.sol index 3103faba18..e5987ae45c 100644 --- a/apps/voting/contracts/test/mocks/VotingMock.sol +++ b/apps/voting/contracts/test/mocks/VotingMock.sol @@ -13,7 +13,7 @@ contract VotingMock is Voting { returns (uint256 voteId) { voteId = _newVote(_executionScript, _metadata, _castVote, _executesIfDecided); - emit StartVote(voteId); + emit StartVote(voteId, msg.sender, _metadata); } // _isValuePct public wrapper diff --git a/apps/voting/test/voting.js b/apps/voting/test/voting.js index abf6fecc47..25c64016e5 100644 --- a/apps/voting/test/voting.js +++ b/apps/voting/test/voting.js @@ -18,7 +18,8 @@ const Voting = artifacts.require('VotingMock') const getContract = name => artifacts.require(name) const bigExp = (x, y) => new web3.BigNumber(x).times(new web3.BigNumber(10).toPower(y)) const pct16 = x => bigExp(x, 16) -const createdVoteId = receipt => receipt.logs.filter(x => x.event == 'StartVote')[0].args.voteId +const startVoteEvent = receipt => receipt.logs.filter(x => x.event == 'StartVote')[0].args +const createdVoteId = receipt => startVoteEvent(receipt).voteId const ANY_ADDR = '0xffffffffffffffffffffffffffffffffffffffff' const NULL_ADDRESS = '0x00' @@ -205,16 +206,19 @@ contract('Voting App', accounts => { }) context('creating vote', () => { - let script, voteId + let script, voteId, creator, metadata beforeEach(async () => { const action = { to: executionTarget.address, calldata: executionTarget.contract.execute.getData() } script = encodeCallScript([action, action]) - voteId = createdVoteId(await voting.newVoteExt(script, 'metadata', false, false, { from: holder51 })) + const startVote = startVoteEvent(await voting.newVoteExt(script, 'metadata', false, false, { from: holder51 })) + voteId = startVote.voteId + creator = startVote.creator + metadata = startVote.metadata }) it('has correct state', async () => { - const [isOpen, isExecuted, creator, startDate, snapshotBlock, supportRequired, minQuorum, y, n, totalVoters, execScript] = await voting.getVote(voteId) + const [isOpen, isExecuted, startDate, snapshotBlock, supportRequired, minQuorum, y, n, totalVoters, execScript] = await voting.getVote(voteId) assert.isTrue(isOpen, 'vote should be open') assert.isFalse(isExecuted, 'vote should not be executed') @@ -226,7 +230,7 @@ contract('Voting App', accounts => { assert.equal(n, 0, 'initial nay should be 0') assert.equal(totalVoters.toString(), bigExp(100, decimals).toString(), 'total voters should be 100') assert.equal(execScript, script, 'script should be correct') - assert.equal(await voting.getVoteMetadata(voteId), 'metadata', 'should have returned correct metadata') + assert.equal(metadata, 'metadata', 'should have returned correct metadata') assert.equal(await voting.getVoterState(voteId, nonHolder), VOTER_STATE.ABSENT, 'nonHolder should not have voted') }) @@ -249,7 +253,7 @@ contract('Voting App', accounts => { await timeTravel(votingTime + 1) const state = await voting.getVote(voteId) - assert.equal(state[5].toNumber(), neededSupport.toNumber(), 'required support in vote should stay equal') + assert.equal(state[4].toNumber(), neededSupport.toNumber(), 'required support in vote should stay equal') await voting.executeVote(voteId) // exec doesn't fail }) @@ -264,7 +268,7 @@ contract('Voting App', accounts => { await timeTravel(votingTime + 1) const state = await voting.getVote(voteId) - assert.equal(state[6].toNumber(), minimumAcceptanceQuorum.toNumber(), 'acceptance quorum in vote should stay equal') + assert.equal(state[5].toNumber(), minimumAcceptanceQuorum.toNumber(), 'acceptance quorum in vote should stay equal') await voting.executeVote(voteId) // exec doesn't fail }) @@ -273,7 +277,7 @@ contract('Voting App', accounts => { const state = await voting.getVote(voteId) const voterState = await voting.getVoterState(voteId, holder29) - assert.equal(state[8].toString(), bigExp(29, decimals).toString(), 'nay vote should have been counted') + assert.equal(state[7].toString(), bigExp(29, decimals).toString(), 'nay vote should have been counted') assert.equal(voterState, VOTER_STATE.NAY, 'holder29 should have nay voter status') }) @@ -283,8 +287,8 @@ contract('Voting App', accounts => { await voting.vote(voteId, true, true, { from: holder29 }) const state = await voting.getVote(voteId) - assert.equal(state[7].toString(), bigExp(29, decimals).toString(), 'yea vote should have been counted') - assert.equal(state[8], 0, 'nay vote should have been removed') + assert.equal(state[6].toString(), bigExp(29, decimals).toString(), 'yea vote should have been counted') + assert.equal(state[7], 0, 'nay vote should have been removed') }) it('token transfers dont affect voting', async () => { @@ -293,7 +297,7 @@ contract('Voting App', accounts => { await voting.vote(voteId, true, true, { from: holder29 }) const state = await voting.getVote(voteId) - assert.equal(state[7].toString(), bigExp(29, decimals).toString(), 'yea vote should have been counted') + assert.equal(state[6].toString(), bigExp(29, decimals).toString(), 'yea vote should have been counted') assert.equal(await token.balanceOf(holder29), 0, 'balance should be 0 at current block') })