1

hello everyone and happy '23.

I'm having issues sorting out the following system.

Contract TipWallet: receives tips with a takeTip() and updates the tip +1 every time it receives one.

Contract Tipper: there is a tipper() function that first calls takeTip() in TipWallet, and if payment is successful, more logic is executed.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;


contract TipWallet {

    
    // the wallet sets the tip
    uint public tip; 
    uint public balance;

    constructor (uint _tip) {
        tip = _tip;
    }

    function payTip() public payable {
        uint _tip = getTip();
        require(_tip == msg.value, "no right tip");
        (bool sent, ) = address(this).call{value: _tip} ("");
        require(sent, "Failed to send ether");
        balance += msg.value;
        // Update the tip
        tip += 1;
    }

    function getTip() public view returns(uint) {
        return tip;
    }
}

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

import "./TipWallet.sol";

contract Tipper {

    TipWallet wallet;
    address[] public buyers;

    constructor(address payable _walletContract) {
        wallet = TipWallet(_walletContract);
    }

    function buy() public payable {
        // pay the tip to the wallet
        wallet.payTip();
        buyers.push(msg.sender);

    }


}

PROBLEMs I'M ENCOUNTERING

  • Contract Tipper gets the msg.value, not Tipwallet
  • OR the takeTip() reverts because there's no tip inside

any tip helps. thanks in advance

1
  • 1
    sorry, updated (I tried so many variations that I didn't know what to put as example)
    – matlemad
    Jan 14, 2023 at 10:17

1 Answer 1

0

There are two problems:

1 . contract TipWallet: the below code is not necessary at all, you can delete it. Sending value to the contract itself (address this) require the contract having receive payable or fallback payable function. This does no effect except making your contract always throwing revert

    (bool sent, ) = address(this).call{value: _tip} ("");
    require(sent, "Failed to send ether");
  1. contract Tipper: you need to send msg.value along with the function call so the wallet contract can receive it:
   // pay the tip to the wallet
   wallet.payTip{value: msg.value }();

The full fixed contracts:

pragma solidity ^0.8.4;
contract TipWallet {
    // the wallet sets the tip
    uint public tip; 
    uint public balance;

    constructor (uint _tip) {
        tip = _tip;
    }

    function payTip() public payable {
        uint _tip = getTip();
        require(_tip == msg.value, "no right tip");
        balance += msg.value;
        // Update the tip
        tip += 1;
    }

    function getTip() public view returns(uint) {
        return tip;
    }
}

contract Tipper {

    TipWallet wallet;
    address[] public buyers;

    constructor(address payable _walletContract) {
        wallet = TipWallet(_walletContract);
    }
    function buy() public payable {
        // pay the tip to the wallet
        wallet.payTip{value: msg.value}();
        buyers.push(msg.sender);

    }
}
1
  • 1
    Thank you so much! it worked. just a small add for whom might follow: in the buy() function in Tipper, make sure to: TipWallet(wallet).payTip{value: msg.value}(); enjoy the weekend :)
    – matlemad
    Jan 14, 2023 at 10:37

Your Answer

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

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