> ## Documentation Index
> Fetch the complete documentation index at: https://luminouslabs-cc5545c6-swen-add-code-runner.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Create Token Pools for Compression to Existing Mints

> Guide to create token pools for compressed tokens for SPL mints with `createTokenPool()`, troubleshooting and advanced configurations.

Create a token for compression fo an existing SPL mint. `createTokenPool()` requires only `fee_payer` and has no mint authority constraint.

<Info>
  The token pool account itself requires rent, but individual compressed token accounts are rent-free.
</Info>

<CodeGroup>
  ```typescript function-create-token-pool.ts theme={null}
  // Creates token pool account for existing SPL mint
  const transactionSignature = await createTokenPool(
      rpc,
      payer,
      mint,
  );
  ```
</CodeGroup>

<Check>
  **Best Practice:** Each mint supports a maximum of 4 token pools total. During compression/decompression operations, token pools get write-locked. Use `addTokenPools()` to create additional pools that increase per-block write-lock capacity.
</Check>

# Full Code Example

<Steps>
  <Step>
    ### Prerequisites

    Make sure you have dependencies and developer environment set up!

    <Accordion title="Prerequisites & Setup">
      **Dependencies**

      <Tabs>
        <Tab title="npm">
          ```bash theme={null}
          npm install @lightprotocol/stateless.js@alpha \
                      @lightprotocol/compressed-token@alpha
          ```
        </Tab>

        <Tab title="yarn">
          ```bash theme={null}
          yarn add @lightprotocol/stateless.js@alpha \
                   @lightprotocol/compressed-token@alpha
          ```
        </Tab>

        <Tab title="pnpm">
          ```bash theme={null}
          pnpm add @lightprotocol/stateless.js@alpha \
                   @lightprotocol/compressed-token@alpha
          ```
        </Tab>
      </Tabs>

      **Developer Environment**

      <Tabs>
        <Tab title="Localnet">
          By default, all guides use Localnet.

          <Tabs>
            <Tab title="npm">
              ```bash theme={null}
              npm install -g @lightprotocol/zk-compression-cli@alpha
              ```
            </Tab>

            <Tab title="yarn">
              ```bash theme={null}
              yarn global add @lightprotocol/zk-compression-cli@alpha
              ```
            </Tab>

            <Tab title="pnpm">
              ```bash theme={null}
              pnpm add -g @lightprotocol/zk-compression-cli@alpha
              ```
            </Tab>
          </Tabs>

          ```bash theme={null}
          # Start a local test validator
          light test-validator

          ## ensure you have the Solana CLI accessible in your system PATH
          ```

          ```typescript theme={null}
          // createRpc() defaults to local test validator endpoints
          import {
            Rpc,
            createRpc,
          } from "@lightprotocol/stateless.js";

          const connection: Rpc = createRpc();

          async function main() {
            let slot = await connection.getSlot();
            console.log(slot);

            let health = await connection.getIndexerHealth(slot);
            console.log(health);
            // "Ok"
          }

          main();
          ```
        </Tab>

        <Tab title="Devnet">
          Replace `<your-api-key>` with your actual API key. [Get your API key here](https://www.helius.dev/zk-compression), if you don't have one yet.

          ```typescript theme={null}
          import { createRpc } from "@lightprotocol/stateless.js";

          // Helius exposes Solana and Photon RPC endpoints through a single URL
          const RPC_ENDPOINT = "https://devnet.helius-rpc.com?api-key=<your_api_key>";
          const connection = createRpc(RPC_ENDPOINT, RPC_ENDPOINT, RPC_ENDPOINT);

          console.log("Connection created!");
          console.log("RPC Endpoint:", RPC_ENDPOINT);
          ```
        </Tab>
      </Tabs>
    </Accordion>
  </Step>

  <Step>
    ### Create Token Pool

    Run this script to create token pools for an SPL mint!

    ```typescript create-token-pools.ts highlight={31-33, 43-45} lines expandable theme={null}
    // 1. Setup funded payer and connect to local validator
    // 2. Create SPL mint
    // 3. Call createTokenPool() to register mint with compressed token program
    // 4. Add additional pools to increase write-lock capacity (optional)

    import { Keypair, PublicKey } from '@solana/web3.js';
    import { createRpc } from '@lightprotocol/stateless.js';
    import { createTokenPool, addTokenPools } from '@lightprotocol/compressed-token';
    import { createMint } from '@solana/spl-token';

    async function createTokenPools() {

        // Step 1: Setup funded payer and connect to local validator
        const rpc = createRpc(); // defaults to localhost:8899
        const payer = Keypair.generate();
        const airdropSignature = await rpc.requestAirdrop(payer.publicKey, 1000000000); // 1 SOL
        await rpc.confirmTransaction(airdropSignature);

        // Step 2: Create SPL mint
        const mint = await createMint(
            rpc,
            payer,
            payer.publicKey, // mint authority
            payer.publicKey, // freeze authority
            9
        );

        console.log("SPL mint created");
        console.log("Mint address:", mint.toBase58());

        // Step 3: Call createTokenPool() to register SPL mint with compressed token program
        // Creates token pool PDA (omnibus account) that holds SPL tokens for compressed tokens
        const poolTx = await createTokenPool(
            rpc,
            payer,
            mint // existing SPL mint to register
        );

        console.log("\nToken pool created!");
        console.log("SPL mint registered with compressed token program:", mint.toBase58());
        console.log("Pool transaction:", poolTx);

        // Step 4: Add up to 3 additional pools - increase write-lock capacity for higher throughput
        const additionalPoolsCount = 2;
        const additionalPoolsTx = await addTokenPools(
            rpc,
            payer,
            mint, // SPL mint with existing token pool
            additionalPoolsCount, // number of additional pools (max 3 more)
        );

        console.log(`\nAdded ${additionalPoolsCount} additional token pools!`);
        console.log("Additional pools transaction:", additionalPoolsTx);


        return {
            mint,
            poolTransaction: poolTx,
            additionalPoolsTransaction: additionalPoolsTx
        };
    }

    createTokenPools().catch(console.error);
    ```
  </Step>
</Steps>

# Troubleshooting

<AccordionGroup>
  <Accordion title="TokenPool not found">
    You're trying to access a token pool that doesn't exist.

    ```typescript theme={null}
    // Create the missing token pool
    const poolTx = await createTokenPool(rpc, payer, mint);
    console.log("Token pool created:", poolTx);
    ```
  </Accordion>
</AccordionGroup>

# Advanced Configuration

<AccordionGroup>
  <Accordion title="Batch Pool Creation">
    Create pools for multiple mints:

    ```typescript theme={null}
    const mints = [
        new PublicKey("MINT_1_ADDRESS"),
        new PublicKey("MINT_2_ADDRESS"),
        new PublicKey("MINT_3_ADDRESS"),
    ];

    for (const mint of mints) {
        try {
            const poolTx = await createTokenPool(rpc, payer, mint);
            console.log(`Pool created for ${mint.toBase58()}:`, poolTx);
        } catch (error) {
            console.log(`Failed for ${mint.toBase58()}:`, error.message);
        }
    }
    ```
  </Accordion>

  <Accordion title="Create Pool with Token-2022">
    Create token pools for Token-2022 mints:

    ```typescript theme={null}
    import { TOKEN_2022_PROGRAM_ID } from '@solana/spl-token';

    const poolTx = await createTokenPool(
        rpc,
        payer,
        mint, // Token-2022 mint
        undefined,
        TOKEN_2022_PROGRAM_ID,
    );
    ```
  </Accordion>
</AccordionGroup>

# Next Steps

<Card title="Learn how to merge multiple compressed token accounts into one to simplify state management." icon="chevron-right" color="#0066ff" href="/compressed-tokens/guides/how-to-merge-compressed-token-accounts" horizontal />
