2

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

2
  • Why is sendFunds() payable? Feb 4, 2018 at 2:42
  • Aw! May be a spill over from my trial and errors @MSwezey . Thanks for noticing. Removing it right away.
    – Karthik
    Feb 4, 2018 at 3:27

2 Answers 2

3

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.

1
  • why do i have Error: Error: the tx doesn't have the correct nonce. account has nonce of.... on truffle test
    – Karthik
    Feb 4, 2018 at 10:38
0

Missing function call at the end:

wallet.receiveFunds.value(cost)();
4
  • I have tried tis and unfortunately the results are the same
    – Karthik
    Feb 3, 2018 at 17:59
  • There must be something else. Did you check if cost > 0?
    – lunr
    Feb 3, 2018 at 18:02
  • yes I debug and I see the cost as 22000000000000000000 which is right.
    – Karthik
    Feb 3, 2018 at 18:05
  • There has be to something else with the code, I made some tests and value transfer happens successfully. I noticed there is a function named ethWallet in your code. Is it meant to be a constructor? If so, you should name it EthWallet otherwise it won't work.
    – lunr
    Feb 3, 2018 at 21:59

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.