-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcli.go
248 lines (196 loc) · 5.14 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package main
import (
"fmt"
"log"
"os"
"strconv"
)
type CLI struct {
}
var helpMessage = `N Chainz: a high performance, decentralized cryptocurrency exchange.
Usage: nchainz COMMAND [ARGS]
The commands are:
Account management
createwallet
Create a wallet with a pair of keys
getbalance ADDRESS SYMBOL
Get the balance for an address
printaddresses
Print all adddreses in wallet file
Creating transactions
order BUY_AMT BUY_SYMBOL SELL_AMT SELL_SYMBOL ADDRESS
Create an ORDER transaction
transfer AMT SYMBOL FROM TO
Create a TRANSFER transaction
freeze AMT SYMBOL FROM UNFREEZE_BLOCK
Create a FREEZE tokens transaction
cancel SYMBOL ORDER_ID
Create a CANCEL_ORDER transaction
claim AMT SYMBOL ADDRESS
Create a CLAIM_FUNDS transaction
create SYMBOL SUPPLY DECIMALS ADDRESS
Create a CREATE_TOKEN transaction
Running a node, miner, or webserver
node HOSTNAME:PORT
Start up a full node providing your hostname on the given port
printchain DB SYMBOL
Prints all the blocks in the blockchain
webserver PORT
Run a webserver on the given port
`
func (cli *CLI) Run() {
if len(os.Args) < 2 {
cli.printHelpAndExit()
}
command := os.Args[1]
switch command {
case "createwallet":
cli.createWallet()
case "getbalance":
address := cli.getString(0)
symbol := cli.getString(1)
client := cli.getClient()
if !ValidateAddress(address) {
LogRed("ERROR: Address %s is not valid. Please try again.", address)
os.Exit(1)
}
client.GetBalance(address, symbol)
case "printaddresses":
cli.printAddresses()
case "order":
buyAmt := cli.getUint(0)
buySymbol := cli.getString(1)
sellAmt := cli.getUint(2)
sellSymbol := cli.getString(3)
seller := cli.getString(4)
if !ValidateAddress(seller) {
LogRed("ERROR: Address %s is not valid. Please try again.", seller)
os.Exit(1)
}
client := cli.getClient()
client.Order(buyAmt, buySymbol, sellAmt, sellSymbol, seller)
case "transfer":
amt := cli.getUint(0)
symbol := cli.getString(1)
from := cli.getString(2)
to := cli.getString(3)
client := cli.getClient()
client.Transfer(amt, symbol, from, to)
case "freeze":
amt := cli.getUint(0)
symbol := cli.getString(1)
from := cli.getString(2)
unfreezeBlock := cli.getUint(3)
client := cli.getClient()
client.Freeze(amt, symbol, from, unfreezeBlock)
case "cancel":
orderSymbol := cli.getString(0)
orderId := cli.getUint(1)
client := cli.getClient()
client.Cancel(orderSymbol, orderId)
case "claim":
amt := cli.getUint(0)
symbol := cli.getString(1)
address := cli.getString(2)
if !ValidateAddress(address) {
LogRed("ERROR: Address %s is not valid. Please try again.", address)
os.Exit(1)
}
client := cli.getClient()
client.Claim(amt, symbol, address)
case "create":
symbol := cli.getString(0)
supply := cli.getUint(1)
decimals := uint8(cli.getUint(2))
address := cli.getString(3)
if !ValidateAddress(address) {
LogRed("ERROR: Address %s is not valid. Please try again.", address)
os.Exit(1)
}
client := cli.getClient()
client.Create(symbol, supply, decimals, address)
case "node":
socket := cli.getString(0)
StartNode(socket)
case "printchain":
db := cli.getString(0)
symbol := cli.getString(1)
cli.printChain(db, symbol)
case "book":
symbol1 := cli.getString(0)
symbol2 := cli.getString(1)
client := cli.getClient()
client.GetBook(symbol1, symbol2)
case "dumpchains":
amt := cli.getUint(0)
client := cli.getClient()
client.DumpChains(amt)
case "webserver":
port := cli.getUint(0)
StartWebserver(port)
default:
cli.printHelpAndExit()
}
}
func (cli *CLI) getString(index int) string {
if len(os.Args)-3 < index {
cli.printHelpAndExit()
}
return os.Args[index+2]
}
func (cli *CLI) getUint(index int) uint64 {
s := cli.getString(index)
i, err := strconv.ParseUint(s, 10, 64)
if err != nil {
cli.printHelpAndExit()
}
return uint64(i)
}
func (cli *CLI) printHelpAndExit() {
fmt.Print(helpMessage)
os.Exit(1)
}
func (cli *CLI) getClient() *Client {
client, err := NewClient()
if err != nil {
log.Printf(err.Error())
os.Exit(1)
}
return client
}
///////////////////////////////////////////////////////
// CLI commands that really should live somewhere else
func (cli *CLI) printChain(db string, symbol string) {
bcs := CreateNewBlockchains(db+".db", false)
bc := bcs.chains[symbol]
bci := bc.Iterator()
fmt.Println(symbol)
fmt.Printf("Height: %d tiphash: %x\n", bc.height, bc.tipHash)
block, err := bci.Prev()
isGenesis := true
for err == nil {
fmt.Printf("Prev Hash: %x\n", block.PrevBlockHash)
fmt.Printf("Data: %x\n", block.Data)
fmt.Printf("Hash: %x\n", block.Hash)
pow := NewProofOfWork(block)
if !isGenesis {
fmt.Printf("Validated Proof of Work: %s\n", strconv.FormatBool(pow.Validate()))
isGenesis = false
}
fmt.Println("-------------------------------")
block, err = bci.Prev()
}
}
func (cli *CLI) printAddresses() {
ws := NewWalletStore(false)
addresses := ws.GetAddresses()
for _, address := range addresses {
fmt.Println(address)
}
}
func (cli *CLI) createWallet() {
ws := NewWalletStore(false)
address := ws.AddWallet()
ws.Persist()
fmt.Printf("New wallet's address: %s\n", address)
}