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

Add error message when Mint and TokenAccount with init are not ordered correctly #2484

Merged
merged 4 commits into from
May 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion lang/syn/src/parser/accounts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ fn constraints_cross_checks(fields: &[AccountField]) -> ParseResult<()> {
}
}

for field in init_fields {
for (pos, field) in init_fields.iter().enumerate() {
// Get payer for init-ed account
let associated_payer_name = match field.constraints.init.clone().unwrap().payer {
// composite payer, check not supported
Expand Down Expand Up @@ -178,6 +178,24 @@ fn constraints_cross_checks(fields: &[AccountField]) -> ParseResult<()> {
));
}
}

// Make sure initialiazed token accounts are always declared after their corresponding mint.
InitKind::Mint { .. } => {
if init_fields.iter().enumerate().any(|(f_pos, f)| {
match &f.constraints.init.as_ref().unwrap().kind {
InitKind::Token { mint, .. }
| InitKind::AssociatedToken { mint, .. } => {
field.ident == mint.to_token_stream().to_string() && pos > f_pos
}
_ => false,
}
}) {
return Err(ParseError::new(
field.ident.span(),
"because of the init constraint, the mint has to be declared before the corresponding token account",
));
}
}
_ => (),
}
}
Expand Down