Use numeric prefixes
This commit is contained in:
20
common/db.go
20
common/db.go
@@ -32,7 +32,18 @@ const (
|
||||
DATA_PUBLIC_KEY_REVERSE
|
||||
DATA_ACTIVE_DECIMAL
|
||||
DATA_ACTIVE_ADDRESS
|
||||
DATA_TRANSACTIONS
|
||||
DATA_PREFIX_SYMBOLS
|
||||
DATA_PREFIX_BALANCES
|
||||
DATA_PREFIX_DECIMALS
|
||||
DATA_PREFIX_ADDRESSES
|
||||
DATA_PREFIX_TX_SENDERS
|
||||
DATA_PREFIX_TX_RECIPIENTS
|
||||
DATA_PREFIX_TX_VALUES
|
||||
DATA_PREFIX_TX_ADDRESSES
|
||||
DATA_PREFIX_TX_HASHES
|
||||
DATA_PREFIX_TX_DATES
|
||||
DATA_PREFIX_TX_SYMBOLS
|
||||
DATA_PREFIX_TX_DECIMALS
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -69,3 +80,10 @@ func StringToDataTyp(str string) (DataTyp, error) {
|
||||
return 0, errors.New("invalid DataTyp string")
|
||||
}
|
||||
}
|
||||
|
||||
// Convert DataTyp to []byte
|
||||
func (d DataTyp) ToBytes() []byte {
|
||||
bytes := make([]byte, 2)
|
||||
binary.BigEndian.PutUint16(bytes, uint16(d))
|
||||
return bytes
|
||||
}
|
||||
|
||||
@@ -57,11 +57,11 @@ func ProcessTransfers(transfers []dataserviceapi.Last10TxResponse) TransferMetad
|
||||
// GetTransferData retrieves and matches transfer data
|
||||
// returns a formatted string of the full transaction/statement
|
||||
func GetTransferData(ctx context.Context, db storage.PrefixDb, publicKey string, index int) (string, error) {
|
||||
keys := []string{"txfrom", "txto", "txval", "txaddr", "txhash", "txdate", "txsym"}
|
||||
data := make(map[string]string)
|
||||
keys := []DataTyp{DATA_PREFIX_TX_SENDERS, DATA_PREFIX_TX_RECIPIENTS, DATA_PREFIX_TX_VALUES, DATA_PREFIX_TX_ADDRESSES, DATA_PREFIX_TX_HASHES, DATA_PREFIX_TX_DATES, DATA_PREFIX_TX_SYMBOLS}
|
||||
data := make(map[DataTyp]string)
|
||||
|
||||
for _, key := range keys {
|
||||
value, err := db.Get(ctx, []byte(key))
|
||||
value, err := db.Get(ctx, key.ToBytes())
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to get %s: %v", key, err)
|
||||
}
|
||||
@@ -69,13 +69,13 @@ func GetTransferData(ctx context.Context, db storage.PrefixDb, publicKey string,
|
||||
}
|
||||
|
||||
// Split the data
|
||||
senders := strings.Split(string(data["txfrom"]), "\n")
|
||||
recipients := strings.Split(string(data["txto"]), "\n")
|
||||
values := strings.Split(string(data["txval"]), "\n")
|
||||
addresses := strings.Split(string(data["txaddr"]), "\n")
|
||||
hashes := strings.Split(string(data["txhash"]), "\n")
|
||||
dates := strings.Split(string(data["txdate"]), "\n")
|
||||
syms := strings.Split(string(data["txsym"]), "\n")
|
||||
senders := strings.Split(string(data[DATA_PREFIX_TX_SENDERS]), "\n")
|
||||
recipients := strings.Split(string(data[DATA_PREFIX_TX_RECIPIENTS]), "\n")
|
||||
values := strings.Split(string(data[DATA_PREFIX_TX_VALUES]), "\n")
|
||||
addresses := strings.Split(string(data[DATA_PREFIX_TX_ADDRESSES]), "\n")
|
||||
hashes := strings.Split(string(data[DATA_PREFIX_TX_HASHES]), "\n")
|
||||
dates := strings.Split(string(data[DATA_PREFIX_TX_DATES]), "\n")
|
||||
syms := strings.Split(string(data[DATA_PREFIX_TX_SYMBOLS]), "\n")
|
||||
|
||||
// Check if index is within range
|
||||
if index < 1 || index > len(senders) {
|
||||
|
||||
@@ -64,22 +64,24 @@ func ScaleDownBalance(balance, decimals string) string {
|
||||
|
||||
// GetVoucherData retrieves and matches voucher data
|
||||
func GetVoucherData(ctx context.Context, db storage.PrefixDb, input string) (*dataserviceapi.TokenHoldings, error) {
|
||||
keys := []string{"sym", "bal", "deci", "addr"}
|
||||
data := make(map[string]string)
|
||||
keys := []DataTyp{DATA_PREFIX_SYMBOLS, DATA_PREFIX_BALANCES, DATA_PREFIX_DECIMALS, DATA_PREFIX_ADDRESSES}
|
||||
data := make(map[DataTyp]string)
|
||||
|
||||
for _, key := range keys {
|
||||
value, err := db.Get(ctx, []byte(key))
|
||||
value, err := db.Get(ctx, key.ToBytes())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get %s: %v", key, err)
|
||||
fmt.Printf("failed to get %v: %v\n", key, err)
|
||||
continue
|
||||
}
|
||||
data[key] = string(value)
|
||||
}
|
||||
|
||||
symbol, balance, decimal, address := MatchVoucher(input,
|
||||
data["sym"],
|
||||
data["bal"],
|
||||
data["deci"],
|
||||
data["addr"])
|
||||
data[DATA_PREFIX_SYMBOLS],
|
||||
data[DATA_PREFIX_BALANCES],
|
||||
data[DATA_PREFIX_DECIMALS],
|
||||
data[DATA_PREFIX_ADDRESSES],
|
||||
)
|
||||
|
||||
if symbol == "" {
|
||||
return nil, nil
|
||||
|
||||
Reference in New Issue
Block a user