Compare commits
4 Commits
96ba48bcba
...
f869ff437e
| Author | SHA1 | Date | |
|---|---|---|---|
| f869ff437e | |||
| d8a6535c6f | |||
| 108d5bdc3e | |||
| 2d6e7e81dd |
@ -203,11 +203,11 @@ func (h *MenuHandlers) determineAndSaveTransactionType(
|
||||
publicKey []byte,
|
||||
recipientPhoneNumber []byte,
|
||||
) error {
|
||||
store := h.userdataStore
|
||||
userStore := h.userdataStore
|
||||
txType := "swap"
|
||||
|
||||
// Read sender's active address
|
||||
senderActiveAddress, err := store.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_ADDRESS)
|
||||
senderActiveAddress, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_ADDRESS)
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "Failed to read sender active address", "error", err)
|
||||
return err
|
||||
@ -215,7 +215,7 @@ func (h *MenuHandlers) determineAndSaveTransactionType(
|
||||
|
||||
var recipientActiveAddress []byte
|
||||
if recipientPhoneNumber != nil {
|
||||
recipientActiveAddress, _ = store.ReadEntry(ctx, string(recipientPhoneNumber), storedb.DATA_ACTIVE_ADDRESS)
|
||||
recipientActiveAddress, _ = userStore.ReadEntry(ctx, string(recipientPhoneNumber), storedb.DATA_ACTIVE_ADDRESS)
|
||||
}
|
||||
|
||||
// recipient has no active token → normal transaction
|
||||
@ -227,17 +227,34 @@ func (h *MenuHandlers) determineAndSaveTransactionType(
|
||||
}
|
||||
|
||||
// Save the transaction type
|
||||
if err := store.WriteEntry(ctx, sessionId, storedb.DATA_SEND_TRANSACTION_TYPE, []byte(txType)); err != nil {
|
||||
if err := userStore.WriteEntry(ctx, sessionId, storedb.DATA_SEND_TRANSACTION_TYPE, []byte(txType)); err != nil {
|
||||
logg.ErrorCtxf(ctx, "Failed to write transaction type", "type", txType, "error", err)
|
||||
return err
|
||||
}
|
||||
|
||||
// Save the recipient's phone number only if it exists
|
||||
if recipientPhoneNumber != nil {
|
||||
if err := store.WriteEntry(ctx, sessionId, storedb.DATA_RECIPIENT_PHONE_NUMBER, recipientPhoneNumber); err != nil {
|
||||
if err := userStore.WriteEntry(ctx, sessionId, storedb.DATA_RECIPIENT_PHONE_NUMBER, recipientPhoneNumber); err != nil {
|
||||
logg.ErrorCtxf(ctx, "Failed to write recipient phone number", "type", txType, "error", err)
|
||||
return err
|
||||
}
|
||||
|
||||
// fetch data for use (to_voucher data)
|
||||
recipientActiveSym, recipientActiveAddress, recipientActiveDecimal, err := h.getRecipientData(ctx, string(recipientPhoneNumber))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
swapMetadata := &dataserviceapi.TokenHoldings{
|
||||
TokenAddress: string(recipientActiveAddress),
|
||||
TokenSymbol: string(recipientActiveSym),
|
||||
TokenDecimals: string(recipientActiveDecimal),
|
||||
}
|
||||
|
||||
// Store the active swap_to data
|
||||
if err := store.UpdateSwapToVoucherData(ctx, userStore, sessionId, swapMetadata); err != nil {
|
||||
logg.ErrorCtxf(ctx, "failed on UpdateSwapToVoucherData", "error", err)
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
logg.InfoCtxf(ctx, "No recipient phone number found for public key", "publicKey", string(publicKey))
|
||||
}
|
||||
@ -309,6 +326,7 @@ func (h *MenuHandlers) ResetTransactionAmount(ctx context.Context, sym string, i
|
||||
}
|
||||
|
||||
// MaxAmount checks the transaction type to determine the displayed max amount.
|
||||
// Checks whether the user selected a custom voucher
|
||||
// If the transaction type is "swap", it checks the max swappable amount and sets this as the content.
|
||||
// If the transaction type is "normal", gets the current sender's balance from the store and sets it as
|
||||
// the result content.
|
||||
@ -334,9 +352,41 @@ func (h *MenuHandlers) MaxAmount(ctx context.Context, sym string, input []byte)
|
||||
return res, err
|
||||
}
|
||||
|
||||
customVoucherSelection, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_TRANSACTION_CUSTOM_VOUCHER)
|
||||
if err == nil {
|
||||
customVoucherValue, _ := strconv.ParseUint(string(customVoucherSelection), 0, 64)
|
||||
if customVoucherValue == 1 {
|
||||
// use the custom voucher
|
||||
customTransactionVoucher, err := store.GetTemporaryVoucherData(ctx, h.userdataStore, sessionId)
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "failed on GetTemporaryVoucherData", "error", err)
|
||||
return res, err
|
||||
}
|
||||
|
||||
activeSym = []byte(customTransactionVoucher.TokenSymbol)
|
||||
activeBal = []byte(customTransactionVoucher.Balance)
|
||||
activeDecimal = []byte(customTransactionVoucher.TokenDecimals)
|
||||
activeAddress = []byte(customTransactionVoucher.TokenAddress)
|
||||
}
|
||||
}
|
||||
|
||||
// Format the active balance amount to 2 decimal places
|
||||
formattedBalance, _ := store.TruncateDecimalString(string(activeBal), 2)
|
||||
|
||||
// confirm the transaction type
|
||||
swapToVoucher, err := store.ReadSwapToVoucher(ctx, h.userdataStore, sessionId)
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "failed on ReadSwapFromVoucher", "error", err)
|
||||
return res, err
|
||||
}
|
||||
|
||||
if string(swapToVoucher.TokenAddress) == string(activeAddress) {
|
||||
// recipient has active token same as selected token → normal transaction
|
||||
transactionType = []byte("normal")
|
||||
} else {
|
||||
transactionType = []byte("swap")
|
||||
}
|
||||
|
||||
// If normal transaction return balance
|
||||
if string(transactionType) == "normal" {
|
||||
res.FlagReset = append(res.FlagReset, flag_swap_transaction)
|
||||
@ -939,10 +989,11 @@ func (h *MenuHandlers) ClearTransactionTypeFlag(ctx context.Context, sym string,
|
||||
var res resource.Result
|
||||
|
||||
flag_swap_transaction, _ := h.flagManager.GetFlag("flag_swap_transaction")
|
||||
flag_multiple_voucher, _ := h.flagManager.GetFlag("flag_multiple_voucher")
|
||||
|
||||
inputStr := string(input)
|
||||
if inputStr == "0" {
|
||||
res.FlagReset = append(res.FlagReset, flag_swap_transaction)
|
||||
res.FlagReset = append(res.FlagReset, flag_swap_transaction, flag_multiple_voucher)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
|
||||
@ -33,7 +33,7 @@ func (h *MenuHandlers) ManageVouchers(ctx context.Context, sym string, input []b
|
||||
flag_no_active_voucher, _ := h.flagManager.GetFlag("flag_no_active_voucher")
|
||||
flag_api_error, _ := h.flagManager.GetFlag("flag_api_call_error")
|
||||
flag_no_stable_vouchers, _ := h.flagManager.GetFlag("flag_no_stable_vouchers")
|
||||
flag_single_voucher, _ := h.flagManager.GetFlag("flag_single_voucher")
|
||||
flag_multiple_voucher, _ := h.flagManager.GetFlag("flag_multiple_voucher")
|
||||
|
||||
publicKey, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY)
|
||||
if err != nil {
|
||||
@ -76,9 +76,9 @@ func (h *MenuHandlers) ManageVouchers(ctx context.Context, sym string, input []b
|
||||
|
||||
// only set the flag if the user has a single voucher
|
||||
if len(vouchersResp) == 1 {
|
||||
res.FlagSet = append(res.FlagSet, flag_single_voucher)
|
||||
res.FlagReset = append(res.FlagReset, flag_multiple_voucher)
|
||||
} else {
|
||||
res.FlagReset = append(res.FlagReset, flag_single_voucher)
|
||||
res.FlagSet = append(res.FlagSet, flag_multiple_voucher)
|
||||
}
|
||||
|
||||
res.FlagReset = append(res.FlagReset, flag_no_active_voucher)
|
||||
@ -412,3 +412,50 @@ func (h *MenuHandlers) GetVoucherDetails(ctx context.Context, sym string, input
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// ValidateCreditVoucher sets the selected voucher as the active transaction voucher
|
||||
func (h *MenuHandlers) ValidateCreditVoucher(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
var res resource.Result
|
||||
sessionId, ok := ctx.Value("SessionId").(string)
|
||||
if !ok {
|
||||
return res, fmt.Errorf("missing session")
|
||||
}
|
||||
|
||||
code := codeFromCtx(ctx)
|
||||
l := gotext.NewLocale(translationDir, code)
|
||||
l.AddDomain("default")
|
||||
|
||||
flag_incorrect_voucher, _ := h.flagManager.GetFlag("flag_incorrect_voucher")
|
||||
|
||||
res.FlagReset = append(res.FlagReset, flag_incorrect_voucher)
|
||||
|
||||
inputStr := string(input)
|
||||
if inputStr == "0" || inputStr == "99" || inputStr == "88" || inputStr == "98" {
|
||||
return res, nil
|
||||
}
|
||||
|
||||
userStore := h.userdataStore
|
||||
metadata, err := store.GetOrderedVoucherData(ctx, userStore, sessionId, inputStr)
|
||||
if err != nil {
|
||||
return res, fmt.Errorf("failed to retrieve swap to voucher data: %v", err)
|
||||
}
|
||||
if metadata == nil {
|
||||
res.FlagSet = append(res.FlagSet, flag_incorrect_voucher)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// Store the transaction voucher data
|
||||
if err := store.StoreTemporaryVoucher(ctx, h.userdataStore, sessionId, metadata); err != nil {
|
||||
logg.ErrorCtxf(ctx, "failed on StoreTemporaryVoucher", "error", err)
|
||||
return res, err
|
||||
}
|
||||
|
||||
// Store the state of the custom transaction voucher
|
||||
err = userStore.WriteEntry(ctx, sessionId, storedb.DATA_TRANSACTION_CUSTOM_VOUCHER, []byte("1"))
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "failed to write custom transaction voucher", "key", storedb.DATA_TRANSACTION_CUSTOM_VOUCHER, "error", err)
|
||||
return res, err
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
@ -154,6 +154,7 @@ func (ls *LocalHandlerService) GetHandler(accountService remote.AccountService)
|
||||
ls.DbRs.AddLocalFunc("pool_deposit_max_amount", appHandlers.PoolDepositMaxAmount)
|
||||
ls.DbRs.AddLocalFunc("confirm_pool_deposit", appHandlers.ConfirmPoolDeposit)
|
||||
ls.DbRs.AddLocalFunc("initiate_pool_deposit", appHandlers.InitiatePoolDeposit)
|
||||
ls.DbRs.AddLocalFunc("validate_credit_voucher", appHandlers.ValidateCreditVoucher)
|
||||
|
||||
ls.first = appHandlers.Init
|
||||
|
||||
|
||||
@ -3,10 +3,13 @@ RELOAD transaction_reset
|
||||
CATCH no_voucher flag_no_active_voucher 1
|
||||
MOUT back 0
|
||||
HALT
|
||||
LOAD clear_trans_type_flag 6
|
||||
RELOAD clear_trans_type_flag
|
||||
LOAD validate_recipient 50
|
||||
RELOAD validate_recipient
|
||||
CATCH api_failure flag_api_call_error 1
|
||||
CATCH api_failure flag_api_call_error 1
|
||||
CATCH invalid_recipient flag_invalid_recipient 1
|
||||
CATCH invite_recipient flag_invalid_recipient_with_invite 1
|
||||
CATCH credit_vouchers flag_multiple_voucher 1
|
||||
INCMP _ 0
|
||||
INCMP credit_amount *
|
||||
|
||||
1
services/registration/credit_vouchers
Normal file
1
services/registration/credit_vouchers
Normal file
@ -0,0 +1 @@
|
||||
{{.get_ordered_vouchers}}
|
||||
15
services/registration/credit_vouchers.vis
Normal file
15
services/registration/credit_vouchers.vis
Normal file
@ -0,0 +1,15 @@
|
||||
LOAD get_ordered_vouchers 0
|
||||
MAP get_ordered_vouchers
|
||||
MOUT back 0
|
||||
MOUT quit 99
|
||||
MNEXT next 88
|
||||
MPREV prev 98
|
||||
HALT
|
||||
INCMP > 88
|
||||
INCMP < 98
|
||||
INCMP _ 0
|
||||
INCMP quit 99
|
||||
LOAD validate_credit_voucher 67
|
||||
RELOAD validate_credit_voucher
|
||||
CATCH . flag_incorrect_voucher 1
|
||||
INCMP credit_amount *
|
||||
@ -37,4 +37,4 @@ flag,flag_low_swap_amount,43,this is set when the swap max limit is less than 0.
|
||||
flag,flag_alias_unavailable,44,this is set when the preferred alias is not available
|
||||
flag,flag_swap_transaction,45,this is set when the transaction will involve performing a swap
|
||||
flag,flag_no_stable_vouchers,46,this is set when the user does not have a stable voucher
|
||||
flag,flag_single_voucher,47,this is set when the user only has a single voucher
|
||||
flag,flag_multiple_voucher,47,this is set when the user only has a multiple voucher
|
||||
|
||||
|
@ -95,6 +95,8 @@ const (
|
||||
DATA_RECIPIENT_PHONE_NUMBER
|
||||
// Currently active swap from balance for the swap
|
||||
DATA_ACTIVE_SWAP_FROM_BALANCE
|
||||
// Holds the state whether the transaction uses a custom voucher
|
||||
DATA_TRANSACTION_CUSTOM_VOUCHER
|
||||
)
|
||||
|
||||
const (
|
||||
|
||||
Loading…
Reference in New Issue
Block a user