I have coded the below code it executes well but the ether cannot be send only the gas fees are sent?

pragma solidity ^0.5.2;

contract sendEther{

    address payable rec= address(0xD694fab8b63BeF0149aa47b8333dFeAe7f55E9C7);
    address public reciever;

    function send() public payable{


  • You don't need a contract to send Ether. Your used client will have ready functionality for it. – Lauri Peltonen Dec 28 '18 at 11:39
  • please mention which IDE you are using for a run this contract? – Mahesh Rajput Dec 28 '18 at 12:04
  • I used the remix solidity compiler online – stellar gold Dec 28 '18 at 12:37
  • so we can't send ether from one address to another using contract? – stellar gold Dec 28 '18 at 12:38
  • I have written a smart contract which sends tokens automatically when ether is sent to it. Now i need to develop a Dapp for purchase of tokens that's why i am looking to send ether to that contract – stellar gold Dec 28 '18 at 12:42

Okay big edit now you've clarified:

In order to send given amounts to given addresses successfully via a smart-contract, you can have a Sender.sol that looks like the following:

pragma solidity 0.5.0;

contract Sender {

    uint public amount = 1 ether;

    function send(address payable _addr) payable public {
        require(msg.value >= amount);

The function requires that you pass in the address of the destination of the funds, and has a require statement that forces the function caller to provide an amount of ether greater than or equal to amount.

In your contract, you can make a setter for this function to control it yourself. If your receiver is another ethereum address, nothing more is required, just call the send function, sending along ether and the receiver will receive it.

If your receiving address is a contract however, it'll need a payable fallback function like so:

pragma solidity 0.5.0;

contract Receiver {
    function() payable external {}



Testing it with Truffle!

Initialise a bare truffle project in a new repo via npx truffle init. Then create two files in the contracts folder called sender.sol & receiver.sol respectively. Paste the correct contract from above into each of those files.

Next, delete everything in the truffle-config.js file and replace it with this:

module.exports = {
  networks: {
    develop: {
      host: "",
      port: 8545,
      network_id: "*",
      websockets: true
  compilers: {
    solc: {
      version: "0.5.0",
        settings: {
          optimizer: {
          enabled: true

Now add a file called 2_deploy_contracts.js to the migrations folder and in it paste this:

const contracts = [

module.exports = deployer =>
  contracts.map(contract => deployer.deploy(artifacts.require(contract)))

Finally, create a file in the test directory called tests.js, and in that paste this:

const senderContract = artifacts.require('Sender.sol')
const receiverContract = artifacts.require('Receiver.sol')

contract(`Test`, accounts => {

  let amount
  const acc1 = accounts[0]
  const acc2 = accounts[1]

  it('Should get the amount required for sending', async () => {
    const { contract: { methods }}= await senderContract.deployed()
    amount = await methods.amount().call()

  it(`Should send the amount to a the receiver contract`, async () => {
    const { contract: { methods }} = await senderContract.deployed()
    const { address } = await receiverContract.deployed()
    const recBalanceBefore = await web3.eth.getBalance(address)
    await methods.send(address).send({from: acc1, value: amount})
    const recBalanceAfter = await web3.eth.getBalance(address)
    assert.isTrue(parseInt(recBalanceAfter) - parseInt(recBalanceBefore) == amount)

  it(`Should send the amount to another address`, async () => {
    const { contract: { methods }} = await senderContract.deployed()
    const acc2BalanceBefore = await web3.eth.getBalance(acc2)
    await methods.send(acc2).send({from: acc1, value: amount})
    const acc2BalanceAfter = await web3.eth.getBalance(acc2)
    assert.isTrue(parseInt(acc2BalanceAfter) - parseInt(acc2BalanceBefore) == amount)

  it(`Should fail to send the amount if < required amount`, async () => {
    const { contract: { methods }} = await senderContract.deployed()
    const { address } = await receiverContract.deployed()
    try {
      await methods.send(address).send({from: acc1, value: amount - 1})
      assert.fail('Should have failed!')
    } catch (_) {}

Now go back to a shell in the root directory where you ran truffle init from, and run npx truffle test. Watch the tests pass.

If you read the test headings (inside the it blocks), you can see that we have successfully tested:

  • Sending ether via your contract to another ethereum account.
  • Sending ether via your contract to another smart-contract.
  • Having the send fail if the caller does not provide the specified amount of ether.

Happy coding!

  • I need to send a specific amount and still i can't send it and i need to pass that amount as a argument – stellar gold Dec 28 '18 at 13:08
  • For example : I need to get a value from the user and pass it to the function and the specific value have to be sent from his wallet – stellar gold Dec 28 '18 at 13:10
  • Have edited to show you how to create and tests a smart-contract that fits your use-case. Enjoy! Please don't forget to accept it as an answer if it solves your question! – gskapka Dec 28 '18 at 14:12
  • Great writeup! Thanks! – angryip Apr 19 '19 at 1:46
  • I think there is a type here _addr.transfer(msg.value); , it should be (amount) ?? – Ender Mar 6 '20 at 5:20

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.