[wallet] return to sync mode

Signed-off-by: Leo Chen <leo@harmony.one>
pull/609/head
Leo Chen 6 years ago
parent d1646b2765
commit 7f04ddb917
  1. 31
      cmd/client/wallet/main.go
  2. 5
      internal/wallet/wallet/lib.go

@ -72,13 +72,6 @@ var (
balanceCommand = flag.NewFlagSet("getFreeToken", flag.ExitOnError) balanceCommand = flag.NewFlagSet("getFreeToken", flag.ExitOnError)
balanceAddressPtr = balanceCommand.String("address", "", "Specify the account address to check balance for") balanceAddressPtr = balanceCommand.String("address", "", "Specify the account address to check balance for")
// Quit the program once stopChan received message
stopChan = make(chan struct{})
// Print out progress char
tick = time.NewTicker(time.Second)
// Flag to wait for async command: transfer
async = true
) )
var ( var (
@ -161,23 +154,12 @@ func main() {
switch os.Args[1] { switch os.Args[1] {
case "transfer": case "transfer":
go processTransferCommand() processTransferCommand()
default: default:
fmt.Printf("Unknown action: %s\n", os.Args[1]) fmt.Printf("Unknown action: %s\n", os.Args[1])
flag.PrintDefaults() flag.PrintDefaults()
os.Exit(1) os.Exit(1)
} }
// Waiting for async call finished and print out some progress
for async {
select {
case <-tick.C:
fmt.Printf("=")
case <-stopChan:
fmt.Println("Done.")
os.Exit(0)
}
}
} }
// createWalletNode creates wallet server node. // createWalletNode creates wallet server node.
@ -298,24 +280,20 @@ func processTransferCommand() {
if err != nil { if err != nil {
fmt.Printf("Cannot base64-decode input data (%s): %s\n", fmt.Printf("Cannot base64-decode input data (%s): %s\n",
base64InputData, err) base64InputData, err)
async = false
return return
} }
if shardID == -1 { if shardID == -1 {
fmt.Println("Please specify the shard ID for the transfer (e.g. --shardID=0)") fmt.Println("Please specify the shard ID for the transfer (e.g. --shardID=0)")
async = false
return return
} }
if amount <= 0 { if amount <= 0 {
fmt.Println("Please specify positive amount to transfer") fmt.Println("Please specify positive amount to transfer")
async = false
return return
} }
priKeys := readPrivateKeys() priKeys := readPrivateKeys()
if len(priKeys) == 0 { if len(priKeys) == 0 {
fmt.Println("No imported account to use.") fmt.Println("No imported account to use.")
async = false
return return
} }
senderIndex, err := strconv.Atoi(sender) senderIndex, err := strconv.Atoi(sender)
@ -330,21 +308,18 @@ func processTransferCommand() {
} }
if senderIndex == -1 { if senderIndex == -1 {
fmt.Println("The specified sender account does not exist in the wallet.") fmt.Println("The specified sender account does not exist in the wallet.")
async = false
return return
} }
} }
if senderIndex >= len(priKeys) { if senderIndex >= len(priKeys) {
fmt.Println("Sender account index out of bounds.") fmt.Println("Sender account index out of bounds.")
async = false
return return
} }
receiverAddress := common.HexToAddress(receiver) receiverAddress := common.HexToAddress(receiver)
if len(receiverAddress) != 20 { if len(receiverAddress) != 20 {
fmt.Println("The receiver address is not valid.") fmt.Println("The receiver address is not valid.")
async = false
return return
} }
@ -359,21 +334,19 @@ func processTransferCommand() {
state, ok := shardIDToAccountState[uint32(shardID)] state, ok := shardIDToAccountState[uint32(shardID)]
if !ok { if !ok {
fmt.Printf("Failed connecting to the shard %d\n", shardID) fmt.Printf("Failed connecting to the shard %d\n", shardID)
async = false
return return
} }
balance := state.balance balance := state.balance
balance = balance.Div(balance, big.NewInt(params.GWei)) balance = balance.Div(balance, big.NewInt(params.GWei))
if amount > float64(balance.Uint64())/params.GWei { if amount > float64(balance.Uint64())/params.GWei {
fmt.Printf("Balance is not enough for the transfer, current balance is %.6f\n", float64(balance.Uint64())/params.GWei) fmt.Printf("Balance is not enough for the transfer, current balance is %.6f\n", float64(balance.Uint64())/params.GWei)
async = false
return return
} }
amountBigInt := big.NewInt(int64(amount * params.GWei)) amountBigInt := big.NewInt(int64(amount * params.GWei))
amountBigInt = amountBigInt.Mul(amountBigInt, big.NewInt(params.GWei)) amountBigInt = amountBigInt.Mul(amountBigInt, big.NewInt(params.GWei))
tx, _ := types.SignTx(types.NewTransaction(state.nonce, receiverAddress, uint32(shardID), amountBigInt, params.TxGas, nil, inputData), types.HomesteadSigner{}, senderPriKey) tx, _ := types.SignTx(types.NewTransaction(state.nonce, receiverAddress, uint32(shardID), amountBigInt, params.TxGas, nil, inputData), types.HomesteadSigner{}, senderPriKey)
wallet.SubmitTransaction(tx, walletNode, uint32(shardID), stopChan) wallet.SubmitTransaction(tx, walletNode, uint32(shardID))
} }
func convertBalanceIntoReadableFormat(balance *big.Int) string { func convertBalanceIntoReadableFormat(balance *big.Int) string {

@ -57,7 +57,7 @@ func GetPeersFromBeaconChain(walletNode *node.Node) []p2p.Peer {
} }
// SubmitTransaction submits the transaction to the Harmony network // SubmitTransaction submits the transaction to the Harmony network
func SubmitTransaction(tx *types.Transaction, walletNode *node.Node, shardID uint32, stopChan chan struct{}) error { func SubmitTransaction(tx *types.Transaction, walletNode *node.Node, shardID uint32) error {
msg := proto_node.ConstructTransactionListMessageAccount(types.Transactions{tx}) msg := proto_node.ConstructTransactionListMessageAccount(types.Transactions{tx})
err := walletNode.GetHost().SendMessageToGroups([]p2p.GroupID{p2p.GroupIDBeaconClient}, p2p_host.ConstructP2pMessage(byte(0), msg)) err := walletNode.GetHost().SendMessageToGroups([]p2p.GroupID{p2p.GroupIDBeaconClient}, p2p_host.ConstructP2pMessage(byte(0), msg))
if err != nil { if err != nil {
@ -68,9 +68,6 @@ func SubmitTransaction(tx *types.Transaction, walletNode *node.Node, shardID uin
// FIXME (leo): how to we know the tx was successful sent to the network // FIXME (leo): how to we know the tx was successful sent to the network
// this is a hacky way to wait for sometime // this is a hacky way to wait for sometime
time.Sleep(2 * time.Second) time.Sleep(2 * time.Second)
if stopChan != nil {
stopChan <- struct{}{}
}
return nil return nil
} }

Loading…
Cancel
Save