Skip to main content
2 of 3
changing one data
Gopal ojha
  • 2.2k
  • 2
  • 12
  • 21

You have to receive ether in contract while sending ether from web3js. You must have payable function to receive an ether. Lets see example below :

In Web3js :

your_contractInstance.yourReceiverFunction(     
             your_input_data, // this is optional for sending ether. You can send ether without passing this input variable. 
            {  
            from:   web3.eth.defaultAccount,  
            to:     your_contract_address,  
            value:  web3.toWei(1, "ether")              
    });    

In solidity :

 function yourReceiverFunction(datatypes your_input_data)  
       payable   
       public {  
    //do anything here with your input data 
    // to receive ether it is not necessary to be any input variable , you    just
    //need to send ether from web3 and receive here. 
    //the key point here is to just receive ether and use it   
    //msg.value holds the ether that sends from web3 in wei.
        
  uint myethervalue = msg.value;      
    } 

Take a refrence of this example.

Gopal ojha
  • 2.2k
  • 2
  • 12
  • 21