- Mint To creates new tokens of a mint and deposits them to a specified token account.
- Before we can mint any tokens, we need an initialized mint account (SPL, Token-2022 or Light) for which we hold the mint authority.
- TypeScript Client
- Rust Client
- Program Guide
mintToInterface mints tokens to token accounts in a single call.The function auto-detects the token program (SPL, Token-2022, or Light) from the mint address.Compare to SPL:Find the source code
here.
1
Mint Tokens to Light Token Account
Installation
Installation
- npm
- yarn
- pnpm
Install packages in your working directory:Install the CLI globally:
Report incorrect code
Copy
Ask AI
npm install @lightprotocol/stateless.js@alpha \
@lightprotocol/compressed-token@alpha
Report incorrect code
Copy
Ask AI
npm install -g @lightprotocol/zk-compression-cli@alpha
Install packages in your working directory:Install the CLI globally:
Report incorrect code
Copy
Ask AI
yarn add @lightprotocol/stateless.js@alpha \
@lightprotocol/compressed-token@alpha
Report incorrect code
Copy
Ask AI
yarn global add @lightprotocol/zk-compression-cli@alpha
Install packages in your working directory:Install the CLI globally:
Report incorrect code
Copy
Ask AI
pnpm add @lightprotocol/stateless.js@alpha \
@lightprotocol/compressed-token@alpha
Report incorrect code
Copy
Ask AI
pnpm add -g @lightprotocol/zk-compression-cli@alpha
- Localnet
- Devnet
Report incorrect code
Copy
Ask AI
# start local test-validator in a separate terminal
light test-validator
In the code examples, use
createRpc() without arguments for localnet.Get an API key from Helius and add to
.env:.env
Report incorrect code
Copy
Ask AI
API_KEY=<your-helius-api-key>
In the code examples, use
createRpc(RPC_URL) with the devnet URL.- Action
- Instruction
Report incorrect code
Copy
Ask AI
import "dotenv/config";
import { Keypair } from "@solana/web3.js";
import { createRpc } from "@lightprotocol/stateless.js";
import {
createMintInterface,
createAtaInterface,
mintToInterface,
getAssociatedTokenAddressInterface,
} from "@lightprotocol/compressed-token";
import { homedir } from "os";
import { readFileSync } from "fs";
// devnet:
const RPC_URL = `https://devnet.helius-rpc.com?api-key=${process.env.API_KEY!}`;
// localnet:
// const RPC_URL = undefined;
const payer = Keypair.fromSecretKey(
new Uint8Array(
JSON.parse(readFileSync(`${homedir()}/.config/solana/id.json`, "utf8"))
)
);
(async function () {
// devnet:
const rpc = createRpc(RPC_URL);
// localnet:
// const rpc = createRpc();
const { mint } = await createMintInterface(rpc, payer, payer, null, 9);
const recipient = Keypair.generate();
await createAtaInterface(rpc, payer, mint, recipient.publicKey);
const destination = getAssociatedTokenAddressInterface(mint, recipient.publicKey);
const tx = await mintToInterface(rpc, payer, mint, destination, payer, 1_000_000_000);
console.log("Tx:", tx);
})();
Report incorrect code
Copy
Ask AI
import "dotenv/config";
import { Keypair, ComputeBudgetProgram, Transaction, sendAndConfirmTransaction } from "@solana/web3.js";
import { createRpc, bn, DerivationMode } from "@lightprotocol/stateless.js";
import {
createMintInterface,
createAtaInterface,
createMintToInterfaceInstruction,
getMintInterface,
getAssociatedTokenAddressInterface,
} from "@lightprotocol/compressed-token";
import { homedir } from "os";
import { readFileSync } from "fs";
// devnet:
const RPC_URL = `https://devnet.helius-rpc.com?api-key=${process.env.API_KEY!}`;
const rpc = createRpc(RPC_URL);
// localnet:
// const rpc = createRpc();
const payer = Keypair.fromSecretKey(
new Uint8Array(
JSON.parse(readFileSync(`${homedir()}/.config/solana/id.json`, "utf8"))
)
);
(async function () {
const { mint } = await createMintInterface(rpc, payer, payer, null, 9);
const recipient = Keypair.generate();
await createAtaInterface(rpc, payer, mint, recipient.publicKey);
const destination = getAssociatedTokenAddressInterface(
mint,
recipient.publicKey
);
const mintInterface = await getMintInterface(rpc, mint);
let validityProof;
if (mintInterface.merkleContext) {
validityProof = await rpc.getValidityProofV2(
[
{
hash: bn(mintInterface.merkleContext.hash),
leafIndex: mintInterface.merkleContext.leafIndex,
treeInfo: mintInterface.merkleContext.treeInfo,
proveByIndex: mintInterface.merkleContext.proveByIndex,
},
],
[],
DerivationMode.compressible
);
}
const ix = createMintToInterfaceInstruction(
mintInterface,
destination,
payer.publicKey,
payer.publicKey,
1_000_000_000,
validityProof
);
const tx = new Transaction().add(
ComputeBudgetProgram.setComputeUnitLimit({ units: 500_000 }),
ix
);
const signature = await sendAndConfirmTransaction(rpc, tx, [payer]);
console.log("Tx:", signature);
})();
Use
MintTo to mint tokens to a light-token account.Compare to SPL:1
Prerequisites
Dependencies
Dependencies
Cargo.toml
Report incorrect code
Copy
Ask AI
[dependencies]
light-compressed-token-sdk = "0.1"
light-client = "0.1"
light-token-types = "0.1"
solana-sdk = "2.2"
borsh = "0.10"
tokio = { version = "1.36", features = ["full"] }
[dev-dependencies]
light-program-test = "0.1" # For in-memory tests with LiteSVM
Developer Environment
Developer Environment
- In-Memory (LightProgramTest)
- Localnet (LightClient)
- Devnet (LightClient)
Test with Lite-SVM (…)
Report incorrect code
Copy
Ask AI
# Initialize project
cargo init my-light-project
cd my-light-project
# Run tests
cargo test
Report incorrect code
Copy
Ask AI
use light_program_test::{LightProgramTest, ProgramTestConfig};
use solana_sdk::signer::Signer;
#[tokio::test]
async fn test_example() {
// In-memory test environment
let mut rpc = LightProgramTest::new(ProgramTestConfig::default())
.await
.unwrap();
let payer = rpc.get_payer().insecure_clone();
println!("Payer: {}", payer.pubkey());
}
Connects to a local test validator.
- npm
- yarn
- pnpm
Report incorrect code
Copy
Ask AI
npm install -g @lightprotocol/zk-compression-cli@alpha
Report incorrect code
Copy
Ask AI
yarn global add @lightprotocol/zk-compression-cli@alpha
Report incorrect code
Copy
Ask AI
pnpm add -g @lightprotocol/zk-compression-cli@alpha
Report incorrect code
Copy
Ask AI
# Initialize project
cargo init my-light-project
cd my-light-project
# Start local test validator (in separate terminal)
light test-validator
Report incorrect code
Copy
Ask AI
use light_client::rpc::{LightClient, LightClientConfig, Rpc};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Connects to http://localhost:8899
let rpc = LightClient::new(LightClientConfig::local()).await?;
let slot = rpc.get_slot().await?;
println!("Current slot: {}", slot);
Ok(())
}
Replace
<your-api-key> with your actual API key. Get your API key here.Report incorrect code
Copy
Ask AI
use light_client::rpc::{LightClient, LightClientConfig, Rpc};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let rpc_url = "https://devnet.helius-rpc.com?api-key=<your_api_key>";
let rpc = LightClient::new(
LightClientConfig::new(rpc_url.to_string(), None, None)
).await?;
println!("Connected to Devnet");
Ok(())
}
2
Mint to Light Token Accounts
View Source Code or find full examples with tests: examples-light-token.
Report incorrect code
Copy
Ask AI
mod shared;
use borsh::BorshDeserialize;
use light_client::rpc::Rpc;
use light_token_sdk::token::MintTo;
use shared::SetupContext;
use solana_sdk::signer::Signer;
#[tokio::test(flavor = "multi_thread")]
async fn mint_to() {
// Setup creates mint and empty ATA
let SetupContext {
mut rpc,
payer,
mint,
ata,
..
} = shared::setup_empty_ata().await;
let mint_amount = 1_000_000_000u64;
let mint_to_ix = MintTo {
mint,
destination: ata,
amount: mint_amount,
authority: payer.pubkey(),
max_top_up: None,
}
.instruction()
.unwrap();
rpc.create_and_send_transaction(&[mint_to_ix], &payer.pubkey(), &[&payer])
.await
.unwrap();
let ata_data = rpc.get_account(ata).await.unwrap().unwrap();
let token = light_token_interface::state::Token::deserialize(&mut &ata_data.data[..]).unwrap();
assert_eq!(token.amount, mint_amount);
}
1
Build Account Infos and CPI
- invoke (External Signer)
- invoke_signed (PDA Authority)
Report incorrect code
Copy
Ask AI
use light_token_sdk::token::MintToCpi;
MintToCpi {
mint: mint.clone(),
destination: destination.clone(),
amount,
authority: authority.clone(),
system_program: system_program.clone(),
max_top_up: None,
}
.invoke()
Report incorrect code
Copy
Ask AI
use light_token_sdk::token::MintToCpi;
let signer_seeds = authority_seeds!(bump);
MintToCpi {
mint: mint.clone(),
destination: destination.clone(),
amount,
authority: authority.clone(),
system_program: system_program.clone(),
max_top_up: None,
}
.invoke_signed(&[signer_seeds])
Accounts List
Accounts List
| Account | Type | Description |
|---|---|---|
| Authority | signer | The mint authority. Must match the light-mint’s mint authority. |
| Payer | signer, mutable | Pays transaction fees. |
| State Tree | mutable | Account of the state Merkle tree storing the light-mint. |
| Input Queue | mutable | Account of the input queue associated with the state tree the light-mint is stored in. The existing hash of the light-mint is to mark it as spent. |
| Output Queue | mutable | Account of the output queue associated with the state tree the light-mint will be stored in. The updated hash of the light-mint is . |
| light-token Accounts | mutable | Destination light-token Solana accounts to receive minted tokens. |
| System Accounts | - | See System Accounts List above. |
| CPI Context | optional (none for most) | Enables batched compressed account operations across multiple programs with a single validity proof. Set to None for most operations. |
| CPI Context Account | optional, mutable | On-chain account that temporarily stores instruction data from multiple CPIs for combined execution. Set to None for most operations. |
Full Code Example
View Source Code or full examples with tests: examples-light-token.
- Anchor
- Native
Report incorrect code
Copy
Ask AI
#![allow(unexpected_cfgs)]
use anchor_lang::prelude::*;
use light_token_sdk::token::MintToCpi;
declare_id!("7SUgjNZYC1h89MuPVYkgEP5A4uYx5GFSjC7mzqMbN8U2");
#[program]
pub mod light_token_anchor_mint_to {
use super::*;
pub fn mint_to<'info>(
ctx: Context<'_, '_, '_, 'info, MintToAccounts<'info>>,
amount: u64,
) -> Result<()> {
MintToCpi {
mint: ctx.accounts.mint.to_account_info(),
destination: ctx.accounts.destination.to_account_info(),
amount,
authority: ctx.accounts.authority.to_account_info(),
system_program: ctx.accounts.system_program.to_account_info(),
max_top_up: None,
}
.invoke()?;
Ok(())
}
}
#[derive(Accounts)]
pub struct MintToAccounts<'info> {
/// CHECK: Validated by light-token CPI
#[account(mut)]
pub mint: AccountInfo<'info>,
/// CHECK: Validated by light-token CPI
#[account(mut)]
pub destination: AccountInfo<'info>,
pub authority: Signer<'info>,
pub system_program: Program<'info, System>,
/// CHECK: Light token program for CPI
pub light_token_program: AccountInfo<'info>,
}
Report incorrect code
Copy
Ask AI
use super::authority_seeds;
use light_token_sdk::token::MintToCpi;
use solana_program::{
account_info::AccountInfo, entrypoint::ProgramResult,
program_error::ProgramError,
};
pub fn mint_to_invoke(accounts: &[AccountInfo], data: &[u8]) -> ProgramResult {
let [mint, destination, authority, system_program, _token_program] =
accounts
else {
return Err(ProgramError::NotEnoughAccountKeys);
};
let amount = u64::from_le_bytes(
data.try_into()
.map_err(|_| ProgramError::InvalidInstructionData)?,
);
MintToCpi {
mint: mint.clone(),
destination: destination.clone(),
amount,
authority: authority.clone(),
system_program: system_program.clone(),
max_top_up: None,
}
.invoke()
}
pub fn mint_to_invoke_signed(accounts: &[AccountInfo], data: &[u8]) -> ProgramResult {
let [mint, destination, authority, system_program, _token_program] =
accounts
else {
return Err(ProgramError::NotEnoughAccountKeys);
};
if data.len() < 9 {
return Err(ProgramError::InvalidInstructionData);
}
let amount = u64::from_le_bytes(data[0..8].try_into().unwrap());
let bump = data[8];
let signer_seeds = authority_seeds!(bump);
MintToCpi {
mint: mint.clone(),
destination: destination.clone(),
amount,
authority: authority.clone(),
system_program: system_program.clone(),
max_top_up: None,
}
.invoke_signed(&[signer_seeds])
}