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

feat: code generate instruction account structs #16

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

sol-mocha
Copy link

@sol-mocha sol-mocha commented Feb 1, 2023

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

  • How should we handle optional accounts in both the struct and in the function to populate the struct?

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 sol-mocha changed the title chore: code generate instruction account struct feat: code generate instruction account struct Feb 1, 2023
@sol-mocha sol-mocha changed the title feat: code generate instruction account struct feat: code generate instruction account structs Feb 1, 2023
@sol-mocha sol-mocha marked this pull request as ready for review February 1, 2023 03:41
@sol-mocha
Copy link
Author

@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
Copy link
Author

sol-mocha commented Feb 2, 2023

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

main.go Outdated Show resolved Hide resolved
@gagliardetto
Copy link
Owner

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:

  • there can be optional accounts
  • there can be groups of accounts
  • there can be an arbitrary number of accounts (unnamed) at the end of the account list (I think they're the "remaining" accounts in anchor)

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

Successfully merging this pull request may close these issues.

2 participants