forked from antonnell/networklist-org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccountStore.js
127 lines (111 loc) · 3.05 KB
/
accountStore.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
import {
ERROR,
STORE_UPDATED,
CONFIGURE,
ACCOUNT_CONFIGURED,
ACCOUNT_CHANGED,
TRY_CONNECT_WALLET,
} from './constants';
import stores from './'
import Web3 from 'web3';
class Store {
constructor(dispatcher, emitter) {
this.dispatcher = dispatcher
this.emitter = emitter
this.store = {
account: null,
web3: null,
}
dispatcher.register(
function (payload) {
switch (payload.type) {
case CONFIGURE:
this.configure(payload);
break;
case TRY_CONNECT_WALLET:
this.tryConnectWallet(payload)
break;
default: {
}
}
}.bind(this)
);
}
getStore(index) {
return(this.store[index]);
};
setStore(obj) {
this.store = {...this.store, ...obj}
return this.emitter.emit(STORE_UPDATED);
};
configure = async () => {
// if (window.ethereum) {
// window.web3 = new Web3(ethereum);
// try {
// await ethereum.enable();
// var accounts= await web3.eth.getAccounts();
// this.setStore({ account: { address: accounts[0] }, web3: window.web3 })
// this.emitter.emit(ACCOUNT_CONFIGURED)
// } catch (error) {
// // User denied account access...
// }
//
// this.updateAccount()
//
// } else if (window.web3) {
// window.web3 = new Web3(web3.currentProvider);
// // Acccounts always exposed
// web3.eth.sendTransaction({/* ... */});
// }
// // Non-dapp browsers...
// else {
// console.log('Non-Ethereum browser detected. You should consider trying MetaMask!');
// }
};
updateAccount = () => {
const that = this
const res = window.ethereum.on('accountsChanged', function (accounts) {
that.setStore({ account: { address: accounts[0] } })
that.emitter.emit(ACCOUNT_CHANGED)
that.emitter.emit(ACCOUNT_CONFIGURED)
})
}
getWeb3Provider = async () => {
let web3context = this.getStore('web3context')
let provider = null
if(!web3context) {
provider = network.providers['1']
} else {
provider = web3context.library.provider
}
if(!provider) {
return null
}
return new Web3(provider);
}
tryConnectWallet = async () => {
if (window.ethereum) {
window.web3 = new Web3(ethereum);
try {
await ethereum.enable();
var accounts= await web3.eth.getAccounts();
this.setStore({ account: { address: accounts[0] }, web3: window.web3 })
this.emitter.emit(ACCOUNT_CONFIGURED)
} catch (error) {
// User denied account access...
}
}
// Legacy dapp browsers...
else if (window.web3) {
window.web3 = new Web3(web3.currentProvider);
var accounts= await web3.eth.getAccounts();
this.setStore({ account: { address: accounts[0] }, web3: window.web3 })
this.emitter.emit(ACCOUNT_CONFIGURED)
}
// Non-dapp browsers...
else {
console.log('Non-Ethereum browser detected. You should consider trying MetaMask!');
}
}
}
export default Store;