Web3.js – Sending Ether via Contract Instance

contract-invocationweb3js

My contract instance is created like:

var instance = web3.eth.contract(abi).at(addressA); 

instance.sendTransaction is showing error in this case, what's the correct way to send ether to this contract?

Best Answer

you can use web3.eth.sendTransaction() and set the contract's address in the "to" field of your transaction object.

Here is how to send a transaction :

var abi=[//your abi array];
var contractAddress = "//your contract address";
var contract = web3.eth.contract(abi).at(contractAddress);

//if you have the fallback payable :

web3.eth.sendTransaction({from:web3.eth.accounts[X],to:contractAddress , value:web3.utils.toWei('0.0000001','ether')}) //the value is an example    

// if you have a specific payable function

 contract.functionName.sendTransaction(parameter_1,parameter_2,parameter_n,{{from:web3.eth.accounts[X], value:xyz}},function (error, result){   if(!error){
                        console.log(result);
                    } else{
                        console.log(error);
                    }
            });
Related Topic