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"
@ -389,11 +390,11 @@ func (h *Handlers) VerifyYob(ctx context.Context, sym string, input []byte) (res
res := resource.Result{} res := resource.Result{}
date := string(input) date := string(input)
_, err := strconv.Atoi(date) _, err := strconv.Atoi(date)
if err != nil { if err != nil {
// If conversion fails, input is not numeric // If conversion fails, input is not numeric
res.FlagSet = append(res.FlagSet, models.USERFLAG_INCORRECTDATEFORMAT) res.FlagSet = append(res.FlagSet, models.USERFLAG_INCORRECTDATEFORMAT)
return res, nil return res, nil
} }
if len(date) == 4 { if len(date) == 4 {
res.FlagReset = append(res.FlagReset, models.USERFLAG_INCORRECTDATEFORMAT) res.FlagReset = append(res.FlagReset, models.USERFLAG_INCORRECTDATEFORMAT)
@ -515,59 +516,62 @@ func (h *Handlers) MaxAmount(ctx context.Context, sym string, input []byte) (res
} }
func (h *Handlers) ValidateAmount(ctx context.Context, sym string, input []byte) (resource.Result, error) { func (h *Handlers) ValidateAmount(ctx context.Context, sym string, input []byte) (resource.Result, error) {
res := resource.Result{} res := resource.Result{}
amountStr := string(input) amountStr := string(input)
accountData, err := h.accountFileHandler.ReadAccountData() accountData, err := h.accountFileHandler.ReadAccountData()
if err != nil { if err != nil {
return res, err return res, err
} }
balanceStr, err := server.CheckBalance(accountData["PublicKey"]) balanceStr, err := server.CheckBalance(accountData["PublicKey"])
if err != nil { if err != nil {
return res, err return res, err
} }
res.Content = balanceStr res.Content = balanceStr
// Parse the balance // Parse the balance
balanceParts := strings.Split(balanceStr, " ") balanceParts := strings.Split(balanceStr, " ")
if len(balanceParts) != 2 { if len(balanceParts) != 2 {
return res, fmt.Errorf("unexpected balance format: %s", balanceStr) return res, fmt.Errorf("unexpected balance format: %s", balanceStr)
} }
balanceValue, err := strconv.ParseFloat(balanceParts[0], 64) balanceValue, err := strconv.ParseFloat(balanceParts[0], 64)
if err != nil { if err != nil {
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 err != nil { if len(matches) < 2 {
res.FlagSet = append(res.FlagSet, models.USERFLAG_INVALID_AMOUNT) res.FlagSet = append(res.FlagSet, models.USERFLAG_INVALID_AMOUNT)
res.Content = amountStr res.Content = amountStr
return res, nil return res, nil
} }
if inputAmount > balanceValue { inputAmount, err := strconv.ParseFloat(matches[1], 64)
res.FlagSet = append(res.FlagSet, models.USERFLAG_INVALID_AMOUNT) if err != nil {
res.Content = amountStr res.FlagSet = append(res.FlagSet, models.USERFLAG_INVALID_AMOUNT)
return res, nil res.Content = amountStr
} return res, nil
}
res.Content = amountStr if inputAmount > balanceValue {
accountData["Amount"] = amountStr res.FlagSet = append(res.FlagSet, models.USERFLAG_INVALID_AMOUNT)
res.Content = amountStr
return res, nil
}
err = h.accountFileHandler.WriteAccountData(accountData) res.Content = fmt.Sprintf("%.3f", inputAmount) // Format to 3 decimal places
if err != nil { accountData["Amount"] = res.Content
return res, err
}
return res, nil err = h.accountFileHandler.WriteAccountData(accountData)
} if err != nil {
return res, err
}
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{}