-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
287 lines (216 loc) · 7.72 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import { ethers } from "https://cdnjs.cloudflare.com/ajax/libs/ethers/5.6.9/ethers.esm.js";
let isMinting = false
let isMinted = false
let walletAddress = ''
let walletId = ''
let mintStartTime = 1662418800
let walletBalance = 0
let minGasRequired = 0.01 //MATIC
const provider = new ethers.providers.JsonRpcProvider("https://polygon-rpc.com");
const CONTRACT_ADDRESS = '0x77874890e357f9d3207332d905188cc012fdcd20'
let CONTRACT_ABI = [{
"inputs": [
{
"internalType": "address",
"name": "account",
"type": "address"
},
{
"internalType": "uint256",
"name": "quantity",
"type": "uint256"
},
{
"internalType": "bytes32[]",
"name": "proof",
"type": "bytes32[]"
}
],
"name": "publicMint",
"outputs": [],
"stateMutability": "payable",
"type": "function"
}]
checkWallet()
async function checkAction() {
const params = new Proxy(new URLSearchParams(window.location.search), {
get: (searchParams, prop) => searchParams.get(prop),
});
let action = params.action;
if (action == 'approve') {
approveTransaction(params.signedTransaction)
return
}
if (action == 'reject') {
rejectTransaction()
return
}
}
document.getElementById('claim-button').addEventListener("click",
function(){
if (isMinting || isMinted) {
if (isMinted) {
window.location = `https://opensea.io/${walletAddress}`
}
} else {
mint()
}
});
async function checkWallet() {
await window.glipWalletSDK.init({
'clientIdentifier': '62fd0e1b5f653536e9c657a8',
chain: 'polygon',
authNetwork: 'cyan'
}
);
if (await window.glipWalletSDK.isConnected()) {
walletId = await window.glipWalletSDK.getWalletID()
let userInfo = await window.glipWalletSDK.getUserInfo();
console.log('walletId', walletId)
walletAddress = userInfo.publicAddress
console.log(walletAddress)
document.getElementById('title').innerHTML = 'Glip Wallet connected'
document.getElementById('subtitle').innerHTML = userInfo.name
document.getElementById('claim-button').style.visibility = 'visible'
document.getElementById('button-description').style.visibility = 'visible'
hideLoading()
if (Date.now() / 1000 < mintStartTime) {
document.getElementById('claim-button').innerHTML = 'Claim not yet started'
}
if (window.localStorage.getItem('minted')) {
isMinted = true
document.getElementById('claim-button').innerHTML = 'NFT claimed! (View)'
document.getElementById('button-description').style.visibility = 'hidden'
}
checkAction()
checkBalance()
} else {
document.getElementById('title').innerHTML = 'Wallet not found<br>You missed your free NFT. Create your Glip Wallet from the app and keep an eye on next reward.'
hideLoading()
}
}
function enoughBalance() {
return walletBalance >= minGasRequired
}
async function mint() {
if (Date.now() / 1000 < mintStartTime) {
return
}
isMinting = true
showLoading()
if (!enoughBalance()) {
requestBalance()
return
}
document.getElementById('claim-button').innerHTML = 'Claiming NFT...'
const contract = new ethers.Contract(CONTRACT_ADDRESS, CONTRACT_ABI, provider);
let whitelistResponse = await fetch("whitelist.json")
let whitelist = await whitelistResponse.json()
console.log(whitelist)
if (!whitelist.includes(walletAddress)) {
document.getElementById('claim-button').innerHTML = `Not registered to claim`
hideLoading()
return
}
const merkleTree = new MerkleTree(whitelist.map(e => ethereumjs.Buffer.Buffer.from(ethers.utils.solidityKeccak256(['address'], [e]).slice(2), 'hex')), keccak256, {sortPairs: true});
const rootHash = merkleTree.getRoot();
const hexProof = merkleTree.getHexProof(ethereumjs.Buffer.Buffer.from(ethers.utils.solidityKeccak256(['address'], [walletAddress]).slice(2), 'hex'))
console.log("Root Hash: ", rootHash.toString('hex'));
console.log(hexProof);
const nonce = await provider.getTransactionCount(walletAddress)
// const feeData = await provider.getFeeData()
// let maxFeePerGas = feeData.maxFeePerGas.toHexString()
// let maxPriorityFeePerGas = feeData.maxPriorityFeePerGas.toHexString()
let maxFeePerGas = '0x9502F9000'
let maxPriorityFeePerGas = '0x9502F9000'
const tx = await contract.populateTransaction['publicMint'](
walletAddress, 1, hexProof,
{ maxFeePerGas: maxFeePerGas, maxPriorityFeePerGas: maxPriorityFeePerGas}
);
tx.type = 2
tx.chainId = 137
tx.maxFeePerGas = maxFeePerGas
tx.maxPriorityFeePerGas = maxPriorityFeePerGas
tx.gasLimit = 200000
tx.nonce = nonce
console.log(tx)
let signer = await window.glipWalletSDK.getSigner();
await signer.signTransaction(tx);
}
function requestBalance() {
console.log('requesting balance')
document.getElementById('claim-button').innerHTML = 'Adding balance to your wallet...'
const options = {method: 'POST', headers: {Accept: 'application/json'}};
fetch(`https://be.namasteapis.com/blockchain/v1/glip/wallet/fund?walletId=${walletId}`, options)
.then(response => response.json())
.then(response => {
console.log(response)
verifyBalanceChangeAndMint()
}
)
.catch(err => console.error(err));
}
function verifyBalanceChangeAndMint() {
setTimeout(function() {
console.log('checking balance increase')
checkBalance(function() {
if (enoughBalance()) {
mint()
} else {
verifyBalanceChangeAndMint()
}
})
}, 4000)
}
function checkBalance(callback) {
provider.getBalance(walletAddress).then((balance) => {
const balanceInEth = ethers.utils.formatEther(balance)
console.log(`balance: ${balanceInEth} MATIC`)
walletBalance = balanceInEth
document.getElementById('balance').innerHTML = `Available Balance: ${balanceInEth} MATIC`
if (callback) {
callback()
}
})
}
async function approveTransaction(signedTx) {
console.log('tx approved')
console.log(signedTx)
document.getElementById('claim-button').innerHTML = 'Claiming NFT...'
document.getElementById('button-description').style.visibility = 'hidden'
showLoading()
try {
let txResponse = await provider.sendTransaction(signedTx)
console.log(txResponse)
try {
let receipt = await txResponse.wait()
if (receipt.status == 1) {
document.getElementById('claim-button').innerHTML = 'NFT Claimed! (View)'
isMinted = true
window.localStorage.setItem('minted', true)
} else {
document.getElementById('claim-button').innerHTML = 'Failed :('
}
} catch {
console.log(e)
document.getElementById('claim-button').innerHTML = 'Failed :('
}
} catch (e){
console.log(e)
document.getElementById('claim-button').innerHTML = 'Failed :('
}
hideLoading()
checkBalance()
}
async function rejectTransaction() {
console.log('tx rejected')
document.getElementById('claim-button').innerHTML = 'Transaction rejected. Try again'
hideLoading()
isMinting = false
}
function showLoading() {
document.getElementById('loader').style.visibility = 'visible'
}
function hideLoading() {
document.getElementById('loader').style.visibility = 'hidden'
}