-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.mjs
More file actions
45 lines (39 loc) · 1.25 KB
/
index.mjs
File metadata and controls
45 lines (39 loc) · 1.25 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
// import
import { loadStdlib } from '@reach-sh/stdlib';
import * as backend from './build/index.main.mjs';
// async
(async () => {
const stdlib = await loadStdlib();
const startingBalance = stdlib.parseCurrency(10);
const accAlice = await stdlib.newTestAccount(startingBalance);
const accBob = await stdlib.newTestAccount(startingBalance);
const ctcAlice = accAlice.deploy(backend);
const ctcBob = accBob.attach(backend, ctcAlice.getInfo());
// define arrays to hold the meaning of the hands and outcomes.
const HAND = ['No', 'Yes'];
const OUTCOME = ['Alice gets 0.00 Algo ', 'No Conensus reached; vote again!', 'Alice gets 5 Algo'];
// defines a constructor for the Player implementation.
const Player = (Who) => ({
// get vote method
getVote: () => {
const hand = Math.floor(Math.random() * 2);
console.log(`${Who} voted ${HAND[hand]}`);
return hand;
},
// See outcome method
seeOutcome: (outcome) => {
console.log(`${Who} saw outcome ${OUTCOME[outcome]}`);
},
});
// instantiate the implementation once for Alice and once for Bob.
await Promise.all([
backend.Alice(
ctcAlice,
Player('Voter 0'),
),
backend.Bob(
ctcBob,
Player('Voter 1'),
),
]);
})();