forked from ericet/MultiClaims
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
155 lines (136 loc) · 4.33 KB
/
app.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
const chainId = 137;
const contractAddress = "0x4f14362861ff557d67aec6880067b29987beb50c";//main net
const etherscanUrl = "https://polygonscan.com/tx";//main net
let provider = null;
const abi = [
{
"inputs": [
{
"internalType": "uint256",
"name": "times",
"type": "uint256"
}
],
"name": "call",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
];
window.onload = () => {
var animateButton = function (e) {
e.preventDefault;
//reset animation
e.target.classList.remove("animate");
e.target.classList.add("animate");
setTimeout(function () {
e.target.classList.remove("animate");
}, 700);
};
var bubblyButtons = document.getElementsByClassName("bubbly-button");
for (var i = 0; i < bubblyButtons.length; i++) {
bubblyButtons[i].addEventListener("click", animateButton, false);
}
window?.ethereum?.on("disconnect", () => {
window.location.reload();
});
window?.ethereum?.on("networkChanged", () => {
window.location.reload();
});
window?.ethereum?.on("chainChanged", () => {
window.location.reload();
});
const connectWallet = async () => {
await window.ethereum.enable();
if (Number(window.ethereum.chainId) !== chainId) {
return failedConnectWallet();
}
provider = new ethers.providers.Web3Provider(window.ethereum);
const accounts = await provider.send("eth_requestAccounts");
document.getElementById("button").innerHTML = accounts[0];
};
const failedConnectWallet = () => {
document.getElementById("button").innerHTML = "Error Network, switch to Polygon Network";
};
const switchNetwork = async () => {
window?.ethereum
?.request({
method: "wallet_addEthereumChain",
params: [
{
chainId: "0x89",
chainName: "Polygon",
nativeCurrency: {
name: "MATIC",
symbol: "MATIC",
decimals: 18,
},
rpcUrls: ["https://polygon-rpc.com/"],
blockExplorerUrls: ["https://polygonscan.com/"],
},
],
})
.then(() => {
connectWallet();
})
.catch(() => {
failedConnectWallet();
});
};
document.getElementById("button").addEventListener("click", switchNetwork);
connectWallet();
const handleMint = async () => {
$.toast().reset("all");
if (!provider) {
connectWallet();
} else {
try {
document.getElementById("mint").innerHTML = "Minting...";
const signer = await provider.getSigner();
const inputValue = document.getElementById("count").value;
if (!inputValue || Math.round(inputValue) !== Number(inputValue)) {
document.getElementById("mint").innerHTML = "Mint";
return $.toast({
heading: "Error",
text: "Enter an integer!",
position: "top-center",
showHideTransition: "fade",
icon: "error",
});
}
const ImageContract = new ethers.Contract(contractAddress, abi, signer);
const amountRaw = ethers.utils.parseUnits(`${0 * inputValue}`, 18).toString();
const estimateGas = await ImageContract.estimateGas.call(inputValue, {
value: amountRaw,
});
const gasLimit = Math.floor(estimateGas.toNumber() * 2);
const response = await ImageContract.call(inputValue, {
value: amountRaw,
gasLimit,
});
console.log(response)
$.toast({
heading: "Minting",
text: "Start to minting!",
position: "top-center",
showHideTransition: "fade",
hideAfter: 10000,
icon: "info",
});
const result = await response.wait();
$.toast().reset("all");
$.toast({
heading: "Success",
text: "Minted Success! ",
showHideTransition: "slide",
position: "top-center",
icon: "success",
});
document.getElementById("mint").innerHTML = "Mint";
let html = `<a href='${etherscanUrl}/${result.transactionHash}' target="_blank">Transaction ID: ${result.transactionHash}</a>`;
$('#transaction').html(html);
} catch (e) {}
}
};
document.getElementById("mint").addEventListener("click", handleMint);
};