3

-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; 
}

}`
2
  • Not sure exactly what's happening here as we can only see bits of your code but one thing that jumps out: In sendPayment you're throwing an error if the payment to the beneficiary succeeds. Did you mean to throw an error if it fails? Mar 2, 2017 at 14:39
  • yes you are right, but the actual issue is I am getting the ether's from sender in my contract, but not able to send it to the receiver Mar 2, 2017 at 20:33

1 Answer 1

3

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.

6
  • no worries. You can upvote or marked solved if the answer was helpful. Mar 3, 2017 at 3:25
  • Still facing the same issue ether not get transferd to another account, can you show any simple example of contract which shows how to transfer ether from one account to another Mar 3, 2017 at 9:21
  • 2
    What you ask is not possible. Wrong approach. Not possible to write a contract to transfer from Alice to Bob ... Alice -> Bob doesn't work. The function above let's Alice send funds to the contract. The contract for forward its own funds to Bob. Alice -> Contract -> Bob. This works. Mar 3, 2017 at 10:33
  • can u tell me how to add ether in contract, I have tried: truffle(development)> web3.eth.sendTransaction({from:web3.eth.coinbase, to:contractAddress, value: web3.toWei(0.05, "ether")}) but it dosen't work Mar 3, 2017 at 17:58
  • 1
    That's by design. Possible we're still not clear about what you want to accomplish. Above function, Alice sends contract 1 Ether. Now contract has 1 Ether. Contract forwards 1 Ether to Bob. Now contract has 0 Ether. Alice has one less, and Bob has one more than initial conditions. Mar 4, 2017 at 5:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.