0

Let's say I have a decentralized application that allows people to buy some object at an arbitrary price.

User 1 purchases the object for 1 eth. A few moments later, user 2 purchases the same item. How can I monitor for this event and automatically return the appropriate amount of etherium to user 2? Here's what I have so far:

contract Adoption {
    address[16] public adopters;

    function returnEth() public payable {
    msg.sender.transfer(msg.value);
    }
    // Adopting a pet
    function adopt(uint petId) public payable returns (uint) {
        require(petId >= 0 && petId <= 15);
        //require(msg.value == 1 ether);
        adopters[petId] = msg.sender;
        returnEth();
        return petId;
        //return value;
    }
    // Retrieving the adopters
    function getAdopters() public view returns (address[16]) {
        return adopters;
    }


}
1
  • Your second question is unrelated to the first. I'd suggest asking it as a new question. (It's hard for others to benefit from your question or answer it if it's buried inside another question.)
    – user19510
    Commented Feb 15, 2018 at 4:54

1 Answer 1

1

Reading between the lines, I think you're saying that user 2 shouldn't be allowed to purchase the item that user 1 already purchased.

If that's the case, then the call user 2 made to purchase the item should fail. You haven't shared the function that is used to make a purchase, but it should revert the transaction if the item is already sold.

14
  • What happens if the transaction is still being confirmed? Commented Feb 15, 2018 at 4:57
  • A transaction that hasn't been confirmed is just waiting around. It doesn't have any effect. When it finally gets processed, it will either succeed (if it's the first to buy the item) or fail.
    – user19510
    Commented Feb 15, 2018 at 4:58
  • That's good information, but I'd still like to know how to send ether to users from the contract. Should I update my original question? Commented Feb 15, 2018 at 5:00
  • It's just <address>.transfer(<amount>).
    – user19510
    Commented Feb 15, 2018 at 5:02
  • So would my original code theoretically work, if it was called at the right time? Commented Feb 15, 2018 at 5:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.