Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to decode UInt256 #103

Open
jcai opened this issue Sep 12, 2023 · 7 comments
Open

How to decode UInt256 #103

jcai opened this issue Sep 12, 2023 · 7 comments

Comments

@jcai
Copy link

jcai commented Sep 12, 2023

I want to decode amount parameter value in usdt contract transfer method.I learn from https://developers.tron.network/docs/parameter-encoding-and-decoding

   val rawAmount = TypeDecoder.decodeStaticStruct(DATA.substring(72,136),0, 
                                    new TypeReference[Uint256](){}); 

But occur some error:

java.lang.RuntimeException: TypeReferenced struct must contain a constructor with types that extend Type
	at org.tron.trident.abi.TypeDecoder.lambda$decodeStaticStructElement$3(TypeDecoder.java:348)
	at java.base/java.util.Optional.orElseThrow(Optional.java:408)
	at org.tron.trident.abi.TypeDecoder.decodeStaticStructElement(TypeDecoder.java:346)
	at org.tron.trident.abi.TypeDecoder.decodeStaticStruct(TypeDecoder.java:329)
@endiaoekoe
Copy link
Contributor

endiaoekoe commented Sep 13, 2023

@jcai I understand you want to decode the amount parameter value in the USDT transfer method, by learning from the Tron documentation example.
The issue is that USDT is an TRC20 token, so the amount parameter in its transfer method is a uint256, which is a primitive type rather than a struct in Solidity.
However, the TypeDecoder's decodeStaticStruct requires a struct type to work properly. So trying to decodeStaticStruct directly on the byte slice of amount will result in the error.

you may try to use decodeUintAsInt method instead in TypeDecoder.java.
or use decodeNumeric instead, The sample code is as below:
Uint256 rawAmount = TypeDecoder.decodeNumeric(DATA.substring(72,136), Uint256.class); //amount

@jcai
Copy link
Author

jcai commented Sep 15, 2023

@mikeluxue Thank your reply.The TypeDecoder only have two public methods.

public static <T extends Array> T decode(String input, int offset, TypeReference<T> typeReference) 
public static <T extends Type> T decodeStaticStruct(String input, int offset, TypeReference<T> typeReference) 

@endiaoekoe
Copy link
Contributor

endiaoekoe commented Sep 15, 2023

@mikeluxue Thank your reply.The TypeDecoder only have two public methods.

public static <T extends Array> T decode(String input, int offset, TypeReference<T> typeReference) 
public static <T extends Type> T decodeStaticStruct(String input, int offset, TypeReference<T> typeReference) 

@jcai wow, sorry I missed checking the privilege of these methods. Obviously, these methods should be set to public privilege.
may submit a PR to optimize this issue later.
by the way, consider it needs some time before the fix. you can manually change the code and change these methods to 'public' and compile/install locally.
for the usage of these methods,you can check the unit test code in testUintDecode() https://github.com/tronprotocol/trident/blob/main/trident-java/abi/src/test/java/org/tron/trident/abi/TypeDecoderTest.java#L77

@jcai
Copy link
Author

jcai commented Sep 15, 2023

@mikeluxue Thank you very much.When I want to parse some address of the block 54724400 ,some error occurs:

Exception in thread "main" java.lang.RuntimeException: Input is too large to put in byte array of size 21
	at org.tron.trident.utils.Numeric.toBytesPadded(Numeric.java:192)
	at org.tron.trident.abi.datatypes.Address.toString(Address.java:69)

My code is:

 val rawRecipient   = TypeDecoder.decodeStaticStruct(DATA.substring(8, 72), 0, new TypeReference[Address]() {}); //recipient address
 val receiveAddress = rawRecipient.toString;
   

DATA is
a9059cbb000000008cad2cef099fcfa65b6907386224d796acd2ddb9af120c5196e0b1c40000000000000000000000000000000000000000000000000000000000e7ef00

transaction link: https://tronscan.org/#/transaction/cd8f71299e97ddfc35170a79414db6d4d879327fdd368a5ebb9c47e0d0bfc9fd

finally I found Trident can't parse the recipient address 000000008cad2cef099fcfa65b6907386224d796acd2ddb9af120c5196e0b1c4

@jcai
Copy link
Author

jcai commented Sep 16, 2023

@mikeluxue Thank you very much.When I want to parse some address of the block 54724400 ,some error occurs:

Exception in thread "main" java.lang.RuntimeException: Input is too large to put in byte array of size 21
	at org.tron.trident.utils.Numeric.toBytesPadded(Numeric.java:192)
	at org.tron.trident.abi.datatypes.Address.toString(Address.java:69)

My code is:

 val rawRecipient   = TypeDecoder.decodeStaticStruct(DATA.substring(8, 72), 0, new TypeReference[Address]() {}); //recipient address
 val receiveAddress = rawRecipient.toString;
   

DATA is a9059cbb000000008cad2cef099fcfa65b6907386224d796acd2ddb9af120c5196e0b1c40000000000000000000000000000000000000000000000000000000000e7ef00

transaction link: https://tronscan.org/#/transaction/cd8f71299e97ddfc35170a79414db6d4d879327fdd368a5ebb9c47e0d0bfc9fd

finally I found Trident can't parse the recipient address 000000008cad2cef099fcfa65b6907386224d796acd2ddb9af120c5196e0b1c4

If I use TypeDecoder.decodeAddress ,it works well.

@endiaoekoe
Copy link
Contributor

@jcai you can see there are many data types in https://github.com/tronprotocol/trident/tree/main/trident-java/abi/src/main/java/org/tron/trident/abi/datatypes ,including StaticStruct and Address type. from my understanding, the StaticStruct type is a custom type similar to Object class in Java, which you can use TypeDecoder.decodeStaticStruct to decode this type of data. the type Address, is kind of a build-in type, which you can use TypeDecoder.decodeAddress to decode.
image

In one word, different data types correspond to different decoders。
in your case. the type Address can not be seen as a StaticStruct type, so it should not be decoded using decodeStaticStruct method.

@jcai
Copy link
Author

jcai commented Sep 22, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants