1

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?

Urja Pawar
  • 200
  • 2
  • 11

2 Answers2

2

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);
                    }
            });
Badr Bellaj
  • 18,780
  • 4
  • 58
  • 75
1

The contract can not send ether unless you write a function that does it. sendTransaction is not a function of your contract that is why you get the error: "instance.sendTransaction is not a function". You either send a normal transaction as smarx wrote in the comments or If you need the ether to come from a contract that does something in your application, you can add this function:

function resendEther(address recipient) public payable{
    recipient.transfer(msg.value);
}

Then you can use the instance:

instance = web3.eth.contract(abi).at(addressA);
instance.resendEther('recipientaddress', {value: 'etheramount'});

Hope this helps.

Jaime
  • 8,340
  • 1
  • 12
  • 20