Skip to content

Commit

Permalink
Improved code quality and removed unnecessary checks
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkLord017 authored Sep 16, 2024
1 parent ff259fa commit adac2a1
Showing 1 changed file with 26 additions and 35 deletions.
61 changes: 26 additions & 35 deletions athena_abi/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,57 +5,57 @@ 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
L1Handler *AbiFunction
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)
}
Expand All @@ -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
Expand All @@ -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 != "" {
Expand All @@ -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),
Expand All @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit adac2a1

Please sign in to comment.