Skip to content

Commit

Permalink
fix: array input and contract info setup
Browse files Browse the repository at this point in the history
  • Loading branch information
zsystm committed Nov 13, 2024
1 parent 48ff902 commit edcd138
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 84 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"ERC20": {
[
{
"name": "ERC20",
"address": "0xdAC17F958D2ee523a2206206994597C13D831ec7"
}
}
]
53 changes: 28 additions & 25 deletions cmd/solizard/solizard.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ const SolizardDir = ".solizard"
var (
// AbiDir is the directory where all abi files are stored
// default is $HOME/.solizard/abis
AbiDir = "abis"
ZeroAddr = common.Address{}
ConfigExist = false
AddrBookExist = false
conf *config.Config
AddrBook config.AddressBook
AbiDir = "abis"
ZeroAddr = common.Address{}
ConfigExist = false
ContractInfoExist = false
Conf *config.Config
ContractInfos []config.ContractInfo
)

func init() {
Expand Down Expand Up @@ -79,8 +79,8 @@ func init() {
return err
}
}
if d.Type().IsRegular() && d.Name() == "address_book.json" {
if err := os.WriteFile(homeDir+"/"+SolizardDir+"/address_book.json", data, 0644); err != nil {
if d.Type().IsRegular() && d.Name() == "contract_infos.json" {
if err := os.WriteFile(homeDir+"/"+SolizardDir+"/contract_infos.json", data, 0644); err != nil {
return err
}
}
Expand All @@ -97,28 +97,28 @@ func init() {
os.Exit(1)
}
ConfigPath := dir + "/" + "config.toml"
if conf, err = config.ReadConfig(ConfigPath); err != nil {
if Conf, err = config.ReadConfig(ConfigPath); err != nil {
fmt.Printf("failed to read config file (reason: %v)\n", err)
ConfigExist = false
} else {
ConfigExist = true
}

AddrBookPath := dir + "/" + "address_book.json"
if AddrBook, err = config.ReadAddressBook(AddrBookPath); err != nil {
fmt.Printf("failed to read address book (reason: %v)\n", err)
AddrBookExist = false
contractInfosPath := dir + "/" + "contract_infos.json"
if ContractInfos, err = config.ReadContractInfos(contractInfosPath); err != nil {
fmt.Printf("failed to read contract infos (reason: %v)\n", err)
ContractInfoExist = false
} else {
if err = AddrBook.Validate(); err != nil {
if err = config.ValidateContractInfos(ContractInfos); err != nil {
fmt.Printf("address book is invalid (reason: %v)\n", err)
panic(err)
}
AddrBookExist = true
ContractInfoExist = true
}
}

func Run() error {
fmt.Println(`🦎 Welcome to Solizard v1.5.0 🦎`)
fmt.Println(`🦎 Welcome to Solizard v1.6.0 🦎`)
mAbi, err := internalabi.LoadABIs(AbiDir)
if err != nil {
return err
Expand All @@ -127,7 +127,7 @@ func Run() error {
sctx := new(ctx.Context)
if ConfigExist {
if prompt.MustSelectApplyConfig() {
sctx = ctx.NewCtx(conf)
sctx = ctx.NewCtx(Conf)
}
}

Expand All @@ -149,17 +149,20 @@ func Run() error {
}
INPUT_CONTRACT_ADDRESS:
// check if address book exists
useAddrBook := false
useContractInfo := false
var contractAddress string
if AddrBookExist {
if value, exists := AddrBook[selectedContractName]; exists {
if yes := prompt.MustSelectAddressBookUsage(value.Address); yes {
contractAddress = value.Address
useAddrBook = true
if ContractInfoExist {
for _, ci := range ContractInfos {
if ci.Name == selectedContractName {
if yes := prompt.MustSelectAddressBookUsage(ci.Address); yes {
contractAddress = ci.Address
useContractInfo = true
break
}
}
}
}
if !useAddrBook {
if !useContractInfo {
contractAddress = prompt.MustInputContractAddress()
}
if err := validation.ValidateContractAddress(sctx, contractAddress); err != nil {
Expand Down Expand Up @@ -230,7 +233,7 @@ func Run() error {
}
fmt.Printf("transaction sent (txHash %v).\n", signedTx.Hash().Hex())
// sleep for x seconds to wait for transaction to be mined
waitTime, err := time.ParseDuration(conf.WaitTime)
waitTime, err := time.ParseDuration(Conf.WaitTime)
fmt.Printf("waiting for transaction to be mined... (sleep %s\n", waitTime.String())
time.Sleep(waitTime)
receipt, err := sctx.EthClient().TransactionReceipt(context.TODO(), signedTx.Hash())
Expand Down
45 changes: 33 additions & 12 deletions internal/abi/abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,31 +80,52 @@ func GetMethodsByType(contractABI abi.ABI, rw MethodType) map[string]abi.Method
func ParseArrayOrSliceInput(input string, typ abi.Type) interface{} {
// parse the input string for array or slice type
// example input format: "[1,2,3]" or "1,2,3"
// 1. trim whitespace
input = strings.ReplaceAll(input, " ", "")
// 2. remove brackets if present
input = strings.Trim(input, "[]")
// 3. split the input string by comma
parts := strings.Split(input, ",")
values := make([]interface{}, len(parts))

for i, part := range parts {
var value interface{}
var addresses []common.Address
var integers []*big.Int
var booleans []bool
var strings []string
var bytes [][]byte
var hashes []common.Hash
for _, part := range parts {
switch typ.Elem.T {
case abi.IntTy, abi.UintTy:
value, _ = new(big.Int).SetString(part, 10)
value, _ := new(big.Int).SetString(part, 10)
integers = append(integers, value)
case abi.BoolTy:
value = part == "true"
booleans = append(booleans, part == "true")
case abi.StringTy:
value = part
strings = append(strings, part)
case abi.AddressTy:
value = common.HexToAddress(part)
v := common.HexToAddress(part)
addresses = append(addresses, v)
case abi.FixedBytesTy, abi.BytesTy:
value = common.Hex2Bytes(part)
bytes = append(bytes, common.Hex2Bytes(part))
case abi.HashTy:
value = common.HexToHash(part)
hashes = append(hashes, common.HexToHash(part))
default:
panic("unsupported array or slice element type: " + typ.Elem.String())
}
values[i] = value
}
return values
if len(addresses) > 0 {
return addresses
} else if len(integers) > 0 {
return integers
} else if len(booleans) > 0 {
return booleans
} else if len(strings) > 0 {
return strings
} else if len(bytes) > 0 {
return bytes
} else if len(hashes) > 0 {
return hashes
}
return nil
}

func ParseTupleInput(input string, typ abi.Type) interface{} {
Expand Down
44 changes: 0 additions & 44 deletions internal/config/address_book.go

This file was deleted.

49 changes: 49 additions & 0 deletions internal/config/contract_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package config

import (
"encoding/json"
"fmt"
"os"

"github.com/ethereum/go-ethereum/common"
)

type ContractInfo struct {
Name string `json:"name"`
Address string `json:"address"`
}

func ReadContractInfos(path string) ([]ContractInfo, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}

var ci []ContractInfo
if err = json.Unmarshal(data, &ci); err != nil {
return nil, err
}
return ci, nil
}

func (ci ContractInfo) Validate() error {
if ci.Name == "" {
return fmt.Errorf("contract name is empty")
}
if ci.Address == "" {
return fmt.Errorf("contract address is empty")
}
if !common.IsHexAddress(ci.Address) {
return fmt.Errorf("contract address is invalid")
}
return nil
}

func ValidateContractInfos(cis []ContractInfo) error {
for _, c := range cis {
if err := c.Validate(); err != nil {
return err
}
}
return nil
}

0 comments on commit edcd138

Please sign in to comment.