| Light Token -> Light Token Account |
|
| SPL token -> Light Token Account |
|
| Light Token -> SPL Account |
|
- TypeScript Client
- Rust Client
- Program
The
transferInterface function transfers tokens between token accounts (SPL, Token 2022, or Light) in a single call.Compare to SPL:Find the source code
here.
Transfer Interface
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@beta \
@lightprotocol/compressed-token@beta
Report incorrect code
Copy
Ask AI
npm install -g @lightprotocol/zk-compression-cli@beta
Install packages in your working directory:Install the CLI globally:
Report incorrect code
Copy
Ask AI
yarn add @lightprotocol/stateless.js@beta \
@lightprotocol/compressed-token@beta
Report incorrect code
Copy
Ask AI
yarn global add @lightprotocol/zk-compression-cli@beta
Install packages in your working directory:Install the CLI globally:
Report incorrect code
Copy
Ask AI
pnpm add @lightprotocol/stateless.js@beta \
@lightprotocol/compressed-token@beta
Report incorrect code
Copy
Ask AI
pnpm add -g @lightprotocol/zk-compression-cli@beta
- 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,
transferInterface,
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 sender = Keypair.generate();
await createAtaInterface(rpc, payer, mint, sender.publicKey);
const senderAta = getAssociatedTokenAddressInterface(mint, sender.publicKey);
await mintToInterface(rpc, payer, mint, senderAta, payer, 1_000_000_000);
const recipient = Keypair.generate();
await createAtaInterface(rpc, payer, mint, recipient.publicKey);
const recipientAta = getAssociatedTokenAddressInterface(mint, recipient.publicKey);
const tx = await transferInterface(rpc, payer, senderAta, mint, recipientAta, sender, 500_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 } from "@lightprotocol/stateless.js";
import {
createMintInterface,
createAtaInterface,
mintToInterface,
createTransferInterfaceInstruction,
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 sender = Keypair.generate();
await createAtaInterface(rpc, payer, mint, sender.publicKey);
const senderAta = getAssociatedTokenAddressInterface(
mint,
sender.publicKey,
);
await mintToInterface(rpc, payer, mint, senderAta, payer, 1_000_000_000);
const recipient = Keypair.generate();
await createAtaInterface(rpc, payer, mint, recipient.publicKey);
const recipientAta = getAssociatedTokenAddressInterface(
mint,
recipient.publicKey,
);
// Transfer tokens between light-token associate token accounts
const ix = createTransferInterfaceInstruction(
senderAta,
recipientAta,
sender.publicKey,
500_000_000,
);
const tx = new Transaction().add(ix);
const signature = await sendAndConfirmTransaction(rpc, tx, [payer, sender]);
console.log("Tx:", signature);
})();
Use the unified
TransferInterface to transfer tokens between token accounts (SPL, Token 2022, or Light) in a single call.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 Interface
The example transfers- SPL token -> Light Token,
- Light Token -> Light Token, and
- Light Token -> SPL token.
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, TransferInterface};
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?;
// Transfers tokens between token accounts (SPL, Token-2022, or Light) in a single call.
let sig = TransferInterface {
source: associated_token_account,
mint,
destination: recipient_associated_token_account,
amount: 1000,
decimals,
..Default::default()
}
.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 anchor_spl::token::spl_token;
use borsh::BorshDeserialize;
use light_client::rpc::Rpc;
use light_program_test::{LightProgramTest, ProgramTestConfig};
use light_token::{
instruction::{
get_associated_token_address, CreateAssociatedTokenAccount, SplInterface,
TransferInterface, LIGHT_TOKEN_PROGRAM_ID,
},
spl_interface::find_spl_interface_pda_with_index,
};
use rust_client::{setup_spl_associated_token_account, setup_spl_mint};
use solana_sdk::signer::Signer;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut rpc = LightProgramTest::new(ProgramTestConfig::new_v2(true, None)).await?;
let payer = rpc.get_payer().insecure_clone();
let decimals = 2u8;
let amount = 10_000u64;
// Setup creates mint, mints tokens and creates SPL associated token account
let mint = setup_spl_mint(&mut rpc, &payer, decimals).await;
let spl_associated_token_account = setup_spl_associated_token_account(&mut rpc, &payer, &mint, &payer.pubkey(), amount).await;
let (interface_pda, interface_bump) = find_spl_interface_pda_with_index(&mint, 0, false);
// Create Light associated token account
let light_associated_token_account = get_associated_token_address(&payer.pubkey(), &mint);
let create_associated_token_account_instruction =
CreateAssociatedTokenAccount::new(payer.pubkey(), payer.pubkey(), mint).instruction()?;
rpc.create_and_send_transaction(&[create_associated_token_account_instruction], &payer.pubkey(), &[&payer])
.await?;
// SPL interface PDA for Mint (holds SPL tokens when transferred to Light Token)
let spl_interface = SplInterface {
mint,
spl_token_program: spl_token::ID,
spl_interface_pda: interface_pda,
spl_interface_pda_bump: interface_bump,
};
// Transfers tokens between token accounts (SPL, Token-2022, or Light) in a single call.
let transfer_instruction = TransferInterface {
source: spl_associated_token_account,
destination: light_associated_token_account,
amount,
decimals,
authority: payer.pubkey(),
payer: payer.pubkey(),
spl_interface: Some(spl_interface),
max_top_up: None,
source_owner: spl_token::ID,
destination_owner: LIGHT_TOKEN_PROGRAM_ID,
}
.instruction()?;
let sig = rpc
.create_and_send_transaction(&[transfer_instruction], &payer.pubkey(), &[&payer])
.await?;
let data = rpc
.get_account(light_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 Interface CPI
TheTransferInterfaceCpi transfers tokens between token accounts (SPL, Token 2022, or Light Token).- invoke (External signer)
- invoke_signed (PDA signer)
Report incorrect code
Copy
Ask AI
use light_token::instruction::TransferInterfaceCpi;
TransferInterfaceCpi::new(
amount,
decimals,
source.clone(),
destination.clone(),
authority.clone(),
payer.clone(),
light_token_authority.clone(),
system_program.clone(),
)
.invoke()?;
Report incorrect code
Copy
Ask AI
use light_token::instruction::TransferInterfaceCpi;
let signer_seeds = authority_seeds!(bump);
TransferInterfaceCpi::new(
amount,
decimals,
source.clone(),
destination.clone(),
authority.clone(),
payer.clone(),
light_token_authority.clone(),
system_program.clone(),
)
.invoke_signed(&[signer_seeds])?;
Full code example
- CPI
- Setup ATA with Macro and CPI
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::TransferInterfaceCpi;
declare_id!("3rb6sG4jiYNLZC8jo8kLsFHpxr2Ci8e8Hh8UmeCMZmUV");
#[program]
pub mod light_token_anchor_transfer_interface {
use super::*;
pub fn transfer(
ctx: Context<TransferAccounts>,
amount: u64,
decimals: u8,
spl_interface_pda_bump: Option<u8>,
) -> Result<()> {
let mut transfer = TransferInterfaceCpi::new(
amount,
decimals,
ctx.accounts.source.to_account_info(),
ctx.accounts.destination.to_account_info(),
ctx.accounts.authority.to_account_info(),
ctx.accounts.payer.to_account_info(),
ctx.accounts.cpi_authority.to_account_info(),
ctx.accounts.system_program.to_account_info(),
);
if let Some(bump) = spl_interface_pda_bump {
transfer = transfer
.with_spl_interface(
ctx.accounts.mint.as_ref().map(|a| a.to_account_info()),
ctx.accounts
.spl_token_program
.as_ref()
.map(|a| a.to_account_info()),
ctx.accounts
.spl_interface_pda
.as_ref()
.map(|a| a.to_account_info()),
Some(bump),
)
.map_err(|e| ProgramError::from(e))?;
}
transfer.invoke().map_err(|e| ProgramError::from(e))?;
Ok(())
}
}
#[derive(Accounts)]
pub struct TransferAccounts<'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
#[account(mut)]
pub destination: AccountInfo<'info>,
pub authority: Signer<'info>,
#[account(mut)]
pub payer: Signer<'info>,
/// CHECK: Validated by light-token CPI
pub cpi_authority: AccountInfo<'info>,
pub system_program: Program<'info, System>,
// SPL interface accounts (optional, for cross-type transfers)
/// CHECK: Validated by light-token CPI - token mint
pub mint: Option<AccountInfo<'info>>,
/// CHECK: SPL Token or Token-2022 program
pub spl_token_program: Option<AccountInfo<'info>>,
/// CHECK: Validated by light-token CPI - pool PDA
#[account(mut)]
pub spl_interface_pda: Option<AccountInfo<'info>>,
}
Uses the
#[light_account(init, associated_token::...)] macro to create the destination ATA during the transfer.View the full example with test utilities.
Report incorrect code
Copy
Ask AI
#![allow(unexpected_cfgs, deprecated)]
use anchor_lang::prelude::*;
use light_sdk::interface::CreateAccountsProof;
use light_token::anchor::{derive_light_cpi_signer, light_program, CpiSigner, LightAccounts};
use light_token::instruction::TransferInterfaceCpi;
declare_id!("672fL1Nm191MbPoygNM9DRiG2psBELn97XUpGbU3jW7E");
pub const LIGHT_CPI_SIGNER: CpiSigner =
derive_light_cpi_signer!("672fL1Nm191MbPoygNM9DRiG2psBELn97XUpGbU3jW7E");
#[derive(AnchorSerialize, AnchorDeserialize, Clone)]
pub struct TransferParams {
pub create_accounts_proof: CreateAccountsProof,
pub dest_associated_token_account_bump: u8,
pub amount: u64,
pub decimals: u8,
}
#[light_program]
#[program]
pub mod create_and_transfer {
use super::*;
pub fn transfer<'info>(
ctx: Context<'_, '_, '_, 'info, Transfer<'info>>,
params: TransferParams,
) -> Result<()> {
TransferInterfaceCpi::new(
params.amount,
params.decimals,
ctx.accounts.source.to_account_info(),
ctx.accounts.destination.to_account_info(),
ctx.accounts.authority.to_account_info(),
ctx.accounts.payer.to_account_info(),
ctx.accounts.light_token_cpi_authority.to_account_info(),
ctx.accounts.system_program.to_account_info(),
)
.invoke()
.map_err(|e| anchor_lang::prelude::ProgramError::from(e))?;
Ok(())
}
}
#[derive(Accounts, LightAccounts)]
#[instruction(params: TransferParams)]
pub struct Transfer<'info> {
#[account(mut)]
pub payer: Signer<'info>,
pub authority: Signer<'info>,
/// CHECK: Validated by light-token CPI
pub mint: AccountInfo<'info>,
/// CHECK: Validated by light-token CPI
#[account(mut)]
pub source: AccountInfo<'info>,
/// CHECK: Validated by light-token CPI
pub recipient: AccountInfo<'info>,
/// CHECK: Validated by light-token CPI
#[account(mut)]
#[light_account(init,
associated_token::authority = recipient,
associated_token::mint = mint,
associated_token::bump = params.dest_associated_token_account_bump
)]
pub destination: UncheckedAccount<'info>,
/// CHECK: Validated by light-token CPI
pub light_token_program: AccountInfo<'info>,
pub system_program: Program<'info, System>,
/// CHECK: Validated by light-token CPI
pub light_token_compressible_config: AccountInfo<'info>,
/// CHECK: Validated by light-token CPI
#[account(mut)]
pub rent_sponsor: AccountInfo<'info>,
/// CHECK: Validated by light-token CPI
pub light_token_cpi_authority: AccountInfo<'info>,
}