toFunctionSelector
Returns the 4-byte function selector for a given function definition
Loading...
Returns the 4-byte function selector for a given function definition
Returns the 4-byte function selector for a given function definition. The selector is the first 4 bytes of the keccak256 hash of the function signature.
import "github.com/ChefBingbong/viem-go/utils/hash"import "github.com/ChefBingbong/viem-go/utils/hash"
// Get function selectorselector, _ := hash.ToFunctionSelector("function ownerOf(uint256 tokenId)")// "0x6352211e"
selector, _ = hash.ToFunctionSelector("function transfer(address to, uint256 amount)")// "0xa9059cbb"
// Get selector as bytesselectorBytes, _ := hash.ToFunctionSelectorBytes("function transfer(address,uint256)")// []byte{0xa9, 0x05, 0x9c, 0xbb}(string, error) for ToFunctionSelector, ([]byte, error) for ToFunctionSelectorBytesThe 4-byte function selector. ToFunctionSelector returns a hex string with 0x prefix, ToFunctionSelectorBytes returns raw bytes.
stringThe function definition string. Can include the function keyword or just the signature.
// With function keywordselector, _ := hash.ToFunctionSelector("function ownerOf(uint256 tokenId)")// "0x6352211e"
// Without function keywordselector, _ = hash.ToFunctionSelector("transfer(address,uint256)")// "0xa9059cbb"Returns the selector as a hex string:
selector, _ := hash.ToFunctionSelector("function transfer(address,uint256)")
// "0xa9059cbb"
Returns the selector as raw 4 bytes:
selectorBytes, _ := hash.ToFunctionSelectorBytes("function transfer(address,uint256)")
// []byte{0xa9, 0x05, 0x9c, 0xbb}