Allow users to include 'CELO' in the amount

This commit is contained in:
Alfred Kamanda 2024-08-27 16:16:15 +03:00
parent 2b10f6023f
commit 633d56b0ad
Signed by: Alfred-mk
GPG Key ID: 7EA3D01708908703

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
"regexp"
"strconv" "strconv"
"strings" "strings"
@ -539,9 +540,16 @@ func (h *Handlers) ValidateAmount(ctx context.Context, sym string, input []byte)
return res, fmt.Errorf("failed to parse balance: %v", err) return res, fmt.Errorf("failed to parse balance: %v", err)
} }
// Parse the input amount // Extract numeric part from input
if amountStr != "0" { re := regexp.MustCompile(`^(\d+(\.\d+)?)\s*(?:CELO)?$`)
inputAmount, err := strconv.ParseFloat(amountStr, 64) matches := re.FindStringSubmatch(strings.TrimSpace(amountStr))
if len(matches) < 2 {
res.FlagSet = append(res.FlagSet, models.USERFLAG_INVALID_AMOUNT)
res.Content = amountStr
return res, nil
}
inputAmount, err := strconv.ParseFloat(matches[1], 64)
if err != nil { if err != nil {
res.FlagSet = append(res.FlagSet, models.USERFLAG_INVALID_AMOUNT) res.FlagSet = append(res.FlagSet, models.USERFLAG_INVALID_AMOUNT)
res.Content = amountStr res.Content = amountStr
@ -554,8 +562,8 @@ func (h *Handlers) ValidateAmount(ctx context.Context, sym string, input []byte)
return res, nil return res, nil
} }
res.Content = amountStr res.Content = fmt.Sprintf("%.3f", inputAmount) // Format to 3 decimal places
accountData["Amount"] = amountStr accountData["Amount"] = res.Content
err = h.accountFileHandler.WriteAccountData(accountData) err = h.accountFileHandler.WriteAccountData(accountData)
if err != nil { if err != nil {
@ -564,10 +572,6 @@ func (h *Handlers) ValidateAmount(ctx context.Context, sym string, input []byte)
return res, nil return res, nil
} }
return res, nil
}
func (h *Handlers) GetRecipient(ctx context.Context, sym string, input []byte) (resource.Result, error) { func (h *Handlers) GetRecipient(ctx context.Context, sym string, input []byte) (resource.Result, error) {
res := resource.Result{} res := resource.Result{}