Compare commits
3 Commits
add-balanc
...
master
Author | SHA1 | Date | |
---|---|---|---|
a3dae12c7a | |||
57426b3565 | |||
b42dec8373 |
@ -3,7 +3,6 @@ package application
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"path"
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
@ -1504,17 +1503,14 @@ func loadUserContent(ctx context.Context, activeSym string, balance string, alia
|
|||||||
l := gotext.NewLocale(translationDir, code)
|
l := gotext.NewLocale(translationDir, code)
|
||||||
l.AddDomain("default")
|
l.AddDomain("default")
|
||||||
|
|
||||||
balFloat, err := strconv.ParseFloat(balance, 64)
|
// Format the balance to 2 decimal places or default to 0.00
|
||||||
|
formattedAmount, err := store.TruncateDecimalString(balance, 2)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
//Only exclude ErrSyntax error to avoid returning an error if the active bal is not available yet
|
formattedAmount = "0.00"
|
||||||
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)
|
// format the final output
|
||||||
|
balStr := fmt.Sprintf("%s %s", formattedAmount, activeSym)
|
||||||
if alias != "" {
|
if alias != "" {
|
||||||
content = l.Get("%s balance: %s\n", alias, balStr)
|
content = l.Get("%s balance: %s\n", alias, balStr)
|
||||||
} else {
|
} else {
|
||||||
|
@ -1840,20 +1840,42 @@ func TestCheckBalance(t *testing.T) {
|
|||||||
name string
|
name string
|
||||||
sessionId string
|
sessionId string
|
||||||
publicKey string
|
publicKey string
|
||||||
|
alias string
|
||||||
activeSym string
|
activeSym string
|
||||||
activeBal string
|
activeBal string
|
||||||
expectedResult resource.Result
|
expectedResult resource.Result
|
||||||
expectError bool
|
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",
|
name: "User with active sym",
|
||||||
sessionId: "session123",
|
sessionId: "session123",
|
||||||
publicKey: "0X98765432109",
|
publicKey: "0X98765432109",
|
||||||
|
alias: "",
|
||||||
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,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
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 {
|
for _, tt := range tests {
|
||||||
@ -1866,13 +1888,25 @@ func TestCheckBalance(t *testing.T) {
|
|||||||
accountService: mockAccountService,
|
accountService: mockAccountService,
|
||||||
}
|
}
|
||||||
|
|
||||||
err := store.WriteEntry(ctx, tt.sessionId, storedb.DATA_ACTIVE_SYM, []byte(tt.activeSym))
|
if tt.alias != "" {
|
||||||
if err != nil {
|
err := store.WriteEntry(ctx, tt.sessionId, storedb.DATA_ACCOUNT_ALIAS, []byte(tt.alias))
|
||||||
t.Fatal(err)
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
err = store.WriteEntry(ctx, tt.sessionId, storedb.DATA_ACTIVE_BAL, []byte(tt.activeBal))
|
|
||||||
if err != nil {
|
if tt.activeSym != "" {
|
||||||
t.Fatal(err)
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := h.CheckBalance(ctx, "check_balance", []byte(""))
|
res, err := h.CheckBalance(ctx, "check_balance", []byte(""))
|
||||||
|
Loading…
Reference in New Issue
Block a user