-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.mjs
104 lines (76 loc) · 2.48 KB
/
index.mjs
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
import {loadStdlib} from '@reach-sh/stdlib';
import * as backend from './build/index.main.mjs';
const stdlib = loadStdlib(process.env);
const startingBalance = stdlib.parseCurrency(100);
const accGovernor= await stdlib.newTestAccount( startingBalance);
console.log('Hello, Alice and Bob!');
console.log('Launching...');
const ctcGovernor = accGovernor.contract(backend);
//const ctcBob = accBob.contract(backend, ctcAlice.getInfo());
console.log('Starting backends...');
const voters=[]
const outcome =["Motion supported ","Moton debunked"]
let done = false; // <---------------------------- This was added to ensure the code will wait
const startingVoting=async()=>{
const newAcc=async(who)=>{
const acc = await stdlib.newTestAccount(startingBalance)
const ctc = acc.contract(backend,ctcGovernor.getInfo())
voters.push({who,acc})
const randomBoolean = Math.random() >= 0.5;
try{
const value = await ctc.apis.Voter.vote(randomBoolean)
const word = value ? "voted for" : "voted against"
console.log(`${who} ${word} proposal`)
}catch(e){
console.log(e)
}
}
await newAcc("Bob")
await newAcc("Thomas")
await newAcc("Nick")
await newAcc("sam")
await newAcc("James")
await newAcc("Joy")
await newAcc("Jane")
///---------- Here is the actual waiting code
while ( ! done ) {
await stdlib.wait(1);
}
}
const voterSeeOutcome=()=>{
voters.map(async(voter)=>{
const acc =voter.acc
const who=voter.who
const ctc = acc.contract(backend,ctcGovernor.getInfo());
try {
const res= await ctc.apis.Voter.showOutcome()
console.log(` ${who} saw poll outcome: ${outcome[res]}`)
} catch (error) {
// console.log(error)
console.log("")
}
})
}
await ctcGovernor.p.Governor({
getProposal: () => ({
proposalID:1,
exists:true,
title:"Good",
description:"A good thing",
ticketPrice: stdlib.parseCurrency(5),
deadline: 25,
}),
proposalReady:()=>{
startingVoting()
},
showOutcome:( proposalID,forProposal, againstProposal)=>{
console.log(` alice saw proposal #${ proposalID} poll outcome: ${ forProposal} to ${againstProposal}`)
voterSeeOutcome()
},
showTimeout:(timeout)=>{
console.log(`${timeout} dealine reach `)
}
})
console.log('Goodbye, Alice and Bob!');
/// And here is where it is cleared
done = true;