Skip to content

A little bit of everything... #216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
16 changes: 16 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

Contract Issues

- [x] 0x258FD2E6b5C155aa5f3e84326A622288bd70f376
- [ ] 0x2aa101BF99CaeF7fc1355D4c493a1fe187A007cE
- [ ] 0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84
- [ ] 0x6000da47483062A0D734Ba3dc7576Ce6A0B645C4
- [ ] 0x609c690e8F7D68a59885c9132e812eEbDaAf0c9e
- [ ] 0x76264869a3eBF51a59FCa5ABa84ee2867c7F190e
- [ ] 0x6eF81a18E1E432C289DC0d1a670B78E8bbF9AA35
- [ ] 0x98D951E9b0C0Bb180F1b3ed40DDE6E1B1B521Cc1
- [ ] 0xCD7ae3373F7e76A817238261b8303FA17D2AF585
- [ ] 0xdEb43523E2857b7ec29D078c77b73709D958c62F
- [ ] 0x8CB3649114051cA5119141a34C200D65dc0Faa73
- [ ] 0xD101dCC414F310268c37eEb4cD376CcFA507F571
- [ ] 0x09B33A99B954e52907c61514B6f8cD37De71076f
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
28 changes: 23 additions & 5 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 @@ -50,8 +51,15 @@ func (b *Builder) processContract(contract *ir.Contract) (*Contract, error) {

// Process state variables.
for _, stateVar := range contract.GetStateVariables() {
method := b.processStateVariable(stateVar)
toReturn = append(toReturn, method)
// Some old contracts will have this broken. It's related to 0.4 contracts.
// Better to have at least something then nothing at all in this point.
if stateVar.Name == "" && stateVar.GetTypeDescription() == nil {
continue
}

if method := b.processStateVariable(stateVar); method != nil {
toReturn = append(toReturn, method)
}
}

// Process events.
Expand Down Expand Up @@ -128,13 +136,23 @@ 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)
structMember := b.resolver.ResolveStructType(typeDescr)
structMember.Name = method.Name
return structMember
default:
method.Type = typeName
method.InternalType = typeDescr.GetString()
Expand Down
6 changes: 3 additions & 3 deletions abi/error.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package abi

