2026-03-17 18:08:53 +01:00
|
|
|
use nssa_core::{
|
|
|
|
|
account::{Account, AccountWithMetadata, Data},
|
2026-05-04 10:34:10 -03:00
|
|
|
program::{AccountPostState, Claim, ProgramId},
|
2026-03-17 18:08:53 +01:00
|
|
|
};
|
|
|
|
|
use token_core::{TokenDefinition, TokenHolding};
|
|
|
|
|
|
|
|
|
|
pub fn initialize_account(
|
|
|
|
|
definition_account: AccountWithMetadata,
|
|
|
|
|
account_to_initialize: AccountWithMetadata,
|
2026-05-04 10:34:10 -03:00
|
|
|
token_program_id: ProgramId,
|
2026-03-17 18:08:53 +01:00
|
|
|
) -> Vec<AccountPostState> {
|
|
|
|
|
assert_eq!(
|
|
|
|
|
account_to_initialize.account,
|
|
|
|
|
Account::default(),
|
|
|
|
|
"Only Uninitialized accounts can be initialized"
|
|
|
|
|
);
|
2026-04-15 14:55:04 -03:00
|
|
|
assert!(
|
|
|
|
|
account_to_initialize.is_authorized,
|
|
|
|
|
"Account to initialize must be authorized"
|
|
|
|
|
);
|
2026-05-04 10:34:10 -03:00
|
|
|
assert_eq!(
|
|
|
|
|
definition_account.account.program_owner, token_program_id,
|
|
|
|
|
"Token definition must be owned by token program"
|
|
|
|
|
);
|
2026-03-17 18:08:53 +01:00
|
|
|
|
|
|
|
|
let definition = TokenDefinition::try_from(&definition_account.account.data)
|
|
|
|
|
.expect("Definition account must be valid");
|
|
|
|
|
let holding =
|
|
|
|
|
TokenHolding::zeroized_from_definition(definition_account.account_id, &definition);
|
|
|
|
|
|
|
|
|
|
let definition_post = definition_account.account;
|
|
|
|
|
let mut account_to_initialize = account_to_initialize.account;
|
|
|
|
|
account_to_initialize.data = Data::from(&holding);
|
|
|
|
|
|
|
|
|
|
vec![
|
|
|
|
|
AccountPostState::new(definition_post),
|
2026-04-15 14:55:04 -03:00
|
|
|
AccountPostState::new_claimed(account_to_initialize, Claim::Authorized),
|
2026-03-17 18:08:53 +01:00
|
|
|
]
|
|
|
|
|
}
|