|
|
|
@ -28,20 +28,36 @@ 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 == "9" {
|
|
|
|
if inputStr == "0" || inputStr == "99" || inputStr == "88" || inputStr == "98" {
|
|
|
|
|
|
|
|
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
|
|
|
|
_, activeBal, _, activeAddress, publicKey, activeDecimal, err := h.getSessionData(ctx, sessionId)
|
|
|
|
_, _, _, _, publicKey, _, err := h.getSessionData(ctx, sessionId)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -83,8 +99,12 @@ 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(activeAddress) == string(recipientActiveAddress) {
|
|
|
|
if string(metadata.TokenAddress) == 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 {
|
|
|
|
@ -92,14 +112,15 @@ func (h *MenuHandlers) GetMpesaMaxLimit(ctx context.Context, sym string, input [
|
|
|
|
return res, err
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
activeFloat, _ := strconv.ParseFloat(string(activeBal), 64)
|
|
|
|
activeFloat, _ := strconv.ParseFloat(string(metadata.Balance), 64)
|
|
|
|
ksh := fmt.Sprintf("%f", activeFloat*rates.Buy)
|
|
|
|
ksh := fmt.Sprintf("%f", activeFloat*rates.Buy)
|
|
|
|
|
|
|
|
|
|
|
|
kshFormatted, _ := store.TruncateDecimalString(ksh, 0)
|
|
|
|
maxKshFormatted, _ := store.TruncateDecimalString(ksh, 0)
|
|
|
|
|
|
|
|
|
|
|
|
res.Content = l.Get(
|
|
|
|
res.Content = l.Get(
|
|
|
|
"Enter the amount of Mpesa to get: (Max %s Ksh)\n",
|
|
|
|
"Enter the amount of Mpesa to withdraw: (Min: Ksh %s, Max %s Ksh)\n",
|
|
|
|
kshFormatted,
|
|
|
|
minKshFormatted,
|
|
|
|
|
|
|
|
maxKshFormatted,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
return res, nil
|
|
|
|
@ -112,7 +133,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(activeAddress))
|
|
|
|
canSwap, err := h.accountService.CheckTokenInPool(ctx, string(activePoolAddress), string(metadata.TokenAddress))
|
|
|
|
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)
|
|
|
|
@ -125,7 +146,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, activeAddress, recipientActiveAddress, publicKey, activeDecimal, recipientActiveDecimal)
|
|
|
|
_, maxRAT, err := h.calculateSendCreditLimits(ctx, activePoolAddress, []byte(metadata.TokenAddress), recipientActiveAddress, publicKey, []byte(metadata.TokenDecimals), 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)
|
|
|
|
@ -155,25 +176,26 @@ 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
|
|
|
|
metadata := &dataserviceapi.TokenHoldings{
|
|
|
|
swapMetadata := &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, metadata); err != nil {
|
|
|
|
if err := store.UpdateSwapToVoucherData(ctx, userStore, sessionId, swapMetadata); 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)
|
|
|
|
kshFormatted, _ := store.TruncateDecimalString(kshStr, 0)
|
|
|
|
maxKshFormatted, _ := store.TruncateDecimalString(kshStr, 0)
|
|
|
|
|
|
|
|
|
|
|
|
res.Content = l.Get(
|
|
|
|
res.Content = l.Get(
|
|
|
|
"Enter the amount of Mpesa to get: (Max %s Ksh)\n",
|
|
|
|
"Enter the amount of Mpesa to withdraw: (Min: Ksh %s, Max %s Ksh)\n",
|
|
|
|
kshFormatted,
|
|
|
|
minKshFormatted,
|
|
|
|
|
|
|
|
maxKshFormatted,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
return res, nil
|
|
|
|
@ -189,7 +211,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 == "9" {
|
|
|
|
if inputStr == "0" || inputStr == "9" {
|
|
|
|
return res, nil
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@ -219,16 +241,18 @@ 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
|
|
|
|
@ -239,19 +263,21 @@ func (h *MenuHandlers) GetMpesaPreview(ctx context.Context, sym string, input []
|
|
|
|
return res, err
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if string(transactionType) == "normal" {
|
|
|
|
// get the selected voucher
|
|
|
|
activeBal, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_BAL)
|
|
|
|
mpesaWithdrawalVoucher, err := store.GetTemporaryVoucherData(ctx, h.userdataStore, sessionId)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
logg.ErrorCtxf(ctx, "failed to read activeBal entry with", "key", storedb.DATA_ACTIVE_BAL, "error", err)
|
|
|
|
logg.ErrorCtxf(ctx, "failed on GetTemporaryVoucherData", "error", err)
|
|
|
|
return res, 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 {
|
|
|
|
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 inputAmount > maxValue {
|
|
|
|
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
|
|
|
|
@ -270,7 +296,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, swapData.ActiveSwapFromSym, inputStr,
|
|
|
|
qouteInputAmount, mpesaWithdrawalVoucher.TokenSymbol, inputStr,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
return res, nil
|
|
|
|
@ -365,6 +391,12 @@ 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)
|
|
|
|
@ -372,12 +404,12 @@ func (h *MenuHandlers) InitiateGetMpesa(ctx context.Context, sym string, input [
|
|
|
|
return res, err
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
finalAmountStr, err := store.ParseAndScaleAmount(data.Amount, data.ActiveDecimal)
|
|
|
|
finalAmountStr, err := store.ParseAndScaleAmount(data.Amount, mpesaWithdrawalVoucher.TokenDecimals)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
tokenTransfer, err := h.accountService.TokenTransfer(ctx, finalAmountStr, data.PublicKey, mpesaAddress, data.ActiveAddress)
|
|
|
|
tokenTransfer, err := h.accountService.TokenTransfer(ctx, finalAmountStr, data.PublicKey, mpesaAddress, mpesaWithdrawalVoucher.TokenAddress)
|
|
|
|
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.")
|
|
|
|
@ -416,11 +448,6 @@ 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
|
|
|
|
@ -437,7 +464,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. You will receive ~ %s ksh", finalKshStr)
|
|
|
|
res.Content = l.Get("Your request has been sent. Please await confirmation")
|
|
|
|
res.FlagReset = append(res.FlagReset, flag_account_authorized)
|
|
|
|
res.FlagReset = append(res.FlagReset, flag_account_authorized)
|
|
|
|
return res, nil
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|