4

I need to transfer ether from one account to another using solidity contract. I am new to ethereum and solidity , so please help me.

| improve this question | | | | |
4

It is possible to send Ether (in units of wei) to an address using the transfer function of the address type of object. Using the balance property one can query the current balance of the address.

Example
address x = 0x123;
address myAddress = this;
if (x.balance < 10 && myAddress.balance >= 10) x.transfer(10);

If x is a contract address, its code (more specifically: its fallback function, if present) will be executed together with the transfer call (this is a limitation of the EVM and cannot be prevented). If that execution runs out of gas or fails in any way, the Ether transfer will be reverted and the current contract will stop with an exception.

Refer the address type features
http://solidity.readthedocs.io/en/latest/types.html?highlight=address

| improve this answer | | | | |
6

From the Solidity documentation, a very basic sending contract would look something like this:

pragma solidity ^0.4.2;

contract Transfer {


    // The keyword "public" makes those variables
    // readable from outside.
    address public recipient;
    mapping (address => uint) public balances;

    // Events allow light clients to react on
    // changes efficiently.
    event Sent(address from, address to, uint amount );


    function send(address receiver, uint amount) {
        if (balances[msg.sender] < amount) return;
        balances[msg.sender] -= amount;
        balances[receiver] += amount;
        return Sent(msg.sender, receiver, amount);
    }

}

Before putting your contract on the Blockchain, I recommend you to test your contract with TestRPC to simulate on your computer a blockchain.

When learning solidity, you should use Browser Solidity to test your contracts.

And there are also Truffle and embark which are environments to help you developp more easily your smart contracts and interact with them through the Web3.js API.

| improve this answer | | | | |
  • Is the recipient variable used in this example? And how is balances populated at the start, so that the contract would know how much ETH the msg.sender has? I'm new to Solidity as well so apologies if this is obvious. :) – Stan James Oct 10 '17 at 22:10

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.