kklas / anchor-client-gen

A tool for generating solana web3 clients from anchor IDLs.
MIT License
135 stars 21 forks source link

using Box<MyType> for instruction arguments should be handled transparently #31

Closed JoeHowarth closed 2 years ago

JoeHowarth commented 2 years ago

Problem:

fn do_something(arg: Box<MyType>) -> Result<()>  {
     ...
}

Currently the above fails with the following error:

generating programId.ts...
generating errors.ts...
generating instructions...
Defined type not found: {"defined":"Box<MyType>"}

Proposal:

Disregard the Box and just look for MyType

kklas commented 2 years ago

Hmm, is the do_something method an instruction or just a regular method? Seems like something's off in the IDL.

JoeHowarth commented 2 years ago

The do_something is an instruction.

Yeah I could definitely see the argument that idl generation should not emit the Box at all. Currently I run a regex on the idl to remove the box and that seems to work.

kklas commented 2 years ago

Right, I was thrown off by the fact that you don't have a ctx argument in the snippet you posted (if I'm not mistaken you have to have ctx as the first argument in an anchor instruction).

So yea I managed to reproduce it. This is definitely an issue with the IDL generation. For example, when you have an Box<bool> arg it will generate this: image

I don't think Box should be generated in the IDL as this is specific to rust memory management and should otherwise work transparently.

Could you open an issue about this in the anchor repo?

In the meantime your workaround seems good to me. I don't think it's worthwhile to handle this in the client generator as it will get resolved in the IDL generation.

JoeHowarth commented 2 years ago

Ahh, yeah I wrote the snippet rather than copy my actual code, should definitely have a ctx 😅

Thanks for opening the issue, I'll keep the workaround for now 👍