尽管使用了recee()函数,但我的合同未通过混音收到付款

发布于 2025-02-07 21:25:38 字数 2308 浏览 2 评论 0原文

我正在以坚固的方式写一份智能合同。合同的目的是允许用户以1个ETH造成NFT。目前,用户能够造币并接受付款(即正确减去用户余额)。但是,当我检查合同的地址(this).balance使用我的AccountBalance()函数时,函数返回0。我按照坚固的文档包括了receion()函数:

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);
        }
    }
}

I am writing a smart contract in Remix with Solidity. The purpose of the contract is to allow a user to mint an NFT for 1 ETH. At present, the user is able to mint and the contract accepts the payment (ie. the user's balance is properly subtracted). But when I check the address(this).balance of the contract with my accountBalance() function, the function returns 0. I have included the receive() function as per the Solidity docs:

event Received(address, uint);
    receive() external payable {
        emit Received(msg.sender, msg.value);
    }

Can someone explain why this is happening and what I need to change about my contract? Here is my contract:

// 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);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静谧幽蓝 2025-02-14 21:25:38

您需要调用提款接收基金,因为_ASYNCTRANSFER来自 pullpayment 在您的合同中,铸造中将基金发送给了托管合同。这就是为什么您在ERC721合同中看到零余额的原因。

You need to call withdrawPayments to receive the fund, because _asyncTransfer from PullPayment in your contract minting sent the fund to the escrow contract. That's why you saw zero balance in ERC721 contract.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文