Web3j: How to Decoding transaction input data

Created on 9 Apr 2018  路  5Comments  路  Source: web3j/web3j

Most helpful comment

Hope below code snippet will help you

 String inputData = "0xa9059cbb0000000000000000000000005c5212ed85cc957c6b656d209a7be8812ab00e330000000000000000000000000000000000000000000000008d8dadf544fc0000";
        String method = inputData.substring(0,10);
        System.out.println(method);
        String to = inputData.substring(10,74);
        String value = inputData.substring(74);
        Method refMethod = TypeDecoder.class.getDeclaredMethod("decode",String.class,int.class,Class.class);
        refMethod.setAccessible(true);
        Address address = (Address)refMethod.invoke(null,to,0,Address.class);
        System.out.println(address.toString());
        Uint256 amount = (Uint256) refMethod.invoke(null,value,0,Uint256.class);
        System.out.println(amount.getValue());

All 5 comments

i have the same question

Hope below code snippet will help you

 String inputData = "0xa9059cbb0000000000000000000000005c5212ed85cc957c6b656d209a7be8812ab00e330000000000000000000000000000000000000000000000008d8dadf544fc0000";
        String method = inputData.substring(0,10);
        System.out.println(method);
        String to = inputData.substring(10,74);
        String value = inputData.substring(74);
        Method refMethod = TypeDecoder.class.getDeclaredMethod("decode",String.class,int.class,Class.class);
        refMethod.setAccessible(true);
        Address address = (Address)refMethod.invoke(null,to,0,Address.class);
        System.out.println(address.toString());
        Uint256 amount = (Uint256) refMethod.invoke(null,value,0,Uint256.class);
        System.out.println(amount.getValue());

@ghqgfj thanks

@ghqgfj I got some input data that length less than 74

Espero que el c贸digo siguiente te ayude

 String inputData = "0xa9059cbb0000000000000000000000005c5212ed85cc957c6b656d209a7be8812ab00e330000000000000000000000000000000000000000000000008d8dadf544fc0000";
        String method = inputData.substring(0,10);
        System.out.println(method);
        String to = inputData.substring(10,74);
        String value = inputData.substring(74);
        Method refMethod = TypeDecoder.class.getDeclaredMethod("decode",String.class,int.class,Class.class);
        refMethod.setAccessible(true);
        Address address = (Address)refMethod.invoke(null,to,0,Address.class);
        System.out.println(address.toString());
        Uint256 amount = (Uint256) refMethod.invoke(null,value,0,Uint256.class);
        System.out.println(amount.getValue());

@ghqgfj

If I wanted to pass it to string it would be like that?

String cadena = (String)refMethod.invoke(null,to,0,String.class);

Was this page helpful?
0 / 5 - 0 ratings