forked from jurteam/mvp-smart-contract
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02_simple_dispute.js
178 lines (154 loc) · 7.6 KB
/
02_simple_dispute.js
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
const assertFail = require("./helpers/assertFail");
const ArbitrationFactory = artifacts.require("./ArbitrationFactoryMock.sol");
const Arbitration = artifacts.require("./ArbitrationMock.sol");
const JURToken = artifacts.require("./JURToken.sol");
contract('Arbitration - Simple dispute', function (accounts) {
var token;
var arbitrationFactory;
var arbitration;
var party1 = accounts[1];
var party2 = accounts[2];
var voter1 = accounts[3];
var voter2 = accounts[4];
var voter3 = accounts[5];
var voter4 = accounts[6];
// =========================================================================
it("0. initialize token contract and arbitration factory contract", async () => {
token = await JURToken.new(["sig1"], {from: accounts[0]});
console.log("JUR Token Address: ", token.address);
//Mint some tokens for party1 and party2
await token.mint(party1, 50, {from: accounts[0]});
await token.mint(party2, 150, {from: accounts[0]});
await token.mint(voter1, 100, {from: accounts[0]});
await token.mint(voter2, 100, {from: accounts[0]});
await token.mint(voter3, 100, {from: accounts[0]});
await token.mint(voter4, 100, {from: accounts[0]});
//Initialise arbitration contract
arbitrationFactory = await ArbitrationFactory.new(token.address, {from: accounts[0]});
console.log("Arbitration Factory Address: ", arbitrationFactory.address);
});
it("1. create new arbitration - state is unsigned", async () => {
let tx = await arbitrationFactory.createArbitration([party1, party2], [0, 200], [50, 150], "Do some work...");
arbitration = Arbitration.at(tx.logs[0].args._arbitration);
console.log("Arbitration Address: " + arbitration.address);
let party1Dispersal = await arbitration.dispersal(party1);
let party2Dispersal = await arbitration.dispersal(party2);
let party1Funding = await arbitration.funding(party1);
let party2Funding = await arbitration.funding(party2);
assert.equal(party1Dispersal.toNumber(), 0);
assert.equal(party2Dispersal.toNumber(), 200);
assert.equal(party1Funding.toNumber(), 50);
assert.equal(party2Funding.toNumber(), 150);
let state = await arbitration.state();
assert.equal(state, 0);
});
it("2. approve arbitration for transfers", async () => {
await token.approve(arbitration.address, 50, {from: party1});
await token.approve(arbitration.address, 150, {from: party2});
await token.approve(arbitration.address, 100, {from: voter1});
await token.approve(arbitration.address, 100, {from: voter2});
await token.approve(arbitration.address, 100, {from: voter3});
await token.approve(arbitration.address, 100, {from: voter4});
});
it("3. only parties can sign arbitration", async () => {
await assertFail(async () => {
await arbitration.sign({from: voter1});
});
});
it("4. party1 signs arbitration", async () => {
await arbitration.sign({from: party1});
});
it("5. party2 signs arbitration - state is now Signed", async () => {
await arbitration.sign({from: party2});
let state = await arbitration.state();
assert.equal(state, 1);
});
it("6. party1 disputes arbitration with insufficient vote - fail", async () => {
await token.mint(party1, 10, {from: accounts[0]});
await token.approve(arbitration.address, 10, {from: party1});
await assertFail(async () => {
await arbitration.dispute(1, [100, 100], {from: party1});
});
});
it("7. party1 disputes arbitration with sufficient vote", async () => {
await arbitration.dispute(10, [100, 100], {from: party1});
let state = await arbitration.state();
assert.equal(state, 3);
assert.equal((await arbitration.disputeDispersal(party1, party1)).toNumber(), 100);
assert.equal((await arbitration.disputeDispersal(party1, party2)).toNumber(), 100);
assert.equal((await arbitration.disputeDispersal(party2, party1)).toNumber(), 0);
assert.equal((await arbitration.disputeDispersal(party2, party2)).toNumber(), 200);
});
it("8. party2 sets their dispute dispersal", async () => {
await arbitration.amendDisputeDispersal([25, 175], {from: party2});
let state = await arbitration.state();
assert.equal(state, 3);
assert.equal((await arbitration.disputeDispersal(party1, party1)).toNumber(), 100);
assert.equal((await arbitration.disputeDispersal(party1, party2)).toNumber(), 100);
assert.equal((await arbitration.disputeDispersal(party2, party1)).toNumber(), 25);
assert.equal((await arbitration.disputeDispersal(party2, party2)).toNumber(), 175);
});
it("9. unable to change dispute dispersal after DISPUTE_DISPERSAL_DURATION", async () => {
await arbitration.setMockedNow(1 * 24 * 60 * 60);
await assertFail(async () => {
await arbitration.amendDisputeDispersal([25, 125], {from: party2});
});
});
it("10. start voting", async () => {
await arbitration.vote(party2, 20, {from: voter1});
await arbitration.vote(party1, 30, {from: voter2});
await arbitration.vote(party2, 30, {from: voter3});
//Party1 has 40 votes, Party2 has 50 votes
assert.equal((await arbitration.totalVotes()).toNumber(), 90);
assert.equal((await arbitration.partyVotes(party1)).toNumber(), 40);
assert.equal((await arbitration.partyVotes(party2)).toNumber(), 50);
});
it("11. unable to vote if ratio between new winner and second best is more than 2", async () => {
await assertFail(async () => {
await arbitration.vote(party2, 31, {from: voter4});
});
});
it("12. dispute ends, no more voting possible", async () => {
await arbitration.setMockedNow(8 * 24 * 60 * 60);
await assertFail(async () => {
await arbitration.vote(party1, 1, {from: voter2});
});
});
it("13. voters and parties unable to withdraw in the first 24 hours", async () => {
await assertFail(async () => {
await arbitration.payoutVoter(0, 10, {from: voter1});
});
});
it("14. voters receive rewards", async () => {
await arbitration.setMockedNow(9 * 24 * 60 * 60);
let voter1Balance = await token.balanceOf(voter1);
let voter2Balance = await token.balanceOf(voter2);
let voter3Balance = await token.balanceOf(voter3);
await arbitration.payoutVoter(0, 10, {from: voter1});
await arbitration.payoutVoter(0, 10, {from: voter2});
await arbitration.payoutVoter(0, 10, {from: voter3});
let voter1FinalBalance = await token.balanceOf(voter1);
let voter2FinalBalance = await token.balanceOf(voter2);
let voter3FinalBalance = await token.balanceOf(voter3);
//Voter1 gets 20 reward tokens, and their original 20 tokens
//Voter2 loses their original 30 tokens
//Voter3 gets 20 reward tokens, and their original 30 tokens
assert.equal(voter1FinalBalance.sub(voter1Balance).toNumber(), 40);
assert.equal(voter2FinalBalance.sub(voter2Balance).toNumber(), 0);
assert.equal(voter3FinalBalance.sub(voter3Balance).toNumber(), 50);
});
it("15. parties receive payouts", async () => {
let party1Balance = await token.balanceOf(party1);
let party2Balance = await token.balanceOf(party2);
await arbitration.payoutParty({from: party1});
await arbitration.payoutParty({from: party2});
await arbitration.payoutVoter(0, 10, {from: party1});
await arbitration.payoutVoter(0, 10, {from: party2});
let party1FinalBalance = await token.balanceOf(party1);
let party2FinalBalance = await token.balanceOf(party2);
//Party1 gets 25 dispersal tokens, and no reward tokens
//Party2 gets 125 dispersal tokens, and no reward tokens
assert.equal(party1FinalBalance.sub(party1Balance).toNumber(), 25);
assert.equal(party2FinalBalance.sub(party2Balance).toNumber(), 175);
});
});