-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
165 lines (152 loc) · 3.49 KB
/
helpers.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
import MetaMaskOnboarding from '@metamask/onboarding';
import Web3 from 'web3';
import detectEthereumProvider from '@metamask/detect-provider';
// import { getProfile } from "./sessionActions";
/**
* Detects the window.ethereum var
*/
const _provider = async () => {
try {
await detectEthereumProvider();
} catch (error) {
return false;
}
return true;
};
/**
* Handles the Web3.js Provider.
* If it detects MetaMask then it will use window.ethereum as the Provider.
* If MetaMask isn't detected then the INFURA_API will be used to as a provider.
*/
const _isMetaMask = async () => {
try {
ethereum.isMetaMask;
} catch (error) {
console.group();
console.warn('isMetaMask: Ethereum Not Detected');
console.warn('To Enjoy the full Experience');
console.warn('Please visit: https://MetaMask.io ❤');
console.groupEnd();
return false;
}
return true;
};
export const initWeb3 = async () => {
try {
if (window.ethereum !== undefined) {
ethereum.autoRefreshOnsNetworkChange = false;
window.web3 = new Web3(window.ethereum);
return true;
} else {
window.web3 = new Web3(
new Web3.providers.HttpProvider('https://api.edennetwork.io/v1/beta')
);
return true;
}
} catch (error) {
console.log('initWeb3()', error);
return false;
}
};
//
export const login = async (props) => {
try {
let ethereumProvider = await web3.currentProvider;
await ethereumProvider.request({method: 'eth_requestAccounts'});
await getAddress();
} catch (error) {
await console.log('Login(): ', error);
}
};
//
export const setProfile = (data) => data;
//
export const getAddress = async () => {
try {
let address = await web3.eth.getAccounts();
if (address) {
let addr;
web3.eth.defaultAccount = await address[0];
addr = await address[0];
return addr;
}
} catch (error) {
return error;
}
};
//
//
export const getBalance = async (addr) => {
try {
return await web3.eth
.getBalance(addr)
.then((res) => {
return setBalance(web3.utils.fromWei(res, 'ether'));
})
.catch((error) => {
if (error.code === 4001) {
// EIP-1193 userRejectedRequest error
console.log('Please Connect Wallet.');
return 'Please Connect Wallet.';
} else {
console.error(error);
return error;
}
});
} catch (error) {
return error;
}
};
//
const getNetwork = async () => {
return await web3.eth.net
.getId()
.then((res) => setChainId(res))
.catch((err) => {
console.log('getNetwork', err);
return err;
});
};
//
async function watchChain() {
try {
return await ethereum.on('chainChanged', (chainId) => {
return chainId;
// window.location.reload();
});
} catch (error) {
console.log('watchChain()', error);
return error;
}
}
//
export const startCore = async (props) => {
try {
await initWeb3();
await getNetwork();
await getAddress();
if (window.ethereum !== undefined) {
await watchChain();
}
return {
address: await getAddress(),
network: await getNetwork(),
};
} catch (error) {
throw error;
}
};
//
// Setters
export const setAddress = async (addr) => {
try {
if (addr == null) addr = [];
return addr;
} catch (error) {
return error;
}
};
export const setChainId = async (payload) => {
return payload;
};
export const setBalance = (payload) => payload;