Compare commits

..

4 Commits

10 changed files with 109 additions and 37 deletions

View File

@ -60,14 +60,6 @@ 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 {
@ -78,6 +70,14 @@ 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,6 +160,18 @@ 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
@ -172,19 +184,17 @@ 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 { if err != nil || inputAmount > maxValue || inputAmount < 0.1 {
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(storedMax) finalAmountStr = string(payDebtVoucher.Balance)
} else { } else {
finalAmountStr, err = store.ParseAndScaleAmount(inputStr, swapData.ActiveSwapToDecimal) finalAmountStr, err = store.ParseAndScaleAmount(inputStr, payDebtVoucher.TokenDecimals)
if err != nil { if err != nil {
return res, err return res, err
} }
@ -195,15 +205,9 @@ 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, swapData.PublicKey, swapData.ActiveSwapToAddress, swapData.ActivePoolAddress, swapData.ActiveSwapFromAddress) r, err := h.accountService.GetPoolSwapQuote(ctx, finalAmountStr, string(publicKey), payDebtVoucher.TokenAddress, swapData.ActivePoolAddress, string(activeAddress))
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)
@ -218,9 +222,16 @@ 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, swapData.ActiveSwapToSym, qouteStr, swapData.ActiveSwapFromSym, inputStr, payDebtVoucher.TokenSymbol, qouteStr, string(activeSym),
) )
return res, nil return res, nil
@ -243,9 +254,21 @@ func (h *MenuHandlers) InitiatePayDebt(ctx context.Context, sym string, input []
userStore := h.userdataStore userStore := h.userdataStore
// Resolve active pool // Fetch session data
_, activePoolName, err := h.resolveActivePoolDetails(ctx, sessionId) _, _, activeSym, activeAddress, publicKey, _, err := h.getSessionData(ctx, sessionId)
if err != nil { if err != nil {
return res, nil
}
// Resolve active pool
activePoolAddress, activePoolName, err := h.resolveActivePoolDetails(ctx, sessionId)
if err != nil {
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 return res, err
} }
@ -263,7 +286,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, swapData.PublicKey, swapData.ActiveSwapToAddress, swapData.ActivePoolAddress, swapData.ActiveSwapFromAddress) r, err := h.accountService.PoolSwap(ctx, swapAmountStr, string(publicKey), payDebtVoucher.TokenAddress, string(activePoolAddress), string(activeAddress))
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)
@ -278,7 +301,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,
swapData.ActiveSwapToSym, string(activeSym),
activePoolName, activePoolName,
) )

View File

@ -0,0 +1 @@
{{.calculate_max_pay_debt}}

View File

@ -0,0 +1,10 @@
LOAD reset_transaction_amount 10
RELOAD reset_transaction_amount
MAP calculate_max_pay_debt
MOUT back 0
HALT
LOAD confirm_debt_removal 140
RELOAD confirm_debt_removal
CATCH invalid_pay_debt_amount flag_invalid_amount 1
INCMP _ 0
INCMP confirm_debt_removal *

View File

@ -1,7 +1,4 @@
LOAD confirm_debt_removal 0
MAP confirm_debt_removal MAP confirm_debt_removal
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

View File

@ -0,0 +1 @@
Amount {{.confirm_debt_removal}} is invalid, please try again:

View File

@ -0,0 +1,7 @@
MAP confirm_debt_removal
RELOAD reset_transaction_amount
MOUT retry 1
MOUT quit 9
HALT
INCMP _ 1
INCMP quit 9

View File

@ -0,0 +1 @@
Kiwango {{.confirm_debt_removal}} sio sahihi, tafadhali weka tena:

View File

@ -1 +1 @@
{{.calculate_max_pay_debt}} {{.get_vouchers}}

View File

@ -1,10 +1,16 @@
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 LOAD get_vouchers 0
MAP get_vouchers
MOUT back 0
MOUT quit 99
MNEXT next 88
MPREV prev 98
HALT
INCMP > 88
INCMP < 98
INCMP _ 0
INCMP quit 99
LOAD calculate_max_pay_debt 0 LOAD calculate_max_pay_debt 0
RELOAD calculate_max_pay_debt RELOAD calculate_max_pay_debt
MAP calculate_max_pay_debt CATCH . flag_incorrect_voucher 1
CATCH low_pay_debt_amount flag_low_swap_amount 1 INCMP calculate_max_pay_debt *
MOUT back 0
HALT
INCMP _ 0
INCMP confirm_debt_removal *

View File

@ -210,3 +210,29 @@ func UpdateSwapFromVoucherData(ctx context.Context, store DataStore, sessionId s
return nil return nil
} }
// ReadSwapFromVoucher retrieves the voucher being swapped into the pool (swap from)
func ReadSwapFromVoucher(ctx context.Context, store DataStore, sessionId string) (*dataserviceapi.TokenHoldings, error) {
keys := []storedb.DataTyp{
storedb.DATA_ACTIVE_SWAP_FROM_SYM,
storedb.DATA_ACTIVE_SWAP_FROM_DECIMAL,
storedb.DATA_ACTIVE_SWAP_FROM_ADDRESS,
storedb.DATA_ACTIVE_SWAP_FROM_BALANCE,
}
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_FROM_SYM],
Balance: data[storedb.DATA_ACTIVE_SWAP_FROM_BALANCE],
TokenDecimals: data[storedb.DATA_ACTIVE_SWAP_FROM_DECIMAL],
TokenAddress: data[storedb.DATA_ACTIVE_SWAP_FROM_ADDRESS],
}, nil
}