To select which function you want to call, first, this function must be in an ABI struct of a Sway program. For instance:
abi MyContract {fnfoo(a:u64);fnbar(a:InputStruct );} {fnbaz(a: ()) { }}
The function selector is the first 4 bytes of the SHA-256 hash function of the signature of the Sway function being called. Then, these 4 bytes are right-aligned to 8 bytes, left-padded with zeroes.
The signature is composed of the function name with the parenthesized list of comma-separated parameter types without spaces. All strings encoded with UTF-8. For custom types such as enum and struct, there is a prefix added to the parenthesized list (see below). Generic struct and enum types also accept a list of comma-separated type arguments in between angle brackets right after the prefix.
For instance, to compute the selector for the following function:
fnentry_one(arg:u64);
we should pass "entry_one(u64)" to the sha256() hashing algorithm. The full digest would be:
Then we would get only the first 4 bytes of this digest and left-pad it to 8 bytes:
0x000000000c36cb9c
The table below summarizes how each function argument type is encoded
Type
Encoding
bool
bool
u8
u8
u16
u16
u32
u32
u64
u64
b256
b256
struct
s<<arg1>,<arg2>,...>(<ty1>,<ty2>,...) where <ty1>, <ty2>, ... are the encoded types of the struct fields and <arg1>, <arg2>, ... are the encoded type arguments
enum
e<<arg1>>,<arg_2>,...>(<ty1>,<ty2>,...) where <ty1>, <ty2>, ... are the encoded types of the enum variants and <arg1>, <arg2>, ... are the encoded type arguments
str[<n>]
str[<n>]
array
a[<ty>;<n>] where <ty> is the encoded element type of the array and <n> is its length
tuple
(<ty1>,<ty2>,...) where <ty1>, <ty2>, ... are the encoded types of the tuple fields
Icon InfoCircle
Note: Non-generic structs and enums do not require angle brackets.