added the TransactionData struct to organiza the data
This commit is contained in:
parent
cb13b09291
commit
1a0b4deab3
@ -2,20 +2,32 @@ package common
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"math/big"
|
||||
"reflect"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func ParseAndScaleAmount(storedAmount, activeDecimal []byte) (string, error) {
|
||||
type TransactionData struct {
|
||||
TemporaryValue string
|
||||
ActiveSym string
|
||||
Amount string
|
||||
PublicKey string
|
||||
Recipient string
|
||||
ActiveDecimal string
|
||||
ActiveAddress string
|
||||
}
|
||||
|
||||
func ParseAndScaleAmount(storedAmount, activeDecimal string) (string, error) {
|
||||
// Parse token decimal
|
||||
tokenDecimal, err := strconv.Atoi(string(activeDecimal))
|
||||
tokenDecimal, err := strconv.Atoi(activeDecimal)
|
||||
if err != nil {
|
||||
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Parse amount
|
||||
amount, _, err := big.ParseFloat(string(storedAmount), 10, 0, big.ToZero)
|
||||
amount, _, err := big.ParseFloat(storedAmount, 10, 0, big.ToZero)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@ -31,24 +43,39 @@ func ParseAndScaleAmount(storedAmount, activeDecimal []byte) (string, error) {
|
||||
return finalAmountStr.String(), nil
|
||||
}
|
||||
|
||||
func ReadTransactionData(ctx context.Context, store DataStore, sessionId string) (map[DataTyp][]byte, error) {
|
||||
dataKeys := []DataTyp{
|
||||
DATA_TEMPORARY_VALUE,
|
||||
DATA_ACTIVE_SYM,
|
||||
DATA_AMOUNT,
|
||||
DATA_PUBLIC_KEY,
|
||||
DATA_RECIPIENT,
|
||||
DATA_ACTIVE_DECIMAL,
|
||||
DATA_ACTIVE_ADDRESS,
|
||||
func ReadTransactionData(ctx context.Context, store DataStore, sessionId string) (TransactionData, error) {
|
||||
data := TransactionData{}
|
||||
fieldToKey := map[string]DataTyp{
|
||||
"TemporaryValue": DATA_TEMPORARY_VALUE,
|
||||
"ActiveSym": DATA_ACTIVE_SYM,
|
||||
"Amount": DATA_AMOUNT,
|
||||
"PublicKey": DATA_PUBLIC_KEY,
|
||||
"Recipient": DATA_RECIPIENT,
|
||||
"ActiveDecimal": DATA_ACTIVE_DECIMAL,
|
||||
"ActiveAddress": DATA_ACTIVE_ADDRESS,
|
||||
}
|
||||
|
||||
data := make(map[DataTyp][]byte)
|
||||
for _, key := range dataKeys {
|
||||
value, err := store.ReadEntry(ctx, sessionId, key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
v := reflect.ValueOf(&data).Elem()
|
||||
for fieldName, key := range fieldToKey {
|
||||
field := v.FieldByName(fieldName)
|
||||
if !field.IsValid() || !field.CanSet() {
|
||||
return data, errors.New("invalid struct field: " + fieldName)
|
||||
}
|
||||
data[key] = value
|
||||
|
||||
value, err := readStringEntry(ctx, store, sessionId, key)
|
||||
if err != nil {
|
||||
return data, err
|
||||
}
|
||||
field.SetString(value)
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func readStringEntry(ctx context.Context, store DataStore, sessionId string, key DataTyp) (string, error) {
|
||||
entry, err := store.ReadEntry(ctx, sessionId, key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(entry), nil
|
||||
}
|
||||
|
@ -1224,13 +1224,13 @@ func (h *Handlers) InitiateTransaction(ctx context.Context, sym string, input []
|
||||
return res, err
|
||||
}
|
||||
|
||||
finalAmountStr, err := common.ParseAndScaleAmount(data[common.DATA_AMOUNT], data[common.DATA_ACTIVE_DECIMAL])
|
||||
finalAmountStr, err := common.ParseAndScaleAmount(data.Amount, data.ActiveDecimal)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
// Call TokenTransfer
|
||||
r, err := h.accountService.TokenTransfer(ctx, finalAmountStr, string(data[common.DATA_PUBLIC_KEY]), string(data[common.DATA_RECIPIENT]), string(data[common.DATA_ACTIVE_ADDRESS]))
|
||||
r, err := h.accountService.TokenTransfer(ctx, finalAmountStr, data.PublicKey, data.Recipient, data.ActiveAddress)
|
||||
if err != nil {
|
||||
flag_api_error, _ := h.flagManager.GetFlag("flag_api_call_error")
|
||||
res.FlagSet = append(res.FlagSet, flag_api_error)
|
||||
@ -1244,9 +1244,9 @@ func (h *Handlers) InitiateTransaction(ctx context.Context, sym string, input []
|
||||
|
||||
res.Content = l.Get(
|
||||
"Your request has been sent. %s will receive %s %s from %s.",
|
||||
string(data[common.DATA_TEMPORARY_VALUE]),
|
||||
string(data[common.DATA_AMOUNT]),
|
||||
string(data[common.DATA_ACTIVE_SYM]),
|
||||
data.TemporaryValue,
|
||||
data.Amount,
|
||||
data.ActiveSym,
|
||||
sessionId,
|
||||
)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user