1

The scenario is: I am executing my contract from remix which is connected to my blockchain and I am able to execute functions from both geth console (by creating a contract instance using abi and address) as well as from the remix UI.

Now I try to do the same using a javascript file so that I can have a UI for it. So I create a js file, include web3 library to it and connect it to my blockchain. Till here, I am able to work successfully. On executing document.write(web3.eth.getBalance(web3.eth.accounts[0]) I get the correct balance. But when I try to execute contractInstance.deposit(web3.eth.accounts[0], 10000) (deposit is a method in my contract which takes two parameters, account address and amount) it throws an invalid address error. However, I am able to execute the same line of code from the geth console.

I guess I am making some silly mistake.

PS: I've tried contractInstance.deposit(web3.eth.accounts[0], 10000).call() and contractInstance.deposit(web3.eth.accounts[0], 10000).call but still it throws the same error.

This is the contract.enter image description here

This is the JS file. (apart from get() method, rest everything is working fine).

Output from the browser (note that the js file is able to print address and balance to the browser's console).

  • Any suggestions on how can I execute contract methods from a js file are welcome. – Anchit Seth Sep 12 '17 at 7:50
1

When calling a function, make sure you add what account you'd like to originate the call from.

return contractInstance.deposit(web3.eth.accounts[0], 1000, {
                    from: web3.eth.accounts[0]
                });
  • Is this the same as what is referred by "msg.sender" in remix. – Anchit Seth Sep 12 '17 at 13:28
  • 1
    The extra object you should be adding to your function arguments should include the account you want to send the transaction from. When the code is running on the VM, this will indeed be the msg.sender you're referring to. – Qkyrie Sep 12 '17 at 13:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.