javascriptsolanasolana-web3jssolana-program-libraryphantom-wallet

How to transfer NFT spl-token using phantom wallet and solana web3js


I can transfer the Solana from one account to another account using phantom wallet using this code

const transferTransaction = new Transaction()
  .add(SystemProgram.transfer({
    fromPubkey: alice.publicKey,
    toPubkey: feePayer.publicKey,
    lamports: lamportsToSend
  }))
  
  const network = "https://api.devnet.solana.com";
const connection = new Connection(network);
transferTransaction.recentBlockhash = (await connection.getRecentBlockhash()).blockhash;
transferTransaction.feePayer = alice.publicKey;
const { signature } = await window.solana.signAndSendTransaction(transferTransaction);
await connection.confirmTransaction(signature);

  console.log(signature);

but I am wondering how can I transfer the NFT if I have the nft minted address?


Solution

  • To transfer an NFT, you first need to find out the address of the NFT's mint and the owner's address. Then instead of calling SystemProgram.transfer, you'll use Token.createTransferCheckedInstruction.

    There's a great example at the Solana Cookbook for transferring SPL tokens: https://solanacookbook.com/references/token.html#transfer-token