-
Notifications
You must be signed in to change notification settings - Fork 2
/
solizard.go
164 lines (156 loc) · 4.77 KB
/
solizard.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package solizard
import (
"context"
"fmt"
"os"
"time"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/zsystm/solizard/internal/abi"
"github.com/zsystm/solizard/internal/config"
"github.com/zsystm/solizard/internal/ctx"
"github.com/zsystm/solizard/internal/prompt"
"github.com/zsystm/solizard/internal/step"
"github.com/zsystm/solizard/internal/validation"
)
const Binary = "solizard"
var (
// AbiDir is the directory where all abi files are stored
// default is $HOME/solizard/abis
AbiDir = "abis"
ZeroAddr = common.Address{}
ConfigExist = false
conf *config.Config
)
func init() {
// get user's home directory
homeDir, err := os.UserHomeDir()
if err != nil {
fmt.Printf("failed to get user's home directory (reason: %v)\n", err)
os.Exit(1)
}
AbiDir = homeDir + "/" + Binary + "/" + AbiDir
if err := validation.DirContainsFiles(AbiDir); err != nil {
fmt.Println(err)
os.Exit(1)
}
ConfigPath := homeDir + "/" + Binary + "/config.toml"
conf, err = config.ReadConfig(ConfigPath)
if err != nil {
fmt.Printf("failed to read config file (reason: %v)\n", err)
ConfigExist = false
}
ConfigExist = true
}
func Run() error {
mAbi, err := abi.LoadABIs(AbiDir)
if err != nil {
return err
}
sctx := new(ctx.Context)
if ConfigExist {
if prompt.MustSelectApplyConfig() {
sctx = ctx.NewCtx(conf)
}
}
// start the main loop
for {
STEP_SELECT_CONTRACT:
selectedAbi := prompt.MustSelectContractABI(mAbi)
INPUT_RPC_URL:
if sctx.EthClient() == nil {
rpcURL := prompt.MustInputRpcUrl()
client, err := ethclient.Dial(rpcURL)
if err != nil {
fmt.Printf("failed to connect to given rpc url: %v, please input valid one\n", err)
goto INPUT_RPC_URL
}
sctx.SetEthClient(client)
}
INPUT_CONTRACT_ADDRESS:
contractAddress := prompt.MustInputContractAddress()
if err := validation.ValidateContractAddress(sctx, contractAddress); err != nil {
fmt.Printf("Invalid contract address (reason: %v)\n", err)
goto INPUT_CONTRACT_ADDRESS
}
SELECT_METHOD:
rw := prompt.MustSelectReadOrWrite()
if rw == abi.WriteMethod {
// input private key
if sctx.PrivateKey() == nil {
pk := prompt.MustInputPrivateKey()
sctx.SetPrivateKey(pk)
}
// input chainId
if sctx.ChainId().Sign() == 0 {
chainID := prompt.MustInputChainID()
sctx.SetChainId(&chainID)
}
}
methodName, method := prompt.MustSelectMethod(selectedAbi, rw)
var input []byte
input = prompt.MustCreateInputDataForMethod(method)
if rw == abi.ReadMethod {
callMsg := ethereum.CallMsg{From: ZeroAddr, To: sctx.ContractAddress(), Data: input}
output, err := sctx.EthClient().CallContract(context.TODO(), callMsg, nil)
if err != nil {
return err
}
res, err := selectedAbi.Unpack(methodName, output)
if err != nil {
return err
}
fmt.Printf("output: %v\n", res)
} else {
nonce, err := sctx.EthClient().NonceAt(context.TODO(), crypto.PubkeyToAddress(sctx.PrivateKey().PublicKey), nil)
if err != nil {
fmt.Printf("failed to get nonce (reason: %v), maybe rpc is not working.\n", err)
goto INPUT_RPC_URL
}
gasPrice, err := sctx.EthClient().SuggestGasPrice(context.TODO())
if err != nil {
fmt.Printf("failed to get gas price (reason: %v), maybe rpc is not working.\n", err)
goto INPUT_RPC_URL
}
sufficientGasLimit := uint64(3000000)
unsignedTx := types.NewTx(&types.LegacyTx{
To: sctx.ContractAddress(),
Nonce: nonce,
Value: common.Big0,
Gas: sufficientGasLimit,
GasPrice: gasPrice,
Data: input,
})
signedTx, err := types.SignTx(unsignedTx, types.NewEIP155Signer(sctx.ChainId()), sctx.PrivateKey())
if err = sctx.EthClient().SendTransaction(context.TODO(), signedTx); err != nil {
fmt.Printf("failed to send transaction (reason: %v), maybe rpc is not working.\n", err)
return err
}
fmt.Printf("transaction sent (txHash %v).\n", signedTx.Hash().Hex())
// sleep for 5 seconds to wait for transaction to be mined
fmt.Println("waiting for transaction to be mined... (sleep 5 sec)")
time.Sleep(5 * time.Second)
receipt, err := sctx.EthClient().TransactionReceipt(context.TODO(), signedTx.Hash())
if err != nil {
fmt.Printf("failed to get transaction receipt (reason: %v).\n", err)
} else {
jsonReceipt, _ := receipt.MarshalJSON()
fmt.Printf("transaction receipt: %s\n", string(jsonReceipt))
}
}
st := prompt.MustSelectStep()
switch st {
case step.StepSelectMethod:
goto SELECT_METHOD
case step.StepChangeContract:
goto STEP_SELECT_CONTRACT
case step.StepChangeContractAddress:
goto INPUT_CONTRACT_ADDRESS
case step.StepExit:
panic("exit")
}
}
}