1

Why call.value()() is used in smart contracts instead of send? Can someone help me to understand importance of call.value()()?

  • Your question was already answered here : ethereum.stackexchange.com/questions/8270/… – Eli Drion Mar 25 '18 at 11:45
  • I got the first two point but third point is not yet clear. How can we call the function without knowing it's signature with the help of call()? In the example that you have mentioned, we are still taking the help of function's signature. One more thing, is it at all possible not to know the function's signature? Since the contract is deployed why the function's signature is not visible? Please help me to understand these points regarding call(). – snehal Mar 25 '18 at 20:14
  • Well, I suppose it's an edge case and rarely used, but for example, let's say a dapp gives you some data to send to their contract if you want to, let's say, register your address. They give you the data in hex, so you don't know anything about the function called. Then, if you want to whitelist a contract instead of a "normal address", it's possible by using the call function, passing the data they gave to you. – Eli Drion Mar 25 '18 at 20:25
1

a.call.value()() and a.call() should be avoided.

Solidity's author, chriseth, recommends to avoid using Solidity's call

https://github.com/ethereum/solidity/issues/2884#issuecomment-329169020

The thing is: a.call() is an ancient beast that should not be used. I would recommend using inline assembly for such tasks, since it provides the same security guarantees but does not do any invisible magic.

Furthermore:

in general: call has many weird quirks and I would much rather just remove it from the language than fix all these quirks.


Many old contracts used call.value()() primarily because it was not well understood (and led to incidents such as TheDAO), or inline assembly was not available at the time.

4

I don't have enough reputation to comment voluntarily so I will answer here. You may want to use call for several, it depends on your needs, although I don't see alot of them.

  • the function you call is payable and you want to send ETH as well.
  • you want to set the gas "given" to the function (21k gas default limit)
  • you do not know the function's signature in the called contract, so you use call with data (in bytes)

Edit : Here's an example.

contract Called{
    uint public myuint;

    function set(uint _var) {
        myuint = _var;
    }

    function get() view returns (uint){
        return myuint;
    }
}

interface Called{
    function set(uint);
    function get() view returns (uint);
}

contract Caller {

    Called public called_address;

    function set_address(address _addy) {
        called_address = Called(_addy);
    }

    function set(uint256 _var) {
        called_address.set(_var);
    } 

    function set_call(address _called, uint256 _var) {
        _called.call(bytes4(sha3("set(uint256)")), _var);
    }
}

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.