0

I am using sepolia testnet, to make a signed transaction between two accounts locally, below is the code :---

let { Transaction: Tx }= require('ethereumjs-tx');
let {Web3} = require('web3');

const account1 = '0x2832B429C73f74Fd6B4612ff0b6c97801fd18553';
const account2 = '0x6a7e74b23019F03B2E287cb241eE520F875C7443';

const privatekey1 = Buffer.from('c0********************************************76','hex')
const privatekey2 = Buffer.from('8a********************************************30','hex')

const privateKey1Uint8Array = new Uint8Array(privatekey1);
const privateKey2Uint8Array = new Uint8Array(privatekey2);

const url = 'https://rpc-sepolia.rockx.com'

let web3 = new Web3(url);

web3.eth.getBalance(account1)
  .then((bal) => {
    const balance = web3.utils.fromWei(bal, 'ether');
    console.log('Balance:', balance);
  })
  .catch((err) => {
    console.error('Error:', err);
  });

web3.eth
  .getTransactionCount(account1)
  .then((txCount) => {
 
    // Build the transaction
    let txObject = {
      nonce: web3.utils.toHex(txCount),
      from: account1,
      to: account2,
      value: web3.utils.toHex(web3.utils.toWei('0.1', 'ether')),
      gasLimit: web3.utils.toHex(21000),
      gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei')),
      chainId: 11155111
    
    };

    

    // Sign the transaction
    const tx = new Tx(txObject);
    tx.sign(privateKey1Uint8Array)
    const rawArray = tx.raw
  

    const serielizedtransaction = tx.serialize();
    const raw = '0x' + serielizedtransaction.toString('hex');
    
    // BroadCast the transaction
    web3.eth
      .sendSignedTransaction(raw)
      .then((txHash) => {
        console.log("Transaction Hash", txHash);
      })
      .catch((err) => {
        console.error("Error:", err);
      });
  })
  .catch((err) => {
    console.error("Error:", err);
  });

The output:---

Balance: 0.399968499999769
Error: TransactionRevertInstructionError: Transaction has been reverted by the EVM
    at /home/runner/Check2/node_modules/web3-eth/lib/commonjs/utils/get_transaction_error.js:48:21
    at Generator.next (<anonymous>)
    at /home/runner/Check2/node_modules/web3-eth/lib/commonjs/utils/get_transaction_error.js:24:71
    at new Promise (<anonymous>)
    at __awaiter (/home/runner/Check2/node_modules/web3-eth/lib/commonjs/utils/get_transaction_error.js:20:12)
    at getTransactionError (/home/runner/Check2/node_modules/web3-eth/lib/commonjs/utils/get_transaction_error.js:33:12)
    at Object.<anonymous> (/home/runner/Check2/node_modules/web3-eth/lib/commonjs/rpc_method_wrappers.js:439:100)
    at Generator.next (<anonymous>)
    at fulfilled (/home/runner/Check2/node_modules/web3-eth/lib/commonjs/rpc_method_wrappers.js:21:58) {
  innerError: undefined,
  reason: 'err: insufficient funds for gas * price + value: address 0x2832B429C73f74Fd6B4612ff0b6c97801fd18553 have 399968499999769000 want 4284899617421126307357911857367734556889520 (supplied gas 21000)',
  signature: undefined,
  receipt: undefined,
  data: undefined,
  code: 402
}

I mean how it's possible that transaction requires '4284899617421126307357911857367734556889520 wei' which is '4284899617421126307357911.85736773455688952 eth' for execution? been stuck in this error for 2 days, any help appreciated!

Thanks!

2 Answers 2

0

I don't know,you can try:

const Web3 = require("web3");

const providerRPC = {
  development: "http://127.0.0.1:8545/",
  moonbase: "",
};
const web3 = new Web3(providerRPC.development); // Change to correct network

const account_from = {
  privateKey:
    "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80",
  address: "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
};
const addressTo = "0x70997970C51812dc3A010C7d01b50e0d17dc79C8";

const balances = async () => {
  // 4. Fetch balance info
  const balanceFrom = web3.utils.fromWei(
    await web3.eth.getBalance(account_from.address),
    "ether"
  );
  const balanceTo = web3.utils.fromWei(
    await web3.eth.getBalance(addressTo),
    "ether"
  );

  console.log(`The balance of ${account_from.address} is: ${balanceFrom} ETH`);
  console.log(`The balance of ${addressTo} is: ${balanceTo} ETH`);
};
const send = async () => {
  console.log("Transaction Start:");
  console.log(
    `Attempting to send transaction from ${account_from.address} to ${addressTo}`
  );

  const createTransaction = await web3.eth.accounts.signTransaction(
    {
      to: addressTo,
      value: web3.utils.toWei("10", "ether"),
      gas: web3.utils.toHex(21000),
      gasPrice: web3.utils.toHex(web3.utils.toWei("10", "gwei")),
    },
    account_from.privateKey
  );

  const createReceipt = await web3.eth.sendSignedTransaction(
    createTransaction.rawTransaction
  );
  console.log(
    `Transaction successful with hash: ${createReceipt.transactionHash}`
  );
  console.log("Transaction End!");
};
async function main() {
  await balances();
  await send();
  await balances();
}
main();

1
  • Thanks for the reply, but i think you are using Ganache local environment, would like to mention that , when I run my mentioned code using Ganache, it works fine but when I try this using the Ethereum testnet like Sepolia or Goerli it shows the error(in question), Any explanation about this will really help, thanks!
    – Shubham
    Jul 10, 2023 at 4:52
0

I followed the instructions as per infura docs on web3.js now its working

POV :- The code asked in question in 4-5 years old, many things have been changed as per new documentation. Please refer to that, Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.