-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.js
329 lines (297 loc) · 8.87 KB
/
login.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import { ethers, getDefaultProvider } from './libs/ethers-5.6.2.esm.min.js'
window.provider = window.passport.connectEvm()
const connectPassport = async function () {
window.accounts = await window.provider.request({ method: 'eth_requestAccounts' })
if (window.accounts) {
getUserInfo()
}
}
const config = {
baseConfig: new window.immutable.config.ImmutableConfiguration({
environment: window.immutable.config.Environment.SANDBOX
})
}
const client = new window.immutable.blockchainData.BlockchainData(config)
const getUserInfo = async function () {
window.userProfile = await window.passport.getUserInfo()
}
const passportLogout = async function () {
const logout = await window.passport.logout()
window.userProfile = {}
}
const CHAIN_NAME = 'imtbl-zkevm-testnet'
// TODO: Replace CONTRACT_ADDRESS
const CONTRACT_ADDRESS = '0x1eac82d5cdc4b3663c5a437ddd0acfbb91f353e9' // process.env.CONTRACT_ADDRESS
const CONTRACT_ABI = [
'function grantRole(bytes32 role, address account)',
'function MINTER_ROLE() view returns (bytes32)',
'function mint(address to, uint256 tokenId)',
'function ownerOf(uint256 tokenId) view returns (address)',
'function hasRole(bytes32 role, address account) view returns (bool)',
'function totalSupply() view returns (uint256)'
]
const NFT_DETAILS = {
1: {
image: 'https://bafkreigugjgtcvkwg7ym7uk5ic65wmtkmbngonaj3twzl3nttuj5w7zjku.ipfs.nftstorage.link/',
name: 'Level 1 Badge',
description: 'This NFT represents your first accomplishment on StackUp Invaders (twk415).',
attributes: [
{
trait_type: 'Level',
value: 'Beginner'
},
{
trait_type: 'Ammo',
value: '1'
}
]
},
2: {
image: 'https://bafkreifxbz53txersuyqok75dmdhyrnfkascznytyvum2i25bunii5dih4.ipfs.nftstorage.link/',
name: 'Level 2 Badge',
description: 'This NFT represents your second accomplishment on StackUp Invaders (twk415) which grants you an upgraded spaceship.',
attributes: [
{
trait_type: 'Level',
value: 'Amateur'
},
{
trait_type: 'Ammo',
value: '2'
}
]
},
3: {
image: 'https://bafkreifxbz53txersuyqok75dmdhyrnfkascznytyvum2i25bunii5dih4.ipfs.nftstorage.link/',
name: 'Level 3 Badge',
description: 'This NFT represents your third accomplishment on StackUp Invaders (twk415).',
attributes: [
{
trait_type: 'Level',
value: 'Amateur'
},
{
trait_type: 'Ammo',
value: '3'
}
]
},
4: {
image: 'https://bafkreifxbz53txersuyqok75dmdhyrnfkascznytyvum2i25bunii5dih4.ipfs.nftstorage.link/',
name: 'Level 4 Badge',
description: 'This NFT represents your fourth accomplishment on StackUp Invaders (twk415).',
attributes: [
{
trait_type: 'Level',
value: 'Novice'
},
{
trait_type: 'Ammo',
value: '4'
}
]
}
}
async function getData (id) {
try {
const nft = document.getElementById('nft')
const details = NFT_DETAILS[id]
if (!details) {
throw new Error('Invalid Token ID')
}
nft.innerHTML = `
<div class="alert alert-success"> Great Score! Claim this NFT, then resume the game.</div>
<div class="card" >
<div class="card-body">
<div class="media">
<img src='${details.image}' class="mr-3 img-thumbnail" alt="nft" style="width: 30%;">
<div class="media-body">
<h5 class="card-title">${details.name}</h5>
<p class="card-text">${details.description}</p>
</div>
</div>
</div>
<div class="card-body">
<button id="claim-btn" class="btn btn-success"> Claim</button>
</div>
</div>
`
const claimBtn = this.document.getElementById('claim-btn')
claimBtn.onclick = async function () {
if (id === '1') {
await mintNft()
} else if (id >= '2') {
await upgradeNft(id)
}
}
return details
} catch (error) {
console.error(error)
alert(error)
}
}
window.getData = getData
/**
* List NFTs owned by an account for a contract
*
* @return {Object|undefined} NFT metadata
*/
const getNftByAccount = async () => {
if (!window.accounts?.[0]) {
return
}
try {
const { result: [nft] } = await client.listNFTsByAccountAddress({
chainName: CHAIN_NAME,
accountAddress: window.accounts[0],
contractAddress: CONTRACT_ADDRESS
})
return nft
} catch (error) {
console.error(error)
}
}
const grantMinterRole = async (recipientAddress) => {
return fetch('/.netlify/functions/grantMinterRole', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ recipientAddress })
})
.then(d => d.json())
.then(async ({ data }) => {
const tx = ethers.utils.parseTransaction(data)
return tx
})
.then(console.log)
}
// Mint NFTs
const mintNft = async function () {
if (window?.provider) {
const provider = new ethers.providers.Web3Provider(window.provider)
const signer = provider.getSigner()
const userAddress = await signer.getAddress()
const contract = new ethers.Contract(CONTRACT_ADDRESS, CONTRACT_ABI, signer)
try {
const minterRole = await contract.MINTER_ROLE()
const hasMinterRole = await contract.hasRole(minterRole, userAddress)
if (!hasMinterRole) {
console.log('Account doesnt have permissions to mint.')
await grantMinterRole(userAddress)
}
const TOKEN_ID = await getNextTokenId(contract)
const currentGasPrice = await provider.getGasPrice()
// const adjustedGasPrice = currentGasPrice.add(ethers.utils.parseUnits('10', 'gwei'))
const tx = await contract.mint(userAddress, TOKEN_ID, {
maxPriorityFeePerGas: 100e9, // 100 Gwei
maxFeePerGas: 150e9,
gasLimit: 200000,
})
const receipt = await tx.wait()
console.log('NFT minted successfully!', receipt)
const nft = document.getElementById('nft')
nft.innerHTML += `
<div class="alert alert-success">
NFT minted successfully! Transaction hash: ${receipt.transactionHash}
<a href="/nft.html?id=${TOKEN_ID}" target="_blank">View your badge here</a>
</div>
`
showViewBadgeButton(TOKEN_ID)
} catch (error) {
console.error('Error minting the first NFT:', error)
}
} else {
console.log('No provider found.')
}
}
const showViewBadgeButton = function (tokenId) {
if (tokenId) {
const btn = window.document.getElementById('view-badge')
btn.href = `/nft.html?id=${tokenId}`
btn.hidden = false
}
}
/**
* Get user level according to NFT metadata
*
* @returns {number} level
*/
const getUserLevel = async function () {
const nft = await getNftByAccount()
nft?.token_id && showViewBadgeButton(nft.token_id)
// determine level based on NFT name
switch (nft?.name) {
case 'Level 1 Badge':
return 1
case 'Level 2 Badge':
return 2
case 'Level 3 Badge':
return 3
case 'Level 4 Badge':
return 4
}
return 0
}
window.getUserLevel = getUserLevel
async function getNextTokenId (contract) {
try {
const totalSupply = await contract.totalSupply()
return totalSupply.toNumber() + 1
} catch (error) {
console.error('Error getting next token ID:', error)
return null
}
}
const upgradeNft = async function (level) {
try {
const { token_id } = await getNftByAccount()
await fetch('./.netlify/functions/upgradeNft', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
nft_metadata: [
{
animation_url: null,
external_url: null,
youtube_url: null,
...NFT_DETAILS[level],
token_id
}
]
})
})
const upgradeEvent = new CustomEvent('upgradeSpaceship')
window.dispatchEvent(upgradeEvent)
const nft = document.getElementById('nft')
if (level == '2') {
nft.innerHTML += `
<div class="alert alert-success">
Your spaceship has been upgraded! So has your NFT.
<a href="/nft.html?id=${token_id}" target="_blank">View your badge here.</a>
</div>
`
} else {
nft.innerHTML += `
<div class="alert alert-success">
Your level is up again! So has your NFT.
<a href="/nft.html?id=${token_id}" target="_blank">View your badge here</a>
</div>
`
}
} catch (error) {
console.error('Error upgrading NFT: ', error)
}
}
window.addEventListener('load', function () {
const passportBtn = this.document.getElementById('btn-passport')
const logoutBtn = this.document.getElementById('btn-logout')
passportBtn.onclick = function () {
window.isconnecting = true
connectPassport()
}
logoutBtn.onclick = passportLogout
window.passport.loginCallback()
})