store the ordered list of vouchers with stables at the top
This commit is contained in:
parent
ea71c08143
commit
50c2aff79e
@ -3,10 +3,12 @@ package application
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"git.defalsify.org/vise.git/db"
|
"git.defalsify.org/vise.git/db"
|
||||||
"git.defalsify.org/vise.git/resource"
|
"git.defalsify.org/vise.git/resource"
|
||||||
|
"git.grassecon.net/grassrootseconomics/sarafu-vise/config"
|
||||||
"git.grassecon.net/grassrootseconomics/sarafu-vise/store"
|
"git.grassecon.net/grassrootseconomics/sarafu-vise/store"
|
||||||
storedb "git.grassecon.net/grassrootseconomics/sarafu-vise/store/db"
|
storedb "git.grassecon.net/grassrootseconomics/sarafu-vise/store/db"
|
||||||
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
||||||
@ -172,35 +174,55 @@ func (h *MenuHandlers) ManageVouchers(ctx context.Context, sym string, input []b
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Filter stable vouchers
|
// Get stable voucher priority order (lower index = higher priority)
|
||||||
filteredStableVouchers := make([]dataserviceapi.TokenHoldings, 0)
|
stablePriority := make(map[string]int)
|
||||||
for _, v := range vouchersResp {
|
stableAddresses := config.StableVoucherAddresses()
|
||||||
|
|
||||||
|
for i, addr := range stableAddresses {
|
||||||
|
stablePriority[strings.ToLower(addr)] = i
|
||||||
|
}
|
||||||
|
|
||||||
|
// Split vouchers into stable and non-stable
|
||||||
|
stableVouchers := make([]dataserviceapi.TokenHoldings, 0)
|
||||||
|
nonStableVouchers := make([]dataserviceapi.TokenHoldings, 0)
|
||||||
|
|
||||||
|
for _, v := range vouchersResp {
|
||||||
if isStableVoucher(v.TokenAddress) {
|
if isStableVoucher(v.TokenAddress) {
|
||||||
filteredStableVouchers = append(filteredStableVouchers, v)
|
stableVouchers = append(stableVouchers, v)
|
||||||
|
} else {
|
||||||
|
nonStableVouchers = append(nonStableVouchers, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// No stable vouchers
|
// No stable vouchers at all
|
||||||
if len(filteredStableVouchers) == 0 {
|
if len(stableVouchers) == 0 {
|
||||||
res.FlagSet = append(res.FlagSet, flag_no_stable_vouchers)
|
res.FlagSet = append(res.FlagSet, flag_no_stable_vouchers)
|
||||||
return res, nil
|
} else {
|
||||||
|
res.FlagReset = append(res.FlagReset, flag_no_stable_vouchers)
|
||||||
}
|
}
|
||||||
|
|
||||||
res.FlagReset = append(res.FlagReset, flag_no_stable_vouchers)
|
// Sort stable vouchers by configured priority
|
||||||
|
sort.SliceStable(stableVouchers, func(i, j int) bool {
|
||||||
|
ai := stablePriority[strings.ToLower(stableVouchers[i].TokenAddress)]
|
||||||
|
aj := stablePriority[strings.ToLower(stableVouchers[j].TokenAddress)]
|
||||||
|
return ai < aj
|
||||||
|
})
|
||||||
|
|
||||||
// Process stable vouchers for later use
|
// Final ordered list: stable first, then others
|
||||||
stableVoucherData := store.ProcessVouchers(filteredStableVouchers)
|
orderedVouchers := append(stableVouchers, nonStableVouchers...)
|
||||||
|
|
||||||
stableVoucherDataMap := map[storedb.DataTyp]string{
|
// Process ALL vouchers (stable first)
|
||||||
storedb.DATA_STABLE_VOUCHER_SYMBOLS: stableVoucherData.Symbols,
|
orderedVoucherData := store.ProcessVouchers(orderedVouchers)
|
||||||
storedb.DATA_STABLE_VOUCHER_BALANCES: stableVoucherData.Balances,
|
|
||||||
storedb.DATA_STABLE_VOUCHER_DECIMALS: stableVoucherData.Decimals,
|
orderedVoucherDataMap := map[storedb.DataTyp]string{
|
||||||
storedb.DATA_STABLE_VOUCHER_ADDRESSES: stableVoucherData.Addresses,
|
storedb.DATA_ORDERED_VOUCHER_SYMBOLS: orderedVoucherData.Symbols,
|
||||||
|
storedb.DATA_ORDERED_VOUCHER_BALANCES: orderedVoucherData.Balances,
|
||||||
|
storedb.DATA_ORDERED_VOUCHER_DECIMALS: orderedVoucherData.Decimals,
|
||||||
|
storedb.DATA_ORDERED_VOUCHER_ADDRESSES: orderedVoucherData.Addresses,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write data entries
|
// Write data entries
|
||||||
for key, value := range stableVoucherDataMap {
|
for key, value := range orderedVoucherDataMap {
|
||||||
if err := userStore.WriteEntry(ctx, sessionId, key, []byte(value)); err != nil {
|
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)
|
logg.ErrorCtxf(ctx, "Failed to write data entry for sessionId: %s", sessionId, "key", key, "error", err)
|
||||||
continue
|
continue
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user