토큰 민트 생성
민트는 모든 SPL 토큰의 기반입니다. 토큰의 속성을 정의하고 새 토큰을 민팅할 수 있는 권한을 제어합니다.
use litesvm::LiteSVM;use litesvm_token::{spl_token::native_mint::DECIMALS,CreateMint,};use solana_sdk::{native_token::LAMPORTS_PER_SOL,signature::{Keypair, Signer},};#[test]fn test_create_mint() {let mut svm = LiteSVM::new();// Create payer account and fund itlet payer = Keypair::new();svm.airdrop(&payer.pubkey(), 10 * LAMPORTS_PER_SOL).unwrap();// Create a new SPL token mint with the payer as the mint authoritylet mint = CreateMint::new(&mut svm, &payer).authority(&payer.pubkey()).decimals(DECIMALS).send().unwrap();}
Token Account 생성
token account는 특정 사용자의 특정 spl-token 잔액을 보유합니다. 이것과 ATA의 차이점을 이해하려면 이 섹션을 읽어보세요.
use litesvm::LiteSVM;use litesvm_token::{spl_token::native_mint::DECIMALS,CreateMint, CreateAccount,};use solana_sdk::{native_token::LAMPORTS_PER_SOL,signature::{Keypair, Signer},};#[test]fn test_create_mint() {let mut svm = LiteSVM::new();// Create payer account and fund itlet payer = Keypair::new();svm.airdrop(&payer.pubkey(), 10 * LAMPORTS_PER_SOL).unwrap();// Create a new SPL token mint with the payer as the mint authoritylet mint = CreateMint::new(&mut svm, &payer).authority(&payer.pubkey()).decimals(DECIMALS).send().unwrap();// Create a token account for the payer// You must have created a Mint Account to be able to create a Token Accountlet token_account = CreateAccount::new(&mut svm, &payer, &mint).owner(&payer.pubkey()).send().unwrap();}
Associated Token Account 생성
Associated Token Accounts (ATAs)는 특정 사용자의 토큰을 보유하기 위한 결정론적 주소입니다. 이것과 ATA의 차이점을 이해하려면 이 섹션을 읽어보세요.
use litesvm::LiteSVM;use litesvm_token::{spl_token::native_mint::DECIMALS,CreateMint, CreateAssociatedTokenAccount,};use solana_sdk::{native_token::LAMPORTS_PER_SOL,signature::{Keypair, Signer},};#[test]fn test_create_mint() {let mut svm = LiteSVM::new();// Create payer account and fund itlet payer = Keypair::new();svm.airdrop(&payer.pubkey(), 10 * LAMPORTS_PER_SOL).unwrap();// Create a new SPL token mint with the payer as the mint authoritylet mint = CreateMint::new(&mut svm, &payer).authority(&payer.pubkey()).decimals(DECIMALS).send().unwrap();// Create an ATA for the payer// You must have created a Mint Account to be able to create a Token Accountlet associated_token_account = CreateAssociatedTokenAccount::new(&mut svm, &payer, &mint).owner(&payer.pubkey()).send().unwrap();}
Token Account에 토큰 민팅
민트와 token account가 준비되면 해당 계정에 토큰을 민팅할 수 있습니다.
use litesvm::LiteSVM;use litesvm_token::{get_spl_account,spl_token::{native_mint::DECIMALS, state::Account as TokenAccount},CreateAssociatedTokenAccount, CreateMint, MintTo,};use solana_sdk::{native_token::LAMPORTS_PER_SOL,signature::{Keypair, Signer},};#[test]fn test_mint_tokens() {let mut svm = LiteSVM::new();// Create payer account and fund itlet payer = Keypair::new();svm.airdrop(&payer.pubkey(), 10 * LAMPORTS_PER_SOL).unwrap();// Create two userslet alice = Keypair::new();// Create a new SPL token mint with alice as the mint authoritylet mint = CreateMint::new(&mut svm, &payer).authority(&alice.pubkey()).decimals(DECIMALS).send().unwrap();// Create associated token accounts (ATAs) for alice and boblet alice_token_account = CreateAssociatedTokenAccount::new(&mut svm, &payer, &mint).owner(&alice.pubkey()).send().unwrap();// Mint 1000 tokens to Alice's accountMintTo::new(&mut svm, &payer, &mint, &alice_token_account, 1000).owner(&alice).send().unwrap();// Verify balancelet alice_account: TokenAccount = get_spl_account(&svm, &alice_token_account).unwrap();let alice_balance = alice_account.amount;assert_eq!(alice_balance, 1000);}
토큰 전송
Transfer 명령어를 사용하여 계정 간에 토큰을 전송합니다.
use litesvm::LiteSVM;use litesvm_token::{get_spl_account,spl_token::{native_mint::DECIMALS, state::Account as TokenAccount},CreateAssociatedTokenAccount, CreateMint, MintTo, Transfer,};use solana_sdk::{native_token::LAMPORTS_PER_SOL,signature::{Keypair, Signer},};#[test]fn test_token_transfer() {let mut svm = LiteSVM::new();// Create payer account and fund itlet payer = Keypair::new();svm.airdrop(&payer.pubkey(), 10 * LAMPORTS_PER_SOL).unwrap();// Create two userslet alice = Keypair::new();let bob = Keypair::new();// Create a new SPL token mint with alice as the mint authoritylet mint = CreateMint::new(&mut svm, &payer).authority(&alice.pubkey()).decimals(DECIMALS).send().unwrap();// Create associated token accounts (ATAs) for alice and boblet alice_token_account = CreateAssociatedTokenAccount::new(&mut svm, &payer, &mint).owner(&alice.pubkey()).send().unwrap();let bob_token_account = CreateAssociatedTokenAccount::new(&mut svm, &payer, &mint).owner(&bob.pubkey()).send().unwrap();// Mint 1000 tokens to Alice's accountMintTo::new(&mut svm, &payer, &mint, &alice_token_account, 1000).owner(&alice).send().unwrap();// Transfer 400 tokens from Alice to BobTransfer::new(&mut svm, &payer, &mint, &bob_token_account, 400).source(&alice_token_account).owner(&alice).send().unwrap();// Verify balanceslet alice_account: TokenAccount = get_spl_account(&svm, &alice_token_account).unwrap();let bob_account: TokenAccount = get_spl_account(&svm, &bob_token_account).unwrap();let alice_balance = alice_account.amount;let bob_balance = bob_account.amount;assert_eq!(alice_balance, 600);assert_eq!(bob_balance, 400);println!("SPL token transfer successful!");}
소수점 처리
대부분의 토큰은 소수점을 사용하여 분수 금액을 나타냅니다. 올바르게 처리하는 방법은 다음과 같습니다:
// For a token with 6 decimals (like USDC)let decimals = 6;let one_token = 10_u64.pow(decimals as u32);let amount_tokens = 100; // Want to send 100 tokenslet amount_raw = amount_tokens * one_token; // 100,000,000 raw units// For a token with 9 decimals (like SOL)let decimals = 9;let one_sol = 10_u64.pow(decimals as u32);let amount_sol = 1.5; // Want to send 1.5 SOLlet amount_lamports = (amount_sol * one_sol as f64) as u64; // 1,500,000,000 lamports
Is this page helpful?