sway icon indicating copy to clipboard operation
sway copied to clipboard

Expose function selector from `ABI`

Open Braqzen opened this issue 2 years ago • 0 comments

Labels may need to be adjusted...

I would like to be able to fetch the encoded value for any function on the ABI / interface.

Say I have the following ABI:

abi Vault {
    pub fn withdraw(asset: AssetId, amount: u64);
}

If I want to make a low level call I need the function selector, which needs to be calculated. It would be useful if I could do

// Haven't considered the return type exactly but perhaps `Bytes` or something that can be converted to it
let selector = Vault.withdraw.function_selector;

I believe this would be equivalent to

// I think you're meant to take first 8 bytes from
let selector: b256 = sha256("withdraw(AssetId,u64)");

Braqzen avatar Dec 02 '23 15:12 Braqzen