Compare commits

...

3 Commits

Author SHA1 Message Date
09954d967f
read the data entries directly from the store
Some checks failed
release / docker (push) Has been cancelled
2026-02-16 10:21:20 +03:00
465b3b5604
use the default stable decimals to scale down the quote 2026-02-16 10:20:10 +03:00
aacea81397
added the default stable voucher address and decimals 2026-02-16 10:14:41 +03:00
4 changed files with 39 additions and 21 deletions

View File

@ -39,5 +39,7 @@ DEFAULT_MPESA_ASSET=cUSD
MPESA_BEARER_TOKEN=eyJeSIsInRcCI6IkpXVCJ.yJwdWJsaWNLZXkiOiIwrrrrrr
MPESA_ONRAMP_BASE=https://pretium.v1.grassecon.net
# Known stable voucher addresses (USDT, USDm)
STABLE_VOUCHER_ADDRESSES=0x48065fbBE25f71C9282ddf5e1cD6D6A887483D5e,0x765DE816845861e75A25fCA122bb6898B8B1282a
# Known stable voucher addresses (USDm, USD₮)
STABLE_VOUCHER_ADDRESSES=0x765DE816845861e75A25fCA122bb6898B8B1282a,0x48065fbBE25f71C9282ddf5e1cD6D6A887483D5e
DEFAULT_STABLE_VOUCHER_ADDRESS=0x765DE816845861e75A25fCA122bb6898B8B1282a
DEFAULT_STABLE_VOUCHER_DECIMALS=18

View File

@ -142,3 +142,11 @@ func StableVoucherAddresses() []string {
return parsed
}
func DefaultStableVoucherAddress() string {
return env.GetEnv("DEFAULT_STABLE_VOUCHER_ADDRESS", "")
}
func DefaultStableVoucherDecimals() string {
return env.GetEnv("DEFAULT_STABLE_VOUCHER_DECIMALS", "")
}

View File

@ -206,7 +206,7 @@ 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
// Credit calculation: How much Active Token that can be swapped for a stable coin
// + any stables sendable to Pretium (in KSH value)
scaledCredit := "0"
@ -214,10 +214,10 @@ func (h *MenuHandlers) CalculateCreditAndDebt(ctx context.Context, sym string, i
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]
// do a swap quote for default stable coin from active voucher
stableAddress := config.DefaultStableVoucherAddress()
stableDecimals := config.DefaultStableVoucherDecimals()
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")
@ -227,7 +227,7 @@ func (h *MenuHandlers) CalculateCreditAndDebt(ctx context.Context, sym string, i
return res, nil
}
finalQuote := store.ScaleDownBalance(r.OutValue, "6")
finalQuote := store.ScaleDownBalance(r.OutValue, stableDecimals)
scaledCredit = store.AddDecimalStrings(scaledCredit, finalQuote)

View File

@ -161,7 +161,7 @@ func (h *MenuHandlers) ConfirmDebtRemoval(ctx context.Context, sym string, input
userStore := h.userdataStore
// Fetch session data
_, _, activeSym, activeAddress, publicKey, _, err := h.getSessionData(ctx, sessionId)
_, _, activeSym, activeAddress, publicKey, activeDecimal, err := h.getSessionData(ctx, sessionId)
if err != nil {
return res, nil
}
@ -172,12 +172,19 @@ func (h *MenuHandlers) ConfirmDebtRemoval(ctx context.Context, sym string, input
return res, err
}
swapData, err := store.ReadSwapPreviewData(ctx, userStore, sessionId)
// Resolve active pool
activePoolAddress, _, err := h.resolveActivePoolDetails(ctx, sessionId)
if err != nil {
return res, err
}
maxValue, err := strconv.ParseFloat(swapData.ActiveSwapMaxAmount, 64)
swapMaxAmount, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_SWAP_MAX_AMOUNT)
if err != nil {
logg.ErrorCtxf(ctx, "failed to read swapMaxAmount entry with", "key", storedb.DATA_ACTIVE_SWAP_MAX_AMOUNT, "error", err)
return res, err
}
maxValue, err := strconv.ParseFloat(string(swapMaxAmount), 64)
if err != nil {
logg.ErrorCtxf(ctx, "Failed to convert the swapMaxAmount to a float", "error", err)
return res, err
@ -191,7 +198,7 @@ func (h *MenuHandlers) ConfirmDebtRemoval(ctx context.Context, sym string, input
}
var finalAmountStr string
if inputStr == swapData.ActiveSwapMaxAmount {
if inputStr == string(swapMaxAmount) {
finalAmountStr = string(payDebtVoucher.Balance)
} else {
finalAmountStr, err = store.ParseAndScaleAmount(inputStr, payDebtVoucher.TokenDecimals)
@ -207,7 +214,7 @@ func (h *MenuHandlers) ConfirmDebtRemoval(ctx context.Context, sym string, input
}
// call the API to get the quote
r, err := h.accountService.GetPoolSwapQuote(ctx, finalAmountStr, string(publicKey), payDebtVoucher.TokenAddress, swapData.ActivePoolAddress, string(activeAddress))
r, err := h.accountService.GetPoolSwapQuote(ctx, finalAmountStr, string(publicKey), payDebtVoucher.TokenAddress, string(activePoolAddress), string(activeAddress))
if err != nil {
flag_api_call_error, _ := h.flagManager.GetFlag("flag_api_call_error")
res.FlagSet = append(res.FlagSet, flag_api_call_error)
@ -216,8 +223,8 @@ func (h *MenuHandlers) ConfirmDebtRemoval(ctx context.Context, sym string, input
return res, nil
}
// Scale down the quoted amount
quoteAmountStr := store.ScaleDownBalance(r.OutValue, swapData.ActiveSwapFromDecimal)
// Scale down the quoted amount (for the AT)
quoteAmountStr := store.ScaleDownBalance(r.OutValue, string(activeDecimal))
// Format to 2 decimal places
qouteStr, _ := store.TruncateDecimalString(string(quoteAmountStr), 2)
@ -272,17 +279,18 @@ func (h *MenuHandlers) InitiatePayDebt(ctx context.Context, sym string, input []
return res, err
}
swapData, err := store.ReadSwapPreviewData(ctx, userStore, sessionId)
if err != nil {
return res, err
}
swapAmount, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_SWAP_AMOUNT)
if err != nil {
logg.ErrorCtxf(ctx, "failed to read swapAmount entry with", "key", storedb.DATA_ACTIVE_SWAP_AMOUNT, "error", err)
return res, err
}
debtQuotedAmount, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE)
if err != nil {
logg.ErrorCtxf(ctx, "failed to read debtQuotedAmount entry with", "key", storedb.DATA_TEMPORARY_VALUE, "error", err)
return res, err
}
swapAmountStr := string(swapAmount)
// Call the poolSwap API
@ -300,7 +308,7 @@ func (h *MenuHandlers) InitiatePayDebt(ctx context.Context, sym string, input []
res.Content = l.Get(
"Your request has been sent. You will receive an SMS when your debt of %s %s has been removed from %s.",
swapData.TemporaryValue,
string(debtQuotedAmount),
string(activeSym),
activePoolName,
)