This commit is contained in:
parent
686f119a9e
commit
43c4b64b42
@ -3,12 +3,10 @@ package application
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"sort"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"git.defalsify.org/vise.git/db"
|
"git.defalsify.org/vise.git/db"
|
||||||
"git.defalsify.org/vise.git/resource"
|
"git.defalsify.org/vise.git/resource"
|
||||||
"git.grassecon.net/grassrootseconomics/sarafu-vise/config"
|
|
||||||
"git.grassecon.net/grassrootseconomics/sarafu-vise/store"
|
"git.grassecon.net/grassrootseconomics/sarafu-vise/store"
|
||||||
storedb "git.grassecon.net/grassrootseconomics/sarafu-vise/store/db"
|
storedb "git.grassecon.net/grassrootseconomics/sarafu-vise/store/db"
|
||||||
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
||||||
@ -114,8 +112,6 @@ func (h *MenuHandlers) CalculateCreditAndDebt(ctx context.Context, sym string, i
|
|||||||
return res, fmt.Errorf("missing session")
|
return res, fmt.Errorf("missing session")
|
||||||
}
|
}
|
||||||
|
|
||||||
userStore := h.userdataStore
|
|
||||||
|
|
||||||
code := codeFromCtx(ctx)
|
code := codeFromCtx(ctx)
|
||||||
l := gotext.NewLocale(translationDir, code)
|
l := gotext.NewLocale(translationDir, code)
|
||||||
l.AddDomain("default")
|
l.AddDomain("default")
|
||||||
@ -129,7 +125,6 @@ func (h *MenuHandlers) CalculateCreditAndDebt(ctx context.Context, sym string, i
|
|||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// default flag reset
|
|
||||||
res.FlagReset = append(res.FlagReset, flag_api_call_error)
|
res.FlagReset = append(res.FlagReset, flag_api_call_error)
|
||||||
|
|
||||||
// Resolve active pool
|
// Resolve active pool
|
||||||
@ -138,7 +133,7 @@ func (h *MenuHandlers) CalculateCreditAndDebt(ctx context.Context, sym string, i
|
|||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch swappable vouchers
|
// Fetch swappable vouchers (pool view)
|
||||||
swappableVouchers, err := h.accountService.GetPoolSwappableFromVouchers(ctx, string(activePoolAddress), string(publicKey))
|
swappableVouchers, err := h.accountService.GetPoolSwappableFromVouchers(ctx, string(activePoolAddress), string(publicKey))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logg.ErrorCtxf(ctx, "failed on GetPoolSwappableFromVouchers", "error", err)
|
logg.ErrorCtxf(ctx, "failed on GetPoolSwappableFromVouchers", "error", err)
|
||||||
@ -147,91 +142,46 @@ func (h *MenuHandlers) CalculateCreditAndDebt(ctx context.Context, sym string, i
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(swappableVouchers) == 0 {
|
if len(swappableVouchers) == 0 {
|
||||||
|
res.Content = l.Get("Credit: %s KSH\nDebt: %s %s\n", "0", "0", string(activeSym))
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build stable voucher priority (lower index = higher priority)
|
// Fetch ALL wallet vouchers (voucher holdings view)
|
||||||
stablePriority := make(map[string]int)
|
allVouchers, err := h.accountService.FetchVouchers(ctx, string(publicKey))
|
||||||
stableAddresses := config.StableVoucherAddresses()
|
if err != nil {
|
||||||
for i, addr := range stableAddresses {
|
logg.ErrorCtxf(ctx, "failed on FetchVouchers", "error", err)
|
||||||
stablePriority[addr] = i
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
stable := make([]dataserviceapi.TokenHoldings, 0)
|
// CREDIT calculation
|
||||||
nonStable := make([]dataserviceapi.TokenHoldings, 0)
|
// Rule:
|
||||||
|
// 1. Swap quote of active voucher → first stable in pool from GetPoolSwappableFromVouchers
|
||||||
|
// 2. PLUS all stable balances from FetchVouchers
|
||||||
|
|
||||||
// Helper: order vouchers (stable first, priority-based)
|
scaledCredit := "0"
|
||||||
orderVouchers := func(vouchers []dataserviceapi.TokenHoldings) []dataserviceapi.TokenHoldings {
|
|
||||||
for _, v := range vouchers {
|
|
||||||
if isStableVoucher(v.TokenAddress) {
|
|
||||||
stable = append(stable, v)
|
|
||||||
} else {
|
|
||||||
nonStable = append(nonStable, v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
sort.SliceStable(stable, func(i, j int) bool {
|
// 1️. Find first stable voucher in POOL (for swap target)
|
||||||
ai := stablePriority[stable[i].TokenAddress]
|
var firstPoolStable *dataserviceapi.TokenHoldings
|
||||||
aj := stablePriority[stable[j].TokenAddress]
|
|
||||||
return ai < aj
|
|
||||||
})
|
|
||||||
|
|
||||||
return append(stable, nonStable...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove active voucher
|
|
||||||
filteredVouchers := make([]dataserviceapi.TokenHoldings, 0, len(swappableVouchers))
|
|
||||||
for _, v := range swappableVouchers {
|
|
||||||
if v.TokenSymbol != string(activeSym) {
|
|
||||||
filteredVouchers = append(filteredVouchers, v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Order remaining vouchers
|
|
||||||
orderedFilteredVouchers := orderVouchers(filteredVouchers)
|
|
||||||
|
|
||||||
// Process & store
|
|
||||||
data := store.ProcessVouchers(orderedFilteredVouchers)
|
|
||||||
|
|
||||||
dataMap := map[storedb.DataTyp]string{
|
|
||||||
storedb.DATA_VOUCHER_SYMBOLS: data.Symbols,
|
|
||||||
storedb.DATA_VOUCHER_BALANCES: data.Balances,
|
|
||||||
storedb.DATA_VOUCHER_DECIMALS: data.Decimals,
|
|
||||||
storedb.DATA_VOUCHER_ADDRESSES: data.Addresses,
|
|
||||||
}
|
|
||||||
|
|
||||||
for key, value := range dataMap {
|
|
||||||
if err := userStore.WriteEntry(ctx, sessionId, key, []byte(value)); err != nil {
|
|
||||||
logg.ErrorCtxf(ctx, "Failed to write data entry for sessionId: %s", sessionId, "key", key, "error", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Credit calculation: How much Active Token that can be swapped for a stable coin
|
|
||||||
// that's on the pool + any stables sendable to Pretium (in KSH value)
|
|
||||||
|
|
||||||
// Find first stable voucher from swappableVouchers
|
|
||||||
var firstStable *dataserviceapi.TokenHoldings
|
|
||||||
for i := range swappableVouchers {
|
for i := range swappableVouchers {
|
||||||
if isStableVoucher(swappableVouchers[i].TokenAddress) {
|
if isStableVoucher(swappableVouchers[i].TokenAddress) {
|
||||||
firstStable = &swappableVouchers[i]
|
firstPoolStable = &swappableVouchers[i]
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
scaledCredit := "0"
|
// 2️. If pool has a stable, get swap quote
|
||||||
|
if firstPoolStable != nil {
|
||||||
if firstStable != nil {
|
finalAmountStr, err := store.ParseAndScaleAmount(
|
||||||
finalAmountStr, err := store.ParseAndScaleAmount(string(activeBal), string(activeDecimal))
|
string(activeBal),
|
||||||
|
string(activeDecimal),
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logg.ErrorCtxf(ctx, "failed on ParseAndScaleAmount", "error", err)
|
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// swap active -> FIRST stable from pool list
|
// swap active -> FIRST stable from pool list
|
||||||
r, err := h.accountService.GetPoolSwapQuote(ctx, finalAmountStr, string(publicKey), string(activeAddress), string(activePoolAddress), firstStable.TokenAddress)
|
r, err := h.accountService.GetPoolSwapQuote(ctx, finalAmountStr, string(publicKey), string(activeAddress), string(activePoolAddress), firstPoolStable.TokenAddress)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
flag_api_call_error, _ := h.flagManager.GetFlag("flag_api_call_error")
|
|
||||||
res.FlagSet = append(res.FlagSet, flag_api_call_error)
|
res.FlagSet = append(res.FlagSet, flag_api_call_error)
|
||||||
res.Content = l.Get("Your request failed. Please try again later.")
|
res.Content = l.Get("Your request failed. Please try again later.")
|
||||||
logg.ErrorCtxf(ctx, "failed on poolSwap", "error", err)
|
logg.ErrorCtxf(ctx, "failed on poolSwap", "error", err)
|
||||||
@ -239,22 +189,27 @@ func (h *MenuHandlers) CalculateCreditAndDebt(ctx context.Context, sym string, i
|
|||||||
}
|
}
|
||||||
|
|
||||||
// scale using REAL stable decimals
|
// scale using REAL stable decimals
|
||||||
finalQuote := store.ScaleDownBalance(r.OutValue, firstStable.TokenDecimals)
|
finalQuote := store.ScaleDownBalance(r.OutValue, firstPoolStable.TokenDecimals)
|
||||||
|
|
||||||
scaledCredit = store.AddDecimalStrings(scaledCredit, finalQuote)
|
scaledCredit = store.AddDecimalStrings(scaledCredit, finalQuote)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add existing stable balances
|
// 3️. Add ALL wallet stable balances (from FetchVouchers)
|
||||||
for _, v := range stable {
|
for _, v := range allVouchers {
|
||||||
|
if isStableVoucher(v.TokenAddress) {
|
||||||
scaled := store.ScaleDownBalance(v.Balance, v.TokenDecimals)
|
scaled := store.ScaleDownBalance(v.Balance, v.TokenDecimals)
|
||||||
scaledCredit = store.AddDecimalStrings(scaledCredit, scaled)
|
scaledCredit = store.AddDecimalStrings(scaledCredit, scaled)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// DEBT calculation
|
||||||
|
// Rule:
|
||||||
|
// - Default = 0
|
||||||
|
// - If active is stable → remain 0
|
||||||
|
// - If active is non-stable and exists in pool → use pool balance
|
||||||
|
|
||||||
// DEBT calculation: All outstanding active token that is in the current pool
|
|
||||||
// (how much of AT that is in the active Pool)
|
|
||||||
scaledDebt := "0"
|
scaledDebt := "0"
|
||||||
|
|
||||||
// If active voucher is NOT stable,
|
|
||||||
// check if it exists in swappableVouchers
|
|
||||||
if !isStableVoucher(string(activeAddress)) {
|
if !isStableVoucher(string(activeAddress)) {
|
||||||
for _, v := range swappableVouchers {
|
for _, v := range swappableVouchers {
|
||||||
if v.TokenSymbol == string(activeSym) {
|
if v.TokenSymbol == string(activeSym) {
|
||||||
@ -264,7 +219,6 @@ func (h *MenuHandlers) CalculateCreditAndDebt(ctx context.Context, sym string, i
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Format (2 decimal places)
|
|
||||||
formattedDebt, _ := store.TruncateDecimalString(scaledDebt, 2)
|
formattedDebt, _ := store.TruncateDecimalString(scaledDebt, 2)
|
||||||
|
|
||||||
// Fetch MPESA rates
|
// Fetch MPESA rates
|
||||||
@ -277,9 +231,7 @@ func (h *MenuHandlers) CalculateCreditAndDebt(ctx context.Context, sym string, i
|
|||||||
}
|
}
|
||||||
|
|
||||||
creditFloat, _ := strconv.ParseFloat(scaledCredit, 64)
|
creditFloat, _ := strconv.ParseFloat(scaledCredit, 64)
|
||||||
|
|
||||||
creditKsh := fmt.Sprintf("%f", creditFloat*rates.Buy)
|
creditKsh := fmt.Sprintf("%f", creditFloat*rates.Buy)
|
||||||
|
|
||||||
kshFormattedCredit, _ := store.TruncateDecimalString(creditKsh, 0)
|
kshFormattedCredit, _ := store.TruncateDecimalString(creditKsh, 0)
|
||||||
|
|
||||||
res.Content = l.Get(
|
res.Content = l.Get(
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user