0
pragma solidity 0.5.9;
contract Testing{

function participate()public  payable{ 
        uint winner = 9;
        require(msg.value == 0.1 ether);



        if ( winner==9)
        {

           require(msg.sender.call.value(this.balance)());

        }
    }
}

I am getting the following error message:

solc testing8_2.sol testing8_2.sol:13:42: Error: Member "balance" not found or not visible after argument-dependent lookup in contract Testing. Use "address(this).balance" to access this address member. require(msg.sender.call.value(this.balance)());

But if I try:

if ( winner==9)
        {

           require(msg.sender.call.value(address(this).balance)());

        }

I get the following error message:

solc testing8_2.sol testing8_2.sol:13:20: Error: Wrong argument count for function call: 0 arguments given but expected 1. This function requires a single bytes argument. Use "" as argument to provide empty calldata. require(msg.sender.call.value(address(this).balance)()); ^--------------------------------------------^ testing8_2.sol:13:12: Error: No matching declaration found after argument-dependent lookup. require(msg.sender.call.value(address(this).balance)()); ^-----^

Somebody please guide me how to solve this problem:

Zulfi.

1

You are simply missing the bytes parameter for call:

pragma solidity 0.5.9;

contract Testing {

    function participate() public payable{ 
        uint winner = 9;
        require(msg.value == 0.1 ether);

        if (winner == 9)
        {
           (bool success, ) = msg.sender.call.value(address(this).balance)("");
            require(success, "Transfer failed.");

        }
    }
}

What is the purpose of the bytes argument?

Since you only want to send ether to the address, but not call a function at that address, the bytes argument is just an empty "". Let's assume you would want to instead call a deposit() function at the msg.sender address. You would then write:

function makeDeposit(address bankAddress) public payable {
        bytes32 functionHash = keccak256("deposit()");      
        bytes4 function4bytes = bytes4(functionHash);
        bytes payload = abi.encode(function4bytes);

        if (msg.value > 0) {
            (bool success,) = bankAddress.call.value(msg.value)(payload);
            require(success, "Ether transfer failed.");
        }      
}

Example taken from this great tutorial on how to use call.value.

5
  • 1
    And of course, since this specific function doesn't take any input arguments, you could simply save that payload as a constant in the contract, in order to avoid calculating it every time the function is called: bytes private constant payload = abi.encode(bytes4(keccak256("deposit()")));. – goodvibration Apr 18 '20 at 6:22
  • You mean we can do the samething as: bankAddres.deposit.value(msg.value)(); The reason for converting the name into an id and then converting the id into bytes is that call only accepts bytes as @soliditydeveloper.com mentioned. Is using the name deposit not better for readability instead of assigning an id to the name? – zak100 Apr 19 '20 at 3:31
  • 1
    @zak100 I don't think bankAddress.deposit.value is a supported way of writing it. The comment from @goodvibration simply states the correct fact you could save some gas by doing the computation outside the function as a one-time constant variable. That is because it never changes, actually even if deposit would have input arguments, you could still do it outside. If would then simply be keccak256("deposit(address,uint256)"). It would be nice to have Solidity features on top to make this more readable. – Markus - soliditydeveloper.com Apr 19 '20 at 6:26
  • 1
    It seems there is a minor short-cut like abi.encodeWithSelector(this.deposit.selector), but I haven't tested it. – Markus - soliditydeveloper.com Apr 20 '20 at 3:31
  • @soliditydeveloper.com, I think use of deposit as function name is correct, please see the link: ethereum.stackexchange.com/questions/72072/…. – zak100 Apr 21 '20 at 0:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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