簡體   English   中英

我正在嘗試創建令牌存儲合同

[英]I'm trying to create a token store contract

創建此合同時,行“_buyer.transfer(buyers[_buyer]);” 生成錯誤““發送”和“轉移”僅適用於“應付地址”類型的對象,而不是“地址”類型的對象。“。

這是合同代碼。

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract CoinStore {
    address owner;
    mapping(address => uint256) buyers;
    mapping(address => uint256) sellers;
    uint256 coinPrice;
    bool tradePaused;

    constructor() {
        owner = msg.sender;
    }

    function setCoinPrice(uint256 _price) public {
        require(msg.sender == owner, "Only the owner can set the coin price.");
        coinPrice = _price;
    }

    function registerBuyer(address _buyer, uint256 _amount) public {
        require(!tradePaused, "Trading is currently paused.");
        buyers[_buyer] = _amount;
    }

    function registerSeller(address _seller, uint256 _amount) public {
        require(!tradePaused, "Trading is currently paused.");
        sellers[_seller] = _amount;
    }

    function pauseTrade() public {
        require(msg.sender == owner, "Only the owner can pause trading.");
        tradePaused = true;
    }

    function startTrade() public {
        require(msg.sender == owner, "Only the owner can start trading.");
        tradePaused = false;
    }

    function completeSale(address _buyer, address _seller) public payable {
        require(_buyer.balance >= buyers[_buyer], "Buyer does not have enough funds.");
        require(_seller.balance >= sellers[_seller], "Seller does not have enough funds.");
        require(msg.value == coinPrice, "Incorrect payment amount.");

        _buyer.transfer(buyers[_buyer]);
        _seller.transfer(sellers[_seller]);

        if (msg.value > buyers[_buyer]) {
            _buyer.transfer(msg.value - buyers[_buyer]);
        }
    }

    function withdrawFunds() public {
        require(msg.sender == owner, "Only the owner can withdraw funds.");
        msg.sender.transfer(address(this).balance);
    }
}

我是 Solidity 的新手,並不是智能合約開發方面的專家。 盡我所能。 請幫幫我。 謝謝你。

您嘗試發送到的地址也必須是可支付的。

您的取款應如下所示:

function withdrawFunds() public {
    require(msg.sender == owner, "Only the owner can withdraw funds.");
    payable(msg.sender).transfer(address(this).balance);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM