Solana CookbookTokens
How to Use Wrapped SOL
Wrapped SOL is just like any other token mint. The difference is using syncNative
and creating token accounts specifically on the NATIVE_MINT
address.
Create Token Account
Like creating
SPL token accounts but
replace mint with NATIVE_MINT
import { NATIVE_MINT } from "@solana/spl-token";
Add Balance
There are two ways to add balance for Wrapped SOL
1. By SOL Transfer
add-balance-by-sol.ts
import {clusterApiUrl,Connection,Keypair,Transaction,SystemProgram,sendAndConfirmTransaction,} from "@solana/web3.js";import {NATIVE_MINT,getAssociatedTokenAddress,createSyncNativeInstruction,} from "@solana/spl-token";import bs58 from "bs58";(async () => {// connectionconst connection = new Connection(clusterApiUrl("devnet"), "confirmed");// 5YNmS1R9nNSCDzb5a7mMJ1dwK9uHeAAF4CmPEwKgVWr8const feePayer = Keypair.fromSecretKey(bs58.decode("588FU4PktJWfGfxtzpAAXywSNt74AvtroVzGfKkVN1LwRuvHwKGr851uH8czM5qm4iqLbs1kKoMKtMJG4ATR7Ld2",),);// G2FAbFQPFa5qKXCetoFZQEvF9BVvCKbvUZvodpVidnoYconst alice = Keypair.fromSecretKey(bs58.decode("4NMwxzmYj2uvHuq8xoqhY8RXg63KSVJM1DXkpbmkUY7YQWuoyQgFnnzn6yo3CMnqZasnNPNuAT2TLwQsCaKkUddp",),);// remember to create ATA firstlet ata = await getAssociatedTokenAddress(NATIVE_MINT, // mintalice.publicKey, // owner);let amount = 1 * 1e9; /* Wrapped SOL's decimals is 9 */let tx = new Transaction().add(// transfer SOLSystemProgram.transfer({fromPubkey: alice.publicKey,toPubkey: ata,lamports: amount,}),// sync wrapped SOL balancecreateSyncNativeInstruction(ata),);console.log(`txhash: ${await sendAndConfirmTransaction(connection, tx, [feePayer, alice])}`,);})();
2. By Token Transfer
add-balance-by-token.ts
import {clusterApiUrl,Connection,Keypair,Transaction,SystemProgram,sendAndConfirmTransaction,} from "@solana/web3.js";import {TOKEN_PROGRAM_ID,NATIVE_MINT,getMinimumBalanceForRentExemptAccount,getAssociatedTokenAddress,ACCOUNT_SIZE,createInitializeAccountInstruction,createTransferInstruction,createCloseAccountInstruction,} from "@solana/spl-token";import bs58 from "bs58";(async () => {// connectionconst connection = new Connection(clusterApiUrl("devnet"), "confirmed");// 5YNmS1R9nNSCDzb5a7mMJ1dwK9uHeAAF4CmPEwKgVWr8const feePayer = Keypair.fromSecretKey(bs58.decode("588FU4PktJWfGfxtzpAAXywSNt74AvtroVzGfKkVN1LwRuvHwKGr851uH8czM5qm4iqLbs1kKoMKtMJG4ATR7Ld2",),);// G2FAbFQPFa5qKXCetoFZQEvF9BVvCKbvUZvodpVidnoYconst alice = Keypair.fromSecretKey(bs58.decode("4NMwxzmYj2uvHuq8xoqhY8RXg63KSVJM1DXkpbmkUY7YQWuoyQgFnnzn6yo3CMnqZasnNPNuAT2TLwQsCaKkUddp",),);// remember to create ATA firstlet ata = await getAssociatedTokenAddress(NATIVE_MINT, // mintalice.publicKey, // owner);let auxAccount = Keypair.generate();let amount = 1 * 1e9; /* Wrapped SOL's decimals is 9 */let tx = new Transaction().add(// create token accountSystemProgram.createAccount({fromPubkey: alice.publicKey,newAccountPubkey: auxAccount.publicKey,space: ACCOUNT_SIZE,lamports:(await getMinimumBalanceForRentExemptAccount(connection)) + amount, // rent + amountprogramId: TOKEN_PROGRAM_ID,}),// init token accountcreateInitializeAccountInstruction(auxAccount.publicKey,NATIVE_MINT,alice.publicKey,),// transfer WSOLcreateTransferInstruction(auxAccount.publicKey,ata,alice.publicKey,amount,),// close aux accountcreateCloseAccountInstruction(auxAccount.publicKey,alice.publicKey,alice.publicKey,),);console.log(`txhash: ${await sendAndConfirmTransaction(connection, tx, [feePayer,auxAccount,alice,])}`,);})();