3

I am trying to wrap SOL in a typescript application. I am looking at the example found at https://spl.solana.com/token#example-wrapping-sol-in-a-token.

I am running into issues importing createSyncNativeInstruction from @solana/spl-token. I don't see such a function defined in @solana/spl-token v0.2.0. I'm assuming it was removed and the docs were not updated. What is the most up to date and recommended way to wrap sol with JS on the latest spl-token implementation?

1

2 Answers 2

2

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

1
  • 1
    Could you extract the relevant code from the cookbook. In the case of cookbook links changing or other factors, your answer will still be useful
    – C.OG
    Commented Aug 13, 2022 at 19:22
1

You can use this package https://www.npmjs.com/package/wrap-sol.

import { wrapSol, unwrapSol } from 'wrap-sol';

const wrapSolTxSig = wrapSol(connection, signer, LAMPORTS_PER_SOL * 0.00001);

const unwrapSolTxSig = unwrapSol(connection, signer);

Dislaimer: I'm the author of the package

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.