forked from Reach-Insurance/react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
66 lines (54 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
import { loadStdlib } from '@reach-sh/stdlib';
const stdlib = await loadStdlib();
import * as backend from './build/index.main.mjs';
//const startingBalance = stdlib.parseCurrency(100);
//REF: https://docs.reach.sh/frontend/#ref-frontends-js-acc
const insurerAccount = await stdlib.newAccountFromMnemonic("replace this with the twelve word secret phrase for the insurer account");
stdlib.fundFromFaucet(insurerAccount, 100); //works on public or private testnets
//==============================contract deployer===========================
const insuranceContract = insurerAccount.contract(backend);
await insuranceContract.participants.Insurer({
getSale: () => {
console.log(`Insurer sets parameters of sale:`, params);
return params;
},
auctionReady: () => {
startBidders();
},
seeBid: (who, amt) => {
console.log(`Insurer saw that ${stdlib.formatAddress(who)} bid ${stdlib.formatCurrency(amt)}.`);
},
showOutcome: (winner, amt) => {
console.log(`Insurer saw that ${stdlib.formatAddress(winner)} won with ${stdlib.formatCurrency(amt)}`);
},
});
//============================participants=============================
const startBidders = async () => {
let bid = minBid;
const runBidder = async (who) => {
const inc = stdlib.parseCurrency(Math.random() * 10);
bid = bid.add(inc);
//const acc = await stdlib.newTestAccount(startingBalance);
const acc = await stdlib.newAccountFromMnemonic("member will replace this with their twelve word secret phrase");
acc.setDebugLabel(who);
await acc.tokenAccept(nftId);
bidders.push([who, acc]);
const contractHandle = acc.contract(backend, insuranceContract.getInfo());
const getBal = async () => stdlib.formatCurrency(await stdlib.balanceOf(acc));
console.log(`${who} decides to bid ${stdlib.formatCurrency(bid)}.`);
console.log(`${who} balance before is ${await getBal()}`);
try {
const [ lastBidder, lastBid ] = await contractHandle.apis.Bidder.bid(bid);
console.log(`${who} out bid ${lastBidder} who bid ${stdlib.formatCurrency(lastBid)}.`);
} catch (e) {
console.log(`${who} failed to bid, because the auction is over`);
}
console.log(`${who} balance after is ${await getBal()}`);
};
await runBidder('Alice');
await runBidder('Bob');
await runBidder('Claire');
while ( ! done ) {
await stdlib.wait(1);
}
};