What Is Default Account State?
The Token Extension Program’sDefaultAccountState mint extension sets the
initial AccountState for every new token account created for that mint.
This is most commonly used to make new token accounts start frozen. Token
accounts that start frozen cannot be used until the mint’s freeze authority
thaws them.
Before you start
The examples on this page run against a local validator athttp://localhost:8899 (WebSocket ws://localhost:8900) and fund their
accounts with airdrops from it. Start one in a separate terminal with
Surfpool (surfpool start) or
solana-test-validator.
Each code section below lists the packages or crates it needs. The TypeScript
examples use top-level await, so run them as ES modules, for example with
npx tsx index.ts in a project whose package.json sets "type": "module".
How to Create a Mint with a Default Account State
To create a mint with a default account state:- Calculate the mint account size and rent needed for the mint and the
DefaultAccountStateextension. - Create the mint account with
CreateAccount, initializeDefaultAccountState, and initialize the mint withInitializeMint. - Create token accounts for the mint and observe that each new token account starts in the current default state.
- Use
UpdateDefaultAccountStateto change the state future token accounts should start in.
1
Calculate account size
Calculate the mint account size for the base mint plus the
DefaultAccountState extension. This is the size used in
CreateAccount.Example
const client = await createClient()
.use(generatedPayer())
.use(
solanaRpc({
rpcUrl: "http://localhost:8899",
rpcSubscriptionsUrl: "ws://localhost:8900"
})
)
.use(rpcAirdrop())
.use(airdropPayer(lamports(1_000_000_000n)));
const mint = await generateKeyPairSigner();
const recipient = await generateKeyPairSigner();
const defaultAccountStateExtension = extension("DefaultAccountState", {
state: AccountState.Frozen
});
const mintSpace = BigInt(getMintSize([defaultAccountStateExtension]));
2
Calculate rent
Calculate rent using the size needed for the mint plus the
DefaultAccountState extension.Example
const client = await createClient()
.use(generatedPayer())
.use(
solanaRpc({
rpcUrl: "http://localhost:8899",
rpcSubscriptionsUrl: "ws://localhost:8900"
})
)
.use(rpcAirdrop())
.use(airdropPayer(lamports(1_000_000_000n)));
const mint = await generateKeyPairSigner();
const recipient = await generateKeyPairSigner();
const defaultAccountStateExtension = extension("DefaultAccountState", {
state: AccountState.Frozen
});
const mintSpace = BigInt(getMintSize([defaultAccountStateExtension]));
const mintRent = await client.rpc
.getMinimumBalanceForRentExemption(mintSpace)
.send();
3
Create the mint account
Create the mint account with the calculated space and lamports.
Example
const client = await createClient()
.use(generatedPayer())
.use(
solanaRpc({
rpcUrl: "http://localhost:8899",
rpcSubscriptionsUrl: "ws://localhost:8900"
})
)
.use(rpcAirdrop())
.use(airdropPayer(lamports(1_000_000_000n)));
const mint = await generateKeyPairSigner();
const recipient = await generateKeyPairSigner();
const defaultAccountStateExtension = extension("DefaultAccountState", {
state: AccountState.Frozen
});
const mintSpace = BigInt(getMintSize([defaultAccountStateExtension]));
const mintRent = await client.rpc
.getMinimumBalanceForRentExemption(mintSpace)
.send();
await client.sendTransaction([
getCreateAccountInstruction({
payer: client.payer,
newAccount: mint,
lamports: mintRent,
space: mintSpace,
programAddress: TOKEN_2022_PROGRAM_ADDRESS
})
]);
4
Initialize DefaultAccountState
Initialize the
DefaultAccountState extension on the mint.Example
const client = await createClient()
.use(generatedPayer())
.use(
solanaRpc({
rpcUrl: "http://localhost:8899",
rpcSubscriptionsUrl: "ws://localhost:8900"
})
)
.use(rpcAirdrop())
.use(airdropPayer(lamports(1_000_000_000n)));
const mint = await generateKeyPairSigner();
const recipient = await generateKeyPairSigner();
const defaultAccountStateExtension = extension("DefaultAccountState", {
state: AccountState.Frozen
});
const mintSpace = BigInt(getMintSize([defaultAccountStateExtension]));
const mintRent = await client.rpc
.getMinimumBalanceForRentExemption(mintSpace)
.send();
await client.sendTransaction([
getCreateAccountInstruction({
payer: client.payer,
newAccount: mint,
lamports: mintRent,
space: mintSpace,
programAddress: TOKEN_2022_PROGRAM_ADDRESS
}),
getInitializeDefaultAccountStateInstruction({
mint: mint.address,
state: AccountState.Frozen
})
]);
5
Initialize the mint
Initialize the mint with
InitializeMint in the same transaction.Example
const client = await createClient()
.use(generatedPayer())
.use(
solanaRpc({
rpcUrl: "http://localhost:8899",
rpcSubscriptionsUrl: "ws://localhost:8900"
})
)
.use(rpcAirdrop())
.use(airdropPayer(lamports(1_000_000_000n)));
const mint = await generateKeyPairSigner();
const recipient = await generateKeyPairSigner();
const defaultAccountStateExtension = extension("DefaultAccountState", {
state: AccountState.Frozen
});
const mintSpace = BigInt(getMintSize([defaultAccountStateExtension]));
const mintRent = await client.rpc
.getMinimumBalanceForRentExemption(mintSpace)
.send();
await client.sendTransaction([
getCreateAccountInstruction({
payer: client.payer,
newAccount: mint,
lamports: mintRent,
space: mintSpace,
programAddress: TOKEN_2022_PROGRAM_ADDRESS
}),
getInitializeDefaultAccountStateInstruction({
mint: mint.address,
state: AccountState.Frozen
}),
getInitializeMintInstruction({
mint: mint.address,
decimals: 0,
mintAuthority: client.payer.address,
freezeAuthority: client.payer.address
})
]);
6
Create a token account
Create a token account for the mint. Because the current default state is
Frozen, the new token account starts frozen.Example
const client = await createClient()
.use(generatedPayer())
.use(
solanaRpc({
rpcUrl: "http://localhost:8899",
rpcSubscriptionsUrl: "ws://localhost:8900"
})
)
.use(rpcAirdrop())
.use(airdropPayer(lamports(1_000_000_000n)));
const mint = await generateKeyPairSigner();
const recipient = await generateKeyPairSigner();
const defaultAccountStateExtension = extension("DefaultAccountState", {
state: AccountState.Frozen
});
const mintSpace = BigInt(getMintSize([defaultAccountStateExtension]));
const mintRent = await client.rpc
.getMinimumBalanceForRentExemption(mintSpace)
.send();
await client.sendTransaction([
getCreateAccountInstruction({
payer: client.payer,
newAccount: mint,
lamports: mintRent,
space: mintSpace,
programAddress: TOKEN_2022_PROGRAM_ADDRESS
}),
getInitializeDefaultAccountStateInstruction({
mint: mint.address,
state: AccountState.Frozen
}),
getInitializeMintInstruction({
mint: mint.address,
decimals: 0,
mintAuthority: client.payer.address,
freezeAuthority: client.payer.address
})
]);
const [tokenAccount] = await findAssociatedTokenPda({
mint: mint.address,
owner: recipient.address,
tokenProgram: TOKEN_2022_PROGRAM_ADDRESS
});
await client.sendTransaction([
await getCreateAssociatedTokenInstructionAsync({
payer: client.payer,
mint: mint.address,
owner: recipient.address
})
]);
7
Update the default account state
Use
UpdateDefaultAccountState to change the default state so future token
accounts no longer start frozen. Token accounts that already exist keep their
current state, so the account created in the previous step stays frozen.Example
const client = await createClient()
.use(generatedPayer())
.use(
solanaRpc({
rpcUrl: "http://localhost:8899",
rpcSubscriptionsUrl: "ws://localhost:8900"
})
)
.use(rpcAirdrop())
.use(airdropPayer(lamports(1_000_000_000n)));
const mint = await generateKeyPairSigner();
const recipient = await generateKeyPairSigner();
const defaultAccountStateExtension = extension("DefaultAccountState", {
state: AccountState.Frozen
});
const mintSpace = BigInt(getMintSize([defaultAccountStateExtension]));
const mintRent = await client.rpc
.getMinimumBalanceForRentExemption(mintSpace)
.send();
await client.sendTransaction([
getCreateAccountInstruction({
payer: client.payer,
newAccount: mint,
lamports: mintRent,
space: mintSpace,
programAddress: TOKEN_2022_PROGRAM_ADDRESS
}),
getInitializeDefaultAccountStateInstruction({
mint: mint.address,
state: AccountState.Frozen
}),
getInitializeMintInstruction({
mint: mint.address,
decimals: 0,
mintAuthority: client.payer.address,
freezeAuthority: client.payer.address
})
]);
const [tokenAccount] = await findAssociatedTokenPda({
mint: mint.address,
owner: recipient.address,
tokenProgram: TOKEN_2022_PROGRAM_ADDRESS
});
await client.sendTransaction([
await getCreateAssociatedTokenInstructionAsync({
payer: client.payer,
mint: mint.address,
owner: recipient.address
})
]);
await client.sendTransaction([
getUpdateDefaultAccountStateInstruction({
mint: mint.address,
freezeAuthority: client.payer,
state: AccountState.Initialized
})
]);
Instruction Order
DefaultAccountStateInstruction::Initialize must come before
InitializeMint. CreateAccount,
DefaultAccountStateInstruction::Initialize, and InitializeMint
must be included in the same transaction.Source Reference
| Item | Description | Source |
|---|---|---|
DefaultAccountState | Mint extension that stores the state new token accounts should inherit. | Source |
DefaultAccountStateInstruction::Initialize | Instruction that initializes the mint’s default token account state before InitializeMint. | Source |
DefaultAccountStateInstruction::Update | Instruction that lets the freeze authority change the default state for future token accounts. | Source |
process_initialize_default_account_state | Processor logic that writes the initial default state onto an uninitialized mint. | Source |
process_update_default_account_state | Processor logic that verifies the freeze authority before changing the mint’s stored default state. | Source |
Kit
These examples use the generated Token-2022 instructions directly.Terminal
npm install @solana/kit @solana/kit-plugin-rpc @solana/kit-plugin-signer @solana-program/system @solana-program/token-2022
Instructions
import {
lamports,
createClient,
generateKeyPairSigner,
unwrapOption
} from "@solana/kit";
import { solanaRpc, rpcAirdrop } from "@solana/kit-plugin-rpc";
import { generatedPayer, airdropPayer } from "@solana/kit-plugin-signer";
import { getCreateAccountInstruction } from "@solana-program/system";
import {
AccountState,
extension,
fetchMint,
fetchToken,
findAssociatedTokenPda,
getCreateAssociatedTokenInstructionAsync,
getInitializeDefaultAccountStateInstruction,
getInitializeMintInstruction,
getMintSize,
getUpdateDefaultAccountStateInstruction,
isExtension,
TOKEN_2022_PROGRAM_ADDRESS
} from "@solana-program/token-2022";
const client = await createClient()
.use(generatedPayer())
.use(
solanaRpc({
rpcUrl: "http://localhost:8899",
rpcSubscriptionsUrl: "ws://localhost:8900"
})
)
.use(rpcAirdrop())
.use(airdropPayer(lamports(1_000_000_000n)));
const mint = await generateKeyPairSigner();
const getAccountStateLabel = (state: AccountState) => {
switch (state) {
case AccountState.Frozen:
return "Frozen";
case AccountState.Initialized:
return "Initialized";
default:
return "Uninitialized";
}
};
const defaultAccountStateExtension = extension("DefaultAccountState", {
state: AccountState.Frozen
});
const mintSpace = BigInt(getMintSize([defaultAccountStateExtension]));
const mintRent = await client.rpc
.getMinimumBalanceForRentExemption(mintSpace)
.send();
await client.sendTransaction([
getCreateAccountInstruction({
payer: client.payer, // Account funding the new mint account.
newAccount: mint, // New mint account to create.
lamports: mintRent, // Lamports funding the mint account rent.
space: mintSpace, // Account size in bytes for the mint plus DefaultAccountState.
programAddress: TOKEN_2022_PROGRAM_ADDRESS // Program that owns the mint account.
}),
getInitializeDefaultAccountStateInstruction({
mint: mint.address, // Mint account that stores the DefaultAccountState extension.
state: AccountState.Frozen // Default state assigned to new token accounts.
}),
getInitializeMintInstruction({
mint: mint.address, // Mint account to initialize.
decimals: 0, // Number of decimals for the token.
mintAuthority: client.payer.address, // Authority allowed to mint new tokens.
freezeAuthority: client.payer.address // Authority allowed to freeze token accounts.
})
]);
const [tokenAccount] = await findAssociatedTokenPda({
mint: mint.address,
owner: client.payer.address,
tokenProgram: TOKEN_2022_PROGRAM_ADDRESS
});
await client.sendTransaction([
await getCreateAssociatedTokenInstructionAsync({
payer: client.payer, // Account funding the associated token account creation.
mint: mint.address, // Mint for the associated token account.
owner: client.payer.address // Owner of the token account.
})
]);
const tokenAccountBeforeUpdate = await fetchToken(client.rpc, tokenAccount);
const mintAccountBeforeUpdate = await fetchMint(client.rpc, mint.address);
const defaultAccountStateBeforeUpdate = (
unwrapOption(mintAccountBeforeUpdate.data.extensions) ?? []
).find((item) => isExtension("DefaultAccountState", item));
await client.sendTransaction([
getUpdateDefaultAccountStateInstruction({
mint: mint.address, // Mint account that stores the DefaultAccountState extension.
freezeAuthority: client.payer, // Freeze authority authorized to update the default state.
state: AccountState.Initialized // New default state assigned to later token accounts.
})
]);
const tokenAccountAfterUpdate = await fetchToken(client.rpc, tokenAccount);
const mintAccountAfterUpdate = await fetchMint(client.rpc, mint.address);
const defaultAccountStateAfterUpdate = (
unwrapOption(mintAccountAfterUpdate.data.extensions) ?? []
).find((item) => isExtension("DefaultAccountState", item));
console.log("Mint Address:", mint.address);
console.log(
"Default Account State Before Update:",
defaultAccountStateBeforeUpdate
);
console.log(
"Token Account State Before Update:",
getAccountStateLabel(tokenAccountBeforeUpdate.data.state)
);
console.log(
"Default Account State After Update:",
defaultAccountStateAfterUpdate
);
console.log(
"Existing Token Account State After Update (unchanged):",
getAccountStateLabel(tokenAccountAfterUpdate.data.state)
);
Web3.js v3
These examples use the class-based@solana/web3.js v3 API with
@solana-program instruction builders.
Terminal
npm install @solana/web3.js@3 @solana/kit @solana-program/token-2022
Instructions
import {
Connection,
Keypair,
PublicKey,
sendAndConfirmTransaction,
SystemProgram,
Transaction,
LAMPORTS_PER_SOL
} from "@solana/web3.js";
import { unwrapOption } from "@solana/kit";
import {
AccountState,
findAssociatedTokenPda,
getCreateAssociatedTokenInstruction,
getInitializeDefaultAccountStateInstruction,
getInitializeMint2Instruction,
getMintDecoder,
getMintSize,
getTokenDecoder,
getUpdateDefaultAccountStateInstruction,
TOKEN_2022_PROGRAM_ADDRESS
} from "@solana-program/token-2022";
const connection = new Connection("http://localhost:8899", "confirmed");
const latestBlockhash = await connection.getLatestBlockhash();
const feePayer = await Keypair.generate();
const getTokenAccountStateLabel = (state: AccountState) => AccountState[state];
const airdropSignature = await connection.requestAirdrop(
feePayer.publicKey,
5 * LAMPORTS_PER_SOL
);
await connection.confirmTransaction({
blockhash: latestBlockhash.blockhash,
lastValidBlockHeight: latestBlockhash.lastValidBlockHeight,
signature: airdropSignature
});
const mint = await Keypair.generate();
const mintLength = getMintSize([
{ __kind: "DefaultAccountState", state: AccountState.Frozen }
]);
const mintRent = await connection.getMinimumBalanceForRentExemption(mintLength);
const createMintAccountInstruction = SystemProgram.createAccount({
fromPubkey: feePayer.publicKey, // Account funding the new mint account.
newAccountPubkey: mint.publicKey, // New mint account to create.
space: mintLength, // Account size in bytes for the mint plus DefaultAccountState.
lamports: mintRent, // Lamports funding the mint account rent.
programId: new PublicKey(TOKEN_2022_PROGRAM_ADDRESS) // Program that owns the mint account.
});
const initializeDefaultStateInstruction =
getInitializeDefaultAccountStateInstruction({
mint: mint.publicKey, // Mint account that stores the DefaultAccountState extension.
state: AccountState.Frozen // Default state assigned to new token accounts.
});
const initializeMintInstruction = getInitializeMint2Instruction({
mint: mint.publicKey, // Mint account to initialize.
decimals: 0, // Number of decimals for the token.
mintAuthority: feePayer.publicKey.toBase58(), // Authority allowed to mint new tokens.
freezeAuthority: feePayer.publicKey.toBase58() // Authority allowed to freeze token accounts.
});
await sendAndConfirmTransaction(
connection,
new Transaction({
feePayer: feePayer.publicKey,
blockhash: latestBlockhash.blockhash,
lastValidBlockHeight: latestBlockhash.lastValidBlockHeight
}).add(
createMintAccountInstruction,
initializeDefaultStateInstruction,
initializeMintInstruction
),
[feePayer, mint]
);
const [tokenAccount] = await findAssociatedTokenPda({
mint: mint.publicKey.toBase58(),
owner: feePayer.publicKey.toBase58(),
tokenProgram: TOKEN_2022_PROGRAM_ADDRESS
});
await sendAndConfirmTransaction(
connection,
new Transaction().add(
getCreateAssociatedTokenInstruction({
payer: feePayer, // Account funding the associated token account creation.
ata: tokenAccount, // Associated token account address to create.
owner: feePayer.publicKey, // Owner of the token account.
mint: mint.publicKey // Mint for the associated token account.
})
),
[feePayer],
{
commitment: "confirmed"
}
);
const readDefaultAccountState = async () => {
const mintAccountInfo = await connection.getAccountInfo(
mint.publicKey,
"confirmed"
);
const mintAccount = getMintDecoder().decode(mintAccountInfo!.data);
return (unwrapOption(mintAccount.extensions) ?? []).find(
(extension) => extension.__kind === "DefaultAccountState"
);
};
const readTokenAccountState = async () => {
const tokenAccountInfo = await connection.getAccountInfo(
new PublicKey(tokenAccount),
"confirmed"
);
return getTokenDecoder().decode(tokenAccountInfo!.data).state;
};
const tokenAccountStateBeforeUpdate = await readTokenAccountState();
const defaultAccountStateBeforeUpdate = await readDefaultAccountState();
const updateDefaultStateInstruction = getUpdateDefaultAccountStateInstruction({
mint: mint.publicKey, // Mint account that stores the DefaultAccountState extension.
state: AccountState.Initialized, // New default state assigned to later token accounts.
freezeAuthority: feePayer // Freeze authority authorized to update the default state.
});
await sendAndConfirmTransaction(
connection,
new Transaction().add(updateDefaultStateInstruction),
[feePayer],
{
commitment: "confirmed"
}
);
const tokenAccountStateAfterUpdate = await readTokenAccountState();
const defaultAccountStateAfterUpdate = await readDefaultAccountState();
console.log("Mint Address:", mint.publicKey.toBase58());
console.log(
"Default Account State Before Update:",
defaultAccountStateBeforeUpdate
);
console.log(
"Token Account State Before Update:",
getTokenAccountStateLabel(tokenAccountStateBeforeUpdate)
);
console.log(
"Default Account State After Update:",
defaultAccountStateAfterUpdate
);
console.log(
"Existing Token Account State After Update (unchanged):",
getTokenAccountStateLabel(tokenAccountStateAfterUpdate)
);
Rust
Add these dependencies toCargo.toml (tested with Rust 1.89, 1.98, and 1.99):
Cargo.toml
[dependencies]
anyhow = "1"
tokio = { version = "1", features = ["full"] }
solana-client = "2"
solana-commitment-config = "2"
solana-sdk = "2"
solana-system-interface = "1"
spl-token-2022-interface = "1"
spl-associated-token-account-interface = "1"
Rust
use anyhow::Result;
use solana_client::nonblocking::rpc_client::RpcClient;
use solana_commitment_config::CommitmentConfig;
use solana_sdk::{
signature::{Keypair, Signer},
transaction::Transaction,
};
use solana_system_interface::instruction::create_account;
use spl_associated_token_account_interface::{
address::get_associated_token_address_with_program_id,
instruction::create_associated_token_account,
};
use spl_token_2022_interface::{
extension::{
default_account_state::{
instruction::{initialize_default_account_state, update_default_account_state},
DefaultAccountState,
},
BaseStateWithExtensions, ExtensionType, StateWithExtensions,
},
instruction::initialize_mint,
state::{Account, AccountState, Mint},
ID as TOKEN_2022_PROGRAM_ID,
};
fn get_account_state_label(state: AccountState) -> &'static str {
match state {
AccountState::Frozen => "Frozen",
AccountState::Initialized => "Initialized",
AccountState::Uninitialized => "Uninitialized",
}
}
#[tokio::main]
async fn main() -> Result<()> {
let client = RpcClient::new_with_commitment(
String::from("http://localhost:8899"),
CommitmentConfig::confirmed(),
);
let fee_payer = Keypair::new();
let airdrop_signature = client
.request_airdrop(&fee_payer.pubkey(), 5_000_000_000)
.await?;
loop {
let confirmed = client.confirm_transaction(&airdrop_signature).await?;
if confirmed {
break;
}
}
let mint = Keypair::new();
let mint_space =
ExtensionType::try_calculate_account_len::<Mint>(&[ExtensionType::DefaultAccountState])?;
let mint_rent = client
.get_minimum_balance_for_rent_exemption(mint_space)
.await?;
let create_mint_account_instruction = create_account(
&fee_payer.pubkey(), // Account funding the new mint account.
&mint.pubkey(), // New mint account to create.
mint_rent, // Lamports funding the mint account rent.
mint_space as u64, // Account size in bytes for the mint plus DefaultAccountState.
&TOKEN_2022_PROGRAM_ID, // Program that owns the mint account.
);
let initialize_default_account_state_instruction = initialize_default_account_state(
&TOKEN_2022_PROGRAM_ID, // Token program that owns the mint.
&mint.pubkey(), // Mint account that stores the DefaultAccountState extension.
&AccountState::Frozen, // Default state assigned to new token accounts.
)?;
let initialize_mint_instruction = initialize_mint(
&TOKEN_2022_PROGRAM_ID, // Program that owns the mint account.
&mint.pubkey(), // Mint account to initialize.
&fee_payer.pubkey(), // Authority allowed to mint new tokens.
Some(&fee_payer.pubkey()), // Authority allowed to freeze token accounts.
0, // Number of decimals for the token.
)?;
let create_mint_transaction = Transaction::new_signed_with_payer(
&[
create_mint_account_instruction,
initialize_default_account_state_instruction,
initialize_mint_instruction,
],
Some(&fee_payer.pubkey()),
&[&fee_payer, &mint],
client.get_latest_blockhash().await?,
);
client
.send_and_confirm_transaction(&create_mint_transaction)
.await?;
let token_account = get_associated_token_address_with_program_id(
&fee_payer.pubkey(),
&mint.pubkey(),
&TOKEN_2022_PROGRAM_ID,
);
let create_token_account_transaction = Transaction::new_signed_with_payer(
&[create_associated_token_account(
&fee_payer.pubkey(), // Account funding the associated token account creation.
&fee_payer.pubkey(), // Owner of the token account.
&mint.pubkey(), // Mint for the associated token account.
&TOKEN_2022_PROGRAM_ID, // Token program that owns the token account.
)],
Some(&fee_payer.pubkey()),
&[&fee_payer],
client.get_latest_blockhash().await?,
);
client
.send_and_confirm_transaction(&create_token_account_transaction)
.await?;
let token_account_data_before_update = client.get_account(&token_account).await?;
let token_account_state_before_update =
StateWithExtensions::<Account>::unpack(&token_account_data_before_update.data)?;
let mint_account_before_update = client.get_account(&mint.pubkey()).await?;
let mint_state_before_update =
StateWithExtensions::<Mint>::unpack(&mint_account_before_update.data)?;
let default_account_state_before_update = mint_state_before_update
.get_extension::<DefaultAccountState>()?;
let update_default_account_state_instruction = update_default_account_state(
&TOKEN_2022_PROGRAM_ID, // Token program that owns the mint.
&mint.pubkey(), // Mint account that stores the DefaultAccountState extension.
&fee_payer.pubkey(), // Freeze authority authorized to update the default state.
&[&fee_payer.pubkey()], // Additional multisig signers.
&AccountState::Initialized, // New default state assigned to later token accounts.
)?;
let update_default_account_state_transaction = Transaction::new_signed_with_payer(
&[update_default_account_state_instruction],
Some(&fee_payer.pubkey()),
&[&fee_payer],
client.get_latest_blockhash().await?,
);
client
.send_and_confirm_transaction(&update_default_account_state_transaction)
.await?;
let token_account_data_after_update = client.get_account(&token_account).await?;
let token_account_state_after_update =
StateWithExtensions::<Account>::unpack(&token_account_data_after_update.data)?;
let mint_account_after_update = client.get_account(&mint.pubkey()).await?;
let mint_state_after_update =
StateWithExtensions::<Mint>::unpack(&mint_account_after_update.data)?;
let default_account_state_after_update =
mint_state_after_update.get_extension::<DefaultAccountState>()?;
println!("Mint Address: {}", mint.pubkey());
println!(
"Default Account State Before Update: {:?}",
default_account_state_before_update
);
println!(
"Token Account State Before Update: {}",
get_account_state_label(token_account_state_before_update.base.state)
);
println!(
"Default Account State After Update: {:?}",
default_account_state_after_update
);
println!(
"Existing Token Account State After Update (unchanged): {}",
get_account_state_label(token_account_state_after_update.base.state)
);
Ok(())
}
Web3.js (legacy)
These examples use legacy@solana/web3.js with @solana/spl-token.
@solana/web3.js 1.x is no longer actively maintainedIt receives critical fixes only. Use Kit for new code. To migrate existing code, follow the Kit upgrade guide, or the web3.js v3 migration guide to keep the class-based API.To run this code, install 1.x with npm install @solana/web3.js@1. A plain npm install @solana/web3.js installs v3, which this code doesn’t run on.Terminal
npm install @solana/web3.js@1 @solana/spl-token
Instructions
import {
Connection,
Keypair,
sendAndConfirmTransaction,
SystemProgram,
Transaction,
LAMPORTS_PER_SOL
} from "@solana/web3.js";
import {
AccountState,
ASSOCIATED_TOKEN_PROGRAM_ID,
createAssociatedTokenAccountInstruction,
createInitializeDefaultAccountStateInstruction,
createInitializeMintInstruction,
createUpdateDefaultAccountStateInstruction,
ExtensionType,
getAccount,
getAssociatedTokenAddressSync,
getDefaultAccountState,
getMint,
getMintLen,
TOKEN_2022_PROGRAM_ID
} from "@solana/spl-token";
const connection = new Connection("http://localhost:8899", "confirmed");
const latestBlockhash = await connection.getLatestBlockhash();
const feePayer = Keypair.generate();
const getTokenAccountStateLabel = (account: {
isInitialized: boolean;
isFrozen: boolean;
}) => {
if (account.isFrozen) return "Frozen";
if (account.isInitialized) return "Initialized";
return "Uninitialized";
};
const airdropSignature = await connection.requestAirdrop(
feePayer.publicKey,
5 * LAMPORTS_PER_SOL
);
await connection.confirmTransaction({
blockhash: latestBlockhash.blockhash,
lastValidBlockHeight: latestBlockhash.lastValidBlockHeight,
signature: airdropSignature
});
const extensions = [ExtensionType.DefaultAccountState];
const mint = Keypair.generate();
const mintLength = getMintLen(extensions);
const mintRent = await connection.getMinimumBalanceForRentExemption(mintLength);
const createMintAccountInstruction = SystemProgram.createAccount({
fromPubkey: feePayer.publicKey, // Account funding the new mint account.
newAccountPubkey: mint.publicKey, // New mint account to create.
space: mintLength, // Account size in bytes for the mint plus DefaultAccountState.
lamports: mintRent, // Lamports funding the mint account rent.
programId: TOKEN_2022_PROGRAM_ID // Program that owns the mint account.
});
const initializeDefaultStateInstruction =
createInitializeDefaultAccountStateInstruction(
mint.publicKey, // Mint account that stores the DefaultAccountState extension.
AccountState.Frozen, // Default state assigned to new token accounts.
TOKEN_2022_PROGRAM_ID // Token program that owns the mint.
);
const initializeMintInstruction = createInitializeMintInstruction(
mint.publicKey, // Mint account to initialize.
0, // Number of decimals for the token.
feePayer.publicKey, // Authority allowed to mint new tokens.
feePayer.publicKey, // Authority allowed to freeze token accounts.
TOKEN_2022_PROGRAM_ID // Program that owns the mint account.
);
await sendAndConfirmTransaction(
connection,
new Transaction({
feePayer: feePayer.publicKey,
blockhash: latestBlockhash.blockhash,
lastValidBlockHeight: latestBlockhash.lastValidBlockHeight
}).add(
createMintAccountInstruction,
initializeDefaultStateInstruction,
initializeMintInstruction
),
[feePayer, mint]
);
const tokenAccount = getAssociatedTokenAddressSync(
mint.publicKey,
feePayer.publicKey,
false,
TOKEN_2022_PROGRAM_ID,
ASSOCIATED_TOKEN_PROGRAM_ID
);
await sendAndConfirmTransaction(
connection,
new Transaction().add(
createAssociatedTokenAccountInstruction(
feePayer.publicKey, // Account funding the associated token account creation.
tokenAccount, // Associated token account address to create.
feePayer.publicKey, // Owner of the token account.
mint.publicKey, // Mint for the associated token account.
TOKEN_2022_PROGRAM_ID, // Token program that owns the token account.
ASSOCIATED_TOKEN_PROGRAM_ID // Associated Token Program that creates the account.
)
),
[feePayer],
{
commitment: "confirmed"
}
);
const tokenAccountBeforeUpdate = await getAccount(
connection,
tokenAccount,
"confirmed",
TOKEN_2022_PROGRAM_ID
);
const mintAccountBeforeUpdate = await getMint(
connection,
mint.publicKey,
"confirmed",
TOKEN_2022_PROGRAM_ID
);
const defaultAccountStateBeforeUpdate = getDefaultAccountState(
mintAccountBeforeUpdate
);
const updateDefaultStateInstruction =
createUpdateDefaultAccountStateInstruction(
mint.publicKey, // Mint account that stores the DefaultAccountState extension.
AccountState.Initialized, // New default state assigned to later token accounts.
feePayer.publicKey, // Freeze authority authorized to update the default state.
[], // Additional multisig signers.
TOKEN_2022_PROGRAM_ID // Token program that owns the mint.
);
await sendAndConfirmTransaction(
connection,
new Transaction().add(updateDefaultStateInstruction),
[feePayer],
{
commitment: "confirmed"
}
);
const tokenAccountAfterUpdate = await getAccount(
connection,
tokenAccount,
"confirmed",
TOKEN_2022_PROGRAM_ID
);
const mintAccountAfterUpdate = await getMint(
connection,
mint.publicKey,
"confirmed",
TOKEN_2022_PROGRAM_ID
);
const defaultAccountStateAfterUpdate = getDefaultAccountState(
mintAccountAfterUpdate
);
console.log("Mint Address:", mint.publicKey.toBase58());
console.log(
"Default Account State Before Update:",
defaultAccountStateBeforeUpdate
);
console.log(
"Token Account State Before Update:",
getTokenAccountStateLabel(tokenAccountBeforeUpdate)
);
console.log(
"Default Account State After Update:",
defaultAccountStateAfterUpdate
);
console.log(
"Existing Token Account State After Update (unchanged):",
getTokenAccountStateLabel(tokenAccountAfterUpdate)
);