send ethers from one contract to another

  • I am wondering how can I have ethers in my contract. I have 2 contracts associated with different accounts.

    I want to send ethers from one contract to another but how do I have ethers in my contract first to be able to send it ?

    Here is the example:

    contract ethertransfer {    
      function fundtransfer(address etherreceiver, uint256 amount) {
        if(!etherreceiver.send(amount)) {
           throw;
        }
      }
    }
    
    contract etherreceiver {
    
      function etherreceiver() {      
      }
    }
    
    1. Send Ether to your contract (ethertransfer) from the external account (a term for user controlled accounts). To be able to make it, you need to have a payable function in your contract (which might be the fallback function https://solidity.readthedocs.io/en/latest/contracts.html#fallback-function). Payable function is the function with the payable modifier (e.g. function receivePayment() payable {}). Then you need to send transaction (if fallback function is defined) to the contract or call the function on your contract (if you decided to have one to receive payments). You may use web3 client to interact with your contact.

    2. It's almost the same when sending Ether from one contract to the other. The target contract need to have payable function as well (it might be again: the fallback function or any other).

      • If the fallback function is defined: use etherreceiver.transfer(100) or etherreceiver.send(100)
      • If there is custom function defined use etherreciver.myPayableFunction.value(100)(myArgument1, myArgument2)

    Further reading: I recommend to read the section regarding modifiers it will help you understand what payable is: https://solidity.readthedocs.io/en/latest/contracts.html#function-modifiers

  • Simplest approach is a payable function, then send it funds from a regular account.

    contract ethertransfer{
    
        function payMe() payable returns(bool success) {
            return true;
        }
    
        function fundtransfer(address etherreceiver, uint256 amount){
            if(!etherreceiver.send(amount)){
               throw;
            }    
        }
    }
    
    
    contract etherreceiver{       
        function etherreceiver(){          
        }    
    }
    

    Hope it helps.

  • As mentioned before, define a payable function to receive the funds, e.g.:

    function fund() payable returns(bool success) {
    }
    

    Then call this function and provide the ethers in the value field.

    After the tx succeeds define a function in which you use send(), e.g.:

    contract_destination_address.send(amount);
    
  • 1) Send Ether to your contract

     Step-1: Create a payable method in your contract just to receive some ether. 
             Better use empty payable fallback method(Can just use the contract address to send ether) 
    

    Ex:

    enter image description here

     Step-2: Send the transaction to your contract using web3js.
    

    2) Send Ether from one contract to another contract

    This can be best illustrated using an example

    **Ex:**
    

    //Contract to receive Ether from another Contract

    contract TransferEtherTo{

    function getBalance() returns(uint){
        return address(this).balance;
    }
    
    //Empty payable fallback method just to receive some
     function() payable{
    }
    

    }

    //************************************************************///

    //Contract to send Ether to another Contract

    contract TransferEtherFrom{

    //Declaring an instance of TransferEtherTo contract
    TransferEtherTo instance;
    
    constructor(){
        //Initializing TransferEtherTo contract
        instance = new TransferEtherTo();
    }
    
    //Returns balance of TransferEtherFrom contract
    function getBalance() returns(uint){
        return address(this).balance;
    }
    
    //Returns balance of TransferEtherTo contract
    function getBalanceInstance() returns(uint){
        return instance.getBalance();
    }
    //Transfers ether to other contract
    function transfer() payable{
        address(instance).send(msg.value);
    }
    
    //Fallback function to receive and transfer Ether
    function() payable{
        address(instance).send(msg.value);
    }
    

    }

    All the ethers sent to TransferEtherFrom contract(through fallback or transfer() methods) will be sent to TransferEtherTo contract.

  • For this, you have to make the function that is calling the address.transfer as payable. Plus there should be a payable function in the receiving contract too.

    contract ethertransfer {    
      function fundtransfer(address etherreceiver, uint256 amount) public payable {
        if(!etherreceiver.transfer(amount)) {
           throw;
        }
      }
    }
    
    contract etherreceiver {
    
      function etherreceiver() external payable{      
      }
    }
    

    Now, when you call the fundtransfer() function, just put in the address of the contract etherreceiver, and the number of ethers you want to transfer.

    After that, while calling the function from desired place(remix/web3js), just put in the value = no. of ethers, this will deduct the no. of ethers from the account calling the fundtransfer() and transfer the value-amount no of ethers to the etherreceiver contract.

  • the sender contract

         contract theSender {
    
         /*Harcoding receiver contract address, one can also use contructor to assign 
          address */
         address payable theReceiverContractAddress;
         theReceiverContractAddress = '0x01...'
    
         //this is an empty function to receive ether from wallet accounts using web3
         function receiverether() external payable {}
    
         function balanceof() { 
          return address(this).balance;
          }
    
         /*this will tranfer the ether stored at this contract's address to the below 
         address */
         function debit() external{ theReceiverContractAddress.transfer(value);}
    
    }
    

    Now the receiving contract

    contract theReceiver{
    
    /*this is required to receive ether from any source, this will store received ether in 
    this contract's address */ 
    
    receive() external payable{}
    
    }
    

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM