Compare commits

...

17 Commits

Author SHA1 Message Date
fc2ca0f546 Merge pull request 'send with swap' (#102) from send-with-swap into master
Some checks failed
release / docker (push) Has been cancelled
Reviewed-on: #102
2025-11-12 08:54:05 +01:00
724bc1bcf3 Merge branch 'master' into send-with-swap
Some checks failed
release / docker (push) Has been cancelled
2025-11-05 15:32:35 +03:00
6e8c0fbcb3 Merge pull request 'Fix get_pools being skipped' (#108) from pool-debug into master
Some checks failed
release / docker (push) Has been cancelled
Reviewed-on: #108
2025-11-05 12:12:14 +01:00
9f8cf95e0f
remove print debug statements 2025-11-05 11:20:55 +03:00
97be6e943c
add a reload for the get_pools 2025-11-05 11:16:52 +03:00
ba93bd9152
add debug logs for the pool data entry 2025-11-05 11:03:45 +03:00
5fac27d00e
add debug logs for the pool keys 2025-11-04 15:27:15 +03:00
348185ef96
use l.Get for the language change
Some checks failed
release / docker (push) Has been cancelled
2025-10-30 12:36:11 +03:00
f51f577e2a
added the swahili translations 2025-10-30 12:35:46 +03:00
582f349be3
handle normal send transactions based on the sym 2025-10-30 11:12:16 +03:00
8ce17a8d1e
add a sym for the credit_max_amount 2025-10-30 11:11:39 +03:00
4092437d21
add a node for the credit-send amount 2025-10-30 11:11:12 +03:00
37f4b60679
removed the credit-swap related code 2025-10-30 11:08:57 +03:00
878b5d0aa5
added the credit_send top-level menu 2025-10-30 11:08:18 +03:00
d2b934feda
use the same translation from the english menu 2025-10-30 10:07:05 +03:00
4c80606b56
properly handle formatting, preventing rounding errors for the case of 2.1 -> 2.09
Some checks failed
release / docker (push) Has been cancelled
2025-10-29 10:55:42 +03:00
38ab1ecdd1
added an edge-case test for precision 2025-10-29 10:53:11 +03:00
18 changed files with 89 additions and 30 deletions

View File

@ -335,8 +335,8 @@ func (h *MenuHandlers) MaxAmount(ctx context.Context, sym string, input []byte)
// Format the active balance amount to 2 decimal places
formattedBalance, _ := store.TruncateDecimalString(string(activeBal), 2)
// If normal transaction, return balance
if string(transactionType) == "normal" {
// If normal transaction, or if the sym is max_amount, return balance
if string(transactionType) == "normal" || sym == "max_amount" {
res.FlagReset = append(res.FlagReset, flag_swap_transaction)
res.Content = l.Get("Maximum amount: %s %s\nEnter amount:", formattedBalance, string(activeSym))
@ -789,7 +789,7 @@ func (h *MenuHandlers) TransactionSwapPreview(ctx context.Context, sym string, i
return res, err
}
res.Content = fmt.Sprintf(
res.Content = l.Get(
"%s will receive %s %s",
string(recipientPhoneNumber), qouteAmount, swapData.ActiveSwapToSym,
)

View File

@ -86,6 +86,7 @@ func (ls *LocalHandlerService) GetHandler(accountService remote.AccountService)
ls.DbRs.AddLocalFunc("transaction_reset", appHandlers.TransactionReset)
ls.DbRs.AddLocalFunc("invite_valid_recipient", appHandlers.InviteValidRecipient)
ls.DbRs.AddLocalFunc("max_amount", appHandlers.MaxAmount)
ls.DbRs.AddLocalFunc("credit_max_amount", appHandlers.MaxAmount)
ls.DbRs.AddLocalFunc("validate_amount", appHandlers.ValidateAmount)
ls.DbRs.AddLocalFunc("reset_transaction_amount", appHandlers.ResetTransactionAmount)
ls.DbRs.AddLocalFunc("get_recipient", appHandlers.GetRecipient)

View File

@ -4,9 +4,6 @@ RELOAD max_amount
MAP max_amount
MOUT back 0
HALT
LOAD clear_trans_type_flag 6
RELOAD clear_trans_type_flag
CATCH transaction_swap flag_swap_transaction 1
LOAD validate_amount 64
RELOAD validate_amount
CATCH api_failure flag_api_call_error 1

View File

@ -0,0 +1 @@
{{.credit_max_amount}}

View File

@ -0,0 +1,18 @@
LOAD reset_transaction_amount 10
LOAD credit_max_amount 160
RELOAD credit_max_amount
MAP credit_max_amount
MOUT back 0
HALT
LOAD clear_trans_type_flag 6
RELOAD clear_trans_type_flag
CATCH transaction_swap flag_swap_transaction 1
LOAD validate_amount 64
RELOAD validate_amount
CATCH api_failure flag_api_call_error 1
CATCH invalid_amount flag_invalid_amount 1
INCMP _ 0
LOAD get_recipient 0
LOAD get_sender 64
LOAD get_amount 32
INCMP transaction_pin *

View File

@ -0,0 +1 @@
{{.credit_max_amount}}

View File

@ -0,0 +1 @@
Enter recipient's phone number/address/alias:

View File

@ -0,0 +1,12 @@
LOAD transaction_reset 0
RELOAD transaction_reset
CATCH no_voucher flag_no_active_voucher 1
MOUT back 0
HALT
LOAD validate_recipient 50
RELOAD validate_recipient
CATCH api_failure flag_api_call_error 1
CATCH invalid_recipient flag_invalid_recipient 1
CATCH invite_recipient flag_invalid_recipient_with_invite 1
INCMP _ 0
INCMP credit_amount *

View File

@ -0,0 +1 @@
Credit-Send

View File

@ -0,0 +1 @@
Tuma-Mkopo

View File

@ -0,0 +1 @@
Weka nambari ya simu/anwani/lakabu:

View File

@ -50,5 +50,7 @@ msgid "Maximum amount: %s %s\nEnter amount:"
msgstr "Kiwango cha juu: %s %s\nWeka kiwango:"
msgid "Credit Available: %s %s\n(You can swap up to %s %s -> %s %s).\nEnter %s amount:"
msgstr "Kiwango kinachopatikana: %s %s\n(Unaweza kubadilisha hadi %s %s -> %s %s)\nWeka %s kiwango:"
msgstr "Kiwango kinachopatikana: %s %s\n(Unaweza kubadilisha hadi %s %s -> %s %s)\nWeka kiwango cha %s:"
msgid "%s will receive %s %s"
msgstr "%s atapokea %s %s"

View File

@ -7,18 +7,20 @@ LOAD check_balance 128
RELOAD check_balance
MAP check_balance
MOUT send 1
MOUT swap 2
MOUT vouchers 3
MOUT select_pool 4
MOUT account 5
MOUT help 6
MOUT credit_send 2
MOUT swap 3
MOUT vouchers 4
MOUT select_pool 5
MOUT account 6
MOUT help 7
MOUT quit 9
HALT
INCMP send 1
INCMP swap_to_list 2
INCMP my_vouchers 3
INCMP select_pool 4
INCMP my_account 5
INCMP help 6
INCMP credit_send 2
INCMP swap_to_list 3
INCMP my_vouchers 4
INCMP select_pool 5
INCMP my_account 6
INCMP help 7
INCMP quit 9
INCMP . *

View File

@ -1,5 +1,6 @@
CATCH no_voucher flag_no_active_voucher 1
LOAD get_pools 0
RELOAD get_pools
MAP get_pools
LOAD get_default_pool 20
RELOAD get_default_pool

View File

@ -1 +1 @@
Weka nambari ya simu:
Weka nambari ya simu/Anwani/Lakabu:

View File

@ -0,0 +1,3 @@
{{.transaction_swap_preview}}
Tafadhali weka PIN yako kudhibitisha:

View File

@ -7,6 +7,7 @@ import (
"math/big"
"reflect"
"strconv"
"strings"
storedb "git.grassecon.net/grassrootseconomics/sarafu-vise/store/db"
)
@ -21,25 +22,34 @@ type TransactionData struct {
ActiveAddress string
}
// TruncateDecimalString safely truncates the input amount to the specified decimal places
// TruncateDecimalString safely truncates (not rounds) a number string to the specified decimal places
func TruncateDecimalString(input string, decimalPlaces int) (string, error) {
num, ok := new(big.Float).SetString(input)
if !ok {
if _, err := strconv.ParseFloat(input, 64); err != nil {
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)
// Split input into integer and fractional parts
parts := strings.SplitN(input, ".", 2)
intPart := parts[0]
var fracPart string
// Truncate by converting to int (chops off decimals)
intPart, _ := scaled.Int(nil)
if len(parts) == 2 {
fracPart = parts[1]
}
// Divide back to get truncated float
truncated := new(big.Float).Quo(new(big.Float).SetInt(intPart), scale)
// Truncate or pad fractional part
if len(fracPart) > decimalPlaces {
fracPart = fracPart[:decimalPlaces]
} else {
fracPart = fracPart + strings.Repeat("0", decimalPlaces-len(fracPart))
}
// Format with fixed decimals
return truncated.Text('f', decimalPlaces), nil
// Handle zero decimal places
if decimalPlaces == 0 {
return intPart, nil
}
return fmt.Sprintf("%s.%s", intPart, fracPart), nil
}
func ParseAndScaleAmount(storedAmount, activeDecimal string) (string, error) {

View File

@ -22,6 +22,13 @@ func TestTruncateDecimalString(t *testing.T) {
want: "4.00",
expectError: false,
},
{
name: "precision test",
input: "2.1",
decimalPlaces: 2,
want: "2.10",
expectError: false,
},
{
name: "single decimal",
input: "4.1",