For sure, here we go:
Server URL : https://c10qqeiy1pia.usemoralis.com:2053/server
chainID is 31337
Here is my component NFTBox.js
which is supposed to render an NFT for sale :
import { useEffect, useState } from "react"
import { useWeb3Contract, useMoralis } from "react-moralis"
//import nftMarsketplaceAbi from "../constants/NftMarsketplace.json"
import nftAbi from "../constants/MintOneToken.json"
export default function NFTBox({ price, nftAddress, tokenId, marsKetplaceAddress, seller }) {
//const [imageURI, setImageURI] = useState("")
const { isWeb3Enabled } = useMoralis()
const { runContractFunction: getTokenURI } = useWeb3Contract({
abi: nftAbi,
contractAddress: nftAddress,
functionName: "tokenURI",
params: {
tokenId: tokenId,
},
})
async function updateUI() {
// get tokenURI
try {
const tokenURI = await getTokenURI()
console.log(tokenURI)
} catch (e) {
console.log(e)
}
// using image tag from the tokenURI
}
useEffect(() => {
if (isWeb3Enabled) {
updateUI()
}
}, [isWeb3Enabled])
}
Here is the abi of the contract upon which the tokenURI
function
[{"type":"constructor","payable":false,"inputs":[]},{"type":"error","name":"MarsKetplace__AlreadyListed","inputs":[{"type":"address","name":"nftAddress"},{"type":"uint256","name":"tokenId"}]},{"type":"error","name":"MarsKetplace__NotApproved","inputs":[]},{"type":"error","name":"MarsKetplace__NotListed","inputs":[{"type":"address","name":"nftAddress"},{"type":"uint256","name":"tokenId"}]},{"type":"error","name":"MarsKetplace__NotOwner","inputs":[]},{"type":"error","name":"MarsKetplace__PriceCantBeZero","inputs":[]},{"type":"error","name":"MarsKetplace__PriceNotMet","inputs":[{"type":"address","name":"nftAddress"},{"type":"uint256","name":"tokenId"},{"type":"uint256","name":"price"}]},{"type":"event","anonymous":false,"name":"NFTBought","inputs":[{"type":"address","name":"buyer","indexed":true},{"type":"address","name":"nftAddress","indexed":true},{"type":"uint256","name":"tokenId","indexed":true},{"type":"uint256","name":"price","indexed":false}]},{"type":"event","anonymous":false,"name":"NFTDeleted","inputs":[{"type":"address","name":"nftAddress","indexed":true},{"type":"uint256","name":"tokenId","indexed":true}]},{"type":"event","anonymous":false,"name":"NFTListed","inputs":[{"type":"address","name":"seller","indexed":true},{"type":"address","name":"nftAddress","indexed":true},{"type":"uint256","name":"tokenId","indexed":true},{"type":"uint256","name":"price","indexed":false}]},{"type":"function","name":"buyNFT","constant":false,"stateMutability":"payable","payable":true,"gas":29000000,"inputs":[{"type":"address","name":"nftAddress"},{"type":"uint256","name":"tokenId"}],"outputs":[]},{"type":"function","name":"cancelListing","constant":false,"payable":false,"gas":29000000,"inputs":[{"type":"address","name":"nftAddress"},{"type":"uint256","name":"tokenId"}],"outputs":[]},{"type":"function","name":"getBalance","constant":true,"stateMutability":"view","payable":false,"gas":29000000,"inputs":[],"outputs":[{"type":"uint256"}]},{"type":"function","name":"getListing","constant":true,"stateMutability":"view","payable":false,"gas":29000000,"inputs":[{"type":"address","name":"nftAddress"},{"type":"uint256","name":"tokenId"}],"outputs":[{"type":"tuple","components":[{"type":"uint256","name":"price"},{"type":"address","name":"seller"}]}]},{"type":"function","name":"listNft","constant":false,"payable":false,"gas":29000000,"inputs":[{"type":"address","name":"nftAddress"},{"type":"uint256","name":"tokenId"},{"type":"uint256","name":"price"}],"outputs":[]},{"type":"function","name":"updateNFTPrice","constant":false,"payable":false,"gas":29000000,"inputs":[{"type":"address","name":"nftAddress"},{"type":"uint256","name":"tokenId"},{"type":"uint256","name":"newPrice"}],"outputs":[]},{"type":"function","name":"withdrawSales","constant":false,"payable":false,"gas":29000000,"inputs":[],"outputs":[]}]
Here is index.js
:
import Head from "next/head"
import Moralis from "moralis"
import styles from "../styles/Home.module.css"
import mars from "./mars.jpg"
import { useMoralisQuery } from "react-moralis"
import NFTBox from "../components/NFTBox"
function Home(props) {
const { data: listedNfts, isFetching: fetchingListedNfts } = useMoralisQuery(
//table name
"ActiveNFT",
// function for query
(query) => query.limit(10).descending("tokenId")
)
console.log(listedNfts)
return (
<div backgroundImage={mars} height="100vp">
<Head>
<title>The MarsKetplace</title>
<meta name="description" content="NFT MarsKetplace" />
<link rel="icon" href="/favicon.ico" />
</Head>
<h1 className="font-bold text-3xl">Own your plot on MARS</h1>
<h3 className="text-2xl">Here are the current NFTs for sale</h3>
<div>
<p>Display NFT here</p>
{fetchingListedNfts ? (
<div>Loading...</div>
) : (
listedNfts.map((nft) => {
console.log(nft.attributes)
const { price, nftAddress, tokenId, marsKetplaceAddress, seller } = nft.attributes
return (
<div>
Price : {price}, NFT address : {nftAddress}, Token ID : {tokenId}, Seller :{seller}
<NFTBox
price={price}
nftAddress={nftAddress}
tokenId={tokenId}
marsKetplaceAddress={marsKetplaceAddress}
seller={seller}
key={`${nftAddress}${tokenId}`}
/>
</div>
)
})
)}
</div>
</div>
)
}
export async function getServerSideProps(context) {
// reads the api key from .env.local and starts Moralis SDK
await Moralis.start({ apiKey: process.env.MORALIS_API_KEY })
return {
props: {},
}
}
export default Home
I am correctly connected to my metamask, using the automatically deployed account from hardhat (on localhost 8545).
Here is the console’s printing result when loading the page after running yarn dev
:
[ParseObject]
react_devtools_backend.js:4082 {marsketplaceAddress: '0xe7f1725e7734ce288f8367e1bb143e90bb3f0512', nftAddress: '0x9fe46736679d2d9a65f0992f2272de9f3c7fa6e0', price: '1000000000000000000', tokenId: '0', seller: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266', …}
undefined NFTBox.js?3c6a:23
The last line next to undefined
highlights console.log(tokenURI)
on the NFTBox.js
component.
I’ve tried removing the try/catch, same result ; I minted and listed a new NFT with a new tokenURI, same result.