12

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() {      
  }
}
9
  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

7

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.

3

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);
2

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.

0

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.

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.