I am trying to build an application, and part of it involves transferring an NFT from my account to the user's. Previously, I was using Moralis to accomplish this, but this code threw an error saying Error: Contract with a Signer cannot override from (operation="overrides.from", code=UNSUPPORTED_OPERATION, version=contracts/5.6.0)
My previous code was:
async function giveNFTtoUser(address, token) {
let userWallet = await getAddress();
let web3 = await Moralis.enableWeb3({
chainId: 0x13881,
privateKey:
"{private key for my account}"
});
console.log('in get nft')
const options = {
type: "erc721",
receiver: userWallet,
contractAddress: address,
tokenId: token
};
let transaction = await Moralis.transfer(options);
let result = await transaction.wait();
return result;
}
How could I do something like this in web3 or ethers, where I use a different account's private key to transfer an NFT from that account to the account of the user that is logged in?
I personally prefer ethers.js. The following frontend code allows users to transfer their NFTs into your account:
If you want your account to transfer it to someone else's, the code is slightly different, and should be run privately in the backend: