2

Hey folks im having trouble funding my contract with web3.

I have a very simple payable function in the contract which is just there to accept money.

function makePayment() public payable returns (bool) {
    return true;
}

For my frontend I have a very basic html form just to input some amount and then a button to fund the contract.

Here is my HTML:

    <form class="pure-form">
        <input id="name" type="text" placeholder="Fund Contract" />
        <button type="submit" class="sendMoney"></button>
    </form>      

    <script>
        var nameInput = document.getElementById('name');

        document.querySelector('form.pure-form').addEventListener('submit', function (e) {

        //prevent the normal submission of the form
        e.preventDefault();

        // console.log(nameInput.value);       
     });
     </script>

Then in my app.js I have a function for sending the money:

sendMoney: function() {
    // console.log(nameInput.value);       
    var practiceInstance;
    App.contracts.Practice.deployed().then(function (instance) {
        practiceInstance = instance;
        return practiceInstance.makePayment().send ({            
            from: web3.eth.accounts[1],
            value: web3.utils.toWei(nameInput.value, 'ether')
        });
    }).catch(function (err) {
        console.log(err.message);
    });
},

This^ function is being called when the "sendMoney" button is tapped.

So what's happening is when I write in an amount of ether in the app to send to the contract I get a log message saying "Cannot read property 'toWei' of undefined"

And in my meta mask it shows that 0 ether is being sent to the contract for some reason:

enter image description here

TylerH
  • 20,799
  • 66
  • 75
  • 101
benjamin852
  • 509
  • 7
  • 19
  • Change it to `web3.toWei` (remove `utils`). – Adam Kipnis May 03 '18 at 14:15
  • Changed it to that. Now it says "practiceInstance.makePayment(...).send is not a function" Anything else maybe wrong in my syntax or logic you think? – benjamin852 May 03 '18 at 14:31
  • Yes. Also remove `()` from `makePayment`. It’s not a function call. It should be `practiceInstance.makePayment.send(...)`. – Adam Kipnis May 03 '18 at 14:37
  • You also won’t be returning a `bool` from the call. Transactions don’t return values from contracts to the client. The `send` call should include a callback that will be called with the transaction hash and then the receipt once mined. But, the basic functionality you’re attempting to do should work despite the return value. – Adam Kipnis May 03 '18 at 14:39
  • @AdamKipnis Are you sure about removing the `()`? I think this may differ between web3.js 0.2x.x, web3.js 1.0 beta, and Truffle. – user94559 May 03 '18 at 15:55
  • Here is what I got as of now: `return practiceInstance.makePayment.send ({ from: web3.eth.accounts[1], value: web3.toWei(nameInput.value, 'ether') });` I also changed and re-migrated my contract so my payable func looks like this now: `function makePayment() public payable { }` Still getting the "practiceInstance.makePayment.send is not a function" error in my console though. Any idea? – benjamin852 May 03 '18 at 16:11
  • 1
    @smarx - It's unclear which version is being used (@benjamin852?). I was assuming this was for 0.20.x, which should be `contract.makePayment.sendTransaction(...)` (sorry, I missed the `send` vs `sendTransaction`). For 1.0, it would be `contract.methods.makePayment().send(...)`. – Adam Kipnis May 03 '18 at 21:24
  • @AdamKipnis. Thats it! sendTransaction() works. I didn't realize i was using the older web3 version. Thanks man you're a life saver! – benjamin852 May 04 '18 at 14:23

0 Answers0