GarouDan
GarouDan

Reputation: 3833

How can we Wrap and Unwrap SOL using the Solana Web3 TS/JS library?

Using Solana Web3 TS library how can we wrap and unwrap SOL?

Here on this link it is possible to see what the SPL Token CLI program does for wrapping and unwrapping SOL, but the conversion of those methods to TypesScript is not straightforward.

How can we programmatically create 2 instructions, one for wrapping SOL and the other one for unwrapping it, so we could include them in a transaction and test them?

Upvotes: 1

Views: 2368

Answers (2)

c_e_nwadike
c_e_nwadike

Reputation: 11

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

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

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

const unwrappingTxSig = unwrapSol(connection, signer);

Disclaimer: I'm the author of the package

Upvotes: 1

Jon C
Jon C

Reputation: 8472

If you look at the implementation that you linked, you'll see that wrapping simply involves doing system_program::create_account and spl_token::initialize_account using the native mint address.

In TS, there's a helper called createWrappedNativeAccount at https://github.com/solana-labs/solana-program-library/blob/master/token/js/src/actions/createWrappedNativeAccount.ts, which you use as:

        const account = await createWrappedNativeAccount(
            connection,
            payer,
            owner,
            lamports,
        );

Upvotes: 1

Related Questions