1

**I'm trying to decode the inputValue of an transaction using web3j without using any contract abi and wrapper class,Im loading the contract using its contract address ,is there any way/inbuilt function to decode inputValue **

    List<TransactionHistory> history = new ArrayList<TransactionHistory>();
    BigInteger block = web3.ethBlockNumber().send().getBlockNumber();
    int block_no = block.intValue();
    // Class obj[]= new Class[array_length]

    TransactionHistory ts[] = new TransactionHistory[block_no];

    while (block_no >= 0) {

        List<EthBlock.TransactionResult> txs = web3
                .ethGetBlockByNumber(DefaultBlockParameter.valueOf(BigInteger.valueOf(block_no)), true).send()
                .getBlock().getTransactions();
        txs.forEach(tx -> {
            int i = 0;
            EthBlock.TransactionObject transaction = (EthBlock.TransactionObject) tx.get();
            if ((transaction.getFrom().toLowerCase()).equals(address.toLowerCase())) {
                // System.out.println("***************GETTING INSDIE OF IF LOOP***********");
                ts[i] = new TransactionHistory();
                ts[i].setFrom(transaction.getFrom());
                ts[i].setTo(transaction.getTo());
                ts[i].setBlockNumber("" + transaction.getBlockNumber());
                ts[i].setGasPrice("" + transaction.getGasPrice());
                ts[i].setNonce("" + transaction.getNonce());
                history.add(ts[i]);
                i++;
            }

            /*
             * if (transaction.getHash().equals(
             * "0x72d108f9e1f08dd23df9d31cf8a0e954644c8944f89430247c7ecaa3b3cec3ad")) {
             * System.out.println("TransactionAdapter to address: " + transaction.getTo());
             * System.out.println("Input: " + transaction.getInput()); }
             */

            BigInteger val = BigInteger.valueOf(transaction.getInput().hashCode());
            String txHash = transaction.getBlockHash();
            // transaction.setInput(val.toString());
            System.out.println("*******" + "\nGET INPUT " + transaction.getInput() + "\nBlockNumber: "
                    + transaction.getBlockNumber() + "\n From: " + transaction.getFrom() + "\n To:"
                    + transaction.getTo() + "\n Nonce: " + transaction.getNonce() + "\n BlockHash:"
                    + transaction.getBlockHash() + "\n GasPrice:" + transaction.getGasPrice());



            // transaction.getTransactionIndex();
            BigInteger transactionIndex1 = transaction.getTransactionIndex();
            String size="0xfdacd5760000000000000000000000000000000000000000000000000000000000000001";
            System.out.println(transaction.getValue());//getting zero value

            System.out.println(transaction.getTo());//not getting address of receiver
            // to fetch sentTo and transaction value->
            String inputData =transaction.getInput();//trying to decode this function

            if(inputData.length()>size.length()) {
                Erc20.getIputDataDecoder(inputData);
            }else {
                    System.out.println("INPUT DATA IS INVALID HENCE ,Sent Value:"+transaction.getValue()+"  Sent to contract "+transaction.getTo()+"\nBlockNumber:"+transaction.getBlockNumber());
            }
        });
        block_no--;

**` here is an function where im trying to decode inputValue **

private static void getIputDataDecoder(String inputData) {
// TODO Auto-generated method stub
String method = inputData.substring(0,10);
System.out.println(method);
String to = inputData.substring(10,74);
String value1 = inputData.substring(74);
Method refMethod = null;
try {
    refMethod = TypeDecoder.class.getDeclaredMethod("decode",String.class,int.class,Class.class);
} catch (NoSuchMethodException | SecurityException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
refMethod.setAccessible(true);
Address address1 = null;
try {
    address1 = (Address)refMethod.invoke(null,to,0,Address.class);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
System.out.println("SENT TO: "+address1.toString());
Uint256 amount = null;
try {
    amount = (Uint256) refMethod.invoke(null,value1,0,Uint256.class);

if(amount!=null) {
System.out.println("Value Sent: "+amount.getValue());
}
}catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

}

 List<EthBlock.TransactionResult> txs = web3
                .ethGetBlockByNumber(DefaultBlockParameter.valueOf(BigInteger.valueOf(block_no)), true).send()
                .getBlock().getTransactions();
        txs.forEach(tx -> {
            int i = 0;
            EthBlock.TransactionObject transaction = (EthBlock.TransactionObject) tx.get();

transaction.getValue();//having problem in these two functions,getting zero value transaction.getTo();//this one too,getting contract address instead of receivers address System.out.println(transaction.getValue()); System.out.println(transaction.getTo()); }

  • Yes, should be pretty easy. – goodvibration Dec 13 '19 at 11:35
  • @goodvibration how can i do it ? – Aniket Dec 13 '19 at 11:38
  • How can I answer that when I don't see your code in front of me??? – goodvibration Dec 13 '19 at 11:41
  • @goodvibration here's an code snippet ,where im trying to fetch transaction details – Aniket Dec 13 '19 at 11:44
  • Wait, are you looking to encode an input value before sending the transaction? I don't quite understand the meaning of "decoding an input" (i.e., either you encode an input or you decode an output). – goodvibration Dec 13 '19 at 11:49
0
    ERC20 javaToken = ERC20.load(contractAddress, web3, creds, new DefaultGasProvider());
    System.out.println("ALL HISTORY OF TT");
    javaToken.transferEventFlowable(DefaultBlockParameterName.EARLIEST, DefaultBlockParameterName.LATEST)
     .subscribe(event
     -> System.out.println("from: " + event._from + ", to: " + event._to + ", value: " + event._value));    

This function helped me to reach my goal

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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