I’m building a dApp where users can stake NFTs for rewards. I’m stuck trying to move NFTs from user wallets to my app’s wallet. Here’s what I’ve tried:
const transferNFT = async (tokenId, sender, receiver) => {
const connection = new Web3Connection('https://devnet.solana.com');
const tokenPublicKey = new Web3Key(tokenId);
const senderPublicKey = sender.publicKey;
const receiverPublicKey = new Web3Key('APP_WALLET_ADDRESS');
const tokenInstance = new NFTToken(connection, tokenPublicKey, NFT_PROGRAM_ID, sender.payer);
// Get sender and receiver token accounts
const senderTokenAccount = await NFTToken.getAssociatedAccount(tokenInstance.associatedProgramId, tokenInstance.programId, tokenPublicKey, senderPublicKey);
const receiverTokenAccount = await NFTToken.getAssociatedAccount(tokenInstance.associatedProgramId, tokenInstance.programId, tokenPublicKey, receiverPublicKey);
const receiverInfo = await connection.getAccountInfo(receiverTokenAccount);
const instructions = [];
if (!receiverInfo) {
instructions.push(NFTToken.createAssociatedAccountInstruction(tokenInstance.associatedProgramId, tokenInstance.programId, tokenPublicKey, receiverTokenAccount, receiverPublicKey, senderPublicKey));
}
instructions.push(NFTToken.createTransferInstruction(NFT_PROGRAM_ID, senderTokenAccount, receiverTokenAccount, senderPublicKey, [], 1));
const tx = new Web3Transaction().add(...instructions);
if (tx) {
const result = await sender.sendTransaction(tx, connection);
console.log('Transfer result:', result);
} else {
console.log('Transfer failed: No transaction data');
}
};
When I run this, the user gets a prompt to approve, but then I get an error about invalid account data. I’ve tried different program IDs, but no luck. Any ideas what’s wrong?
yo sophiaAtom88, sounds like ur havin a tough time with those nft transfers. have u tried using @solana/web3.js instead of Web3Connection? it’s more up-to-date and might solve ur issues. also, double-check ur program IDs and make sure ur using the right network (devnet vs mainnet). good luck with ur project!
I encountered similar issues when working on NFT transfers in Solana. One crucial step I found was to ensure the correct mint authority is set. Double-check that your app’s wallet has the necessary permissions to transfer the NFT.
Another potential issue could be with the account data structure. Solana’s account model is quite specific, and any mismatch can lead to errors. I’d recommend using the @solana/spl-token library for handling token operations - it abstracts away some of the complexities.
Have you verified the token accounts exist and have the correct balance? Sometimes, the error you’re seeing can occur if the sender’s token account is empty or if the receiver’s account doesn’t exist.
Lastly, consider using the Phantom Wallet adapter if you haven’t already. It simplifies the connection process and handles some edge cases automatically.
hey there sophiaAtom88! interesting project you’ve got going on with nft staking. i’ve been playing around with solana nft transfers too and ran into similar issues. 
have you double-checked that you’re using the correct token program id? for nfts, it should be ‘TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA’. also, make sure you’re handling the case where the receiver doesn’t have an associated token account yet.
one thing that helped me was using the metaplex js sdk alongside web3.js. it simplifies a lot of the nft-specific stuff. maybe give that a try?
btw, are you testing this on devnet or mainnet? sometimes devnet can be a bit finicky.
curious to hear if any of this helps or if you’ve made any progress! keep us posted 