forked from rstormsf/multisender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb3Store.js
57 lines (54 loc) · 1.64 KB
/
web3Store.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
import { action, observable } from "mobx";
import getWeb3 from '../getWeb3';
import Web3 from 'web3';
class Web3Store {
@observable web3 = {};
@observable defaultAccount = '';
getWeb3Promise = null;
@observable loading = true;
@observable errors = [];
@observable userTokens = [];
@observable explorerUrl = '';
@observable startedUrl = window.location.hash
constructor(rootStore) {
this.getWeb3Promise = getWeb3().then(async (web3Config) => {
const {web3Instance, defaultAccount} = web3Config;
this.defaultAccount = defaultAccount;
this.web3 = new Web3(web3Instance.currentProvider);
this.getUserTokens(web3Config)
this.setExplorerUrl(web3Config.explorerUrl)
console.log('web3 loaded')
}).catch((e) => {
console.error(e,'web3 not loaded')
this.errors.push(e.message)
})
}
@action
setExplorerUrl(url){
this.explorerUrl = url
}
@action
setStartedUrl(url){
this.startedUrl = url;
}
async getUserTokens({trustApiName, defaultAccount}) {
window.fetch(`https://${trustApiName}.trustwalletapp.com/tokens?address=${defaultAccount}`).then((res) => {
return res.json()
}).then((res) => {
let tokens = res.docs.map(({contract}) => {
const {address, symbol} = contract;
return {label: `${symbol} - ${address}`, value: address}
})
tokens.unshift({
value: '0x000000000000000000000000000000000000bEEF',
label: "ETH - Ethereum Native Currency"
})
this.userTokens = tokens;
this.loading = false;
}).catch((e) => {
this.loading = false;
console.error(e);
})
}
}
export default Web3Store;