-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (60 loc) · 2.15 KB
/
index.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
import { ethers } from "./ethers.js";
import { contractAddress, abi } from "./constant.js";
const btn_connect = document.getElementById("btn_connect");
const btn_getdata = document.getElementById("btn_getdata");
const btn_getowner = document.getElementById("btn_getowner");
btn_connect.onclick = connect;
btn_getdata.onclick = getdata;
btn_getowner.onclick = getowner;
async function connect() {
if (typeof window.ethereum != "undefined") {
console.log("Connecting to metamask...");
await window.ethereum.request({ method: "eth_requestAccounts" });
console.log("Connected");
} else {
console.log("No metamask!!!");
}
}
async function getdata() {
if (typeof window.ethereum != "undefined") {
console.log("Getting data...");
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
const contract = new ethers.Contract(contractAddress, abi, signer);
const transActionResponse = await contract.getData();
// await listenForTransactionMine(transActionResponse, provider);
console.log(transActionResponse)
console.log("Get data Finished");
} else {
console.log("No metamask!!!");
}
}
async function getowner() {
if (typeof window.ethereum != "undefined") {
console.log("Getting owneradderss...");
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
const contract = new ethers.Contract(contractAddress, abi, signer);
const transActionResponse = await contract.getOwner();
// await listenForTransactionMine(transActionResponse, provider);
console.log(transActionResponse)
console.log("Get owner Finished");
} else {
console.log("No metamask!!!");
}
}
function listenForTransactionMine(transactionResponse, provider) {
console.log(`Mining ${transactionResponse.hash}`);
return new Promise((resolve, reject) => {
try {
provider.once(transactionResponse.hash, (transactionReceipt) => {
console.log(
`Completed with ${transactionReceipt.confirmations} confirmations. `
);
resolve();
});
} catch (error) {
reject(error);
}
});
}