|
|
|
@ -60,6 +60,14 @@ func (h *MenuHandlers) CalculateMaxPayDebt(ctx context.Context, sym string, inpu
|
|
|
|
return res, nil
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logg.InfoCtxf(ctx, "Metadata from GetVoucherData:", "metadata", metadata)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Store the active swap from data
|
|
|
|
|
|
|
|
if err := store.UpdateSwapFromVoucherData(ctx, userStore, sessionId, metadata); err != nil {
|
|
|
|
|
|
|
|
logg.ErrorCtxf(ctx, "failed on UpdateSwapFromVoucherData", "error", err)
|
|
|
|
|
|
|
|
return res, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Get the max swap limit with the selected voucher
|
|
|
|
// Get the max swap limit with the selected voucher
|
|
|
|
r, err := h.accountService.GetSwapFromTokenMaxLimit(ctx, string(activePoolAddress), metadata.TokenAddress, string(activeAddress), string(publicKey))
|
|
|
|
r, err := h.accountService.GetSwapFromTokenMaxLimit(ctx, string(activePoolAddress), metadata.TokenAddress, string(activeAddress), string(publicKey))
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
@ -70,14 +78,6 @@ func (h *MenuHandlers) CalculateMaxPayDebt(ctx context.Context, sym string, inpu
|
|
|
|
|
|
|
|
|
|
|
|
maxLimit := r.Max
|
|
|
|
maxLimit := r.Max
|
|
|
|
|
|
|
|
|
|
|
|
metadata.Balance = maxLimit
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Store the active swap from data
|
|
|
|
|
|
|
|
if err := store.UpdateSwapFromVoucherData(ctx, userStore, sessionId, metadata); err != nil {
|
|
|
|
|
|
|
|
logg.ErrorCtxf(ctx, "failed on UpdateSwapFromVoucherData", "error", err)
|
|
|
|
|
|
|
|
return res, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Scale down the amount
|
|
|
|
// Scale down the amount
|
|
|
|
maxAmountStr := store.ScaleDownBalance(maxLimit, metadata.TokenDecimals)
|
|
|
|
maxAmountStr := store.ScaleDownBalance(maxLimit, metadata.TokenDecimals)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
@ -160,18 +160,6 @@ func (h *MenuHandlers) ConfirmDebtRemoval(ctx context.Context, sym string, input
|
|
|
|
|
|
|
|
|
|
|
|
userStore := h.userdataStore
|
|
|
|
userStore := h.userdataStore
|
|
|
|
|
|
|
|
|
|
|
|
// Fetch session data
|
|
|
|
|
|
|
|
_, _, activeSym, activeAddress, publicKey, _, err := h.getSessionData(ctx, sessionId)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
payDebtVoucher, err := store.ReadSwapFromVoucher(ctx, h.userdataStore, sessionId)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
logg.ErrorCtxf(ctx, "failed on ReadSwapFromVoucher", "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
|
|
|
|
@ -184,17 +172,19 @@ func (h *MenuHandlers) ConfirmDebtRemoval(ctx context.Context, sym string, input
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inputAmount, err := strconv.ParseFloat(inputStr, 64)
|
|
|
|
inputAmount, err := strconv.ParseFloat(inputStr, 64)
|
|
|
|
if err != nil || inputAmount > maxValue || inputAmount < 0.1 {
|
|
|
|
if err != nil || 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
storedMax, _ := userStore.ReadEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE)
|
|
|
|
|
|
|
|
|
|
|
|
var finalAmountStr string
|
|
|
|
var finalAmountStr string
|
|
|
|
if inputStr == swapData.ActiveSwapMaxAmount {
|
|
|
|
if inputStr == swapData.ActiveSwapMaxAmount {
|
|
|
|
finalAmountStr = string(payDebtVoucher.Balance)
|
|
|
|
finalAmountStr = string(storedMax)
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
finalAmountStr, err = store.ParseAndScaleAmount(inputStr, payDebtVoucher.TokenDecimals)
|
|
|
|
finalAmountStr, err = store.ParseAndScaleAmount(inputStr, swapData.ActiveSwapToDecimal)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -205,9 +195,15 @@ func (h *MenuHandlers) ConfirmDebtRemoval(ctx context.Context, sym string, input
|
|
|
|
logg.ErrorCtxf(ctx, "failed to write swap amount entry with", "key", storedb.DATA_ACTIVE_SWAP_AMOUNT, "value", finalAmountStr, "error", err)
|
|
|
|
logg.ErrorCtxf(ctx, "failed to write swap amount entry with", "key", storedb.DATA_ACTIVE_SWAP_AMOUNT, "value", finalAmountStr, "error", err)
|
|
|
|
return res, err
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// store the user's 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 inputStr amount entry with", "key", storedb.DATA_TEMPORARY_VALUE, "value", inputStr, "error", err)
|
|
|
|
|
|
|
|
return res, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// call the API to get the quote
|
|
|
|
// 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, swapData.PublicKey, swapData.ActiveSwapToAddress, swapData.ActivePoolAddress, swapData.ActiveSwapFromAddress)
|
|
|
|
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)
|
|
|
|
@ -222,16 +218,9 @@ func (h *MenuHandlers) ConfirmDebtRemoval(ctx context.Context, sym string, input
|
|
|
|
// Format to 2 decimal places
|
|
|
|
// Format to 2 decimal places
|
|
|
|
qouteStr, _ := store.TruncateDecimalString(string(quoteAmountStr), 2)
|
|
|
|
qouteStr, _ := store.TruncateDecimalString(string(quoteAmountStr), 2)
|
|
|
|
|
|
|
|
|
|
|
|
// store the quote in the temporary value key
|
|
|
|
|
|
|
|
err = userStore.WriteEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE, []byte(qouteStr))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
logg.ErrorCtxf(ctx, "failed to write swap max amount entry with", "key", storedb.DATA_TEMPORARY_VALUE, "value", qouteStr, "error", err)
|
|
|
|
|
|
|
|
return res, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
res.Content = l.Get(
|
|
|
|
res.Content = l.Get(
|
|
|
|
"Please confirm that you will use %s %s to remove your debt of %s %s\n",
|
|
|
|
"Please confirm that you will use %s %s to remove your debt of %s %s\n",
|
|
|
|
inputStr, payDebtVoucher.TokenSymbol, qouteStr, string(activeSym),
|
|
|
|
inputStr, swapData.ActiveSwapToSym, qouteStr, swapData.ActiveSwapFromSym,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
return res, nil
|
|
|
|
@ -254,24 +243,12 @@ func (h *MenuHandlers) InitiatePayDebt(ctx context.Context, sym string, input []
|
|
|
|
|
|
|
|
|
|
|
|
userStore := h.userdataStore
|
|
|
|
userStore := h.userdataStore
|
|
|
|
|
|
|
|
|
|
|
|
// Fetch session data
|
|
|
|
|
|
|
|
_, _, activeSym, activeAddress, publicKey, _, err := h.getSessionData(ctx, sessionId)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Resolve active pool
|
|
|
|
// Resolve active pool
|
|
|
|
activePoolAddress, activePoolName, err := h.resolveActivePoolDetails(ctx, sessionId)
|
|
|
|
_, activePoolName, err := h.resolveActivePoolDetails(ctx, sessionId)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
payDebtVoucher, err := store.ReadSwapFromVoucher(ctx, h.userdataStore, sessionId)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
logg.ErrorCtxf(ctx, "failed on ReadSwapFromVoucher", "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
|
|
|
|
@ -286,7 +263,7 @@ func (h *MenuHandlers) InitiatePayDebt(ctx context.Context, sym string, input []
|
|
|
|
swapAmountStr := string(swapAmount)
|
|
|
|
swapAmountStr := string(swapAmount)
|
|
|
|
|
|
|
|
|
|
|
|
// Call the poolSwap API
|
|
|
|
// Call the poolSwap API
|
|
|
|
r, err := h.accountService.PoolSwap(ctx, swapAmountStr, string(publicKey), payDebtVoucher.TokenAddress, string(activePoolAddress), string(activeAddress))
|
|
|
|
r, err := h.accountService.PoolSwap(ctx, swapAmountStr, swapData.PublicKey, swapData.ActiveSwapToAddress, swapData.ActivePoolAddress, swapData.ActiveSwapFromAddress)
|
|
|
|
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)
|
|
|
|
@ -301,7 +278,7 @@ func (h *MenuHandlers) InitiatePayDebt(ctx context.Context, sym string, input []
|
|
|
|
res.Content = l.Get(
|
|
|
|
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.",
|
|
|
|
"Your request has been sent. You will receive an SMS when your debt of %s %s has been removed from %s.",
|
|
|
|
swapData.TemporaryValue,
|
|
|
|
swapData.TemporaryValue,
|
|
|
|
string(activeSym),
|
|
|
|
swapData.ActiveSwapToSym,
|
|
|
|
activePoolName,
|
|
|
|
activePoolName,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|