2024-10-31 14:19:44 +01:00
|
|
|
package common
|
2024-10-29 22:52:23 +01:00
|
|
|
|
|
|
|
import (
|
2024-10-30 16:30:55 +01:00
|
|
|
"context"
|
2024-10-29 22:52:23 +01:00
|
|
|
"fmt"
|
2024-11-16 12:52:02 +01:00
|
|
|
"math/big"
|
2024-10-29 22:52:23 +01:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"git.grassecon.net/urdt/ussd/internal/storage"
|
2024-10-30 16:30:55 +01:00
|
|
|
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
2024-10-29 22:52:23 +01:00
|
|
|
)
|
|
|
|
|
2024-10-30 16:40:03 +01:00
|
|
|
// VoucherMetadata helps organize data fields
|
2024-10-29 22:52:23 +01:00
|
|
|
type VoucherMetadata struct {
|
2024-10-30 16:40:03 +01:00
|
|
|
Symbols string
|
|
|
|
Balances string
|
|
|
|
Decimals string
|
|
|
|
Addresses string
|
2024-10-29 22:52:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ProcessVouchers converts holdings into formatted strings
|
2024-10-30 16:30:55 +01:00
|
|
|
func ProcessVouchers(holdings []dataserviceapi.TokenHoldings) VoucherMetadata {
|
2024-10-29 22:52:23 +01:00
|
|
|
var data VoucherMetadata
|
|
|
|
var symbols, balances, decimals, addresses []string
|
|
|
|
|
|
|
|
for i, h := range holdings {
|
|
|
|
symbols = append(symbols, fmt.Sprintf("%d:%s", i+1, h.TokenSymbol))
|
2024-11-16 12:52:02 +01:00
|
|
|
|
|
|
|
// Scale down the balance
|
|
|
|
scaledBalance := ScaleDownBalance(h.Balance, h.TokenDecimals)
|
|
|
|
|
|
|
|
balances = append(balances, fmt.Sprintf("%d:%s", i+1, scaledBalance))
|
2024-10-29 22:52:23 +01:00
|
|
|
decimals = append(decimals, fmt.Sprintf("%d:%s", i+1, h.TokenDecimals))
|
|
|
|
addresses = append(addresses, fmt.Sprintf("%d:%s", i+1, h.ContractAddress))
|
|
|
|
}
|
|
|
|
|
2024-10-30 16:40:03 +01:00
|
|
|
data.Symbols = strings.Join(symbols, "\n")
|
|
|
|
data.Balances = strings.Join(balances, "\n")
|
|
|
|
data.Decimals = strings.Join(decimals, "\n")
|
|
|
|
data.Addresses = strings.Join(addresses, "\n")
|
2024-10-29 22:52:23 +01:00
|
|
|
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
2024-11-16 12:52:02 +01:00
|
|
|
func ScaleDownBalance(balance, decimals string) string {
|
|
|
|
// Convert balance and decimals to big.Float
|
|
|
|
bal := new(big.Float)
|
|
|
|
bal.SetString(balance)
|
|
|
|
|
|
|
|
dec, ok := new(big.Int).SetString(decimals, 10)
|
|
|
|
if !ok {
|
|
|
|
dec = big.NewInt(0) // Default to 0 decimals in case of conversion failure
|
|
|
|
}
|
|
|
|
|
|
|
|
divisor := new(big.Float).SetInt(new(big.Int).Exp(big.NewInt(10), dec, nil))
|
|
|
|
scaledBalance := new(big.Float).Quo(bal, divisor)
|
|
|
|
|
|
|
|
// Return the scaled balance without trailing decimals if it's an integer
|
|
|
|
if scaledBalance.IsInt() {
|
|
|
|
return scaledBalance.Text('f', 0)
|
|
|
|
}
|
|
|
|
return scaledBalance.Text('f', -1)
|
|
|
|
}
|
|
|
|
|
2024-10-29 22:52:23 +01:00
|
|
|
// GetVoucherData retrieves and matches voucher data
|
2024-10-30 16:30:55 +01:00
|
|
|
func GetVoucherData(ctx context.Context, db storage.PrefixDb, input string) (*dataserviceapi.TokenHoldings, error) {
|
2024-10-29 22:52:23 +01:00
|
|
|
keys := []string{"sym", "bal", "deci", "addr"}
|
|
|
|
data := make(map[string]string)
|
|
|
|
|
|
|
|
for _, key := range keys {
|
|
|
|
value, err := db.Get(ctx, []byte(key))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to get %s: %v", key, err)
|
|
|
|
}
|
|
|
|
data[key] = string(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
symbol, balance, decimal, address := MatchVoucher(input,
|
|
|
|
data["sym"],
|
|
|
|
data["bal"],
|
|
|
|
data["deci"],
|
|
|
|
data["addr"])
|
|
|
|
|
|
|
|
if symbol == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2024-10-30 16:30:55 +01:00
|
|
|
return &dataserviceapi.TokenHoldings{
|
|
|
|
TokenSymbol: string(symbol),
|
|
|
|
Balance: string(balance),
|
|
|
|
TokenDecimals: string(decimal),
|
|
|
|
ContractAddress: string(address),
|
2024-10-29 22:52:23 +01:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MatchVoucher finds the matching voucher symbol, balance, decimals and contract address based on the input.
|
|
|
|
func MatchVoucher(input, symbols, balances, decimals, addresses string) (symbol, balance, decimal, address string) {
|
|
|
|
symList := strings.Split(symbols, "\n")
|
|
|
|
balList := strings.Split(balances, "\n")
|
|
|
|
decList := strings.Split(decimals, "\n")
|
|
|
|
addrList := strings.Split(addresses, "\n")
|
|
|
|
|
2024-11-16 12:52:02 +01:00
|
|
|
logg.Tracef("found", "symlist", symList, "syms", symbols, "input", input)
|
2024-10-29 22:52:23 +01:00
|
|
|
for i, sym := range symList {
|
|
|
|
parts := strings.SplitN(sym, ":", 2)
|
2024-10-31 22:42:23 +01:00
|
|
|
|
2024-10-29 22:52:23 +01:00
|
|
|
if input == parts[0] || strings.EqualFold(input, parts[1]) {
|
|
|
|
symbol = parts[1]
|
|
|
|
if i < len(balList) {
|
|
|
|
balance = strings.SplitN(balList[i], ":", 2)[1]
|
|
|
|
}
|
|
|
|
if i < len(decList) {
|
|
|
|
decimal = strings.SplitN(decList[i], ":", 2)[1]
|
|
|
|
}
|
|
|
|
if i < len(addrList) {
|
|
|
|
address = strings.SplitN(addrList[i], ":", 2)[1]
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-31 14:19:44 +01:00
|
|
|
// StoreTemporaryVoucher saves voucher metadata as temporary entries in the DataStore.
|
|
|
|
func StoreTemporaryVoucher(ctx context.Context, store DataStore, sessionId string, data *dataserviceapi.TokenHoldings) error {
|
2024-10-31 22:42:23 +01:00
|
|
|
tempData := fmt.Sprintf("%s,%s,%s,%s", data.TokenSymbol, data.Balance, data.TokenDecimals, data.ContractAddress)
|
2024-10-29 22:52:23 +01:00
|
|
|
|
2024-10-31 22:42:23 +01:00
|
|
|
if err := store.WriteEntry(ctx, sessionId, DATA_TEMPORARY_VALUE, []byte(tempData)); err != nil {
|
|
|
|
return err
|
2024-10-29 22:52:23 +01:00
|
|
|
}
|
2024-10-31 22:42:23 +01:00
|
|
|
|
2024-10-29 22:52:23 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-31 14:19:44 +01:00
|
|
|
// GetTemporaryVoucherData retrieves temporary voucher metadata from the DataStore.
|
|
|
|
func GetTemporaryVoucherData(ctx context.Context, store DataStore, sessionId string) (*dataserviceapi.TokenHoldings, error) {
|
2024-10-31 22:42:23 +01:00
|
|
|
temp_data, err := store.ReadEntry(ctx, sessionId, DATA_TEMPORARY_VALUE)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2024-10-29 22:52:23 +01:00
|
|
|
}
|
|
|
|
|
2024-10-31 22:42:23 +01:00
|
|
|
values := strings.SplitN(string(temp_data), ",", 4)
|
2024-10-29 22:52:23 +01:00
|
|
|
|
2024-10-31 22:42:23 +01:00
|
|
|
data := &dataserviceapi.TokenHoldings{}
|
2024-10-29 22:52:23 +01:00
|
|
|
|
2024-10-31 22:42:23 +01:00
|
|
|
data.TokenSymbol = values[0]
|
|
|
|
data.Balance = values[1]
|
|
|
|
data.TokenDecimals = values[2]
|
|
|
|
data.ContractAddress = values[3]
|
2024-10-29 22:52:23 +01:00
|
|
|
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
2024-11-30 13:26:13 +01:00
|
|
|
// UpdateVoucherData updates the active voucher data in the DataStore.
|
2024-10-31 14:19:44 +01:00
|
|
|
func UpdateVoucherData(ctx context.Context, store DataStore, sessionId string, data *dataserviceapi.TokenHoldings) error {
|
2024-11-03 15:34:26 +01:00
|
|
|
logg.TraceCtxf(ctx, "dtal", "data", data)
|
2024-10-29 22:52:23 +01:00
|
|
|
// Active voucher data entries
|
2024-10-31 14:19:44 +01:00
|
|
|
activeEntries := map[DataTyp][]byte{
|
|
|
|
DATA_ACTIVE_SYM: []byte(data.TokenSymbol),
|
|
|
|
DATA_ACTIVE_BAL: []byte(data.Balance),
|
|
|
|
DATA_ACTIVE_DECIMAL: []byte(data.TokenDecimals),
|
|
|
|
DATA_ACTIVE_ADDRESS: []byte(data.ContractAddress),
|
2024-10-29 22:52:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write active data
|
|
|
|
for key, value := range activeEntries {
|
|
|
|
if err := store.WriteEntry(ctx, sessionId, key, value); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|