-
Notifications
You must be signed in to change notification settings - Fork 7
/
erc20cmd.go
104 lines (90 loc) · 3.23 KB
/
erc20cmd.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
package internal
import (
"encoding/json"
"fmt"
"os"
"github.com/spf13/cobra"
erc20bridge "github.com/threefoldtech/rivine-extension-erc20/api/bridge"
erc20api "github.com/threefoldtech/rivine-extension-erc20/http"
"github.com/threefoldtech/rivine/pkg/cli"
)
// CreateERC20Cmd creates rootcommand for ERC20 and adds a subcommand
// if rootcommand executed the user will also see the output of the syncing status of ethereum
func CreateERC20Cmd(client *CommandLineClient) *cobra.Command {
erc20SubCmds := &erc20SubCmds{cli: client}
// define Rootcommand
var (
rootCmd = &cobra.Command{
Use: "erc20",
Short: "Perform erc20 actions",
Run: erc20SubCmds.getSyncingStatus,
}
getSyncingStatusCmd = &cobra.Command{
Use: "syncstatus",
Short: "Get the ethereum dowloader sync status",
Run: erc20SubCmds.getSyncingStatus,
}
getBalanceInfoCmd = &cobra.Command{
Use: "balance",
Short: "Get the ethereum balance and address information",
Run: erc20SubCmds.getBalanceInfo,
}
)
rootCmd.AddCommand(getSyncingStatusCmd)
rootCmd.AddCommand(getBalanceInfoCmd)
// register flags
rootCmd.PersistentFlags().Var(
cli.NewEncodingTypeFlag(cli.EncodingTypeHuman, &erc20SubCmds.persistentCfg.EncodingType, cli.EncodingTypeHuman|cli.EncodingTypeJSON), "encoding",
cli.EncodingTypeFlagDescription(cli.EncodingTypeHuman|cli.EncodingTypeJSON))
return rootCmd
}
type erc20SubCmds struct {
cli *CommandLineClient
persistentCfg struct {
EncodingType cli.EncodingType
}
}
// getSyncingStatus Gets the ethereum blockchain syncing status from the deamon API
func (erc20SubCmds *erc20SubCmds) getSyncingStatus(cmd *cobra.Command, args []string) {
var syncingStatus erc20api.ERC20SyncingStatus
err := erc20SubCmds.cli.GetWithResponse("/erc20/downloader/status", &syncingStatus)
if err != nil {
cli.DieWithError("error while fetching the syncing status", err)
}
// encode depending on the encoding flag
switch erc20SubCmds.persistentCfg.EncodingType {
case cli.EncodingTypeHuman:
fmt.Printf(`
Ethereum sync status:
Starting block height: %d
Current block height: %d
Highest block height: %d
`, syncingStatus.Status.StartingBlock, syncingStatus.Status.CurrentBlock, syncingStatus.Status.HighestBlock)
case cli.EncodingTypeJSON:
err = json.NewEncoder(os.Stdout).Encode(syncingStatus.Status)
if err != nil {
cli.DieWithError("failed to encode syncing status", err)
}
}
}
// getSyncingStatus Gets the ethereum blockchain syncing status from the deamon API
func (erc20SubCmds *erc20SubCmds) getBalanceInfo(cmd *cobra.Command, args []string) {
var balanceInfo erc20api.ERC20BalanceInformation
err := erc20SubCmds.cli.GetWithResponse("/erc20/account/balance", &balanceInfo)
if err != nil {
cli.DieWithError("error while fetching the balance information", err)
}
// encode depending on the encoding flag
switch erc20SubCmds.persistentCfg.EncodingType {
case cli.EncodingTypeHuman:
ether := erc20bridge.Denominate(balanceInfo.BalanceInfo.Balance)
fmt.Printf(`Address: %s
Balance: %s
`, balanceInfo.BalanceInfo.Address.String(), ether)
case cli.EncodingTypeJSON:
err = json.NewEncoder(os.Stdout).Encode(balanceInfo.BalanceInfo)
if err != nil {
cli.DieWithError("failed to encode balance information", err)
}
}
}