2024-11-14 19:21:04 +01:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-11-15 07:20:19 +01:00
|
|
|
"errors"
|
2024-11-14 19:21:04 +01:00
|
|
|
"math/big"
|
2024-11-15 07:20:19 +01:00
|
|
|
"reflect"
|
2024-11-14 19:21:04 +01:00
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2024-11-15 07:20:19 +01:00
|
|
|
type TransactionData struct {
|
|
|
|
TemporaryValue string
|
|
|
|
ActiveSym string
|
|
|
|
Amount string
|
|
|
|
PublicKey string
|
|
|
|
Recipient string
|
|
|
|
ActiveDecimal string
|
|
|
|
ActiveAddress string
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseAndScaleAmount(storedAmount, activeDecimal string) (string, error) {
|
2024-11-14 19:21:04 +01:00
|
|
|
// Parse token decimal
|
2024-11-15 07:20:19 +01:00
|
|
|
tokenDecimal, err := strconv.Atoi(activeDecimal)
|
2024-11-14 19:21:04 +01:00
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse amount
|
2024-11-15 07:20:19 +01:00
|
|
|
amount, _, err := big.ParseFloat(storedAmount, 10, 0, big.ToZero)
|
2024-11-14 19:21:04 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scale the amount
|
|
|
|
multiplier := new(big.Float).SetInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(tokenDecimal)), nil))
|
|
|
|
finalAmount := new(big.Float).Mul(amount, multiplier)
|
|
|
|
|
|
|
|
// Convert finalAmount to a string
|
|
|
|
finalAmountStr := new(big.Int)
|
|
|
|
finalAmount.Int(finalAmountStr)
|
|
|
|
|
|
|
|
return finalAmountStr.String(), nil
|
|
|
|
}
|
|
|
|
|
2024-11-15 07:20:19 +01:00
|
|
|
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,
|
2024-11-14 19:21:04 +01:00
|
|
|
}
|
|
|
|
|
2024-11-15 07:20:19 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
value, err := readStringEntry(ctx, store, sessionId, key)
|
2024-11-14 19:21:04 +01:00
|
|
|
if err != nil {
|
2024-11-15 07:20:19 +01:00
|
|
|
return data, err
|
2024-11-14 19:21:04 +01:00
|
|
|
}
|
2024-11-15 07:20:19 +01:00
|
|
|
field.SetString(value)
|
2024-11-14 19:21:04 +01:00
|
|
|
}
|
2024-11-15 07:20:19 +01:00
|
|
|
|
2024-11-14 19:21:04 +01:00
|
|
|
return data, nil
|
|
|
|
}
|
2024-11-15 07:20:19 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|