|
|
|
|
@@ -1468,7 +1468,6 @@ func loadUserContent(ctx context.Context, activeSym string, balance string, alia
|
|
|
|
|
// CheckBalance retrieves the balance of the active voucher and sets
|
|
|
|
|
// the balance as the result content.
|
|
|
|
|
func (h *MenuHandlers) CheckBalance(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
res resource.Result
|
|
|
|
|
err error
|
|
|
|
|
@@ -1496,19 +1495,22 @@ func (h *MenuHandlers) CheckBalance(ctx context.Context, sym string, input []byt
|
|
|
|
|
// get the active sym and active balance
|
|
|
|
|
activeSym, err := store.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_SYM)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println("could not find the activeSym in checkBalance:", err)
|
|
|
|
|
if !db.IsNotFound(err) {
|
|
|
|
|
fmt.Println("the err:", err)
|
|
|
|
|
logg.ErrorCtxf(ctx, "failed to read activeSym entry with", "key", storedb.DATA_ACTIVE_SYM, "error", err)
|
|
|
|
|
return res, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Println("The active data in CheckBalance:", string(activeSym))
|
|
|
|
|
|
|
|
|
|
activeBal, err := store.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_BAL)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if !db.IsNotFound(err) {
|
|
|
|
|
logg.ErrorCtxf(ctx, "failed to read activeBal entry with", "key", storedb.DATA_ACTIVE_BAL, "error", err)
|
|
|
|
|
return res, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
content, err = loadUserContent(ctx, string(activeSym), string(activeBal), alias)
|
|
|
|
|
@@ -1930,7 +1932,6 @@ func (h *MenuHandlers) InitiateTransaction(ctx context.Context, sym string, inpu
|
|
|
|
|
// and sets the first as the default voucher, if no active voucher is set.
|
|
|
|
|
func (h *MenuHandlers) SetDefaultVoucher(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
|
|
|
|
var res resource.Result
|
|
|
|
|
var err error
|
|
|
|
|
userStore := h.userdataStore
|
|
|
|
|
|
|
|
|
|
sessionId, ok := ctx.Value("SessionId").(string)
|
|
|
|
|
@@ -1941,9 +1942,12 @@ func (h *MenuHandlers) SetDefaultVoucher(ctx context.Context, sym string, input
|
|
|
|
|
flag_no_active_voucher, _ := h.flagManager.GetFlag("flag_no_active_voucher")
|
|
|
|
|
|
|
|
|
|
// check if the user has an active sym
|
|
|
|
|
_, err = userStore.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_SYM)
|
|
|
|
|
|
|
|
|
|
activeSym, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_SYM)
|
|
|
|
|
fmt.Println("The activeSym in SetDefaultVoucher:", string(activeSym))
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println("The err", err)
|
|
|
|
|
fmt.Println("Checking the data as no activeSym", "DATA_ACTIVE_SYM", storedb.DATA_ACTIVE_SYM)
|
|
|
|
|
|
|
|
|
|
if db.IsNotFound(err) {
|
|
|
|
|
publicKey, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY)
|
|
|
|
|
if err != nil {
|
|
|
|
|
@@ -1954,12 +1958,25 @@ func (h *MenuHandlers) SetDefaultVoucher(ctx context.Context, sym string, input
|
|
|
|
|
// Fetch vouchers from the API using the public key
|
|
|
|
|
vouchersResp, err := h.accountService.FetchVouchers(ctx, string(publicKey))
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println("setting the flag_no_active_voucher")
|
|
|
|
|
res.FlagSet = append(res.FlagSet, flag_no_active_voucher)
|
|
|
|
|
return res, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// vouchersResp := []dataserviceapi.TokenHoldings{
|
|
|
|
|
// {
|
|
|
|
|
// ContractAddress: "0xcB453B742Bc8BE1BAa03Ce1927e287788d0aF065",
|
|
|
|
|
// TokenSymbol: "PES",
|
|
|
|
|
// TokenDecimals: "6",
|
|
|
|
|
// Balance: "10000000",
|
|
|
|
|
// },
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
fmt.Println("fetched user vouchers in SetDefaultVoucher", "public_key", string(publicKey), "vouchers", vouchersResp)
|
|
|
|
|
|
|
|
|
|
// Return if there is no voucher
|
|
|
|
|
if len(vouchersResp) == 0 {
|
|
|
|
|
fmt.Println("setting the flag_no_active_voucher in SetDefaultVoucher")
|
|
|
|
|
res.FlagSet = append(res.FlagSet, flag_no_active_voucher)
|
|
|
|
|
return res, nil
|
|
|
|
|
}
|
|
|
|
|
@@ -1974,10 +1991,14 @@ func (h *MenuHandlers) SetDefaultVoucher(ctx context.Context, sym string, input
|
|
|
|
|
// Scale down the balance
|
|
|
|
|
scaledBalance := store.ScaleDownBalance(defaultBal, defaultDec)
|
|
|
|
|
|
|
|
|
|
fmt.Println("firstVoucher data", "defaultSym", defaultSym, "defaultBal", defaultBal, "defaultDec", defaultDec, "defaultAddr", defaultAddr)
|
|
|
|
|
|
|
|
|
|
// TODO: implement atomic transaction
|
|
|
|
|
// set the active symbol
|
|
|
|
|
err = userStore.WriteEntry(ctx, sessionId, storedb.DATA_ACTIVE_SYM, []byte(defaultSym))
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println("got an error in writing DATA_ACTIVE_SYM", "defaultSym", defaultSym)
|
|
|
|
|
|
|
|
|
|
logg.ErrorCtxf(ctx, "failed to write defaultSym entry with", "key", storedb.DATA_ACTIVE_SYM, "value", defaultSym, "error", err)
|
|
|
|
|
return res, err
|
|
|
|
|
}
|
|
|
|
|
@@ -2003,7 +2024,7 @@ func (h *MenuHandlers) SetDefaultVoucher(ctx context.Context, sym string, input
|
|
|
|
|
return res, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logg.ErrorCtxf(ctx, "failed to read activeSym entry with", "key", storedb.DATA_ACTIVE_SYM, "error", err)
|
|
|
|
|
fmt.Println("failed to read activeSym entry with", "key", storedb.DATA_ACTIVE_SYM, "error", err)
|
|
|
|
|
return res, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -2034,6 +2055,17 @@ func (h *MenuHandlers) CheckVouchers(ctx context.Context, sym string, input []by
|
|
|
|
|
return res, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// vouchersResp := []dataserviceapi.TokenHoldings{
|
|
|
|
|
// {
|
|
|
|
|
// ContractAddress: "0xcB453B742Bc8BE1BAa03Ce1927e287788d0aF065",
|
|
|
|
|
// TokenSymbol: "PES",
|
|
|
|
|
// TokenDecimals: "6",
|
|
|
|
|
// Balance: "10000000",
|
|
|
|
|
// },
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
fmt.Println("fetched user vouchers", "public_key", string(publicKey), "vouchers", vouchersResp)
|
|
|
|
|
|
|
|
|
|
// check the current active sym and update the data
|
|
|
|
|
activeSym, _ := userStore.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_SYM)
|
|
|
|
|
if activeSym != nil {
|
|
|
|
|
@@ -2066,8 +2098,15 @@ func (h *MenuHandlers) CheckVouchers(ctx context.Context, sym string, input []by
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
activeBal, _ := userStore.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_BAL)
|
|
|
|
|
activeAddr, _ := userStore.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_ADDRESS)
|
|
|
|
|
|
|
|
|
|
fmt.Println(ctx, "The active data in CheckVouchers:", "activeSym", string(activeSym), string(activeBal), string(activeAddr))
|
|
|
|
|
|
|
|
|
|
data := store.ProcessVouchers(vouchersResp)
|
|
|
|
|
|
|
|
|
|
fmt.Println(ctx, "The data in CheckVouchers:", "data", data)
|
|
|
|
|
|
|
|
|
|
// Store all voucher data
|
|
|
|
|
dataMap := map[storedb.DataTyp]string{
|
|
|
|
|
storedb.DATA_VOUCHER_SYMBOLS: data.Symbols,
|
|
|
|
|
|