Compare commits

..

No commits in common. "fe534b118165ea78223eb72b6ea8488bc2dfe97b" and "09954d967f6721c8a30ecb1c3c8d66d583063b97" have entirely different histories.

2 changed files with 26 additions and 83 deletions

View File

@ -50,7 +50,7 @@ func (h *MenuHandlers) GetMpesaMaxLimit(ctx context.Context, sym string, input [
return res, nil return res, nil
} }
// Store the active transaction voucher data (from token) // Store the active transaction voucher data
if err := store.StoreTemporaryVoucher(ctx, h.userdataStore, sessionId, metadata); err != nil { if err := store.StoreTemporaryVoucher(ctx, h.userdataStore, sessionId, metadata); err != nil {
logg.ErrorCtxf(ctx, "failed on StoreTemporaryVoucher", "error", err) logg.ErrorCtxf(ctx, "failed on StoreTemporaryVoucher", "error", err)
return res, err return res, err
@ -93,7 +93,7 @@ func (h *MenuHandlers) GetMpesaMaxLimit(ctx context.Context, sym string, input [
return res, err return res, err
} }
// fetch data for verification (to_voucher data) // fetch data for verification
recipientActiveSym, recipientActiveAddress, recipientActiveDecimal, err := h.getRecipientData(ctx, string(recipientPhoneNumber)) recipientActiveSym, recipientActiveAddress, recipientActiveDecimal, err := h.getRecipientData(ctx, string(recipientPhoneNumber))
if err != nil { if err != nil {
return res, err return res, err
@ -103,7 +103,7 @@ func (h *MenuHandlers) GetMpesaMaxLimit(ctx context.Context, sym string, input [
minksh := fmt.Sprintf("%f", config.MinMpesaWithdrawAmount()) minksh := fmt.Sprintf("%f", config.MinMpesaWithdrawAmount())
minKshFormatted, _ := store.TruncateDecimalString(minksh, 0) minKshFormatted, _ := store.TruncateDecimalString(minksh, 0)
// If SAT is the same as RAT, 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(metadata.TokenAddress) == string(recipientActiveAddress) {
txType = "normal" txType = "normal"
// Save the transaction type // Save the transaction type
@ -132,7 +132,7 @@ func (h *MenuHandlers) GetMpesaMaxLimit(ctx context.Context, sym string, input [
return res, err return res, err
} }
// Check if selected 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(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)
@ -140,7 +140,7 @@ func (h *MenuHandlers) GetMpesaMaxLimit(ctx context.Context, sym string, input [
return res, nil return res, nil
} }
if !canSwap.CanSwapFrom { // pool issue (CATCH on .vis) if !canSwap.CanSwapFrom { // pool issue (TODO on vis)
res.FlagSet = append(res.FlagSet, flag_incorrect_pool) res.FlagSet = append(res.FlagSet, flag_incorrect_pool)
return res, nil return res, nil
} }
@ -175,7 +175,7 @@ func (h *MenuHandlers) GetMpesaMaxLimit(ctx context.Context, sym string, input [
return res, err return res, err
} }
// save swap related data for the swap preview (the swap to) // save swap related data for the swap preview
swapMetadata := &dataserviceapi.TokenHoldings{ swapMetadata := &dataserviceapi.TokenHoldings{
TokenAddress: string(recipientActiveAddress), TokenAddress: string(recipientActiveAddress),
TokenSymbol: string(recipientActiveSym), TokenSymbol: string(recipientActiveSym),
@ -253,8 +253,7 @@ func (h *MenuHandlers) GetMpesaPreview(ctx context.Context, sym string, input []
// divide by the buy rate // divide by the buy rate
inputAmount := kshAmount / rates.Buy inputAmount := kshAmount / rates.Buy
// Resolve active pool swapData, err := store.ReadSwapPreviewData(ctx, userStore, sessionId)
activePoolAddress, _, err := h.resolveActivePoolDetails(ctx, sessionId)
if err != nil { if err != nil {
return res, err return res, err
} }
@ -271,13 +270,13 @@ func (h *MenuHandlers) GetMpesaPreview(ctx context.Context, sym string, input []
return res, err return res, err
} }
if string(transactionType) == "normal" {
// get the max based on the selected voucher balance
maxValue, err := strconv.ParseFloat(mpesaWithdrawalVoucher.Balance, 64) maxValue, err := strconv.ParseFloat(mpesaWithdrawalVoucher.Balance, 64)
if err != nil { if err != nil {
logg.ErrorCtxf(ctx, "Failed to convert the stored balance string to a float", "error", err) logg.ErrorCtxf(ctx, "Failed to convert the swapMaxAmount to a float", "error", err)
return res, err return res, err
} }
if string(transactionType) == "normal" {
if inputAmount > maxValue { 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
@ -303,20 +302,8 @@ func (h *MenuHandlers) GetMpesaPreview(ctx context.Context, sym string, input []
return res, nil return res, nil
} }
swapToVoucher, err := store.ReadSwapToVoucher(ctx, h.userdataStore, sessionId)
if err != nil {
logg.ErrorCtxf(ctx, "failed on ReadSwapFromVoucher", "error", err)
return res, err
}
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
}
// use the stored max RAT // use the stored max RAT
maxRATValue, err := strconv.ParseFloat(string(swapMaxAmount), 64) maxRATValue, err := strconv.ParseFloat(swapData.ActiveSwapMaxAmount, 64)
if err != nil { if err != nil {
logg.ErrorCtxf(ctx, "Failed to convert the swapMaxAmount to a float", "error", err) logg.ErrorCtxf(ctx, "Failed to convert the swapMaxAmount to a float", "error", err)
return res, err return res, err
@ -328,21 +315,15 @@ func (h *MenuHandlers) GetMpesaPreview(ctx context.Context, sym string, input []
return res, nil return res, nil
} }
// Format the amount to 2 decimal places formattedAmount := fmt.Sprintf("%f", inputAmount)
formattedAmount, err := store.TruncateDecimalString(fmt.Sprintf("%f", inputAmount), 2)
if err != nil {
res.FlagSet = append(res.FlagSet, flag_invalid_amount)
res.Content = inputStr
return res, nil
}
finalAmountStr, err := store.ParseAndScaleAmount(formattedAmount, swapToVoucher.TokenDecimals) finalAmountStr, err := store.ParseAndScaleAmount(formattedAmount, swapData.ActiveSwapToDecimal)
if err != nil { if err != nil {
return res, err return res, err
} }
// call the credit send API to get the reverse quote // call the credit send API to get the reverse quote
r, err := h.accountService.GetCreditSendReverseQuote(ctx, string(activePoolAddress), mpesaWithdrawalVoucher.TokenAddress, swapToVoucher.TokenAddress, finalAmountStr) r, err := h.accountService.GetCreditSendReverseQuote(ctx, swapData.ActivePoolAddress, swapData.ActiveSwapFromAddress, swapData.ActiveSwapToAddress, finalAmountStr)
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.")
@ -368,13 +349,13 @@ func (h *MenuHandlers) GetMpesaPreview(ctx context.Context, sym string, input []
} }
// covert for display // covert for display
quoteInputStr := store.ScaleDownBalance(sendInputAmount, mpesaWithdrawalVoucher.TokenDecimals) quoteInputStr := store.ScaleDownBalance(sendInputAmount, swapData.ActiveSwapFromDecimal)
// Format the quoteInputStr amount to 2 decimal places // Format the quoteInputStr amount to 2 decimal places
qouteInputAmount, _ := store.TruncateDecimalString(quoteInputStr, 2) qouteInputAmount, _ := store.TruncateDecimalString(quoteInputStr, 2)
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
@ -400,12 +381,16 @@ func (h *MenuHandlers) InitiateGetMpesa(ctx context.Context, sym string, input [
mpesaAddress := config.DefaultMpesaAddress() mpesaAddress := config.DefaultMpesaAddress()
swapData, err := store.ReadSwapPreviewData(ctx, userStore, sessionId)
if err != nil {
return res, err
}
transactionType, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_SEND_TRANSACTION_TYPE) transactionType, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_SEND_TRANSACTION_TYPE)
if err != nil { if err != nil {
return res, err return res, err
} }
// get the selected voucher
mpesaWithdrawalVoucher, err := store.GetTemporaryVoucherData(ctx, h.userdataStore, sessionId) mpesaWithdrawalVoucher, err := store.GetTemporaryVoucherData(ctx, h.userdataStore, sessionId)
if err != nil { if err != nil {
logg.ErrorCtxf(ctx, "failed on GetTemporaryVoucherData", "error", err) logg.ErrorCtxf(ctx, "failed on GetTemporaryVoucherData", "error", err)
@ -440,24 +425,6 @@ func (h *MenuHandlers) InitiateGetMpesa(ctx context.Context, sym string, input [
return res, nil return res, nil
} }
publicKey, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY)
if err != nil {
logg.ErrorCtxf(ctx, "failed to read publicKey entry", "key", storedb.DATA_PUBLIC_KEY, "error", err)
return res, err
}
swapToVoucher, err := store.ReadSwapToVoucher(ctx, h.userdataStore, sessionId)
if err != nil {
logg.ErrorCtxf(ctx, "failed on ReadSwapFromVoucher", "error", err)
return res, err
}
// Resolve active pool
activePoolAddress, _, err := h.resolveActivePoolDetails(ctx, sessionId)
if err != nil {
return res, err
}
swapAmount, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_SWAP_AMOUNT) swapAmount, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_SWAP_AMOUNT)
if err != nil { if err != nil {
logg.ErrorCtxf(ctx, "failed to read swapAmount entry with", "key", storedb.DATA_ACTIVE_SWAP_AMOUNT, "error", err) logg.ErrorCtxf(ctx, "failed to read swapAmount entry with", "key", storedb.DATA_ACTIVE_SWAP_AMOUNT, "error", err)
@ -467,7 +434,7 @@ func (h *MenuHandlers) InitiateGetMpesa(ctx context.Context, sym string, input [
swapAmountStr := string(swapAmount) swapAmountStr := string(swapAmount)
// Call the poolSwap API // Call the poolSwap API
poolSwap, err := h.accountService.PoolSwap(ctx, swapAmountStr, string(publicKey), mpesaWithdrawalVoucher.TokenAddress, string(activePoolAddress), swapToVoucher.TokenAddress) poolSwap, err := h.accountService.PoolSwap(ctx, swapAmountStr, swapData.PublicKey, swapData.ActiveSwapFromAddress, swapData.ActivePoolAddress, swapData.ActiveSwapToAddress)
if err != nil { if err != nil {
flag_api_call_error, _ := h.flagManager.GetFlag("flag_api_call_error") flag_api_call_error, _ := h.flagManager.GetFlag("flag_api_call_error")
res.FlagSet = append(res.FlagSet, flag_api_call_error) res.FlagSet = append(res.FlagSet, flag_api_call_error)
@ -487,7 +454,7 @@ func (h *MenuHandlers) InitiateGetMpesa(ctx context.Context, sym string, input [
} }
// Initiate a send to mpesa after the swap // Initiate a send to mpesa after the swap
tokenTransfer, err := h.accountService.TokenTransfer(ctx, string(amount),string(publicKey), mpesaAddress, swapToVoucher.TokenAddress) tokenTransfer, err := h.accountService.TokenTransfer(ctx, string(amount), swapData.PublicKey, mpesaAddress, swapData.ActiveSwapToAddress)
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.")

View File

@ -236,27 +236,3 @@ func ReadSwapFromVoucher(ctx context.Context, store DataStore, sessionId string)
TokenAddress: data[storedb.DATA_ACTIVE_SWAP_FROM_ADDRESS], TokenAddress: data[storedb.DATA_ACTIVE_SWAP_FROM_ADDRESS],
}, nil }, nil
} }
// ReadSwapToVoucher retrieves the swap to voucher being swapped from the pool
func ReadSwapToVoucher(ctx context.Context, store DataStore, sessionId string) (*dataserviceapi.TokenHoldings, error) {
keys := []storedb.DataTyp{
storedb.DATA_ACTIVE_SWAP_TO_SYM,
storedb.DATA_ACTIVE_SWAP_TO_DECIMAL,
storedb.DATA_ACTIVE_SWAP_TO_ADDRESS,
}
data := make(map[storedb.DataTyp]string)
for _, key := range keys {
value, err := store.ReadEntry(ctx, sessionId, key)
if err != nil {
return nil, fmt.Errorf("failed to get data key %x: %v", key, err)
}
data[key] = string(value)
}
return &dataserviceapi.TokenHoldings{
TokenSymbol: data[storedb.DATA_ACTIVE_SWAP_TO_SYM],
TokenDecimals: data[storedb.DATA_ACTIVE_SWAP_TO_DECIMAL],
TokenAddress: data[storedb.DATA_ACTIVE_SWAP_TO_ADDRESS],
}, nil
}