### Install spl-feature-proposal CLI Source: https://www.solana-program.com/docs/feature-proposal Install the command-line utility for managing feature proposals. Ensure Rust is installed. ```bash cargo install spl-feature-proposal-cli ``` -------------------------------- ### Install spl-token-wrap CLI Source: https://www.solana-program.com/docs/token-wrap Install the spl-token-wrap command-line utility from crates.io. Alternatively, build from source. ```bash $ cargo install spl-token-wrap-cli ``` ```bash $ git clone https://github.com/solana-program/token-wrap.git $ cd token-wrap $ cargo build --bin spl-token-wrap ``` -------------------------------- ### Install spl-token CLI Source: https://www.solana-program.com/docs/token Install the spl-token command-line utility for experimenting with SPL tokens. Requires Rust to be installed. ```bash $ cargo install spl-token-cli ``` -------------------------------- ### Install spl-single-pool CLI Source: https://www.solana-program.com/docs/single-pool Install the command-line program for interacting with the single pool program. Requires Rust to be installed. ```bash $ cargo install spl-single-pool-cli ``` -------------------------------- ### Install spl-stake-pool CLI Source: https://www.solana-program.com/docs/stake-pool/cli Installs the stake pool command-line utility. Ensure Rust is installed before running this command. ```bash cargo install spl-stake-pool-cli ``` -------------------------------- ### Initiate Feature Proposal Source: https://www.solana-program.com/docs/feature-proposal Use this command to start the feature proposal process by submitting a JSON configuration file. It outputs key addresses and token information. Ensure the feature proposal JSON is correctly configured. ```bash $ spl-feature-proposal propose feature-proposal.json ``` -------------------------------- ### Create Associated Token Account Instruction Source: https://www.solana-program.com/docs/token-2022/wallet This example demonstrates how to create an instruction for a new associated token account. Pass the correct token program ID, which can be either `Tokenz...` or `Tokenkeg...`. ```typescript import { Connection, PublicKey } from '@solana/web3.js'; import { createAssociatedTokenAccountInstruction } from '@solana/spl-token'; const tokenProgramId = new PublicKey( 'TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb' ); // either `Tokenz...` or `Tokenkeg...` const wallet = new PublicKey('11111111111111111111111111111111'); // insert your key const mint = new PublicKey('11111111111111111111111111111111'); // insert mint key const associatedTokenAccount = associatedTokenAccountAddress(mint, wallet, tokenProgramId); const instruction = createAssociatedTokenAccountInstruction( wallet, // payer associatedTokenAccount, // associated token account wallet, // owner tokenProgramId, // token program id ); ``` -------------------------------- ### Start Test Validator with Account Directory Source: https://www.solana-program.com/docs/token-2022/wallet Initialize a local solana-test-validator using saved account data from a specified directory. This allows for quick restarts of the test environment with pre-configured accounts. ```bash $ solana-test-validator -r --account-dir test-accounts ``` -------------------------------- ### Setup Local Test Validator Source: https://www.solana-program.com/docs/stake-pool/quickstart Sets up a local Solana test validator with a specified number of validator vote accounts. Outputs a file containing the public keys of these accounts. Fails if another validator is already running. ```bash ./setup-test-validator.sh 10 local_validators.txt ``` -------------------------------- ### Get Solana CLI Configuration Source: https://www.solana-program.com/docs/single-pool View the current configuration for the Solana command-line tool, including RPC URL and keypair path. ```bash $ solana config get ``` -------------------------------- ### Encrypted Account Balance Example Source: https://www.solana-program.com/docs/confidential-balances/overview Illustrates how an account balance can be encrypted using the owner's public key before being stored on-chain. ```rust Account { mint: Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB, owner: 5vBrLAPeMjJr9UfssGbjUaBmWtrXTg2vZuMN6L4c8HE6, // pubkey_owner amount: PKE::encrypt(pubkey_owner, 50), // amount encrypted ... } ``` -------------------------------- ### Reallocate Account for Extensions Source: https://www.solana-program.com/docs/token-2022/extensions This example shows how the CLI automatically handles reallocation when enabling extensions like 'required transfer memos' on an account that doesn't have sufficient space. The 'Reallocate' instruction is added implicitly if needed. ```CLIJS $ spl-token create-account EbPBt3XkCb9trcV4c8fidhrvoeURbDbW87Acustzyi8N Creating account 4Uzz67txwYbfYpF8r5UGEMYJwhPAYQ5eFUY89KTYc2bL Signature: 57wZHDaQtSzszDkusrnozZNj5PemQhpqHMEFLWFKpqASCErcDuBuYuEky5g3evHtkjMrKgh1s3aEap1L8y5UhW5W $ spl-token enable-required-transfer-memos 4Uzz67txwYbfYpF8r5UGEMYJwhPAYQ5eFUY89KTYc2bL Signature: 5MnWtrhMK32zkbacDMwBNft48VAUpr4EoRM87hkT9AFYvPgPEU7V7ERV6gdfb3kASri4wnUnr13hNKuYJ66pD8Fs ``` -------------------------------- ### Finding all token accounts for a specific mint Source: https://www.solana-program.com/docs/token This example demonstrates how to use the `getProgramAccounts` JSON RPC method to find all token accounts associated with a particular mint address. It filters accounts by data size and then by the mint address offset. ```APIDOC ## getProgramAccounts (Find Token Accounts by Mint) ### Description Fetches all token accounts for a specific mint address. ### Method POST ### Endpoint `http://api.mainnet-beta.solana.com` ### Parameters #### Request Body ```json { "jsonrpc": "2.0", "id": 1, "method": "getProgramAccounts", "params": [ "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA", { "encoding": "jsonParsed", "filters": [ { "dataSize": 165 }, { "memcmp": { "offset": 0, "bytes": "[MINT_ADDRESS]" } } ] } ] } ``` ### Request Example ```json { "jsonrpc": "2.0", "id": 1, "method": "getProgramAccounts", "params": [ "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA", { "encoding": "jsonParsed", "filters": [ { "dataSize": 165 }, { "memcmp": { "offset": 0, "bytes": "TESTpKgj42ya3st2SQTKiANjTBmncQSCqLAZGcSPLGM" } } ] } ] } ``` ### Response #### Success Response (200) Returns a list of token accounts matching the criteria. #### Response Example ```json { "jsonrpc": "2.0", "result": [ { "account": { "data": { "programId": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA", "layout": { "mint": "TESTpKgj42ya3st2SQTKiANjTBmncQSCqLAZGcSPLGM", "owner": "[OWNER_ADDRESS]", "amount": "[TOKEN_AMOUNT]", "delegate": { "account": "[DELEGATE_ADDRESS]", "amount": "[DELEGATED_AMOUNT]" }, "state": "initialized", "isNative": 0, "rentExemptBase100k": true } }, "executable": false, "lamports": 2039280, "owner": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA", "rentEpoch": 340 }, "pubkey": "[TOKEN_ACCOUNT_ADDRESS]" } ], "id": 1 } ``` ``` -------------------------------- ### Run Solana Test Validator Locally Source: https://www.solana-program.com/docs/stake-pool/cli Starts a local Solana validator for testing purposes. This command clones an upgradeable program from mainnet-beta and sets a reduced number of slots per epoch for faster testing. It also configures the Solana CLI to connect to this local validator. ```bash solana-test-validator --clone-upgradeable-program SPoo1Ku8WFXoNDMHPsrGSTSG1Y47rzgn41SLUNakuHy --url mainnet-beta --slots-per-epoch 32 solana config set --url http://127.0.0.1:8899 ``` -------------------------------- ### Attempt Minting with Insufficient Signers Source: https://www.solana-program.com/docs/token Demonstrates that attempting to mint tokens when the multisig authority is required, but only one signer is provided, will fail due to a missing signature. This highlights the security of the multisig setup. ```bash $ spl-token mint 4VNVRJetwapjwYU8jf4qPgaCeD76wyz8DuNj8yMCQ62o 1 EX8zyi2ZQUuoYtXd4MKmyHYLTjqFdWeuoTHcsTdJcKHC \ --owner 46ed77fd4WTN144q62BwjU2B3ogX3Xmmc8PT5Z3Xc2re \ --multisig-signer signer-1.json ``` ```text Minting 1 tokens Token: 4VNVRJetwapjwYU8jf4qPgaCeD76wyz8DuNj8yMCQ62o Recipient: EX8zyi2ZQUuoYtXd4MKmyHYLTjqFdWeuoTHcsTdJcKHC RPC response error -32002: Transaction simulation failed: Error processing Instruction 0: missing required signature for instruction ``` -------------------------------- ### Finding all token accounts for a wallet Source: https://www.solana-program.com/docs/token This example shows how to use the `getProgramAccounts` JSON RPC method to find all token accounts owned by a specific wallet address. It filters accounts by data size and then by the owner address offset. ```APIDOC ## getProgramAccounts (Find Token Accounts by Owner) ### Description Fetches all token accounts owned by a specific wallet address. ### Method POST ### Endpoint `http://api.mainnet-beta.solana.com` ### Parameters #### Request Body ```json { "jsonrpc": "2.0", "id": 1, "method": "getProgramAccounts", "params": [ "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA", { "encoding": "jsonParsed", "filters": [ { "dataSize": 165 }, { "memcmp": { "offset": 32, "bytes": "[OWNER_WALLET_ADDRESS]" } } ] } ] } ``` ### Request Example ```json { "jsonrpc": "2.0", "id": 1, "method": "getProgramAccounts", "params": [ "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA", { "encoding": "jsonParsed", "filters": [ { "dataSize": 165 }, { "memcmp": { "offset": 32, "bytes": "vines1vzrYbzLMRdu58ou5XTby4qAqVRLmqo36NKPTg" } } ] } ] } ``` ### Response #### Success Response (200) Returns a list of token accounts owned by the specified wallet. #### Response Example ```json { "jsonrpc": "2.0", "result": [ { "account": { "data": { "programId": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA", "layout": { "mint": "[MINT_ADDRESS]", "owner": "vines1vzrYbzLMRdu58ou5XTby4qAqVRLmqo36NKPTg", "amount": "[TOKEN_AMOUNT]", "delegate": { "account": "[DELEGATE_ADDRESS]", "amount": "[DELEGATED_AMOUNT]" }, "state": "initialized", "isNative": 0, "rentExemptBase100k": true } }, "executable": false, "lamports": 2039280, "owner": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA", "rentEpoch": 340 }, "pubkey": "[TOKEN_ACCOUNT_ADDRESS]" } ], "id": 1 } ``` ``` -------------------------------- ### Initialize Mint with MintCloseAuthority Extension Source: https://www.solana-program.com/docs/token-2022/onchain Calculate space, allocate, and initialize the `MintCloseAuthority` extension before the mint itself. This is specific to Token-2022. ```rust use spl_token_2022::{extension::ExtensionType, instruction::*, state::Mint}; use solana_sdk::{system_instruction, transaction::Transaction}; // Calculate the space required using the `ExtensionType` let space = ExtensionType::try_calculate_account_len::(&[ExtensionType::MintCloseAuthority]).unwrap(); // get the Rent object and calculate the rent required let rent_required = rent.minimum_balance(space); // and then create the account using those parameters let create_instruction = system_instruction::create_account(&payer.pubkey(), mint_pubkey, rent_required, space, token_program_id); // Important: you must initialize the mint close authority *BEFORE* initializing the mint, // and only when working with Token-2022, since the instruction is unsupported by Token. let initialize_close_authority_instruction = initialize_mint_close_authority(token_program_id, mint_pubkey, Some(close_authority)).unwrap(); let initialize_mint_instruction = initialize_mint(token_program_id, mint_pubkey, mint_authority_pubkey, freeze_authority, 9).unwrap(); // Make the transaction with all of these instructions let create_mint_transaction = Transaction::new(&[create_instruction, initialize_close_authority_instruction, initialize_mint_instruction], Some(&payer.pubkey)); // Sign it and send it however you want! ``` -------------------------------- ### Get Recent Blockhash Source: https://www.solana-program.com/docs/token-wrap Obtain a recent blockhash to be used for all signers in the transaction. This blockhash must be consistent across all signing steps. ```bash $ solana block Blockhash: E12VZaDq99G7Tg38Jr7U2VWRCmxjzWzsow8dPMhA47Rm ⬆️ send this to all signers ``` -------------------------------- ### Initialize Token Metadata Source: https://www.solana-program.com/docs/token-2022/extensions After creating a token with the metadata extension enabled, use this command to initialize the token's metadata with a name, symbol, and URI. Ensure you sign with the mint authority. ```bash $ spl-token initialize-metadata 5K8RVdjpY3CHujyKjQ7RkyiCJqTG8Kba9krNfpZnmvpS MyTokenName TOKEN http://my.token --update-authority 3pGiHDDek35npQuyWQ7FGcWxqJdHvVPDHDDmBFs2YxQj Signature: 2H16XtBqdwSbvvq8g5o2jhy4TknP6zgt71KHawEdyPvNuvusQrV4dPccUrMqjFeNTbk75AtzmzUVueH3yWiTjBCG ``` -------------------------------- ### Encrypted Transfer Amount Example Source: https://www.solana-program.com/docs/confidential-balances/overview Shows how a transfer amount can be encrypted under the sender's public key to maintain confidentiality during transactions. ```rust Transfer { amount: PKE::encrypt(pubkey_owner, 10), } ``` -------------------------------- ### Dump Token-2022 Program to Local File Source: https://www.solana-program.com/docs/token-2022/onchain Download the Token-2022 program binary (`spl_token_2022.so`) to your local fixtures directory using the Solana CLI. This is necessary for program testing environments that do not include Token-2022 by default. ```bash $ solana program dump TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb spl_token_2022.so ``` -------------------------------- ### Calculate Stake Deposit Fee Source: https://www.solana-program.com/docs/stake-pool/cli This example demonstrates the calculation for the total fee when depositing a stake account, considering both stake delegation and rent exemption fees. ```plaintext total_fee = stake_delegation * stake_deposit_fee + rent_exemption * sol_deposit_fee total_fee = 10 * 1% + .00228288 * 5% total_fee = 0.100114144 ``` -------------------------------- ### Update Instruction Processor Accounts Source: https://www.solana-program.com/docs/token-2022/onchain Modify your instruction processor to include new accounts for token programs. This example shows how to extract additional token program IDs. ```rust let account_info_iter = &mut accounts.iter(); let swap_info = next_account_info(account_info_iter)?; let authority_info = next_account_info(account_info_iter)?; let user_transfer_authority_info = next_account_info(account_info_iter)?; let source_info = next_account_info(account_info_iter)?; let swap_source_info = next_account_info(account_info_iter)?; let swap_destination_info = next_account_info(account_info_iter)?; let destination_info = next_account_info(account_info_iter)?; let pool_mint_info = next_account_info(account_info_iter)?; let pool_fee_account_info = next_account_info(account_info_iter)?; let token_program_info = next_account_info(account_info_iter)? ``` ```rust let account_info_iter = &mut accounts.iter(); let swap_info = next_account_info(account_info_iter)?; let authority_info = next_account_info(account_info_iter)?; let user_transfer_authority_info = next_account_info(account_info_iter)?; let source_info = next_account_info(account_info_iter)?; let swap_source_info = next_account_info(account_info_iter)?; let swap_destination_info = next_account_info(account_info_iter)?; let destination_info = next_account_info(account_info_iter)?; let pool_mint_info = next_account_info(account_info_iter)?; let pool_fee_account_info = next_account_info(account_info_iter)?; let source_token_program_info = next_account_info(account_info_iter)?; // added let destination_token_program_info = next_account_info(account_info_iter)?; // added let pool_token_program_info = next_account_info(account_info_iter)?; // renamed ``` -------------------------------- ### Range Proof Example: Invalid Transfer Source: https://www.solana-program.com/docs/confidential-balances/overview Demonstrates that a range proof verification will fail if the encrypted value falls outside the specified bounds. This ensures that invalid amounts are rejected. ```rust let ct = PKE::encrypt(pk, x); let public_data = (pk, ct); assert_eq!(RangeProof::verify(public_data, proof), false); ``` -------------------------------- ### Create Token with Group Configurations Source: https://www.solana-program.com/docs/token-2022/extensions Use this command to create a new token with group configurations enabled. After creation, initialize the group configurations by specifying the maximum size. ```bash $ spl-token --program-id TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb create-token --enable-group Creating token 812A34SxxYx9KqFwUNAuW7Wpwtmuj2pc5u1TGQcvPnj3 under program TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb To initialize group configurations inside the mint, please run `spl-token initialize-group 812A34SxxYx9KqFwUNAuW7Wpwtmuj2pc5u1TGQcvPnj3 `, and sign with the mint authority. Address: 812A34SxxYx9KqFwUNAuW7Wpwtmuj2pc5u1TGQcvPnj3 Decimals: 9 Signature: 2BZH8KE7zVcBj7Mmnu6uCM9NT4ey7qHasZmEk6Bt3tyx1wKCXS3JtcgEvrXXEMFB5numQgA9wvR67o2Z4YQdEw7m $ spl-token initialize-group 812A34SxxYx9KqFwUNAuW7Wpwtmuj2pc5u1TGQcvPnj3 12 --update-authority 3pGiHDDek35npQuyWQ7FGcWxqJdHvVPDHDDmBFs2YxQj Signature: 2H16XtBqdwSbvvq8g5o2jhy4TknP6zgt71KHawEdyPvNuvusQrV4dPccUrMqjFeNTbk75AtzmzUVueH3yWiTjBCG ``` -------------------------------- ### Range Proof Example: Valid Transfer Source: https://www.solana-program.com/docs/confidential-balances/overview Demonstrates generating and verifying a range proof for a ciphertext that encrypts a value within the specified bounds. This is used to ensure a transfer amount is valid. ```rust let ct = PKE::encrypt(pk, x); let public_data = (pk, ct); let private_data = (sk, x); let proof = RangeProof::prove(public_data, private_data); assert_eq!(RangeProof::verify(public_data, proof), true); ``` -------------------------------- ### Create Token with Member Configurations Source: https://www.solana-program.com/docs/token-2022/extensions Enables the member extension for a token, allowing configurations like group address and member number to be stored directly in the mint. Requires the member-pointer extension to be initialized during mint creation. ```bash $ spl-token --program-id TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb create-token --enable-member Creating token 9uyqmf9Ued4yQKi4hXT5wMzPF5Nv1S6skAjkjxcCaAyV under program TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb To initialize group member configurations inside the mint, please run `spl-token initialize-member 9uyqmf9Ued4yQKi4hXT5wMzPF5Nv1S6skAjkjxcCaAyV`, and sign with the mint authority. Address: 9uyqmf9Ued4yQKi4hXT5wMzPF5Nv1S6skAjkjxcCaAyV Decimals: 9 Signature: 2BZH8KE7zVcBj7Mmnu6uCM9NT4ey7qHasZmEk6Bt3tyx1wKCXS3JtcgEvrXXEMFB5numQgA9wvR67o2Z4YQdEw7m $ spl-token initialize-member 9uyqmf9Ued4yQKi4hXT5wMzPF5Nv1S6skAjkjxcCaAyV --update-authority 3pGiHDDek35npQuyWQ7FGcWxqJdHvVPDHDDmBFs2YxQj Signature: 2H16XtBqdwSbvvq8g5o2jhy4TknP6zgt71KHawEdyPvNuvusQrV4dPccUrMqjFeNTbk75AtzmzUVueH3yWiTjBCG ``` -------------------------------- ### Build and Deploy Transfer Hook Program Source: https://www.solana-program.com/docs/token-2022/extensions These commands demonstrate how to build a Solana program using Cargo and deploy it to the network. This is a prerequisite for managing transfer hooks. ```bash $ cargo build-sbf $ solana program deploy target/deploy/spl-transfer-hook-example.so ``` -------------------------------- ### Create Token Account Source: https://www.solana-program.com/docs/token Create a new account to hold a balance of a specific token. This account is initially empty. ```bash $ spl-token create-account AQoKYV7tYpTrFZN6P5oUufbQKAUr9mNYGe1TTJC9wajM Creating account 7UX2i7SucgLMQcfZ75s3VXmZZY4YRUyJN9X1RgfMoDUi Signature: 42Sa5eK9dMEQyvD9GMHuKxXf55WLZ7tfjabUKDhNoZRAxj9MsnN7omriWMEHXLea3aYpjZ862qocRLVikvkHkyfy ``` -------------------------------- ### View Solana CLI Configuration Source: https://www.solana-program.com/docs/token Display the current configuration for the Solana command-line tool, including RPC URL and keypair path. ```bash $ solana config get Config File: ${HOME}/.config/solana/cli/config.yml RPC URL: https://api.mainnet-beta.solana.com WebSocket URL: wss://api.mainnet-beta.solana.com/ (computed) Keypair Path: ${HOME}/.config/solana/id.json ``` -------------------------------- ### View Solana CLI Configuration Source: https://www.solana-program.com/docs/stake-pool/cli Displays the current configuration settings for the Solana CLI, including RPC URL and keypair path. ```bash solana config get # Config File: ${HOME}/.config/solana/cli/config.yml # RPC URL: https://api.mainnet-beta.solana.com # WebSocket URL: wss://api.mainnet-beta.solana.com/ (computed) # Keypair Path: ${HOME}/.config/solana/id.json ``` -------------------------------- ### Create and Delegate Stake Account Source: https://www.solana-program.com/docs/stake-pool/cli Generates a new stake account keypair, creates the stake account with initial SOL, and delegates it to a specified validator. Ensure the stake account is active before depositing into the pool. ```bash solana-keygen new --no-passphrase -o stake-account.json # Generating a new keypair # Wrote new keypair to stake-account.json # ============================================================================ # pubkey: 97wBBiLVA7fUViEew8yV8R6tTdKithZDVz8LHLfF9sTJ # ============================================================================ # Save this seed phrase to recover your new keypair: # ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ # ============================================================================ ``` ```bash solana create-stake-account stake-account.json 10 # Signature: 5Y9r6MNoqJzVX8TWryAJbdp8i2DvintfxbYWoY6VcLEPgphK2tdydhtJTd3o3dF7QdM2Pg8sBFDZuyNcMag3nPvj ``` ```bash solana delegate-stake 97wBBiLVA7fUViEew8yV8R6tTdKithZDVz8LHLfF9sTJ 38DYMkwYCvsj8TC6cNaEvFHHVDYeWDp1qUgMgyjNqZXk # Signature: 2cDjHXSHjuadGQf1NQpPi43A8R19aCifsY16yTcictKPHcSAXN5TvXZ58nDJwkYs12tuZfTh5WVgAMSvptfrKdPP ``` -------------------------------- ### Create Token with Metadata Pointer Source: https://www.solana-program.com/docs/token-2022/extensions Use this command to create a new token and associate it with an external metadata address. Ensure the metadata address is correctly specified. ```bash $ spl-token --program-id TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb create-token --metadata-address 7N4HggYEJAtCLJdnHGCtFqfxcB5rhQCsQTze3ftYstVj Creating token HFg1FFaj4PqFHmkYrqbZsarNJEZT436aXAXgQFMJihwc under program TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb Address: HFg1FFaj4PqFHmkYrqbZsarNJEZT436aXAXgQFMJihwc Decimals: 9 Signature: 3ug4Ejs16jJgEm1WyBwDDxzh9xqPzQ3a2cmy1hSYiPFcLQi9U12HYF1Dbhzb2bx75SSydfU6W4e11dGUXaPbJqVc ``` -------------------------------- ### Create a Token Account Source: https://www.solana-program.com/docs/token After creating a token, create an account to hold tokens of that type. This command requires the token's address and returns the new account's address and a signature. ```bash $ spl-token create-account 559u4Tdr9umKwft3yHMsnAxohhzkFnUBPAFtibwuZD9z Creating account 7KqpRwzkkeweW5jQoETyLzhvs9rcCj9dVQ1MnzudirsM Signature: sjChze6ecaRtvuQVZuwURyg6teYeiH8ZwT6UTuFNKjrdayQQ3KNdPB7d2DtUZ6McafBfEefejHkJ6MWQEfVHLtC ``` -------------------------------- ### Create Token with Metadata Enabled Source: https://www.solana-program.com/docs/token-2022/extensions This command creates a token and enables the metadata extension, allowing metadata to be stored directly within the mint account. You will need to initialize the metadata separately. ```bash $ spl-token --program-id TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb create-token --enable-metadata Creating token 5K8RVdjpY3CHujyKjQ7RkyiCJqTG8Kba9krNfpZnmvpS under program TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb To initialize metadata inside the mint, please run `spl-token initialize-metadata 5K8RVdjpY3CHujyKjQ7RkyiCJqTG8Kba9krNfpZnmvpS `, and sign with the mint authority Address: 5K8RVdjpY3CHujyKjQ7RkyiCJqTG8Kba9krNfpZnmvpS Decimals: 9 Signature: 2BZH8KE7zVcBj7Mmnu6uCM9NT4ey7qHasZmEk6Bt3tyx1wKCXS3JtcgEvrXXEMFB5numQgA9wvR67o2Z4YQdEw7m ``` -------------------------------- ### Initialize Mint with Close Authority Source: https://www.solana-program.com/docs/token-2022/onchain Sets up a new mint account with a mint close authority. This prevents the mint from being closed, which is important for programs that store mint-related information. ```rust use spl_token_2022::{extension::ExtensionType, instruction::*, state::Mint}; use solana_sdk::{system_instruction, transaction::Transaction}; // Calculate the space required using the `ExtensionType` let space = ExtensionType::try_calculate_account_len::(&[ExtensionType::MintCloseAuthority]).unwrap(); // get the Rent object and calculate the rent required let rent_required = rent.minimum_balance(space); // and then create the account using those parameters let create_instruction = system_instruction::create_account(&payer.pubkey(), mint_pubkey, rent_required, space, token_program_id); // Important: you must initialize the mint close authority *BEFORE* initializing the mint, // and only when working with Token-2020, since the instruction is unsupported by Token. let initialize_close_authority_instruction = initialize_mint_close_authority(token_program_id, mint_pubkey, Some(close_authority)).unwrap(); let initialize_mint_instruction = initialize_mint(token_program_id, mint_pubkey, mint_authority_pubkey, freeze_authority, 9).unwrap(); // Make the transaction with all of these instructions let create_mint_transaction = Transaction::new(&[create_instruction, initialize_close_authority_instruction, initialize_mint_instruction], Some(&payer.pubkey)); ``` -------------------------------- ### Create Token with Default Frozen Accounts Source: https://www.solana-program.com/docs/token-2022/extensions Use this command to create a new token with the freeze authority set and all accounts defaulting to a frozen state. This requires creating the mint and then an associated account. ```bash $ spl-token --program-id TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb create-token --enable-freeze --default-account-state frozen Creating token 8Sqz2zV8TFTnkLtnCdqRkjJsre3GKRwHcZd3juE5jJHf under program TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb Address: 8Sqz2zV8TFTnkLtnCdqRkjJsre3GKRwHcZd3juE5jJHf Decimals: 9 Signature: 5wfYvovguPEbyv2uSWxGt9JcpTWgyuP4hY3wutjS32Ahnoni4qd7gf6sLre855WvT6xLHwrvV7J8bVmXymNU2qUz $ spl-token create-account 8Sqz2zV8TFTnkLtnCdqRkjJsre3GKRwHcZd3juE5jJHf Creating account 6XpKagP1N3K1XnzStufpV5YZ6DksEkQWgLNG9kPpLyvv Signature: 2awxWdQMgv89ew34sEyG361vshB2wPXHHfva5iJ43dWr18f2Pr6awoXfsqYPpyS2eSbH6jhfVY9EUck8iJ4wCSN6 $ spl-token display 6XpKagP1N3K1XnzStufpV5YZ6DksEkQWgLNG9kPpLyvv SPL Token Account Address: 6XpKagP1N3K1XnzStufpV5YZ6DksEkQWgLNG9kPpLyvv Program: TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb Balance: 0 Decimals: 9 Mint: 8Sqz2zV8TFTnkLtnCdqRkjJsre3GKRwHcZd3juE5jJHf Owner: 4SnSuUtJGKvk2GYpBwmEsWG53zTurVM8yXGsoiZQyMJn State: Frozen Delegation: (not set) Close authority: (not set) Extensions: Immutable owner ``` -------------------------------- ### Create Token Mint with Permanent Delegate Enabled Source: https://www.solana-program.com/docs/token-2022/extensions Create a new token mint with the permanent delegate extension enabled. The CLI defaults the permanent delegate to the mint authority. ```bash $ spl-token --program-id TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb create-token --enable-permanent-delegate Creating token 7LUgoQCqhk3VMPhpAnmS1zdCFW4C6cupxgbqWrTwydGx under program TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb Address: 7LUgoQCqhk3VMPhpAnmS1zdCFW4C6cupxgbqWrTwydGx Decimals: 9 Signature: 439yVq2WfUEegAPv5BAkFampBPo696UbZ58RAYCzvUcbcBcxhfThpt1pcdKmiQrurHj65CqmWiHzrfT12BhL3Nxb ``` -------------------------------- ### Initialize Token Account with ImmutableOwner Extension Source: https://www.solana-program.com/docs/token-2022/onchain Calculate space, allocate, and initialize the `ImmutableOwner` extension before the token account itself. This extension is supported by both Token and Token-2022. ```rust use spl_token_2022::{extension::ExtensionType, instruction::*, state::Account}; use solana_sdk::{system_instruction, transaction::Transaction}; // Calculate the space required using the `ExtensionType` let space = ExtensionType::try_calculate_account_len::(&[ExtensionType::ImmutableOwner]).unwrap(); // get the Rent object and calculate the rent required let rent_required = rent.minimum_balance(space); // and then create the account using those parameters let create_instruction = system_instruction::create_account(&payer.pubkey(), account_pubkey, rent_required, space, token_program_id); // Important: you must initialize immutable owner *BEFORE* initializing the account let initialize_immutable_owner_instruction = initialize_immutable_owner(token_program_id, account_pubkey).unwrap(); let initialize_account_instruction = initialize_account(token_program_id, account_pubkey, mint_pubkey, owner_pubkey).unwrap(); // Make the transaction with all of these instructions let create_account_transaction = Transaction::new(&[create_instruction, initialize_immutable_owner_instruction, initialize_account_instruction], Some(&payer.pubkey)); // Sign it and send it however you want! ``` -------------------------------- ### Create Solana Stake Pool Source: https://www.solana-program.com/docs/stake-pool/quickstart Use this command to create a new stake pool with custom parameters such as fees, referral rates, and maximum validators. Ensure all required keypair files are available. ```bash $ ./setup-stake-pool.sh 15 Creating pool + spl-stake-pool create-pool --epoch-fee-numerator 3 --epoch-fee-denominator 1000 --withdrawal-fee-numerator 3 --withdrawal-fee-denominator 1000 --deposit-fee-numerator 3 --deposit-fee-denominator 1000 --referral-fee 50 --max-validators 2350 --pool-keypair keys/stake-pool.json --validator-list-keypair keys/validator-list.json --mint-keypair keys/mint.json --reserve-keypair keys/reserve.json Creating reserve stake 4tvTkLB4X7ahUYZ2NaTohkG3mud4UBBvu9ZEGD4Wk9mt Creating mint BoNneHKDrX9BHjjvSpPfnQyRjsnc9WFH71v8wrgCd7LB Creating associated token account DgyZrAq88bnG1TNRxpgDQzWXpzEurCvfY2ukKFWBvADQ to receive stake pool tokens of mint BoNneHKDrX9BHjjvSpPfnQyRjsnc9WFH71v8wrgCd7LB, owned by 4SnSuUtJGKvk2GYpBwmEsWG53zTurVM8yXGsoiZQyMJn Creating pool fee collection account DgyZrAq88bnG1TNRxpgDQzWXpzEurCvfY2ukKFWBvADQ Signature: 51yf2J6dSGAx42KPs2oTMTV4ufEm1ncAHyLPQ6PNf4sbeMHGqno7BGn2tHkUnrd7PRXiWBbGzCWpJNevYjmoLgn2 Creating stake pool Zg5YBPAk8RqBR9kaLLSoN5C8Uv7nErBz1WC63HTsCPR with validator list 86VZZCuqiz7sDJpFKjQy9c9dZQN9vwDKbYgY8pcwHuaF Signature: 47QHcWMEa5Syg13C3SQRA4n88Y8iLx1f39wJXQAStRUxpt2VD5t6pYgAdruNRHUQt1ZBY8QwbvEC1LX9j3nPrAzn Depositing SOL into stake pool Update not required Using existing associated token account DgyZrAq88bnG1TNRxpgDQzWXpzEurCvfY2ukKFWBvADQ to receive stake pool tokens of mint BoNneHKDrX9BHjjvSpPfnQyRjsnc9WFH71v8wrgCd7LB, owned by 4SnSuUtJGKvk2GYpBwmEsWG53zTurVM8yXGsoiZQyMJn Signature: 4jnS368HcofZ1rUpsGZtmSK9kVxFzJRndSX5VS7eMV3kVgzyg9efA4mcgd2C6BoSNksTmTonRGXTVM1WMywFpiKq ``` -------------------------------- ### Initialize Instruction Interface Source: https://www.solana-program.com/docs/token-2022/onchain The `Initialize` instruction remains unchanged as it does not directly invoke the token programs for token A and B, although it does require the token program for the pool token mint. ```rust /// Initializes a new swap /// /// 0. `[writable, signer]` New Token-swap to create. /// 1. `[]` swap authority derived from `create_program_address(&[Token-swap account])` /// 2. `[]` token_a Account. Must be non zero, owned by swap authority. /// 3. `[]` token_b Account. Must be non zero, owned by swap authority. /// 4. `[writable]` Pool Token Mint. Must be empty, owned by swap authority. /// 5. `[]` Pool Token Account to deposit trading and withdraw fees. /// Must be empty, not owned by swap authority /// 6. `[writable]` Pool Token Account to deposit the initial pool token /// supply. Must be empty, not owned by swap authority. /// 7. `[]` Token program id Initialize { ... } // details omitted ``` -------------------------------- ### Initialize a Single-Validator Stake Pool Source: https://www.solana-program.com/docs/single-pool Create a new single-validator stake pool for a given validator vote account. This action is permissionless. ```bash $ spl-single-pool manage initialize Ammgaa2iZfA745BmZMhkcS27uh87fEVDC6Gm2RXz5hrC ``` -------------------------------- ### Swap Instruction with Mint Accounts Source: https://www.solana-program.com/docs/token-2022/onchain Illustrates the structure of a swap instruction, emphasizing the inclusion of source and destination mint accounts, which is a key difference from the standard `transfer` instruction. ```rust /// Swap the tokens in the pool. /// /// 0. `[]` Token-swap /// 1. `[]` swap authority /// 2. `[]` user transfer authority /// 3. `[writable]` token_(A|B) SOURCE Account, amount is transferable by user transfer authority, /// 4. `[writable]` token_(A|B) Base Account to swap INTO. Must be the SOURCE token. /// 5. `[writable]` token_(A|B) Base Account to swap FROM. Must be the DESTINATION token. /// 6. `[writable]` token_(A|B) DESTINATION Account assigned to USER as the owner. /// 7. `[writable]` Pool token mint, to generate trading fees /// 8. `[writable]` Fee account, to receive trading fees /// 9. `[]` Token (A|B) SOURCE mint /// 10. `[]` Token (A|B) DESTINATION mint /// 11. `[]` Token (A|B) SOURCE program id /// 12. `[]` Token (A|B) DESTINATION program id /// 13. `[]` Pool Token program id /// 14. `[optional, writable]` Host fee account to receive additional trading fees Swap(...), ``` -------------------------------- ### Create a Token Mint with a Transfer Hook Source: https://www.solana-program.com/docs/token-2022/extensions This command creates a new token mint and configures it to use a specified program as its transfer hook. This is essential for implementing custom transfer logic, such as NFT royalties. ```bash $ spl-token --program-id TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb create-token --transfer-hook 7N4HggYEJAtCLJdnHGCtFqfxcB5rhQCsQTze3ftYstVj Creating token HFg1FFaj4PqFHmkYrqbZsarNJEZT436aXAXgQFMJihwc under program TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb Address: HFg1FFaj4PqFHmkYrqbZsarNJEZT436aXAXgQFMJihwc Decimals: 9 Signature: 3ug4Ejs16jJgEm1WyBwDDxzh9xqPzQ3a2cmy1hSYiPFcLQi9U12HYF1Dbhzb2bx75SSydfU6W4e11dGUXaPbJqVc ```