Skip to content

Commit bbb7d1c

Browse files
authored
ATA: Fix owner and data for token accounts (#159)
1 parent f66d556 commit bbb7d1c

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

programs/token/src/associated_token.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub fn create_account_for_associated_token_account(
4848
let account = Account {
4949
lamports: Rent::default().minimum_balance(TokenAccount::LEN),
5050
data,
51-
owner: ID,
51+
owner: TOKEN_PROGRAM_ID,
5252
executable: false,
5353
rent_epoch: 0,
5454
};
@@ -66,13 +66,17 @@ pub fn create_account_for_associated_token_2022_account(
6666
&TOKEN_2022_PROGRAM_ID,
6767
);
6868

69-
let mut data = vec![0u8; TokenAccount::LEN];
69+
// space for immutable owner extension and account type
70+
const EXTENDED_ACCOUNT_LEN: usize = TokenAccount::LEN + 5;
71+
let mut data = vec![0u8; EXTENDED_ACCOUNT_LEN];
7072
TokenAccount::pack(token_account_data, &mut data).unwrap();
73+
data[TokenAccount::LEN] = 2; // AccountType::Account
74+
data[TokenAccount::LEN + 1] = 7; // ExtensionType::ImmutableOwner
7175

7276
let account = Account {
73-
lamports: Rent::default().minimum_balance(TokenAccount::LEN),
77+
lamports: Rent::default().minimum_balance(EXTENDED_ACCOUNT_LEN),
7478
data,
75-
owner: ID,
79+
owner: TOKEN_2022_PROGRAM_ID,
7680
executable: false,
7781
rent_epoch: 0,
7882
};

0 commit comments

Comments
 (0)