Compare commits
No commits in common. "e97d4be296ee2dd35197b28aabb7487fc3061886" and "20c8af582c223e912de97017d80fe208f1c6dc94" have entirely different histories.
e97d4be296
...
20c8af582c
@ -3,7 +3,6 @@ package application
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"path"
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
@ -1120,6 +1119,7 @@ func (h *MenuHandlers) GetCurrentProfileInfo(ctx context.Context, sym string, in
|
|||||||
logg.ErrorCtxf(ctx, "Failed to read account alias entry with", "key", "error", storedb.DATA_ACCOUNT_ALIAS, err)
|
logg.ErrorCtxf(ctx, "Failed to read account alias entry with", "key", "error", storedb.DATA_ACCOUNT_ALIAS, err)
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
res.FlagSet = append(res.FlagSet, flag_offerings_set)
|
||||||
res.Content = string(profileInfo)
|
res.Content = string(profileInfo)
|
||||||
default:
|
default:
|
||||||
break
|
break
|
||||||
@ -1438,33 +1438,6 @@ func (h *MenuHandlers) ShowBlockedAccount(ctx context.Context, sym string, input
|
|||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// loadUserContent loads the main user content in the main menu: the alias,balance associated with active voucher
|
|
||||||
func loadUserContent(ctx context.Context, activeSym string, balance string, alias string) (string, error) {
|
|
||||||
var content string
|
|
||||||
|
|
||||||
code := codeFromCtx(ctx)
|
|
||||||
l := gotext.NewLocale(translationDir, code)
|
|
||||||
l.AddDomain("default")
|
|
||||||
|
|
||||||
balFloat, err := strconv.ParseFloat(balance, 64)
|
|
||||||
if err != nil {
|
|
||||||
//Only exclude ErrSyntax error to avoid returning an error if the active bal is not available yet
|
|
||||||
if !errors.Is(err, strconv.ErrSyntax) {
|
|
||||||
logg.ErrorCtxf(ctx, "failed to parse activeBal as float", "value", balance, "error", err)
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
balFloat = 0.00
|
|
||||||
}
|
|
||||||
// Format to 2 decimal places
|
|
||||||
balStr := fmt.Sprintf("%.2f %s", balFloat, activeSym)
|
|
||||||
if alias != "" {
|
|
||||||
content = l.Get("%s balance: %s\n", alias, balStr)
|
|
||||||
} else {
|
|
||||||
content = l.Get("balance: %s\n", balStr)
|
|
||||||
}
|
|
||||||
return content, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// CheckBalance retrieves the balance of the active voucher and sets
|
// CheckBalance retrieves the balance of the active voucher and sets
|
||||||
// the balance as the result content.
|
// the balance as the result content.
|
||||||
func (h *MenuHandlers) CheckBalance(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
func (h *MenuHandlers) CheckBalance(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||||
@ -1481,6 +1454,10 @@ func (h *MenuHandlers) CheckBalance(ctx context.Context, sym string, input []byt
|
|||||||
return res, fmt.Errorf("missing session")
|
return res, fmt.Errorf("missing session")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
code := codeFromCtx(ctx)
|
||||||
|
l := gotext.NewLocale(translationDir, code)
|
||||||
|
l.AddDomain("default")
|
||||||
|
|
||||||
store := h.userdataStore
|
store := h.userdataStore
|
||||||
|
|
||||||
accAlias, err := store.ReadEntry(ctx, sessionId, storedb.DATA_ACCOUNT_ALIAS)
|
accAlias, err := store.ReadEntry(ctx, sessionId, storedb.DATA_ACCOUNT_ALIAS)
|
||||||
@ -1496,24 +1473,41 @@ func (h *MenuHandlers) CheckBalance(ctx context.Context, sym string, input []byt
|
|||||||
// get the active sym and active balance
|
// get the active sym and active balance
|
||||||
activeSym, err := store.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_SYM)
|
activeSym, err := store.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_SYM)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !db.IsNotFound(err) {
|
if db.IsNotFound(err) {
|
||||||
|
balance := "0.00"
|
||||||
|
if alias != "" {
|
||||||
|
content = l.Get("%s balance: %s\n", alias, balance)
|
||||||
|
} else {
|
||||||
|
content = l.Get("balance: %s\n", balance)
|
||||||
|
}
|
||||||
|
res.Content = content
|
||||||
|
return res, nil
|
||||||
|
}
|
||||||
|
|
||||||
logg.ErrorCtxf(ctx, "failed to read activeSym entry with", "key", storedb.DATA_ACTIVE_SYM, "error", err)
|
logg.ErrorCtxf(ctx, "failed to read activeSym entry with", "key", storedb.DATA_ACTIVE_SYM, "error", err)
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
activeBal, err := store.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_BAL)
|
activeBal, err := store.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_BAL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !db.IsNotFound(err) {
|
|
||||||
logg.ErrorCtxf(ctx, "failed to read activeBal entry with", "key", storedb.DATA_ACTIVE_BAL, "error", err)
|
logg.ErrorCtxf(ctx, "failed to read activeBal entry with", "key", storedb.DATA_ACTIVE_BAL, "error", err)
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Convert activeBal from []byte to float64
|
||||||
|
balFloat, err := strconv.ParseFloat(string(activeBal), 64)
|
||||||
|
if err != nil {
|
||||||
|
logg.ErrorCtxf(ctx, "failed to parse activeBal as float", "value", string(activeBal), "error", err)
|
||||||
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
content, err = loadUserContent(ctx, string(activeSym), string(activeBal), alias)
|
// Format to 2 decimal places
|
||||||
if err != nil {
|
balStr := fmt.Sprintf("%.2f %s", balFloat, activeSym)
|
||||||
return res, err
|
|
||||||
|
if alias != "" {
|
||||||
|
content = l.Get("%s balance: %s\n", alias, balStr)
|
||||||
|
} else {
|
||||||
|
content = l.Get("balance: %s\n", balStr)
|
||||||
}
|
}
|
||||||
res.Content = content
|
res.Content = content
|
||||||
|
|
||||||
|
|||||||
@ -1727,7 +1727,7 @@ func TestCheckBalance(t *testing.T) {
|
|||||||
publicKey: "0X98765432109",
|
publicKey: "0X98765432109",
|
||||||
activeSym: "ETH",
|
activeSym: "ETH",
|
||||||
activeBal: "1.5",
|
activeBal: "1.5",
|
||||||
expectedResult: resource.Result{Content: "balance: 1.50 ETH\n"},
|
expectedResult: resource.Result{Content: "Balance: 1.50 ETH\n"},
|
||||||
expectError: false,
|
expectError: false,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user