-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
112 lines (104 loc) · 4.11 KB
/
index.js
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import React from 'react';
import AppViews from './views/AppViews';
import DeployerViews from './views/DeployerViews';
import AttacherViews from './views/AttacherViews';
import {renderDOM, renderView} from './views/render';
import './index.css';
import * as backend from './build/index.main.mjs';
import { loadStdlib } from '@reach-sh/stdlib';
const reach = loadStdlib(process.env);
import { ALGO_MyAlgoConnect as MyAlgoConnect } from '@reach-sh/stdlib';
reach.setWalletFallback(reach.walletFallback({
providerEnv: 'TestNet', MyAlgoConnect }));
const handToInt = [0, 1, 2, 3, 4, 5];
const GUESS = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const intToOutcome = ['Bob wins', 'Draw', 'Alice wins'];
const {standardUnit} = reach;
const defaults = {defaultFundAmt: '10', defaultWager: '3', standardUnit};
class App extends React.Component{
constructor(props){
super(props);
this.state = {view: 'ConnectAccount', ...defaults};
}
async componentDidMount(){
const acc = await reach.getDefaultAccount();
const balAtomic = await reach.balanceOf(acc);
const bal = reach.formatCurrency(balAtomic, 4);
this.setState({acc, bal});
if(await reach.canFundFromFaucet()){
this.setState({view: 'FundAccount'});
}else{
this.setState({view: 'DeployerOrAttacher'});
}
}
render(){return renderView(this, AppViews); }
async fundAccount(fundAmount){
await reach.fundFromFaucet(this.state.acc, reach.parseCurrency(fundAmount));
this.setState({view: 'DeployerOrAttacher'});
}
async skipFundAccount(){this.setState({view: 'DeployerOrAttacher'});}
selectAttacher(){this.setState({view: 'Wrapper', ContentView: Attacher});}
selectDeployer(){this.setState({view: 'Wrapper', ContentView: Deployer});}
}
//use function in backend const Player:....
class Player extends React.Component{
random() {return reach.hasRandom.random();}
async getHand(){//Fun([], UInt)
const hand = await new Promise(resolveHandP => {
this.setState({view: 'GetHand', playable: true, resolveHandP});
});
this.setState({view: 'WaitingForResults', hand});
return handToInt[hand];
}
async getGuess(){//getGuess(Fun([], UInt))
const guess = await new Promise(resolveHandP => {
this.setState({view: 'GetGuess', playable: true, resolveHandP});
});
this.setState({view: 'WaitingForResults', hand});
return GUESS[guess];
}
//second function(outcome)
seeOutcome(i){this.setState({view: 'Done', outcome: intToOutcome[i]});}
informTimeout(){this.setState({view: 'Timeout'});}
playHand(hand){this.state.resolveHandP(hand);}
}
class Deployer extends Player{
constructor(props){
super(props);
this.state = {view: 'SetWager'};
}
setWager(wager){this.setState({view: 'Deploy', wager});}
async deploy(){
const ctc = this.props.acc.contract(backend);
this.setState({view: 'Deploying', ctc});
this.wager = reach.parseCurrency(this.state.wager);
this.deadline = { ETH: 100, ALGO: 100, CFX: 1000}[reach.connector];
backend.Alice(ctc, this);
const ctcInfoStr = JSON.stringify(await ctc.getInfo(), null, 2);
this.setState({view: 'WaitingForAttacher', ctcInfoStr});
}
render(){return renderView(this, DeployerViews );}
}
class Attacher extends Player{
constructor(props){
super(props);
this.state = {view: 'Attach'};
}
attach(ctcInfoStr){
const ctc = this.props.acc.contract(backend, JSON.parse(ctcInfoStr));
this.setState({view: 'Attaching'});
backend.Bob(ctc, this);
}
async acceptWager(wagerAtomic){
const wager = reach.formatCurrency(wagerAtomic, 4);
return await new Promise(resolveAcceptedP => {
this.setState({view: 'AcceptTerms', wager, resolveAcceptedP});
});
}
termsAccepted(){
this.state.resolveAcceptedP();
this.setState({view: 'WaitingForTurn'});
}
render(){return renderView(this, AttacherViews);}
}
renderDOM(<App />);