FuelLabs / fuels-ts

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

Generate interfaces in `typegen` and read them via generic type parameters #2941

Open nedsalk opened 2 months ago

nedsalk commented 2 months ago

Have typegen generate a type representation of the abi and then read it via type parameters on the appropriate classes. A typegen contract output would look like this:

// CounterContract.ts
export interface CounterAbi {
  functions: {
    get_count: {
      inputs: [];
      output: BN;
    };
    increment_count: {
      inputs: [amount: BigNumberish];
      output: BN;
    };
  };
  configurableConstants: { };
}
export class CounterInterface extends Interface<CounterAbi> { }

export class CounterContract extends Contract<CounterAbi> { ... }

// CounterContractFactory.ts
import type { CounterAbi } from './CounterContract'
export class BytesContractFactory extends ContractFactory<CounterAbi> { ... }

The same would apply for scripts and predicates.

Note that to 100% test these type generics we'd have to introduce type-level testing to verify that e.g. the functions on Interface<MyAbi> correspond to the passed-in MyAbi's functions, their inputs and outputs. Alternatively, we could do type checking in our test suites and have the type checking succeeding be a proxy for verifying that the implementation is correct.

petertonysmith94 commented 1 month ago

This will be done once the coder refactor is finished.
TS-599 TS-600