1616
1717package com .klaytn .caver .abi ;
1818
19+ import com .klaytn .caver .abi .datatypes .*;
1920import com .klaytn .caver .contract .ContractEvent ;
2021import com .klaytn .caver .contract .ContractIOType ;
2122import com .klaytn .caver .contract .ContractMethod ;
22- import org .web3j .abi .*;
23- import org .web3j .abi .datatypes .*;
2423import org .web3j .crypto .Hash ;
2524import org .web3j .utils .Numeric ;
2625
3130import java .util .List ;
3231import java .util .stream .Collectors ;
3332
33+ import static com .klaytn .caver .abi .TypeEncoder .isDynamic ;
34+
3435/**
3536 * Representing a ABI type encode / decode.
3637 */
@@ -48,7 +49,7 @@ public static String encodeFunctionCall(ContractMethod method, List<Object> para
4849 List <String > solTypeList = new ArrayList ();
4950
5051 for (ContractIOType contractIOType : method .getInputs ()) {
51- solTypeList .add (contractIOType .getType ());
52+ solTypeList .add (contractIOType .getTypeAsString ());
5253 }
5354
5455 return encodeFunctionCall (functionSignature , solTypeList , params );
@@ -133,12 +134,13 @@ public static String buildFunctionString(ContractMethod method) {
133134 result .append (method .getName ());
134135 result .append ("(" );
135136 String params = method .getInputs ().stream ()
136- .map (ContractIOType ::getType )
137+ .map (ContractIOType ::getTypeAsString )
137138 .collect (Collectors .joining ("," ));
138139 result .append (params );
139140 result .append (")" );
140141
141- return result .toString ();
142+ //remove "tuple" string
143+ return result .toString ().replace ("tuple" , "" );
142144 }
143145
144146 /**
@@ -171,12 +173,13 @@ public static String buildEventString(ContractEvent event) {
171173 result .append (event .getName ());
172174 result .append ("(" );
173175 String params = event .getInputs ().stream ()
174- .map (ContractIOType ::getType )
176+ .map (ContractIOType ::getTypeAsString )
175177 .collect (Collectors .joining ("," ));
176178 result .append (params );
177179 result .append (")" );
178180
179- return result .toString ();
181+ //remove "tuple" string
182+ return result .toString ().replace ("tuple" , "" );
180183 }
181184
182185 /**
@@ -209,7 +212,7 @@ public static String encodeParameter(String solidityType, Object value) throws C
209212 public static String encodeParameters (ContractMethod method , List <Object > values ) throws ClassNotFoundException , NoSuchMethodException , InvocationTargetException , InstantiationException , IllegalAccessException {
210213 List <String > solTypeList = new ArrayList <>();
211214 for (ContractIOType type : method .getInputs ()) {
212- solTypeList .add (type .getType ());
215+ solTypeList .add (type .getTypeAsString ());
213216 }
214217
215218 return encodeParameters (solTypeList , values );
@@ -251,25 +254,26 @@ public static String encodeParameter(Type parameter) {
251254 * @return String
252255 */
253256 public static String encodeParameters (List <Type > parameters ) {
254- int dynamicDataOffset = getLength (parameters ) * Type .MAX_BYTE_LENGTH ;
255- StringBuilder result = new StringBuilder ();
256- StringBuilder dynamicData = new StringBuilder ();
257-
258- for (Type parameter :parameters ) {
259- String encodedValue = TypeEncoder .encode (parameter );
260-
261- if (isDynamic (parameter )) {
262- String encodedDataOffset = TypeEncoder .encode (new Uint (BigInteger .valueOf (dynamicDataOffset )));
263- result .append (encodedDataOffset );
264- dynamicData .append (encodedValue );
265- dynamicDataOffset += encodedValue .length () >> 1 ;
266- } else {
267- result .append (encodedValue );
268- }
269- }
270- result .append (dynamicData );
271-
272- return result .toString ();
257+ return new DefaultFunctionEncoder ().encodeParameters (parameters );
258+ // int dynamicDataOffset = getLength(parameters) * Type.MAX_BYTE_LENGTH;
259+ // StringBuilder result = new StringBuilder();
260+ // StringBuilder dynamicData = new StringBuilder();
261+ //
262+ // for (Type parameter:parameters) {
263+ // String encodedValue = TypeEncoder.encode(parameter);
264+ //
265+ // if (isDynamic(parameter)) {
266+ // String encodedDataOffset = TypeEncoder.encode(new Uint(BigInteger.valueOf(dynamicDataOffset)));
267+ // result.append(encodedDataOffset);
268+ // dynamicData.append(encodedValue);
269+ // dynamicDataOffset += encodedValue.length() >> 1;
270+ // } else {
271+ // result.append(encodedValue);
272+ // }
273+ // }
274+ // result.append(dynamicData);
275+ //
276+ // return result.toString();
273277 }
274278
275279 /**
@@ -311,7 +315,7 @@ public static List<Type> decodeParameters(ContractMethod method, String encoded)
311315 List <TypeReference <Type >> resultParams = new ArrayList <>();
312316
313317 for (ContractIOType ioType : method .getOutputs ()) {
314- resultParams .add (TypeReference .makeTypeReference (ioType .getType ()));
318+ resultParams .add (TypeReference .makeTypeReference (ioType .getTypeAsString ()));
315319 }
316320
317321 return FunctionReturnDecoder .decode (encoded , resultParams );
@@ -331,9 +335,9 @@ public static EventValues decodeLog(List<ContractIOType> inputs, String data, Li
331335
332336 for (ContractIOType input : inputs ) {
333337 if (input .isIndexed ()) {
334- indexedList .add (TypeReference .makeTypeReference (input .getType ()));
338+ indexedList .add (TypeReference .makeTypeReference (input .getTypeAsString ()));
335339 } else {
336- nonIndexedList .add (TypeReference .makeTypeReference (input .getType ()));
340+ nonIndexedList .add (TypeReference .makeTypeReference (input .getTypeAsString ()));
337341 }
338342 }
339343
@@ -349,21 +353,21 @@ public static EventValues decodeLog(List<ContractIOType> inputs, String data, Li
349353 return new EventValues (indexedValues , nonIndexedValues );
350354 }
351355
352- private static int getLength (List <Type > parameters ) {
353- int count = 0 ;
354- for (Type type :parameters ) {
355- if (type instanceof StaticArray ) {
356- count += ((StaticArray ) type ).getValue ().size ();
357- } else {
358- count ++;
359- }
360- }
361- return count ;
362- }
363-
364- private static boolean isDynamic (Type parameter ) {
365- return parameter instanceof DynamicBytes
366- || parameter instanceof Utf8String
367- || parameter instanceof DynamicArray ;
368- }
356+ // private static int getLength(List<Type> parameters) {
357+ // int count = 0;
358+ // for (Type type:parameters) {
359+ // if (type instanceof StaticArray) {
360+ // count += ((StaticArray) type).getValue().size();
361+ // } else {
362+ // count++;
363+ // }
364+ // }
365+ // return count;
366+ // }
367+
368+ // private static boolean isDynamic(Type parameter) {
369+ // return parameter instanceof DynamicBytes
370+ // || parameter instanceof Utf8String
371+ // || parameter instanceof DynamicArray;
372+ // }
369373}
0 commit comments