gagliardetto / anchor-go

Generate Go clients from anchor IDLs for Solana blockchain programs
MIT License
153 stars 40 forks source link

Code Generation Produced Conflicts between Account and Instruction Struct #17

Open sol-mocha opened 1 year ago

sol-mocha commented 1 year ago

Currently, if the IDL lists the same name for an account and an instruction, this will produce go code in which the account name and the instruction name will be the same. For example the IDl for the token-swap program here will yield

type Swap struct {
    Version        uint8
    IsInitialized  bool
    BumpSeed       uint8
    TokenProgramId ag_solanago.PublicKey
    TokenA         ag_solanago.PublicKey
    TokenB         ag_solanago.PublicKey
    PoolMint       ag_solanago.PublicKey
    TokenAMint     ag_solanago.PublicKey
    TokenBMint     ag_solanago.PublicKey
    PoolFeeAccount ag_solanago.PublicKey
    Fees           Fees
    SwapCurve      SwapCurve
}

and

// Swap is the `swap` instruction.
type Swap struct {
    AmountIn         *uint64
    MinimumAmountOut *uint64

    // [0] = [] swap
    //
    // [1] = [] authority
    //
    // [2] = [SIGNER] userTransferAuthority
    //
    // [3] = [WRITE] source
    //
    // [4] = [WRITE] swapSource
    //
    // [5] = [WRITE] swapDestination
    //
    // [6] = [WRITE] destination
    //
    // [7] = [WRITE] poolMint
    //
    // [8] = [WRITE] poolFee
    //
    // [9] = [] tokenProgram
    ag_solanago.AccountMetaSlice `bin:"-"`
}

One Possible fix can be to introduce a flag to add a custom suffix to the account struct (or the instruction struct).

In this example with a custom account suffix of Account we would get

type SwapAccount struct {
    Version        uint8
    IsInitialized  bool
    BumpSeed       uint8
    TokenProgramId ag_solanago.PublicKey
    TokenA         ag_solanago.PublicKey
    TokenB         ag_solanago.PublicKey
    PoolMint       ag_solanago.PublicKey
    TokenAMint     ag_solanago.PublicKey
    TokenBMint     ag_solanago.PublicKey
    PoolFeeAccount ag_solanago.PublicKey
    Fees           Fees
    SwapCurve      SwapCurve
}

// Swap is the `swap` instruction.
type Swap struct {
    AmountIn         *uint64
    MinimumAmountOut *uint64

    // [0] = [] swap
    //
    // [1] = [] authority
    //
    // [2] = [SIGNER] userTransferAuthority
    //
    // [3] = [WRITE] source
    //
    // [4] = [WRITE] swapSource
    //
    // [5] = [WRITE] swapDestination
    //
    // [6] = [WRITE] destination
    //
    // [7] = [WRITE] poolMint
    //
    // [8] = [WRITE] poolFee
    //
    // [9] = [] tokenProgram
    ag_solanago.AccountMetaSlice `bin:"-"`
}
gagliardetto commented 1 year ago

Yep, what you're describing is totally a bug.

The whole anchor-go package needs to be refactored from top to bottom :sweat_smile:

I'll get to it (eventually).

And one of the things that would need to be done is to split the various generated stuff into separate folders (but even before that, there's a lot more).

sol-mocha commented 1 year ago

Yep, what you're describing is totally a bug.

The whole anchor-go package needs to be refactored from top to bottom :sweat_smile:

I'll get to it (eventually).

And one of the things that would need to be done is to split the various generated stuff into separate folders (but even before that, there's a lot more).

You might like what we're building for grizzlython 👀 dcaf labs is working on some code gen tooling, forked/refactored from anchor-go