Skip to content

Commit

Permalink
support eth.FixedSizeArrayType for ethereum codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduard-Voiculescu committed Jun 28, 2024
1 parent 29f6620 commit 3fcb39a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
11 changes: 10 additions & 1 deletion ethfull/abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,7 @@ func (e *protoCallModel) populateFields(call *eth.MethodDef) error {

e.Fields = append(e.Fields, protoField{Name: fieldName, Type: fieldType})
}

for _, parameter := range call.ReturnParameters {
fieldName := xstrings.ToSnakeCase("output_" + parameter.Name)
fieldName = codegen.SanitizeProtoFieldName(fieldName)
Expand Down Expand Up @@ -577,6 +578,14 @@ func getProtoFieldType(solidityType eth.SolidityType) string {
case eth.SignedFixedPointType, eth.UnsignedFixedPointType:
return "string"

case eth.FixedSizeArrayType:
// Flaky, I think we should support a single level of "array"
fieldType := getProtoFieldType(v.ElementType)
if fieldType == SKIP_FIELD {
return SKIP_FIELD
}
return "repeated " + fieldType

case eth.ArrayType:
// Flaky, I think we should support a single level of "array"
fieldType := getProtoFieldType(v.ElementType)
Expand All @@ -585,7 +594,7 @@ func getProtoFieldType(solidityType eth.SolidityType) string {
}
return "repeated " + fieldType

case eth.StructType, eth.FixedSizeArrayType:
case eth.StructType:
return SKIP_FIELD

default:
Expand Down
45 changes: 41 additions & 4 deletions ethfull/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,9 +423,16 @@ func generateFieldSqlTypes(fieldType eth.SolidityType) string {
case eth.SignedFixedPointType, eth.UnsignedFixedPointType:
return "DECIMAL"

case eth.StructType, eth.FixedSizeArrayType:
case eth.StructType:
return SKIP_FIELD

case eth.FixedSizeArrayType:
elemType := generateFieldSqlTypes(v.ElementType)
if elemType == "" || elemType == SKIP_FIELD {
return SKIP_FIELD
}

return elemType + "[]"
case eth.ArrayType:
elemType := generateFieldSqlTypes(v.ElementType)
if elemType == "" || elemType == SKIP_FIELD {
Expand Down Expand Up @@ -465,6 +472,19 @@ func generateFieldTableChangeCode(fieldType eth.SolidityType, fieldAccess string
case eth.SignedFixedPointType, eth.UnsignedFixedPointType:
return "set", fmt.Sprintf("BigDecimal::from_str(&%s).unwrap()", fieldAccess)

case eth.FixedSizeArrayType:
// FIXME: Implement multiple contract support, check what is the actual semantics there
_, inner := generateFieldTableChangeCode(v.ElementType, "x", byRef)
if inner == SKIP_FIELD {
return SKIP_FIELD, SKIP_FIELD
}

iter := "into_iter()"
if byRef {
iter = "iter()"
}

return "set_psql_array", fmt.Sprintf("%s.%s.map(|x| %s).collect::<Vec<_>>()", fieldAccess, iter, inner)
case eth.ArrayType:
// FIXME: Implement multiple contract support, check what is the actual semantics there
_, inner := generateFieldTableChangeCode(v.ElementType, "x", byRef)
Expand All @@ -479,7 +499,7 @@ func generateFieldTableChangeCode(fieldType eth.SolidityType, fieldAccess string

return "set_psql_array", fmt.Sprintf("%s.%s.map(|x| %s).collect::<Vec<_>>()", fieldAccess, iter, inner)

case eth.StructType, eth.FixedSizeArrayType:
case eth.StructType:
return SKIP_FIELD, SKIP_FIELD

default:
Expand Down Expand Up @@ -516,6 +536,20 @@ func generateFieldTransformCode(fieldType eth.SolidityType, fieldAccess string,
case eth.SignedFixedPointType, eth.UnsignedFixedPointType:
return fmt.Sprintf("%s.to_string()", fieldAccess)

case eth.FixedSizeArrayType:
inner := generateFieldTransformCode(v.ElementType, "x", byRef)
if inner == SKIP_FIELD {
fmt.Println("skip case eth.FixedSizeArrayType:")
return SKIP_FIELD
}

iter := "into_iter()"
if byRef {
iter = "iter()"
}

return fmt.Sprintf("%s.%s.map(|x| %s).collect::<Vec<_>>()", fieldAccess, iter, inner)

case eth.ArrayType:
inner := generateFieldTransformCode(v.ElementType, "x", byRef)
if inner == SKIP_FIELD {
Expand All @@ -529,7 +563,7 @@ func generateFieldTransformCode(fieldType eth.SolidityType, fieldAccess string,

return fmt.Sprintf("%s.%s.map(|x| %s).collect::<Vec<_>>()", fieldAccess, iter, inner)

case eth.StructType, eth.FixedSizeArrayType:
case eth.StructType:
return SKIP_FIELD

default:
Expand Down Expand Up @@ -566,7 +600,10 @@ func generateFieldGraphQLTypes(fieldType eth.SolidityType) string {
case eth.ArrayType:
return "[" + generateFieldGraphQLTypes(v.ElementType) + "]!"

case eth.StructType, eth.FixedSizeArrayType:
case eth.FixedSizeArrayType:
return "[" + generateFieldGraphQLTypes(v.ElementType) + "]!"

case eth.StructType:
return SKIP_FIELD

default:
Expand Down

0 comments on commit 3fcb39a

Please sign in to comment.