1

I want to transfer a token from one wallet to another using solana web3.js but am confused on how to. I found this link which does go over how SPL tokens are transferred but there are a few missing variables I am not sure about.

const transaction = new web3.Transaction().add(
  splToken.Token.createTransferInstruction(
    splToken.TOKEN_PROGRAM_ID,
    fromTokenAccount.address,
    toTokenAccount.address,
    account.publicKey,
    [],
    1
  )
);

Like here, what is splToken, the token program ID, fromTokenAccount, and toTokenAccount, and what can these values be for Wrapped Sol.

1 Answer 1

3

Let's look at the definition of createTransferInstruction at https://github.com/solana-labs/solana-program-library/blob/e651623033fca7997ccd21e55d0f2388473122f9/token/js/src/instructions/transfer.ts#L36:

/**
 * Construct a Transfer instruction
 *
 * @param source       Source account
 * @param destination  Destination account
 * @param owner        Owner of the source account
 * @param amount       Number of tokens to transfer
 * @param multiSigners Signing accounts if `owner` is a multisig
 * @param programId    SPL Token program account
 *
 * @return Instruction to add to a transaction
 */
export function createTransferInstruction(
    source: PublicKey,
    destination: PublicKey,
    owner: PublicKey,
    amount: number | bigint,
    multiSigners: (Signer | PublicKey)[] = [],
    programId = TOKEN_PROGRAM_ID
): TransactionInstruction {

You'll notice that the mint isn't included in a normal transfer instruction, so in your case, you'll just need:

createTransferInstruction(
    fromTokenAccount.address,
    toTokenAccount.address,
    account.publicKey,
    1,
    [],
    TOKEN_PROGRAM_ID,
  )

If you do any instruction that does require the mint address, like transferChecked, you can use the NATIVE_MINT of So11111111111111111111111111111111111111112 defined at https://github.com/solana-labs/solana-program-library/blob/e651623033fca7997ccd21e55d0f2388473122f9/token/js/src/constants.ts#L13

Edit: based on the cookbook link you provided, you can press "</>" to view the whole source, but here are the specific answers:

  • splToken is const splToken = require("@solana/spl-token");, so you just need to import the spl-token JS library
  • The token program id is the account at address TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA, given by splToken.TOKEN_PROGRAM_ID
  • fromTokenAccount and toTokenAccount are accounts that hold wrapped SOL. Typically, the easiest way to create them is to use the "associated token account" for the native mint mentioned earlier

You can read more about how the token program works at https://spl.solana.com/token#example-wrapping-sol-in-a-token and the associated-token-account program at https://spl.solana.com/associated-token-account

2
  • I am still not sure on how to transfer the WSol from one account to another, if you can read the question again you'll understand what are the things I was missing on.
    – Zero
    Commented Mar 7 at 8:03
  • Sure, no problem, I tried to clarify your specific questions
    – Jon C
    Commented Mar 7 at 12:52

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.