[Ethereum] Ether transfer to another account

contract-developmentethergo-ethereumsoliditytruffle

-I am trying to transfer ether from one account to another using smart contract which is deployed on azure.

-when I call receiverAddress.send(amount)
in the function in my contract it returns the transaction receipt/ acknowledgement in json format.

-My account get debited by amount I specified but the receivers account is not credited.
the receipt shows from :myAddress ie. sender address
and to:contract address

-I don't understand why the to field has a contract address when I am calling receiverAddress.send(amount)

-Is there any thing I am missing or I need to transfer that ether from contact to the receivers account??

-Bellow is the snippet of transaction receipt:-Transaction Receipt

-function from contract

function sendPayment(address beneficiary,uint amount){
     coin-=amount;
     if (beneficiary.send(amount))
     throw;
}

this is code of contract:

`pragma solidity ^0.4.4;

import "./strings.sol";

contract SDFFinance {
    using strings for *;
    address addr;
    address public driver1;
    address public driver2;
    address public driver3;
    string statusOf;
    uint coin;

function SDFFinance(uint balance) {
    coin=balance;
    driver1=0x1b7207197717fe2114d3d22f38c81d980e74e13a;
    driver2=0x1b7207197717fe2114d3d22f38c81d980e74e13a;
    driver3=0x1b7207197717fe2114d3d22f38c81d980e74e13a;
    statusOf='OPN';
  }  

function changeShipmentStatus(string shipmentId,string st){
            statusOf=st;
        processPayment();
}

function processPayment(){
    if(statusOf.toSlice().equals('RAP'.toSlice())){
        sendPayment(driver1);
    }
    else if(statusOf.toSlice().equals('DAL'.toSlice())){
        sendPayment(driver2);
    }

    else
    if(statusOf.toSlice().equals('ATD'.toSlice())){
        sendPayment(driver3);
    }
}



function sendPayment(address beneficiary) payable returns(bool success) {
  if(msg.value==0) throw;
  if(!beneficiary.send(msg.value)) throw;
  return true;
}

function getBalance() returns(uint){
    return coin; 
}

function getStatus() returns(string){
    return statusOf; 
}

function getAddress() returns(address){
    return addr; 
}

}`

Best Answer

It sounds like a misunderstanding.

It's not possible to write a contract that will reach out and spend the sender's funds. You get a transaction hash before the network processes it, and very possibly things are going wrong.

Let's consider three parties:

  • Sender
  • Beneficiary
  • Contract

The sender can send funds to the contract. The contract can forward received funds (or spend its own) to the beneficiary.

It would be fairly common to write a function that receives funds from the sender and sends them on to the beneficiary. In that case, the function must be tagged with the keyword payable, otherwise, it will throw and revert when Ether is attached.

I'm not certain what coin -= amount does.

Here's approximately what it would look like to forward received Ether.

function sendPayment(address beneficiary) payable returns(bool success) {
  if(msg.value==0) throw;
  if(!beneficiary.send(msg.value)) throw;
  return true;
}

To forward funds with that function, send it with (roughly) contractInstance.sendPayment(beneficiary, {from: sender, to: contract, value: amount}).then(function(txnHash) ...

Notice I removed amount from the function. I think the idea is it's implicitly the amount of Ether sent.

Also worth noting. It's important to wait for the transaction to be mined to see the final result.

Hope it helps.

Related Topic