-
Notifications
You must be signed in to change notification settings - Fork 0
/
web3.ts
98 lines (95 loc) · 1.87 KB
/
web3.ts
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
import Web3 from 'web3'
const address = "0xd9145CCE52D386f254917e481eB44e9943F39138";
const abi = [
{
"anonymous": false,
"inputs": [],
"name": "NewPost",
"type": "event"
},
{
"inputs": [
{
"internalType": "string",
"name": "_img",
"type": "string"
},
{
"internalType": "string",
"name": "_text",
"type": "string"
}
],
"name": "sendHash",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "getCounter",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "_index",
"type": "uint256"
}
],
"name": "getHash",
"outputs": [
{
"internalType": "string",
"name": "img",
"type": "string"
},
{
"internalType": "string",
"name": "text",
"type": "string"
},
{
"internalType": "address",
"name": "owner",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
}
];
export const connectWallet = async () => {
let wallet, contract, accounts;
if(process.client){
if (window.ethereum) {
let instance = new Web3(window.ethereum);
try {
window.ethereum.enable();
wallet = instance;
} catch (error) {
alert('Please allow access for the app to work');
}
} else if (window.web3) {
wallet = new Web3(window.web3.currentProvider);
} else {
alert('Non-Ethereum browser detected. You should consider trying MetaMask!');
}
if(wallet){
contract = new wallet.eth.Contract(abi, address)
accounts = await wallet.eth.getAccounts()
}
return {
wallet, contract, accounts
}
}
}