Function selector
Whenever you call a contract method the SDK will generate a function selector according to the fuel specs which will be used by the node to identify which method we wish to execute.
If, for whatever reason, you wish to generate the function selector yourself you can do so:
// fn some_fn_name(arg1: Vec<str[3]>, arg2: u8)
let fn_name = "some_fn_name";
let inputs = [Vec::<SizedAsciiString<3>>::param_type(), u8::param_type()];
let selector = resolve_fn_selector(fn_name, &inputs);
assert_eq!(selector, [0, 0, 0, 0, 7, 161, 3, 203]);
If you don't have the ParamType
If you won't or can't run the abigen!
macro and all you have is the JSON ABI of you contract, you can still get the function
selector, but you have to jump through an extra hoop to get the ParamTypes
:
let abi: ProgramABI = serde_json::from_str(&abi_file_contents)?;
let type_lookup = abi
.types
.into_iter()
.map(|a_type| (a_type.type_id, a_type))
.collect::<HashMap<_, _>>();
let a_fun = abi
.functions
.into_iter()
.find(|fun| fun.name == "array_of_structs")
.unwrap();
let inputs = a_fun
.inputs
.into_iter()
.map(|type_appl| ParamType::try_from_type_application(&type_appl, &type_lookup))
.collect::<Result<Vec<_>>>()?;
let selector = resolve_fn_selector(&a_fun.name, &inputs);
assert_eq!(selector, [0, 0, 0, 0, 39, 152, 108, 146,]);