We need to have the template for the project scaffold. Anchor is a development framework for building solana programs.
anchor init token-vaultThis creates the project scaffold.
Create the folder wallets.
Through solana cli we can generate the key value pairs which are solana wallets, or can be create through the automate .sh file where we need to inject the solana command into the .sh file to generate the wallet generation.
Config File: Its like .env
- RPC URL: https://api.devnet.solana.com — endpoint talks to solana block chain (api) RPC Server
- WebSocket URL: wss://api.devnet.solana.com/ (computed) — real time live updates
- Keypair Path: file path for wallet
- Commitment: confirmed
solana-keygen new -o ./token-vault/wallets/admin.jsonThis creates private key admin.json -> JSON array and Pub key -> Base58 String.
solana-keygen new -o ./token-vault/wallets/test_wallets/test_1.json
solana-keygen new -o ./token-vault/wallets/test_wallets/test_2.json
solana-keygen new -o ./token-vault/wallets/test_wallets/test_3.json
solana-keygen new -o ./token-vault/wallets/test_wallets/test_4.json
solana-keygen new -o ./token-vault/wallets/test_wallets/test_5.jsonThis will generates the 5 test wallets. We can automate this by writing script .sh loop through command.
Token account is to hold the spl tokens. To hold tokens, wallet should must have ATA.
Through solana cli we able to change config setting:
solana config set --url devnetSet the keypair path, which wallet to fund the sols to create the spl token and all (admin):
solana config set -k cybrense/token-vault/wallets/admin.jsonWe need to fund the sols into the admin wallet. In devnet env we can do it by airdrop, faucet etc. Before that check the solana address where the address matches with the admin key as solana cli fetches the keypair path which we given in config settings.
How we can know the solana pub key if we forgot? Able to fetch through solana private key:
solana address -k cybrense/token-vault/wallets/admin.jsonOnce both are matching confirmed, fund the sols:
solana airdrop 5This airdrops 5 sols to the admin wallet.
Note: The block chain will throws error due to rate limiter leave it try faucet or i have the sols in different wallet i can transfer sols to this admin wallet.
Requesting airdrop of 5 SOL
Error: airdrop request failed. This can happen when the rate limit is reached.
After funding check the balance:
solana balanceNow create the spl-token through cli command:
spl-token create-token --decimals 9This create spl token with decimals 9, its uses legacy token program not spl token 2022, classic spl token program.
Token is creates, and the mint address: UBiXc5J3i1eEbGKircN2TCG844ukKH9NA4yKE54DnYK
Now next is to create an ATA which means associated token account which is to link with the mint address + wallet address combination creates one address these are called type of PDAs which means program derived address.
So basically the TOKEN ACCOUNT to be created to hold the spl tokens and for the address of ATA token account derived combination of wallet address + Mint address. This address is a type of PDA, generated by associated token account.
ATA = To hold the spl tokens for a specific wallet.
So for admin we need to create the token account to the spl tokens which we created in last step.
For this who will fund? For every transaction every creation of account needs SOLS. Admin wallet is fee payer here to create the token account.
spl-token create-token <MINT_ADDRESS> --owner <WALLET_ADDRESS> --fee-payer <KEY_PATH>spl-token create-account UBiXc5J3i1eEbGKircN2TCG844ukKH9NA4yKE54DnYK --owner w2XW6gdWcfqonYHYksiQdf5EEXM1QP6Zqt1QQFr4wJv --fee-payer cybrense/token-vault/wallets/admin.jsonThis will creates the ATA account for the admin of particular Mint.
Creating account 9FLV7Dxv6QjK3CK588siLK3zEoP4QEu88FzBuMqHvR2E
Here is the address of ATA.
Now check the balance, there shouldnt be 10 sols, the fees for creating the ata will be around 0.02 sols.
Now current balance 9.99648412 SOL
Then mint the tokens to admin ata by:
spl-token mint UBiXc5J3i1eEbGKircN2TCG844ukKH9NA4yKE54DnYK 1000 --owner cybrense/token-vault/wallets/admin.jsonAdmin ATA has 1000 tokens.
Its initialized through the contract (by seeds) as its PDA and distribute function where it distributes the tokens from vault to the test wallets equally.
Client script which calls all these functions and execute.
It Assumes already done via CLI:
solana-keygen new(admin + 5 test wallets)spl-token create-token --decimals 9spl-token create-account <MINT>(admin ATA)spl-token mint <MINT> 1000
This script handles:
- Load keypairs from disk
- Airdrop SOL to admin if needed
- Call
initialize→ creates Vault PDA + Vault ATA on-chain - Transfer tokens from admin ATA → Vault ATA
- Call
distribute→ sends equal shares to all 5 wallets (creates ATAs) - Print final balances
Run script by:
ts-node client/client.tsHere is the output
══════════════════════════════════════════════════
Token Vault — End-to-End Test Script
Cluster : devnet
Mint : UBiXc5J3i1eEbGKircN2TCG844ukKH9NA4yKE54DnYK
══════════════════════════════════════════════════
📂 Loading keypairs …
Admin : w2XW6gdWcfqonYHYksiQdf5EEXM1QP6Zqt1QQFr4wJv
Wallet1: CbR1D549ZfzHeZvnLCZ5UraJLq2gXYf7SawXY1aatwdR
Wallet2: EKpU9eEkePsWvuVRaXc65pWRteUJSrVphiPNyK8D1tBE
Wallet3: 9ez9D8tFFJENhNvFuAFJpiRVuSBfFCUEUmA23QVgyyvD
Wallet4: H2wd16XkQ6DZvvy9UMitJeocNezLd9tpdAFCkZNvZK2v
Wallet5: 8b9Ke5GHHAhJmborjt2R8wv67XR4Te5H9B4D8Bi4KLkY
🔗 Program: 6BF9ypMtnZDVRZbLZy9F9QK9fKQTAdJjc3VAytoTLTUu
💸 Checking admin balance …
✔ Already funded (15.843 SOL)
📍 Derived addresses
Admin ATA : 9FLV7Dxv6QjK3CK588siLK3zEoP4QEu88FzBuMqHvR2E
Vault PDA : 6dZrkEzsuanrVEEfhwi9so3fD8Sjq9Y8J7KeH3M8NKkb
Vault ATA : AMXABqhrrsVVKzTWTLf9RpUEApjgMJtnScuKasbMf5zs
🏛️ Initializing vault …
✔ Vault already initialized — skipping
🔍 Admin ATA balance: 1,000 tokens
💼 Depositing 500 tokens into vault …
✅ TX: 2CavyUHStg1BDCyyFcvRU3k1Amnh3ocZtq2vJgNeynJzq7k7AVawZzvXtvMfaLR2NQwhuyZYUoNJhjuzubFrinuv
🚀 Distributing 500 tokens → 5 wallets …
✅ TX: 5C6U47LCJ1DGjpYrtexSNdodex7fVVJ6wxFx3Z34wXtwpfs7wzg1vDqCsr51PoXR1raYPjuCG29TQ4Lu17pYJEjw
📊 Final Balances
─────────────────────────────────────────────────
Admin ATA : 500 tokens
Vault ATA : 1,000 tokens
Wallet 1 : 100 tokens (CbR1D5…twdR)
Wallet 2 : 100 tokens (EKpU9e…1tBE)
Wallet 3 : 100 tokens (9ez9D8…yyvD)
Wallet 4 : 100 tokens (H2wd16…ZK2v)
Wallet 5 : 100 tokens (8b9Ke5…KLkY)
══════════════════════════════════════════════════
All steps completed successfully! 🎉
══════════════════════════════════════════════════