I’m struggling to transfer an NFT using the @hashgraph/sdk package for Hedera. The NFT is already linked to the seller’s account and shows up in the HashPack NFT section. But when I try to use the TransferTransaction method from @hashgraph/sdk@2.17.1, I get an error.
Here’s a simplified version of what I’m trying:
const moveNFT = async (nftId, from, to) => {
const hederaClient = await setupClient();
const tokenId = TokenId.fromString(nftId);
const senderKey = PrivateKey.fromString(await fetchKey(from));
const receiverKey = PrivateKey.fromString(await fetchKey(to));
try {
const transfer = await new TransferTransaction()
.addNftTransfer(tokenId, 1, from, to)
.addHbarTransfer(from, new Hbar(1))
.addHbarTransfer(to, new Hbar(-1))
.freezeWith(hederaClient)
.sign(senderKey);
const signedTransfer = await transfer.sign(receiverKey);
const result = await signedTransfer.execute(hederaClient);
const receipt = await result.getReceipt(hederaClient);
console.log(`NFT transfer status: ${receipt.status}`);
return receipt.status;
} catch (err) {
console.error('NFT transfer failed:', err);
}
};
But I keep getting a ‘TOKEN_NOT_ASSOCIATED_TO_ACCOUNT’ error. Any ideas on what I’m doing wrong or how to fix this?