[wallet] add cli -p profile to wallet

wallet supports configuration file

Signed-off-by: Leo Chen <leo@harmony.one>
pull/689/head
Leo Chen 6 years ago
parent 875d5f02f9
commit b7d72a6fd9
  1. 83
      cmd/client/wallet/main.go

@ -54,6 +54,8 @@ type AccountState struct {
const ( const (
rpcRetry = 3 rpcRetry = 3
defaultConfigFile = ".hmy/wallet.ini"
defaultProfile = "default"
) )
var ( var (
@ -77,32 +79,7 @@ var (
) )
var ( var (
// list of bootnodes walletProfile *utils.WalletProfile
addrStrings = []string{"/ip4/100.26.90.187/tcp/9876/p2p/QmZJJx6AdaoEkGLrYG4JeLCKeCKDjnFz2wfHNHxAqFSGA9", "/ip4/54.213.43.194/tcp/9876/p2p/QmQayinFSgMMw5cSpDUiD9pQ2WeP6WNmGxpZ6ou3mdVFJX"}
// list of rpc servers
rpcServers = []p2p.Peer{
p2p.Peer{
IP: "18.236.187.250",
Port: "14555",
},
p2p.Peer{
IP: "54.186.236.223",
Port: "14555",
},
p2p.Peer{
IP: "18.213.246.142",
Port: "14555",
},
p2p.Peer{
IP: "75.101.226.226",
Port: "14555",
},
p2p.Peer{
IP: "34.221.85.140",
Port: "14555",
},
}
) )
// setupLog setup log for verbose output // setupLog setup log for verbose output
@ -122,7 +99,10 @@ func main() {
// os.Arg[1] will be the subcommand // os.Arg[1] will be the subcommand
if len(os.Args) < 2 { if len(os.Args) < 2 {
fmt.Println("Usage:") fmt.Println("Usage:")
fmt.Println(" wallet <action> <params>") fmt.Println(" wallet -p profile <action> <params>")
fmt.Println(" -p profile - Specify the profile of the wallet, either testnet/devnet or others configured. Default is: testnet")
fmt.Println(" The profile is in file:", defaultConfigFile)
fmt.Println()
fmt.Println("Actions:") fmt.Println("Actions:")
fmt.Println(" 1. new - Generates a new account and store the private key locally") fmt.Println(" 1. new - Generates a new account and store the private key locally")
fmt.Println(" 2. list - Lists all accounts in local keystore") fmt.Println(" 2. list - Lists all accounts in local keystore")
@ -133,7 +113,7 @@ func main() {
fmt.Println(" --address - The address to check balance for") fmt.Println(" --address - The address to check balance for")
fmt.Println(" 6. getFreeToken - Gets free token on each shard") fmt.Println(" 6. getFreeToken - Gets free token on each shard")
fmt.Println(" --address - The free token receiver account's address") fmt.Println(" --address - The free token receiver account's address")
fmt.Println(" 7. transfer") fmt.Println(" 7. transfer - Transfer token from one account to another")
fmt.Println(" --from - The sender account's address or index in the local keystore") fmt.Println(" --from - The sender account's address or index in the local keystore")
fmt.Println(" --to - The receiver account's address") fmt.Println(" --to - The receiver account's address")
fmt.Println(" --amount - The amount of token to transfer") fmt.Println(" --amount - The amount of token to transfer")
@ -149,22 +129,22 @@ ARG:
case "--verbose": case "--verbose":
setupLog() setupLog()
os.Args = os.Args[:len(os.Args)-1] os.Args = os.Args[:len(os.Args)-1]
case "--devnet":
// the multiaddress of bootnodes for devnet
addrStrings = []string{"/ip4/100.26.90.187/tcp/9871/p2p/Qmdfjtk6hPoyrH1zVD9PEH4zfWLo38dP2mDvvKXfh3tnEv", "/ip4/54.213.43.194/tcp/9871/p2p/QmRVbTpEYup8dSaURZfF6ByrMTSKa4UyUzJhSjahFzRqNj"}
os.Args = os.Args[:len(os.Args)-1]
default: default:
break ARG break ARG
} }
} }
if len(os.Getenv("RpcNodes")) > 0 { var profile string
rpcServers = utils.StringsToPeers(os.Getenv("RpcNodes")) if os.Args[1] == "-p" {
profile = os.Args[2]
os.Args = os.Args[2:]
} else {
profile = defaultProfile
} }
if len(rpcServers) == 0 { if len(os.Args) == 1 {
fmt.Println("Error: please set environment variable RpcNodes") fmt.Println("Missing action")
fmt.Println("Example: export RpcNodes=127.0.0.1:8000,192.168.0.1:9999") flag.PrintDefaults()
os.Exit(0) os.Exit(1)
} }
// Switch on the subcommand // Switch on the subcommand
@ -180,10 +160,13 @@ ARG:
case "import": case "import":
processImportCommnad() processImportCommnad()
case "balances": case "balances":
readProfile(profile)
processBalancesCommand() processBalancesCommand()
case "getFreeToken": case "getFreeToken":
readProfile(profile)
processGetFreeToken() processGetFreeToken()
case "transfer": case "transfer":
readProfile(profile)
processTransferCommand() processTransferCommand()
default: default:
fmt.Printf("Unknown action: %s\n", os.Args[1]) fmt.Printf("Unknown action: %s\n", os.Args[1])
@ -192,9 +175,19 @@ ARG:
} }
} }
func readProfile(profile string) {
fmt.Printf("Using %s profile for wallet\n", profile)
var err error
walletProfile, err = utils.ReadWalletProfile(defaultConfigFile, profile)
if err != nil {
fmt.Printf("Read wallet profile error: %v\nExiting ...\n", err)
os.Exit(2)
}
}
// createWalletNode creates wallet server node. // createWalletNode creates wallet server node.
func createWalletNode() *node.Node { func createWalletNode() *node.Node {
bootNodeAddrs, err := utils.StringsToAddrs(addrStrings) bootNodeAddrs, err := utils.StringsToAddrs(walletProfile.Bootnodes)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -427,14 +420,14 @@ func convertBalanceIntoReadableFormat(balance *big.Int) string {
} }
// FetchBalance fetches account balance of specified address from the Harmony network // FetchBalance fetches account balance of specified address from the Harmony network
// TODO: (chao) add support for non beacon chain shards
func FetchBalance(address common.Address) map[uint32]AccountState { func FetchBalance(address common.Address) map[uint32]AccountState {
result := make(map[uint32]AccountState) result := make(map[uint32]AccountState)
balance := big.NewInt(0) balance := big.NewInt(0)
result[0] = AccountState{balance, 0} for i := 0; i < walletProfile.Shards; i++ {
result[uint32(i)] = AccountState{balance, 0}
for retry := 0; retry < rpcRetry; retry++ { for retry := 0; retry < rpcRetry; retry++ {
server := rpcServers[rand.Intn(len(rpcServers))] server := walletProfile.RPCServer[i][rand.Intn(len(walletProfile.RPCServer[i]))]
client, err := clientService.NewClient(server.IP, server.Port) client, err := clientService.NewClient(server.IP, server.Port)
if err != nil { if err != nil {
continue continue
@ -448,16 +441,18 @@ func FetchBalance(address common.Address) map[uint32]AccountState {
} }
log.Debug("FetchBalance", "response", response) log.Debug("FetchBalance", "response", response)
balance.SetBytes(response.Balance) balance.SetBytes(response.Balance)
result[0] = AccountState{balance, response.Nonce} result[uint32(i)] = AccountState{balance, response.Nonce}
break break
} }
}
return result return result
} }
// GetFreeToken requests for token test token on each shard // GetFreeToken requests for token test token on each shard
func GetFreeToken(address common.Address) { func GetFreeToken(address common.Address) {
for retry := 0; retry < rpcRetry; retry++ { for retry := 0; retry < rpcRetry; retry++ {
server := rpcServers[0] // use the 1st server from shard 0 (beacon chain) to make the getFreeToken call
server := walletProfile.RPCServer[0][0]
client, err := clientService.NewClient(server.IP, server.Port) client, err := clientService.NewClient(server.IP, server.Port)
if err != nil { if err != nil {
continue continue

Loading…
Cancel
Save