forked from andreykobal/skale-nft-game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
81 lines (65 loc) · 2.17 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
// index.js
const express = require("express");
const { ethers } = require("hardhat");
const { SDK, Auth, TEMPLATES, Metadata } = require("@infura/sdk");
require("dotenv").config();
const app = express();
const PORT = process.env.PORT || 3000;
app.use(express.json());
// Create Auth object
const auth = new Auth({
projectId: process.env.INFURA_API_KEY,
secretId: process.env.INFURA_API_KEY_SECRET,
privateKey: process.env.WALLET_PRIVATE_KEY,
rpcUrl: process.env.EVM_RPC_URL,
chainId: 137, // Polygon
});
// Instantiate SDK
const sdk = new SDK(auth);
const CONTRACT_ADDRESS = "0xe3515d63BCE48059146134176DBB18B9Db0D80D8";
async function mintItem(ownerAddress, tokenURI) {
const GameItem = await ethers.getContractFactory("GameItem");
const gameItem = await GameItem.attach(CONTRACT_ADDRESS);
return gameItem.mintItem(ownerAddress, tokenURI);
}
app.post("/mint", async (req, res) => {
try {
const { ownerAddress, tokenURI } = req.body;
if (!ownerAddress || !tokenURI) {
return res.status(400).json({ error: "Missing required fields" });
}
const result = await mintItem(ownerAddress, tokenURI);
res.status(200).json({ result });
} catch (error) {
console.error(error);
res.status(500).json({ error: "Internal server error" });
}
});
app.get("/tokenMetadata", async (req, res) => {
const { contractAddress, walletAddress } = req.query;
if (!contractAddress || !walletAddress) {
return res.status(400).send("contractAddress and walletAddress are required");
}
try {
const mynfts = await sdk.api.getNFTs({ publicAddress: walletAddress });
const metadataPromises = [];
for (const nft of mynfts.assets) {
if (nft.contract === contractAddress) {
metadataPromises.push(
sdk.api.getTokenMetadata({
contractAddress: nft.contract,
tokenId: nft.tokenId,
})
);
}
}
const metadataArray = await Promise.all(metadataPromises);
res.status(200).json(metadataArray);
} catch (error) {
console.log(error);
res.status(500).send("Error fetching token metadata");
}
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});