I’m working on a basic NFT contract and I need help. My goal is to grab the wallet addresses of NFT buyers when they mint and send that info to another contract. I’m not sure how to do this. I’m new to coding so please explain it simply.
Here’s a sample of what I’ve got so far:
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
contract SimpleNFT is ERC721 {
uint256 private tokenIdCounter;
string public constant NFT_URI = "ipfs://example";
constructor() ERC721("SimpleNFT", "SNFT") {
tokenIdCounter = 0;
}
function createToken() public {
tokenIdCounter++;
_safeMint(msg.sender, tokenIdCounter);
}
function tokenURI(uint256 tokenId) public view override returns (string memory) {
return NFT_URI;
}
}
I tried making a getter function but got confused. Also tried importing the NFT contract into another contract but that didn’t work either. Any ideas on how to pass the buyer addresses to a different contract? Thanks!
hey zoe, thats a cool project ur working on! i’m pretty new to solidity myself, but i’ve been playing around with nfts lately. have u thought about using events to track the minting? u could emit an event when someone mints an nft, and then have ur other contract listen for those events. something like:
event TokenMinted(address buyer, uint256 tokenId);
function createToken() public {
tokenIdCounter++;
_safeMint(msg.sender, tokenIdCounter);
emit TokenMinted(msg.sender, tokenIdCounter);
}
then in ur other contract, u could have a function that listens for these events and stores the addresses. but im not sure how to do that part exactly
maybe someone else here knows?
also, have u considered using a mapping to keep track of buyers? like mapping(address => bool) public buyers; and then in ur mint function u could do buyers[msg.sender] = true;
just some ideas! what do u think? how important is it to have this info in a separate contract?
yo zoe, cool project! i’ve done smthing similar. u could use a simple array to store addresses:
address[] public buyers;
function createToken() public {
tokenIdCounter++;
_safeMint(msg.sender, tokenIdCounter);
buyers.push(msg.sender);
}
the code then retrieves the array. other contracts can call it. hope this helps!
To transfer buyer addresses to another contract, you could implement an interface in your NFT contract that allows external calls. Here’s a potential approach:
pragma solidity ^0.8.0;
interface IBuyerTracker {
function addBuyer(address buyer) external;
}
contract SimpleNFT is ERC721 {
IBuyerTracker public buyerTracker;
constructor(address _buyerTracker) ERC721("SimpleNFT", "SNFT") {
buyerTracker = IBuyerTracker(_buyerTracker);
}
function createToken() public {
tokenIdCounter++;
_safeMint(msg.sender, tokenIdCounter);
buyerTracker.addBuyer(msg.sender);
}
}
This way, your NFT contract calls the external contract directly when a token is minted. The other contract would need to implement the IBuyerTracker interface. This method is more gas-efficient than storing all addresses in the NFT contract itself.