我有一个openZeppelin ERC721 NFT合约(MyNFTPrice.sol)和一个单独的PaymentSplitter合约。我的理解是这两个合同需要分开部署。我的问题是,我如何从我的NFT合约(MyNFTPrice.sol)发送挖矿价格到PaymentSplitter合约?目前,制造NFT的价格驻留在mynft价格中。Sol合同地址
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);
}
}
可以使用address payable
的transfer()成员
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
}
将0x123
替换为PaymentSplitter
的真实地址
您还可以将地址存储在变量中,并在需要时更改它。在这种情况下,建议使用授权机制,例如Ownable模式,以便只有授权的发送方可以更改值。
由于PaymentSplitter
是一个契约,它需要包含receive()或fallback()payable
函数。否则,PaymentSplitter
作为接收方将拒绝收到的付款,并有效地导致整个mintNFT()
交易恢复。
contract PaymentSplitter {
receive() external payable {
// can be empty
}
}