forked from urdt/ussd
Use numeric prefixes
This commit is contained in:
parent
4ecfc9de38
commit
93c44861e0
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
|
||||
|
@ -1569,15 +1569,15 @@ func (h *Handlers) CheckVouchers(ctx context.Context, sym string, input []byte)
|
||||
data := common.ProcessVouchers(vouchersResp)
|
||||
|
||||
// Store all voucher data
|
||||
dataMap := map[string]string{
|
||||
"sym": data.Symbols,
|
||||
"bal": data.Balances,
|
||||
"deci": data.Decimals,
|
||||
"addr": data.Addresses,
|
||||
dataMap := map[common.DataTyp]string{
|
||||
common.DATA_PREFIX_SYMBOLS: data.Symbols,
|
||||
common.DATA_PREFIX_BALANCES: data.Balances,
|
||||
common.DATA_PREFIX_DECIMALS: data.Decimals,
|
||||
common.DATA_PREFIX_ADDRESSES: data.Addresses,
|
||||
}
|
||||
|
||||
for key, value := range dataMap {
|
||||
if err := h.prefixDb.Put(ctx, []byte(key), []byte(value)); err != nil {
|
||||
if err := h.prefixDb.Put(ctx, []byte(key.ToBytes()), []byte(value)); err != nil {
|
||||
return res, nil
|
||||
}
|
||||
}
|
||||
@ -1590,7 +1590,7 @@ func (h *Handlers) GetVoucherList(ctx context.Context, sym string, input []byte)
|
||||
var res resource.Result
|
||||
|
||||
// Read vouchers from the store
|
||||
voucherData, err := h.prefixDb.Get(ctx, []byte("sym"))
|
||||
voucherData, err := h.prefixDb.Get(ctx, common.DATA_PREFIX_SYMBOLS.ToBytes())
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "Failed to read the voucherData from prefixDb", "error", err)
|
||||
return res, err
|
||||
@ -1732,19 +1732,19 @@ func (h *Handlers) CheckTransactions(ctx context.Context, sym string, input []by
|
||||
data := common.ProcessTransfers(transactionsResp)
|
||||
|
||||
// Store all transaction data
|
||||
dataMap := map[string]string{
|
||||
"txfrom": data.Senders,
|
||||
"txto": data.Recipients,
|
||||
"txval": data.TransferValues,
|
||||
"txaddr": data.Addresses,
|
||||
"txhash": data.TxHashes,
|
||||
"txdate": data.Dates,
|
||||
"txsym": data.Symbols,
|
||||
"txdeci": data.Decimals,
|
||||
dataMap := map[common.DataTyp]string{
|
||||
common.DATA_PREFIX_TX_SENDERS: data.Senders,
|
||||
common.DATA_PREFIX_TX_RECIPIENTS: data.Recipients,
|
||||
common.DATA_PREFIX_TX_VALUES: data.TransferValues,
|
||||
common.DATA_PREFIX_TX_ADDRESSES: data.Addresses,
|
||||
common.DATA_PREFIX_TX_HASHES: data.TxHashes,
|
||||
common.DATA_PREFIX_TX_DATES: data.Dates,
|
||||
common.DATA_PREFIX_TX_SYMBOLS: data.Symbols,
|
||||
common.DATA_PREFIX_TX_DECIMALS: data.Decimals,
|
||||
}
|
||||
|
||||
for key, value := range dataMap {
|
||||
if err := h.prefixDb.Put(ctx, []byte(key), []byte(value)); err != nil {
|
||||
if err := h.prefixDb.Put(ctx, []byte(key.ToBytes()), []byte(value)); err != nil {
|
||||
logg.ErrorCtxf(ctx, "failed to write to prefixDb", "error", err)
|
||||
return res, err
|
||||
}
|
||||
@ -1770,22 +1770,22 @@ func (h *Handlers) GetTransactionsList(ctx context.Context, sym string, input []
|
||||
}
|
||||
|
||||
// Read transactions from the store and format them
|
||||
TransactionSenders, err := h.prefixDb.Get(ctx, []byte("txfrom"))
|
||||
TransactionSenders, err := h.prefixDb.Get(ctx, common.DATA_PREFIX_TX_SENDERS.ToBytes())
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "Failed to read the TransactionSenders from prefixDb", "error", err)
|
||||
return res, err
|
||||
}
|
||||
TransactionSyms, err := h.prefixDb.Get(ctx, []byte("txsym"))
|
||||
TransactionSyms, err := h.prefixDb.Get(ctx, common.DATA_PREFIX_TX_SYMBOLS.ToBytes())
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "Failed to read the TransactionSyms from prefixDb", "error", err)
|
||||
return res, err
|
||||
}
|
||||
TransactionValues, err := h.prefixDb.Get(ctx, []byte("txval"))
|
||||
TransactionValues, err := h.prefixDb.Get(ctx, common.DATA_PREFIX_TX_VALUES.ToBytes())
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "Failed to read the TransactionValues from prefixDb", "error", err)
|
||||
return res, err
|
||||
}
|
||||
TransactionDates, err := h.prefixDb.Get(ctx, []byte("txdate"))
|
||||
TransactionDates, err := h.prefixDb.Get(ctx, common.DATA_PREFIX_TX_DATES.ToBytes())
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "Failed to read the TransactionDates from prefixDb", "error", err)
|
||||
return res, err
|
||||
|
Loading…
Reference in New Issue
Block a user