- TransferChecked validates that the decimals parameter matches the mint’s decimals.
- Use for Light→Light transfers when you need decimal verification.
- For transfers involving SPL or Token 2022 accounts, use Transfer Interface instead.
- Rust Client
- Program
Prerequisites
Dependencies
Dependencies
Cargo.toml
Report incorrect code
Copy
Ask AI
[dependencies]
light-token = "0.4.0"
light-client = { version = "0.19.0", features = ["v2"] }
solana-sdk = "2"
borsh = "0.10.4"
tokio = { version = "1", features = ["full"] }
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@beta
Report incorrect code
Copy
Ask AI
yarn global add @lightprotocol/zk-compression-cli@beta
Report incorrect code
Copy
Ask AI
pnpm add -g @lightprotocol/zk-compression-cli@beta
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(())
}
Transfer with Decimal Check
View the source code and full example with shared test utilities.
- Action
- Instruction
Report incorrect code
Copy
Ask AI
use borsh::BorshDeserialize;
use light_client::rpc::Rpc;
use light_token_client::actions::{CreateAta, TransferChecked};
use rust_client::{setup, SetupContext};
use solana_sdk::{signature::Keypair, signer::Signer};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let SetupContext {
mut rpc,
payer,
mint,
associated_token_account,
decimals,
..
} = setup().await;
// Create recipient associated token account
let recipient = Keypair::new();
let (_signature, recipient_associated_token_account) = CreateAta {
mint,
owner: recipient.pubkey(),
idempotent: true,
}
.execute(&mut rpc, &payer)
.await?;
// TransferChecked validates decimals match the mint's decimals.
// Only use for Light->Light transfers.
// Use TransferInterface for all other transfers (Light, SPL or Token-2022).
let sig = TransferChecked {
source: associated_token_account,
mint,
destination: recipient_associated_token_account,
amount: 1000,
decimals,
}
.execute(&mut rpc, &payer, &payer)
.await?;
let data = rpc
.get_account(recipient_associated_token_account)
.await?
.ok_or("Account not found")?;
let token = light_token_interface::state::Token::deserialize(&mut &data.data[..])?;
println!("Balance: {} Tx: {sig}", token.amount);
Ok(())
}
Report incorrect code
Copy
Ask AI
use borsh::BorshDeserialize;
use light_client::rpc::Rpc;
use light_token::instruction::{
get_associated_token_address, CreateAssociatedTokenAccount, TransferChecked,
};
use rust_client::{setup, SetupContext};
use solana_sdk::{signature::Keypair, signer::Signer};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Setup creates mint and associated token account with tokens
let SetupContext {
mut rpc,
payer,
mint,
associated_token_account,
decimals,
..
} = setup().await;
let transfer_amount = 400_000u64;
// Create recipient associated token account
let recipient = Keypair::new();
let recipient_associated_token_account = get_associated_token_address(&recipient.pubkey(), &mint);
let create_associated_token_account_instruction = CreateAssociatedTokenAccount::new(payer.pubkey(), recipient.pubkey(), mint)
.instruction()?;
rpc.create_and_send_transaction(&[create_associated_token_account_instruction], &payer.pubkey(), &[&payer])
.await?;
// TransferChecked validates decimals match the mint's decimals
// Only use for Light->Light transfers.
// Use TransferInterface for all other transfers (Light, SPL or Token-2022).
let transfer_instruction = TransferChecked {
source: associated_token_account,
mint,
destination: recipient_associated_token_account,
amount: transfer_amount,
decimals,
authority: payer.pubkey(),
max_top_up: None,
fee_payer: None,
}
.instruction()?;
let sig = rpc
.create_and_send_transaction(&[transfer_instruction], &payer.pubkey(), &[&payer])
.await?;
let data = rpc
.get_account(recipient_associated_token_account)
.await?
.ok_or("Account not found")?;
let token = light_token_interface::state::Token::deserialize(&mut &data.data[..])?;
println!("Balance: {} Tx: {sig}", token.amount);
Ok(())
}
Transfer Checked with CPI
- invoke (External signer)
- invoke_signed (PDA signer)
Report incorrect code
Copy
Ask AI
use light_token::instruction::TransferCheckedCpi;
TransferCheckedCpi {
source: source.clone(),
mint: mint.clone(),
destination: destination.clone(),
amount,
decimals,
authority: authority.clone(),
system_program: system_program.clone(),
max_top_up: None,
fee_payer: None,
}
.invoke()?;
Report incorrect code
Copy
Ask AI
use light_token::instruction::TransferCheckedCpi;
let signer_seeds = authority_seeds!(bump);
TransferCheckedCpi {
source: source.clone(),
mint: mint.clone(),
destination: destination.clone(),
amount,
decimals,
authority: authority.clone(),
system_program: system_program.clone(),
max_top_up: None,
fee_payer: None,
}
.invoke_signed(&[signer_seeds])?;
Full Code Example
View the source code and full example with shared test utilities.
Report incorrect code
Copy
Ask AI
#![allow(unexpected_cfgs, deprecated)]
use anchor_lang::prelude::*;
use light_token::instruction::TransferCheckedCpi;
declare_id!("HXmfewpozFdxhM8BayL9v5541gwoGMXTrUoip5KySs2f");
#[program]
pub mod light_token_anchor_transfer_checked {
use super::*;
pub fn transfer_checked(
ctx: Context<TransferCheckedAccounts>,
amount: u64,
decimals: u8,
) -> Result<()> {
TransferCheckedCpi {
source: ctx.accounts.source.to_account_info(),
mint: ctx.accounts.mint.to_account_info(),
destination: ctx.accounts.destination.to_account_info(),
amount,
decimals,
authority: ctx.accounts.authority.to_account_info(),
system_program: ctx.accounts.system_program.to_account_info(),
max_top_up: None,
fee_payer: None,
}
.invoke()?;
Ok(())
}
}
#[derive(Accounts)]
pub struct TransferCheckedAccounts<'info> {
/// CHECK: Light token program for CPI
pub light_token_program: AccountInfo<'info>,
/// CHECK: Validated by light-token CPI
#[account(mut)]
pub source: AccountInfo<'info>,
/// CHECK: Validated by light-token CPI
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>,
}