Merge remote-tracking branch 'refs/remotes/origin/wip-account-creation' into wip-account-creation

This commit is contained in:
Carlosokumu 2024-08-27 23:12:52 +03:00
commit 37ebc411b5
Signed by untrusted user: carlos
GPG Key ID: 7BD6BC8160A5C953

View File

@ -4,7 +4,9 @@ import (
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
"regexp"
"strconv" "strconv"
"strings"
"git.defalsify.org/vise.git/engine" "git.defalsify.org/vise.git/engine"
"git.defalsify.org/vise.git/lang" "git.defalsify.org/vise.git/lang"
@ -498,33 +500,70 @@ func (h *Handlers) ResetTransactionAmount(ctx context.Context, sym string, input
func (h *Handlers) MaxAmount(ctx context.Context, sym string, input []byte) (resource.Result, error) { func (h *Handlers) MaxAmount(ctx context.Context, sym string, input []byte) (resource.Result, error) {
res := resource.Result{} res := resource.Result{}
// mimic a max amount accountData, err := h.accountFileHandler.ReadAccountData()
res.Content = "10.00" if err != nil {
return res, err
}
balance, err := server.CheckBalance(accountData["PublicKey"])
if err != nil {
return res, nil
}
res.Content = balance
return res, nil return res, nil
} }
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{}
amount := 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
} }
if amount != "0" { balanceStr, err := server.CheckBalance(accountData["PublicKey"])
// mimic invalid amount if err != nil {
if amount == "00" { return res, err
res.FlagSet = append(res.FlagSet, models.USERFLAG_INVALID_AMOUNT) }
res.Content = amount res.Content = balanceStr
// Parse the balance
balanceParts := strings.Split(balanceStr, " ")
if len(balanceParts) != 2 {
return res, fmt.Errorf("unexpected balance format: %s", balanceStr)
}
balanceValue, err := strconv.ParseFloat(balanceParts[0], 64)
if err != nil {
return res, fmt.Errorf("failed to parse balance: %v", err)
}
// Extract numeric part from input
re := regexp.MustCompile(`^(\d+(\.\d+)?)\s*(?:CELO)?$`)
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 return res, nil
} }
res.Content = amount inputAmount, err := strconv.ParseFloat(matches[1], 64)
if err != nil {
res.FlagSet = append(res.FlagSet, models.USERFLAG_INVALID_AMOUNT)
res.Content = amountStr
return res, nil
}
accountData["Amount"] = amount if inputAmount > balanceValue {
res.FlagSet = append(res.FlagSet, models.USERFLAG_INVALID_AMOUNT)
res.Content = amountStr
return res, nil
}
res.Content = fmt.Sprintf("%.3f", inputAmount) // Format to 3 decimal places
accountData["Amount"] = res.Content
err = h.accountFileHandler.WriteAccountData(accountData) err = h.accountFileHandler.WriteAccountData(accountData)
if err != nil { if err != nil {
@ -533,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{}