I'm trying to make a program to bulk transfer all nft's from one wallet to another. However, I can't seem to make the signing process work. I'm trying to use a piece of code from this tutorial on how to do this, but it is giving me an error saying:
Error: Returned error: invalid sender
at Object.ErrorResponse (C:\Users\x\Desktop)
at C:\Users\x\Desktop\nft-transfer\node_modules\web3-core-requestmanager\lib\index.js:300:36
at XMLHttpRequest.request.onreadystatechange (C:\Users\Gibbo\Desktop\nft-transfer\node_modules\web3-providers-http\lib\index.js:98:13)
Code:
async function transfer(token, tokenId) {
const contract = new web3.eth.Contract(abi, token);
const nonce = await web3.eth.getTransactionCount(publicKey);
try {
const tx = {
from: publicKey,
to: token,
gas: web3.utils.toHex(gasPrice),
value: 0,
chainId: 4,
nonce: nonce,
chain: "rinkeby",
baseChain: "rinkeby",
hardfork: "berlin",
data: contract.methods
.transferFrom(publicKey, toAddress, tokenId)
.encodeABI(),
};
const signedTx = await web3.eth.accounts.signTransaction(tx, privateKey);
const hash = await sendTx(signedTx);
return hash;
} catch (e) {
console.log(e)
}
}
const sendTx = (signedTx) =>
new Promise((resolve, reject) => {
web3.eth.sendSignedTransaction(signedTx.rawTransaction, (err, hash) => {
if (!err) {
console.log('The hash of your transaction is: ', hash);
return resolve(hash);
}
console.log(
'Something went wrong when submitting your transaction:',
err,
);
return reject(err);
});
});