toEventSelector
Returns the event selector (topic0) for a given event definition
Loading...
Returns the event selector (topic0) for a given event definition
Returns the event selector (topic0) for a given event definition. The selector is the full keccak256 hash of the event signature.
import "github.com/ChefBingbong/viem-go/utils/hash"import "github.com/ChefBingbong/viem-go/utils/hash"
// Get event selectorselector, _ := hash.ToEventSelector("event Transfer(address indexed from, address indexed to, uint256 amount)")// "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
selector, _ = hash.ToEventSelector("Transfer(address,address,uint256)")// "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
// Get selector as bytesselectorBytes, _ := hash.ToEventSelectorBytes("event Transfer(address,address,uint256)")// []byte{0xdd, 0xf2, 0x52, ...}(string, error) for ToEventSelector, ([]byte, error) for ToEventSelectorBytesThe event selector (32-byte hash). ToEventSelector returns a hex string with 0x prefix, ToEventSelectorBytes returns raw bytes.
stringThe event definition string. Can include the event keyword and indexed keywords, or just the signature.
// With event keyword and indexedselector, _ := hash.ToEventSelector("event Transfer(address indexed from, address indexed to, uint256 amount)")// "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
// Without event keywordselector, _ = hash.ToEventSelector("Transfer(address,address,uint256)")// "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"Returns the selector as a hex string:
selector, _ := hash.ToEventSelector("event Transfer(address,address,uint256)")
// "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
Returns the selector as raw 32 bytes:
selectorBytes, _ := hash.ToEventSelectorBytes("event Transfer(address,address,uint256)")
// []byte{0xdd, 0xf2, 0x52, ...}
indexed keywords removed) before hashing