mirror of
https://github.com/logos-blockchain/lez-programs.git
synced 2026-08-26 22:51:14 +00:00
fix(ata)!: namespace accounts by token program
ATA accounts are now namespaced by token program, so callers must explicitly pass the token_program_id when invoking ATA::Transfer. BREAKING CHANGE: `Instruction::Transfer`, `Instruction::Burn`, `Instruction::Create` now requires a `token_program_id` field. Any existing call site that omits it will fail to compile. Closes #83
This commit is contained in:
+20
-3
@@ -9,15 +9,32 @@ pub fn burn_from_associated_token_account(
|
||||
holder_ata: AccountWithMetadata,
|
||||
token_definition: AccountWithMetadata,
|
||||
ata_program_id: ProgramId,
|
||||
token_program_id: ProgramId,
|
||||
amount: u128,
|
||||
) -> (Vec<AccountPostState>, Vec<ChainedCall>) {
|
||||
let token_program_id = holder_ata.account.program_owner;
|
||||
assert!(owner.is_authorized, "Owner authorization is missing");
|
||||
assert_eq!(
|
||||
holder_ata.account.program_owner, token_program_id,
|
||||
"Holder ATA must be owned by expected token program"
|
||||
);
|
||||
assert_eq!(
|
||||
token_definition.account.program_owner, token_program_id,
|
||||
"Token definition must be owned by expected token program"
|
||||
);
|
||||
let definition_id = TokenHolding::try_from(&holder_ata.account.data)
|
||||
.expect("Holder ATA must hold a valid token")
|
||||
.definition_id();
|
||||
let seed =
|
||||
ata_core::verify_ata_and_get_seed(&holder_ata, &owner, definition_id, ata_program_id);
|
||||
assert_eq!(
|
||||
definition_id, token_definition.account_id,
|
||||
"Holder ATA token definition does not match"
|
||||
);
|
||||
let seed = ata_core::verify_ata_and_get_seed(
|
||||
&holder_ata,
|
||||
&owner,
|
||||
token_program_id,
|
||||
definition_id,
|
||||
ata_program_id,
|
||||
);
|
||||
|
||||
let post_states = vec![
|
||||
AccountPostState::new(owner.account.clone()),
|
||||
|
||||
+20
-1
@@ -2,27 +2,46 @@ use nssa_core::{
|
||||
account::{Account, AccountWithMetadata},
|
||||
program::{AccountPostState, ChainedCall, Claim, ProgramId},
|
||||
};
|
||||
use token_core::{TokenDefinition, TokenHolding};
|
||||
|
||||
pub fn create_associated_token_account(
|
||||
owner: AccountWithMetadata,
|
||||
token_definition: AccountWithMetadata,
|
||||
ata_account: AccountWithMetadata,
|
||||
ata_program_id: ProgramId,
|
||||
token_program_id: ProgramId,
|
||||
) -> (Vec<AccountPostState>, Vec<ChainedCall>) {
|
||||
// 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.
|
||||
let token_program_id = token_definition.account.program_owner;
|
||||
assert_eq!(
|
||||
token_definition.account.program_owner, token_program_id,
|
||||
"Token definition must be owned by expected token program"
|
||||
);
|
||||
let _definition = TokenDefinition::try_from(&token_definition.account.data)
|
||||
.expect("Token definition must be valid");
|
||||
let seed = ata_core::verify_ata_and_get_seed(
|
||||
&ata_account,
|
||||
&owner,
|
||||
token_program_id,
|
||||
token_definition.account_id,
|
||||
ata_program_id,
|
||||
);
|
||||
|
||||
// Idempotent: already initialized → no-op
|
||||
if ata_account.account != Account::default() {
|
||||
assert_eq!(
|
||||
ata_account.account.program_owner, token_program_id,
|
||||
"Existing ATA must be owned by expected token program"
|
||||
);
|
||||
let holding = TokenHolding::try_from(&ata_account.account.data)
|
||||
.expect("Existing ATA must hold a valid token");
|
||||
assert_eq!(
|
||||
holding.definition_id(),
|
||||
token_definition.account_id,
|
||||
"Existing ATA token definition does not match"
|
||||
);
|
||||
return (
|
||||
vec![
|
||||
AccountPostState::new_claimed_if_default(owner.account.clone(), Claim::Authorized),
|
||||
|
||||
+184
-9
@@ -7,6 +7,7 @@ use token_core::{TokenDefinition, TokenHolding};
|
||||
|
||||
const ATA_PROGRAM_ID: nssa_core::program::ProgramId = [1u32; 8];
|
||||
const TOKEN_PROGRAM_ID: nssa_core::program::ProgramId = [2u32; 8];
|
||||
const OTHER_TOKEN_PROGRAM_ID: nssa_core::program::ProgramId = [3u32; 8];
|
||||
|
||||
fn owner_id() -> AccountId {
|
||||
AccountId::new([0x01u8; 32])
|
||||
@@ -19,7 +20,7 @@ fn definition_id() -> AccountId {
|
||||
fn ata_id() -> AccountId {
|
||||
get_associated_token_account_id(
|
||||
&ATA_PROGRAM_ID,
|
||||
&compute_ata_seed(owner_id(), definition_id()),
|
||||
&compute_ata_seed(TOKEN_PROGRAM_ID, owner_id(), definition_id()),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -79,6 +80,7 @@ fn create_emits_chained_call_for_uninitialized_ata() {
|
||||
definition_account(),
|
||||
uninitialized_ata_account(),
|
||||
ATA_PROGRAM_ID,
|
||||
TOKEN_PROGRAM_ID,
|
||||
);
|
||||
|
||||
assert_eq!(post_states.len(), 3);
|
||||
@@ -91,7 +93,11 @@ fn create_emits_chained_call_for_uninitialized_ata() {
|
||||
vec![definition_account(), authorized_ata],
|
||||
&token_core::Instruction::InitializeAccount,
|
||||
)
|
||||
.with_pda_seeds(vec![compute_ata_seed(owner_id(), definition_id())]);
|
||||
.with_pda_seeds(vec![compute_ata_seed(
|
||||
TOKEN_PROGRAM_ID,
|
||||
owner_id(),
|
||||
definition_id(),
|
||||
)]);
|
||||
|
||||
assert_eq!(chained_calls, vec![expected_call]);
|
||||
}
|
||||
@@ -103,6 +109,7 @@ fn create_is_idempotent_for_initialized_ata() {
|
||||
definition_account(),
|
||||
initialized_ata_account(),
|
||||
ATA_PROGRAM_ID,
|
||||
TOKEN_PROGRAM_ID,
|
||||
);
|
||||
|
||||
assert_eq!(post_states.len(), 3);
|
||||
@@ -126,27 +133,41 @@ fn create_panics_on_wrong_ata_address() {
|
||||
definition_account(),
|
||||
wrong_ata,
|
||||
ATA_PROGRAM_ID,
|
||||
TOKEN_PROGRAM_ID,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_associated_token_account_id_is_deterministic() {
|
||||
let seed = compute_ata_seed(owner_id(), definition_id());
|
||||
let seed = compute_ata_seed(TOKEN_PROGRAM_ID, owner_id(), definition_id());
|
||||
let id1 = get_associated_token_account_id(&ATA_PROGRAM_ID, &seed);
|
||||
let id2 = get_associated_token_account_id(&ATA_PROGRAM_ID, &seed);
|
||||
assert_eq!(id1, id2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_associated_token_account_id_differs_by_token_program() {
|
||||
let id1 = get_associated_token_account_id(
|
||||
&ATA_PROGRAM_ID,
|
||||
&compute_ata_seed(TOKEN_PROGRAM_ID, owner_id(), definition_id()),
|
||||
);
|
||||
let id2 = get_associated_token_account_id(
|
||||
&ATA_PROGRAM_ID,
|
||||
&compute_ata_seed(OTHER_TOKEN_PROGRAM_ID, owner_id(), definition_id()),
|
||||
);
|
||||
assert_ne!(id1, id2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_associated_token_account_id_differs_by_owner() {
|
||||
let other_owner = AccountId::new([0x99u8; 32]);
|
||||
let id1 = get_associated_token_account_id(
|
||||
&ATA_PROGRAM_ID,
|
||||
&compute_ata_seed(owner_id(), definition_id()),
|
||||
&compute_ata_seed(TOKEN_PROGRAM_ID, owner_id(), definition_id()),
|
||||
);
|
||||
let id2 = get_associated_token_account_id(
|
||||
&ATA_PROGRAM_ID,
|
||||
&compute_ata_seed(other_owner, definition_id()),
|
||||
&compute_ata_seed(TOKEN_PROGRAM_ID, other_owner, definition_id()),
|
||||
);
|
||||
assert_ne!(id1, id2);
|
||||
}
|
||||
@@ -156,13 +177,63 @@ fn get_associated_token_account_id_differs_by_definition() {
|
||||
let other_def = AccountId::new([0x99u8; 32]);
|
||||
let id1 = get_associated_token_account_id(
|
||||
&ATA_PROGRAM_ID,
|
||||
&compute_ata_seed(owner_id(), definition_id()),
|
||||
&compute_ata_seed(TOKEN_PROGRAM_ID, owner_id(), definition_id()),
|
||||
);
|
||||
let id2 = get_associated_token_account_id(
|
||||
&ATA_PROGRAM_ID,
|
||||
&compute_ata_seed(TOKEN_PROGRAM_ID, owner_id(), other_def),
|
||||
);
|
||||
let id2 =
|
||||
get_associated_token_account_id(&ATA_PROGRAM_ID, &compute_ata_seed(owner_id(), other_def));
|
||||
assert_ne!(id1, id2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "Token definition must be owned by expected token program")]
|
||||
fn create_panics_when_definition_is_owned_by_unexpected_token_program() {
|
||||
let mut definition = definition_account();
|
||||
definition.account.program_owner = OTHER_TOKEN_PROGRAM_ID;
|
||||
|
||||
crate::create::create_associated_token_account(
|
||||
owner_account(),
|
||||
definition,
|
||||
uninitialized_ata_account(),
|
||||
ATA_PROGRAM_ID,
|
||||
TOKEN_PROGRAM_ID,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "Existing ATA must be owned by expected token program")]
|
||||
fn create_panics_when_existing_ata_is_owned_by_unexpected_token_program() {
|
||||
let mut ata = initialized_ata_account();
|
||||
ata.account.program_owner = OTHER_TOKEN_PROGRAM_ID;
|
||||
|
||||
crate::create::create_associated_token_account(
|
||||
owner_account(),
|
||||
definition_account(),
|
||||
ata,
|
||||
ATA_PROGRAM_ID,
|
||||
TOKEN_PROGRAM_ID,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "Existing ATA token definition does not match")]
|
||||
fn create_panics_when_existing_ata_definition_mismatches_requested_definition() {
|
||||
let mut ata = initialized_ata_account();
|
||||
ata.account.data = Data::from(&TokenHolding::Fungible {
|
||||
definition_id: AccountId::new([0xAAu8; 32]),
|
||||
balance: 100,
|
||||
});
|
||||
|
||||
crate::create::create_associated_token_account(
|
||||
owner_account(),
|
||||
definition_account(),
|
||||
ata,
|
||||
ATA_PROGRAM_ID,
|
||||
TOKEN_PROGRAM_ID,
|
||||
);
|
||||
}
|
||||
|
||||
fn recipient_id() -> AccountId {
|
||||
AccountId::new([0x03u8; 32])
|
||||
}
|
||||
@@ -190,6 +261,7 @@ fn transfer_emits_chained_call_for_initialized_recipient() {
|
||||
initialized_ata_account(),
|
||||
initialized_recipient_account(),
|
||||
ATA_PROGRAM_ID,
|
||||
TOKEN_PROGRAM_ID,
|
||||
25,
|
||||
);
|
||||
|
||||
@@ -205,7 +277,11 @@ fn transfer_emits_chained_call_for_initialized_recipient() {
|
||||
amount_to_transfer: 25,
|
||||
},
|
||||
)
|
||||
.with_pda_seeds(vec![compute_ata_seed(owner_id(), definition_id())]);
|
||||
.with_pda_seeds(vec![compute_ata_seed(
|
||||
TOKEN_PROGRAM_ID,
|
||||
owner_id(),
|
||||
definition_id(),
|
||||
)]);
|
||||
|
||||
assert_eq!(chained_calls, vec![expected_call]);
|
||||
}
|
||||
@@ -221,6 +297,7 @@ fn transfer_panics_when_owner_not_authorized() {
|
||||
initialized_ata_account(),
|
||||
initialized_recipient_account(),
|
||||
ATA_PROGRAM_ID,
|
||||
TOKEN_PROGRAM_ID,
|
||||
1,
|
||||
);
|
||||
}
|
||||
@@ -239,6 +316,23 @@ fn transfer_panics_when_recipient_is_default() {
|
||||
initialized_ata_account(),
|
||||
default_recipient,
|
||||
ATA_PROGRAM_ID,
|
||||
TOKEN_PROGRAM_ID,
|
||||
1,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "Sender ATA must be owned by expected token program")]
|
||||
fn transfer_panics_when_sender_ata_is_owned_by_unexpected_token_program() {
|
||||
let mut sender = initialized_ata_account();
|
||||
sender.account.program_owner = OTHER_TOKEN_PROGRAM_ID;
|
||||
|
||||
crate::transfer::transfer_from_associated_token_account(
|
||||
owner_account(),
|
||||
sender,
|
||||
initialized_recipient_account(),
|
||||
ATA_PROGRAM_ID,
|
||||
TOKEN_PROGRAM_ID,
|
||||
1,
|
||||
);
|
||||
}
|
||||
@@ -254,6 +348,7 @@ fn transfer_panics_when_recipient_is_foreign_owned() {
|
||||
initialized_ata_account(),
|
||||
foreign_recipient,
|
||||
ATA_PROGRAM_ID,
|
||||
TOKEN_PROGRAM_ID,
|
||||
1,
|
||||
);
|
||||
}
|
||||
@@ -269,6 +364,7 @@ fn transfer_panics_when_recipient_data_is_malformed() {
|
||||
initialized_ata_account(),
|
||||
malformed_recipient,
|
||||
ATA_PROGRAM_ID,
|
||||
TOKEN_PROGRAM_ID,
|
||||
1,
|
||||
);
|
||||
}
|
||||
@@ -287,6 +383,85 @@ fn transfer_panics_when_recipient_definition_mismatches_sender() {
|
||||
initialized_ata_account(),
|
||||
mismatched_recipient,
|
||||
ATA_PROGRAM_ID,
|
||||
TOKEN_PROGRAM_ID,
|
||||
1,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn burn_emits_chained_call_for_initialized_ata() {
|
||||
let (post_states, chained_calls) = crate::burn::burn_from_associated_token_account(
|
||||
owner_account(),
|
||||
initialized_ata_account(),
|
||||
definition_account(),
|
||||
ATA_PROGRAM_ID,
|
||||
TOKEN_PROGRAM_ID,
|
||||
25,
|
||||
);
|
||||
|
||||
assert_eq!(post_states.len(), 3);
|
||||
assert_eq!(chained_calls.len(), 1);
|
||||
|
||||
let mut holder_auth = initialized_ata_account();
|
||||
holder_auth.is_authorized = true;
|
||||
let expected_call = ChainedCall::new(
|
||||
TOKEN_PROGRAM_ID,
|
||||
vec![definition_account(), holder_auth],
|
||||
&token_core::Instruction::Burn { amount_to_burn: 25 },
|
||||
)
|
||||
.with_pda_seeds(vec![compute_ata_seed(
|
||||
TOKEN_PROGRAM_ID,
|
||||
owner_id(),
|
||||
definition_id(),
|
||||
)]);
|
||||
|
||||
assert_eq!(chained_calls, vec![expected_call]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "Holder ATA must be owned by expected token program")]
|
||||
fn burn_panics_when_holder_ata_is_owned_by_unexpected_token_program() {
|
||||
let mut holder = initialized_ata_account();
|
||||
holder.account.program_owner = OTHER_TOKEN_PROGRAM_ID;
|
||||
|
||||
crate::burn::burn_from_associated_token_account(
|
||||
owner_account(),
|
||||
holder,
|
||||
definition_account(),
|
||||
ATA_PROGRAM_ID,
|
||||
TOKEN_PROGRAM_ID,
|
||||
1,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "Token definition must be owned by expected token program")]
|
||||
fn burn_panics_when_definition_is_owned_by_unexpected_token_program() {
|
||||
let mut definition = definition_account();
|
||||
definition.account.program_owner = OTHER_TOKEN_PROGRAM_ID;
|
||||
|
||||
crate::burn::burn_from_associated_token_account(
|
||||
owner_account(),
|
||||
initialized_ata_account(),
|
||||
definition,
|
||||
ATA_PROGRAM_ID,
|
||||
TOKEN_PROGRAM_ID,
|
||||
1,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "Holder ATA token definition does not match")]
|
||||
fn burn_panics_when_holder_definition_mismatches_supplied_definition() {
|
||||
let mut definition = definition_account();
|
||||
definition.account_id = AccountId::new([0xBBu8; 32]);
|
||||
|
||||
crate::burn::burn_from_associated_token_account(
|
||||
owner_account(),
|
||||
initialized_ata_account(),
|
||||
definition,
|
||||
ATA_PROGRAM_ID,
|
||||
TOKEN_PROGRAM_ID,
|
||||
1,
|
||||
);
|
||||
}
|
||||
|
||||
+6
-1
@@ -9,16 +9,21 @@ pub fn transfer_from_associated_token_account(
|
||||
sender_ata: AccountWithMetadata,
|
||||
recipient: AccountWithMetadata,
|
||||
ata_program_id: ProgramId,
|
||||
token_program_id: ProgramId,
|
||||
amount: u128,
|
||||
) -> (Vec<AccountPostState>, Vec<ChainedCall>) {
|
||||
let token_program_id = sender_ata.account.program_owner;
|
||||
assert!(owner.is_authorized, "Owner authorization is missing");
|
||||
assert_eq!(
|
||||
sender_ata.account.program_owner, token_program_id,
|
||||
"Sender ATA must be owned by expected token program"
|
||||
);
|
||||
let sender_definition_id = TokenHolding::try_from(&sender_ata.account.data)
|
||||
.expect("Sender ATA must hold a valid token")
|
||||
.definition_id();
|
||||
let sender_seed = ata_core::verify_ata_and_get_seed(
|
||||
&sender_ata,
|
||||
&owner,
|
||||
token_program_id,
|
||||
sender_definition_id,
|
||||
ata_program_id,
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user