Skip to content

Commit

Permalink
lang: Add error message when Mint and TokenAccount with init are no…
Browse files Browse the repository at this point in the history
…t ordered correctly (coral-xyz#2484)
  • Loading branch information
CanardMandarin authored May 9, 2023
1 parent 9a93a2e commit 714d524
Showing 1 changed file with 19 additions and 1 deletion.
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

0 comments on commit 714d524

Please sign in to comment.