Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Account context codegen for TypeScript #3477

Open
0xPlish opened this issue Jan 11, 2025 · 4 comments
Open

Account context codegen for TypeScript #3477

0xPlish opened this issue Jan 11, 2025 · 4 comments

Comments

@0xPlish
Copy link

0xPlish commented Jan 11, 2025

Currently it seems like only the Rust side of Anchor has all of the instruction account contexts generated as structs. It would be nice if the Typescript side had the same. We are finding it cumbersome to constantly update fields in client-side types when contracts change by one or two accounts.

@acheroncrypto
Copy link
Collaborator

We are finding it cumbersome to constantly update fields in client-side types when contracts change by one or two accounts.

All client-side types should get updated automatically once you update your IDL. Could you share a snippet regarding where you need to manually update?

@0xPlish
Copy link
Author

0xPlish commented Jan 17, 2025

@acheroncrypto thanks for your response.

Where are the client side types supposed to be located? From what I can tell, the expectation on the .someInstruction().accounts(...) function is an interface provided by the IDL, but there is no concrete type available to the client.

Contrast this in Rust, where there is the cpi::accounts module with all the account context structs generated.

Maybe I am missing something.

@acheroncrypto
Copy link
Collaborator

We do not generate hardcoded types for TypeScript (all types are inferred from the generated IDL type). This is different for Rust because there is no way to do what's possible on TypeScript (inferring types) in Rust.

We do have utility types to get certain types, e.g. accounts, but we don't have one specific for instruction accounts. We could add something like:

type InstructionAccounts<
  I extends anchor.Idl,
  N extends I["instructions"][number]["name"],
  Ixs extends I["instructions"] = I["instructions"]
> = Ixs extends [infer Head, ...infer Tail]
  ? Head extends { name: N }
    ? anchor.Accounts<Head>[N]
    : Tail extends Ixs
    ? InstructionAccounts<I, N, Tail>
    : never
  : never;

And you'd use it as:

type MyIxAccounts = InstructionAccounts<MyIdl, "myIx">;

Would this be useful to you? I know it's not exactly what you want, but pretty much anything you need can be derived from the generated IDL type without having to hardcode them.

@0xPlish
Copy link
Author

0xPlish commented Jan 19, 2025

Oh this is very powerful, will try it out when availabile @acheroncrypto thank you. Despite Rust having the more sound type system, TypeScript does simulate some very cool typelevel calculations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants