0

I'm using MetaMask and making some changes to the Ethereum Pet Shop tutorial. I'm working with two different MetaMask accounts using the Ganache CLI, and when adopt() is called, I'm expecting some eth to be sent back to the previous "owner", or my other account, however, it seems like no eth is being sent from the contract at all.

Here's the code:

pragma solidity ^0.4.19;
contract Adoption {
  struct Pet {
    address owner;
    uint256 price;

  }

  Pet[16] data;

  function Adoption() public {
    for (uint i = 0; i < 16; i++) {

      data[i].price = 500;
      data[i].owner = msg.sender;
    }
  }


  // Adopting a pet
  function adopt(uint petId) public payable returns (uint, uint) {
    require(petId >= 0 && petId <= 15);
    if ( data[petId].price == 0 ) {
      data[petId].price = 100;
    } else {
      data[petId].price = data[petId].price * 2;
    }

    require(msg.value >= data[petId].price * uint256(1));
    returnEth(data[petId].owner,  (data[petId].price / 2)); 
    data[petId].owner = msg.sender;
    return (petId, data[petId].price);
    //return value;
  }





  function getAdopters() external view returns (address[], uint256[]) {
    address[] memory owners = new address[](16);
    uint256[] memory prices =  new uint256[](16);
    for (uint i=0; i<16; i++) {
      owners[i] = (data[i].owner);
      prices[i] = (data[i].price);
    }
    return (owners,prices);
  }

}

Can anyone point me in the right direction? Thanks.

To clarify, I want to send ethereum from the contract to the previous "owner" of a pet.

2 Answers 2

1

When you divide 500 / 1000 in this line oldOwner.transfer((price / 1000)); you get 0. Note that transfer() accepts the value in wei not in Ether. If your intention is to send 0.5 wei, then it's not possible.

Here is more details about integer division in Solidity Can't do any integer division

3
  • Thanks for pointing that out. However, I tried using oldOwner.transfer(price); without any division after setting data[i].price = 100000000; , and there's still no movement between the MetaMask wallets. Commented Feb 20, 2018 at 17:31
  • any idea why this is happening? Commented Feb 20, 2018 at 21:48
  • Did you try it on Rinkeby? Just to see if this is only related to Ganache. Also try checking the result in geth instead of MetaMask to see if it's MetaMask related. Commented Feb 21, 2018 at 7:39
0

I had this exact problem, if you want to send the correct amount of ETH when testing the program in Remix, you can just enter the value (in wei) that you want to be sent with the transaction in the value field under the run tab. If you want to send ETH via a front end it gets a bit more tricky and you need to use the sendTransaction function in Web3.js, here's a sample function that you can call on an event (perhaps a button click)

   function callThisFunction() {
        web3.eth.getAccounts(function(error, result) {
        web3.eth.sendTransaction(
            {from:web3.eth.accounts[0],
            to: contractAdress,
            value:  "amount in wei you're requesting", 
            data: "hash of the function you want to call"
                }, function(err, result) {
          if (!err)
            console.log(result + " success"); 

        });
    });

    }

If you simply call this function and fill in the fields it will automatically fill in the correct amount of Ether (In your case MetaMask) that the contract wants the user to send for the program to work correctly.

7
  • it doesn't work at all in remix or ethfiddle. Commented Feb 20, 2018 at 4:53
  • @vipertherapper what exactly doesn't work? I noticed in your code you need another } at the end of your contract, if you put your contract in Remix it will have told you this.
    – JAG
    Commented Feb 20, 2018 at 6:56
  • are you sure? i pasted it into Remix and it doesn't give me a warning. Commented Feb 20, 2018 at 7:04
  • @vipertherapper you might have missed it when copying and pasting it onto stack overflow? What's the problem that you're having?
    – JAG
    Commented Feb 20, 2018 at 7:05
  • When adopt() is called on my front end, no eth is sent from the contract out to the owner. Commented Feb 20, 2018 at 7:19

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.