Skip to content

Commit

Permalink
commit with debug statements
Browse files Browse the repository at this point in the history
  • Loading branch information
rishikpulhani3106 committed Sep 24, 2024
1 parent 1b3d6c3 commit f93f72a
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 99 deletions.
1 change: 1 addition & 0 deletions athena_abi/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func StarknetAbiFromJSON(abiJson []map[string]interface{}, abiName string, class
// Parse events
parsedAbiEvents := []AbiEvent{}
fmt.Println("now parsing events")
fmt.Println("the grouped abi events is ", groupedAbi["event"])
for _, eventData := range groupedAbi["event"] {
fmt.Println("eventdata is ", eventData)
parsedEvent, errParsingEvent := ParseAbiEvent(eventData, definedTypes)
Expand Down
44 changes: 35 additions & 9 deletions athena_abi/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,21 @@ func extractInnerType(abiType string) string {

// The function takes in a list of type definitions (dict) and returns a dict of sets (map[string]bool)
func BuildTypeGraph(typeDefs []map[string]interface{}) map[string]map[string]bool {
fmt.Println()
fmt.Println("typdefs os ", typeDefs)
fmt.Println()
fmt.Println("the types of typdefs is ", reflect.TypeOf(typeDefs))
fmt.Println()
outputGraph := make(map[string]map[string]bool)
for _, typeDef := range typeDefs {
referencedTypes := []string{}
fmt.Println("the typedef is ", typeDef)
fmt.Println()
fmt.Println("the types of typdef is ", reflect.TypeOf(typeDef))
fmt.Println()
fmt.Println("the type of typedef.member", reflect.TypeOf(typeDef["members"])) //here reflect.typeof() retunrs the inner type stored in the interface always
if typeDef["type"] == "struct" {
for _, member := range typeDef["members"].([]map[string]interface{}) {
for _, member := range typeDef["members"].([]map[string]interface{}) { //here this works as typeDef["members"] is of interface{} type not a slice of interface so a single type assertion will work
referencedTypes = append(referencedTypes, member["type"].(string))
}
} else {
Expand Down Expand Up @@ -269,7 +279,7 @@ func parseType(abiType string, customTypes map[string]interface{}) (StarknetType
fmt.Println("abitype is", abiType)
parts := strings.Split(abiType, "::")[1:]
fmt.Println("c")
fmt.Println(parts)
fmt.Println("the parts is ", parts)

switch {
case len(parts) == 1 && parts[0] == "felt252":
Expand Down Expand Up @@ -420,6 +430,7 @@ func parseAbiParameters(names []string, types []string, customTypes map[string]i
if err != nil {
return nil, err
}
fmt.Println("the res id qwerty", res)
outputParameters = append(outputParameters, AbiParameter{
Name: names[i],
Type: res,
Expand Down Expand Up @@ -503,17 +514,28 @@ func ParseAbiEvent(abiEvent map[string]interface{}, customTypes map[string]inter
if value, exists := abiEvent["kind"]; exists {
if value == "struct" {
//eventParameters = abiEvent["members"].([]map[string]interface{})
fmt.Println()
fmt.Println("abievent.members is ", abiEvent["members"])
fmt.Println()
eventMembers := abiEvent["members"].([]interface{}) // Assert as []interface{}
eventParameters := make([]map[string]interface{}, len(eventMembers))

for i, member := range eventMembers {
eventParameters[i] = member.(map[string]interface{}) // Assert each element as map[string]interface{}
fmt.Println("eventmembers is ", eventMembers)
fmt.Println()
//eventParameters := make([]map[string]interface{}, len(eventMembers))

for _, member := range eventMembers {
fmt.Println("the memberbeing added is ", member)
fmt.Println("asdf if is running")
mem, _ := member.(map[string]interface{}) // Assert each element as map[string]interface{}
eventParameters = append(eventParameters, mem)
fmt.Println("the eventparameter status is ", eventParameters)
}
} else {
fmt.Println("asdf else is running")
fmt.Print("in parsig abi event the else1 nil was there")
return nil, nil
}
} else if inputs, ok := abiEvent["inputs"].([]map[string]interface{}); ok {
fmt.Println("searching for inputs")
for _, e := range inputs {
eventParameter := map[string]interface{}{"kind": "data"}
for k, v := range e {
Expand All @@ -523,6 +545,7 @@ func ParseAbiEvent(abiEvent map[string]interface{}, customTypes map[string]inter
}
} else if data, ok := abiEvent["data"].([]interface{}); ok {
fmt.Println(ok)
fmt.Println("searching for data")
var result []map[string]interface{}
for _, item := range data {
fmt.Println("item is of type ", reflect.TypeOf(item))
Expand Down Expand Up @@ -576,8 +599,10 @@ func ParseAbiEvent(abiEvent map[string]interface{}, customTypes map[string]inter
}

} else {
fmt.Println("the type is ", reflect.TypeOf(abiEvent["data"]))
fmt.Println("the data is ", abiEvent["data"])
fmt.Println("not found anything now in else")
//fmt.Println("the type is ", reflect.TypeOf(abiEvent["data"]))
//fmt.Println("the data is ", abiEvent["data"])
fmt.Println("asdf else 3 is running")
data, ok := abiEvent["data"].([]map[string]interface{})
fmt.Println("data is", data)
fmt.Println("ok is ", ok)
Expand All @@ -592,7 +617,8 @@ func ParseAbiEvent(abiEvent map[string]interface{}, customTypes map[string]inter
types = append(types, eventParameter["type"].(string))
names = append(names, eventParameter["name"].(string))
}

fmt.Println("types is ", types)
fmt.Println("names is ", names)
decodedParams, err := parseAbiParameters(
names,
types,
Expand Down
106 changes: 106 additions & 0 deletions athena_abi/parse_full_abi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package athena_abi

import (
"encoding/hex"
"encoding/json"
"fmt"
"testing"

Expand Down Expand Up @@ -185,3 +186,108 @@ func TestLoadWildcardArraySyntax(t *testing.T) {
expectedStructIdStr := "{key:Felt,value:Felt}"
assert.Equal(t, expectedStructIdStr, structType.idStr(), "Struct idStr should match expected")
}

func TestWildcardSizeSyntax(t *testing.T) {
// felt* syntax length parameter can be calldata_len or calldata_size
abiFunction := map[string]interface{}{
"inputs": []interface{}{
map[string]interface{}{"name": "selector", "type": "felt"},
map[string]interface{}{"name": "calldata_size", "type": "felt"},
map[string]interface{}{"name": "calldata", "type": "felt*"},
},
"name": "__default__",
"outputs": []interface{}{
map[string]interface{}{"name": "retdata_size", "type": "felt"},
map[string]interface{}{"name": "retdata", "type": "felt*"},
},
"type": "function",
}

customTypes := make(map[string]interface{})
parsedAbiFunc, err := ParseAbiFunction(abiFunction, customTypes)

assert.NoError(t, err, "ParseAbiFunction should not return an error")
assert.NotNil(t, parsedAbiFunc, "ParseAbiFunction should return a non-nil result")

assert.Len(t, parsedAbiFunc.inputs, 2, "There should be 2 inputs")

assert.Equal(t, "selector", parsedAbiFunc.inputs[0].Name, "First input name should be 'selector'")
assert.Equal(t, Felt, parsedAbiFunc.inputs[0].Type, "First input type should be Felt")

assert.Equal(t, "calldata", parsedAbiFunc.inputs[1].Name, "Second input name should be 'calldata'")
assert.IsType(t, StarknetArray{}, parsedAbiFunc.inputs[1].Type, "Second input type should be StarknetArray")

calldataType, ok := parsedAbiFunc.inputs[1].Type.(StarknetArray)
assert.True(t, ok, "Second input type should be castable to StarknetArray")
assert.Equal(t, Felt, calldataType.InnerType, "Inner type of calldata should be Felt")
}
func TestNoStructDefinition(t *testing.T) {
// Assuming NO_STRUCT_ABI_DEFINITION is a JSON string
var abiJson []map[string]interface{}
err := json.Unmarshal([]byte(NO_STRUCT_ABI_DEFINITION), &abiJson) // Unmarshal JSON string into abiJson
assert.NoError(t, err, "Error unmarshalling ABI for no_struct")

classHash, err := hex.DecodeString(NO_STRUCT_CLASS_HASH[2:])
assert.NoError(t, err, "Error decoding class hash for no_struct")

// Decode ABI using StarknetAbiFromJSON
decoder, err := StarknetAbiFromJSON(abiJson, "no_struct", classHash)
assert.NoError(t, err, "Error decoding ABI for no_struct")
assert.NotNil(t, decoder, "Expected decoder to be non-nil for no_struct")
}
func TestFeltTypes(t *testing.T) {
// Assuming VERSION_0_ABI_DEFINITION is a JSON string
var abiJson []map[string]interface{}
err := json.Unmarshal([]byte(VERSION_0_ABI_DEFINITION), &abiJson) // Unmarshal JSON string into abiJson
assert.NoError(t, err, "Error unmarshalling ABI for felt_types")

classHash, err := hex.DecodeString(VERSION_0_CLASS_HASH[2:])
assert.NoError(t, err, "Error decoding class hash for felt_types")

// Decode ABI using StarknetAbiFromJSON
decoder, err := StarknetAbiFromJSON(abiJson, "felt_types", classHash)
assert.NoError(t, err, "Error decoding ABI for felt_types")
assert.NotNil(t, decoder, "Expected decoder to be non-nil for felt_types")
}

// Test for parsing event keys from the ERC20 ABI definition
func TestParseEventKeys(t *testing.T) {
// Load the ABI for erc20_key_events with version 2
abiJson, err := loadAbi("erc20_key_events", 2)
assert.NoError(t, err, "Error loading ABI for erc20_key_events")

// Decode the class hash from a hex string
classHash, err := hex.DecodeString("0261ad90e1901833f794ee3d69816846f68ddb4fb7bb9ffec2d8f0c8608e298d")
assert.NoError(t, err, "Error decoding class hash for erc20_key_events")

// Decode the ABI using StarknetAbiFromJSON
parsedAbi, err := StarknetAbiFromJSON(abiJson, "erc20_key_events", classHash)
fmt.Println("parsedabi is helc ", parsedAbi)
fmt.Println("the err is helc ", err)
assert.NoError(t, err, "Error parsing ABI for erc20_key_events")
assert.NotNil(t, parsedAbi, "Parsed ABI should not be nil for erc20_key_events")

// Access the "Approval" event from the parsed ABI
approveEvent, ok := parsedAbi.Events["Approval"]
assert.True(t, ok, "Approval event should be found in the parsed ABI")
fmt.Println("approve event ", approveEvent)
// Validate the event's parameters
expectedParameters := []string{"owner", "spender", "value"}
assert.Equal(t, expectedParameters, approveEvent.parameters, "Expected parameters do not match")

// Validate the event's keys
expectedKeys := map[string]StarknetType{ //confirm this change
"owner": ContractAddress,
"spender": ContractAddress,
}
assert.Equal(t, expectedKeys, approveEvent.keys, "Expected keys do not match") //

// Validate the event's data
expectedData := map[string]StarknetType{ //confirm this change
"value": U256,
}
assert.Equal(t, expectedData, approveEvent.data, "Expected data do not match") //

// Validate the event's name
assert.Equal(t, "Approval", approveEvent.name, "Expected event name does not match")
}
90 changes: 0 additions & 90 deletions backup.txt

This file was deleted.

0 comments on commit f93f72a

Please sign in to comment.