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 Answers2

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

medvedev1088
  • 10,996
  • 5
  • 35
  • 63
  • 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. – rustyshackleford Feb 20 '18 at 17:31
  • any idea why this is happening? – rustyshackleford Feb 20 '18 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. – medvedev1088 Feb 21 '18 at 07: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.

JAG
  • 494
  • 1
  • 3
  • 12