0

I have an openZeppelin ERC721 NFT contract (MyNFTPrice.sol) and also a separate PaymentSplitter contract. My understanding is that these two contract need to be deployed separately. My question is, how do I send the price of minting from my NFT contract (MyNFTPrice.sol) to the PaymentSplitter contract? Currently, the price for minting an NFT resides in the MyNFTPrice.sol contract address.

MyNFTPrice.sol

 pragma solidity ^0.8.0;

 import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
 import "@openzeppelin/contracts/utils/Counters.sol";
 import "@openzeppelin/contracts/access/Ownable.sol";
 import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";

contract MyNFTPrice is ERC721URIStorage {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;

constructor() public ERC721("MyNFTPrice", "NFTPRICE") {}


// Mint new NFT
function mintNFT(address recipient, string memory tokenURI) public payable  {

    require(msg.value >= 50000000000000000, "You need 0.05 ETH to mint the NFT"); 

    _tokenIds.increment();

    uint256 newItemId = _tokenIds.current();
    _mint(recipient, newItemId);
    _setTokenURI(newItemId, tokenURI);

}
}
johnDoe
  • 709
  • 11
  • 29

1 Answers1

0

You can use the transfer() member of the address payable.

function mintNFT(address recipient, string memory tokenURI) public payable  {
    require(msg.value >= 50000000000000000, "You need 0.05 ETH to mint the NFT");

    // effectively redirects the `msg.value` to the `0x123` address
    payable(address(0x123)).transfer(msg.value);

    // ... rest of your code
}

Replace the 0x123 to the real address of the PaymentSplitter.

You can also store the address in a variable and change it when you need to. In this case, it's recommended to use an authorization mechanism, such as the Ownable pattern, so that only an authorized sender can change the value.


Since the PaymentSplitter is a contract, it needs to contain the receive() or fallback() payable function. Otherwise the PaymentSplitter as the receiver would reject the incoming payment and effectively cause the whole mintNFT() transaction to revert.

contract PaymentSplitter {
    receive() external payable {
        // can be empty
    }
}
Petr Hejda
  • 40,554
  • 8
  • 72
  • 100