-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.mjs
76 lines (64 loc) · 2.4 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
import { loadStdlib, getConnector } from '@reach-sh/stdlib';
import * as backend from './build/index.main.mjs';
const numberOfBuyers = 8;
(async () => {
const stdlib = await loadStdlib();
const connector = getConnector();
const startingBalance = stdlib.parseCurrency(100);
const accFunder = await stdlib.newTestAccount(startingBalance);
const accBuyers = await Promise.all(
Array.from({ length: numberOfBuyers }, () =>
stdlib.newTestAccount(startingBalance)));
const getBalance = async (who) =>
stdlib.formatCurrency(await stdlib.balanceOf(who), 4);
const ctcFunder = accFunder.deploy(backend);
const ctcInfo = ctcFunder.getInfo();
const ticketPrice = stdlib.parseCurrency(3); // start ticket price at 3
const unitPrice = stdlib.parseCurrency(1); // keep ticket price constant
const deadline = connector === 'ALGO' ? 4 : 8;
const funderParams = {
deadline,
ticketPrice,
unitPrice,
}
const resultText = (outcome, addr) =>
outcome.includes(addr) ? 'won' : 'lost';
const bidHistory = {};
await Promise.all([
backend.Funder(ctcFunder, {
showOutcome: (outcome) =>
console.log(`Funder saw they ${resultText(outcome, accFunder.getAddress())}`),
getParams: () => funderParams,
showBalance: async () => {
console.log(`Funder balance now ${await getBalance(accFunder)}`)
}
})
].concat(
accBuyers.map((accBuyer, i) => {
const ctcBuyer = accBuyer.attach(backend, ctcInfo);
const Who = `Buyer #${i}`;
return backend.Buyer(ctcBuyer, {
showOutcome: (outcome) =>
console.log(`${Who} saw they ${resultText(outcome, accBuyer.getAddress())}`),
// considering buying if not yet bought yet and
// buyer wants to buy
shouldBuyTicket: () =>
!bidHistory[Who] && Math.random() * 2 > .5,
showPurchase: (addr, price, j) => {
if (stdlib.addressEq(addr, accBuyer, i)) {
console.log(`${j}: ${Who} bought a ticket at price ${price}.`);
bidHistory[Who] = true;
}
},
showBalance: async () => {
console.log(`${Who} balance now ${await getBalance(accBuyer)}`)
}
});
})
));
console.log(`Funder balance now ${await getBalance(accFunder)}`)
accBuyers.map(async (accBuyer, i) => {
const Who = `Buyer #${i}`;
console.log(`${Who} balance now ${await getBalance(accBuyer)}`)
});
})();