Compare commits

..

No commits in common. "1ba90a8b780bf109c68aad4e31fbefb616787e95" and "b40ad782949f682e02eb90f6347e6c122476a8f1" have entirely different histories.

3 changed files with 7 additions and 36 deletions

View File

@ -3,7 +3,6 @@ package common
import ( import (
"context" "context"
"fmt" "fmt"
"math/big"
"strings" "strings"
"git.grassecon.net/urdt/ussd/internal/storage" "git.grassecon.net/urdt/ussd/internal/storage"
@ -25,11 +24,7 @@ func ProcessVouchers(holdings []dataserviceapi.TokenHoldings) VoucherMetadata {
for i, h := range holdings { for i, h := range holdings {
symbols = append(symbols, fmt.Sprintf("%d:%s", i+1, h.TokenSymbol)) symbols = append(symbols, fmt.Sprintf("%d:%s", i+1, h.TokenSymbol))
balances = append(balances, fmt.Sprintf("%d:%s", i+1, h.Balance))
// Scale down the balance
scaledBalance := ScaleDownBalance(h.Balance, h.TokenDecimals)
balances = append(balances, fmt.Sprintf("%d:%s", i+1, scaledBalance))
decimals = append(decimals, fmt.Sprintf("%d:%s", i+1, h.TokenDecimals)) decimals = append(decimals, fmt.Sprintf("%d:%s", i+1, h.TokenDecimals))
addresses = append(addresses, fmt.Sprintf("%d:%s", i+1, h.ContractAddress)) addresses = append(addresses, fmt.Sprintf("%d:%s", i+1, h.ContractAddress))
} }
@ -42,26 +37,6 @@ func ProcessVouchers(holdings []dataserviceapi.TokenHoldings) VoucherMetadata {
return data return data
} }
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)
}
// GetVoucherData retrieves and matches voucher data // GetVoucherData retrieves and matches voucher data
func GetVoucherData(ctx context.Context, db storage.PrefixDb, input string) (*dataserviceapi.TokenHoldings, error) { func GetVoucherData(ctx context.Context, db storage.PrefixDb, input string) (*dataserviceapi.TokenHoldings, error) {
keys := []string{"sym", "bal", "deci", "addr"} keys := []string{"sym", "bal", "deci", "addr"}
@ -100,7 +75,7 @@ func MatchVoucher(input, symbols, balances, decimals, addresses string) (symbol,
decList := strings.Split(decimals, "\n") decList := strings.Split(decimals, "\n")
addrList := strings.Split(addresses, "\n") addrList := strings.Split(addresses, "\n")
logg.Tracef("found", "symlist", symList, "syms", symbols, "input", input) logg.Tracef("found" , "symlist", symList, "syms", symbols, "input", input)
for i, sym := range symList { for i, sym := range symList {
parts := strings.SplitN(sym, ":", 2) parts := strings.SplitN(sym, ":", 2)

View File

@ -59,13 +59,13 @@ func TestMatchVoucher(t *testing.T) {
func TestProcessVouchers(t *testing.T) { func TestProcessVouchers(t *testing.T) {
holdings := []dataserviceapi.TokenHoldings{ holdings := []dataserviceapi.TokenHoldings{
{ContractAddress: "0xd4c288865Ce", TokenSymbol: "SRF", TokenDecimals: "6", Balance: "100000000"}, {ContractAddress: "0xd4c288865Ce", TokenSymbol: "SRF", TokenDecimals: "6", Balance: "100"},
{ContractAddress: "0x41c188d63Qa", TokenSymbol: "MILO", TokenDecimals: "4", Balance: "200000000"}, {ContractAddress: "0x41c188d63Qa", TokenSymbol: "MILO", TokenDecimals: "4", Balance: "200"},
} }
expectedResult := VoucherMetadata{ expectedResult := VoucherMetadata{
Symbols: "1:SRF\n2:MILO", Symbols: "1:SRF\n2:MILO",
Balances: "1:100\n2:20000", Balances: "1:100\n2:200",
Decimals: "1:6\n2:4", Decimals: "1:6\n2:4",
Addresses: "1:0xd4c288865Ce\n2:0x41c188d63Qa", Addresses: "1:0xd4c288865Ce\n2:0x41c188d63Qa",
} }

View File

@ -1468,9 +1468,6 @@ func (h *Handlers) SetDefaultVoucher(ctx context.Context, sym string, input []by
defaultDec := firstVoucher.TokenDecimals defaultDec := firstVoucher.TokenDecimals
defaultAddr := firstVoucher.ContractAddress defaultAddr := firstVoucher.ContractAddress
// Scale down the balance
scaledBalance := common.ScaleDownBalance(defaultBal, defaultDec)
// set the active symbol // set the active symbol
err = store.WriteEntry(ctx, sessionId, common.DATA_ACTIVE_SYM, []byte(defaultSym)) err = store.WriteEntry(ctx, sessionId, common.DATA_ACTIVE_SYM, []byte(defaultSym))
if err != nil { if err != nil {
@ -1478,9 +1475,9 @@ func (h *Handlers) SetDefaultVoucher(ctx context.Context, sym string, input []by
return res, err return res, err
} }
// set the active balance // set the active balance
err = store.WriteEntry(ctx, sessionId, common.DATA_ACTIVE_BAL, []byte(scaledBalance)) err = store.WriteEntry(ctx, sessionId, common.DATA_ACTIVE_BAL, []byte(defaultBal))
if err != nil { if err != nil {
logg.ErrorCtxf(ctx, "failed to write defaultBal entry with", "key", common.DATA_ACTIVE_BAL, "value", scaledBalance, "error", err) logg.ErrorCtxf(ctx, "failed to write defaultBal entry with", "key", common.DATA_ACTIVE_BAL, "value", defaultBal, "error", err)
return res, err return res, err
} }
// set the active decimals // set the active decimals
@ -1566,7 +1563,6 @@ func (h *Handlers) GetVoucherList(ctx context.Context, sym string, input []byte)
} }
// ViewVoucher retrieves the token holding and balance from the subprefixDB // ViewVoucher retrieves the token holding and balance from the subprefixDB
// and displays it to the user for them to select it
func (h *Handlers) ViewVoucher(ctx context.Context, sym string, input []byte) (resource.Result, error) { func (h *Handlers) ViewVoucher(ctx context.Context, sym string, input []byte) (resource.Result, error) {
var res resource.Result var res resource.Result
sessionId, ok := ctx.Value("SessionId").(string) sessionId, ok := ctx.Value("SessionId").(string)