-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathApp.js
188 lines (156 loc) · 5.16 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import React, { Component } from 'react';
import './App.css';
import Web3 from 'web3';
import contract from 'truffle-contract';
import unit from 'ethjs-unit';
import BN from 'bn.js';
import 'whatwg-fetch';
import Heading from './components/Heading';
import Dialog from './components/Dialog';
import NoEther from './components/NoEther';
import Balances from './components/Balances';
import PurchaseAdt from './components/PurchaseAdt';
import TxHash from './components/TxHash';
import AdTokenPrice from './components/AdTokenPrice';
class App extends Component {
constructor() {
super();
this.state = {
amount: 1,
ethBalance: '-',
adtBalance: '-',
txHash: '',
loading: true,
message: 'Please unlock MetaMask and connect to the Rinkeby Test Network',
subMessage: '',
address : '',
metaMask: false
};
}
componentDidMount() {
window.setTimeout(() => {
if (typeof this.web3 !== 'undefined') {
this.web3 = new Web3(this.web3.currentProvider);
} else if (typeof window.web3 !== 'undefined') {
this.web3 = new Web3(window.web3.currentProvider);
} else {
throw new Error('You need MetaMask!');
}
this.web3.eth.defaultAccount = this.web3.eth.accounts[0];
if (this.web3.eth.defaultAccount) {
this.setupBalances();
} else if (this.web3.eth.defaultAccount === undefined) {
this.setState({
message: 'Please unlock MetaMask and connect to the Rinkeby Test Network',
loading: false
});
}
}, 1000);
}
getSale = async () => {
let saleUrl = 'https://s3-us-west-2.amazonaws.com/adchain-registry-contracts/Sale.json';
let saleArtifact;
try {
saleArtifact = await fetch(saleUrl);
} catch (err) {
return false;
}
const Sale = contract(await saleArtifact.json());
Sale.setProvider(this.web3.currentProvider);
try {
await Sale.deployed();
} catch (err) {
this.setState({
message: 'Please unlock MetaMask and connect to the Rinkeby Test Network',
loading: false
});
}
return Sale.deployed();
};
getToken = async () => {
const sale = await this.getSale();
if (!sale) {
return false;
}
let tokenUrl =
'https://s3-us-west-2.amazonaws.com/adchain-registry-contracts/HumanStandardToken.json';
const tokenAddress = await sale.token.call();
const tokenArtifact = await fetch(tokenUrl);
const Token = contract(await tokenArtifact.json());
Token.setProvider(this.web3.currentProvider);
return Token.at(tokenAddress);
};
trimDecimals = (n) => (+n).toFixed(3).replace(/([0-9]+(\.[0-9]+[1-9])?)(\.?0+$)/,'$1');
setupBalances = async () => {
const token = await this.getToken();
if (!token) {
this.setState({
message: 'Hmm...something went wrong. If you are running an ad blocker, please disable it to continue',
loading: false
});
return false;
}
const account = this.web3.eth.accounts[0];
const rawBal = await token.balanceOf.call(account);
const adtDisplayValue = rawBal.div(new BN('10', 10).pow(new BN('9', 10)));
const adtBalance = this.trimDecimals(adtDisplayValue);
this.web3.eth.getBalance(account, (err, res) => {
const ethDisplayValue = res.div(new BN('10', 10).pow(new BN('18', 10)));
const ethBalance = this.trimDecimals(ethDisplayValue);
this.setState({
message: 'Your Rinkeby MetaMask address:',
subMessage: account,
address: account,
adtBalance: adtBalance.toString(10),
ethBalance: ethBalance.toString(10),
loading: false,
metaMask: true
});
});
};
handleChange = e => {
this.setState({
amount: e.target.value
});
};
handleSubmit = async e => {
e.preventDefault();
const saleInstance = await this.getSale();
console.log('saleInstance', saleInstance);
const weiValue = unit.toWei(this.state.amount, 'ether');
const txn = {
from: this.state.subMessage,
value: weiValue
}
const txHash = await saleInstance.purchaseTokens.sendTransaction(txn);
this.setState({
txHash: txHash
})
};
render() {
return (
<div className="App">
<Heading />
<Dialog message={this.state.message} metaMask={this.state.metaMask} loading={this.state.loading} subMessage={this.state.subMessage} />
<div className="mt-25">
{
this.state.subMessage && (<Balances loading={this.state.loading} address={this.state.address} adtBalance={this.state.adtBalance} ethBalance={this.state.ethBalance} /> )
}
{
this.state.ethBalance === '0' && <NoEther />
}
{
<AdTokenPrice loading={this.state.loading}/>
}
{
this.state.subMessage && ( <PurchaseAdt handleChange={this.handleChange} handleSubmit={this.handleSubmit} amount={this.state.amount} /> )
}
{
this.state.txHash && <TxHash txHash={this.state.txHash} />
}
</div>
</div>
);
}
}
export default App;