Compare commits

..

15 Commits

24 changed files with 386 additions and 15 deletions

View File

@ -119,7 +119,7 @@ func (h *MenuHandlers) CalculateCreditAndDebt(ctx context.Context, sym string, i
l.AddDomain("default") l.AddDomain("default")
flag_api_call_error, _ := h.flagManager.GetFlag("flag_api_call_error") flag_api_call_error, _ := h.flagManager.GetFlag("flag_api_call_error")
flag_no_pay_debt_vouchers, _ := h.flagManager.GetFlag("flag_no_pay_debt_vouchers") flag_no_stable_vouchers, _ := h.flagManager.GetFlag("flag_no_stable_vouchers")
// default response // default response
res.FlagReset = append(res.FlagReset, flag_api_call_error) res.FlagReset = append(res.FlagReset, flag_api_call_error)
@ -161,11 +161,11 @@ func (h *MenuHandlers) CalculateCreditAndDebt(ctx context.Context, sym string, i
// No stable vouchers → cannot pay debt // No stable vouchers → cannot pay debt
if len(filteredSwappableVouchers) == 0 { if len(filteredSwappableVouchers) == 0 {
res.FlagSet = append(res.FlagSet, flag_no_pay_debt_vouchers) res.FlagSet = append(res.FlagSet, flag_no_stable_vouchers)
return res, nil return res, nil
} }
res.FlagReset = append(res.FlagReset, flag_no_pay_debt_vouchers) res.FlagReset = append(res.FlagReset, flag_no_stable_vouchers)
// Process stable vouchers for later use // Process stable vouchers for later use
data := store.ProcessVouchers(filteredSwappableVouchers) data := store.ProcessVouchers(filteredSwappableVouchers)

View File

@ -0,0 +1,220 @@
package application
import (
"context"
"fmt"
"strconv"
"strings"
"git.defalsify.org/vise.git/resource"
"git.grassecon.net/grassrootseconomics/sarafu-vise/store"
storedb "git.grassecon.net/grassrootseconomics/sarafu-vise/store/db"
"gopkg.in/leonelquinteros/gotext.v1"
)
// GetPoolDepositVouchers returns a list of stable coins
func (h *MenuHandlers) GetPoolDepositVouchers(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")
userStore := h.userdataStore
// Read stable vouchers from the store
voucherData, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_STABLE_VOUCHER_SYMBOLS)
if err != nil {
logg.ErrorCtxf(ctx, "failed to read stable voucherData entires with", "key", storedb.DATA_STABLE_VOUCHER_SYMBOLS, "error", err)
return res, err
}
if len(voucherData) == 0 {
return res, nil
}
voucherBalances, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_STABLE_VOUCHER_BALANCES)
if err != nil {
logg.ErrorCtxf(ctx, "failed to read stable voucherData entires with", "key", storedb.DATA_STABLE_VOUCHER_BALANCES, "error", err)
return res, err
}
formattedVoucherList := store.FormatVoucherList(ctx, string(voucherData), string(voucherBalances))
finalOutput := strings.Join(formattedVoucherList, "\n")
res.Content = l.Get("Select number or symbol from your vouchers:\n%s", finalOutput)
return res, nil
}
// PoolDepositMaxAmount returns the balance of the selected voucher
func (h *MenuHandlers) PoolDepositMaxAmount(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.GetStableVoucherData(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 pool deposit voucher data
if err := store.StoreTemporaryVoucher(ctx, h.userdataStore, sessionId, metadata); err != nil {
logg.ErrorCtxf(ctx, "failed on StoreTemporaryVoucher", "error", err)
return res, err
}
// Format the balance amount to 2 decimal places
formattedBalance, _ := store.TruncateDecimalString(string(metadata.Balance), 2)
res.Content = l.Get("Maximum amount: %s %s\nEnter amount:", formattedBalance, metadata.TokenSymbol)
return res, nil
}
// ConfirmPoolDeposit displays the pool deposit preview for a PIN confirmation
func (h *MenuHandlers) ConfirmPoolDeposit(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")
}
inputStr := string(input)
if inputStr == "0" {
return res, nil
}
flag_invalid_amount, _ := h.flagManager.GetFlag("flag_invalid_amount")
res.FlagReset = append(res.FlagReset, flag_invalid_amount)
code := codeFromCtx(ctx)
l := gotext.NewLocale(translationDir, code)
l.AddDomain("default")
userStore := h.userdataStore
poolDepositVoucher, err := store.GetTemporaryVoucherData(ctx, h.userdataStore, sessionId)
if err != nil {
logg.ErrorCtxf(ctx, "failed on GetTemporaryVoucherData", "error", err)
return res, err
}
maxValue, err := strconv.ParseFloat(poolDepositVoucher.Balance, 64)
if err != nil {
logg.ErrorCtxf(ctx, "Failed to convert the swapMaxAmount to a float", "error", err)
return res, err
}
inputAmount, err := strconv.ParseFloat(inputStr, 64)
if err != nil || inputAmount > maxValue || inputAmount < 0.1 {
res.FlagSet = append(res.FlagSet, flag_invalid_amount)
res.Content = inputStr
return res, nil
}
err = userStore.WriteEntry(ctx, sessionId, storedb.DATA_AMOUNT, []byte(inputStr))
if err != nil {
logg.ErrorCtxf(ctx, "failed to write pool deposit amount entry with", "key", storedb.DATA_AMOUNT, "value", inputStr, "error", err)
return res, err
}
// Resolve active pool
_, activePoolName, err := h.resolveActivePoolDetails(ctx, sessionId)
if err != nil {
return res, err
}
res.Content = l.Get(
"You will deposit %s %s into %s\n",
inputStr, poolDepositVoucher.TokenSymbol, activePoolName,
)
return res, nil
}
// InitiatePoolDeposit calls the pool deposit API
func (h *MenuHandlers) InitiatePoolDeposit(ctx context.Context, sym string, input []byte) (resource.Result, error) {
var res resource.Result
var err error
sessionId, ok := ctx.Value("SessionId").(string)
if !ok {
return res, fmt.Errorf("missing session")
}
flag_account_authorized, _ := h.flagManager.GetFlag("flag_account_authorized")
code := codeFromCtx(ctx)
l := gotext.NewLocale(translationDir, code)
l.AddDomain("default")
// Resolve active pool
activePoolAddress, activePoolName, err := h.resolveActivePoolDetails(ctx, sessionId)
if err != nil {
return res, err
}
poolDepositVoucher, err := store.GetTemporaryVoucherData(ctx, h.userdataStore, sessionId)
if err != nil {
logg.ErrorCtxf(ctx, "failed on GetTemporaryVoucherData", "error", err)
return res, err
}
poolDepositdata, err := store.ReadTransactionData(ctx, h.userdataStore, sessionId)
if err != nil {
return res, err
}
finalAmountStr, err := store.ParseAndScaleAmount(poolDepositdata.Amount, poolDepositVoucher.TokenDecimals)
if err != nil {
return res, err
}
// Call pool deposit API
r, err := h.accountService.PoolDeposit(ctx, finalAmountStr, poolDepositdata.PublicKey, string(activePoolAddress), poolDepositVoucher.TokenAddress)
if err != nil {
flag_api_call_error, _ := h.flagManager.GetFlag("flag_api_call_error")
res.FlagSet = append(res.FlagSet, flag_api_call_error)
res.Content = l.Get("Your request failed. Please try again later.")
logg.ErrorCtxf(ctx, "failed on pool deposit", "error", err)
return res, nil
}
trackingId := r.TrackingId
logg.InfoCtxf(ctx, "Pool deposit", "trackingId", trackingId)
res.Content = l.Get(
"Your request has been sent. You will receive an SMS when %s %s has been deposited into %s.",
poolDepositdata.Amount,
poolDepositVoucher.TokenSymbol,
activePoolName,
)
res.FlagReset = append(res.FlagReset, flag_account_authorized)
return res, nil
}

