[Ethereum] decode ethereum input data

raw-transactiontransactionsweb3js

Howsit guys, I am busy going through the mempool and trying to track various transactions and parts of transactions. As a first step I thought I would list transactions and the bits I might need later on. Here comes the issue.

I cannot get any information out of the transaction input. I have tried all the previous solutions and packages recommended but to no avail.

When I use the web3.utils.toAscii I get gibberish.
When I use ethereum-tx-decoders decodeTx I get invalid arrayify value.
when I use any of the other web3 tools I get an error.

Please find the latest itteration of my code below after many hours of attempting all the solutions I could find (above are the three most recent failures)

const txDecoder = require("ethereum-tx-decoder");
const express = require("express");
require("dotenv").config();
const Web3 = require("web3");
const curl = `wss://mainnet.infura.io/ws/v3/${process.env.infuraProjId}`;
const web3 = new Web3(new Web3.providers.WebsocketProvider(curl));

let counter = 0;

const subscription = web3.eth.subscribe(
    "pendingTransactions",
    (error, transactionHash) => {
        if (!error) {
            web3.eth
                .getTransaction(transactionHash)
                .then((tx) => {
                    if (tx !== null) {
                        counter++;
                        // console.log(tx.hash);

                        const decodedInput = txDecoder.decodeTx(tx.input);
                        console.log(decodedInput);

                       
                    }
                })
                .catch((err) => console.error(err));
        } else {
            console.error(error);
        }
    }
);
// .on("data", (transactionHash) => {
//     web3.eth.getTransaction(transactionHash).then((transaction) => {
//         console.log("coming from data");
//         console.log(transaction.from, transaction.to);
//     });
// }); 

// ^^ this kept returning null

Please and thank you

Best Answer

you are missing a vital piece. You wont be able to decode the input or logs until you have the ABI of the "receiver" contract for TX input and the FROM contract for the logs.

To get the ABI: either use etherscan, use https://sourcify.dev/ or find it in IPFS/BZZ by reading the bytecode and parsing out the address if the solidity compiler was allowed to upload it.

AFTER you have the ABI, you pass it to the ethersjs/web3js/web3py and those libraries will be able to parse the input/output.

Edit: Also please keep in mind: if the receiver contract is a "Proxy" contract, you will then need to hunt down the "end receiver" contract address from the internal transactions.