So I am creating a web application that interacts with a smart contract that I deployed on the Ropsten TestNet.

I am able to withdraw ether from the contract to my Metamask account using a withdraw function.

However I am struggling to find out the correct method to deposit ether to the Smart Contract i.e what syntax/function do I need to use. My contract is definitely Payable(), and lets me deposit ether when I view and interact with it through Etherscan.

Any help would be greatly appreciated as I am under a lot of pressure. Thanks!

Here is how I can withdraw ether... now I need a deposit function

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('TheContractAddressHere');

function withdraw() {
  contractInstance.withdraw(300000000000000000, { from: web3.eth.accounts[0]}, function(err, result){ revert(); } );

function depositFunction() {    
    contractInstance.deposit({ from: web3.eth.accounts[0], value: <amount-in-Wei>}, function(err, result){ .... } );
  • 1
    Man, I can't start to thank you enough, you have saved my bacon big time, its part of my final year project and due tomorrow . Thanks again once more! – OBRI3N94 May 19 '19 at 16:31
  • 1
    :) you are welcome – Majd TL May 19 '19 at 19:32

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.