Compare commits

..

No commits in common. "43b963995f18baf8181d74875af52c16784cd93d" and "5c1b4ab00206e72957ce066e4ec56267cf4e8d68" have entirely different histories.

10 changed files with 36 additions and 108 deletions

View File

@ -60,6 +60,14 @@ func (h *MenuHandlers) CalculateMaxPayDebt(ctx context.Context, sym string, inpu
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
r, err := h.accountService.GetSwapFromTokenMaxLimit(ctx, string(activePoolAddress), metadata.TokenAddress, string(activeAddress), string(publicKey))
if err != nil {
@ -70,14 +78,6 @@ func (h *MenuHandlers) CalculateMaxPayDebt(ctx context.Context, sym string, inpu
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
maxAmountStr := store.ScaleDownBalance(maxLimit, metadata.TokenDecimals)
if err != nil {
@ -160,18 +160,6 @@ func (h *MenuHandlers) ConfirmDebtRemoval(ctx context.Context, sym string, input
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)
if err != nil {
return res, err
@ -184,17 +172,19 @@ func (h *MenuHandlers) ConfirmDebtRemoval(ctx context.Context, sym string, input
}
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.Content = inputStr
return res, nil
}
storedMax, _ := userStore.ReadEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE)
var finalAmountStr string
if inputStr == swapData.ActiveSwapMaxAmount {
finalAmountStr = string(payDebtVoucher.Balance)
finalAmountStr = string(storedMax)
} else {
finalAmountStr, err = store.ParseAndScaleAmount(inputStr, payDebtVoucher.TokenDecimals)
finalAmountStr, err = store.ParseAndScaleAmount(inputStr, swapData.ActiveSwapToDecimal)
if err != nil {
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)
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
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 {
flag_api_call_error, _ := h.flagManager.GetFlag("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
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(
"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
@ -254,24 +243,12 @@ func (h *MenuHandlers) InitiatePayDebt(ctx context.Context, sym string, input []
userStore := h.userdataStore
// Fetch session data
_, _, activeSym, activeAddress, publicKey, _, err := h.getSessionData(ctx, sessionId)
if err != nil {
return res, nil
}
// Resolve active pool
activePoolAddress, activePoolName, err := h.resolveActivePoolDetails(ctx, sessionId)
_, 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
}
swapData, err := store.ReadSwapPreviewData(ctx, userStore, sessionId)
if err != nil {
return res, err
@ -286,7 +263,7 @@ func (h *MenuHandlers) InitiatePayDebt(ctx context.Context, sym string, input []
swapAmountStr := string(swapAmount)
// 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 {
flag_api_call_error, _ := h.flagManager.GetFlag("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(
"Your request has been sent. You will receive an SMS when your debt of %s %s has been removed from %s.",
swapData.TemporaryValue,
string(activeSym),
swapData.ActiveSwapToSym,
activePoolName,
)

View File

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

View File

@ -1,10 +0,0 @@
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,4 +1,7 @@
LOAD confirm_debt_removal 0
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 quit 9
HALT

View File

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

View File

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

View File

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

View File

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

View File

@ -1,16 +1,10 @@
CATCH no_voucher flag_no_active_voucher 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
CATCH no_stable_voucher flag_no_stable_vouchers 1
LOAD calculate_max_pay_debt 0
RELOAD calculate_max_pay_debt
CATCH . flag_incorrect_voucher 1
INCMP calculate_max_pay_debt *
MAP calculate_max_pay_debt
CATCH low_pay_debt_amount flag_low_swap_amount 1
MOUT back 0
HALT
INCMP _ 0
INCMP confirm_debt_removal *

View File

@ -209,30 +209,4 @@ func UpdateSwapFromVoucherData(ctx context.Context, store DataStore, sessionId s
}
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
}
}