View File

@ -585,7 +585,7 @@ func (h *MenuHandlers) ValidateAmount(ctx context.Context, sym string, input []b
return res, nil return res, nil
} }
if inputAmount > balanceValue { if inputAmount > balanceValue || inputAmount < 0.1{
res.FlagSet = append(res.FlagSet, flag_invalid_amount) res.FlagSet = append(res.FlagSet, flag_invalid_amount)
res.Content = amountStr res.Content = amountStr
return res, nil return res, nil

View File

@ -16,7 +16,8 @@ import (
// ManageVouchers retrieves the token holdings from the API using the "PublicKey" and // ManageVouchers retrieves the token holdings from the API using the "PublicKey" and
// 1. sets the first as the default voucher if no active voucher is set. // 1. sets the first as the default voucher if no active voucher is set.
// 2. Stores list of vouchers // 2. Stores list of vouchers
// 3. updates the balance of the active voucher // 3. Stores list of filtered stable vouchers
// 4. updates the balance of the active voucher
func (h *MenuHandlers) ManageVouchers(ctx context.Context, sym string, input []byte) (resource.Result, error) { func (h *MenuHandlers) ManageVouchers(ctx context.Context, sym string, input []byte) (resource.Result, error) {
var res resource.Result var res resource.Result
userStore := h.userdataStore userStore := h.userdataStore
@ -29,6 +30,7 @@ func (h *MenuHandlers) ManageVouchers(ctx context.Context, sym string, input []b
flag_no_active_voucher, _ := h.flagManager.GetFlag("flag_no_active_voucher") flag_no_active_voucher, _ := h.flagManager.GetFlag("flag_no_active_voucher")
flag_api_error, _ := h.flagManager.GetFlag("flag_api_call_error") flag_api_error, _ := h.flagManager.GetFlag("flag_api_call_error")
flag_no_stable_vouchers, _ := h.flagManager.GetFlag("flag_no_stable_vouchers")
publicKey, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY) publicKey, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY)
if err != nil { if err != nil {
@ -170,6 +172,41 @@ func (h *MenuHandlers) ManageVouchers(ctx context.Context, sym string, input []b
} }
} }
// Filter stable vouchers
filteredStableVouchers := make([]dataserviceapi.TokenHoldings, 0)
for _, v := range vouchersResp {
if isStableVoucher(v.TokenAddress) {
filteredStableVouchers = append(filteredStableVouchers, v)
}
}
// No stable vouchers
if len(filteredStableVouchers) == 0 {
res.FlagSet = append(res.FlagSet, flag_no_stable_vouchers)
return res, nil
}
res.FlagReset = append(res.FlagReset, flag_no_stable_vouchers)
// Process stable vouchers for later use
stableVoucherData := store.ProcessVouchers(filteredStableVouchers)
stableVoucherDataMap := map[storedb.DataTyp]string{
storedb.DATA_STABLE_VOUCHER_SYMBOLS: stableVoucherData.Symbols,
storedb.DATA_STABLE_VOUCHER_BALANCES: stableVoucherData.Balances,
storedb.DATA_STABLE_VOUCHER_DECIMALS: stableVoucherData.Decimals,
storedb.DATA_STABLE_VOUCHER_ADDRESSES: stableVoucherData.Addresses,
}
// Write data entries
for key, value := range stableVoucherDataMap {
if err := userStore.WriteEntry(ctx, sessionId, key, []byte(value)); err != nil {
logg.ErrorCtxf(ctx, "Failed to write data entry for sessionId: %s", sessionId, "key", key, "error", err)
continue
}
}
return res, nil return res, nil
} }

View File

@ -150,6 +150,10 @@ func (ls *LocalHandlerService) GetHandler(accountService remote.AccountService)
ls.DbRs.AddLocalFunc("calculate_max_pay_debt", appHandlers.CalculateMaxPayDebt) ls.DbRs.AddLocalFunc("calculate_max_pay_debt", appHandlers.CalculateMaxPayDebt)
ls.DbRs.AddLocalFunc("confirm_debt_removal", appHandlers.ConfirmDebtRemoval) ls.DbRs.AddLocalFunc("confirm_debt_removal", appHandlers.ConfirmDebtRemoval)
ls.DbRs.AddLocalFunc("initiate_pay_debt", appHandlers.InitiatePayDebt) ls.DbRs.AddLocalFunc("initiate_pay_debt", appHandlers.InitiatePayDebt)
ls.DbRs.AddLocalFunc("get_pool_deposit_vouchers", appHandlers.GetPoolDepositVouchers)
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.first = appHandlers.Init ls.first = appHandlers.Init

View File

@ -0,0 +1 @@
Pool deposit

View File

@ -0,0 +1 @@
Weka kwa bwawa

View File

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

View File

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

View File

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

View File

@ -32,7 +32,7 @@ msgid "Symbol: %s\nBalance: %s"
msgstr "Sarafu: %s\nSalio: %s" msgstr "Sarafu: %s\nSalio: %s"
msgid "Your request has been sent. You will receive an SMS when your %s %s has been swapped for %s." msgid "Your request has been sent. You will receive an SMS when your %s %s has been swapped for %s."
msgstr "Ombi lako limetumwa. Utapokea SMS wakati %s %s yako itakapobadilishwa kuwa %s." msgstr "Ombi lako limetumwa. Utapokea ujumbe wakati %s %s yako itakapobadilishwa kuwa %s."
msgid "%s balance: %s\n" msgid "%s balance: %s\n"
msgstr "%s salio: %s\n" msgstr "%s salio: %s\n"
@ -83,4 +83,10 @@ msgid "Your active voucher %s is already set"
msgstr "Sarafu yako %s ishachaguliwa" msgstr "Sarafu yako %s ishachaguliwa"
msgid "Select number or symbol from your vouchers:\n%s" msgid "Select number or symbol from your vouchers:\n%s"
msgstr "Chagua nambari au ishara kutoka kwa sarafu zako:\n%s" msgstr "Chagua nambari au ishara kutoka kwa sarafu zako:\n%s"
msgid "You will deposit %s %s into %s\n"
msgstr "Utaweka %s %s kwenye %s\n"
msgid "Your request has been sent. You will receive an SMS when %s %s has been deposited into %s."
msgstr "Ombi lako limetumwa. Utapokea ujumbe wakati %s %s itawekwa kwenye %s."

View File

@ -3,14 +3,16 @@ RELOAD calc_credit_debt
CATCH api_failure flag_api_call_error 1 CATCH api_failure flag_api_call_error 1
MAP calc_credit_debt MAP calc_credit_debt
MOUT pay_debt 1 MOUT pay_debt 1
MOUT get_mpesa 2 MOUT deposit 2
MOUT send_mpesa 3 MOUT get_mpesa 3
MOUT send_mpesa 4
MOUT back 0 MOUT back 0
MOUT quit 9 MOUT quit 9
HALT HALT
INCMP ^ 0 INCMP ^ 0
INCMP pay_debt 1 INCMP pay_debt 1
INCMP get_mpesa 2 INCMP pool_deposit 2
INCMP send_mpesa 3 INCMP get_mpesa 3
INCMP send_mpesa 4
INCMP quit 9 INCMP quit 9
INCMP . * INCMP . *

View File

@ -1,5 +1,5 @@
CATCH no_voucher flag_no_active_voucher 1 CATCH no_voucher flag_no_active_voucher 1
CATCH no_stable_voucher flag_no_pay_debt_vouchers 1 CATCH no_stable_voucher flag_no_stable_vouchers 1
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 MAP calculate_max_pay_debt

View File

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

View File

@ -0,0 +1,17 @@
CATCH no_voucher flag_no_active_voucher 1
CATCH no_stable_voucher flag_no_stable_vouchers 1
LOAD get_pool_deposit_vouchers 0
MAP get_pool_deposit_vouchers
MOUT back 0
MOUT quit 99
MNEXT next 88
MPREV prev 98
HALT
INCMP > 88
INCMP < 98
INCMP _ 0
INCMP quit 99
LOAD pool_deposit_max_amount 120
RELOAD pool_deposit_max_amount
CATCH . flag_incorrect_voucher 1
INCMP pool_deposit_amount *

View File

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

View File

@ -0,0 +1,10 @@
LOAD reset_transaction_amount 10
RELOAD reset_transaction_amount
MAP pool_deposit_max_amount
MOUT back 0
HALT
LOAD confirm_pool_deposit 140
RELOAD confirm_pool_deposit
CATCH invalid_pool_deposit_amount flag_invalid_amount 1
INCMP _ 0
INCMP pool_deposit_confirmation *

View File

@ -0,0 +1,2 @@
{{.confirm_pool_deposit}}
Please enter your PIN to confirm:

View File

@ -0,0 +1,10 @@
MAP confirm_pool_deposit
MOUT back 0
MOUT quit 9
HALT
LOAD authorize_account 6
RELOAD authorize_account
CATCH incorrect_pin flag_incorrect_pin 1
INCMP _ 0
INCMP quit 9
INCMP pool_deposit_initiated *

View File

@ -0,0 +1,2 @@
{{.confirm_pool_deposit}}
Tafadhali weka PIN yako kudhibitisha:

View File

@ -0,0 +1,4 @@
LOAD reset_incorrect_pin 6
CATCH _ flag_account_authorized 0
LOAD initiate_pool_deposit 0
HALT

View File

@ -36,4 +36,4 @@ flag,flag_incorrect_pool,42,this is set when the user selects an invalid pool
flag,flag_low_swap_amount,43,this is set when the swap max limit is less than 0.1 flag,flag_low_swap_amount,43,this is set when the swap max limit is less than 0.1
flag,flag_alias_unavailable,44,this is set when the preferred alias is not available 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_swap_transaction,45,this is set when the transaction will involve performing a swap
flag,flag_no_pay_debt_vouchers,46,this is set when the user does not have a stable voucher to pay debt flag,flag_no_stable_vouchers,46,this is set when the user does not have a stable voucher

1 flag flag_language_set 8 checks whether the user has set their prefered language
36 flag flag_low_swap_amount 43 this is set when the swap max limit is less than 0.1
37 flag flag_alias_unavailable 44 this is set when the preferred alias is not available
38 flag flag_swap_transaction 45 this is set when the transaction will involve performing a swap
39 flag flag_no_pay_debt_vouchers flag_no_stable_vouchers 46 this is set when the user does not have a stable voucher to pay debt this is set when the user does not have a stable voucher

View File

@ -65,7 +65,7 @@ const (
DATA_ACCOUNT_ALIAS DATA_ACCOUNT_ALIAS
//currently suggested alias by the api awaiting user's confirmation as accepted account alias //currently suggested alias by the api awaiting user's confirmation as accepted account alias
DATA_SUGGESTED_ALIAS DATA_SUGGESTED_ALIAS
//Key used to store a value of 1 for a user to reset their own PIN once they access the menu. //Key used to store a value of 1 for a user to reset their own PIN once they access the menu.
DATA_SELF_PIN_RESET DATA_SELF_PIN_RESET
// Holds the active pool contract address for the swap // Holds the active pool contract address for the swap
DATA_ACTIVE_POOL_ADDRESS DATA_ACTIVE_POOL_ADDRESS
@ -104,7 +104,14 @@ const (
DATA_VOUCHER_DECIMALS DATA_VOUCHER_DECIMALS
// List of voucher EVM addresses for vouchers valid in the user context. // List of voucher EVM addresses for vouchers valid in the user context.
DATA_VOUCHER_ADDRESSES DATA_VOUCHER_ADDRESSES
// List of senders for valid transactions in the user context. // List of stable voucher symbols in the user context.
DATA_STABLE_VOUCHER_SYMBOLS
// List of stable voucher decimal counts for vouchers valid in the user context.
DATA_STABLE_VOUCHER_BALANCES
// List of stable voucher decimal counts for vouchers valid in the user context.
DATA_STABLE_VOUCHER_DECIMALS
// List of stable voucher EVM addresses for vouchers valid in the user context.
DATA_STABLE_VOUCHER_ADDRESSES
) )
const ( const (

View File

@ -120,6 +120,43 @@ func GetVoucherData(ctx context.Context, store DataStore, sessionId string, inpu
}, nil }, nil
} }
// GetStableVoucherData retrieves and matches stable voucher data
func GetStableVoucherData(ctx context.Context, store DataStore, sessionId string, input string) (*dataserviceapi.TokenHoldings, error) {
keys := []storedb.DataTyp{
storedb.DATA_STABLE_VOUCHER_SYMBOLS,
storedb.DATA_STABLE_VOUCHER_BALANCES,
storedb.DATA_STABLE_VOUCHER_DECIMALS,
storedb.DATA_STABLE_VOUCHER_ADDRESSES,
}
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)
}
symbol, balance, decimal, address := MatchVoucher(input,
data[storedb.DATA_STABLE_VOUCHER_SYMBOLS],
data[storedb.DATA_STABLE_VOUCHER_BALANCES],
data[storedb.DATA_STABLE_VOUCHER_DECIMALS],
data[storedb.DATA_STABLE_VOUCHER_ADDRESSES],
)
if symbol == "" {
return nil, nil
}
return &dataserviceapi.TokenHoldings{
TokenSymbol: string(symbol),
Balance: string(balance),
TokenDecimals: string(decimal),
TokenAddress: string(address),
}, nil
}
// MatchVoucher finds the matching voucher symbol, balance, decimals and contract address based on the input. // MatchVoucher finds the matching voucher symbol, balance, decimals and contract address based on the input.
func MatchVoucher(input, symbols, balances, decimals, addresses string) (symbol, balance, decimal, address string) { func MatchVoucher(input, symbols, balances, decimals, addresses string) (symbol, balance, decimal, address string) {
symList := strings.Split(symbols, "\n") symList := strings.Split(symbols, "\n")