-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Add token_program
constraint to token, mint, and associated token accounts
#2460
Add token_program
constraint to token, mint, and associated token accounts
#2460
Conversation
@elliotkennedy is attempting to deploy a commit to the coral-xyz Team on Vercel. A member of the Team first needs to authorize it. |
41c184c
to
3a731e9
Compare
…ounts in order to customise the token program interface which the account refers to
3a731e9
to
8c3a0c2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking great, please see the comments.
- Add token program constraint for initialized mints.
e7a54fe
to
1a89f5a
Compare
Thanks so much for your review @acheroncrypto - on addressing the comments I realised that the initialized |
- Fix ATA `token_program` constraint parsing - Add `token_program` constraint test cases
31fd002
to
b8faeca
Compare
Hey @acheroncrypto, I added a lot of test cases in the last commit - they are summarised in the table below:
I fixed a couple of bugs in the process, mostly around missing I did not fix #2447 and there are no test cases around ATA initialization with I think this is now good to ship - please take another look when you can - thanks! |
As you've stated, there are some cases that wouldn't work with just an owner constraint but I have to say this does feel a bit of an anti-pattern that we are specifying the program when using
Not specifically speaking of this PR but there is quite a bit of duplicate code on these checks which would make us much more likely to miss some of the implementations.
Thank you for fixing those and also for writing tests that cover many cases. |
I take your point ;) - however I think of this as more of a reference from an My opinion is that in general there should be a preference for inferring the interface implementation but any time you want to mix implementations of the same interface you will need some some sort of type reference. Particularly with Solana requiring program state up-front. I think there is a wider question about how interfaces should work in Solana and that probably needs to be addressed in the runtime. I believe it is on the roadmap for v2 but I don't have any idea how it should look. However it is a distinct problem to the current anchor notion of interfaces. Another thought is perhaps an Another approach would be to inverse the reference/constraint - I think removing the There's another question about new token 2022 features - how should they be implemented? Do we continue adding constraints to initialise Mints with new flags?
I agree :) I think there is definitely scope for reuse around
Pleasure + very, very relieved to look in the misc package :) |
I think we agree on this part but the implementation here is only focused on the token program and any other interface would need to be implemented seperately, which again is going to add lots of duplication. I'm thinking out loud and don't think this is a problem at the moment since token program interface is going to be by far the most important one for people.
Good question, I think adding new flags for the new token 2022 features would be what people would expect from Anchor since it's the way Anchor does things but I'm open to new ideas on this subject.
Yes, definitely! I think this looks good to merge and clean up can be made in a seperate PR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks again for such a great PR! It's well written, no breaking changes, fixes bugs and adds many test cases. 👏
Thanks for your review & kind words @acheroncrypto ! |
I'm trying to use this in my project but I get a I create it like so:
And removing it makes my instruction work. I suppose I need to sign the IX using the token account keys but I don't have them since I want an ATA PDA. Is this expected behavior? Is there a way around it? |
This is expected. You need to use the Try - #[account(
init,
payer = payer,
token::mint = mint,
token::authority = creator,
token::token_program = token_program
)]
pub token_account: InterfaceAccount<'info, AssociatedToken>, Edit: this bug might also be a blocker - #2541 |
Thanks for your answer! Using
And another saying |
Apologies I misremembered the syntax there - Try this: #[account(
init,
payer = payer,
associated_token::mint = mint,
associated_token::authority = creator,
associated_token::token_program = token_program
)]
pub token_account: InterfaceAccount<'info, TokenAccount>,
pub associated_token_program: Program<'info, AssociatedToken>, |
It worked! I was missing the associated program but the error got me confused.. Thanks a lot, I will point to this thread on StackExchange |
Overview
It is currently only possible to cpi a single
token_program
.As discussed in #2363 & #2386 - it should be possible to override the required
token_program
account field when intializing aMint
,TokenAccount
orAssociatedTokenAccount
.These new constraints allow this, and also act as
owner
constraints when not used with aninit
.This is useful when an instruction needs to support some combination of spl-token and spl-token-2022 mints.
API changes
token_program
attribute toTokenAccount
,Mint
andAssociatedTokenAccount
types - this allows you to specify the spl-token or spl-token-2022token_program
field which should be used in constraints and CPI calls - rather than enforcing a single account field calledtoken_program
.Example skeleton endpoint supporting both spl-token and spl-token-2022 mints -
Discussion
token_program
field; a new, specialized, attribute seems correct for this.Breaking changes
None, where the attribute is not used, the existing
token_program
account field will be used.