0

I was trying to save the content of a donation form to a blockchain but when I try I get Error: Returned error: invalid sender at Object.ErrorResponse. Can someone please help me?

const contractABI = require('./build/contracts/DonationContract.json');
const { Transaction } = require('ethereumjs-tx');

const MAX_TIME_FRAME = 60; // Adjust the maximum time frame as needed
const provider =new Web3.providers.HttpProvider('https://sepolia.infura.io/v3/+ infura key');//'https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID'
const web3 = new Web3(provider);
require('dotenv').config();


try {
    
    const networkId = await web3.eth.net.getId();
    const contractAddress = "0x";
    const contract = new web3.eth.Contract(contractABI.abi, contractAddress);

    const transactionObject =  contract.methods.createDonation(
      formData.street_address,
      formData.pickup_date,
      formData.pickup_time,
      formData.availability_date,
      formData.pickup_hours,
      formData.item_type,
      formData.other_item,
      formData.item_description,
      formData.quantity,
      formData.requires_refrigeration,
      formData.best_consumed_date,
      formData.partial_donation
    ); 
    const privateKey = process.env.PRIVATE_KEY;
    const fromAddress = 'etheruem address';


    const tx = new Transaction({
      nonce: await web3.eth.getTransactionCount(fromAddress),
      to: contractAddress,
      data: transactionObject.encodeABI(),
      gasPrice: web3.utils.toHex(await web3.eth.getGasPrice()),
      gasLimit: web3.utils.toHex(500000),
      value: web3.utils.toHex(0),
    });

    tx.sign(Buffer.from(privateKey, 'hex'));

    const serializedTx = tx.serialize();

    const receipt = await web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'))
    {
      if (!err) {
        console.log(result);
      };
    }

Can someone please assist me.

Yilmaz
  • 35,338
  • 10
  • 157
  • 202
Tinawaa1
  • 7
  • 2
  • I added this line to my code as suggested, but it generated another error: const tx = new Transaction(Tx, {'chain':'sepolia', 'chainId':'11155111'}); Error: Chain with name sepolia not supported at Common._getChainParams (/ – Tinawaa1 Jun 06 '23 at 21:21

1 Answers1

0

From docs example, you have to mention the chain

var tx = new Tx(rawTx, {'chain':'ropsten'});

If you read the note on the same link

When using ethereumjs-tx@2.0.0 if you don’t specify the parameter chain it will use mainnet by default.

Yilmaz
  • 35,338
  • 10
  • 157
  • 202
  • I try that and i am getting an error Error: Chain with name sepolia not supported at Common._getChainParams (/home/cougarnet.uh.edu/fmnartey/AI-FEED/donation-module/node_modules/ethereumjs-common/dist/index.js:58:15) at Common.setChain (/home/c – Tinawaa1 Jun 06 '23 at 16:50
  • can you update npm packages to the latest version – Yilmaz Jun 06 '23 at 23:49
  • Done but its still not working – Tinawaa1 Jun 07 '23 at 19:27