@@ -537,6 +537,68 @@ public byte[] encodeMethodFromString(String abi, String methodName, List<String>
537537 throw new ContractCodecException (errorMsg );
538538 }
539539
540+ public byte [] encodeMethodFromStringByContractABIDefinition (
541+ ContractABIDefinition contractABIDefinition , String methodName , List <String > params )
542+ throws ContractCodecException {
543+ List <ABIDefinition > methods = contractABIDefinition .getFunctions ().get (methodName );
544+ if (methods == null ) {
545+ logger .debug (
546+ "Invalid methodName: {}, all the functions are: {}" ,
547+ methodName ,
548+ contractABIDefinition .getFunctions ());
549+ throw new ContractCodecException (
550+ "Invalid method "
551+ + methodName
552+ + " , supported functions are: "
553+ + contractABIDefinition .getFunctions ().keySet ());
554+ }
555+
556+ for (ABIDefinition abiDefinition : methods ) {
557+ if (abiDefinition .getInputs ().size () == params .size ()) {
558+ ABIObject inputObject = ABIObjectFactory .createInputObject (abiDefinition );
559+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
560+ try {
561+ byte [] methodId = abiDefinition .getMethodId (cryptoSuite );
562+ ABIObject abiObject = contractCodecJsonWrapper .encode (inputObject , params );
563+ byte [] encode = abiObject .encode (isWasm );
564+ outputStream .write (methodId );
565+ outputStream .write (encode );
566+ return outputStream .toByteArray ();
567+ } catch (Exception e ) {
568+ logger .error (" exception in encodeMethodFromString : {}" , e .getMessage ());
569+ }
570+ }
571+ }
572+
573+ String errorMsg =
574+ " cannot encode in encodeMethodFromString with appropriate interface ABI, make sure params match" ;
575+ logger .error (errorMsg );
576+ throw new ContractCodecException (errorMsg );
577+ }
578+
579+ public byte [] encodeMethodFromStringByABIDefinition (
580+ ABIDefinition abiDefinition , List <String > params ) throws ContractCodecException {
581+ if (abiDefinition .getInputs ().size () == params .size ()) {
582+ ABIObject inputObject = ABIObjectFactory .createInputObject (abiDefinition );
583+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
584+ try {
585+ byte [] methodId = abiDefinition .getMethodId (cryptoSuite );
586+ ABIObject abiObject = contractCodecJsonWrapper .encode (inputObject , params );
587+ byte [] encode = abiObject .encode (isWasm );
588+ outputStream .write (methodId );
589+ outputStream .write (encode );
590+ return outputStream .toByteArray ();
591+ } catch (Exception e ) {
592+ logger .error (
593+ " exception in encodeMethodFromStringByAbiDefinition : {}" , e .getMessage ());
594+ }
595+ }
596+ String errorMsg =
597+ " cannot encode in encodeMethodFromStringByAbiDefinition with appropriate interface ABI, make sure params match" ;
598+ logger .error (errorMsg );
599+ throw new ContractCodecException (errorMsg );
600+ }
601+
540602 public byte [] encodeMethodByIdFromString (String abi , byte [] methodId , List <String > params )
541603 throws ContractCodecException {
542604 ContractABIDefinition contractABIDefinition = this .abiDefinitionFactory .loadABI (abi );
@@ -1029,6 +1091,57 @@ public List<Object> decodeEvent(String abi, String eventName, EventLog log)
10291091 throw new ContractCodecException (errorMsg );
10301092 }
10311093
1094+ public List <Object > decodeEventByContractABIDefinition (
1095+ ContractABIDefinition contractABIDefinition , String eventName , EventLog log )
1096+ throws ContractCodecException {
1097+ List <ABIDefinition > events = contractABIDefinition .getEvents ().get (eventName );
1098+ if (events == null ) {
1099+ throw new ContractCodecException (
1100+ "Invalid event "
1101+ + eventName
1102+ + ", supported events are: "
1103+ + contractABIDefinition .getEvents ().keySet ());
1104+ }
1105+ for (ABIDefinition abiDefinition : events ) {
1106+ ABIObject inputObject = ABIObjectFactory .createEventInputObject (abiDefinition );
1107+ try {
1108+ List <Object > params = new ArrayList <>();
1109+ if (!log .getData ().equals ("0x" )) {
1110+ params =
1111+ ContractCodecTools .decodeJavaObject (inputObject , log .getData (), isWasm );
1112+ }
1113+ List <String > topics = log .getTopics ();
1114+ return this .mergeEventParamsAndTopics (abiDefinition , params , topics );
1115+ } catch (Exception e ) {
1116+ logger .error (" exception in decodeEventToObject : {}" , e .getMessage ());
1117+ }
1118+ }
1119+
1120+ String errorMsg = " cannot decode in decodeEventToObject with appropriate interface ABI" ;
1121+ logger .error (errorMsg );
1122+ throw new ContractCodecException (errorMsg );
1123+ }
1124+
1125+ public List <Object > decodeEventByAbiDefinition (ABIDefinition abiDefinition , EventLog log )
1126+ throws ContractCodecException {
1127+ ABIObject inputObject = ABIObjectFactory .createEventInputObject (abiDefinition );
1128+ try {
1129+ List <Object > params = new ArrayList <>();
1130+ if (!log .getData ().equals ("0x" )) {
1131+ params = ContractCodecTools .decodeJavaObject (inputObject , log .getData (), isWasm );
1132+ }
1133+ List <String > topics = log .getTopics ();
1134+ return this .mergeEventParamsAndTopics (abiDefinition , params , topics );
1135+ } catch (Exception e ) {
1136+ logger .error (" exception in decodeEventByAbiDefinition : {}" , e .getMessage ());
1137+ }
1138+
1139+ String errorMsg =
1140+ " cannot decode in decodeEventByAbiDefinition with appropriate interface ABI" ;
1141+ logger .error (errorMsg );
1142+ throw new ContractCodecException (errorMsg );
1143+ }
1144+
10321145 public List <Object > decodeEventByTopic (String abi , String eventTopic , EventLog log )
10331146 throws ContractCodecException {
10341147 ContractABIDefinition contractABIDefinition = this .abiDefinitionFactory .loadABI (abi );
@@ -1090,6 +1203,59 @@ public List<String> decodeEventToString(String abi, String eventName, EventLog l
10901203 throw new ContractCodecException (errorMsg );
10911204 }
10921205
1206+ public List <String > decodeEventToStringByContractABIDefinition (
1207+ ContractABIDefinition contractABIDefinition , String eventName , EventLog log )
1208+ throws ContractCodecException {
1209+ List <ABIDefinition > events = contractABIDefinition .getEvents ().get (eventName );
1210+ if (events == null ) {
1211+ throw new ContractCodecException (
1212+ "Invalid event "
1213+ + eventName
1214+ + ", current supported events are: "
1215+ + contractABIDefinition .getEvents ().keySet ());
1216+ }
1217+ for (ABIDefinition abiDefinition : events ) {
1218+ ABIObject inputObject = ABIObjectFactory .createEventInputObject (abiDefinition );
1219+ try {
1220+ List <String > params = new ArrayList <>();
1221+ if (!log .getData ().equals ("0x" )) {
1222+ params =
1223+ contractCodecJsonWrapper .decode (
1224+ inputObject , Hex .decode (log .getData ()), isWasm );
1225+ }
1226+ List <String > topics = log .getTopics ();
1227+ return this .mergeEventParamsAndTopicsToString (abiDefinition , params , topics );
1228+ } catch (Exception e ) {
1229+ logger .error (" exception in decodeEventToString : {}" , e .getMessage ());
1230+ }
1231+ }
1232+
1233+ String errorMsg = " cannot decode in decodeEventToString with appropriate interface ABI" ;
1234+ logger .error (errorMsg );
1235+ throw new ContractCodecException (errorMsg );
1236+ }
1237+
1238+ public List <String > decodeEventToStringByABIDefinition (
1239+ ABIDefinition abiDefinition , EventLog log ) throws ContractCodecException {
1240+ ABIObject inputObject = ABIObjectFactory .createEventInputObject (abiDefinition );
1241+ try {
1242+ List <String > params = new ArrayList <>();
1243+ if (!log .getData ().equals ("0x" )) {
1244+ params =
1245+ contractCodecJsonWrapper .decode (
1246+ inputObject , Hex .decode (log .getData ()), isWasm );
1247+ }
1248+ List <String > topics = log .getTopics ();
1249+ return this .mergeEventParamsAndTopicsToString (abiDefinition , params , topics );
1250+ } catch (Exception e ) {
1251+ logger .error (" exception in decodeEventToString : {}" , e .getMessage ());
1252+ }
1253+
1254+ String errorMsg = " cannot decode in decodeEventToString with appropriate interface ABI" ;
1255+ logger .error (errorMsg );
1256+ throw new ContractCodecException (errorMsg );
1257+ }
1258+
10931259 public List <String > decodeEventByTopicToString (String abi , String eventTopic , EventLog log )
10941260 throws ContractCodecException {
10951261 ContractABIDefinition contractABIDefinition = this .abiDefinitionFactory .loadABI (abi );
0 commit comments