0

So I am very new to Blockchain development so sorry in advance!

I have deployed a very basic Smart Contract to the Ropsten testnet. I am developing a front-end and from here, I want to withdraw ether/wei from the contract address(using a withdraw method), to the account calling the function(injected Metamask). It works perfect when I go onto etherscan to my contract and use the withdraw function there directly. I just don't know how to call the withdraw function through web3 js properly. I keep getting the following error message...

https://imgur.com/a/RSVrJIF

Can someone help point me in the right direction? Thanks!

pragma solidity ^0.5.0;

contract Bank{
    mapping(address=>uint) balances;

    function deposit() public payable{
        balances[msg.sender]+=msg.value;
    }
    function withdraw(uint amount) public{
        if(balances[msg.sender]>=amount){
            balances[msg.sender]-=amount;
            msg.sender.transfer(amount);
        }
    }
}
```solidity

```Js

Web3 = require('web3');

if (typeof web3 != 'undefined')
{
    web3 = new Web3(web3.currentProvider);
    console.log("existing web3: provider " + typeof web3);
    console.log(web3.currentProvider);
}
else{
    //To Do..
    web3 = new Web3(new 
    Web3.providers.HttpProvider("https://ropsten.infura.io/"));
    console.log("new provider " + web3);
}

console.log(web3.isConnected());
console.log(web3.eth.accounts[0]);

abi = JSON.parse('[{"constant":false,"inputs":[{"name":"amount","type":"uint256"}],"name":"withdraw","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"getMyBalance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"deposit","outputs":[],"payable":true,"stateMutability":"payable","type":"function"}]');

var bankContract = web3.eth.contract(abi);
contractInstance = bankContract.at('myaddressishere');
console.log (contractInstance);

function withdraw() {
    contractInstance.withdraw(300000000000000000, { from: web3.eth.accounts[0]})
}
```JS
  • What's the output of console.log(web3.eth.accounts[0]);? Are you seeing the address you expect? – smarx Apr 11 at 18:01
  • It returns undefined. My actual smart contract address is returned however. The error is thrown when I execute the withdraw method/function. – OBRI3N94 Apr 11 at 18:12
  • This probably means MetaMask is locked or in privacy mode, assuming you're using the MetaMask path through the code. The error is because the from address is invalid (specifically, it's undefined). – smarx Apr 11 at 18:28
  • Thanks for the help smarx, really appreciate it as it's part of my final year project. It does seem to have fixed it as the output of console.log(web3.eth.accounts[0]); returns my address. However I now have run into another problem when I try the withdraw function.. imgur.com/a/DIJGZS2 – OBRI3N94 Apr 11 at 18:40
  • As the error says, you need to pass a callback parameter. contractInstance.withdraw(300000000000000000, { from: web3.eth.accounts[0]}, function (err, result) { /* ... */ }); – smarx Apr 11 at 18:43

Your Answer

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

Browse other questions tagged or ask your own question.