Reputation: 121
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.
Upvotes: 2
Views: 2818
Reputation: 775
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.
address(_seller).transfer(msg.value)
transfers 1 ether to _seller
.address(_buyer).transfer(msg.value)
transfers 1 ether to _seller
.msg.sender.transfer(msg.value)
transfers 1 ether to _seller
.It fails because there's only 1 ether.
Upvotes: 1