-
Notifications
You must be signed in to change notification settings - Fork 168
/
Copy pathindex.mjs
61 lines (53 loc) · 2.08 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
import { loadStdlib } from '@reach-sh/stdlib';
import * as backend from './build/index.main.mjs';
const runDemo = async (delayReceiver, delayFunder) => {
const stdlib = loadStdlib();
const getBalance = async (who) => stdlib.formatCurrency(
await stdlib.balanceOf(who), 4,
);
const MATURITY = 10;
const REFUND = 10;
const DORMANT = 10;
const fDelay = delayFunder ? MATURITY + REFUND + DORMANT + 1 : 0;
const rDelay = delayReceiver ? MATURITY + REFUND + 1 : 0;
console.log(`Begin demo with funder delay(${fDelay}) and receiver delay(${rDelay}).`);
const common = (who, delay = 0) => ({
funded: async () => {
console.log(`${who} sees that the account is funded.`);
if ( delay != 0 ) {
console.log(`${who} begins to wait...`);
await stdlib.wait(delay);
}
},
ready : async () => console.log(`${who} is ready to receive the funds.`),
recvd : async () => console.log(`${who} received the funds.`)
});
const startingBalance = stdlib.parseCurrency(100);
const funder = await stdlib.newTestAccount(startingBalance);
const receiver = await stdlib.newTestAccount(startingBalance);
const bystander = await stdlib.newTestAccount(startingBalance);
const ctcFunder = funder.contract(backend);
const ctcReceiver = receiver.contract(backend, ctcFunder.getInfo());
const ctcBystander = bystander.contract(backend, ctcFunder.getInfo());
await Promise.all([
backend.Funder(ctcFunder, {
...common('Funder', fDelay),
getParams: () => ({
receiverAddr: receiver.networkAccount,
payment: stdlib.parseCurrency(10),
maturity: MATURITY,
refund: REFUND,
dormant: DORMANT,
}),
}),
backend.Receiver(ctcReceiver, common('Receiver', rDelay)),
backend.Bystander(ctcBystander, common('Bystander')),
]);
for (const [who, acc] of [['Funder', funder], ['Receiver', receiver], ['Bystander', bystander]]) {
console.log(`${who} has a balance of ${await getBalance(acc)}`);
}
console.log(`\n`);
};
await runDemo(false, false);
await runDemo(true, false);
await runDemo(true, true);