首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >即使函数声明为payable,得到此错误:如果发送值,则应支付被调用的函数。

即使函数声明为payable,得到此错误:如果发送值,则应支付被调用的函数。
EN

Ethereum用户
提问于 2020-04-08 10:00:59
回答 1查看 411关注 0票数 0

虽然函数purchaseProduct()声明为even,但我还不知道为什么会出现上述错误。我在Remix上尝试了这段代码,得到了这个错误。

VM错误:还原。还原事务已恢复到初始状态。注意:如果您发送值,则应支付被调用的函数,而您发送的值应小于当前余额。调试事务以获得更多信息。

除了purchaseProduct()函数之外,一切都很正常。我正在尝试从struct 'User‘、'Product’中获取数据,并在一个新的结构中(即“Order”)重新存储数据。

我已经使用require()进行了验证。在Remix上运行代码时:

  1. 创建2个用户

  1. 使用第一个用户创建产品,然后,

  1. 使用第二用户的purchaseProduct
代码语言:javascript
复制
pragma solidity >=0.5.0;
pragma experimental ABIEncoderV2;

//Address: '0x7772B9117586b5F745bDb563a935E91b00449157'

contract Buyproduct{

    uint public orderCount = 0;
    mapping(uint => Order) public orders;

    uint public productCount = 0;
    mapping(uint => Product) public products;

    uint public userCount = 0;
    mapping(address => User) public users;

    struct Order {
        uint oid;
        address payable seller;
        address payable buyer;
      //  string timestamp;
        string status;
        uint pid;
        string location;
        uint quantitiy;
    }

    struct Product {
        uint pid;
        string name;
        uint price;
        address payable seller;

        string info;
        uint quantity;
    }

    struct User {
        uint uid;
        string name;
        uint role;
        address payable user;
        string location;
        bool created;
    }

    event ProductCreated(
        uint pid,
        string name,
        uint price,
        address payable seller,
        string info,
        uint quantity
    );

    event  UserCreated(
        uint uid,
        string name,
        uint role,
        address payable user,
        string location,
        bool created
    );

    event ProductPurchased(
        uint oid,
        address payable seller,
        address payable buyer,
        string status,
        uint pid,
        string location,
        uint quantity
    );

    event ReviewAdded(
        uint rid,
        address reviewer,
        uint pid,
        string review
    );

    function createUser(string memory _name, uint _role, string memory _location) public payable{
        //No repeated address
        require(users[msg.sender].created == false, 'User already created');
        //Increase userCount
        userCount++;
        //Add user
        users[msg.sender] = User(userCount, _name, _role, msg.sender, _location, true);
        //Trigger an event
        emit UserCreated(userCount, _name, _role, msg.sender, _location, true);
    }

    function createProduct(string memory _name, uint _price, string memory _info, uint _quantity) public payable {
        // Require a valid price
        require(_price > 0, 'Invalid Price');
        // Increment product count
        productCount ++;
        // Create the product
        products[productCount] = Product(productCount, _name, _price, msg.sender, _info, _quantity);
        // Trigger an event
        emit ProductCreated(productCount, _name, _price, msg.sender, _info, _quantity);
    }

    function purchaseProduct(uint _id, /*string memory timestamp,*/ uint _quantity) public payable {
        // Fetch the product
        Product memory _product = products[_id];
        // Fetch the owner
        address payable _seller = _product.seller;
        //Validate the buyer
        require(users[msg.sender].created == true, 'Unregistered user');
        // Make sure the product has a valid id
        require(_product.pid > 0 && _product.pid <= productCount, 'Invalid Product ID');
        // Require that there is enough Ether in the transaction
        require(msg.value >= _product.price, 'Not enough ether in Wallet');
        // Require that the buyer is not the seller
        require(_seller != msg.sender,'Invalid Purchase');
        //Incrmement orderCount
        orderCount++;
        // Transfer ownership to the buyer
        orders[orderCount] = Order(orderCount, _seller, msg.sender,/* ts,*/ 'Ordered', _product.pid, users[msg.sender].location, _quantity);
        //Reduce the quantity of the product
        _product.quantity -= _quantity;
        // Pay the contract by sending them Ether
        address payable wallet = address(uint160(address(this)));
        wallet.transfer(msg.value);
        // Trigger an event
        emit ProductPurchased(orderCount, _seller, msg.sender, /*timestamp,*/ 'Ordered', _product.pid, users[msg.sender].location, _quantity);
    }


}'''
EN

回答 1

Ethereum用户

发布于 2020-04-08 11:45:57

代码语言:javascript
复制
    function createProduct(string memory _name, uint _price, string memory _info, uint _quantity) public payable {

这个函数的签名在醚中有_price。因此,尝试更改默认单位从卫乙醚,并发送适当的价格的产品。

我的在工作

代码语言:javascript
复制
[vm]from:0x4b0...4d2dbto:Buyproduct.purchaseProduct(uint256,uint256) 0x1df...bda71value:2000000000000000000 weidata:0xa04...00001logs:1hash:0x4e0...0d2b0

您也不需要显式地调用这些。

代码语言:javascript
复制
address payable wallet = address(uint160(address(this)));
wallet.transfer(msg.value);

合同余额将自动更新。您可以通过在合同上实现此功能来进行检查。

代码语言:javascript
复制
    function getBalance()
             public 
             view 
             returns(uint) {
                return address(this).balance;}
票数 1
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/82275

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档