Skip to content

Commit

Permalink
Bunch of type fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
0x19 committed Jun 16, 2024
1 parent 4ce8f44 commit 27ff826
Show file tree
Hide file tree
Showing 17 changed files with 364 additions and 27 deletions.
1 change: 0 additions & 1 deletion abi/constructor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package abi

import (
"fmt"

"github.com/unpackdev/solgo/ir"
)

Expand Down
13 changes: 11 additions & 2 deletions abi/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
ast_pb "github.com/unpackdev/protos/dist/go/ast"
"github.com/unpackdev/solgo/ast"
"github.com/unpackdev/solgo/ir"
"strings"
)

// Contract represents a collection of Ethereum contract methods.
Expand Down Expand Up @@ -128,10 +129,18 @@ func (b *Builder) buildMethodIO(method MethodIO, typeDescr *ast.TypeDescription)
method.Inputs = append(method.Inputs, inputList...)
method.Outputs = append(method.Outputs, outputList...)
case "contract":
method.Type = "address"
if strings.ContainsAny(typeDescr.GetString(), "[]") {
method.Type = "address[]"
} else {
method.Type = "address"
}
method.InternalType = typeDescr.GetString()
case "enum":
method.Type = "uint8"
if strings.ContainsAny(typeDescr.GetString(), "[]") {
method.Type = "uint8[]"
} else {
method.Type = "uint8"
}
method.InternalType = typeDescr.GetString()
case "struct":
return b.resolver.ResolveStructType(typeDescr)
Expand Down
6 changes: 4 additions & 2 deletions accounts/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const (
// It embeds ClientPool for network interactions and KeyStore for account management.
// It also includes fields for account details, network information, and additional tags.
type Account struct {
client *clients.Client `json:"-" yaml:"-"` // Client for Ethereum client interactions
client *clients.Client
*keystore.KeyStore `json:"-" yaml:"-"` // KeyStore for managing account keys
Address common.Address `json:"address" yaml:"address"` // Ethereum address of the account
Type utils.AccountType `json:"type" yaml:"type"` // Account type
Expand Down Expand Up @@ -79,7 +79,7 @@ func (a *Account) GetClient() *clients.Client {
// It does not affect the real balance on the Ethereum network.
func (a *Account) SetAccountBalance(ctx context.Context, amount *big.Int) error {
amountHex := common.Bytes2Hex(amount.Bytes())
return a.client.GetRpcClient().Call(nil, "anvil_setBalance", a.GetAddress(), amountHex)
return a.client.GetRpcClient().CallContext(ctx, nil, "anvil_setBalance", a.GetAddress(), amountHex)
}

// Balance retrieves the account's balance from the Ethereum network at a specified block number.
Expand Down Expand Up @@ -236,6 +236,8 @@ func LoadAccount(path string) (*Account, error) {
if err != nil {
return nil, err
}
account.Address = account.KeystoreAccount.Address
account.Type = utils.KeystoreAccountType

return &account, nil
}
4 changes: 2 additions & 2 deletions accounts/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import "github.com/unpackdev/solgo/utils"
type Options struct {
// KeystorePath specifies the file system path to the directory where the keystore files are stored.
// The keystore is used to securely store the private keys of Ethereum accounts.
KeystorePath string `json:"keystore_path" yaml:"keystore_path"`
KeystorePath string `json:"keystore_path" yaml:"keystorePath"`

// SupportedNetworks lists the Ethereum based networks that the account manager will interact with.
// Each network has a corresponding keystore and set of account configurations.
SupportedNetworks []utils.Network `json:"supported_networks" yaml:"supported_networks"`
SupportedNetworks []utils.Network `json:"supported_networks" yaml:"networks"`
}
33 changes: 32 additions & 1 deletion ast/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,30 @@ func normalizeTypeDescription(typeName string) (string, string) {
}
}

// builtInTypePrefixes is a set of known built-in type prefixes.
var builtInTypePrefixes = []string{
"uint",
"int",
"bool",
"bytes",
"string",
"address",
"addresspayable",
"tuple",
}

// isBuiltInType checks if a type name is a built-in type by its prefix.
func isBuiltInType(typeName string) bool {
for _, prefix := range builtInTypePrefixes {
if strings.HasPrefix(typeName, prefix) {
return true
}
}
return false
}

// normalizeTypeDescriptionWithStatus normalizes type names and generates corresponding type identifiers.
// Returns true if normalization occured.
// Returns true if normalization occurred.
func normalizeTypeDescriptionWithStatus(typeName string) (string, string, bool) {
isArray := strings.Contains(typeName, "[") && strings.Contains(typeName, "]")
isSlice := strings.HasSuffix(typeName, "[]")
Expand All @@ -137,16 +159,25 @@ func normalizeTypeDescriptionWithStatus(typeName string) (string, string, bool)
case isArray:
numberPart := typeName[strings.Index(typeName, "[")+1 : strings.Index(typeName, "]")]
typePart := typeName[:strings.Index(typeName, "[")]
if !isBuiltInType(typePart) {
return typeName, fmt.Sprintf("t_%s", typeName), false
}
normalizedTypePart := normalizeTypeName(typePart)
return normalizedTypePart + "[" + numberPart + "]", fmt.Sprintf("t_%s_array", normalizedTypePart), true

case isSlice:
typePart := typeName[:len(typeName)-2]
if !isBuiltInType(typePart) {
return typeName, fmt.Sprintf("t_%s", typeName), false
}
normalizedTypePart := normalizeTypeName(typePart)
return normalizedTypePart + "[]", fmt.Sprintf("t_%s_slice", normalizedTypePart), true

case isPrefixSlice:
typePart := typeName[2:]
if !isBuiltInType(typePart) {
return typeName, fmt.Sprintf("t_%s", typeName), false
}
normalizedTypePart := normalizeTypeName(typePart)
return "[]" + normalizedTypePart, fmt.Sprintf("t_%s_slice", normalizedTypePart), true

Expand Down
1 change: 0 additions & 1 deletion ast/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ast

import (
"github.com/goccy/go-json"

ast_pb "github.com/unpackdev/protos/dist/go/ast"
"github.com/unpackdev/solgo/parser"
)
Expand Down
18 changes: 17 additions & 1 deletion ast/reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,17 @@ func (r *Resolver) GetUnprocessedCount() int {
// ResolveByNode attempts to resolve a node by its name and returns the resolved Node and its TypeDescription.
// If the node cannot be found, it is added to the UnprocessedNodes map for future resolution.
func (r *Resolver) ResolveByNode(node Node[NodeType], name string) (int64, *TypeDescription) {
rNode, rNodeType := r.resolveByNode(name, node)
isSlice := strings.HasSuffix(name, "[]")
isPrefixSlice := strings.HasPrefix(name, "[]")
cleanedName := name

if isSlice {
cleanedName = strings.TrimSuffix(name, "[]")
} else if isPrefixSlice {
cleanedName = strings.TrimPrefix(name, "[]")
}

rNode, rNodeType := r.resolveByNode(cleanedName, node)

// Node could not be found in this moment, we are going to see if we can discover it in the
// future at the end of whole parsing process.
Expand All @@ -62,6 +72,12 @@ func (r *Resolver) ResolveByNode(node Node[NodeType], name string) (int64, *Type
Name: name,
Node: node,
}
} else {
if isSlice && !strings.Contains(rNodeType.TypeString, "[]") {
rNodeType.TypeString = rNodeType.TypeString + "[]"
} else if isPrefixSlice && !strings.Contains(rNodeType.TypeString, "[]") {
rNodeType.TypeString = "[]" + rNodeType.TypeString
}
}

return rNode, rNodeType
Expand Down
45 changes: 38 additions & 7 deletions ast/type_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ func (t *TypeName) WithParentNode(p Node[NodeType]) {

// SetReferenceDescriptor sets the reference descriptions of the TypeName node.
func (t *TypeName) SetReferenceDescriptor(refId int64, refDesc *TypeDescription) bool {
t.ReferencedDeclaration = refId
t.TypeDescription = refDesc
if t.TypeDescription == nil {
t.ReferencedDeclaration = refId
t.TypeDescription = refDesc
}

// Lets update the parent node as well in case that type description is not set...
/* parentNodeId := t.GetSrc().GetParentIndex()
Expand Down Expand Up @@ -370,7 +372,6 @@ func (t *TypeName) parseTypeName(unit *SourceUnit[Node[ast_pb.SourceUnit]], pare
Length: int64(pathCtx.GetStop().GetStop() - pathCtx.GetStart().GetStart() + 1),
ParentIndex: t.GetId(),
},

NodeType: ast_pb.NodeType_IDENTIFIER_PATH,
}

Expand All @@ -386,7 +387,7 @@ func (t *TypeName) parseTypeName(unit *SourceUnit[Node[ast_pb.SourceUnit]], pare
}

if found {
t.TypeDescription = &TypeDescription{
t.PathNode.TypeDescription = &TypeDescription{
TypeIdentifier: normalizedTypeIdentifier,
TypeString: normalizedTypeName,
}
Expand All @@ -400,6 +401,13 @@ func (t *TypeName) parseTypeName(unit *SourceUnit[Node[ast_pb.SourceUnit]], pare
}
}

// Alright lets now figure out main type description as it can be different such as
// PathNode vs PathNode[]
if refId, refTypeDescription := t.GetResolver().ResolveByNode(t, t.Name); refTypeDescription != nil {
t.ReferencedDeclaration = refId
t.TypeDescription = refTypeDescription
}

} else if ctx.TypeName() != nil {
t.generateTypeName(unit, ctx.TypeName(), t, t)
} else {
Expand Down Expand Up @@ -438,7 +446,6 @@ func (t *TypeName) parseTypeName(unit *SourceUnit[Node[ast_pb.SourceUnit]], pare
t.TypeDescription = refTypeDescription
}
}

}
}

Expand Down Expand Up @@ -515,17 +522,39 @@ func (t *TypeName) parseIdentifierPath(unit *SourceUnit[Node[ast_pb.SourceUnit]]
}

if found {
t.TypeDescription = &TypeDescription{
t.PathNode.TypeDescription = &TypeDescription{
TypeIdentifier: normalizedTypeIdentifier,
TypeString: normalizedTypeName,
}
} else {
if refId, refTypeDescription := t.GetResolver().ResolveByNode(t, identifierCtx.GetText()); refTypeDescription != nil {
if refId, refTypeDescription := t.GetResolver().ResolveByNode(t.PathNode, identifierCtx.GetText()); refTypeDescription != nil {
t.PathNode.ReferencedDeclaration = refId
t.PathNode.TypeDescription = refTypeDescription
}
}

bNormalizedTypeName, bNormalizedTypeIdentifier, bFound := normalizeTypeDescriptionWithStatus(
identifierCtx.GetText(),
)

// Alright lets now figure out main type description as it can be different such as
// PathNode vs PathNode[]
if bFound {
t.TypeDescription = &TypeDescription{
TypeIdentifier: bNormalizedTypeIdentifier,
TypeString: bNormalizedTypeName,
}
} else {
if refId, refTypeDescription := t.GetResolver().ResolveByNode(t, t.Name); refTypeDescription != nil {
t.ReferencedDeclaration = refId
t.TypeDescription = refTypeDescription
}
}

/* if t.Id == 1787 {
fmt.Println("HERE I AM")
utils.DumpNodeWithExit(t)
}*/
}
}

Expand Down Expand Up @@ -705,6 +734,7 @@ func (t *TypeName) generateTypeName(sourceUnit *SourceUnit[Node[ast_pb.SourceUni
} else if specificCtx.IdentifierPath() != nil {
typeName.NodeType = ast_pb.NodeType_USER_DEFINED_PATH_NAME
t.parseIdentifierPath(sourceUnit, parentNode.GetId(), specificCtx.IdentifierPath().(*parser.IdentifierPathContext))

} else {

normalizedTypeName, normalizedTypeIdentifier := normalizeTypeDescription(
Expand Down Expand Up @@ -777,6 +807,7 @@ func (t *TypeName) Parse(unit *SourceUnit[Node[ast_pb.SourceUnit]], fnNode Node[
case *antlr.TerminalNodeImpl:
continue
default:

expression := NewExpression(t.ASTBuilder)
if expr := expression.ParseInterface(unit, fnNode, t.GetId(), ctx.Expression()); expr != nil {
t.Expression = expr
Expand Down
4 changes: 2 additions & 2 deletions bindings/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func NewToken(ctx context.Context, network utils.Network, manager *Manager, opts

// Now lets register all the bindings with the manager
for _, opt := range opts {
for _, network := range opt.Networks {
if _, err := manager.RegisterBinding(network, opt.NetworkID, opt.Type, opt.Address, opt.ABI); err != nil {
for _, oNetwork := range opt.Networks {
if _, err := manager.RegisterBinding(oNetwork, opt.NetworkID, opt.Type, opt.Address, opt.ABI); err != nil {
return nil, err
}
}
Expand Down
Loading

0 comments on commit 27ff826

Please sign in to comment.