How Do I "Unwrap Sol" Using the Web3 Sdk?

I need to unwrap some SOL using the web3 sdk. I'm wondering, Is it just a simple transfer from the wrapped account into SOL account or is it more complicated than that? Below Is just some sample code I've setup.

const emptyWSolAccount = async (amount:any) => {
  const wsolAddress = await Token.getAssociatedTokenAddress(
    ASSOCIATED_TOKEN_PROGRAM_ID,
    TOKEN_PROGRAM_ID,
    new PublicKey("So11111111111111111111111111111111111111112"),
    wallet.publicKey
  );

  const wsolAccount = await connection.getAccountInfo(wsolAddress);

  if (wsolAccount) {
    const transaction = new Transaction({
      feePayer: wallet.publicKey,
    });
    const instructions = [];

    const lamports = amount * LAMPORTS_PER_SOL;
    
    instructions.push(
      SystemProgram.transfer({
        fromPubkey: wsolAddress,
        toPubkey: wallet.publicKey,
        lamports: lamports, // 1 sol
      })
    );

    transaction.add(...instructions);
    transaction.recentBlockhash = await (
      await connection.getRecentBlockhash()
    ).blockhash;
    transaction.partialSign(wallet.payer);
    const result = await connection.sendTransaction(transaction, [
      wallet.payer,
    ]);
    console.log({ result });
    return { result };
  }

  return ;
};

1 Answer

This is not really explained anywhere, but unwrapping SOL actually means closing the wrapped SOL account.

So instead of using a SystemProgram.transfer instruction, you'll do:

    instructions.push(
      splToken.instructions.createCloseAccountInstruction(
        wsolAddress,
        wallet.publicKey,
        wallet.publicKey,
      )
    );

This closes wsolAddress and sends the contents to wallet, assuming that wallet is the owner.

You can see the CLI implementation at

Your Answer

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

Robert Thorne

Robert Thorne

Automotive & Future Transportation Editor

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.

Share this article
Twitter Facebook Pinterest