-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
168 lines (147 loc) · 4.44 KB
/
cli.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
165
166
167
168
package main
import (
"bufio"
"eth_data/internal/cmd"
"eth_data/internal/db"
"eth_data/internal/eth"
"fmt"
"log"
"math"
"os"
"os/exec"
"strconv"
"strings"
"time"
"github.com/joho/godotenv"
)
func main() {
db, err := db.Open("data")
if err != nil {
log.Fatalf("Failed to open the database: %v", err)
}
defer db.Close()
// Load the .env file
err = godotenv.Load()
if err != nil {
log.Fatalf("Error loading .env file")
}
secretUrl := os.Getenv("SECRET_URL")
if secretUrl == "" {
log.Fatal("Missing SECRET_URL env var")
}
ethClient := eth.NewEthClient(secretUrl)
defer ethClient.Close()
promptLoop(ethClient)
}
func promptLoop(ethClient *eth.EthClient) {
reader := bufio.NewScanner(os.Stdin)
fmt.Print("> ")
for reader.Scan() {
text := strings.TrimSpace(reader.Text())
if text == "exit" {
// Close the program
return
} else {
handleCommand(text, ethClient)
}
fmt.Print("> ")
}
}
// Executes the given command
func handleCommand(text string, ethClient *eth.EthClient) {
// text := "fetchWalletTxs 0x0839D7318603205fEF3340f365b087A5F92c6Df8 20804500 20804599"
command, args := parseCommand(text)
switch command {
case "clear":
clearScreen()
case "help":
displayHelp()
case "blockNumber":
blockNumber, err := ethClient.LastestBlockNumber()
if err == nil {
fmt.Println("Latest block number:", blockNumber)
} else {
fmt.Printf("Error: %+v\n", err)
}
case "blockByNumber":
block, err := ethClient.BlockByNumber(args[0])
if err == nil {
fmt.Printf("Dump: %+v\n", block)
} else {
fmt.Printf("Error: %+v\n", err)
}
case "lastFinalizedBlock":
block, err := ethClient.BlockByNumber("finalized")
if err == nil {
fmt.Println("Keys:", block.BlockKeys())
fmt.Println("Timestamp:", dateTimeFormat(block.Timestamp()))
} else {
fmt.Printf("Error: %+v\n", err)
}
case "lastFinalizedTx":
block, err := ethClient.BlockByNumber("finalized")
if err == nil {
lastTransaction := block.LastTransaction()
fmt.Println("Last finalized block number:", lastTransaction.BlockNumber())
fmt.Printf("Dump: %+v\n", lastTransaction)
fmt.Printf("Keys: %+v\n", lastTransaction.TxKeys())
fmt.Println("Hash:", lastTransaction.GetString("hash"))
fmt.Println("Gas:", lastTransaction.Gas())
fmt.Println("GasPrice:", lastTransaction.GasPrice())
fmt.Println("BaseFeePerGas:", block.BaseFeePerGas())
fmt.Println("MaxTipPerGas:", eth.ParseHex(lastTransaction.GetString("maxPriorityFeePerGas")))
fmt.Println("MaxFeePerGas:", eth.ParseHex(lastTransaction.GetString("maxFeePerGas")))
gasPrice1 := block.BaseFeePerGas() + eth.ParseHex(lastTransaction.GetString("maxPriorityFeePerGas"))
gasPrice2 := eth.ParseHex(lastTransaction.GetString("maxFeePerGas"))
fmt.Println("EffectiveGasPrice:", math.Min(float64(gasPrice1), float64(gasPrice2)))
fmt.Printf("Input: %+v\n", lastTransaction.GetString("input"))
} else {
fmt.Printf("Error: %+v\n", err)
}
case "fetchWalletTxs":
if len(args) != 3 {
fmt.Println("Usage: fetchWalletTxs <walletAddress> <firstBlockInt> <lastBlockInt>")
} else {
walletAddress := strings.ToLower(args[0])
firstBlock, err := strconv.ParseInt(args[1], 10, 64)
if err != nil {
fmt.Println("Usage: fetchWalletTxs <walletAddress> <firstBlockInt> <lastBlockInt>")
} else {
lastBlock, err := strconv.ParseInt(args[2], 10, 64)
if err != nil {
fmt.Println("Usage: fetchWalletTxs <walletAddress> <firstBlockInt> <lastBlockInt>")
} else {
cmd.FetchWalletTxsCmd(ethClient, walletAddress, firstBlock, lastBlock)
}
}
}
default:
fmt.Println(text, ": command not found")
}
}
func parseCommand(text string) (string, []string) {
parts := strings.Split(text, " ")
command := parts[0]
args := parts[1:]
return command, args
}
// Shows the available commands
func displayHelp() {
fmt.Printf("This is a CLI to retrieve Ethereum data.\n\n")
fmt.Println("Available commands:")
fmt.Println("blockNumber - Gets the latest block number")
fmt.Println("lastFinalizedBlock - Gets the last finalized block")
fmt.Println("lastFinalizedTx - Gets the last tx from finalized number")
fmt.Println("clear - Clear the terminal screen")
fmt.Println("help - Show available commands")
fmt.Println("exit - Closes this program")
}
// Clears the terminal screen
func clearScreen() {
cmd := exec.Command("clear")
cmd.Stdout = os.Stdout
cmd.Run()
}
func dateTimeFormat(timestamp int64) string {
return time.Unix(timestamp, 0).Format("2006-01-02 15:04:05")
}