-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
337 lines (303 loc) · 12 KB
/
App.js
File metadata and controls
337 lines (303 loc) · 12 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import * as React from 'react';
import {StyleSheet, Text, View, ScrollView, TextInput, Button, AsyncStorage, Clipboard} from 'react-native';
import {useEffect, useState, useCallback} from "react";
import Web3 from "web3";
import ModalDropdown from 'react-native-modal-dropdown';
import HDWallet from 'ethereum-hdwallet';
const erc20Abi = require("./ERC20.json");
export default function App() {
const rpcUrl = "https://goerli.infura.io/v3/33beb15f6748419fbb8b16ddf1420b31";
const [ethBalance, setEthBalance] = useState(0);
const [web3, setWeb3] = useState(null);
const [account, setAccount] = useState({});
const [accountPrivateKey, setAccountPrivateKey] = useState("");
const [addresses, setAddresses] = useState([]);
const [privateKeys, setPrivateKeys] = useState([]);
const [myTokenBalance, setMyTokenBalance] = useState(0);
const [token, setToken] = useState(null);
const [mnemonic, setMnemonic] = useState("");
const [privateKeyInput, setPrivateKeyInput] = useState("");
const [addressForBalanceOf, setAddressForBalanceOf] = useState("");
const [balanceOf, setBalanceOf] = useState("");
const [addressForTransferToken, setAddressForTransferToken] = useState("");
const [transferTokenAmount, setTransferTokenAmount] = useState(0);
const [transferTokenResult, setTransferTokenResult] = useState("");
const [mintResult, setMintResult] = useState("");
const [addressForTransferEth, setAddressForTransferEth] = useState("");
const [transferEthAmount, setTransferEthAmount] = useState(0);
const [transferEthResult, setTransferEthResult] = useState("");
const prepareInfo = () => {
return {
token: token ? token._address : "",
privateKeys,
addresses,
rpc: rpcUrl,
};
};
const saveData = () => {
AsyncStorage.setItem('jsondata', JSON.stringify(prepareInfo()));
};
const loadData = async () => {
const dataString = await AsyncStorage.getItem('jsondata');
if (!!dataString && dataString.length) {
const jsonData = JSON.parse(dataString);
setAddresses(jsonData.addresses);
setPrivateKeys(jsonData.privateKeys);
}
};
const updateInfo = () => {
if (!!web3) {
web3.eth.getBalance(account.address).then((balance) => {
setEthBalance(balance);
});
if (!!token && !!account) {
token.methods.balanceOf(account.address).call().then((bal) => {
setMyTokenBalance(bal);
});
}
}
};
const updateBalanceOf = () => {
if (!!web3) {
if (!!token && !!addressForBalanceOf) {
setBalanceOf("pending");
token.methods.balanceOf(addressForBalanceOf).call().then((bal) => {
setBalanceOf(bal);
}).catch(() => {
setBalanceOf("error");
});
}
}
};
const transferToken = async () => {
if (!!web3) {
if (!!token && !!addressForTransferToken && !!transferTokenAmount) {
setTransferTokenResult("pending");
const tx = token.methods.transfer(addressForTransferToken, transferTokenAmount)
.encodeABI();
console.log(":tx", tx);
const nonce = await web3.eth.getTransactionCount(account.address);
console.log(":nonce", nonce);
let options = {
from: account.address,
to: token._address,
gas: 80000,
gasPrice: 1500000000,
value: 0,
data: tx,
nonce: nonce
};
const signedTx = await web3.eth.accounts.signTransaction(options, accountPrivateKey).catch((err) => {
console.log(err);
setTransferTokenResult("error");
});
console.log(":signedTx", signedTx);
web3.eth.sendSignedTransaction(signedTx.rawTransaction).then((bal) => {
setTransferTokenResult("ok");
}).catch((err) => {
console.log(err);
setTransferTokenResult("error");
});
}
}
};
const mintToken = async () => {
if (!!web3) {
if (!!token && !!account.address) {
setMintResult("pending");
const tx = token.methods.mint(account.address, 1000)
.encodeABI();
const nonce = await web3.eth.getTransactionCount(account.address);
console.log(":nonce", nonce);
let options = {
from: account.address,
to: token._address,
gas: 80000,
gasPrice: 1500000000,
value: 0,
data: tx,
nonce: nonce
};
const signedTx = await web3.eth.accounts.signTransaction(options, accountPrivateKey).catch((err) => {
console.log(err);
setMintResult("error");
});
console.log(":signedTx", signedTx);
web3.eth.sendSignedTransaction(signedTx.rawTransaction).then((bal) => {
setMintResult("ok");
updateInfo();
}).catch((err) => {
console.log(err);
setMintResult("error");
});
}
}
};
const transferEth = async () => {
if (!!web3) {
if (!!transferEthAmount && !!addressForTransferEth) {
setTransferEthResult("pending");
const nonce = await web3.eth.getTransactionCount(account.address);
console.log(":nonce", nonce);
let options = {
from: account.address,
to: addressForTransferEth,
gas: 80000,
gasPrice: 1500000000,
value: transferEthAmount,
nonce: nonce
};
const signedTx = await web3.eth.accounts.signTransaction(options, accountPrivateKey).catch((err) => {
console.log(err);
setTransferEthResult("error");
});
console.log(":signedTx", signedTx);
web3.eth.sendSignedTransaction(signedTx.rawTransaction).then((bal) => {
setTransferEthResult("ok");
updateInfo();
}).catch((err) => {
console.log(err);
setTransferEthResult("error");
});
}
}
};
useEffect(() => {
updateInfo();
}, [account]);
useEffect(() => {
if (privateKeys.length > 0) {
saveData();
}
}, [privateKeys]);
const createAccount = (privateKey) => {
const acc = web3.eth.accounts.privateKeyToAccount(privateKey);
setAddresses([...addresses, acc.address]);
setPrivateKeys([...privateKeys, privateKey]);
};
const createRandomKey = () => {
createAccount(web3.utils.randomHex(32));
};
const createFromMnemonic = async () => {
const hdwallet = HDWallet.fromMnemonic(mnemonic);
const privateKey = `0x${hdwallet.derive(`m/44'/60'/0'/0/0`).getPrivateKey().toString('hex')}`;
createAccount(privateKey);
};
const createFromPrivateKey = () => {
createAccount(privateKeyInput);
};
const init = () => {
console.log("init web3");
const web3Instance = new Web3(rpcUrl);
setWeb3(web3Instance);
const tokenInstance = new web3Instance.eth.Contract(erc20Abi.abi, "0x1fFE9c7110Bb3A07463bE5EBA80BD40F03EB3e3e");
setToken(tokenInstance);
};
useEffect(() => {
init();
loadData().catch(console.error);
}, []);
const copyAll = () => {
Clipboard.setString(JSON.stringify(prepareInfo(), null, 4));
};
const selectAccount = (index, address) => {
setAccountPrivateKey(privateKeys[index]);
const acc = web3.eth.accounts.privateKeyToAccount(privateKeys[index]);
setAccount(acc);
};
return (
<View style={styles.container}>
<ScrollView>
<Text>Embedded web3 wallet</Text>
<Text/>
<Button
onPress={copyAll}
title="Copy token address, private keys, addresses to clipboard"
color="#841584"
/>
<Text/>
<Button
onPress={createRandomKey}
title="Create random"
/>
<Text>Mnemonic:</Text>
<TextInput style={styles.textInput} onChangeText={setMnemonic} defaultValue={mnemonic}/>
<Button
onPress={createFromMnemonic}
title="Import mnemonic"
/>
<Text>Private key:</Text>
<TextInput style={styles.textInput} onChangeText={setPrivateKeyInput} defaultValue={privateKeyInput}/>
<Button
onPress={createFromPrivateKey}
title="Import private key"
/>
<Text>Select account:</Text>
<ModalDropdown style={styles.dropDown} options={addresses} onSelect={selectAccount}/>
<Text>My balance: {ethBalance} wei</Text>
<Text/>
<Text>Token: {token ? token._address : ""}</Text>
<Text/>
<Button
onPress={mintToken}
title="Mint 1000 tokens to me"
/>
<Text>Mint status: {mintResult}</Text>
<Text>My token balance: {myTokenBalance}</Text>
<Text/>
<Text>Balance of:</Text>
<TextInput style={styles.textInput} onChangeText={setAddressForBalanceOf}
defaultValue={addressForBalanceOf}/>
<Button
onPress={updateBalanceOf}
title="Update"
/>
<Text>balance: {balanceOf}</Text>
<Text/>
<Text>Transfer token to address:</Text>
<TextInput style={styles.textInput} onChangeText={setAddressForTransferToken}
defaultValue={addressForTransferToken}/>
<Text>Amount:</Text>
<TextInput style={styles.textInput} onChangeText={setTransferTokenAmount}
defaultValue={transferTokenAmount}/>
<Button
onPress={transferToken}
title="Transfer token"
/>
<Text>State: {transferTokenResult}</Text>
<Text/>
<Text>Transfer eth to address:</Text>
<TextInput style={styles.textInput} onChangeText={setAddressForTransferEth}
defaultValue={addressForTransferEth}/>
<Text>Amount:</Text>
<TextInput style={styles.textInput} onChangeText={setTransferEthAmount}
defaultValue={transferEthAmount}/>
<Button
onPress={transferEth}
title="Transfer eth"
/>
<Text>Status: {transferEthResult}</Text>
</ScrollView>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
marginTop: 50,
},
textInput: {
borderColor: "#000",
borderWidth: 1,
height: 40,
width: 300,
padding: 10
},
dropDown: {
height: 40,
backgroundColor: "#ccc",
}
});