-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathCongress.js
More file actions
185 lines (146 loc) · 7.3 KB
/
Congress.js
File metadata and controls
185 lines (146 loc) · 7.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
const web3 = global.web3;
contract('Congress', function(accounts) {
//Congress initial params for testing
const congressInitialParams = {
minimumQuorumForProposals: 3,
minutesForDebate: 5,
marginOfVotesForMajority: 2,
congressLeader: accounts[0]
};
//Proposal params for testing
const proposalParams = {
beneficiary: accounts[9],
etherAmount: 1,
JobDescription: 'Some job description',
transactionBytecode: web3.sha3('some content')
};
//positions of field from Proposal struct
// struct Proposal {
// address recipient;
// uint amount;
// string description;
// uint votingDeadline;
// bool executed;
// bool proposalPassed;
// uint numberOfVotes;
// int currentResult;
// bytes32 proposalHash;
// Vote[] votes;
// mapping (address => bool) voted;
// }
const PROPOSAL_VOTING_DEADLINE_FIELD = 3;
const PROPOSAL_EXECUTED_FIELD = 4;
const PROPOSAL_PASSED_FIELD = 4;
const PROPOSAL_NUMBER_OF_VOTES_FIELD = 6;
const PROPOSAL_CURRENT_RESULT = 7;
let congress;
//create new smart contract instance before each test method
beforeEach(async function() {
congress = await Congress.new(...Object.values(congressInitialParams), {
value: web3.toWei(2, 'ether') // money for proposals
});
});
it("should set initial attributes", async function() {
assert.equal(await congress.minimumQuorum(), congressInitialParams.minimumQuorumForProposals);
assert.equal(await congress.debatingPeriodInMinutes(), congressInitialParams.minutesForDebate);
assert.equal(await congress.majorityMargin(), congressInitialParams.marginOfVotesForMajority);
//by default tx goes from accounts[0]
assert.equal(await congress.owner(), accounts[0]);
});
it("should allow owner to add members", async function() {
//try to add 3 members
for (let i = 1; i <= 3; i++) {
let addResult = await congress.addMember(accounts[i], 'Name for account ' + i);
//members array positions starts from 2.
//members[0] - empty, members[1] - owner/founder, who deployed contract
let memberInfoFromContract = await congress.members(i + 1);
assert.equal(memberInfoFromContract[0], accounts[i]);
assert.equal(memberInfoFromContract[1], 'Name for account ' + i);
}
});
it("should disallow no owner to add members", async function() {
let addError;
try {
//contract throws error here
await congress.addMember(accounts[1], 'Name for account 1', {
from: accounts[9]
});
} catch (error) {
addError = error;
}
assert.notEqual(addError, undefined, 'Error must be thrown');
assert.isAbove(addError.message.search('invalid JUMP'), -1, 'invalid JUMP error must be returned');
});
it("should set member attribute 'memberSince' (uint) equal to block creation", async function() {
let addResultTx = await congress.addMember(accounts[5], 'Name for account 5');
let memberInfoFromContract = await congress.members(2);
//Information about block by tx id, returned by "addMember" changing state method
let block = web3.eth.getBlock(web3.eth.getTransactionReceipt(addResultTx).blockNumber);
//3rd attr of "Member" struct - now , m
assert.equal(memberInfoFromContract[2], block.timestamp);
});
it("should fire event 'ProposalAdded' when member add proposal", async function() {
let proposedAddedEvetListener = congress.ProposalAdded();
await congress.addMember(accounts[5], 'Name for account 5');
await congress.newProposal(...Object.values(proposalParams), {
from: accounts[5]
});
let proposalAddedLog = await new Promise(
(resolve, reject) => proposedAddedEvetListener.get(
(error, log) => error ? reject(error) : resolve(log)
));
assert.equal(proposalAddedLog.length, 1, 'should be 1 event');
let eventArgs = proposalAddedLog[0].args;
assert.equal(eventArgs.proposalID, 0);
assert.equal(eventArgs.recipient, proposalParams.beneficiary);
assert.equal(eventArgs.amount, proposalParams.etherAmount);
assert.equal(eventArgs.description, proposalParams.JobDescription);
});
it("should pay for executed proposal", async function() {
let curAccount9Balance = web3.eth.getBalance(accounts[9]).toNumber();
//proposal votingDeadline = now + debatingPeriodInMinutes (2 - set in Congres constructor)* 1 minutes;
await congress.newProposal(...Object.values(proposalParams), {
from: accounts[0] //account[0] already member
});
// we need minimumQuorumForProposals = 3 votes (setted in constructor)
// so add 3 members and vote
for (let i of[3, 4, 5]) {
await congress.addMember(accounts[i], 'Name for account ' + i);
//vote from account[i] for proposal with position = 0, with support = true
await congress.vote(0, true, 'Some justification text from account ' + i, {
from: accounts[i]
});
}
let curProposalState = await congress.proposals(0);
// increase time for 10 minutes, more then minutesForDebate (5)
await new Promise((resolve, reject) =>
web3.currentProvider.sendAsync({
jsonrpc: "2.0",
method: "evm_increaseTime",
params: [10 * 600],
id: new Date().getTime()
}, (error, result) => error ? reject(error) : resolve(result.result))
);
//check that we are ready for execute proposal
assert.equal(curProposalState[PROPOSAL_EXECUTED_FIELD], false, 'proposal not yet executed');
assert.equal(curProposalState[PROPOSAL_NUMBER_OF_VOTES_FIELD], 3, 'proposal now have 3 votes');
assert.isAtLeast(curProposalState[PROPOSAL_NUMBER_OF_VOTES_FIELD],
congressInitialParams.minimumQuorumForProposals,
'current proposal votes amount is more or even to minimumQuorumForProposals');
assert.isAbove(curProposalState[PROPOSAL_CURRENT_RESULT],
congressInitialParams.marginOfVotesForMajority,
'current proposal result is more or even to marginOfVotesForMajority');
assert.isNotOk(curProposalState[PROPOSAL_PASSED_FIELD]);
assert.isOk(await congress.checkProposalCode(
0, proposalParams.beneficiary, proposalParams.etherAmount, proposalParams.transactionBytecode));
//so we have all conditions for executig proposal:
//3 votes for proposal, time more then "minutesForDebate"
//method executeProposal for proposal 0 should work
await congress.executeProposal(0, proposalParams.transactionBytecode);
let newAccount9Balance = web3.eth.getBalance(accounts[9]).toNumber();
assert.equal(web3.fromWei(newAccount9Balance - curAccount9Balance, 'ether'), proposalParams.etherAmount,
'balance of acccount[9] must increase to proposalParams.etherAmount');
let newProposalState = await congress.proposals(0);
assert.isOk(newProposalState[PROPOSAL_PASSED_FIELD]);
});
});