Hey everyone! I’m working on a project and I need some help. I want to find out how to get the highest price an NFT has been sold for using the main smart contract in Solidity. My goal is to create a mapping between the token ID and its highest sale price within the collection.
I’m pretty sure there’s a smart way to do this, but I’m stuck. Can anyone point me in the right direction? I’m talking about the contract that actually mints the NFTs, which inherits from the ERC721 standard.
Any tips or code snippets would be super helpful! Thanks in advance for your help. Really appreciate the community here!
hey there ben_comics! interesting project you’ve got going on. i’m curious, have you considered using events to track nft sales? you could emit an event every time an nft is sold, including the price and token id. then you could create a separate contract or use an off-chain solution to listen for these events and keep track of the highest sale price for each token.
what’s your thought process on updating the highest price? would you want to do it on-chain or off-chain? on-chain might be more gas-intensive but could be useful for other contracts to access directly.
also, have you looked into using oracles to get price data from external marketplaces? might be worth exploring if you want a more comprehensive view of sales across different platforms.
let me know what you think! always excited to brainstorm on nft projects
yo Ben_Comics, have u checked out OpenZeppelin’s ERC721 implementation? they got some cool features you might wanna use. for tracking highest sale price, maybe add a mapping in ur contract like this:
One possible solution is to implement a function that updates a mapping every time a transfer occurs. This mapping would hold the highest sale price for each token, and you would update it conditionally during each sale. It is important to note that not every transfer qualifies as a sale, so any implementation should include logic to distinguish a genuine sale from other transfers.
Keep in mind that updating state variables frequently may raise gas costs, so it might be beneficial to consider off-chain tracking through events if scalability becomes a concern.