Function selector • Stylus by Example
How to compute the encoded function selector of a contract's function using the Arbitrum Stylus Rust SDK.
Function selector
When a smart contract is called, the first 4 bytes of the calldata sent as part of the request are called the "function selector", and identify which function of the smart contract to call.
You can compute a specific function selector by using the function_selector! macro.
Here's an example that computes the selector of a function named foo:
function_selector!("foo") // returns 0xc2985578Functions usually take a number of arguments that you need to pass in order for the call to be successful. For example, here's the signature of a function that takes 2 arguments, an address and a uint256:
function transfer(address recipient, uint256 amount) external returns (bool);To compute the selector for this function, pass the types of the arguments to the function_selector macro:
function_selector!("transfer", Address, U256) // returns 0xa9059cbbfunction_selector will return a byte array containing the encoded function selector.