2026-03-17 18:08:53 +01:00
|
|
|
use nssa_core::{
|
|
|
|
|
account::{Account, AccountWithMetadata},
|
2026-04-15 14:55:04 -03:00
|
|
|
program::{AccountPostState, ChainedCall, Claim, ProgramId},
|
2026-03-17 18:08:53 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
pub fn create_associated_token_account(
|
|
|
|
|
owner: AccountWithMetadata,
|
|
|
|
|
token_definition: AccountWithMetadata,
|
|
|
|
|
ata_account: AccountWithMetadata,
|
|
|
|
|
ata_program_id: ProgramId,
|
|
|
|
|
) -> (Vec<AccountPostState>, Vec<ChainedCall>) {
|
2026-04-15 14:55:04 -03:00
|
|
|
// No explicit owner authorization check is needed here: ATA creation is idempotent, so the
|
|
|
|
|
// call itself may proceed without `owner.is_authorized`. If the owner account is still
|
|
|
|
|
// default, the returned post-state will still carry `Claim::Authorized` so the runtime can
|
|
|
|
|
// claim that owner account when needed.
|
2026-03-17 18:08:53 +01:00
|
|
|
let token_program_id = token_definition.account.program_owner;
|
2026-04-15 14:55:04 -03:00
|
|
|
let seed = ata_core::verify_ata_and_get_seed(
|
2026-03-17 18:08:53 +01:00
|
|
|
&ata_account,
|
|
|
|
|
&owner,
|
|
|
|
|
token_definition.account_id,
|
|
|
|
|
ata_program_id,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Idempotent: already initialized → no-op
|
|
|
|
|
if ata_account.account != Account::default() {
|
|
|
|
|
return (
|
|
|
|
|
vec![
|
2026-04-15 14:55:04 -03:00
|
|
|
AccountPostState::new_claimed_if_default(owner.account.clone(), Claim::Authorized),
|
2026-03-17 18:08:53 +01:00
|
|
|
AccountPostState::new(token_definition.account.clone()),
|
|
|
|
|
AccountPostState::new(ata_account.account.clone()),
|
|
|
|
|
],
|
|
|
|
|
vec![],
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let post_states = vec![
|
2026-04-15 14:55:04 -03:00
|
|
|
AccountPostState::new_claimed_if_default(owner.account.clone(), Claim::Authorized),
|
2026-03-17 18:08:53 +01:00
|
|
|
AccountPostState::new(token_definition.account.clone()),
|
|
|
|
|
AccountPostState::new(ata_account.account.clone()),
|
|
|
|
|
];
|
2026-04-15 14:55:04 -03:00
|
|
|
let mut ata_account_auth = ata_account.clone();
|
|
|
|
|
ata_account_auth.is_authorized = true;
|
2026-03-17 18:08:53 +01:00
|
|
|
let chained_call = ChainedCall::new(
|
|
|
|
|
token_program_id,
|
2026-04-15 14:55:04 -03:00
|
|
|
vec![token_definition.clone(), ata_account_auth],
|
2026-03-17 18:08:53 +01:00
|
|
|
&token_core::Instruction::InitializeAccount,
|
2026-04-15 14:55:04 -03:00
|
|
|
)
|
|
|
|
|
.with_pda_seeds(vec![seed]);
|
2026-03-17 18:08:53 +01:00
|
|
|
(post_states, vec![chained_call])
|
|
|
|
|
}
|