update the credit and debt calculations
This commit is contained in:
parent
0ef706a47e
commit
cdd83dfd73
@ -128,10 +128,8 @@ func (h *MenuHandlers) CalculateCreditAndDebt(ctx context.Context, sym string, i
|
|||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// default response
|
// default flag reset
|
||||||
formattedDebt, _ := store.TruncateDecimalString(string(activeBal), 2)
|
|
||||||
res.FlagReset = append(res.FlagReset, flag_api_call_error)
|
res.FlagReset = append(res.FlagReset, flag_api_call_error)
|
||||||
res.Content = l.Get("Credit: %s KSH\nDebt: %s %s\n", "0", formattedDebt, string(activeSym))
|
|
||||||
|
|
||||||
// Resolve active pool
|
// Resolve active pool
|
||||||
activePoolAddress, _, err := h.resolveActivePoolDetails(ctx, sessionId)
|
activePoolAddress, _, err := h.resolveActivePoolDetails(ctx, sessionId)
|
||||||
@ -143,6 +141,7 @@ func (h *MenuHandlers) CalculateCreditAndDebt(ctx context.Context, sym string, i
|
|||||||
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)
|
||||||
|
res.Content = l.Get("Credit: %s KSH\nDebt: %s %s\n", "0", "0", string(activeSym))
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -208,30 +207,42 @@ func (h *MenuHandlers) CalculateCreditAndDebt(ctx context.Context, sym string, i
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Credit calculation: How much Active Token that can be swapped for a stable coin
|
// Credit calculation: How much Active Token that can be swapped for a stable coin
|
||||||
// + any stables sendable to Pretium (in KSH value)
|
// 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 {
|
||||||
|
if isStableVoucher(swappableVouchers[i].TokenAddress) {
|
||||||
|
firstStable = &swappableVouchers[i]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
scaledCredit := "0"
|
scaledCredit := "0"
|
||||||
|
|
||||||
finalAmountStr, err := store.ParseAndScaleAmount(string(activeBal), string(activeDecimal))
|
if firstStable != nil {
|
||||||
if err != nil {
|
finalAmountStr, err := store.ParseAndScaleAmount(string(activeBal), string(activeDecimal))
|
||||||
return res, err
|
if err != nil {
|
||||||
}
|
logg.ErrorCtxf(ctx, "failed on ParseAndScaleAmount", "error", err)
|
||||||
// do a swap quote for default stable coin from active voucher
|
return res, err
|
||||||
stableAddress := config.DefaultStableVoucherAddress()
|
}
|
||||||
stableDecimals := config.DefaultStableVoucherDecimals()
|
|
||||||
|
|
||||||
r, err := h.accountService.GetPoolSwapQuote(ctx, finalAmountStr, string(publicKey), string(activeAddress), string(activePoolAddress), stableAddress)
|
// swap active -> FIRST stable from pool list
|
||||||
if err != nil {
|
r, err := h.accountService.GetPoolSwapQuote(ctx, finalAmountStr, string(publicKey), string(activeAddress), string(activePoolAddress), firstStable.TokenAddress)
|
||||||
flag_api_call_error, _ := h.flagManager.GetFlag("flag_api_call_error")
|
if err != nil {
|
||||||
res.FlagSet = append(res.FlagSet, flag_api_call_error)
|
flag_api_call_error, _ := h.flagManager.GetFlag("flag_api_call_error")
|
||||||
res.Content = l.Get("Your request failed. Please try again later.")
|
res.FlagSet = append(res.FlagSet, flag_api_call_error)
|
||||||
logg.ErrorCtxf(ctx, "failed on poolSwap", "error", err)
|
res.Content = l.Get("Your request failed. Please try again later.")
|
||||||
return res, nil
|
logg.ErrorCtxf(ctx, "failed on poolSwap", "error", err)
|
||||||
|
return res, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// scale using REAL stable decimals
|
||||||
|
finalQuote := store.ScaleDownBalance(r.OutValue, firstStable.TokenDecimals)
|
||||||
|
scaledCredit = store.AddDecimalStrings(scaledCredit, finalQuote)
|
||||||
}
|
}
|
||||||
|
|
||||||
finalQuote := store.ScaleDownBalance(r.OutValue, stableDecimals)
|
// Add existing stable balances
|
||||||
|
|
||||||
scaledCredit = store.AddDecimalStrings(scaledCredit, finalQuote)
|
|
||||||
|
|
||||||
for _, v := range stable {
|
for _, v := range stable {
|
||||||
scaled := store.ScaleDownBalance(v.Balance, v.TokenDecimals)
|
scaled := store.ScaleDownBalance(v.Balance, v.TokenDecimals)
|
||||||
scaledCredit = store.AddDecimalStrings(scaledCredit, scaled)
|
scaledCredit = store.AddDecimalStrings(scaledCredit, scaled)
|
||||||
@ -239,6 +250,21 @@ func (h *MenuHandlers) CalculateCreditAndDebt(ctx context.Context, sym string, i
|
|||||||
|
|
||||||
// DEBT calculation: All outstanding active token that is in the current pool
|
// DEBT calculation: All outstanding active token that is in the current pool
|
||||||
// (how much of AT that is in the active Pool)
|
// (how much of AT that is in the active Pool)
|
||||||
|
scaledDebt := "0"
|
||||||
|
|
||||||
|
// If active voucher is NOT stable,
|
||||||
|
// check if it exists in swappableVouchers
|
||||||
|
if !isStableVoucher(string(activeAddress)) {
|
||||||
|
for _, v := range swappableVouchers {
|
||||||
|
if v.TokenSymbol == string(activeSym) {
|
||||||
|
scaledDebt = store.ScaleDownBalance(v.Balance, v.TokenDecimals)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Format (2 decimal places)
|
||||||
|
formattedDebt, _ := store.TruncateDecimalString(scaledDebt, 2)
|
||||||
|
|
||||||
// Fetch MPESA rates
|
// Fetch MPESA rates
|
||||||
rates, err := h.accountService.GetMpesaOnrampRates(ctx)
|
rates, err := h.accountService.GetMpesaOnrampRates(ctx)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user