I am working on a marketplace NFT website. Should I use web3.js to track owners and other contract details when a user opens a specific website, or make a database that tracks those data? I want to get the owner, tokenId and some other built in read functions.
2 Answers
for simplicity, use web3js to track these data. It is difficult to ensure data from database will be up to date. For advance use case, create a program to monitor the block head and when detect token contracts, update or insert these token contracts information into database. Using database will yield better user experience.
You can get the owners of an NFT using Moralis NFT API.
Here is an example of getting the owners using moralis
const Moralis = require("moralis").default;
const runApp = async () => {
const { EvmChain } = Moralis.EvmUtils;
await Moralis.start({
apiKey: "YOUR_API_KEY",
// ...and any other configuration
});
const chain = EvmChain.ETHEREUM;
const address = "0xed5af388653567af2f388e6224dc7c4b3241c544";
const response = await Moralis.EvmApi.nft.getNFTOwners({
address,
chain,
});
console.log(response.raw);
};
runApp();
And this is what the results will look like.
[
{
"token_address": "0xed5af388653567af2f388e6224dc7c4b3241c544",
"token_id": "7418",
"amount": "1",
"owner_of": "0x77e3e957082ca648c1c5b0f3e6aec00ab1245186",
"token_hash": "74455e7bcbe36c4ef69341bdd0539086",
"block_number_minted": "13992215",
"block_number": "16765479",
"contract_type": "ERC721",
"name": "Azuki",
"symbol": "AZUKI",
"token_uri": "https://ipfs.moralis.io:2053/ipfs/QmZcH4YvBVVRJtdn4RdbaqgspFU8gH6P9vomDpBVpAL3u4/7418",
"metadata": "{\"name\":\"Azuki #7418\",\"image\":\"ipfs://QmYDvPAXtiJg7s8JdRBSLWdgSphQdac8j1YuQNNxcGE1hg/7418.png\",\"attributes\":[{\"trait_type\":\"Type\",\"value\":\"Blue\"},{\"trait_type\":\"Hair\",\"value\":\"Silver Pixie\"},{\"trait_type\":\"Face\",\"value\":\"Red Fang Face Paint\"},{\"trait_type\":\"Clothing\",\"value\":\"Red Panda Hoodie\"},{\"trait_type\":\"Eyes\",\"value\":\"Striking\"},{\"trait_type\":\"Mouth\",\"value\":\"Smile\"},{\"trait_type\":\"Offhand\",\"value\":\"Bean Juice\"},{\"trait_type\":\"Background\",\"value\":\"Off White D\"}]}",
"last_token_uri_sync": "2023-02-10T09:09:27.248Z",
"last_metadata_sync": "2023-03-05T08:27:32.388Z",
"minter_address": "0x07df20aa702114a9540c9eec83d6a2a92fbdf46d"
},
{
"token_address": "0xed5af388653567af2f388e6224dc7c4b3241c544",
"token_id": "2949",
"amount": "1",
"owner_of": "0x77e3e957082ca648c1c5b0f3e6aec00ab1245186",
"token_hash": "4a02af90c1314abadf0aa15ff30447b6",
"block_number_minted": "13992208",
"block_number": "16765479",
"contract_type": "ERC721",
"name": "Azuki",
"symbol": "AZUKI",
"token_uri": "https://ipfs.moralis.io:2053/ipfs/QmZcH4YvBVVRJtdn4RdbaqgspFU8gH6P9vomDpBVpAL3u4/2949",
"metadata": "{\"name\":\"Azuki #2949\",\"image\":\"ipfs://QmYDvPAXtiJg7s8JdRBSLWdgSphQdac8j1YuQNNxcGE1hg/2949.png\",\"attributes\":[{\"trait_type\":\"Type\",\"value\":\"Human\"},{\"trait_type\":\"Hair\",\"value\":\"Powder Blue Pigtails\"},{\"trait_type\":\"Headgear\",\"value\":\"Purple Kanzashi\"},{\"trait_type\":\"Neck\",\"value\":\"Chill Headphones\"},{\"trait_type\":\"Clothing\",\"value\":\"Tank Top with Jacket\"},{\"trait_type\":\"Eyes\",\"value\":\"Relaxed\"},{\"trait_type\":\"Mouth\",\"value\":\"Frown\"},{\"trait_type\":\"Background\",\"value\":\"Off White A\"}]}",
"last_token_uri_sync": "2023-02-10T07:29:09.377Z",
"last_metadata_sync": "2023-03-05T08:03:11.332Z",
"minter_address": "0x98ebfcab380f83c3412f7036f1b1dc149c907c83"
},
{
"token_address": "0xed5af388653567af2f388e6224dc7c4b3241c544",
"token_id": "8766",
"amount": "1",
"owner_of": "0x1a51bcbc8d3642680d8267550f54e6e4cc5c4984",
"token_hash": "80c5705c17437a741548d6898a8f2be7",
"block_number_minted": "13992217",
"block_number": "16765479",
"contract_type": "ERC721",
"name": "Azuki",
"symbol": "AZUKI",
"token_uri": "https://ipfs.moralis.io:2053/ipfs/QmZcH4YvBVVRJtdn4RdbaqgspFU8gH6P9vomDpBVpAL3u4/8766",
"metadata": "{\"name\":\"Azuki #8766\",\"image\":\"ipfs://QmYDvPAXtiJg7s8JdRBSLWdgSphQdac8j1YuQNNxcGE1hg/8766.png\",\"attributes\":[{\"trait_type\":\"Type\",\"value\":\"Human\"},{\"trait_type\":\"Hair\",\"value\":\"Powder Blue Pigtails\"},{\"trait_type\":\"Clothing\",\"value\":\"Light Armor\"},{\"trait_type\":\"Eyes\",\"value\":\"Ruby\"},{\"trait_type\":\"Mouth\",\"value\":\"420\"},{\"trait_type\":\"Background\",\"value\":\"Red\"}]}",
"last_token_uri_sync": "2023-02-11T01:04:12.853Z",
"last_metadata_sync": "2023-03-05T08:03:11.331Z",
"minter_address": "0x36abd0a112b5215c062a7a7125ba8cc23986bbe0"
},
]
You can find a guide here: How to get NFT owners
Disclosure: I work at Moralis
-
How quick is this api system? How much time will it take vs a mysql database that is on the server?– NFT_kingMar 6 at 9:33
-
Hi @NFT_king, Fetching data with moralis API is instant, getting the data yourself from the blockchain and storing it regularly might be time-consuming. Mar 8 at 21:30