1

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?

Amir Alam
  • 155
  • 1
  • 12
  • 1
    I know close to nothing about NFT management but is this related ? : https://stackoverflow.com/q/70024541/1606432 – Pogrindis Jan 21 '22 at 10:13
  • @Pogrindis much appreciated, but i already did it, it was done without phantom wallet but when i try to execute same transaction answered in this question, phantom gives me error, but wallet independent it is working fine – Amir Alam Jan 21 '22 at 10:16

1 Answers1

2

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

Jon C
  • 7,019
  • 10
  • 17
  • I could now easily transfer the tokens as you have mentioned, but now I want to make the user buy from my wallet and when i am trying to do it, it is giving me an error that an unknown signer if i am not transferring NFT from the connected wallet. means i can't transfer an nft from another wallet that is being connected on a phantom.Can you tell me please how can i solve this problem – Amir Alam Jan 22 '22 at 01:36
  • 1
    In that case, it's a very different solution, and you'll have to write an on-chain program to handle the exchange. Your program would transfer payment from the user's wallet to a wallet owned by the program, and then the NFT would be transferred from the program to the user. You can start with https://solanacookbook.com/references/anchor.html and move forward from there! – Jon C Jan 22 '22 at 10:09
  • but i can transfer nft from my Alice wallet to the connected wallet, from this example https://solanacookbook.com/references/token.html#create-token, and its working charmly. problem is that it has the secret key of the feePair but when i will implement this on real site, i cant have the secret key for the connected user – Amir Alam Jan 22 '22 at 10:56
  • 1
    That's true, wallets will not give you the secret key for the connected user, but they provide an interface to sign transactions. For that, you'll want to use a wallet adapter like https://github.com/solana-labs/wallet-adapter/ – Jon C Jan 22 '22 at 17:22
  • SO with walletadapter i can achieve this functionality where Alice can sell nft to any user who connects and wants to buy? – Amir Alam Jan 23 '22 at 12:52
  • 1
    Yep, and then you'll also need an on-chain program, as I recommended earlier. In order to transfer from Alice's wallet to the user's wallet, Alice will need to sign somehow. If you keep your secret key on the UI, then anyone could find it and steal the NFTs! On the other hand, an on-chain program can handle all the logic of transferring. – Jon C Jan 24 '22 at 10:31