Hey everyone! I’m trying to move some NFTs I created on OpenSea. They’re on the Polygon Mumbai network. I want to send them to other addresses using Alchemy Web3 in a Node.js API. No wallet is available, so I’m signing the transaction manually. Here’s my code:
async function transferNFT() {
require('dotenv').config();
const { TEST_API_ENDPOINT, SECRET_KEY } = process.env;
const { createAlchemyWeb3 } = require('@alch/alchemy-web3');
const web3Instance = createAlchemyWeb3(TEST_API_ENDPOINT);
const senderWallet = '0x1234...';
const currentNonce = await web3Instance.eth.getTransactionCount(senderWallet, 'latest');
const txDetails = {
// Not sure about this part
nft: {
id: '0xabcd...',
},
gasLimit: 60000,
recipient: '0x5678...',
amount: 1,
nonce: currentNonce
};
const signedTransaction = await web3Instance.eth.accounts.signTransaction(txDetails, SECRET_KEY);
web3Instance.eth.sendSignedTransaction(signedTransaction.rawTransaction, (err, txHash) => {
if (err) {
console.error('Oops! Transaction failed:', err);
} else {
console.log('Success! Transaction hash:', txHash);
}
});
}
transferNFT();
I think the transaction object isn’t right. What should I put there? Any help would be awesome!
hey, try using the erc721 transferFrom method. update ur txDetails so ‘to’ is the contract address and ‘data’ is built via web3.eth.abi.encodeFunctionCall with sender, recipient and token id. good luck!
heya strummingmelody! i’ve been playing around with nft transfers on polygon mumbai too. it’s pretty cool stuff, right?
looks like you’re close, but yeah the txDetails needs a bit of tweaking. have you considered using the erc721 interface? that might make things easier.
what if you tried something like this:
const nftContract = new web3Instance.eth.Contract(ERC721ABI, nftContractAddress);
const txDetails = {
from: senderWallet,
to: nftContractAddress,
gas: 250000, // might need to adjust this
data: nftContract.methods.transferFrom(senderWallet, recipientAddress, tokenId).encodeABI(),
nonce: currentNonce
};
just make sure you’ve got the right abi and contract address. oh, and don’t forget to replace tokenId with the actual id of the nft you’re sending!
btw, what kind of nfts are you working with? anything cool? 
I’ve worked with NFT transfers on Polygon Mumbai, and your approach is on the right track. However, you’re correct that the transaction object needs adjustment. For NFT transfers, you should use the ERC-721 transfer function. Here’s how to modify your txDetails:
const contract = new web3Instance.eth.Contract(ERC721_ABI, NFT_CONTRACT_ADDRESS);
const txDetails = {
from: senderWallet,
to: NFT_CONTRACT_ADDRESS,
gas: 300000,
data: contract.methods.transferFrom(senderWallet, recipientAddress, tokenId).encodeABI(),
nonce: currentNonce
};
Make sure to replace ERC721_ABI with the actual ABI, NFT_CONTRACT_ADDRESS with your contract’s address, and set the correct tokenId. Also, consider using a higher gas limit for NFT transfers. This structure should work for your Polygon Mumbai NFT transfers via Alchemy.