2

Anyone know why my parameter is being seemingly ignored when I run the dapp?

This function is called when I press the button to send an X amount of money to a ganache account who's address I input into an HTML form while using the dapp.

    App.contracts.EthereumPractice.deployed().then(function (instance) {
        return instance.sendMoney.sendTransaction(addressInput.value, {
             from: web3.eth.accounts[0],
             value: etherAmount
        }); 
    },

I'm pretty confident the ^etherAmount variabe is not the problem as the money is being sent, it's just being sent to the wrong place (the contract adress not the imported ganache account address).

My solidity function takes in an address parameter and transfers the money to that address parameter value, so what am I doing wrong in the Web3 part?

Here's the solidity func for those who just want to double check that

function sendMoney(address _sendToThisAddress) public {
    _sendToThisAddress.transfer(this.balance);
}

When my meta mask pops up it ignores the address parameter and instead transfer the money straight to the contracts address rather than to the import ganache account address, which I am trying to send the money to.

TylerH
  • 20,799
  • 66
  • 75
  • 101
benjamin852
  • 509
  • 7
  • 19

1 Answers1

1

There's a couple bugs with your code. You have to mark the function as payable and you're transferring the entire balance of the contract, not what you are sending. When you send ether to a payable function, the ether is owned by the contract. From there, you can then send it (or a different amount) to another address. If you're trying to send exactly what is sent from your client to the address parameter, you should do the following:

function sendMoney(address _sendToThisAddress) public payable {
    _sendToThisAddress.transfer(msg.value);
}
Adam Kipnis
  • 10,175
  • 10
  • 35
  • 48
  • Got it. So I added the payable and changed this.balance to msg.value. And if I understand correctly, I cannot directly send from one address to another address? Like it has to go through the contract. So how would I send it from the contract to that second address where I want the ether to end up at? Thx! – benjamin852 May 14 '18 at 03:13
  • You CAN send ether to an address directly. No smart contract is needed. You just call transfer on the EOA account address. – Adam Kipnis May 14 '18 at 04:55