Implement 'getbalance' command
This commit is contained in:
parent
f83ccd7b4c
commit
87eb17bbe5
36
cli.go
36
cli.go
|
@ -13,10 +13,27 @@ type CLI struct {
|
||||||
bc *Blockchain
|
bc *Blockchain
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (cli *CLI) getBalance(address string) {
|
||||||
|
balance := 0
|
||||||
|
|
||||||
|
utxs := cli.bc.FindUnspentTransactions(address)
|
||||||
|
|
||||||
|
for _, tx := range utxs {
|
||||||
|
for _, out := range tx.Vout {
|
||||||
|
if out.Unlock(address) {
|
||||||
|
balance += out.Value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Balance of '%s': %d\n", address, balance)
|
||||||
|
}
|
||||||
|
|
||||||
func (cli *CLI) printUsage() {
|
func (cli *CLI) printUsage() {
|
||||||
fmt.Println("Usage:")
|
fmt.Println("Usage:")
|
||||||
fmt.Println(" printchain - print all the blocks of the blockchain")
|
fmt.Println(" getbalance -address ADDRESS - Get balance of ADDRESS")
|
||||||
fmt.Println(" send -from FROM -to TO -amount AMOUNT - send AMOUNT of coins from FROM address to TO")
|
fmt.Println(" printchain - Print all the blocks of the blockchain")
|
||||||
|
fmt.Println(" send -from FROM -to TO -amount AMOUNT - Send AMOUNT of coins from FROM address to TO")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *CLI) validateArgs() {
|
func (cli *CLI) validateArgs() {
|
||||||
|
@ -54,14 +71,21 @@ func (cli *CLI) send(from, to string, amount int) {
|
||||||
func (cli *CLI) Run() {
|
func (cli *CLI) Run() {
|
||||||
cli.validateArgs()
|
cli.validateArgs()
|
||||||
|
|
||||||
|
getBalanceCmd := flag.NewFlagSet("getbalance", flag.ExitOnError)
|
||||||
sendCmd := flag.NewFlagSet("send", flag.ExitOnError)
|
sendCmd := flag.NewFlagSet("send", flag.ExitOnError)
|
||||||
printChainCmd := flag.NewFlagSet("printchain", flag.ExitOnError)
|
printChainCmd := flag.NewFlagSet("printchain", flag.ExitOnError)
|
||||||
|
|
||||||
|
getBalanceAddress := getBalanceCmd.String("address", "", "The address to get balance for")
|
||||||
sendFrom := sendCmd.String("from", "", "Source wallet address")
|
sendFrom := sendCmd.String("from", "", "Source wallet address")
|
||||||
sendTo := sendCmd.String("to", "", "Destination wallet address")
|
sendTo := sendCmd.String("to", "", "Destination wallet address")
|
||||||
sendAmount := sendCmd.Int("amount", 0, "Amount to send")
|
sendAmount := sendCmd.Int("amount", 0, "Amount to send")
|
||||||
|
|
||||||
switch os.Args[1] {
|
switch os.Args[1] {
|
||||||
|
case "getbalance":
|
||||||
|
err := getBalanceCmd.Parse(os.Args[2:])
|
||||||
|
if err != nil {
|
||||||
|
log.Panic(err)
|
||||||
|
}
|
||||||
case "printchain":
|
case "printchain":
|
||||||
err := printChainCmd.Parse(os.Args[2:])
|
err := printChainCmd.Parse(os.Args[2:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -77,6 +101,14 @@ func (cli *CLI) Run() {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if getBalanceCmd.Parsed() {
|
||||||
|
if *getBalanceAddress == "" {
|
||||||
|
getBalanceCmd.Usage()
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
cli.getBalance(*getBalanceAddress)
|
||||||
|
}
|
||||||
|
|
||||||
if printChainCmd.Parsed() {
|
if printChainCmd.Parsed() {
|
||||||
cli.printChain()
|
cli.printChain()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue