Compare commits
11 Commits
7e2e3c3722
...
35d562ffdd
Author | SHA1 | Date | |
---|---|---|---|
35d562ffdd | |||
7d8631fa87 | |||
c8159113f0 | |||
126415050c | |||
5b989d83e1 | |||
7199d0f3e0 | |||
feea74c2c9 | |||
b6dcf65ea3 | |||
da7b6e93e1 | |||
97be43b9ec | |||
a9ecb1bbae |
4
go.mod
4
go.mod
@ -5,12 +5,12 @@ go 1.23.4
|
||||
require (
|
||||
git.defalsify.org/vise.git v0.3.2-0.20250507172020-cb22240f1cb9
|
||||
git.grassecon.net/grassrootseconomics/common v0.9.0-beta.1.0.20250417111317-2953f4c2f32e
|
||||
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250428082711-5d221b8d565f
|
||||
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250517114512-050998ff82b1
|
||||
git.grassecon.net/grassrootseconomics/visedriver v0.9.0-beta.2.0.20250408094335-e2d1f65bb306
|
||||
git.grassecon.net/grassrootseconomics/visedriver-africastalking v0.0.0-20250129070628-5a539172c694
|
||||
github.com/alecthomas/assert/v2 v2.2.2
|
||||
github.com/gofrs/uuid v4.4.0+incompatible
|
||||
github.com/grassrootseconomics/ussd-data-service v1.4.0-beta
|
||||
github.com/grassrootseconomics/ussd-data-service v1.4.4-beta
|
||||
github.com/jackc/pgx/v5 v5.7.1
|
||||
github.com/peteole/testdata-loader v0.3.0
|
||||
github.com/stretchr/testify v1.9.0
|
||||
|
8
go.sum
8
go.sum
@ -6,6 +6,12 @@ git.grassecon.net/grassrootseconomics/common v0.9.0-beta.1.0.20250417111317-2953
|
||||
git.grassecon.net/grassrootseconomics/common v0.9.0-beta.1.0.20250417111317-2953f4c2f32e/go.mod h1:wgQJZGIS6QuNLHqDhcsvehsbn5PvgV7aziRebMnJi60=
|
||||
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250428082711-5d221b8d565f h1:OAHCP3YR1C5h1WFnnEnLs5kn6jTxQHQYWYtQaMZJIMY=
|
||||
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250428082711-5d221b8d565f/go.mod h1:gOn89ipaDcDvmQXRMQYKUqcw/sJcwVOPVt2eC6Geip8=
|
||||
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250516094326-3b85167ad84a h1:QNh0NaKtGbSeRPlTVKEAnqc0R5rnVrpDMrCHD/EaU5U=
|
||||
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250516094326-3b85167ad84a/go.mod h1:K/TPgZ4OhPHBQq2X0ab3JZs4YjiexzSURZcfHLs9Pf4=
|
||||
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250517113706-ee434dba6980 h1:FQUwTDFWduY7gCMi1U2OiFlsuAHLojWUw2hvZ4cGC2s=
|
||||
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250517113706-ee434dba6980/go.mod h1:K/TPgZ4OhPHBQq2X0ab3JZs4YjiexzSURZcfHLs9Pf4=
|
||||
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250517114512-050998ff82b1 h1:0hvILlGkZnXO8o7nZth4xu77vAS4zVQ6Ae0rb5x/Idg=
|
||||
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250517114512-050998ff82b1/go.mod h1:K/TPgZ4OhPHBQq2X0ab3JZs4YjiexzSURZcfHLs9Pf4=
|
||||
git.grassecon.net/grassrootseconomics/visedriver v0.9.0-beta.2.0.20250408094335-e2d1f65bb306 h1:Jo+yWysWw/N5BJQtAyEMN8ePVvAyPHv+JG4lQti+1N4=
|
||||
git.grassecon.net/grassrootseconomics/visedriver v0.9.0-beta.2.0.20250408094335-e2d1f65bb306/go.mod h1:FdLwYtzsjOIcDiW4uDgDYnB4Wdzq12uJUe0QHSSPbSo=
|
||||
git.grassecon.net/grassrootseconomics/visedriver-africastalking v0.0.0-20250129070628-5a539172c694 h1:DjJlBSz0S13acft5XZDWk7ZYnzElym0xLMYEVgyNJ+E=
|
||||
@ -66,6 +72,8 @@ github.com/grassrootseconomics/ussd-data-service v1.2.0-beta h1:fn1gwbWIwHVEBtUC
|
||||
github.com/grassrootseconomics/ussd-data-service v1.2.0-beta/go.mod h1:omfI0QtUwIdpu9gMcUqLMCG8O1XWjqJGBx1qUMiGWC0=
|
||||
github.com/grassrootseconomics/ussd-data-service v1.4.0-beta h1:4fMd/3h2ZIhRg4GdHQmRw5FfD3MpJvFNNJQo+Q27f5M=
|
||||
github.com/grassrootseconomics/ussd-data-service v1.4.0-beta/go.mod h1:9sGnorpKaK76FmOGXoh/xv7x5siSFNYdXxQo9BKW4DI=
|
||||
github.com/grassrootseconomics/ussd-data-service v1.4.4-beta h1:turlyo0i3OLj29mWpWNoB/3Qao8qEngT/5d1jDWTZE4=
|
||||
github.com/grassrootseconomics/ussd-data-service v1.4.4-beta/go.mod h1:9sGnorpKaK76FmOGXoh/xv7x5siSFNYdXxQo9BKW4DI=
|
||||
github.com/graygnuorg/go-gdbm v0.0.0-20220711140707-71387d66dce4 h1:U4kkNYryi/qfbBF8gh7Vsbuz+cVmhf5kt6pE9bYYyLo=
|
||||
github.com/graygnuorg/go-gdbm v0.0.0-20220711140707-71387d66dce4/go.mod h1:zpZDgZFzeq9s0MIeB1P50NIEWDFFHSFBohI/NbaTD/Y=
|
||||
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
|
||||
|
@ -2659,6 +2659,12 @@ func (h *MenuHandlers) ClearTemporaryValue(ctx context.Context, sym string, inpu
|
||||
// GetPools fetches a list of 5 top pools
|
||||
func (h *MenuHandlers) GetPools(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")
|
||||
}
|
||||
userStore := h.userdataStore
|
||||
|
||||
flag_api_error, _ := h.flagManager.GetFlag("flag_api_error")
|
||||
|
||||
// call the api to get a list of top 5 pools sorted by swaps
|
||||
@ -2683,9 +2689,11 @@ func (h *MenuHandlers) GetPools(ctx context.Context, sym string, input []byte) (
|
||||
storedb.DATA_POOL_ADDRESSES: data.PoolContractAdrresses,
|
||||
}
|
||||
|
||||
// Write data entries
|
||||
for key, value := range dataMap {
|
||||
if err := h.prefixDb.Put(ctx, []byte(storedb.ToBytes(key)), []byte(value)); err != nil {
|
||||
return res, 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)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
@ -2694,100 +2702,6 @@ func (h *MenuHandlers) GetPools(ctx context.Context, sym string, input []byte) (
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// LoadSwapFromList gets all the possible tokens a user can select to swap from
|
||||
func (h *MenuHandlers) LoadSwapFromList(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")
|
||||
}
|
||||
|
||||
userStore := h.userdataStore
|
||||
publicKey, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY)
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "failed to read publicKey entry with", "key", storedb.DATA_PUBLIC_KEY, "error", err)
|
||||
return res, err
|
||||
}
|
||||
|
||||
flag_incorrect_pool, _ := h.flagManager.GetFlag("flag_incorrect_pool")
|
||||
flag_api_error, _ := h.flagManager.GetFlag("flag_api_error")
|
||||
|
||||
inputStr := string(input)
|
||||
if inputStr == "0" {
|
||||
res.FlagReset = append(res.FlagReset, flag_incorrect_pool)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
poolData, err := store.GetPoolData(ctx, h.prefixDb, inputStr)
|
||||
if err != nil {
|
||||
return res, fmt.Errorf("failed to retrieve pool data: %v", err)
|
||||
}
|
||||
|
||||
if poolData == nil {
|
||||
// no match found. Call the API
|
||||
poolResp := dataserviceapi.PoolDetails{
|
||||
PoolName: "DevTest",
|
||||
PoolSymbol: "DEVT",
|
||||
PoolContractAdrress: "0x145F87d6198dEDD45C614FFD8b70E9a2fCCc5cc9",
|
||||
LimiterAddress: "",
|
||||
VoucherRegistry: "",
|
||||
}
|
||||
|
||||
if (poolResp == dataserviceapi.PoolDetails{}) {
|
||||
// If the API does not return the data, set the flag
|
||||
res.FlagSet = append(res.FlagSet, flag_incorrect_pool)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
poolData = &poolResp
|
||||
}
|
||||
|
||||
activePoolAddress := poolData.PoolContractAdrress
|
||||
|
||||
// set the active pool contract address
|
||||
err = userStore.WriteEntry(ctx, sessionId, storedb.DATA_ACTIVE_POOL_ADDRESS, []byte(activePoolAddress))
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "failed to write active PoolContractAdrress entry with", "key", storedb.DATA_ACTIVE_POOL_ADDRESS, "value", activePoolAddress, "error", err)
|
||||
return res, err
|
||||
}
|
||||
|
||||
res.FlagReset = append(res.FlagReset, flag_incorrect_pool)
|
||||
|
||||
// call the api using the activePoolAddress and publicKey to get a list of SwapfromSymbolsData
|
||||
swapFromList, err := h.accountService.GetPoolSwappableFromVouchers(ctx, activePoolAddress, string(publicKey))
|
||||
if err != nil {
|
||||
res.FlagSet = append(res.FlagSet, flag_api_error)
|
||||
logg.ErrorCtxf(ctx, "failed on FetchTransactions", "error", err)
|
||||
return res, err
|
||||
}
|
||||
|
||||
// Return if there are no vouchers
|
||||
if len(swapFromList) == 0 {
|
||||
res.FlagSet = append(res.FlagSet, flag_incorrect_pool)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
data := store.ProcessVouchers(swapFromList)
|
||||
|
||||
// Store all from list voucher data
|
||||
dataMap := map[storedb.DataTyp]string{
|
||||
storedb.DATA_POOL_FROM_SYMBOLS: data.Symbols,
|
||||
storedb.DATA_POOL_FROM_BALANCES: data.Balances,
|
||||
storedb.DATA_POOL_FROM_DECIMALS: data.Decimals,
|
||||
storedb.DATA_POOL_FROM_ADDRESSES: data.Addresses,
|
||||
}
|
||||
|
||||
for key, value := range dataMap {
|
||||
if err := h.prefixDb.Put(ctx, []byte(storedb.ToBytes(key)), []byte(value)); err != nil {
|
||||
return res, nil
|
||||
}
|
||||
}
|
||||
|
||||
res.Content = h.ReplaceSeparatorFunc(data.Symbols)
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// LoadSwapFromList returns a list of possible vouchers to swap to
|
||||
func (h *MenuHandlers) LoadSwapToList(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
var res resource.Result
|
||||
@ -2803,6 +2717,22 @@ func (h *MenuHandlers) LoadSwapToList(ctx context.Context, sym string, input []b
|
||||
return res, err
|
||||
}
|
||||
|
||||
// get the active address and symbol
|
||||
activeAddress, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_ADDRESS)
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "failed to read activeAddress entry with", "key", storedb.DATA_ACTIVE_ADDRESS, "error", err)
|
||||
return res, err
|
||||
}
|
||||
activeSym, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_SYM)
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "failed to read activeSym entry with", "key", storedb.DATA_ACTIVE_SYM, "error", err)
|
||||
return res, err
|
||||
}
|
||||
|
||||
code := codeFromCtx(ctx)
|
||||
l := gotext.NewLocale(translationDir, code)
|
||||
l.AddDomain("default")
|
||||
|
||||
flag_incorrect_voucher, _ := h.flagManager.GetFlag("flag_incorrect_voucher")
|
||||
flag_api_error, _ := h.flagManager.GetFlag("flag_api_error")
|
||||
|
||||
@ -2811,30 +2741,42 @@ func (h *MenuHandlers) LoadSwapToList(ctx context.Context, sym string, input []b
|
||||
return res, nil
|
||||
}
|
||||
|
||||
metadata, err := store.GetSwapFromVoucherData(ctx, h.prefixDb, inputStr)
|
||||
if err != nil {
|
||||
return res, fmt.Errorf("failed to retrieve swap from voucher data: %v", err)
|
||||
defaultPool := dataserviceapi.PoolDetails{
|
||||
PoolName: "Kenya ROLA Pool",
|
||||
PoolSymbol: "ROLA",
|
||||
PoolContractAdrress: "0x48a953cA5cf5298bc6f6Af3C608351f537AAcb9e",
|
||||
LimiterAddress: "",
|
||||
VoucherRegistry: "",
|
||||
}
|
||||
|
||||
if metadata == nil {
|
||||
activePoolAddress := defaultPool.PoolContractAdrress
|
||||
|
||||
// set the active pool contract address
|
||||
err = userStore.WriteEntry(ctx, sessionId, storedb.DATA_ACTIVE_POOL_ADDRESS, []byte(activePoolAddress))
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "failed to write active PoolContractAdrress entry with", "key", storedb.DATA_ACTIVE_POOL_ADDRESS, "value", activePoolAddress, "error", err)
|
||||
return res, err
|
||||
}
|
||||
|
||||
// call the api using the ActivePoolAddress and ActiveVoucherAddress to check if it is part of the pool
|
||||
r, err := h.accountService.CheckTokenInPool(ctx, activePoolAddress, string(activeAddress))
|
||||
if err != nil {
|
||||
res.FlagSet = append(res.FlagSet, flag_api_error)
|
||||
logg.ErrorCtxf(ctx, "failed on CheckTokenInPool", "error", err)
|
||||
return res, err
|
||||
}
|
||||
|
||||
if !r.CanSwapFrom {
|
||||
res.FlagSet = append(res.FlagSet, flag_incorrect_voucher)
|
||||
res.Content = l.Get(
|
||||
"%s is not in the KENYA ROLA POOL. Please update your voucher and try again.",
|
||||
activeSym,
|
||||
)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// Store the active swap from data
|
||||
if err := store.UpdateSwapFromVoucherData(ctx, userStore, sessionId, metadata); err != nil {
|
||||
logg.ErrorCtxf(ctx, "failed on UpdateSwapFromVoucherData", "error", err)
|
||||
return res, err
|
||||
}
|
||||
|
||||
res.FlagReset = append(res.FlagReset, flag_incorrect_voucher)
|
||||
|
||||
activePoolAddress, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_POOL_ADDRESS)
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "failed to read activePoolAddress entry with", "key", storedb.DATA_ACTIVE_POOL_ADDRESS, "error", err)
|
||||
return res, err
|
||||
}
|
||||
|
||||
// call the api using the activePoolAddress and publicKey to get a list of SwapToSymbolsData
|
||||
swapToList, err := h.accountService.GetPoolSwappableVouchers(ctx, string(activePoolAddress), string(publicKey))
|
||||
if err != nil {
|
||||
@ -2859,8 +2801,9 @@ func (h *MenuHandlers) LoadSwapToList(ctx context.Context, sym string, input []b
|
||||
}
|
||||
|
||||
for key, value := range dataMap {
|
||||
if err := h.prefixDb.Put(ctx, []byte(storedb.ToBytes(key)), []byte(value)); err != nil {
|
||||
return res, 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)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
@ -2890,7 +2833,8 @@ func (h *MenuHandlers) SwapMaxLimit(ctx context.Context, sym string, input []byt
|
||||
return res, nil
|
||||
}
|
||||
|
||||
metadata, err := store.GetSwapToVoucherData(ctx, h.prefixDb, inputStr)
|
||||
userStore := h.userdataStore
|
||||
metadata, err := store.GetSwapToVoucherData(ctx, userStore, sessionId, inputStr)
|
||||
if err != nil {
|
||||
return res, fmt.Errorf("failed to retrieve swap to voucher data: %v", err)
|
||||
}
|
||||
@ -2899,8 +2843,6 @@ func (h *MenuHandlers) SwapMaxLimit(ctx context.Context, sym string, input []byt
|
||||
return res, nil
|
||||
}
|
||||
|
||||
userStore := h.userdataStore
|
||||
|
||||
// Store the active swap_to data
|
||||
if err := store.UpdateSwapToVoucherData(ctx, userStore, sessionId, metadata); err != nil {
|
||||
logg.ErrorCtxf(ctx, "failed on UpdateSwapToVoucherData", "error", err)
|
||||
|
@ -130,10 +130,7 @@ func (ls *LocalHandlerService) GetHandler(accountService remote.AccountService)
|
||||
ls.DbRs.AddLocalFunc("confirm_new_alias", appHandlers.ConfirmNewAlias)
|
||||
ls.DbRs.AddLocalFunc("check_account_created", appHandlers.CheckAccountCreated)
|
||||
ls.DbRs.AddLocalFunc("reset_api_call_failure", appHandlers.ResetApiCallFailure)
|
||||
|
||||
ls.DbRs.AddLocalFunc("get_pools", appHandlers.GetPools)
|
||||
ls.DbRs.AddLocalFunc("swap_from_list", appHandlers.LoadSwapFromList)
|
||||
ls.DbRs.AddLocalFunc("swap_to_list", appHandlers.LoadSwapToList)
|
||||
ls.DbRs.AddLocalFunc("swap_to_list", appHandlers.LoadSwapToList)
|
||||
ls.DbRs.AddLocalFunc("swap_max_limit", appHandlers.SwapMaxLimit)
|
||||
ls.DbRs.AddLocalFunc("swap_preview", appHandlers.SwapPreview)
|
||||
ls.DbRs.AddLocalFunc("initiate_swap", appHandlers.InitiateSwap)
|
||||
|
@ -1 +1 @@
|
||||
Failed to connect to the custodial service .Please try again.
|
||||
Failed to connect to the custodial service. Please try again.
|
@ -35,4 +35,7 @@ msgid "Your request has been sent. You will receive an SMS when your %s %s has b
|
||||
msgstr "Ombi lako limetumwa. Utapokea SMS wakati %s %s yako itakapobadilishwa kuwa %s."
|
||||
|
||||
msgid "%s balance: %s\n"
|
||||
msgstr "%s salio: %s\n"
|
||||
msgstr "%s salio: %s\n"
|
||||
|
||||
msgid "%s is not in the KENYA ROLA POOL. Please update your voucher and try again."
|
||||
msgstr "%s haipo kwenye BWAWA LA KENYA ROLA. Tafadhali badilisha sarafu yako na ujaribu tena."
|
@ -15,7 +15,7 @@ MOUT help 5
|
||||
MOUT quit 9
|
||||
HALT
|
||||
INCMP send 1
|
||||
INCMP pool_swap 2
|
||||
INCMP swap_to_list 2
|
||||
INCMP my_vouchers 3
|
||||
INCMP my_account 4
|
||||
INCMP help 5
|
||||
|
1
services/registration/missing_voucher
Normal file
1
services/registration/missing_voucher
Normal file
@ -0,0 +1 @@
|
||||
{{.swap_to_list}}
|
6
services/registration/missing_voucher.vis
Normal file
6
services/registration/missing_voucher.vis
Normal file
@ -0,0 +1,6 @@
|
||||
MAP swap_to_list
|
||||
MOUT back 0
|
||||
MOUT quit 9
|
||||
HALT
|
||||
INCMP ^ 0
|
||||
INCMP quit 9
|
1
services/registration/missing_voucher_swa
Normal file
1
services/registration/missing_voucher_swa
Normal file
@ -0,0 +1 @@
|
||||
{{.swap_to_list}}
|
@ -1,2 +0,0 @@
|
||||
Enter number or pool symbol to swap from:
|
||||
{{.get_pools}}
|
@ -1,8 +0,0 @@
|
||||
LOAD get_pools 100
|
||||
MAP get_pools
|
||||
MOUT back 0
|
||||
MOUT quit 99
|
||||
HALT
|
||||
INCMP _ 0
|
||||
INCMP quit 99
|
||||
INCMP swap_from_list *
|
@ -1,2 +0,0 @@
|
||||
Chagua bwawa la sarafu la kubadilishana:
|
||||
{{.get_pools}}
|
@ -1,2 +0,0 @@
|
||||
Select number or symbol to swap FROM:
|
||||
{{.swap_from_list}}
|
@ -1,7 +0,0 @@
|
||||
LOAD swap_from_list 0
|
||||
CATCH _ flag_incorrect_pool 1
|
||||
MAP swap_from_list
|
||||
MOUT back 0
|
||||
HALT
|
||||
INCMP _ 0
|
||||
INCMP swap_to_list *
|
@ -1,2 +0,0 @@
|
||||
Chagua nambari au ishara ya sarafu kubadilisha KUTOKA:
|
||||
{{.swap_from_list}}
|
@ -1,6 +1,7 @@
|
||||
LOAD swap_to_list 0
|
||||
CATCH _ flag_incorrect_voucher 1
|
||||
RELOAD swap_to_list
|
||||
MAP swap_to_list
|
||||
CATCH missing_voucher flag_incorrect_voucher 1
|
||||
MOUT back 0
|
||||
HALT
|
||||
LOAD swap_max_limit 64
|
||||
|
@ -23,8 +23,8 @@ func ProcessPools(pools []dataserviceapi.PoolDetails) PoolsMetadata {
|
||||
|
||||
for i, p := range pools {
|
||||
poolNames = append(poolNames, fmt.Sprintf("%d:%s", i+1, p.PoolName))
|
||||
poolSymbols = append(poolSymbols, fmt.Sprintf("%d:%s", i+1, p.PoolSymbol))
|
||||
poolContractAdrresses = append(poolContractAdrresses, fmt.Sprintf("%d:%s", i+1, p.PoolContractAdrress))
|
||||
poolSymbols = append(poolSymbols, fmt.Sprintf("%d:%s", i+1, p.PoolSymbol))
|
||||
poolContractAdrresses = append(poolContractAdrresses, fmt.Sprintf("%d:%s", i+1, p.PoolContractAdrress))
|
||||
}
|
||||
|
||||
data.PoolNames = strings.Join(poolNames, "\n")
|
||||
@ -36,7 +36,7 @@ func ProcessPools(pools []dataserviceapi.PoolDetails) PoolsMetadata {
|
||||
|
||||
// GetPoolData retrieves and matches pool data
|
||||
// if no match is found, it fetches the API with the symbol
|
||||
func GetPoolData(ctx context.Context, db storedb.PrefixDb, input string) (*dataserviceapi.PoolDetails, error) {
|
||||
func GetPoolData(ctx context.Context, store DataStore, sessionId string, input string) (*dataserviceapi.PoolDetails, error) {
|
||||
keys := []storedb.DataTyp{
|
||||
storedb.DATA_POOL_NAMES,
|
||||
storedb.DATA_POOL_SYMBOLS,
|
||||
@ -45,9 +45,9 @@ func GetPoolData(ctx context.Context, db storedb.PrefixDb, input string) (*datas
|
||||
data := make(map[storedb.DataTyp]string)
|
||||
|
||||
for _, key := range keys {
|
||||
value, err := db.Get(ctx, storedb.ToBytes(key))
|
||||
value, err := store.ReadEntry(ctx, sessionId, key)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get prefix key %x: %v", storedb.ToBytes(key), err)
|
||||
return nil, fmt.Errorf("failed to get data key %x: %v", key, err)
|
||||
}
|
||||
data[key] = string(value)
|
||||
}
|
||||
@ -63,9 +63,9 @@ func GetPoolData(ctx context.Context, db storedb.PrefixDb, input string) (*datas
|
||||
}
|
||||
|
||||
return &dataserviceapi.PoolDetails{
|
||||
PoolName: string(name),
|
||||
PoolSymbol: string(symbol),
|
||||
PoolContractAdrress: string(address),
|
||||
PoolName: string(name),
|
||||
PoolSymbol: string(symbol),
|
||||
PoolContractAdrress: string(address),
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
@ -37,9 +37,9 @@ func ReadSwapData(ctx context.Context, store DataStore, sessionId string) (SwapD
|
||||
fieldToKey := map[string]storedb.DataTyp{
|
||||
"PublicKey": storedb.DATA_PUBLIC_KEY,
|
||||
"ActivePoolAddress": storedb.DATA_ACTIVE_POOL_ADDRESS,
|
||||
"ActiveSwapFromSym": storedb.DATA_ACTIVE_SWAP_FROM_SYM,
|
||||
"ActiveSwapFromDecimal": storedb.DATA_ACTIVE_SWAP_FROM_DECIMAL,
|
||||
"ActiveSwapFromAddress": storedb.DATA_ACTIVE_SWAP_FROM_ADDRESS,
|
||||
"ActiveSwapFromSym": storedb.DATA_ACTIVE_SYM,
|
||||
"ActiveSwapFromDecimal": storedb.DATA_ACTIVE_DECIMAL,
|
||||
"ActiveSwapFromAddress": storedb.DATA_ACTIVE_ADDRESS,
|
||||
"ActiveSwapToSym": storedb.DATA_ACTIVE_SWAP_TO_SYM,
|
||||
"ActiveSwapToAddress": storedb.DATA_ACTIVE_SWAP_TO_ADDRESS,
|
||||
}
|
||||
@ -66,10 +66,10 @@ func ReadSwapPreviewData(ctx context.Context, store DataStore, sessionId string)
|
||||
fieldToKey := map[string]storedb.DataTyp{
|
||||
"PublicKey": storedb.DATA_PUBLIC_KEY,
|
||||
"ActiveSwapMaxAmount": storedb.DATA_ACTIVE_SWAP_MAX_AMOUNT,
|
||||
"ActiveSwapFromDecimal": storedb.DATA_ACTIVE_SWAP_FROM_DECIMAL,
|
||||
"ActiveSwapFromDecimal": storedb.DATA_ACTIVE_DECIMAL,
|
||||
"ActivePoolAddress": storedb.DATA_ACTIVE_POOL_ADDRESS,
|
||||
"ActiveSwapFromAddress": storedb.DATA_ACTIVE_SWAP_FROM_ADDRESS,
|
||||
"ActiveSwapFromSym": storedb.DATA_ACTIVE_SWAP_FROM_SYM,
|
||||
"ActiveSwapFromAddress": storedb.DATA_ACTIVE_ADDRESS,
|
||||
"ActiveSwapFromSym": storedb.DATA_ACTIVE_SYM,
|
||||
"ActiveSwapToAddress": storedb.DATA_ACTIVE_SWAP_TO_ADDRESS,
|
||||
"ActiveSwapToSym": storedb.DATA_ACTIVE_SWAP_TO_SYM,
|
||||
"ActiveSwapToDecimal": storedb.DATA_ACTIVE_SWAP_TO_DECIMAL,
|
||||
@ -93,7 +93,7 @@ func ReadSwapPreviewData(ctx context.Context, store DataStore, sessionId string)
|
||||
}
|
||||
|
||||
// GetSwapFromVoucherData retrieves and matches swap from voucher data
|
||||
func GetSwapFromVoucherData(ctx context.Context, db storedb.PrefixDb, input string) (*dataserviceapi.TokenHoldings, error) {
|
||||
func GetSwapFromVoucherData(ctx context.Context, store DataStore, sessionId string, input string) (*dataserviceapi.TokenHoldings, error) {
|
||||
keys := []storedb.DataTyp{
|
||||
storedb.DATA_POOL_FROM_SYMBOLS,
|
||||
storedb.DATA_POOL_FROM_BALANCES,
|
||||
@ -103,9 +103,9 @@ func GetSwapFromVoucherData(ctx context.Context, db storedb.PrefixDb, input stri
|
||||
data := make(map[storedb.DataTyp]string)
|
||||
|
||||
for _, key := range keys {
|
||||
value, err := db.Get(ctx, storedb.ToBytes(key))
|
||||
value, err := store.ReadEntry(ctx, sessionId, key)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get prefix key %x: %v", storedb.ToBytes(key), err)
|
||||
return nil, fmt.Errorf("failed to get data key %x: %v", key, err)
|
||||
}
|
||||
data[key] = string(value)
|
||||
}
|
||||
@ -129,28 +129,8 @@ func GetSwapFromVoucherData(ctx context.Context, db storedb.PrefixDb, input stri
|
||||
}, nil
|
||||
}
|
||||
|
||||
// UpdateSwapFromVoucherData updates the active swap to voucher data in the DataStore.
|
||||
func UpdateSwapFromVoucherData(ctx context.Context, store DataStore, sessionId string, data *dataserviceapi.TokenHoldings) error {
|
||||
logg.TraceCtxf(ctx, "dtal", "data", data)
|
||||
// Active swap from voucher data entries
|
||||
activeEntries := map[storedb.DataTyp][]byte{
|
||||
storedb.DATA_ACTIVE_SWAP_FROM_SYM: []byte(data.TokenSymbol),
|
||||
storedb.DATA_ACTIVE_SWAP_FROM_DECIMAL: []byte(data.TokenDecimals),
|
||||
storedb.DATA_ACTIVE_SWAP_FROM_ADDRESS: []byte(data.ContractAddress),
|
||||
}
|
||||
|
||||
// Write active data
|
||||
for key, value := range activeEntries {
|
||||
if err := store.WriteEntry(ctx, sessionId, key, value); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetSwapToVoucherData retrieves and matches voucher data
|
||||
func GetSwapToVoucherData(ctx context.Context, db storedb.PrefixDb, input string) (*dataserviceapi.TokenHoldings, error) {
|
||||
func GetSwapToVoucherData(ctx context.Context, store DataStore, sessionId string, input string) (*dataserviceapi.TokenHoldings, error) {
|
||||
keys := []storedb.DataTyp{
|
||||
storedb.DATA_POOL_TO_SYMBOLS,
|
||||
storedb.DATA_POOL_TO_BALANCES,
|
||||
@ -160,9 +140,9 @@ func GetSwapToVoucherData(ctx context.Context, db storedb.PrefixDb, input string
|
||||
data := make(map[storedb.DataTyp]string)
|
||||
|
||||
for _, key := range keys {
|
||||
value, err := db.Get(ctx, storedb.ToBytes(key))
|
||||
value, err := store.ReadEntry(ctx, sessionId, key)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get prefix key %x: %v", storedb.ToBytes(key), err)
|
||||
return nil, fmt.Errorf("failed to get data key %x: %v", key, err)
|
||||
}
|
||||
data[key] = string(value)
|
||||
}
|
||||
|
@ -1,15 +1,10 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
visedb "git.defalsify.org/vise.git/db"
|
||||
memdb "git.defalsify.org/vise.git/db/mem"
|
||||
storedb "git.grassecon.net/grassrootseconomics/sarafu-vise/store/db"
|
||||
"github.com/alecthomas/assert/v2"
|
||||
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestReadSwapData(t *testing.T) {
|
||||
@ -95,16 +90,8 @@ func TestReadSwapPreviewData(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetSwapFromVoucherData(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
db := memdb.NewMemDb()
|
||||
err := db.Connect(ctx, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
prefix := storedb.ToBytes(visedb.DATATYPE_USERDATA)
|
||||
spdb := storedb.NewSubPrefixDb(db, prefix)
|
||||
sessionId := "session123"
|
||||
ctx, store := InitializeTestDb(t)
|
||||
|
||||
// Test pool swap data
|
||||
mockData := map[storedb.DataTyp][]byte{
|
||||
@ -116,13 +103,12 @@ func TestGetSwapFromVoucherData(t *testing.T) {
|
||||
|
||||
// Put the data
|
||||
for key, value := range mockData {
|
||||
err = spdb.Put(ctx, []byte(storedb.ToBytes(key)), []byte(value))
|
||||
if err != nil {
|
||||
if err := store.WriteEntry(ctx, sessionId, key, []byte(value)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
result, err := GetSwapFromVoucherData(ctx, spdb, "1")
|
||||
result, err := GetSwapFromVoucherData(ctx, store, sessionId, "1")
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "AMANI", result.TokenSymbol)
|
||||
@ -131,54 +117,9 @@ func TestGetSwapFromVoucherData(t *testing.T) {
|
||||
assert.Equal(t, "0xc7B78Ac9ACB9E025C8234621FC515bC58179dEAe", result.ContractAddress)
|
||||
}
|
||||
|
||||
func TestUpdateSwapFromVoucherData(t *testing.T) {
|
||||
ctx, store := InitializeTestDb(t)
|
||||
sessionId := "session123"
|
||||
|
||||
// New swap from voucher data
|
||||
newData := &dataserviceapi.TokenHoldings{
|
||||
TokenSymbol: "AMANI",
|
||||
TokenDecimals: "6",
|
||||
ContractAddress: "0xc7B78Ac9ACB9E025C8234621FC515bC58179dEAe",
|
||||
}
|
||||
|
||||
// Old temporary data
|
||||
tempData := &dataserviceapi.TokenHoldings{
|
||||
TokenSymbol: "OLD",
|
||||
TokenDecimals: "8",
|
||||
ContractAddress: "0xold",
|
||||
}
|
||||
require.NoError(t, StoreTemporaryVoucher(ctx, store, sessionId, tempData))
|
||||
|
||||
// Execute update
|
||||
err := UpdateSwapFromVoucherData(ctx, store, sessionId, newData)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Verify active swap from data was stored correctly
|
||||
activeEntries := map[storedb.DataTyp][]byte{
|
||||
storedb.DATA_ACTIVE_SWAP_FROM_SYM: []byte(newData.TokenSymbol),
|
||||
storedb.DATA_ACTIVE_SWAP_FROM_DECIMAL: []byte(newData.TokenDecimals),
|
||||
storedb.DATA_ACTIVE_SWAP_FROM_ADDRESS: []byte(newData.ContractAddress),
|
||||
}
|
||||
|
||||
for key, expectedValue := range activeEntries {
|
||||
storedValue, err := store.ReadEntry(ctx, sessionId, key)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, expectedValue, storedValue, "Active swap from data mismatch for key %v", key)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetSwapToVoucherData(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
db := memdb.NewMemDb()
|
||||
err := db.Connect(ctx, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
prefix := storedb.ToBytes(visedb.DATATYPE_USERDATA)
|
||||
spdb := storedb.NewSubPrefixDb(db, prefix)
|
||||
sessionId := "session123"
|
||||
ctx, store := InitializeTestDb(t)
|
||||
|
||||
// Test pool swap data
|
||||
mockData := map[storedb.DataTyp][]byte{
|
||||
@ -190,17 +131,16 @@ func TestGetSwapToVoucherData(t *testing.T) {
|
||||
|
||||
// Put the data
|
||||
for key, value := range mockData {
|
||||
err = spdb.Put(ctx, []byte(storedb.ToBytes(key)), []byte(value))
|
||||
if err != nil {
|
||||
if err := store.WriteEntry(ctx, sessionId, key, []byte(value)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
result, err := GetSwapToVoucherData(ctx, spdb, "1")
|
||||
result, err := GetSwapToVoucherData(ctx, store, sessionId, "1")
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "cUSD", result.TokenSymbol)
|
||||
assert.Equal(t, "", result.Balance)
|
||||
assert.Equal(t, "6", result.TokenDecimals)
|
||||
assert.Equal(t, "0xc7B78Ac9ACB9E025C8234621", result.ContractAddress)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user