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 {
    // 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;
        return petId;
        //return value;
    // Retrieving the adopters
    function getAdopters() public view returns (address[16]) {
        return adopters;

  • 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 Feb 15 '18 at 4:54

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.

  • What happens if the transaction is still being confirmed? – rustyshackleford Feb 15 '18 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 Feb 15 '18 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? – rustyshackleford Feb 15 '18 at 5:00
  • It's just <address>.transfer(<amount>). – user19510 Feb 15 '18 at 5:02
  • So would my original code theoretically work, if it was called at the right time? – rustyshackleford Feb 15 '18 at 5:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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