[英]Despite using the receive() function, my contract is not receiving payment in Remix
我正在使用 Remix with Solidity 编写智能合约。 合约的目的是允许用户用 1 ETH 铸造 NFT。 目前,用户可以铸币并且合约接受付款(即用户的余额被适当地扣除)。 但是当我使用我的accountBalance()
函数检查合约的address(this).balance
时,该函数返回0
。 我根据 Solidity 文档包含了receive()
函数:
event Received(address, uint);
receive() external payable {
emit Received(msg.sender, msg.value);
}
有人可以解释为什么会发生这种情况以及我需要对我的合同进行哪些更改吗? 这是我的合同:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
// imports
import '@openzeppelin/contracts/token/ERC721/ERC721.sol';
import '@openzeppelin/contracts/access/Ownable.sol';
import '@openzeppelin/contracts/security/PullPayment.sol';
// contract
contract RobocopPoster is ERC721, Ownable, PullPayment {
// constants
uint256 public mintPrice;
uint256 public totalSupply;
uint256 public maxSupply;
uint256 public maxPerWallet;
bool public mintEnabled;
mapping (address => uint256) public walletMints;
// constructor
// initialize variables
constructor() payable ERC721('RobocopPoster', 'SFFPC') {
mintPrice = 1 ether;
totalSupply = 0;
maxSupply = 1000;
maxPerWallet = 3;
}
event Received(address, uint);
receive() external payable {
emit Received(msg.sender, msg.value);
}
// functions
function setMintEnabled(bool mintEnabled_) external onlyOwner {
mintEnabled = mintEnabled_;
}
function withdrawPayments(address payable payee) public override onlyOwner virtual {
super.withdrawPayments(payee);
}
function accountBalance() public view returns (uint256) {
return (address(this).balance);
}
function mint(uint256 quantity_) public payable {
require(mintEnabled, 'Minting not enabled.');
require(msg.value == quantity_ * mintPrice, 'wrong mint value');
require(totalSupply + quantity_ <= maxSupply, 'sold out');
require(walletMints[msg.sender] + quantity_ <= maxPerWallet, 'exceed max wallet');
walletMints[msg.sender] += quantity_;
_asyncTransfer(address(this), msg.value);
for (uint i = 0; i < quantity_; i++) {
uint256 newTokenId = totalSupply + 1;
totalSupply++;
_safeMint(msg.sender, newTokenId);
}
}
}
您需要调用withdrawPayments
来接收资金,因为在您的合约铸币中来自_asyncTransfer
的_asyncTransfer将资金发送到了托管合约。 这就是为什么您在 ERC721 合约中看到零余额的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.