-
Notifications
You must be signed in to change notification settings - Fork 5
/
web3feature.js
executable file
·71 lines (57 loc) · 1.79 KB
/
web3feature.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
import CONTRACT_ABI from "./Contract.json" assert { type: "json" };
const Web3Modal = window.Web3Modal.default;
const WalletConnectProvider = window.WalletConnectProvider.default;
let web3, web3Modal, userAddress;
function init() {
console.log("Initializing example");
console.log("WalletConnectProvider is", WalletConnectProvider);
// Check that the web page is run in https (MetaMask won't be available)
if(location.protocol !== 'https:') {
document.querySelector("#btn-connect").setAttribute("disabled", "disabled")
return;
}
const providerOptions = {
walletconnect: {
package: WalletConnectProvider,
options: {
infuraId: "XXXXX",
}
},
};
web3Modal = new Web3Modal({
cacheProvider: false,
providerOptions, // required
disableInjectedProvider: false,
});
}
async function onConnect() {
let provider;
console.log("opening web3modal", web3Modal);
try {
provider = await web3Modal.connect();
} catch(e) {
console.log("Could not get a wallet connection", e);
return;
}
console.log('connected');
document.querySelector("#initial").style.display = "none";
document.querySelector("#mint").style.display = "block";
web3 = new Web3(provider);
const accounts = await web3.eth.getAccounts();
userAddress = accounts[0];
}
async function onMint() {
const numOfMints = 1;
const contract = new web3.eth.Contract(CONTRACT_ABI, "0x123ABC");
console.log("minting...");
try {
await contract.methods.mint(numOfMints).send({from: userAddress, value: 40000000000000000});
} catch(e) {
console.log(e);
}
}
window.addEventListener('load', async () => {
init();
document.querySelector("#btn-connect").addEventListener("click", onConnect);
document.querySelector("#btn-mint").addEventListener("click", onMint);
});