Skip to main content
extracted
Source Link
vicyyn
  • 4.3k
  • 6
  • 31

How to manage wrapped SOL

you can use thisWrapped SOL 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 Create Token Account 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

let tx = new Transaction().add(
  // trasnfer SOL
  SystemProgram.transfer({
    fromPubkey: alice.publicKey,
    toPubkey: ata,
    lamports: amount,
  }),
  // sync wrapped SOL balance
  createSyncNativeInstruction(ata)
);

2. By Token Transfer

let tx = new Transaction().add(
  // create token account
  SystemProgram.createAccount({
    fromPubkey: alice.publicKey,
    newAccountPubkey: auxAccount.publicKey,
    space: ACCOUNT_SIZE,
    lamports:
      (await getMinimumBalanceForRentExemptAccount(connection)) + amount, // rent + amount
    programId: TOKEN_PROGRAM_ID,
  }),
  // init token account
  createInitializeAccountInstruction(
    auxAccount.publicKey,
    NATIVE_MINT,
    alice.publicKey
  ),
  // transfer WSOL
  createTransferInstruction(auxAccount.publicKey, ata, alice.publicKey, amount),
  // close aux account
  createCloseAccountInstruction(
    auxAccount.publicKey,
    alice.publicKey,
    alice.publicKey
  )
);

example from solana cookbooksource

you can use this example from solana cookbook

How to manage wrapped SOL

Wrapped SOL 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 Create Token Account 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

let tx = new Transaction().add(
  // trasnfer SOL
  SystemProgram.transfer({
    fromPubkey: alice.publicKey,
    toPubkey: ata,
    lamports: amount,
  }),
  // sync wrapped SOL balance
  createSyncNativeInstruction(ata)
);

2. By Token Transfer

let tx = new Transaction().add(
  // create token account
  SystemProgram.createAccount({
    fromPubkey: alice.publicKey,
    newAccountPubkey: auxAccount.publicKey,
    space: ACCOUNT_SIZE,
    lamports:
      (await getMinimumBalanceForRentExemptAccount(connection)) + amount, // rent + amount
    programId: TOKEN_PROGRAM_ID,
  }),
  // init token account
  createInitializeAccountInstruction(
    auxAccount.publicKey,
    NATIVE_MINT,
    alice.publicKey
  ),
  // transfer WSOL
  createTransferInstruction(auxAccount.publicKey, ata, alice.publicKey, amount),
  // close aux account
  createCloseAccountInstruction(
    auxAccount.publicKey,
    alice.publicKey,
    alice.publicKey
  )
);

source

Source Link
vicyyn
  • 4.3k
  • 6
  • 31

you can use this example from solana cookbook