Search code examples
javascriptethereumsolidityweb3js

Ganache Address Parameter Being Overrided Web3 -0.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.


Solution

  • 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);
    }