gagliardetto / anchor-go

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

feat: code generate instruction account structs #16

Open sol-mocha opened 1 year ago

sol-mocha commented 1 year ago

Description

This PR updates the code gen in main.go to create structs for instruction accounts, as well as a helper to populate said structs given the AccountMetaSlice in the instruction builder. This will allow devs to safely access instruction accounts in a readable way without having to write their own converters and types.

Open Questions

Example

idl/examples/composite.json now produces the following extra code

[
        {
          "name": "foo",
          "accounts": [
            {
              "name": "dummyA",
              "isMut": true,
              "isSigner": false
            }
          ]
        },
        {
          "name": "bar",
          "accounts": [
            {
              "name": "dummyB",
              "isMut": true,
              "isSigner": false
            }
          ]
        }
]
type CompositeUpdateAccounts struct {
    Foo struct {
        DummyA ag_solanago.PublicKey
    }
    Bar struct {
        DummyB ag_solanago.PublicKey
    }
}

func (inst *CompositeUpdate) GetCompositeUpdateAccounts() *CompositeUpdateAccounts {
    res := &CompositeUpdateAccounts{}
    res.Foo.DummyA = inst.AccountMetaSlice[0].PublicKey
    res.Bar.DummyB = inst.AccountMetaSlice[1].PublicKey
    return res
}
sol-mocha commented 1 year ago

@gagliardetto wondering if you can give some insight into the open question I have in the PR description. Do you anticipate any issues with the new code in relation to optional accounts?

sol-mocha commented 1 year ago

Here is a real life example using these changes. Its applied on the drip program idl.

gagliardetto commented 1 year ago

Hey @dcaf-mocha

Sorry for the delay in getting back to you.

Thanks for the PR! That's a great idea.

From what I remember: