diff --git a/athena_abi/core.go b/athena_abi/core.go index 621c7b1e..3151afca 100644 --- a/athena_abi/core.go +++ b/athena_abi/core.go @@ -5,11 +5,9 @@ import ( "log" ) -type b256 [32]byte - type StarknetABI struct { ABIName *string - ClassHash b256 + ClassHash []byte Functions map[string]AbiFunction Events map[string]AbiEvent Constructor []AbiParameter @@ -17,45 +15,47 @@ type StarknetABI struct { ImplementedInterfaces map[string]AbiInterface } +// Declare errors +var ( + errParseDefinedTypes = errors.New("unable to parse defined types") + errParseInterfaces = errors.New("unable to parse interfaces") + errParseFunctions = errors.New("unable to parse functions") + errParseEvents = errors.New("unable to parse events") + errParseConstructor = errors.New("unable to parse constructor") + errParseL1Handler = errors.New("unable to parse L1 handler") + errParseImplementedInterfaces = errors.New("unable to parse implemented interfaces") +) + // Parse Starknet ABI from JSON // @param abiJSON // @param abiname // @param classHash -func StarknetAbiFromJSON(abiJson []map[string]interface{}, abiName string, classHash b256) (*StarknetABI, error) { - if abiJson == nil { - return nil, errors.New("unable to parse ABI") - } +func StarknetAbiFromJSON(abiJson []map[string]interface{}, abiName string, classHash []byte) (*StarknetABI, error) { groupedAbi := GroupAbiByType(abiJson) // Parse defined types (structs and enums) - if groupedAbi["type_def"] == nil { - log.Println("No type definitions found in ABI") - } definedTypes, err := ParseEnumsAndStructs(groupedAbi["type_def"]) if err != nil { sortedDefs, errDef := TopoSortTypeDefs(groupedAbi["type_def"]) - if errDef != nil { - return nil, errors.New("unable to parse types") + if errDef == nil { + defineTypes, errDtypes := ParseEnumsAndStructs(sortedDefs) + definedTypes = defineTypes + errDef = errDtypes } - defineTypes, errDtypes := ParseEnumsAndStructs(sortedDefs) - definedTypes = defineTypes - if errDtypes != nil { - return nil, errors.New("unable to parse types") + if errDef != nil { + return nil, errParseDefinedTypes } log.Println("ABI Struct and Enum definitions out of order & required topological sorting") } // Parse interfaces var definedInterfaces []AbiInterface - if groupedAbi["interface"] == nil { - log.Println("No interfaces found in ABI") - } for _, iface := range groupedAbi["interface"] { functions := []AbiFunction{} for _, funcData := range iface["items"].([]interface{}) { parsedAbi, errWhileParsing := ParseAbiFunction(funcData.(map[string]interface{}), definedTypes) if errWhileParsing != nil { - return nil, errors.New("unable to parse interfaces") + return nil, errParseInterfaces } functions = append(functions, *parsedAbi) } @@ -66,21 +66,17 @@ func StarknetAbiFromJSON(abiJson []map[string]interface{}, abiName string, class } // Parse functions - if groupedAbi["function"] == nil { - log.Println("No functions found in ABI") - } functions := make(map[string]AbiFunction) for _, functionData := range groupedAbi["function"] { funcName := functionData["name"].(string) abiFunc, errParsingFunctions := ParseAbiFunction(functionData, definedTypes) if errParsingFunctions != nil { - return nil, errors.New("unable to parse functions") + return nil, errParseFunctions } functions[funcName] = *abiFunc } // Add functions from interfaces - for _, iface := range definedInterfaces { for _, function := range iface.functions { functions[function.name] = function @@ -89,19 +85,14 @@ func StarknetAbiFromJSON(abiJson []map[string]interface{}, abiName string, class // Parse events parsedAbiEvents := []AbiEvent{} - if groupedAbi["event"] == nil { - log.Println("No events found in ABI") - } for _, eventData := range groupedAbi["event"] { parsedEvent, errParsingEvent := ParseAbiEvent(eventData, definedTypes) if errParsingEvent != nil { - return nil, errors.New("unable to parse events") + return nil, errParseEvents } parsedAbiEvents = append(parsedAbiEvents, *parsedEvent) } - if len(parsedAbiEvents) == 0 { - log.Println("No events found in ABI") - } + events := make(map[string]AbiEvent) for _, event := range parsedAbiEvents { if event.name != "" { @@ -116,7 +107,7 @@ func StarknetAbiFromJSON(abiJson []map[string]interface{}, abiName string, class param := paramData.(map[string]interface{}) typed, errorParsingType := parseType(param["type"].(string), definedTypes) if errorParsingType != nil { - return nil, errors.New("unable to parse constructor") + return nil, errParseConstructor } constructor = append(constructor, AbiParameter{ Name: param["name"].(string), @@ -132,7 +123,7 @@ func StarknetAbiFromJSON(abiJson []map[string]interface{}, abiName string, class if len(groupedAbi["l1_handler"]) == 1 { handler, errorParsingFunction := ParseAbiFunction(groupedAbi["l1_handler"][0], definedTypes) if errorParsingFunction != nil { - return nil, errors.New("unable to parse L1 handler") + return nil, errParseL1Handler } l1Handler = handler } else { @@ -143,7 +134,7 @@ func StarknetAbiFromJSON(abiJson []map[string]interface{}, abiName string, class implementedInterfaces := make(map[string]AbiInterface) implArray, ok := groupedAbi["impl"] if !ok { - return nil, errors.New("unable to parse implemented interfaces") + return nil, errParseImplementedInterfaces } for _, implData := range implArray { implMap := implData