Compare commits

...

44 Commits

Author SHA1 Message Date
823133aa37 Merge pull request 'New alias flow' (#96) from new-alias-flow into master
Some checks failed
release / docker (push) Has been cancelled
Reviewed-on: #96
2025-07-08 13:07:01 +02:00
44673ef61b
remove extra whitespace 2025-07-07 21:25:29 +03:00
559b67a3e6
display the full alias on its own line 2025-07-07 21:24:16 +03:00
b1eb460988
add a isAliasUnavailable helper function
Some checks failed
release / docker (push) Has been cancelled
2025-07-07 18:55:17 +03:00
619edc39f8
remove unused function GetSuggestedAlias 2025-07-07 17:52:59 +03:00
efa29b087e
remove unused node and function confirm_new_alias 2025-07-07 17:51:45 +03:00
e9988242b1
CATCH unavailable aliases and navigate to the alias_updated node 2025-07-07 17:47:18 +03:00
55afbedd1e
have a node to CATCH aliases that are not available (already registered aliases) 2025-07-07 17:46:25 +03:00
a3ab91f6a3
handle alias registration and updates on the single RequestCustomAlias function 2025-07-07 17:43:04 +03:00
92a122732d
add the home menu option 2025-07-07 17:41:56 +03:00
682785fc3f
add a new flag_alias_unavailable flag 2025-07-07 17:40:06 +03:00
ebb4581c27
renamed the update_alias to alias_updated 2025-07-07 14:41:46 +03:00
cd1936c12f
require accounts to be authorized to access the alias node 2025-07-07 14:17:29 +03:00
16dd0f6ebf
set the first voucher as the active voucher when the current active sym is not found in the vouchers response
Some checks failed
release / docker (push) Has been cancelled
2025-07-02 19:10:02 +03:00
a3dae12c7a
update the TestCheckBalance and add more test cases
Some checks failed
release / docker (push) Has been cancelled
2025-07-02 09:30:30 +03:00
57426b3565
use the TruncateDecimalString to format the displayed balance 2025-07-02 09:29:56 +03:00
b42dec8373 Merge pull request 'add balance to voucher list' (#89) from add-balance-to-voucher-list into master
Some checks failed
release / docker (push) Has been cancelled
Reviewed-on: #89
2025-07-02 07:51:34 +02:00
2807f039a7 Merge branch 'master' into add-balance-to-voucher-list 2025-07-02 08:50:50 +03:00
0e6334058d Merge pull request 'add functionality to Update the Alias' (#88) from update-alias into master
Some checks failed
release / docker (push) Has been cancelled
Reviewed-on: #88
2025-07-02 07:48:20 +02:00
ba2cb4b813
use the DATA_SUGGESTED_ALIAS to check the status 2025-07-01 01:29:52 +03:00
8416d4fddb
use a single alias variable 2025-07-01 01:18:03 +03:00
a33ff7ffda
added logging 2025-07-01 00:56:50 +03:00
7d1951ec7a
use updated sarafu-api with correct order of variables 2025-07-01 00:51:05 +03:00
2ad5c2e8df
use the correct PUT method in the update 2025-07-01 00:37:15 +03:00
e1219354bb
use the correct AliasUpdateURL on sarafu-api 2025-07-01 00:32:42 +03:00
b31a68ad8e
added logging 2025-07-01 00:27:45 +03:00
ea3a6d8382
call the UpdateAlias if the account has an Aias 2025-06-30 18:18:06 +03:00
0fcadd4634 Merge branch 'master' into update-alias 2025-06-30 14:43:55 +03:00
9234bfd579
update the TestGetVoucherList to the expected content 2025-06-30 13:36:07 +03:00
706b6fe629
include the balance next to each voucher 2025-06-30 13:31:55 +03:00
a543569236
add a helper function FormatVoucherList to combine the voucher symbols with their balances 2025-06-30 13:14:34 +03:00
dd56d52f4c Merge pull request 'fix-amount-conversion' (#87) from fix-amount-conversion into master
Some checks failed
release / docker (push) Has been cancelled
Reviewed-on: #87
2025-06-26 14:07:29 +02:00
3592e7747c
add a test case with large decimal amount 2025-06-26 12:30:44 +03:00
7fe40faa9d
use the TruncateDecimalString helper function to format amounts to 2 d.p 2025-06-26 12:30:14 +03:00
02fd02dc21
add a test for the TruncateDecimalString 2025-06-26 12:26:49 +03:00
b884b82197
add test cases with high decimals for the ParseAndScaleAmount func 2025-06-26 12:20:34 +03:00
280c382a3d
add a helper function 'TruncateDecimalString' to format amounts to specified decimal places 2025-06-26 12:19:13 +03:00
b497dde1e8
remove unused 'constructAccountAlias' func 2025-06-26 10:02:55 +03:00
cedd55fd31
use updated sarafu-api that includes the UpdateAlias functionality 2025-06-26 09:56:29 +03:00
88926480dc
Return finalAmount as a string with 0 decimal places (rounded) 2025-06-24 15:11:15 +03:00
dabdf7eba2
add a test case to ensure the stored amount is not rounded off 2025-06-24 14:55:37 +03:00
96f6ca7d08
truncate two decimal places without risking float rounding issues 2025-06-24 14:55:09 +03:00
bc48dddd99
use the correct ContractAddress for the Last10TxResponse struct 2025-06-24 14:04:07 +03:00
2ec612978d
update sarafu-api to correctly unmarshal nested pool details response in RetrievePoolDetails
Some checks failed
release / docker (push) Has been cancelled
2025-06-24 12:10:04 +03:00
21 changed files with 377 additions and 266 deletions

2
go.mod
View File

@ -5,7 +5,7 @@ go 1.23.4
require (
git.defalsify.org/vise.git v0.3.2-0.20250528124150-03bf7bfc1b66
git.grassecon.net/grassrootseconomics/common v0.9.0-beta.1.0.20250417111317-2953f4c2f32e
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250624074830-5aa032400c12
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250630214912-814bef2b209a
git.grassecon.net/grassrootseconomics/visedriver v0.9.0-beta.2.0.20250408094335-e2d1f65bb306
git.grassecon.net/grassrootseconomics/visedriver-africastalking v0.0.0-20250129070628-5a539172c694
github.com/alecthomas/assert/v2 v2.2.2

10
go.sum
View File

@ -10,6 +10,16 @@ git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250623075057-
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250623075057-7b42d509e6d4/go.mod h1:y/vsN8UO0wSxZk3gv0y5df4RPKMJI6TIxjVcVCPF8T8=
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250624074830-5aa032400c12 h1:vD8biQmN36eouuE+TdxgXQjKisRf5cTGu/tMPv3afs0=
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250624074830-5aa032400c12/go.mod h1:y/vsN8UO0wSxZk3gv0y5df4RPKMJI6TIxjVcVCPF8T8=
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250624090744-339ba854c997 h1:8bCKyYoV4YiVBvCZlRclc3aQlBYpWhgtM35mvniDFD8=
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250624090744-339ba854c997/go.mod h1:y/vsN8UO0wSxZk3gv0y5df4RPKMJI6TIxjVcVCPF8T8=
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250626065419-57ee409f9629 h1:ew3vCFrLS/7/8uULTTPCbsHzFntQ6X68SScnBEy3pl0=
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250626065419-57ee409f9629/go.mod h1:y/vsN8UO0wSxZk3gv0y5df4RPKMJI6TIxjVcVCPF8T8=
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250630213135-50ee455e7069 h1:re+hdr5NAC6JqhyvjMCkgX17fFi0u3Mawc6RBnBJW8I=
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250630213135-50ee455e7069/go.mod h1:y/vsN8UO0wSxZk3gv0y5df4RPKMJI6TIxjVcVCPF8T8=
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250630213606-12940bb5f284 h1:2zMU9jPd6xEO6oY9oxr84sdT9G3d09eyAkjVBAz9eco=
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250630213606-12940bb5f284/go.mod h1:y/vsN8UO0wSxZk3gv0y5df4RPKMJI6TIxjVcVCPF8T8=
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250630214912-814bef2b209a h1:KuhJ/WY4RCGmrXUA680ciaponM4vM5zBOJfnCpUo2fc=
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250630214912-814bef2b209a/go.mod h1:y/vsN8UO0wSxZk3gv0y5df4RPKMJI6TIxjVcVCPF8T8=
git.grassecon.net/grassrootseconomics/visedriver v0.9.0-beta.2.0.20250408094335-e2d1f65bb306 h1:Jo+yWysWw/N5BJQtAyEMN8ePVvAyPHv+JG4lQti+1N4=
git.grassecon.net/grassrootseconomics/visedriver v0.9.0-beta.2.0.20250408094335-e2d1f65bb306/go.mod h1:FdLwYtzsjOIcDiW4uDgDYnB4Wdzq12uJUe0QHSSPbSo=
git.grassecon.net/grassrootseconomics/visedriver-africastalking v0.0.0-20250129070628-5a539172c694 h1:DjJlBSz0S13acft5XZDWk7ZYnzElym0xLMYEVgyNJ+E=

View File

@ -3,7 +3,6 @@ package application
import (
"bytes"
"context"
"errors"
"fmt"
"path"
"strconv"
@ -1496,7 +1495,7 @@ func (h *MenuHandlers) ShowBlockedAccount(ctx context.Context, sym string, input
return res, nil
}
// loadUserContent loads the main user content in the main menu: the alias,balance associated with active voucher
// loadUserContent loads the main user content in the main menu: the alias, balance and active symbol associated with active voucher
func loadUserContent(ctx context.Context, activeSym string, balance string, alias string) (string, error) {
var content string
@ -1504,19 +1503,16 @@ func loadUserContent(ctx context.Context, activeSym string, balance string, alia
l := gotext.NewLocale(translationDir, code)
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 {
//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
formattedAmount = "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 != "" {
content = l.Get("%s balance: %s\n", alias, balStr)
content = l.Get("%s\nBalance: %s\n", alias, balStr)
} else {
content = l.Get("Balance: %s\n", balStr)
}
@ -1529,7 +1525,6 @@ func (h *MenuHandlers) CheckBalance(ctx context.Context, sym string, input []byt
var (
res resource.Result
err error
alias string
content string
)
@ -1564,11 +1559,8 @@ func (h *MenuHandlers) CheckBalance(ctx context.Context, sym string, input []byt
logg.ErrorCtxf(ctx, "failed to read account alias entry with", "key", storedb.DATA_ACCOUNT_ALIAS, "error", err)
return res, err
}
} else {
alias = strings.Split(string(accAlias), ".")[0]
}
content, err = loadUserContent(ctx, string(activeSym), string(activeBal), alias)
content, err = loadUserContent(ctx, string(activeSym), string(activeBal), string(accAlias))
if err != nil {
return res, err
}
@ -1830,12 +1822,12 @@ func (h *MenuHandlers) ValidateAmount(ctx context.Context, sym string, input []b
return res, fmt.Errorf("missing session")
}
flag_invalid_amount, _ := h.flagManager.GetFlag("flag_invalid_amount")
store := h.userdataStore
userStore := h.userdataStore
var balanceValue float64
// retrieve the active balance
activeBal, err := store.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_BAL)
activeBal, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_BAL)
if err != nil {
logg.ErrorCtxf(ctx, "failed to read activeBal entry with", "key", storedb.DATA_ACTIVE_BAL, "error", err)
return res, err
@ -1861,9 +1853,15 @@ func (h *MenuHandlers) ValidateAmount(ctx context.Context, sym string, input []b
return res, nil
}
// Format the amount with 2 decimal places before saving
formattedAmount := fmt.Sprintf("%.2f", inputAmount)
err = store.WriteEntry(ctx, sessionId, storedb.DATA_AMOUNT, []byte(formattedAmount))
// Format the amount to 2 decimal places before saving (truncated)
formattedAmount, err := store.TruncateDecimalString(amountStr, 2)
if err != nil {
res.FlagSet = append(res.FlagSet, flag_invalid_amount)
res.Content = amountStr
return res, nil
}
err = userStore.WriteEntry(ctx, sessionId, storedb.DATA_AMOUNT, []byte(formattedAmount))
if err != nil {
logg.ErrorCtxf(ctx, "failed to write amount entry with", "key", storedb.DATA_AMOUNT, "value", formattedAmount, "error", err)
return res, err
@ -2086,8 +2084,9 @@ func (h *MenuHandlers) ManageVouchers(ctx context.Context, sym string, input []b
}
if activeData == nil {
logg.ErrorCtxf(ctx, "activeSym not found in vouchers", "activeSym", activeSymStr)
return res, fmt.Errorf("activeSym %s not found in vouchers", activeSymStr)
logg.ErrorCtxf(ctx, "activeSym not found in vouchers, setting the first voucher as the default", "activeSym", activeSymStr)
firstVoucher := vouchersResp[0]
activeData = &firstVoucher
}
// Scale down the balance
@ -2135,17 +2134,25 @@ func (h *MenuHandlers) GetVoucherList(ctx context.Context, sym string, input []b
// Read vouchers from the store
voucherData, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_VOUCHER_SYMBOLS)
logg.InfoCtxf(ctx, "reading GetVoucherList entries for sessionId: %s", sessionId, "key", storedb.DATA_VOUCHER_SYMBOLS, "voucherData", voucherData)
logg.InfoCtxf(ctx, "reading voucherData in GetVoucherList", "sessionId", sessionId, "key", storedb.DATA_VOUCHER_SYMBOLS, "voucherData", voucherData)
if err != nil {
logg.ErrorCtxf(ctx, "failed to read voucherData entires with", "key", storedb.DATA_VOUCHER_SYMBOLS, "error", err)
return res, err
}
formattedData := h.ReplaceSeparatorFunc(string(voucherData))
voucherBalances, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_VOUCHER_BALANCES)
logg.InfoCtxf(ctx, "reading voucherBalances in GetVoucherList", "sessionId", sessionId, "key", storedb.DATA_VOUCHER_BALANCES, "voucherBalances", voucherBalances)
if err != nil {
logg.ErrorCtxf(ctx, "failed to read voucherData entires with", "key", storedb.DATA_VOUCHER_BALANCES, "error", err)
return res, err
}
logg.InfoCtxf(ctx, "final output for sessionId: %s", sessionId, "key", storedb.DATA_VOUCHER_SYMBOLS, "formattedData", formattedData)
formattedVoucherList := store.FormatVoucherList(ctx, string(voucherData), string(voucherBalances))
finalOutput := strings.Join(formattedVoucherList, "\n")
res.Content = string(formattedData)
logg.InfoCtxf(ctx, "final output for GetVoucherList", "sessionId", sessionId, "finalOutput", finalOutput)
res.Content = finalOutput
return res, nil
}
@ -2561,55 +2568,10 @@ func (h *MenuHandlers) persistLanguageCode(ctx context.Context, code string) err
return h.persistInitialLanguageCode(ctx, sessionId, code)
}
// constructAccountAlias retrieves and alias based on the first and family name
// and writes the result in DATA_ACCOUNT_ALIAS
func (h *MenuHandlers) constructAccountAlias(ctx context.Context) error {
var alias string
store := h.userdataStore
sessionId, ok := ctx.Value("SessionId").(string)
if !ok {
return fmt.Errorf("missing session")
}
firstName, err := store.ReadEntry(ctx, sessionId, storedb.DATA_FIRST_NAME)
if err != nil {
if db.IsNotFound(err) {
return nil
}
return err
}
familyName, err := store.ReadEntry(ctx, sessionId, storedb.DATA_FAMILY_NAME)
if err != nil {
if db.IsNotFound(err) {
return nil
}
return err
}
pubKey, err := store.ReadEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY)
if err != nil {
if db.IsNotFound(err) {
return nil
}
return err
}
aliasInput := fmt.Sprintf("%s%s", firstName, familyName)
aliasResult, err := h.accountService.RequestAlias(ctx, string(pubKey), aliasInput)
if err != nil {
logg.ErrorCtxf(ctx, "failed to retrieve alias", "alias", aliasInput, "error_alias_request", err)
return fmt.Errorf("Failed to retrieve alias: %s", err.Error())
}
alias = aliasResult.Alias
//Store the alias
err = store.WriteEntry(ctx, sessionId, storedb.DATA_ACCOUNT_ALIAS, []byte(alias))
if err != nil {
logg.ErrorCtxf(ctx, "failed to write account alias", "key", storedb.DATA_ACCOUNT_ALIAS, "value", alias, "error", err)
return err
}
return nil
}
// RequestCustomAlias requests an ENS based alias name based on a user's input,then saves it as temporary value
func (h *MenuHandlers) RequestCustomAlias(ctx context.Context, sym string, input []byte) (resource.Result, error) {
var res resource.Result
var alias string
sessionId, ok := ctx.Value("SessionId").(string)
if !ok {
return res, fmt.Errorf("missing session")
@ -2619,6 +2581,7 @@ func (h *MenuHandlers) RequestCustomAlias(ctx context.Context, sym string, input
}
flag_api_error, _ := h.flagManager.GetFlag("flag_api_call_error")
flag_alias_unavailable, _ := h.flagManager.GetFlag("flag_alias_unavailable")
store := h.userdataStore
aliasHint, err := store.ReadEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE)
@ -2634,31 +2597,70 @@ func (h *MenuHandlers) RequestCustomAlias(ctx context.Context, sym string, input
if err != nil {
return res, err
}
pubKey, err := store.ReadEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY)
publicKey, err := store.ReadEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY)
if err != nil {
if db.IsNotFound(err) {
return res, nil
}
}
sanitizedInput := sanitizeAliasHint(string(input))
aliasResult, err := h.accountService.RequestAlias(ctx, string(pubKey), sanitizedInput)
if err != nil {
res.FlagSet = append(res.FlagSet, flag_api_error)
logg.ErrorCtxf(ctx, "failed to retrieve alias", "alias", string(aliasHint), "error_alias_request", err)
return res, nil
// Check if an alias already exists
existingAlias, err := store.ReadEntry(ctx, sessionId, storedb.DATA_ACCOUNT_ALIAS)
if err == nil && len(existingAlias) > 0 {
logg.InfoCtxf(ctx, "Current alias", "alias", string(existingAlias))
unavailable, err := h.isAliasUnavailable(ctx, sanitizedInput)
if err == nil && unavailable {
res.FlagSet = append(res.FlagSet, flag_alias_unavailable)
res.FlagReset = append(res.FlagReset, flag_api_error)
return res, nil
}
res.FlagReset = append(res.FlagReset, flag_alias_unavailable)
// Update existing alias
aliasResult, err := h.accountService.UpdateAlias(ctx, sanitizedInput, string(publicKey))
if err != nil {
res.FlagSet = append(res.FlagSet, flag_api_error)
logg.ErrorCtxf(ctx, "failed to update alias", "alias", sanitizedInput, "error", err)
return res, nil
}
alias = aliasResult.Alias
logg.InfoCtxf(ctx, "Updated alias", "alias", alias)
} else {
logg.InfoCtxf(ctx, "Registering a new alias", "err", err)
unavailable, err := h.isAliasUnavailable(ctx, sanitizedInput)
if err == nil && unavailable {
res.FlagSet = append(res.FlagSet, flag_alias_unavailable)
res.FlagReset = append(res.FlagReset, flag_api_error)
return res, nil
}
res.FlagReset = append(res.FlagReset, flag_alias_unavailable)
// Register a new alias
aliasResult, err := h.accountService.RequestAlias(ctx, string(publicKey), sanitizedInput)
if err != nil {
res.FlagSet = append(res.FlagSet, flag_api_error)
logg.ErrorCtxf(ctx, "failed to retrieve alias", "alias", sanitizedInput, "error_alias_request", err)
return res, nil
}
res.FlagReset = append(res.FlagReset, flag_api_error)
alias = aliasResult.Alias
logg.InfoCtxf(ctx, "Registered alias", "alias", alias)
}
res.FlagReset = append(res.FlagReset, flag_api_error)
alias := aliasResult.Alias
logg.InfoCtxf(ctx, "Suggested alias ", "alias", alias)
//Store the returned alias,wait for user to confirm it as new account alias
err = store.WriteEntry(ctx, sessionId, storedb.DATA_SUGGESTED_ALIAS, []byte(alias))
//Store the new account alias
logg.InfoCtxf(ctx, "Final registered alias", "alias", alias)
err = store.WriteEntry(ctx, sessionId, storedb.DATA_ACCOUNT_ALIAS, []byte(alias))
if err != nil {
logg.ErrorCtxf(ctx, "failed to write account alias", "key", storedb.DATA_TEMPORARY_VALUE, "value", alias, "error", err)
logg.ErrorCtxf(ctx, "failed to write account alias", "key", storedb.DATA_ACCOUNT_ALIAS, "value", alias, "error", err)
return res, err
}
}
return res, nil
}
@ -2673,53 +2675,19 @@ func sanitizeAliasHint(input string) string {
return input
}
// GetSuggestedAlias loads and displays the suggested alias name from the temporary value
func (h *MenuHandlers) GetSuggestedAlias(ctx context.Context, sym string, input []byte) (resource.Result, error) {
var res resource.Result
store := h.userdataStore
func (h *MenuHandlers) isAliasUnavailable(ctx context.Context, alias string) (bool, error) {
fqdn := fmt.Sprintf("%s.%s", alias, "sarafu.eth")
logg.InfoCtxf(ctx, "Checking if the fqdn alias is taken", "fqdn", fqdn)
sessionId, ok := ctx.Value("SessionId").(string)
if !ok {
return res, fmt.Errorf("missing session")
}
suggestedAlias, err := store.ReadEntry(ctx, sessionId, storedb.DATA_SUGGESTED_ALIAS)
aliasAddress, err := h.accountService.CheckAliasAddress(ctx, fqdn)
if err != nil {
return res, nil
return false, err
}
res.Content = string(suggestedAlias)
return res, nil
}
// ConfirmNewAlias reads the suggested alias from the [DATA_SUGGECTED_ALIAS] key and confirms it as the new account alias.
func (h *MenuHandlers) ConfirmNewAlias(ctx context.Context, sym string, input []byte) (resource.Result, error) {
var res resource.Result
store := h.userdataStore
logdb := h.logDb
flag_alias_set, _ := h.flagManager.GetFlag("flag_alias_set")
sessionId, ok := ctx.Value("SessionId").(string)
if !ok {
return res, fmt.Errorf("missing session")
}
newAlias, err := store.ReadEntry(ctx, sessionId, storedb.DATA_SUGGESTED_ALIAS)
if err != nil {
return res, nil
}
logg.InfoCtxf(ctx, "Confirming new alias", "alias", string(newAlias))
err = store.WriteEntry(ctx, sessionId, storedb.DATA_ACCOUNT_ALIAS, []byte(string(newAlias)))
if err != nil {
logg.ErrorCtxf(ctx, "failed to clear DATA_ACCOUNT_ALIAS_VALUE entry with", "key", storedb.DATA_ACCOUNT_ALIAS, "value", "empty", "error", err)
return res, err
if len(aliasAddress.Address) > 0 {
return true, nil
}
err = logdb.WriteLogEntry(ctx, sessionId, storedb.DATA_ACCOUNT_ALIAS, []byte(newAlias))
if err != nil {
logg.DebugCtxf(ctx, "Failed to write account alias db log entry", "key", storedb.DATA_ACCOUNT_ALIAS, "value", newAlias)
}
res.FlagSet = append(res.FlagSet, flag_alias_set)
return res, nil
return false, nil
}
// ClearTemporaryValue empties the DATA_TEMPORARY_VALUE at the main menu to prevent
@ -3046,7 +3014,15 @@ func (h *MenuHandlers) SwapPreview(ctx context.Context, sym string, input []byte
return res, nil
}
finalAmountStr, err := store.ParseAndScaleAmount(inputStr, swapData.ActiveSwapFromDecimal)
// Format the amount to 2 decimal places
formattedAmount, err := store.TruncateDecimalString(inputStr, 2)
if err != nil {
res.FlagSet = append(res.FlagSet, flag_invalid_amount)
res.Content = inputStr
return res, nil
}
finalAmountStr, err := store.ParseAndScaleAmount(formattedAmount, swapData.ActiveSwapFromDecimal)
if err != nil {
return res, err
}

View File

@ -1678,6 +1678,22 @@ func TestValidateAmount(t *testing.T) {
Content: "0.02ms",
},
},
{
name: "Test with valid decimal amount",
input: []byte("0.149"),
activeBal: []byte("5"),
expectedResult: resource.Result{
Content: "0.14",
},
},
{
name: "Test with valid large decimal amount",
input: []byte("1.8599999999"),
activeBal: []byte("5"),
expectedResult: resource.Result{
Content: "1.85",
},
},
}
for _, tt := range tests {
@ -1824,20 +1840,42 @@ 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 {
@ -1850,13 +1888,25 @@ func TestCheckBalance(t *testing.T) {
accountService: mockAccountService,
}
err := store.WriteEntry(ctx, tt.sessionId, storedb.DATA_ACTIVE_SYM, []byte(tt.activeSym))
if err != nil {
t.Fatal(err)
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_BAL, []byte(tt.activeBal))
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)
}
}
res, err := h.CheckBalance(ctx, "check_balance", []byte(""))
@ -2192,20 +2242,25 @@ func TestGetVoucherList(t *testing.T) {
ReplaceSeparatorFunc: mockReplaceSeparator,
}
mockSyms := []byte("1:SRF\n2:MILO")
mockSymbols := []byte("1:SRF\n2:MILO")
mockBalances := []byte("1:10.099999\n2:40.7")
// Put voucher sym data from the store
err := store.WriteEntry(ctx, sessionId, storedb.DATA_VOUCHER_SYMBOLS, mockSyms)
// Put voucher symnols and balances data to the store
err := store.WriteEntry(ctx, sessionId, storedb.DATA_VOUCHER_SYMBOLS, mockSymbols)
if err != nil {
t.Fatal(err)
}
err = store.WriteEntry(ctx, sessionId, storedb.DATA_VOUCHER_BALANCES, mockBalances)
if err != nil {
t.Fatal(err)
}
expectedSyms := []byte("1: SRF\n2: MILO")
expectedList := []byte("1: SRF 10.09\n2: MILO 40.70")
res, err := h.GetVoucherList(ctx, "", []byte(""))
assert.NoError(t, err)
assert.Equal(t, res.Content, string(expectedSyms))
assert.Equal(t, res.Content, string(expectedList))
}
func TestViewVoucher(t *testing.T) {
@ -2529,11 +2584,11 @@ func TestCheckTransactions(t *testing.T) {
mockTXResponse := []dataserviceapi.Last10TxResponse{
{
Sender: "0X13242618721", Recipient: "0x41c188d63Qa", TransferValue: "100", TokenAddress: "0X1324262343rfdGW23",
Sender: "0X13242618721", Recipient: "0x41c188d63Qa", TransferValue: "100", ContractAddress: "0X1324262343rfdGW23",
TxHash: "0x123wefsf34rf", DateBlock: time.Now(), TokenSymbol: "SRF", TokenDecimals: "6",
},
{
Sender: "0x41c188d63Qa", Recipient: "0X13242618721", TransferValue: "200", TokenAddress: "0X1324262343rfdGW23",
Sender: "0x41c188d63Qa", Recipient: "0X13242618721", TransferValue: "200", ContractAddress: "0X1324262343rfdGW23",
TxHash: "0xq34wresfdb44", DateBlock: time.Now(), TokenSymbol: "SRF", TokenDecimals: "6",
},
}
@ -2585,11 +2640,11 @@ func TestGetTransactionsList(t *testing.T) {
mockTXResponse := []dataserviceapi.Last10TxResponse{
{
Sender: "0X13242618721", Recipient: "0x41c188d63Qa", TransferValue: "1000", TokenAddress: "0X1324262343rfdGW23",
Sender: "0X13242618721", Recipient: "0x41c188d63Qa", TransferValue: "1000", ContractAddress: "0X1324262343rfdGW23",
TxHash: "0x123wefsf34rf", DateBlock: dateBlock, TokenSymbol: "SRF", TokenDecimals: "2",
},
{
Sender: "0x41c188d63Qa", Recipient: "0X13242618721", TransferValue: "2000", TokenAddress: "0X1324262343rfdGW23",
Sender: "0x41c188d63Qa", Recipient: "0X13242618721", TransferValue: "2000", ContractAddress: "0X1324262343rfdGW23",
TxHash: "0xq34wresfdb44", DateBlock: dateBlock, TokenSymbol: "SRF", TokenDecimals: "2",
},
}
@ -2654,11 +2709,11 @@ func TestViewTransactionStatement(t *testing.T) {
mockTXResponse := []dataserviceapi.Last10TxResponse{
{
Sender: "0X13242618721", Recipient: "0x41c188d63Qa", TransferValue: "1000", TokenAddress: "0X1324262343rfdGW23",
Sender: "0X13242618721", Recipient: "0x41c188d63Qa", TransferValue: "1000", ContractAddress: "0X1324262343rfdGW23",
TxHash: "0x123wefsf34rf", DateBlock: dateBlock, TokenSymbol: "SRF", TokenDecimals: "2",
},
{
Sender: "0x41c188d63Qa", Recipient: "0X13242618721", TransferValue: "2000", TokenAddress: "0X1324262343rfdGW23",
Sender: "0x41c188d63Qa", Recipient: "0X13242618721", TransferValue: "2000", ContractAddress: "0X1324262343rfdGW23",
TxHash: "0xq34wresfdb44", DateBlock: dateBlock, TokenSymbol: "SRF", TokenDecimals: "2",
},
}
@ -3154,97 +3209,6 @@ func TestResetUnregisteredNumber(t *testing.T) {
assert.Equal(t, expectedResult, res)
}
func TestConstructAccountAlias(t *testing.T) {
ctx, store := InitializeTestStore(t)
sessionId := "session123"
mockAccountService := new(mocks.MockAccountService)
ctx = context.WithValue(ctx, "SessionId", sessionId)
h := &MenuHandlers{
userdataStore: store,
accountService: mockAccountService,
}
tests := []struct {
name string
firstName string
familyName string
publicKey string
expectedAlias string
aliasResponse *models.RequestAliasResult
aliasError error
expectedError error
}{
{
name: "Valid alias construction",
firstName: "John",
familyName: "Doe",
publicKey: "pubkey123",
expectedAlias: "JohnDoeAlias",
aliasResponse: &models.RequestAliasResult{Alias: "JohnDoeAlias"},
aliasError: nil,
expectedError: nil,
},
{
name: "Account service fails to return alias",
firstName: "Jane",
familyName: "Smith",
publicKey: "pubkey456",
expectedAlias: "",
aliasResponse: nil,
aliasError: fmt.Errorf("service unavailable"),
expectedError: fmt.Errorf("Failed to retrieve alias: service unavailable"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.firstName != "" {
err := store.WriteEntry(ctx, sessionId, storedb.DATA_FIRST_NAME, []byte(tt.firstName))
require.NoError(t, err)
}
if tt.familyName != "" {
err := store.WriteEntry(ctx, sessionId, storedb.DATA_FAMILY_NAME, []byte(tt.familyName))
require.NoError(t, err)
}
if tt.publicKey != "" {
err := store.WriteEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY, []byte(tt.publicKey))
require.NoError(t, err)
}
aliasInput := fmt.Sprintf("%s%s", tt.firstName, tt.familyName)
// Mock service behavior
mockAccountService.On(
"RequestAlias",
tt.publicKey,
aliasInput,
).Return(tt.aliasResponse, tt.aliasError)
// Call the function under test
err := h.constructAccountAlias(ctx)
// Assertions
if tt.expectedError != nil {
assert.EqualError(t, err, tt.expectedError.Error())
} else {
assert.NoError(t, err)
if tt.expectedAlias != "" {
storedAlias, err := store.ReadEntry(ctx, sessionId, storedb.DATA_ACCOUNT_ALIAS)
require.NoError(t, err)
assert.Equal(t, tt.expectedAlias, string(storedAlias))
}
}
// Ensure mock expectations were met
mockAccountService.AssertExpectations(t)
})
}
}
func TestInsertProfileItems(t *testing.T) {
ctx, store := InitializeTestStore(t)
sessionId := "session123"

View File

@ -130,8 +130,6 @@ func (ls *LocalHandlerService) GetHandler(accountService remote.AccountService)
ls.DbRs.AddLocalFunc("clear_temporary_value", appHandlers.ClearTemporaryValue)
ls.DbRs.AddLocalFunc("reset_invalid_pin", appHandlers.ResetInvalidPIN)
ls.DbRs.AddLocalFunc("request_custom_alias", appHandlers.RequestCustomAlias)
ls.DbRs.AddLocalFunc("get_suggested_alias", appHandlers.GetSuggestedAlias)
ls.DbRs.AddLocalFunc("confirm_new_alias", appHandlers.ConfirmNewAlias)
ls.DbRs.AddLocalFunc("check_account_created", appHandlers.CheckAccountCreated)
ls.DbRs.AddLocalFunc("reset_api_call_failure", appHandlers.ResetApiCallFailure)
ls.DbRs.AddLocalFunc("swap_to_list", appHandlers.LoadSwapToList)

View File

@ -1,5 +1,3 @@
LOAD confirm_new_alias 0
RELOAD confirm_new_alias
MOUT back 0
MOUT quit 9
HALT

View File

@ -1,2 +0,0 @@
Your full alias will be: {{.get_suggested_alias}}
Please enter your PIN to confirm:

View File

@ -1,12 +0,0 @@
LOAD reset_invalid_pin 6
RELOAD reset_invalid_pin
LOAD get_suggested_alias 0
RELOAD get_suggested_alias
MAP get_suggested_alias
MOUT back 0
HALT
INCMP _ 0
RELOAD authorize_account
CATCH incorrect_pin flag_incorrect_pin 1
CATCH invalid_pin flag_invalid_pin 1
CATCH update_alias flag_allow_update 1

View File

@ -1,2 +0,0 @@
Lakabu yako kamili itakuwa: {{.get_suggested_alias}}
Tafadhali weka PIN yako ili kuthibitisha:

View File

@ -0,0 +1 @@
Home

View File

@ -0,0 +1 @@
Mwanzo

View File

@ -1,3 +1,7 @@
LOAD reset_account_authorized 0
LOAD reset_incorrect_pin 0
CATCH incorrect_pin flag_incorrect_pin 1
CATCH pin_entry flag_account_authorized 0
LOAD get_current_profile_info 0
MAP get_current_profile_info
MOUT back 0
@ -5,5 +9,7 @@ HALT
INCMP _ 0
LOAD request_custom_alias 0
RELOAD request_custom_alias
MAP request_custom_alias
CATCH unavailable_alias flag_alias_unavailable 1
CATCH api_failure flag_api_call_error 1
INCMP confirm_new_alias *
INCMP alias_updated *

View File

@ -34,4 +34,4 @@ flag,flag_alias_set,40,this is set when an account alias has been assigned to a
flag,flag_account_pin_reset,41,this is set on an account when an admin triggers a PIN reset for themflag,flag_incorrect_pool,39,this is set when the user selects an invalid pool
flag,flag_incorrect_pool,42,this is set when the user selects an invalid pool
flag,flag_low_swap_amount,43,this is set when the swap max limit is less than 0.1
flag,flag_alias_unavailable,44,this is set when the preferred alias is not available

Can't render this file because it has a wrong number of fields in line 34.

View File

@ -0,0 +1 @@
The alias {{.request_custom_alias}} isn't available

View File

@ -0,0 +1,8 @@
MAP request_custom_alias
MOUT back 0
MOUT home 9
MOUT quit 99
HALT
INCMP _ 0
INCMP ^ 9
INCMP quit 99

View File

@ -0,0 +1 @@
Lakabu ulilochagua {{.request_custom_alias}} halipatikani

View File

@ -3,6 +3,7 @@ package store
import (
"context"
"errors"
"fmt"
"math/big"
"reflect"
"strconv"
@ -20,6 +21,27 @@ type TransactionData struct {
ActiveAddress string
}
// TruncateDecimalString safely truncates the input amount to the specified decimal places
func TruncateDecimalString(input string, decimalPlaces int) (string, error) {
num, ok := new(big.Float).SetString(input)
if !ok {
return "", fmt.Errorf("invalid input")
}
// Multiply by 10^decimalPlaces
scale := new(big.Float).SetInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(decimalPlaces)), nil))
scaled := new(big.Float).Mul(num, scale)
// Truncate by converting to int (chops off decimals)
intPart, _ := scaled.Int(nil)
// Divide back to get truncated float
truncated := new(big.Float).Quo(new(big.Float).SetInt(intPart), scale)
// Format with fixed decimals
return truncated.Text('f', decimalPlaces), nil
}
func ParseAndScaleAmount(storedAmount, activeDecimal string) (string, error) {
// Parse token decimal
tokenDecimal, err := strconv.Atoi(activeDecimal)
@ -38,11 +60,8 @@ func ParseAndScaleAmount(storedAmount, activeDecimal string) (string, error) {
multiplier := new(big.Float).SetInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(tokenDecimal)), nil))
finalAmount := new(big.Float).Mul(amount, multiplier)
// Convert finalAmount to a string
finalAmountStr := new(big.Int)
finalAmount.Int(finalAmountStr)
return finalAmountStr.String(), nil
// Return finalAmount as a string with 0 decimal places (rounded)
return finalAmount.Text('f', 0), nil
}
func ReadTransactionData(ctx context.Context, store DataStore, sessionId string) (TransactionData, error) {

View File

@ -7,6 +7,109 @@ import (
"github.com/alecthomas/assert/v2"
)
func TestTruncateDecimalString(t *testing.T) {
tests := []struct {
name string
input string
decimalPlaces int
want string
expectError bool
}{
{
name: "whole number",
input: "4",
decimalPlaces: 2,
want: "4.00",
expectError: false,
},
{
name: "single decimal",
input: "4.1",
decimalPlaces: 2,
want: "4.10",
expectError: false,
},
{
name: "one decimal place",
input: "4.19",
decimalPlaces: 1,
want: "4.1",
expectError: false,
},
{
name: "truncates to 2 dp",
input: "0.149",
decimalPlaces: 2,
want: "0.14",
expectError: false,
},
{
name: "does not round",
input: "1.8599999999",
decimalPlaces: 2,
want: "1.85",
expectError: false,
},
{
name: "high precision input",
input: "123.456789",
decimalPlaces: 4,
want: "123.4567",
expectError: false,
},
{
name: "zero",
input: "0",
decimalPlaces: 2,
want: "0.00",
expectError: false,
},
{
name: "invalid input string",
input: "abc",
decimalPlaces: 2,
want: "",
expectError: true,
},
{
name: "edge rounding case",
input: "4.99999999",
decimalPlaces: 2,
want: "4.99",
expectError: false,
},
{
name: "small value",
input: "0.0001",
decimalPlaces: 2,
want: "0.00",
expectError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := TruncateDecimalString(tt.input, tt.decimalPlaces)
if tt.expectError {
if err == nil {
t.Errorf("TruncateDecimalString(%q, %d) expected error, got nil", tt.input, tt.decimalPlaces)
}
return
}
if err != nil {
t.Errorf("TruncateDecimalString(%q, %d) unexpected error: %v", tt.input, tt.decimalPlaces, err)
return
}
if got != tt.want {
t.Errorf("TruncateDecimalString(%q, %d) = %q, want %q", tt.input, tt.decimalPlaces, got, tt.want)
}
})
}
}
func TestParseAndScaleAmount(t *testing.T) {
tests := []struct {
name string
@ -64,6 +167,20 @@ func TestParseAndScaleAmount(t *testing.T) {
want: "0",
expectError: false,
},
{
name: "high decimals",
amount: "1.85",
decimals: "18",
want: "1850000000000000000",
expectError: false,
},
{
name: "6 d.p",
amount: "2.32",
decimals: "6",
want: "2320000",
expectError: false,
},
}
for _, tt := range tests {

View File

@ -182,3 +182,30 @@ func UpdateVoucherData(ctx context.Context, store DataStore, sessionId string, d
return nil
}
// FormatVoucherList combines the voucher symbols with their balances (SRF 0.11)
func FormatVoucherList(ctx context.Context, symbolsData, balancesData string) []string {
symbols := strings.Split(symbolsData, "\n")
balances := strings.Split(balancesData, "\n")
var combined []string
for i := 0; i < len(symbols) && i < len(balances); i++ {
symbolParts := strings.SplitN(symbols[i], ":", 2)
balanceParts := strings.SplitN(balances[i], ":", 2)
if len(symbolParts) == 2 && len(balanceParts) == 2 {
index := strings.TrimSpace(symbolParts[0])
symbol := strings.TrimSpace(symbolParts[1])
rawBalance := strings.TrimSpace(balanceParts[1])
formattedBalance, err := TruncateDecimalString(rawBalance, 2)
if err != nil {
logg.ErrorCtxf(ctx, "failed to format balance", "balance", rawBalance, "error", err)
formattedBalance = rawBalance
}
combined = append(combined, fmt.Sprintf("%s: %s %s", index, symbol, formattedBalance))
}
}
return combined
}