-
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 seeds::program constraint for PDAs. #1197
Changes from 6 commits
72a3533
d0e9705
619efcd
b951979
3a6acef
676b934
7acab82
746733c
b00c815
867f156
4fb094b
4379b7a
5abfcea
f11b787
8988f48
279a4a1
872bad2
0ee3e57
0b2a583
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -182,6 +182,15 @@ pub fn parse_token(stream: ParseStream) -> ParseResult<ConstraintToken> { | |
}; | ||
ConstraintToken::Bump(Context::new(ident.span(), ConstraintTokenBump { bump })) | ||
} | ||
"program_seed" => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any place in the existing solana sdk that uses the term "program_seed"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tag @jstarry. Any ideas for an accurate keyword to refer to the base program id for PDAs? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah tbh I also find that term a little odd There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. other words mentioned in discord: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Discussed offline. Let's do |
||
stream.parse::<Token![=]>()?; | ||
ConstraintToken::ProgramSeed(Context::new( | ||
ident.span(), | ||
ConstraintTokenProgramSeed { | ||
program_seed: stream.parse()?, | ||
}, | ||
)) | ||
} | ||
_ => { | ||
stream.parse::<Token![=]>()?; | ||
let span = ident | ||
|
@@ -308,6 +317,7 @@ pub struct ConstraintGroupBuilder<'ty> { | |
pub mint_freeze_authority: Option<Context<ConstraintMintFreezeAuthority>>, | ||
pub mint_decimals: Option<Context<ConstraintMintDecimals>>, | ||
pub bump: Option<Context<ConstraintTokenBump>>, | ||
pub program_seed: Option<Context<ConstraintTokenProgramSeed>>, | ||
} | ||
|
||
impl<'ty> ConstraintGroupBuilder<'ty> { | ||
|
@@ -338,6 +348,7 @@ impl<'ty> ConstraintGroupBuilder<'ty> { | |
mint_freeze_authority: None, | ||
mint_decimals: None, | ||
bump: None, | ||
program_seed: None, | ||
} | ||
} | ||
|
||
|
@@ -494,6 +505,7 @@ impl<'ty> ConstraintGroupBuilder<'ty> { | |
mint_freeze_authority, | ||
mint_decimals, | ||
bump, | ||
program_seed, | ||
} = self; | ||
|
||
// Converts Option<Context<T>> -> Option<T>. | ||
|
@@ -519,6 +531,7 @@ impl<'ty> ConstraintGroupBuilder<'ty> { | |
bump: into_inner!(bump) | ||
.map(|b| b.bump) | ||
.expect("bump must be provided with seeds"), | ||
program_seed: into_inner!(program_seed).map(|id| id.program_seed), | ||
}); | ||
let associated_token = match (associated_token_mint, associated_token_authority) { | ||
(Some(mint), Some(auth)) => Some(ConstraintAssociatedToken { | ||
|
@@ -620,6 +633,7 @@ impl<'ty> ConstraintGroupBuilder<'ty> { | |
ConstraintToken::MintFreezeAuthority(c) => self.add_mint_freeze_authority(c), | ||
ConstraintToken::MintDecimals(c) => self.add_mint_decimals(c), | ||
ConstraintToken::Bump(c) => self.add_bump(c), | ||
ConstraintToken::ProgramSeed(c) => self.add_program_seed(c), | ||
} | ||
} | ||
|
||
|
@@ -725,6 +739,33 @@ impl<'ty> ConstraintGroupBuilder<'ty> { | |
Ok(()) | ||
} | ||
|
||
fn add_program_seed(&mut self, c: Context<ConstraintTokenProgramSeed>) -> ParseResult<()> { | ||
if self.program_seed.is_some() { | ||
return Err(ParseError::new(c.span(), "program_seed already provided")); | ||
} | ||
if self.seeds.is_none() { | ||
return Err(ParseError::new( | ||
c.span(), | ||
"seeds must be provided before program_seed", | ||
)); | ||
} | ||
if let Some(ref init) = self.init { | ||
if init.if_needed { | ||
return Err(ParseError::new( | ||
c.span(), | ||
"program_seed cannot be used with init_if_needed", | ||
)); | ||
} else { | ||
return Err(ParseError::new( | ||
c.span(), | ||
"program_seed cannot be used with init", | ||
)); | ||
} | ||
} | ||
self.program_seed.replace(c); | ||
Ok(()) | ||
} | ||
|
||
fn add_token_authority(&mut self, c: Context<ConstraintTokenAuthority>) -> ParseResult<()> { | ||
if self.token_authority.is_some() { | ||
return Err(ParseError::new( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,5 +15,8 @@ | |
}, | ||
"scripts": { | ||
"test": "anchor test" | ||
}, | ||
"dependencies": { | ||
"mocha": "^9.1.3" | ||
} | ||
} |
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.
used the opportunity to move this into feature section (wasnt really a bug fix)