Skip to content

feat: code generate instruction account structs#16

Open
sol-mocha wants to merge 8 commits intogagliardetto:mainfrom
sol-mocha:mohca/codegen-accounts
Open

feat: code generate instruction account structs#16
sol-mocha wants to merge 8 commits intogagliardetto:mainfrom
sol-mocha:mohca/codegen-accounts

Conversation

@sol-mocha
Copy link
Copy Markdown

@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
Copy Markdown
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 sol-mocha force-pushed the mohca/codegen-accounts branch from 219ba33 to 4454062 Compare February 1, 2023 03:44
@sol-mocha
Copy link
Copy Markdown
Author

sol-mocha commented Feb 2, 2023

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

@gagliardetto
Copy link
Copy Markdown
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