Compare commits

..

No commits in common. "a3dae12c7ad9a297a03fa39238077de0b18f4348" and "b42dec8373ccf6e1874b29f04d41ef7c82b16b51" have entirely different histories.

2 changed files with 16 additions and 46 deletions

View File

@ -3,6 +3,7 @@ package application
import (
"bytes"
"context"
"errors"
"fmt"
"path"
"strconv"
@ -1503,14 +1504,17 @@ func loadUserContent(ctx context.Context, activeSym string, balance string, alia
l := gotext.NewLocale(translationDir, code)
l.AddDomain("default")
// Format the balance to 2 decimal places or default to 0.00
formattedAmount, err := store.TruncateDecimalString(balance, 2)
balFloat, err := strconv.ParseFloat(balance, 64)
if err != nil {
formattedAmount = "0.00"
//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 the final output
balStr := fmt.Sprintf("%s %s", formattedAmount, activeSym)
// Format to 2 decimal places
balStr := fmt.Sprintf("%.2f %s", balFloat, activeSym)
if alias != "" {
content = l.Get("%s balance: %s\n", alias, balStr)
} else {

View File

@ -1840,42 +1840,20 @@ func TestCheckBalance(t *testing.T) {
name string
sessionId string
publicKey string
alias string
activeSym string
activeBal string
expectedResult resource.Result
expectError bool
}{
{
name: "User with no active sym",
sessionId: "session123",
publicKey: "0X98765432109",
alias: "",
activeSym: "",
activeBal: "",
expectedResult: resource.Result{Content: "Balance: 0.00 \n"},
expectError: false,
},
{
name: "User with active sym",
sessionId: "session123",
publicKey: "0X98765432109",
alias: "",
activeSym: "ETH",
activeBal: "1.5",
expectedResult: resource.Result{Content: "Balance: 1.50 ETH\n"},
expectError: false,
},
{
name: "User with active sym and alias",
sessionId: "session123",
publicKey: "0X98765432109",
alias: "user72",
activeSym: "SRF",
activeBal: "10.967",
expectedResult: resource.Result{Content: "user72 balance: 10.96 SRF\n"},
expectError: false,
},
}
for _, tt := range tests {
@ -1888,25 +1866,13 @@ func TestCheckBalance(t *testing.T) {
accountService: mockAccountService,
}
if tt.alias != "" {
err := store.WriteEntry(ctx, tt.sessionId, storedb.DATA_ACCOUNT_ALIAS, []byte(tt.alias))
if err != nil {
t.Fatal(err)
}
err := store.WriteEntry(ctx, tt.sessionId, storedb.DATA_ACTIVE_SYM, []byte(tt.activeSym))
if err != nil {
t.Fatal(err)
}
if tt.activeSym != "" {
err := store.WriteEntry(ctx, tt.sessionId, storedb.DATA_ACTIVE_SYM, []byte(tt.activeSym))
if err != nil {
t.Fatal(err)
}
}
if tt.activeBal != "" {
err := store.WriteEntry(ctx, tt.sessionId, storedb.DATA_ACTIVE_BAL, []byte(tt.activeBal))
if err != nil {
t.Fatal(err)
}
err = store.WriteEntry(ctx, tt.sessionId, storedb.DATA_ACTIVE_BAL, []byte(tt.activeBal))
if err != nil {
t.Fatal(err)
}
res, err := h.CheckBalance(ctx, "check_balance", []byte(""))