-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.txt
74 lines (61 loc) · 1.98 KB
/
index.txt
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
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 OUTCOME = ["A Wins", "Draw", "B Wins"]
const commonInteract = (who) => ({
...stdlib.hasRandom,
getCount: () => {
const count = Math.floor(Math.random() * 6);
console.log(`${who} threw ${count} fingers`);
if (Math.random() <= 0.01) {
for ( let i = 0; i < 10; i++) {
console.log(`${who} takes their sweet time sending it back...`);
stdlib.wait(1);
}
}
return count
},
getGuess: () => {
const guess = Math.floor(Math.random() * 11);
console.log(`${who} guessed ${guess} fingers`);
return guess;
},
seeOutcome: (outcome) => {
console.log(`${who} saw the outcome: ${OUTCOME[outcome]}`)
},
informTimeout: () => {
console.log(`${who} observed a timeout`)
}
})
const [ accAlice, accBob ] =
await stdlib.newTestAccounts(2, startingBalance);
console.log('Hello, Alice and Bob!');
const fmt = (x) => stdlib.formatCurrency(x, 4);
const getBalance = async (who) => fmt(await stdlib.balanceOf(who));
const beforeAlice = await getBalance(accAlice);
const beforeBob = await getBalance(accBob);
console.log(`Balance Alice: ${beforeAlice}`);
console.log(`Balance Bob: ${beforeBob}`);
console.log('Launching...');
const ctcAlice = accAlice.contract(backend);
const ctcBob = accBob.contract(backend, ctcAlice.getInfo());
console.log('Starting backends...');
await Promise.all([
ctcAlice.p.Alice({
...commonInteract("Alice"),
wager: stdlib.parseCurrency(5),
deadline: 30,
}),
ctcBob.p.Bob({
...commonInteract("Bob"),
acceptWager: (amt) => {
console.log(`Bob accepts the wager of ${fmt(amt)}`)
}
})
])
const afterAlice = await getBalance(accAlice);
const afterBob = await getBalance(accBob);
console.log(`Balance Alice: ${afterAlice}`);
console.log(`Balance Bob: ${afterBob}`);
console.log('Goodbye, Alice and Bob!');