├── README.md ├── index.html └── main.js /README.md: -------------------------------------------------------------------------------- 1 | # web3-minting 2 | A simple demo for connecting with metamask and minting an NFT 3 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Web3 Minting Demo 8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 26 |
27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /main.js: -------------------------------------------------------------------------------- 1 | const mainnetContract = "0x60D7E6f0b7de6a124aC9300E30C652e54726894f"; 2 | 3 | const mainnetAbi = [ 4 | { inputs: [], stateMutability: "nonpayable", type: "constructor" }, 5 | { 6 | anonymous: false, 7 | inputs: [ 8 | { 9 | indexed: true, 10 | internalType: "address", 11 | name: "owner", 12 | type: "address", 13 | }, 14 | { 15 | indexed: true, 16 | internalType: "address", 17 | name: "approved", 18 | type: "address", 19 | }, 20 | { 21 | indexed: true, 22 | internalType: "uint256", 23 | name: "tokenId", 24 | type: "uint256", 25 | }, 26 | ], 27 | name: "Approval", 28 | type: "event", 29 | }, 30 | { 31 | anonymous: false, 32 | inputs: [ 33 | { 34 | indexed: true, 35 | internalType: "address", 36 | name: "owner", 37 | type: "address", 38 | }, 39 | { 40 | indexed: true, 41 | internalType: "address", 42 | name: "operator", 43 | type: "address", 44 | }, 45 | { indexed: false, internalType: "bool", name: "approved", type: "bool" }, 46 | ], 47 | name: "ApprovalForAll", 48 | type: "event", 49 | }, 50 | { 51 | anonymous: false, 52 | inputs: [ 53 | { 54 | indexed: true, 55 | internalType: "address", 56 | name: "previousOwner", 57 | type: "address", 58 | }, 59 | { 60 | indexed: true, 61 | internalType: "address", 62 | name: "newOwner", 63 | type: "address", 64 | }, 65 | ], 66 | name: "OwnershipTransferred", 67 | type: "event", 68 | }, 69 | { 70 | anonymous: false, 71 | inputs: [ 72 | { indexed: true, internalType: "address", name: "from", type: "address" }, 73 | { indexed: true, internalType: "address", name: "to", type: "address" }, 74 | { 75 | indexed: true, 76 | internalType: "uint256", 77 | name: "tokenId", 78 | type: "uint256", 79 | }, 80 | ], 81 | name: "Transfer", 82 | type: "event", 83 | }, 84 | { 85 | inputs: [], 86 | name: "MAX_CRANIUMS", 87 | outputs: [{ internalType: "uint256", name: "", type: "uint256" }], 88 | stateMutability: "view", 89 | type: "function", 90 | }, 91 | { 92 | inputs: [], 93 | name: "PROV", 94 | outputs: [{ internalType: "string", name: "", type: "string" }], 95 | stateMutability: "view", 96 | type: "function", 97 | }, 98 | { 99 | inputs: [ 100 | { internalType: "address", name: "to", type: "address" }, 101 | { internalType: "uint256", name: "tokenId", type: "uint256" }, 102 | ], 103 | name: "approve", 104 | outputs: [], 105 | stateMutability: "nonpayable", 106 | type: "function", 107 | }, 108 | { 109 | inputs: [{ internalType: "address", name: "owner", type: "address" }], 110 | name: "balanceOf", 111 | outputs: [{ internalType: "uint256", name: "", type: "uint256" }], 112 | stateMutability: "view", 113 | type: "function", 114 | }, 115 | { 116 | inputs: [], 117 | name: "baseURI", 118 | outputs: [{ internalType: "string", name: "", type: "string" }], 119 | stateMutability: "view", 120 | type: "function", 121 | }, 122 | { 123 | inputs: [], 124 | name: "craniumPrice", 125 | outputs: [{ internalType: "uint256", name: "", type: "uint256" }], 126 | stateMutability: "view", 127 | type: "function", 128 | }, 129 | { 130 | inputs: [], 131 | name: "flipSaleState", 132 | outputs: [], 133 | stateMutability: "nonpayable", 134 | type: "function", 135 | }, 136 | { 137 | inputs: [{ internalType: "uint256", name: "tokenId", type: "uint256" }], 138 | name: "getApproved", 139 | outputs: [{ internalType: "address", name: "", type: "address" }], 140 | stateMutability: "view", 141 | type: "function", 142 | }, 143 | { 144 | inputs: [ 145 | { internalType: "address", name: "owner", type: "address" }, 146 | { internalType: "address", name: "operator", type: "address" }, 147 | ], 148 | name: "isApprovedForAll", 149 | outputs: [{ internalType: "bool", name: "", type: "bool" }], 150 | stateMutability: "view", 151 | type: "function", 152 | }, 153 | { 154 | inputs: [], 155 | name: "maxCraniumPurchase", 156 | outputs: [{ internalType: "uint256", name: "", type: "uint256" }], 157 | stateMutability: "view", 158 | type: "function", 159 | }, 160 | { 161 | inputs: [ 162 | { internalType: "uint256", name: "numberOfTokens", type: "uint256" }, 163 | ], 164 | name: "mintCraniums", 165 | outputs: [], 166 | stateMutability: "payable", 167 | type: "function", 168 | }, 169 | { 170 | inputs: [], 171 | name: "name", 172 | outputs: [{ internalType: "string", name: "", type: "string" }], 173 | stateMutability: "view", 174 | type: "function", 175 | }, 176 | { 177 | inputs: [], 178 | name: "owner", 179 | outputs: [{ internalType: "address", name: "", type: "address" }], 180 | stateMutability: "view", 181 | type: "function", 182 | }, 183 | { 184 | inputs: [{ internalType: "uint256", name: "tokenId", type: "uint256" }], 185 | name: "ownerOf", 186 | outputs: [{ internalType: "address", name: "", type: "address" }], 187 | stateMutability: "view", 188 | type: "function", 189 | }, 190 | { 191 | inputs: [], 192 | name: "renounceOwnership", 193 | outputs: [], 194 | stateMutability: "nonpayable", 195 | type: "function", 196 | }, 197 | { 198 | inputs: [], 199 | name: "reserveCraniums", 200 | outputs: [], 201 | stateMutability: "nonpayable", 202 | type: "function", 203 | }, 204 | { 205 | inputs: [ 206 | { internalType: "address", name: "from", type: "address" }, 207 | { internalType: "address", name: "to", type: "address" }, 208 | { internalType: "uint256", name: "tokenId", type: "uint256" }, 209 | ], 210 | name: "safeTransferFrom", 211 | outputs: [], 212 | stateMutability: "nonpayable", 213 | type: "function", 214 | }, 215 | { 216 | inputs: [ 217 | { internalType: "address", name: "from", type: "address" }, 218 | { internalType: "address", name: "to", type: "address" }, 219 | { internalType: "uint256", name: "tokenId", type: "uint256" }, 220 | { internalType: "bytes", name: "_data", type: "bytes" }, 221 | ], 222 | name: "safeTransferFrom", 223 | outputs: [], 224 | stateMutability: "nonpayable", 225 | type: "function", 226 | }, 227 | { 228 | inputs: [], 229 | name: "saleIsActive", 230 | outputs: [{ internalType: "bool", name: "", type: "bool" }], 231 | stateMutability: "view", 232 | type: "function", 233 | }, 234 | { 235 | inputs: [ 236 | { internalType: "address", name: "operator", type: "address" }, 237 | { internalType: "bool", name: "approved", type: "bool" }, 238 | ], 239 | name: "setApprovalForAll", 240 | outputs: [], 241 | stateMutability: "nonpayable", 242 | type: "function", 243 | }, 244 | { 245 | inputs: [{ internalType: "string", name: "baseURI", type: "string" }], 246 | name: "setBaseURI", 247 | outputs: [], 248 | stateMutability: "nonpayable", 249 | type: "function", 250 | }, 251 | { 252 | inputs: [{ internalType: "bytes4", name: "interfaceId", type: "bytes4" }], 253 | name: "supportsInterface", 254 | outputs: [{ internalType: "bool", name: "", type: "bool" }], 255 | stateMutability: "view", 256 | type: "function", 257 | }, 258 | { 259 | inputs: [], 260 | name: "symbol", 261 | outputs: [{ internalType: "string", name: "", type: "string" }], 262 | stateMutability: "view", 263 | type: "function", 264 | }, 265 | { 266 | inputs: [{ internalType: "uint256", name: "index", type: "uint256" }], 267 | name: "tokenByIndex", 268 | outputs: [{ internalType: "uint256", name: "", type: "uint256" }], 269 | stateMutability: "view", 270 | type: "function", 271 | }, 272 | { 273 | inputs: [ 274 | { internalType: "address", name: "owner", type: "address" }, 275 | { internalType: "uint256", name: "index", type: "uint256" }, 276 | ], 277 | name: "tokenOfOwnerByIndex", 278 | outputs: [{ internalType: "uint256", name: "", type: "uint256" }], 279 | stateMutability: "view", 280 | type: "function", 281 | }, 282 | { 283 | inputs: [{ internalType: "uint256", name: "tokenId", type: "uint256" }], 284 | name: "tokenURI", 285 | outputs: [{ internalType: "string", name: "", type: "string" }], 286 | stateMutability: "view", 287 | type: "function", 288 | }, 289 | { 290 | inputs: [], 291 | name: "totalSupply", 292 | outputs: [{ internalType: "uint256", name: "", type: "uint256" }], 293 | stateMutability: "view", 294 | type: "function", 295 | }, 296 | { 297 | inputs: [ 298 | { internalType: "address", name: "from", type: "address" }, 299 | { internalType: "address", name: "to", type: "address" }, 300 | { internalType: "uint256", name: "tokenId", type: "uint256" }, 301 | ], 302 | name: "transferFrom", 303 | outputs: [], 304 | stateMutability: "nonpayable", 305 | type: "function", 306 | }, 307 | { 308 | inputs: [{ internalType: "address", name: "newOwner", type: "address" }], 309 | name: "transferOwnership", 310 | outputs: [], 311 | stateMutability: "nonpayable", 312 | type: "function", 313 | }, 314 | { 315 | inputs: [], 316 | name: "withdraw", 317 | outputs: [], 318 | stateMutability: "nonpayable", 319 | type: "function", 320 | }, 321 | ]; 322 | 323 | const cost = "60000000000000000"; 324 | 325 | var Web3; 326 | var window; 327 | 328 | async function getAccounts() { 329 | try { 330 | let acc = await window.ethereum.request({ 331 | method: "eth_requestAccounts", 332 | }); 333 | 334 | return acc; 335 | } catch (e) { 336 | return []; 337 | } 338 | } 339 | 340 | async function connectMetamask() { 341 | if (window.ethereum) { 342 | try { 343 | const result = await this.getAccounts(); 344 | if (Array.isArray(result) && result.length > 0) { 345 | let acc = result[0]; 346 | return acc; 347 | } else { 348 | return false; 349 | } 350 | } catch (err) { 351 | return false; 352 | } 353 | } else { 354 | return false; 355 | } 356 | } 357 | 358 | async function mint() { 359 | let totalToMint = document.getElementById("to_mint").value; 360 | var web3 = new Web3(Web3.givenProvider); 361 | window.contract = await new web3.eth.Contract(mainnetAbi, mainnetContract); 362 | const transactionParameters = { 363 | to: mainnetContract, 364 | from: (await this.getAccounts())[0], 365 | value: bigInt(cost).multiply(bigInt(totalToMint.toString())).toString(16), 366 | data: window.contract.methods.mintCraniums(totalToMint).encodeABI(), 367 | }; 368 | try { 369 | await window.ethereum.request({ 370 | method: "eth_sendTransaction", 371 | params: [transactionParameters], 372 | }); 373 | } catch (error) { 374 | console.log(error); 375 | } 376 | } 377 | --------------------------------------------------------------------------------