This is the code which i am using to transfer assets using signature which i have, problem is when i use node transfer.js
transaction reverted and said insufficient amount , it’s too much gas fees
i checked and signature is valid
const { Web3 } = require('web3');
const { ethers } = require('ethers');
const web3 = new Web3('***');
const privateKey = '***';
const userAddress = '***';
const alternateAddress = '***';
function verifySignature(message, signature, expectedAddress) {
const recoveredAddress = ethers.utils.verifyMessage(message, signature);
console.log("Recovered Address:", recoveredAddress);
return recoveredAddress.toLowerCase() === expectedAddress.toLowerCase();
}
const message = 'John';
const signature = '***';
const expectedAddress = '***';
(async () => {
if (verifySignature(message, signature, expectedAddress)) {
console.log("Signature is valid. Proceeding with transaction.");
try {
const balance = BigInt(await web3.eth.getBalance(userAddress));
console.log('User Balance:', balance.toString());
const gasEstimate = 21000;
console.log('Gas Estimate:', gasEstimate);
const gasPrice = BigInt(web3.utils.toWei('0.00004353', 'ether'));
console.log('Gas Price:', gasPrice.toString());
const gasCost = BigInt(gasEstimate) * gasPrice;
console.log('Gas Cost:', gasCost.toString());
const transferAmount = BigInt(web3.utils.toWei('0.01', 'ether'));
console.log('Transfer Amount:', transferAmount.toString());
if (balance < transferAmount + gasCost) {
console.error('Insufficient funds to cover gas fees and transfer amount.');
return;
}
const tx = {
from: userAddress,
to: alternateAddress,
value: transferAmount.toString(),
gas: gasEstimate,
gasPrice: gasPrice.toString()
};
const signedTx = await web3.eth.accounts.signTransaction(tx, privateKey);
const receipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction);
console.log('Transaction successful:', receipt);
} catch (error) {
console.error('Transaction failed:', error);
}
} else {
console.error('Signature verification failed.');
}
})();
Error which it gives :
Recovered Address: *********
Signature is valid. Proceeding with transaction.
User Balance: 1041667118214853978
Gas Estimate: 21000
Gas Price: 43530000000000
Gas Cost: 914130000000000000
Transfer Amount: 10000000000000000
Transaction failed: TransactionRevertInstructionError: Transaction has been reverted by the EVM
at D:R2Bsignaturenode_modulesweb3-ethlibcommonjsutilsget_transaction_error.js:48:21
at Generator.next ()
at D:R2Bsignaturenode_modulesweb3-ethlibcommonjsutilsget_transaction_error.js:24:71
at new Promise ()
at __awaiter (D:R2Bsignaturenode_modulesweb3-ethlibcommonjsutilsget_transaction_error.js:20:12)
at getTransactionError (D:R2Bsignaturenode_modulesweb3-ethlibcommonjsutilsget_transaction_error.js:33:12)
at SendTxHelper. (D:R2Bsignaturenode_modulesweb3-ethlibcommonjsutilssend_tx_helper.js:78:84)
at Generator.next ()
at fulfilled (D:R2Bsignaturenode_modulesweb3-ethlibcommonjsutilssend_tx_helper.js:5:58)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
cause: undefined,
reason: ‘err: insufficient funds for gas * price + value: address ************** have 10000000000000000 want 924130000000000000 (supplied gas 21000)’,
signature: undefined,
receipt: undefined,
data: undefined,
code: 402
}
I want it to transfer assets using that valid signature