FuelLabs / fuels-ts

Fuel Network Typescript SDK
https://docs.fuel.network/docs/fuels-ts/
Apache License 2.0
44.22k stars 1.34k forks source link

Make `Option<x>` optional with typegen #2788

Closed nedsalk closed 2 months ago

nedsalk commented 2 months ago

For a simple sway program:

contract;

abi MyProgram {
    fn optional_input(input: Option<u8>) -> u8;
}

impl MyProgram for Contract {
    fn optional_input(input: Option<u8>) -> u8 {
        1
    }
}

Typegen generates the function type

optional_input: InvokeFunction<[input: Option<BigNumberish>], number>;

which mandates providing an input even though it's optional in sway. The generated type should be

optional_input: InvokeFunction<[input?: Option<BigNumberish>], number>;

Note that this might become tricky once multiple inputs are introduced where some are optional and some are not, akin to what is mentioned in #2148.

mvares commented 2 months ago

@nedsalk, in more complex sway programs using Option<x>, does this still happen?

petertonysmith94 commented 2 months ago

@mvares the current behaviour is outlined here

Typegen generates the function type

optional_input: InvokeFunction<[input: Option<BigNumberish>], number>;

Please note, I've started the work for this already :)