0

I'm going through the Numerai contracts and noticing a common pattern - https://github.com/numerai/contract/blob/master/contracts/NumeraireBackend.sol

and https://gist.github.com/Arachnid/4ca9da48d51e23e5cfe0f0e14dd6318f

function stakeOnBehalf(address _staker, uint256 _value, bytes32 _tag, uint256 _tournamentID, uint256 _roundID, uint256 _confidence) stopInEmergency onlyPayloadSize(6) returns (bool ok) {
    return delegateContract.delegatecall(bytes4(sha3("stakeOnBehalf(address,uint256,bytes32,uint256,uint256,uint256)")), _staker, _value, _tag, _tournamentID, _roundID, _confidence);
}

I see the delegatecall with the bytes4 and get the sha3 hash of a string before the variables of the function. I understand what sha3 does in this context, but my question is why?

Is it for security? What is the purpose of the string before the variables as well?

2

That string is called the function selector. It is calculated as the first four bytes of the sha3 hash of the signature of the function. The signature is defined as the canonical expression of the basic prototype, i.e. the function name with the parenthesised list of parameter types. Parameter types are split by a single comma - no spaces are used (as you can also observe in the code snippet you provided).

Function selectors are used to tell Solidity and EVM which functions you actually want to (delegate) call.

See more info here. You might also find this helpful.

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.