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