Skip to content

Commit 3c72bbd

Browse files
mikemaccanaclaude
andcommitted
Fix metadata: create account at full size upfront, no CPI realloc needed
token-2022 cannot realloc an account inside a CPI call (InvalidRealloc). Pre-allocate the mint account at the full size required for all extensions including the TokenMetadata TLV so no realloc is needed. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 81da8e5 commit 3c72bbd

1 file changed

Lines changed: 2 additions & 5 deletions

File tree

  • tokens/token-2022/metadata/quasar/src

tokens/token-2022/metadata/quasar/src/lib.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,26 +79,23 @@ pub fn handle_initialize(
7979
symbol: &[u8],
8080
uri: &[u8],
8181
) -> Result<(), ProgramError> {
82-
// Create account with only base + MetadataPointer TLV space initially.
83-
// TokenMetadataInitialize reallocates the account internally when called.
8482
// 165 (base) + 1 (AccountType) + 68 (MetadataPointer TLV: 2+2+64) = 234 bytes
8583
let mint_size_base: usize = 234;
8684

87-
// Compute full rent to over-fund the account so the token-2022 realloc
88-
// during TokenMetadataInitialize has sufficient lamports.
8985
// TokenMetadata TLV: 2 (type) + 2 (length) + data
9086
// data: update_auth(32) + mint(32) + name(4+len) + symbol(4+len) + uri(4+len) + additional(4)
9187
let metadata_data_len = 32 + 32 + 4 + name.len() + 4 + symbol.len() + 4 + uri.len() + 4;
9288
let mint_size_full = mint_size_base + 4 + metadata_data_len;
9389
let lamports = Rent::get()?.try_minimum_balance(mint_size_full)?;
9490

91+
// Create at full size upfront: token-2022 cannot realloc an account inside CPI.
9592
accounts
9693
.system_program
9794
.create_account(
9895
accounts.payer,
9996
accounts.mint_account,
10097
lamports,
101-
mint_size_base as u64,
98+
mint_size_full as u64,
10299
accounts.token_program.to_account_view().address(),
103100
)
104101
.invoke()?;

0 commit comments

Comments
 (0)