-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
161 lines (131 loc) · 4.18 KB
/
main.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
package main
import (
"context"
"errors"
"fmt"
"io/fs"
"os"
"strings"
"time"
"github.com/ardanlabs/bets/app/tooling/crypto/commands"
scbook "github.com/ardanlabs/bets/business/contract/go/book"
"github.com/ardanlabs/bets/business/core/book"
"github.com/ardanlabs/ethereum"
"github.com/ardanlabs/ethereum/currency"
"github.com/ethereum/go-ethereum/common"
)
const (
keyStorePath = "zarf/ethereum/keystore/"
passPhrase = "123"
)
func main() {
if len(os.Args) == 1 {
commands.PrintUsage()
return
}
if err := run(); err != nil {
fmt.Println("\nError")
fmt.Println("----------------------------------------------------")
fmt.Println(err)
os.Exit(1)
}
}
func run() error {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
// =========================================================================
// Parse flags for settings.
flags, args, err := commands.Parse()
if err != nil {
return err
}
if _, exists := flags["h"]; exists {
commands.PrintUsage()
return nil
}
// =========================================================================
// Find the key file for the specified address.
keyFile, err := findKeyFile(keyStorePath, args.FileKey)
if err != nil {
return err
}
// =========================================================================
// Construct the converted for ETH to USD conversions.
converter, err := currency.NewConverter(scbook.BookMetaData.ABI, args.CoinMarketCapKey)
if err != nil {
converter = currency.NewDefaultConverter(scbook.BookMetaData.ABI)
}
oneETHToUSD, oneUSDToETH := converter.Values()
// =========================================================================
// Construct the bank API.
backend, err := ethereum.CreateDialedBackend(ctx, args.Network)
if err != nil {
return err
}
defer backend.Close()
privateKey, err := ethereum.PrivateKeyByKeyFile(keyFile, args.PassPhrase)
if err != nil {
return err
}
book, err := book.New(ctx, backend, privateKey, args.ContractID)
if err != nil {
return err
}
// =========================================================================
// Display the settings and execute the specified command.
fmt.Println("\nSettings")
fmt.Println("----------------------------------------------------")
fmt.Println("network :", args.Network)
fmt.Println("privatekey :", keyFile)
fmt.Println("passphrase :", args.PassPhrase)
fmt.Println("oneETHToUSD :", oneETHToUSD)
fmt.Println("oneUSDToETH :", oneUSDToETH)
fmt.Println("key address :", book.Client().Address())
fmt.Println("contract id :", args.ContractID)
if _, exists := flags["a"]; exists {
return commands.Deposit(ctx, converter, book, args.Money)
}
if _, exists := flags["r"]; exists {
return commands.Withdraw(ctx, converter, book)
}
if _, exists := flags["b"]; exists {
return commands.Balance(ctx, converter, book, args.Address)
}
if _, exists := flags["w"]; exists {
return commands.Wallet(ctx, converter, book.Client(), common.HexToAddress(args.Address))
}
if _, exists := flags["d"]; exists {
return commands.Deploy(ctx, converter, book.Client())
}
if _, exists := flags["t"]; exists {
return commands.Transaction(ctx, converter, book.Client(), args.TranID)
}
return errors.New("no functional command provided")
}
// =============================================================================
// findKeyFile searches the keystore for the specified address key file.
func findKeyFile(keyStorePath string, address string) (string, error) {
keyStorePath = strings.TrimSuffix(keyStorePath, "/")
errFound := errors.New("found")
var filePath string
fn := func(fileName string, dirEntry fs.DirEntry, err error) error {
if err != nil {
return fmt.Errorf("walkdir failure: %w", err)
}
if dirEntry.IsDir() {
return nil
}
if strings.Contains(strings.ToLower(fileName), strings.ToLower(address[2:])) {
filePath = fmt.Sprintf("%s/%s", keyStorePath, fileName)
return errFound
}
return nil
}
if err := fs.WalkDir(os.DirFS(keyStorePath), ".", fn); err != nil {
if errors.Is(err, errFound) {
return filePath, nil
}
return "", fmt.Errorf("walking directory: %w", err)
}
return "", errors.New("not found")
}