Skip to content

Commit

Permalink
updated zcnstatus to use common.Balance
Browse files Browse the repository at this point in the history
  • Loading branch information
bbist committed Sep 8, 2021
1 parent aa6055f commit 25c0f5a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 31 deletions.
5 changes: 3 additions & 2 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"sync"

"github.com/0chain/gosdk/core/common"
"github.com/0chain/gosdk/zcncore"
"gopkg.in/cheggaaa/pb.v1"
)
Expand All @@ -19,7 +20,7 @@ type ZCNStatus struct {
wg *sync.WaitGroup
success bool
errMsg string
balance int64
balance common.Balance
wallets []string
clientID string
}
Expand All @@ -38,7 +39,7 @@ func (zcn *ZCNStatus) OnBalanceAvailable(status int, value int64, info string) {
} else {
zcn.success = false
}
zcn.balance = value
zcn.balance = common.Balance(value)
}

func (zcn *ZCNStatus) OnTransactionComplete(t *zcncore.Transaction, status int) {
Expand Down
29 changes: 14 additions & 15 deletions cmd/getbalance.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,22 @@ var getbalancecmd = &cobra.Command{
statusBar := &ZCNStatus{wg: wg}
wg.Add(1)
err := zcncore.GetBalance(statusBar)
if err == nil {
wg.Wait()
} else {
ExitWithError(err.Error())
if err != nil {
ExitWithError(err)
return
}
if statusBar.success {
token := zcncore.ConvertToToken(statusBar.balance)
tokenUSD, err := zcncore.ConvertTokenToUSD(token)
if err != nil {
ExitWithError("\nGet balance failed. " + err.Error() + "\n")
} else {
fmt.Printf("\nBalance: %v (%v USD)\n", token, tokenUSD)
}
} else {
ExitWithError("\nGet balance failed. " + statusBar.errMsg + "\n")
wg.Wait()
if !statusBar.success {
ExitWithError(fmt.Sprintf("\nFailed to get balance: %s\n", statusBar.errMsg))
return
}
return
b := statusBar.balance
usd, err := zcncore.ConvertTokenToUSD(b.ToToken())
if err != nil {
ExitWithError(fmt.Sprintf("\nBalance: %v (Failed to get USD: %v)\n", b, err))
return
}
fmt.Printf("\nBalance: %v (%.2f USD)\n", b, usd)
},
}

Expand Down
22 changes: 8 additions & 14 deletions cmd/mswallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,23 +343,17 @@ func checkBalance(wallet string) bool {
statusBar := &ZCNStatus{wg: wg}
wg.Add(1)
err := zcncore.GetBalanceWallet(wallet, statusBar)
if err == nil {
wg.Wait()
} else {
fmt.Println(err.Error())
os.Exit(1)
if err != nil {
ExitWithError(err)
return false
}
if statusBar.success {
fmt.Printf("\nBalance: %v\n", zcncore.ConvertToToken(statusBar.balance))
if zcncore.ConvertToToken(statusBar.balance) > 0 {
return true
}
wg.Wait()
if !statusBar.success {
ExitWithError(fmt.Sprintf("\nFailed to get balance: %s\n", statusBar.errMsg))
return false

}
fmt.Println("\nGet balance failed. " + statusBar.errMsg + "\n")
return false

fmt.Printf("\nBalance: %v\n", statusBar.balance)
return statusBar.balance.ToToken() > 0
}

func createAWallet() string {
Expand Down

0 comments on commit 25c0f5a

Please sign in to comment.