Transferring buyer addresses from an NFT contract to a non-NFT contract in Solidity

Hey everyone! I’m a newbie in Solidity and I’m stuck with a problem. I’ve got a basic NFT contract and I want to grab the wallet addresses of the NFT buyers when it’s minted. Then I need to send these addresses to another contract I made so I can do more stuff with them later. I’m not sure how to do this. Can anyone help me out?

Here’s a simple example of what I’m working with:

pragma solidity ^0.8.0;

contract SimpleNFT {
    address[] public buyers;
    uint256 public tokenCount;

    function mint() public {
        tokenCount++;
        buyers.push(msg.sender);
    }
}

contract OtherContract {
    // How do I get the buyers array from SimpleNFT?
}

I tried making a getter function and importing the NFT contract into the other one, but I got lost. Any tips would be awesome! Thanks!

I’ve encountered a similar challenge in my projects. One effective approach is to implement a function in your SimpleNFT contract that allows the OtherContract to retrieve the buyers’ addresses. Here’s how you could modify your SimpleNFT contract:

function getBuyers() public view returns (address[] memory) {
    return buyers;
}

Then, in your OtherContract, you can call this function by passing the SimpleNFT contract address:

contract OtherContract {
    SimpleNFT public nftContract;

    constructor(address _nftContractAddress) {
        nftContract = SimpleNFT(_nftContractAddress);
    }

    function retrieveBuyers() public view returns (address[] memory) {
        return nftContract.getBuyers();
    }
}

This method allows for a clean separation of concerns between contracts while enabling data transfer. Remember to consider gas costs if dealing with a large number of buyers.

yo StrummingMelody, cool project! have u tried using interfaces? they’re pretty handy for this kinda stuff. u could define an interface in ur OtherContract that matches the SimpleNFT functions u wanna use. then u can interact with SimpleNFT thru that interface. it’s like a bridge between contracts. lemme know if u need more help!

hey there, StrummingMelody! that’s a cool problem you’re working on. i’m kinda new to solidity too, but i’ve been playing around with contracts lately. have you thought about using events? :thinking:

you could emit an event in your SimpleNFT contract when someone mints, like this:

event NewBuyer(address buyer);

function mint() public {
    tokenCount++;
    buyers.push(msg.sender);
    emit NewBuyer(msg.sender);
}

i thought it might be an easy way to catch each buyer’s address. then in your OtherContract, you could listen for these events and handle the addresses accordingly. it’s pretty neat!

so, what do you plan on doing with these addresses? maybe integrating some reward mechanism or something else interesting? would love to hear more about your project!