import (
"fmt"

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

Expand All @@ -19,7 +17,9 @@ func (b *Builder) processError(unit *ir.Error) (*Method, error) {

for _, parameter := range unit.GetParameters() {
if parameter.GetTypeDescription() == nil {
return nil, fmt.Errorf("nil type description for error parameter %s", parameter.GetName())
//utils.DumpNodeWithExit(unit)
//return nil, fmt.Errorf("nil type description for error parameter %s", parameter.GetName())
continue
}

methodIo := MethodIO{
Expand Down
4 changes: 4 additions & 0 deletions abi/state_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ func (b *Builder) processStateVariable(stateVar *ir.StateVariable) *Method {
StateMutability: b.normalizeStateMutability(stateVar.GetStateMutability()),
}

if stateVar.GetTypeDescription() == nil {
return nil
}

typeName := b.resolver.ResolveType(stateVar.GetTypeDescription())

switch typeName {
Expand Down
54 changes: 53 additions & 1 deletion abi/type.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package abi

import (
ast_pb "github.com/unpackdev/protos/dist/go/ast"
"strings"

"github.com/unpackdev/solgo/ast"
Expand Down Expand Up @@ -96,10 +97,15 @@ func (t *TypeResolver) ResolveStructType(typeName *ast.TypeDescription) MethodIO
nameCleaned = strings.TrimRight(nameCleaned, "[]")
nameParts := strings.Split(nameCleaned, ".")

methodType := "tuple"
if strings.Contains(typeName.GetString(), "[]") {
methodType = "tuple[]"
}

toReturn := MethodIO{
Name: nameParts[1],
Components: make([]MethodIO, 0),
Type: "tuple",
Type: methodType,
InternalType: typeName.GetString(),
}

Expand Down Expand Up @@ -143,6 +149,52 @@ func (t *TypeResolver) ResolveStructType(typeName *ast.TypeDescription) MethodIO
}
}

// We did not discover any structs in the contracts themselves... Apparently this is a global
// struct definition...
if len(toReturn.Components) == 0 {
for _, node := range t.parser.GetAstBuilder().GetRoot().GetGlobalNodes() {
if node.GetType() == ast_pb.NodeType_STRUCT_DEFINITION {
if structVar, ok := node.(*ast.StructDefinition); ok {
if structVar.GetName() == toReturn.Name {
for _, member := range structVar.GetMembers() {
// Mapping types are not supported in structs
if isMappingType(member.GetTypeDescription().GetString()) {
continue
}

if isContractType(member.GetTypeDescription().GetString()) {
toReturn.Outputs = append(toReturn.Outputs, MethodIO{
Name: member.GetName(),
Type: "address",
InternalType: member.GetTypeDescription().GetString(),
})

continue
}

dType := t.discoverType(member.GetTypeDescription().GetString())
if len(dType.Outputs) > 0 {
for _, out := range dType.Outputs {
toReturn.Components = append(toReturn.Components, MethodIO{
Name: out.Name,
Type: out.Type,
InternalType: member.GetTypeDescription().GetString(),
})
}
} else {
toReturn.Components = append(toReturn.Components, MethodIO{
Name: member.GetName(),
Type: dType.Type,
InternalType: member.GetTypeDescription().GetString(),
})
}
}
}
}
}
}
}

return toReturn
}

Expand Down
49 changes: 24 additions & 25 deletions accounts/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,17 @@ import (
"context"
"encoding/base64"
"fmt"
"github.com/goccy/go-json"
"log"
"math/big"
"os"
"strings"

account "github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/goccy/go-json"
"github.com/unpackdev/solgo/clients"
"github.com/unpackdev/solgo/utils"
"log"
"math/big"
"os"
)

const (
Expand All @@ -28,7 +25,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 @@ -78,8 +75,7 @@ func (a *Account) GetClient() *clients.Client {
// This method is mainly used for testing purposes in simulation environments like Anvil.
// 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(), amount.String())
}

// Balance retrieves the account's balance from the Ethereum network at a specified block number.
Expand Down Expand Up @@ -112,21 +108,22 @@ func (a *Account) TransactOpts(client *clients.Client, amount *big.Int, simulate

if !simulate {
if a.Type == utils.SimpleAccountType {
privateKey, err := crypto.HexToECDSA(strings.TrimLeft(a.PrivateKey, "0x"))
if err != nil {
return nil, err
}

auth, err := bind.NewKeyedTransactorWithChainID(privateKey, big.NewInt(client.GetNetworkID()))
if err != nil {
return nil, err
}

auth.Nonce = big.NewInt(int64(nonce))
auth.GasPrice = gasPrice
auth.GasLimit = DEFAULT_GAS_LIMIT
auth.Value = amount
return auth, nil
/* privateKey, err := crypto.HexToECDSA(strings.TrimLeft(a.PrivateKey, "0x"))
if err != nil {
return nil, err
}*/

/* auth, err := bind.NewKeyedTransactorWithChainID(privateKey, big.NewInt(client.GetNetworkID()))
if err != nil {
return nil, err
}

auth.Nonce = big.NewInt(int64(nonce))
auth.GasPrice = gasPrice
auth.GasLimit = DEFAULT_GAS_LIMIT
auth.Value = amount
return auth, nil*/
return nil, nil
} else if a.Type == utils.KeystoreAccountType {
password, _ := a.DecodePassword()

Expand Down Expand Up @@ -236,6 +233,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"`
}
1 change: 0 additions & 1 deletion ast/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ast

import (
"fmt"

ast_pb "github.com/unpackdev/protos/dist/go/ast"
"github.com/unpackdev/solgo/parser"
)
Expand Down
Loading
Loading