-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
142 lines (127 loc) · 4.46 KB
/
App.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import React, { Component } from "react";
import SimpleStorageContract from "./contracts/SimpleStorage.json";
import Web3 from "web3";
import Fortmatic from "fortmatic";
import "./App.css";
import Header from "./components/Header";
import FormView from "./components/FormView.js";
import TipForm from "./components/TipForm";
import { Container, Col, Row, Form, FormGroup } from "reactstrap";
import { Heading, Field, Input, Button, Card, OutlineButton } from "rimble-ui";
import FortmaticLogin from "./components/FortmaticLogin.js";
class App extends Component {
constructor(props) {
super(props);
this.state = {
storageValue: 0,
web3: null,
accounts: null,
contract: null
};
this.setFortmatic = this.setFortmatic.bind(this);
}
componentDidMount = async () => {
try {
// Get network provider and web3 instance.
// const web3 = await getWeb3();
let fm = new Fortmatic("pk_test_C6808B2B488687F6", "kovan");
let web3;
// Post EIP-1102 update which MetaMask no longer injects web3
if (window.ethereum) {
// Use MetaMask provider
web3 = new Web3(window.ethereum);
} else {
// Use Fortmatic provider
web3 = new Web3(fm.getProvider());
}
// Legacy dApp browsers which web3 is still being injected
if (typeof web3 !== "undefined") {
// Use injected provider
window.web3 = new Web3(web3.currentProvider);
} else {
// Use Fortmatic provider
window.web3 = new Web3(fm.getProvider());
}
// Use web3 to get the user's accounts.
const accounts = await web3.eth.getAccounts();
console.log(accounts);
// Set web3, accounts, and contract to the state, and then proceed with an
// example of interacting with the contract's methods.
console.log(web3);
this.setState({ web3, accounts });
} catch (error) {
// Catch any errors for any of the above operations.
alert(
`Failed to load web3, accounts, or contract. Check console for details.`
);
console.error(error);
}
};
async setFortmatic(event) {
let fm = new Fortmatic("pk_test_C6808B2B488687F6", "kovan");
let web3;
// Post EIP-1102 update which MetaMask no longer injects web3
web3 = new Web3(fm.getProvider());
// Legacy dApp browsers which web3 is still being injected
window.web3 = new Web3(fm.getProvider());
// U/se web3 to get the user's accounts.
const accounts = await web3.eth.getAccounts();
this.setState({ web3, accounts });
console.log(accounts);
// Get the contract instance.
// Set web3, accounts, and contract to the state, and then proceed with an
// example of interacting with the contract's methods.
console.log(web3);
}
runExample = async () => {
const { accounts, contract } = this.state;
// Stores a given value, 5 by default.
await contract.methods.set(5).send({ from: accounts[0] });
// Get the value from the contract to prove it worked.
const response = await contract.methods.get().call();
// Update state with the result.
this.setState({ storageValue: response });
};
render() {
const { accounts, contract } = this.state;
console.log(accounts);
if (!this.state.web3) {
return <div>Loading Web3, accounts, and contract...</div>;
}
if (!accounts[0]) {
return (
<>
<Container className="mt-4">
<Row className="justify-content-center">
<Col lg="6">
<Heading.h2>Missing Web3 Provider</Heading.h2>
<Card className="mt-4 mx-auto">
<Heading.h4>Login with Google</Heading.h4>
<p>
Login with Google pressing the blue button in the left
corner and then refresh the website
</p>
</Card>
<Card className="mt-4 mx-auto">
<Heading.h4>Login with Fortmatic</Heading.h4>
<p>Press the button and use your cellphone to login</p>
<Button onClick={this.setFortmatic}>
Login with Fortmatic
</Button>
</Card>
</Col>
</Row>
</Container>
</>
);
}
return (
<>
<Header account={accounts[0]} web3={window.web3} contracts={contract} />
<FormView account={accounts[0]} />
<TipForm />
</>
);
}
}
export default App;