Compare commits

..

No commits in common. "c2cfd0fe449353521d0e9a2b550c67dac3187cf7" and "43b963995f18baf8181d74875af52c16784cd93d" have entirely different histories.

4 changed files with 22 additions and 46 deletions

View File

@ -111,6 +111,7 @@ func MinMpesaWithdrawAmount() float64 {
return f
}
func MaxMpesaSendAmount() float64 {
v := env.GetEnv("MAX_MPESA_SEND_AMOUNT", "250000")
f, err := strconv.ParseFloat(v, 64)
@ -134,11 +135,11 @@ func StableVoucherAddresses() []string {
list := strings.Split(raw, ",")
for _, addr := range list {
clean := strings.TrimSpace(addr)
clean := strings.ToLower(strings.TrimSpace(addr))
if clean != "" {
parsed = append(parsed, clean)
}
}
return parsed
}
}

View File

@ -5,6 +5,7 @@ import (
"fmt"
"sort"
"strconv"
"strings"
"git.defalsify.org/vise.git/db"
"git.defalsify.org/vise.git/resource"
@ -127,7 +128,7 @@ func (h *MenuHandlers) CalculateCreditAndDebt(ctx context.Context, sym string, i
res.Content = l.Get("Credit: %s KSH\nDebt: %s KSH\n", "0", "0")
// Fetch session data
_, activeBal, activeSym, activeAddress, publicKey, activeDecimal, err := h.getSessionData(ctx, sessionId)
_, activeBal, activeSym, _, publicKey, _, err := h.getSessionData(ctx, sessionId)
if err != nil {
return res, nil
}
@ -153,14 +154,14 @@ func (h *MenuHandlers) CalculateCreditAndDebt(ctx context.Context, sym string, i
stablePriority := make(map[string]int)
stableAddresses := config.StableVoucherAddresses()
for i, addr := range stableAddresses {
stablePriority[addr] = i
stablePriority[strings.ToLower(addr)] = i
}
stable := make([]dataserviceapi.TokenHoldings, 0)
nonStable := make([]dataserviceapi.TokenHoldings, 0)
// Helper: order vouchers (stable first, priority-based)
orderVouchers := func(vouchers []dataserviceapi.TokenHoldings) []dataserviceapi.TokenHoldings {
stable := make([]dataserviceapi.TokenHoldings, 0)
nonStable := make([]dataserviceapi.TokenHoldings, 0)
for _, v := range vouchers {
if isStableVoucher(v.TokenAddress) {
stable = append(stable, v)
@ -170,8 +171,8 @@ func (h *MenuHandlers) CalculateCreditAndDebt(ctx context.Context, sym string, i
}
sort.SliceStable(stable, func(i, j int) bool {
ai := stablePriority[stable[i].TokenAddress]
aj := stablePriority[stable[j].TokenAddress]
ai := stablePriority[strings.ToLower(stable[i].TokenAddress)]
aj := stablePriority[strings.ToLower(stable[j].TokenAddress)]
return ai < aj
})
@ -206,41 +207,15 @@ func (h *MenuHandlers) CalculateCreditAndDebt(ctx context.Context, sym string, i
}
}
// Credit calculation: How much Active Token (such as ALF) that can be swapped for a stable coin
// + any stables sendable to Pretium (in KSH value)
scaledCredit := "0"
// Credit = active voucher balance
scaledCredit := string(activeBal)
finalAmountStr, err := store.ParseAndScaleAmount(string(activeBal), string(activeDecimal))
if err != nil {
return res, err
}
// do a swap quote to get the max I can get when I swap my active voucher
// for a stable coin (say I can get 4 USD). Then I add that to my exisitng
// stable coins and covert to Ksh
stableAddress := stableAddresses[0]
r, err := h.accountService.GetPoolSwapQuote(ctx, finalAmountStr, string(publicKey), string(activeAddress), string(activePoolAddress), stableAddress)
if err != nil {
flag_api_call_error, _ := h.flagManager.GetFlag("flag_api_call_error")
res.FlagSet = append(res.FlagSet, flag_api_call_error)
res.Content = l.Get("Your request failed. Please try again later.")
logg.ErrorCtxf(ctx, "failed on poolSwap", "error", err)
return res, nil
}
finalQuote := store.ScaleDownBalance(r.OutValue, "6")
scaledCredit = store.AddDecimalStrings(scaledCredit, finalQuote)
for _, v := range stable {
scaled := store.ScaleDownBalance(v.Balance, v.TokenDecimals)
scaledCredit = store.AddDecimalStrings(scaledCredit, scaled)
}
// DEBT calculation: All outstanding active token that is in the current pool
// (how much of AT that is in the active Pool)
// Debt = sum of stable vouchers only
scaledDebt := "0"
// convert the current balance to Ksh
scaledDebt = string(activeBal)
for _, v := range orderedFilteredVouchers {
scaled := store.ScaleDownBalance(v.Balance, v.TokenDecimals)
scaledDebt = store.AddDecimalStrings(scaledDebt, scaled)
}
// Fetch MPESA rates
rates, err := h.accountService.GetMpesaOnrampRates(ctx)

View File

@ -310,7 +310,7 @@ func (h *MenuHandlers) InitiatePayDebt(ctx context.Context, sym string, input []
}
func isStableVoucher(tokenAddress string) bool {
addr := strings.TrimSpace(tokenAddress)
addr := strings.ToLower(strings.TrimSpace(tokenAddress))
for _, stable := range config.StableVoucherAddresses() {
if addr == stable {
return true

View File

@ -152,7 +152,7 @@ func (h *MenuHandlers) ManageVouchers(ctx context.Context, sym string, input []b
stablePriority := make(map[string]int)
stableAddresses := config.StableVoucherAddresses()
for i, addr := range stableAddresses {
stablePriority[addr] = i
stablePriority[strings.ToLower(addr)] = i
}
// Helper: order vouchers (stable first, priority-based)
@ -169,8 +169,8 @@ func (h *MenuHandlers) ManageVouchers(ctx context.Context, sym string, input []b
}
sort.SliceStable(stable, func(i, j int) bool {
ai := stablePriority[stable[i].TokenAddress]
aj := stablePriority[stable[j].TokenAddress]
ai := stablePriority[strings.ToLower(stable[i].TokenAddress)]
aj := stablePriority[strings.ToLower(stable[j].TokenAddress)]
return ai < aj
})