|
|
|
|
@@ -4,6 +4,7 @@ import (
|
|
|
|
|
"context"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"math/big"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
@@ -344,8 +345,6 @@ func (h *MenuHandlers) MaxAmount(ctx context.Context, sym string, input []byte)
|
|
|
|
|
return res, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res.FlagSet = append(res.FlagSet, flag_swap_transaction)
|
|
|
|
|
|
|
|
|
|
// Resolve active pool address
|
|
|
|
|
activePoolAddress, err := h.resolveActivePoolAddress(ctx, sessionId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
@@ -380,8 +379,11 @@ func (h *MenuHandlers) MaxAmount(ctx context.Context, sym string, input []byte)
|
|
|
|
|
// retrieve the max credit send amounts
|
|
|
|
|
maxSAT, maxRAT, err := h.calculateSendCreditLimits(ctx, activePoolAddress, activeAddress, recipientActiveAddress, publicKey, activeDecimal, recipientActiveDecimal)
|
|
|
|
|
if err != nil {
|
|
|
|
|
res.FlagSet = append(res.FlagSet, flag_api_call_error)
|
|
|
|
|
// if an error (such as Swap rates not found for the speficied pool and tokens), fall back to a normal transaction
|
|
|
|
|
logg.ErrorCtxf(ctx, "failed on calculateSendCreditLimits", "error", err)
|
|
|
|
|
res.FlagReset = append(res.FlagReset, flag_swap_transaction)
|
|
|
|
|
res.Content = l.Get("Maximum amount: %s %s\nEnter amount:", formattedBalance, string(activeSym))
|
|
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -413,6 +415,9 @@ func (h *MenuHandlers) MaxAmount(ctx context.Context, sym string, input []byte)
|
|
|
|
|
return res, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// only set the flag once all checks pass
|
|
|
|
|
res.FlagSet = append(res.FlagSet, flag_swap_transaction)
|
|
|
|
|
|
|
|
|
|
res.Content = l.Get(
|
|
|
|
|
"Credit Available: %s %s\n(You can swap up to %s %s -> %s %s).\nEnter %s amount:",
|
|
|
|
|
maxRAT,
|
|
|
|
|
@@ -753,8 +758,19 @@ func (h *MenuHandlers) TransactionSwapPreview(ctx context.Context, sym string, i
|
|
|
|
|
return res, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// multiply by 1.015 (i.e. * 1015 / 1000)
|
|
|
|
|
amountInt, ok := new(big.Int).SetString(finalAmountStr, 10)
|
|
|
|
|
if !ok {
|
|
|
|
|
return res, fmt.Errorf("invalid amount: %s", finalAmountStr)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
amountInt.Mul(amountInt, big.NewInt(1015))
|
|
|
|
|
amountInt.Div(amountInt, big.NewInt(1000))
|
|
|
|
|
|
|
|
|
|
scaledFinalAmountStr := amountInt.String()
|
|
|
|
|
|
|
|
|
|
// call the credit send API to get the reverse quote
|
|
|
|
|
r, err := h.accountService.GetCreditSendReverseQuote(ctx, swapData.ActivePoolAddress, swapData.ActiveSwapFromAddress, swapData.ActiveSwapToAddress, finalAmountStr)
|
|
|
|
|
r, err := h.accountService.GetCreditSendReverseQuote(ctx, swapData.ActivePoolAddress, swapData.ActiveSwapFromAddress, swapData.ActiveSwapToAddress, scaledFinalAmountStr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
flag_api_call_error, _ := h.flagManager.GetFlag("flag_api_call_error")
|
|
|
|
|
res.FlagSet = append(res.FlagSet, flag_api_call_error)
|
|
|
|
|
@@ -766,23 +782,23 @@ func (h *MenuHandlers) TransactionSwapPreview(ctx context.Context, sym string, i
|
|
|
|
|
sendInputAmount := r.InputAmount // amount of SAT that should be swapped
|
|
|
|
|
sendOutputAmount := r.OutputAmount // amount of RAT that will be received
|
|
|
|
|
|
|
|
|
|
// store the sendOutputAmount as the final amount (that will be sent)
|
|
|
|
|
err = userStore.WriteEntry(ctx, sessionId, storedb.DATA_AMOUNT, []byte(sendOutputAmount))
|
|
|
|
|
// store the finalAmountStr as the final amount (that will be sent after the swap)
|
|
|
|
|
err = userStore.WriteEntry(ctx, sessionId, storedb.DATA_AMOUNT, []byte(finalAmountStr))
|
|
|
|
|
if err != nil {
|
|
|
|
|
logg.ErrorCtxf(ctx, "failed to write output amount value entry with", "key", storedb.DATA_AMOUNT, "value", sendOutputAmount, "error", err)
|
|
|
|
|
return res, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Scale down the quoted output amount
|
|
|
|
|
quoteAmountStr := store.ScaleDownBalance(sendOutputAmount, swapData.ActiveSwapToDecimal)
|
|
|
|
|
// quoteAmountStr := store.ScaleDownBalance(sendOutputAmount, swapData.ActiveSwapToDecimal)
|
|
|
|
|
|
|
|
|
|
// Format the qouteAmount amount to 2 decimal places
|
|
|
|
|
qouteAmount, _ := store.TruncateDecimalString(quoteAmountStr, 2)
|
|
|
|
|
// qouteAmount, _ := store.TruncateDecimalString(quoteAmountStr, 2)
|
|
|
|
|
|
|
|
|
|
// store the qouteAmount in the temporary value
|
|
|
|
|
err = userStore.WriteEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE, []byte(qouteAmount))
|
|
|
|
|
err = userStore.WriteEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE, []byte(inputStr))
|
|
|
|
|
if err != nil {
|
|
|
|
|
logg.ErrorCtxf(ctx, "failed to write temporary qouteAmount entry with", "key", storedb.DATA_TEMPORARY_VALUE, "value", qouteAmount, "error", err)
|
|
|
|
|
logg.ErrorCtxf(ctx, "failed to write temporary inputStr entry with", "key", storedb.DATA_TEMPORARY_VALUE, "value", inputStr, "error", err)
|
|
|
|
|
return res, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -795,7 +811,7 @@ func (h *MenuHandlers) TransactionSwapPreview(ctx context.Context, sym string, i
|
|
|
|
|
|
|
|
|
|
res.Content = l.Get(
|
|
|
|
|
"%s will receive %s %s",
|
|
|
|
|
string(recipientPhoneNumber), qouteAmount, swapData.ActiveSwapToSym,
|
|
|
|
|
string(recipientPhoneNumber), inputStr, swapData.ActiveSwapToSym,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
|
|