Compare commits
No commits in common. "504fcb67d3898d56c56f91bd9bfdf6b061b99114" and "5ed7cbd40a1b90337aafb1cb73c7c9e3d1db979e" have entirely different histories.
504fcb67d3
...
5ed7cbd40a
@ -34,7 +34,6 @@ INCLUDE_STABLES_PARAM=false
|
|||||||
DEFAULT_MPESA_ADDRESS=0x48a953cA5cf5298bc6f6Af3C608351f537AAcb9e
|
DEFAULT_MPESA_ADDRESS=0x48a953cA5cf5298bc6f6Af3C608351f537AAcb9e
|
||||||
MIN_MPESA_SEND_AMOUNT=100
|
MIN_MPESA_SEND_AMOUNT=100
|
||||||
MAX_MPESA_SEND_AMOUNT=250000
|
MAX_MPESA_SEND_AMOUNT=250000
|
||||||
MIN_MPESA_WITHDRAW_AMOUNT=20
|
|
||||||
DEFAULT_MPESA_ASSET=cUSD
|
DEFAULT_MPESA_ASSET=cUSD
|
||||||
MPESA_BEARER_TOKEN=eyJeSIsInRcCI6IkpXVCJ.yJwdWJsaWNLZXkiOiIwrrrrrr
|
MPESA_BEARER_TOKEN=eyJeSIsInRcCI6IkpXVCJ.yJwdWJsaWNLZXkiOiIwrrrrrr
|
||||||
MPESA_ONRAMP_BASE=https://pretium.v1.grassecon.net
|
MPESA_ONRAMP_BASE=https://pretium.v1.grassecon.net
|
||||||
|
|||||||
@ -102,16 +102,6 @@ func MinMpesaSendAmount() float64 {
|
|||||||
return f
|
return f
|
||||||
}
|
}
|
||||||
|
|
||||||
func MinMpesaWithdrawAmount() float64 {
|
|
||||||
v := env.GetEnv("MIN_MPESA_WITHDRAW_AMOUNT", "20")
|
|
||||||
f, err := strconv.ParseFloat(v, 64)
|
|
||||||
if err != nil {
|
|
||||||
return 20 // fallback
|
|
||||||
}
|
|
||||||
return f
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
func MaxMpesaSendAmount() float64 {
|
func MaxMpesaSendAmount() float64 {
|
||||||
v := env.GetEnv("MAX_MPESA_SEND_AMOUNT", "250000")
|
v := env.GetEnv("MAX_MPESA_SEND_AMOUNT", "250000")
|
||||||
f, err := strconv.ParseFloat(v, 64)
|
f, err := strconv.ParseFloat(v, 64)
|
||||||
|
|||||||
@ -28,36 +28,20 @@ func (h *MenuHandlers) GetMpesaMaxLimit(ctx context.Context, sym string, input [
|
|||||||
flag_api_call_error, _ := h.flagManager.GetFlag("flag_api_call_error")
|
flag_api_call_error, _ := h.flagManager.GetFlag("flag_api_call_error")
|
||||||
flag_low_swap_amount, _ := h.flagManager.GetFlag("flag_low_swap_amount")
|
flag_low_swap_amount, _ := h.flagManager.GetFlag("flag_low_swap_amount")
|
||||||
flag_incorrect_pool, _ := h.flagManager.GetFlag("flag_incorrect_pool")
|
flag_incorrect_pool, _ := h.flagManager.GetFlag("flag_incorrect_pool")
|
||||||
flag_incorrect_voucher, _ := h.flagManager.GetFlag("flag_incorrect_voucher")
|
|
||||||
|
|
||||||
code := codeFromCtx(ctx)
|
code := codeFromCtx(ctx)
|
||||||
l := gotext.NewLocale(translationDir, code)
|
l := gotext.NewLocale(translationDir, code)
|
||||||
l.AddDomain("default")
|
l.AddDomain("default")
|
||||||
|
|
||||||
inputStr := string(input)
|
inputStr := string(input)
|
||||||
if inputStr == "0" || inputStr == "99" || inputStr == "88" || inputStr == "98" {
|
if inputStr == "0" || inputStr == "9" {
|
||||||
res.FlagReset = append(res.FlagReset, flag_low_swap_amount, flag_api_call_error, flag_incorrect_voucher)
|
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
userStore := h.userdataStore
|
userStore := h.userdataStore
|
||||||
metadata, err := store.GetOrderedVoucherData(ctx, userStore, sessionId, inputStr)
|
|
||||||
if err != nil {
|
|
||||||
return res, fmt.Errorf("failed to retrieve swap to voucher data: %v", err)
|
|
||||||
}
|
|
||||||
if metadata == nil {
|
|
||||||
res.FlagSet = append(res.FlagSet, flag_incorrect_voucher)
|
|
||||||
return res, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Store the active transaction voucher data
|
|
||||||
if err := store.StoreTemporaryVoucher(ctx, h.userdataStore, sessionId, metadata); err != nil {
|
|
||||||
logg.ErrorCtxf(ctx, "failed on StoreTemporaryVoucher", "error", err)
|
|
||||||
return res, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fetch session data
|
// Fetch session data
|
||||||
_, _, _, _, publicKey, _, err := h.getSessionData(ctx, sessionId)
|
_, activeBal, _, activeAddress, publicKey, activeDecimal, err := h.getSessionData(ctx, sessionId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
@ -99,12 +83,8 @@ func (h *MenuHandlers) GetMpesaMaxLimit(ctx context.Context, sym string, input [
|
|||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch min withdrawal amount from config/env
|
|
||||||
minksh := fmt.Sprintf("%f", config.MinMpesaWithdrawAmount())
|
|
||||||
minKshFormatted, _ := store.TruncateDecimalString(minksh, 0)
|
|
||||||
|
|
||||||
// If RAT is the same as SAT, return early with KSH format
|
// If RAT is the same as SAT, return early with KSH format
|
||||||
if string(metadata.TokenAddress) == string(recipientActiveAddress) {
|
if string(activeAddress) == string(recipientActiveAddress) {
|
||||||
txType = "normal"
|
txType = "normal"
|
||||||
// Save the transaction type
|
// Save the transaction type
|
||||||
if err := userStore.WriteEntry(ctx, sessionId, storedb.DATA_SEND_TRANSACTION_TYPE, []byte(txType)); err != nil {
|
if err := userStore.WriteEntry(ctx, sessionId, storedb.DATA_SEND_TRANSACTION_TYPE, []byte(txType)); err != nil {
|
||||||
@ -112,15 +92,14 @@ func (h *MenuHandlers) GetMpesaMaxLimit(ctx context.Context, sym string, input [
|
|||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
activeFloat, _ := strconv.ParseFloat(string(metadata.Balance), 64)
|
activeFloat, _ := strconv.ParseFloat(string(activeBal), 64)
|
||||||
ksh := fmt.Sprintf("%f", activeFloat*rates.Buy)
|
ksh := fmt.Sprintf("%f", activeFloat*rates.Buy)
|
||||||
|
|
||||||
maxKshFormatted, _ := store.TruncateDecimalString(ksh, 0)
|
kshFormatted, _ := store.TruncateDecimalString(ksh, 0)
|
||||||
|
|
||||||
res.Content = l.Get(
|
res.Content = l.Get(
|
||||||
"Enter the amount of Mpesa to withdraw: (Min: Ksh %s, Max %s Ksh)\n",
|
"Enter the amount of Mpesa to get: (Max %s Ksh)\n",
|
||||||
minKshFormatted,
|
kshFormatted,
|
||||||
maxKshFormatted,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
@ -133,7 +112,7 @@ func (h *MenuHandlers) GetMpesaMaxLimit(ctx context.Context, sym string, input [
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check if sender token is swappable
|
// Check if sender token is swappable
|
||||||
canSwap, err := h.accountService.CheckTokenInPool(ctx, string(activePoolAddress), string(metadata.TokenAddress))
|
canSwap, err := h.accountService.CheckTokenInPool(ctx, string(activePoolAddress), string(activeAddress))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
res.FlagSet = append(res.FlagSet, flag_api_call_error)
|
res.FlagSet = append(res.FlagSet, flag_api_call_error)
|
||||||
logg.ErrorCtxf(ctx, "failed on CheckTokenInPool", "error", err)
|
logg.ErrorCtxf(ctx, "failed on CheckTokenInPool", "error", err)
|
||||||
@ -146,7 +125,7 @@ func (h *MenuHandlers) GetMpesaMaxLimit(ctx context.Context, sym string, input [
|
|||||||
}
|
}
|
||||||
|
|
||||||
// retrieve the max credit send amounts
|
// retrieve the max credit send amounts
|
||||||
_, maxRAT, err := h.calculateSendCreditLimits(ctx, activePoolAddress, []byte(metadata.TokenAddress), recipientActiveAddress, publicKey, []byte(metadata.TokenDecimals), recipientActiveDecimal)
|
_, maxRAT, err := h.calculateSendCreditLimits(ctx, activePoolAddress, activeAddress, recipientActiveAddress, publicKey, activeDecimal, recipientActiveDecimal)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
res.FlagSet = append(res.FlagSet, flag_api_call_error)
|
res.FlagSet = append(res.FlagSet, flag_api_call_error)
|
||||||
logg.ErrorCtxf(ctx, "failed on calculateSendCreditLimits", "error", err)
|
logg.ErrorCtxf(ctx, "failed on calculateSendCreditLimits", "error", err)
|
||||||
@ -176,26 +155,25 @@ func (h *MenuHandlers) GetMpesaMaxLimit(ctx context.Context, sym string, input [
|
|||||||
}
|
}
|
||||||
|
|
||||||
// save swap related data for the swap preview
|
// save swap related data for the swap preview
|
||||||
swapMetadata := &dataserviceapi.TokenHoldings{
|
metadata := &dataserviceapi.TokenHoldings{
|
||||||
TokenAddress: string(recipientActiveAddress),
|
TokenAddress: string(recipientActiveAddress),
|
||||||
TokenSymbol: string(recipientActiveSym),
|
TokenSymbol: string(recipientActiveSym),
|
||||||
TokenDecimals: string(recipientActiveDecimal),
|
TokenDecimals: string(recipientActiveDecimal),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Store the active swap_to data
|
// Store the active swap_to data
|
||||||
if err := store.UpdateSwapToVoucherData(ctx, userStore, sessionId, swapMetadata); err != nil {
|
if err := store.UpdateSwapToVoucherData(ctx, userStore, sessionId, metadata); err != nil {
|
||||||
logg.ErrorCtxf(ctx, "failed on UpdateSwapToVoucherData", "error", err)
|
logg.ErrorCtxf(ctx, "failed on UpdateSwapToVoucherData", "error", err)
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
maxKsh := maxFloat * rates.Buy
|
maxKsh := maxFloat * rates.Buy
|
||||||
kshStr := fmt.Sprintf("%f", maxKsh)
|
kshStr := fmt.Sprintf("%f", maxKsh)
|
||||||
maxKshFormatted, _ := store.TruncateDecimalString(kshStr, 0)
|
kshFormatted, _ := store.TruncateDecimalString(kshStr, 0)
|
||||||
|
|
||||||
res.Content = l.Get(
|
res.Content = l.Get(
|
||||||
"Enter the amount of Mpesa to withdraw: (Min: Ksh %s, Max %s Ksh)\n",
|
"Enter the amount of Mpesa to get: (Max %s Ksh)\n",
|
||||||
minKshFormatted,
|
kshFormatted,
|
||||||
maxKshFormatted,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
@ -211,7 +189,7 @@ func (h *MenuHandlers) GetMpesaPreview(ctx context.Context, sym string, input []
|
|||||||
|
|
||||||
// INPUT IN RAT Ksh
|
// INPUT IN RAT Ksh
|
||||||
inputStr := string(input)
|
inputStr := string(input)
|
||||||
if inputStr == "0" || inputStr == "9" {
|
if inputStr == "9" {
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -241,18 +219,16 @@ func (h *MenuHandlers) GetMpesaPreview(ctx context.Context, sym string, input []
|
|||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
min := config.MinMpesaWithdrawAmount()
|
|
||||||
|
|
||||||
if kshAmount < min {
|
|
||||||
// if the input is below the minimum
|
|
||||||
res.FlagSet = append(res.FlagSet, flag_invalid_amount)
|
|
||||||
res.Content = inputStr
|
|
||||||
return res, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// divide by the buy rate
|
// divide by the buy rate
|
||||||
inputAmount := kshAmount / rates.Buy
|
inputAmount := kshAmount / rates.Buy
|
||||||
|
|
||||||
|
// store the user's raw input amount in the temporary value
|
||||||
|
err = userStore.WriteEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE, []byte(inputStr))
|
||||||
|
if err != nil {
|
||||||
|
logg.ErrorCtxf(ctx, "failed to write temporary inputStr entry with", "key", storedb.DATA_TEMPORARY_VALUE, "value", inputStr, "error", err)
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
|
||||||
swapData, err := store.ReadSwapPreviewData(ctx, userStore, sessionId)
|
swapData, err := store.ReadSwapPreviewData(ctx, userStore, sessionId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, err
|
return res, err
|
||||||
@ -263,21 +239,19 @@ func (h *MenuHandlers) GetMpesaPreview(ctx context.Context, sym string, input []
|
|||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the selected voucher
|
|
||||||
mpesaWithdrawalVoucher, err := store.GetTemporaryVoucherData(ctx, h.userdataStore, sessionId)
|
|
||||||
if err != nil {
|
|
||||||
logg.ErrorCtxf(ctx, "failed on GetTemporaryVoucherData", "error", err)
|
|
||||||
return res, err
|
|
||||||
}
|
|
||||||
|
|
||||||
maxValue, err := strconv.ParseFloat(mpesaWithdrawalVoucher.Balance, 64)
|
|
||||||
if err != nil {
|
|
||||||
logg.ErrorCtxf(ctx, "Failed to convert the swapMaxAmount to a float", "error", err)
|
|
||||||
return res, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if string(transactionType) == "normal" {
|
if string(transactionType) == "normal" {
|
||||||
if inputAmount > maxValue {
|
activeBal, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_BAL)
|
||||||
|
if err != nil {
|
||||||
|
logg.ErrorCtxf(ctx, "failed to read activeBal entry with", "key", storedb.DATA_ACTIVE_BAL, "error", err)
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
balanceValue, err := strconv.ParseFloat(string(activeBal), 64)
|
||||||
|
if err != nil {
|
||||||
|
logg.ErrorCtxf(ctx, "Failed to convert the activeBal to a float", "error", err)
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if inputAmount > balanceValue {
|
||||||
res.FlagSet = append(res.FlagSet, flag_invalid_amount)
|
res.FlagSet = append(res.FlagSet, flag_invalid_amount)
|
||||||
res.Content = inputStr
|
res.Content = inputStr
|
||||||
return res, nil
|
return res, nil
|
||||||
@ -296,7 +270,7 @@ func (h *MenuHandlers) GetMpesaPreview(ctx context.Context, sym string, input []
|
|||||||
|
|
||||||
res.Content = l.Get(
|
res.Content = l.Get(
|
||||||
"You are sending %s %s in order to receive ~ %s ksh",
|
"You are sending %s %s in order to receive ~ %s ksh",
|
||||||
qouteInputAmount, mpesaWithdrawalVoucher.TokenSymbol, inputStr,
|
qouteInputAmount, swapData.ActiveSwapFromSym, inputStr,
|
||||||
)
|
)
|
||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
@ -391,12 +365,6 @@ func (h *MenuHandlers) InitiateGetMpesa(ctx context.Context, sym string, input [
|
|||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
mpesaWithdrawalVoucher, err := store.GetTemporaryVoucherData(ctx, h.userdataStore, sessionId)
|
|
||||||
if err != nil {
|
|
||||||
logg.ErrorCtxf(ctx, "failed on GetTemporaryVoucherData", "error", err)
|
|
||||||
return res, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if string(transactionType) == "normal" {
|
if string(transactionType) == "normal" {
|
||||||
// Call TokenTransfer for the normal transaction
|
// Call TokenTransfer for the normal transaction
|
||||||
data, err := store.ReadTransactionData(ctx, h.userdataStore, sessionId)
|
data, err := store.ReadTransactionData(ctx, h.userdataStore, sessionId)
|
||||||
@ -404,12 +372,12 @@ func (h *MenuHandlers) InitiateGetMpesa(ctx context.Context, sym string, input [
|
|||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
finalAmountStr, err := store.ParseAndScaleAmount(data.Amount, mpesaWithdrawalVoucher.TokenDecimals)
|
finalAmountStr, err := store.ParseAndScaleAmount(data.Amount, data.ActiveDecimal)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
tokenTransfer, err := h.accountService.TokenTransfer(ctx, finalAmountStr, data.PublicKey, mpesaAddress, mpesaWithdrawalVoucher.TokenAddress)
|
tokenTransfer, err := h.accountService.TokenTransfer(ctx, finalAmountStr, data.PublicKey, mpesaAddress, data.ActiveAddress)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
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.")
|
||||||
@ -448,6 +416,11 @@ func (h *MenuHandlers) InitiateGetMpesa(ctx context.Context, sym string, input [
|
|||||||
// TODO: remove this temporary time delay and replace with a swap and send endpoint
|
// TODO: remove this temporary time delay and replace with a swap and send endpoint
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
|
|
||||||
|
finalKshStr, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE)
|
||||||
|
if err != nil {
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
|
||||||
amount, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_AMOUNT)
|
amount, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_AMOUNT)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, err
|
return res, err
|
||||||
@ -464,7 +437,7 @@ func (h *MenuHandlers) InitiateGetMpesa(ctx context.Context, sym string, input [
|
|||||||
|
|
||||||
logg.InfoCtxf(ctx, "final TokenTransfer after swap", "trackingId", tokenTransfer.TrackingId)
|
logg.InfoCtxf(ctx, "final TokenTransfer after swap", "trackingId", tokenTransfer.TrackingId)
|
||||||
|
|
||||||
res.Content = l.Get("Your request has been sent. Please await confirmation")
|
res.Content = l.Get("Your request has been sent. You will receive ~ %s ksh", finalKshStr)
|
||||||
res.FlagReset = append(res.FlagReset, flag_account_authorized)
|
res.FlagReset = append(res.FlagReset, flag_account_authorized)
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -12,8 +12,8 @@ import (
|
|||||||
"gopkg.in/leonelquinteros/gotext.v1"
|
"gopkg.in/leonelquinteros/gotext.v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetOrderedVouchers returns a list of ordered vouchers with stables at the top
|
// GetPoolDepositVouchers returns a list of stable coins
|
||||||
func (h *MenuHandlers) GetOrderedVouchers(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
func (h *MenuHandlers) GetPoolDepositVouchers(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)
|
||||||
if !ok {
|
if !ok {
|
||||||
@ -26,10 +26,10 @@ func (h *MenuHandlers) GetOrderedVouchers(ctx context.Context, sym string, input
|
|||||||
|
|
||||||
userStore := h.userdataStore
|
userStore := h.userdataStore
|
||||||
|
|
||||||
// Read ordered vouchers from the store
|
// Read stable vouchers from the store
|
||||||
voucherData, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_ORDERED_VOUCHER_SYMBOLS)
|
voucherData, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_STABLE_VOUCHER_SYMBOLS)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logg.ErrorCtxf(ctx, "failed to read stable voucherData entires with", "key", storedb.DATA_ORDERED_VOUCHER_SYMBOLS, "error", err)
|
logg.ErrorCtxf(ctx, "failed to read stable voucherData entires with", "key", storedb.DATA_STABLE_VOUCHER_SYMBOLS, "error", err)
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,9 +37,9 @@ func (h *MenuHandlers) GetOrderedVouchers(ctx context.Context, sym string, input
|
|||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
voucherBalances, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_ORDERED_VOUCHER_BALANCES)
|
voucherBalances, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_STABLE_VOUCHER_BALANCES)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logg.ErrorCtxf(ctx, "failed to read stable voucherData entires with", "key", storedb.DATA_ORDERED_VOUCHER_BALANCES, "error", err)
|
logg.ErrorCtxf(ctx, "failed to read stable voucherData entires with", "key", storedb.DATA_STABLE_VOUCHER_BALANCES, "error", err)
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,7 +73,7 @@ func (h *MenuHandlers) PoolDepositMaxAmount(ctx context.Context, sym string, inp
|
|||||||
}
|
}
|
||||||
|
|
||||||
userStore := h.userdataStore
|
userStore := h.userdataStore
|
||||||
metadata, err := store.GetOrderedVoucherData(ctx, userStore, sessionId, inputStr)
|
metadata, err := store.GetStableVoucherData(ctx, userStore, sessionId, inputStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, fmt.Errorf("failed to retrieve swap to voucher data: %v", err)
|
return res, fmt.Errorf("failed to retrieve swap to voucher data: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,12 +3,10 @@ package application
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"sort"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"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"
|
||||||
@ -174,55 +172,35 @@ func (h *MenuHandlers) ManageVouchers(ctx context.Context, sym string, input []b
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get stable voucher priority order (lower index = higher priority)
|
// Filter stable vouchers
|
||||||
stablePriority := make(map[string]int)
|
filteredStableVouchers := make([]dataserviceapi.TokenHoldings, 0)
|
||||||
stableAddresses := config.StableVoucherAddresses()
|
|
||||||
|
|
||||||
for i, addr := range stableAddresses {
|
|
||||||
stablePriority[strings.ToLower(addr)] = i
|
|
||||||
}
|
|
||||||
|
|
||||||
// Split vouchers into stable and non-stable
|
|
||||||
stableVouchers := make([]dataserviceapi.TokenHoldings, 0)
|
|
||||||
nonStableVouchers := make([]dataserviceapi.TokenHoldings, 0)
|
|
||||||
|
|
||||||
for _, v := range vouchersResp {
|
for _, v := range vouchersResp {
|
||||||
|
|
||||||
if isStableVoucher(v.TokenAddress) {
|
if isStableVoucher(v.TokenAddress) {
|
||||||
stableVouchers = append(stableVouchers, v)
|
filteredStableVouchers = append(filteredStableVouchers, v)
|
||||||
} else {
|
|
||||||
nonStableVouchers = append(nonStableVouchers, v)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// No stable vouchers at all
|
// No stable vouchers
|
||||||
if len(stableVouchers) == 0 {
|
if len(filteredStableVouchers) == 0 {
|
||||||
res.FlagSet = append(res.FlagSet, flag_no_stable_vouchers)
|
res.FlagSet = append(res.FlagSet, flag_no_stable_vouchers)
|
||||||
} else {
|
return res, nil
|
||||||
res.FlagReset = append(res.FlagReset, flag_no_stable_vouchers)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sort stable vouchers by configured priority
|
res.FlagReset = append(res.FlagReset, flag_no_stable_vouchers)
|
||||||
sort.SliceStable(stableVouchers, func(i, j int) bool {
|
|
||||||
ai := stablePriority[strings.ToLower(stableVouchers[i].TokenAddress)]
|
|
||||||
aj := stablePriority[strings.ToLower(stableVouchers[j].TokenAddress)]
|
|
||||||
return ai < aj
|
|
||||||
})
|
|
||||||
|
|
||||||
// Final ordered list: stable first, then others
|
// Process stable vouchers for later use
|
||||||
orderedVouchers := append(stableVouchers, nonStableVouchers...)
|
stableVoucherData := store.ProcessVouchers(filteredStableVouchers)
|
||||||
|
|
||||||
// Process ALL vouchers (stable first)
|
stableVoucherDataMap := map[storedb.DataTyp]string{
|
||||||
orderedVoucherData := store.ProcessVouchers(orderedVouchers)
|
storedb.DATA_STABLE_VOUCHER_SYMBOLS: stableVoucherData.Symbols,
|
||||||
|
storedb.DATA_STABLE_VOUCHER_BALANCES: stableVoucherData.Balances,
|
||||||
orderedVoucherDataMap := map[storedb.DataTyp]string{
|
storedb.DATA_STABLE_VOUCHER_DECIMALS: stableVoucherData.Decimals,
|
||||||
storedb.DATA_ORDERED_VOUCHER_SYMBOLS: orderedVoucherData.Symbols,
|
storedb.DATA_STABLE_VOUCHER_ADDRESSES: stableVoucherData.Addresses,
|
||||||
storedb.DATA_ORDERED_VOUCHER_BALANCES: orderedVoucherData.Balances,
|
|
||||||
storedb.DATA_ORDERED_VOUCHER_DECIMALS: orderedVoucherData.Decimals,
|
|
||||||
storedb.DATA_ORDERED_VOUCHER_ADDRESSES: orderedVoucherData.Addresses,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write data entries
|
// Write data entries
|
||||||
for key, value := range orderedVoucherDataMap {
|
for key, value := range stableVoucherDataMap {
|
||||||
if err := userStore.WriteEntry(ctx, sessionId, key, []byte(value)); err != nil {
|
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)
|
logg.ErrorCtxf(ctx, "Failed to write data entry for sessionId: %s", sessionId, "key", key, "error", err)
|
||||||
continue
|
continue
|
||||||
|
|||||||
@ -150,7 +150,7 @@ func (ls *LocalHandlerService) GetHandler(accountService remote.AccountService)
|
|||||||
ls.DbRs.AddLocalFunc("calculate_max_pay_debt", appHandlers.CalculateMaxPayDebt)
|
ls.DbRs.AddLocalFunc("calculate_max_pay_debt", appHandlers.CalculateMaxPayDebt)
|
||||||
ls.DbRs.AddLocalFunc("confirm_debt_removal", appHandlers.ConfirmDebtRemoval)
|
ls.DbRs.AddLocalFunc("confirm_debt_removal", appHandlers.ConfirmDebtRemoval)
|
||||||
ls.DbRs.AddLocalFunc("initiate_pay_debt", appHandlers.InitiatePayDebt)
|
ls.DbRs.AddLocalFunc("initiate_pay_debt", appHandlers.InitiatePayDebt)
|
||||||
ls.DbRs.AddLocalFunc("get_ordered_vouchers", appHandlers.GetOrderedVouchers)
|
ls.DbRs.AddLocalFunc("get_pool_deposit_vouchers", appHandlers.GetPoolDepositVouchers)
|
||||||
ls.DbRs.AddLocalFunc("pool_deposit_max_amount", appHandlers.PoolDepositMaxAmount)
|
ls.DbRs.AddLocalFunc("pool_deposit_max_amount", appHandlers.PoolDepositMaxAmount)
|
||||||
ls.DbRs.AddLocalFunc("confirm_pool_deposit", appHandlers.ConfirmPoolDeposit)
|
ls.DbRs.AddLocalFunc("confirm_pool_deposit", appHandlers.ConfirmPoolDeposit)
|
||||||
ls.DbRs.AddLocalFunc("initiate_pool_deposit", appHandlers.InitiatePoolDeposit)
|
ls.DbRs.AddLocalFunc("initiate_pool_deposit", appHandlers.InitiatePoolDeposit)
|
||||||
|
|||||||
@ -1 +1 @@
|
|||||||
{{.get_ordered_vouchers}}
|
{{.get_mpesa_max_limit}}
|
||||||
@ -1,15 +1,10 @@
|
|||||||
CATCH no_voucher flag_no_active_voucher 1
|
CATCH no_voucher flag_no_active_voucher 1
|
||||||
LOAD get_ordered_vouchers 0
|
|
||||||
MAP get_ordered_vouchers
|
|
||||||
MOUT back 0
|
|
||||||
MOUT quit 99
|
|
||||||
MNEXT next 88
|
|
||||||
MPREV prev 98
|
|
||||||
HALT
|
|
||||||
INCMP > 88
|
|
||||||
INCMP < 98
|
|
||||||
INCMP _ 0
|
|
||||||
INCMP quit 99
|
|
||||||
LOAD get_mpesa_max_limit 0
|
LOAD get_mpesa_max_limit 0
|
||||||
RELOAD get_mpesa_max_limit
|
RELOAD get_mpesa_max_limit
|
||||||
INCMP mpesa_max_limit *
|
MAP get_mpesa_max_limit
|
||||||
|
MOUT back 0
|
||||||
|
MOUT quit 9
|
||||||
|
HALT
|
||||||
|
INCMP _ 0
|
||||||
|
INCMP quit 9
|
||||||
|
INCMP get_mpesa_confirmation *
|
||||||
|
|||||||
@ -1,4 +1,7 @@
|
|||||||
|
LOAD get_mpesa_preview 0
|
||||||
MAP get_mpesa_preview
|
MAP get_mpesa_preview
|
||||||
|
CATCH api_failure flag_api_call_error 1
|
||||||
|
CATCH invalid_credit_send_amount flag_invalid_amount 1
|
||||||
MOUT back 0
|
MOUT back 0
|
||||||
MOUT quit 9
|
MOUT quit 9
|
||||||
HALT
|
HALT
|
||||||
|
|||||||
@ -1 +1 @@
|
|||||||
Withdraw
|
Get M-Pesa
|
||||||
@ -1 +0,0 @@
|
|||||||
Amount {{.get_mpesa_preview}} is invalid, please try again:
|
|
||||||
@ -1,7 +0,0 @@
|
|||||||
MAP get_mpesa_preview
|
|
||||||
RELOAD reset_transaction_amount
|
|
||||||
MOUT retry 1
|
|
||||||
MOUT quit 9
|
|
||||||
HALT
|
|
||||||
INCMP _ 1
|
|
||||||
INCMP quit 9
|
|
||||||
@ -1 +0,0 @@
|
|||||||
Kiwango {{.get_mpesa_preview}} sio sahihi, tafadhali weka tena:
|
|
||||||
@ -1 +0,0 @@
|
|||||||
{{.get_mpesa_max_limit}}
|
|
||||||
@ -1,13 +0,0 @@
|
|||||||
LOAD reset_transaction_amount 10
|
|
||||||
RELOAD reset_transaction_amount
|
|
||||||
MAP get_mpesa_max_limit
|
|
||||||
MOUT back 0
|
|
||||||
MOUT quit 9
|
|
||||||
HALT
|
|
||||||
INCMP _ 0
|
|
||||||
INCMP quit 9
|
|
||||||
LOAD get_mpesa_preview 90
|
|
||||||
RELOAD get_mpesa_preview
|
|
||||||
CATCH api_failure flag_api_call_error 1
|
|
||||||
CATCH invalid_get_mpesa_amount flag_invalid_amount 1
|
|
||||||
INCMP get_mpesa_confirmation *
|
|
||||||
@ -1 +1 @@
|
|||||||
{{.get_ordered_vouchers}}
|
{{.get_pool_deposit_vouchers}}
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
CATCH no_voucher flag_no_active_voucher 1
|
CATCH no_voucher flag_no_active_voucher 1
|
||||||
CATCH no_stable_voucher flag_no_stable_vouchers 1
|
CATCH no_stable_voucher flag_no_stable_vouchers 1
|
||||||
LOAD get_ordered_vouchers 0
|
LOAD get_pool_deposit_vouchers 0
|
||||||
MAP get_ordered_vouchers
|
MAP get_pool_deposit_vouchers
|
||||||
MOUT back 0
|
MOUT back 0
|
||||||
MOUT quit 99
|
MOUT quit 99
|
||||||
MNEXT next 88
|
MNEXT next 88
|
||||||
|
|||||||
@ -1 +1 @@
|
|||||||
Top-up
|
Send M-Pesa
|
||||||
@ -104,14 +104,14 @@ const (
|
|||||||
DATA_VOUCHER_DECIMALS
|
DATA_VOUCHER_DECIMALS
|
||||||
// List of voucher EVM addresses for vouchers valid in the user context.
|
// List of voucher EVM addresses for vouchers valid in the user context.
|
||||||
DATA_VOUCHER_ADDRESSES
|
DATA_VOUCHER_ADDRESSES
|
||||||
// List of ordered voucher symbols in the user context.
|
// List of stable voucher symbols in the user context.
|
||||||
DATA_ORDERED_VOUCHER_SYMBOLS
|
DATA_STABLE_VOUCHER_SYMBOLS
|
||||||
// List of ordered voucher balances in the user context.
|
// List of stable voucher decimal counts for vouchers valid in the user context.
|
||||||
DATA_ORDERED_VOUCHER_BALANCES
|
DATA_STABLE_VOUCHER_BALANCES
|
||||||
// List of ordered voucher decimals in the user context.
|
// List of stable voucher decimal counts for vouchers valid in the user context.
|
||||||
DATA_ORDERED_VOUCHER_DECIMALS
|
DATA_STABLE_VOUCHER_DECIMALS
|
||||||
// List of ordered voucher EVM addresses in the user context.
|
// List of stable voucher EVM addresses for vouchers valid in the user context.
|
||||||
DATA_ORDERED_VOUCHER_ADDRESSES
|
DATA_STABLE_VOUCHER_ADDRESSES
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|||||||
@ -120,13 +120,13 @@ func GetVoucherData(ctx context.Context, store DataStore, sessionId string, inpu
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetOrderedVoucherData retrieves and matches ordered voucher data
|
// GetStableVoucherData retrieves and matches stable voucher data
|
||||||
func GetOrderedVoucherData(ctx context.Context, store DataStore, sessionId string, input string) (*dataserviceapi.TokenHoldings, error) {
|
func GetStableVoucherData(ctx context.Context, store DataStore, sessionId string, input string) (*dataserviceapi.TokenHoldings, error) {
|
||||||
keys := []storedb.DataTyp{
|
keys := []storedb.DataTyp{
|
||||||
storedb.DATA_ORDERED_VOUCHER_SYMBOLS,
|
storedb.DATA_STABLE_VOUCHER_SYMBOLS,
|
||||||
storedb.DATA_ORDERED_VOUCHER_BALANCES,
|
storedb.DATA_STABLE_VOUCHER_BALANCES,
|
||||||
storedb.DATA_ORDERED_VOUCHER_DECIMALS,
|
storedb.DATA_STABLE_VOUCHER_DECIMALS,
|
||||||
storedb.DATA_ORDERED_VOUCHER_ADDRESSES,
|
storedb.DATA_STABLE_VOUCHER_ADDRESSES,
|
||||||
}
|
}
|
||||||
data := make(map[storedb.DataTyp]string)
|
data := make(map[storedb.DataTyp]string)
|
||||||
|
|
||||||
@ -139,10 +139,10 @@ func GetOrderedVoucherData(ctx context.Context, store DataStore, sessionId strin
|
|||||||
}
|
}
|
||||||
|
|
||||||
symbol, balance, decimal, address := MatchVoucher(input,
|
symbol, balance, decimal, address := MatchVoucher(input,
|
||||||
data[storedb.DATA_ORDERED_VOUCHER_SYMBOLS],
|
data[storedb.DATA_STABLE_VOUCHER_SYMBOLS],
|
||||||
data[storedb.DATA_ORDERED_VOUCHER_BALANCES],
|
data[storedb.DATA_STABLE_VOUCHER_BALANCES],
|
||||||
data[storedb.DATA_ORDERED_VOUCHER_DECIMALS],
|
data[storedb.DATA_STABLE_VOUCHER_DECIMALS],
|
||||||
data[storedb.DATA_ORDERED_VOUCHER_ADDRESSES],
|
data[storedb.DATA_STABLE_VOUCHER_ADDRESSES],
|
||||||
)
|
)
|
||||||
|
|
||||||
if symbol == "" {
|
if symbol == "" {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user