简体   繁体   中英

How to send etherenum to msg.sender in solidity 0.5.0

mapping(uint => Product) public products;

struct Product {

    uint id;
    string name;
    uint price;
    address payable owner;
    bool purchased;   
}

function purchaseProduct(unit _id) public payable {

    Product memory _product = products[_id];
    address payable _seller = _product.owner;
    address payable _buyer = msg.sender;
}

address(_seller).transfer(msg.value) works good. But msg.sender.transfer(msg.value) and address(_buyer).transfer(msg.value) doesn't work.

please help me to fix this issue.

In this block (added line number), msg.value is the amount of ether transferred to the contract when calling function.

At line 1, it has already transferred all the ether to _seller. Line 2 and 3 will fail because there is no more ether left.

1    address(_seller).transfer(msg.value);
2    address(_buyer).transfer(msg.value);
3    msg.sender.transfer(msg.value);  

Example: I call purchaseProduct() function with value of 1 ether.

  1. address(_seller).transfer(msg.value) transfers 1 ether to _seller .
  2. address(_buyer).transfer(msg.value) transfers 1 ether to _seller .
  3. msg.sender.transfer(msg.value) transfers 1 ether to _seller .

It fails because there's only 1 ether.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM