[Ethereum] Transfer ether from one payable contract to another not working

etherremixsolidity

Scenario:

I have users who will initiate booking for one product with x price. This contract will create a wallet contract between that user and the product so that he can return/cancel the booking at any time and get the refund or succeed.

Code:

User talks via contract EthBooking

contract EthBooking {
    function addBooking(uint listingId) public payable returns(uint bookingId) {
    bookingId =  BookingLibrary.book(ethDB, listingId, msg.sender, msg.value);

    //Send money to the contract
    return bookingId;
}
}

Booking Library code:

library BookingLibrary {

function book(address db, uint listingId, address sender, uint value) 
        internal returns (uint bookingId) 
{
    var cost = ListingLibrary.getCost(db,listingId);
        //Create Contract wallet
        EthWallet wallet = new EthWallet();
        //Send money to the contract
        wallet.receiveFunds.value(cost);
    return bookingId;
}

EthWallet Code:

contract EthWallet is Mortal {

function ethWallet() {
}

function receiveFunds() payable {

}

function sendFunds(address receiver, uint amount) {
    receiver.transfer(amount);
}

}

PROBLEM

The ethers sent is held in contract EthBooking and is not transferred to EthWallet as expected with code wallet.receiveFunds.value(cost); .

Please guide me about if the chosen approach is right and also why the ethers are not transferred to the destination.
PS: The code is shortened with removal of irrelevant parts to avoid confusion

Best Answer

I mocked this up in Truffle.

Using your way:

wallet.receiveFunds.value(cost);

Resulted in failure:

[ Instantiate Contract ]
    √ should instantiate the pact correctly
Wallet address: 0xa830ec357b950fde26d220b0f256b6d47529bdbd
Amount sent in wei: 3000000000000000000
Balance in eth wallet: 0
    (failed) 1) should transfer funds using library

Using this method:

wallet.receiveFunds.value(cost)();

Resulted in success:

[ Instantiate Contract ]
    √ should instantiate the pact correctly
Wallet address: 0xcc9fa65469197707e06eb16c2b01b9a62ddea75f
Amount sent in wei: 3000000000000000000
Balance in eth wallet: 3000000000000000000
    √ should transfer funds using library (410ms)

Others that worked:

wallet.send(cost);  // made a fallback function in EthWallet for this
wallet.transfer(cost);    // same as above, fallback function
address(wallet).call.value(cost)();  // fallback again

See Sending and Receiving Ether @ http://solidity.readthedocs.io/en/develop/security-considerations.html?highlight=send%20transfer

Other Notes: Your constructor should be exactly the same. Not ethWallet but EthWallet.