Compare commits

..

34 Commits

Author SHA1 Message Date
alfred-mk
5a9fdbd580 use the stored DATA_ACTIVE_POOL_ADDRESS or fall back to default config.DefaultPoolAddress() in loading the swapToList 2025-06-07 12:56:41 +03:00
alfred-mk
310f15d59d add a catch for the flag_api_call_error 2025-06-07 12:43:52 +03:00
alfred-mk
3bfc0169ee correctly set the flag_incorrect_pool flag 2025-06-07 05:59:19 +03:00
alfred-mk
5d8daf56bc add pool related functions 2025-06-07 04:41:48 +03:00
alfred-mk
6532dd89f4 add pool related helper functions, for storing, reading and updating pool data 2025-06-07 04:40:51 +03:00
alfred-mk
22cf59f82a add keys for the DATA_ACTIVE_POOL_NAME and DATA_ACTIVE_POOL_SYM 2025-06-07 04:40:03 +03:00
alfred-mk
b574db1c89 add the pool_set node that stores the selected pool as the default 2025-06-07 04:37:55 +03:00
alfred-mk
419a020346 add the view_pool node that requests for a PIN before setting the selected Pool 2025-06-07 04:37:08 +03:00
alfred-mk
57287183bd add the displayed menu item for the select_pool node 2025-06-07 04:36:13 +03:00
alfred-mk
3e08d19975 add the select_pool node that shows the current default pool 2025-06-07 04:35:37 +03:00
alfred-mk
626538f38e add the navigation to the select_pool node 2025-06-07 04:34:50 +03:00
alfred-mk
1eb51bf8f3 add the default swahili translation for the view pool node 2025-06-07 04:34:09 +03:00
alfred-mk
2f706c1c0f use updated sarafu-api package 2025-06-07 04:33:26 +03:00
alfred-mk
9a1d3c0176 remove misplaced CATCH for the flag_incorrect_pin 2025-06-07 03:34:56 +03:00
alfred-mk
7228b818e3 swap-fix (#78)
Some checks failed
release / docker (push) Has been cancelled
- Resolves a "key not found error" on the swap max limit node
- Resolves double PIN requests when authorizing transactions #67

Reviewed-on: #78
Co-authored-by: alfred-mk <alfredmwaik@gmail.com>
Co-committed-by: alfred-mk <alfredmwaik@gmail.com>
2025-06-03 09:19:13 +02:00
alfred-mk
fac703576f updated the go-vise package to commit hash a170e8a79da067e30b3c28a999a4d87fdd07ebaf
Some checks failed
release / docker (push) Has been cancelled
2025-05-23 15:30:48 +03:00
alfred-mk
df8e0b4d4a remove misplaced CATCH statement 2025-05-23 15:28:40 +03:00
alfred-mk
b4bacfdc03 use the updated sarafu-api package with correct PoolSwapURL
Some checks failed
release / docker (push) Has been cancelled
2025-05-22 15:34:04 +03:00
alfred-mk
8dc203d111 use the TemporaryValue to store the user's swap amount input
Some checks failed
release / docker (push) Has been cancelled
2025-05-22 15:18:52 +03:00
alfred-mk
dcadea8737 Convert TokenDecimals (uint8) to string before storing it 2025-05-22 15:09:46 +03:00
alfred-mk
71496d0015 change position of the catch statements
Some checks failed
release / docker (push) Has been cancelled
2025-05-22 12:41:49 +03:00
alfred-mk
d9fc5440c5 prevent access to the swap menu if one does not have a voucher 2025-05-22 12:24:10 +03:00
alfred-mk
0495472530 match the reset_incorrect_pin symbol to the function name 2025-05-22 12:13:50 +03:00
alfred-mk
fd43060021 update the api failure template
Some checks failed
release / docker (push) Has been cancelled
2025-05-21 18:52:05 +03:00
alfred-mk
cf6e105fb9 catch the api call error flag 2025-05-21 18:51:41 +03:00
alfred-mk
91b57fece2 use the ProcessTokens function and remove the unused DATA_POOL_TO_BALANCES 2025-05-21 18:51:10 +03:00
alfred-mk
043c79384c correctly process TokenDetails 2025-05-21 18:39:45 +03:00
alfred-mk
1cc1d00ffe added a function to ProcessTokens from the GetPoolSwappableVouchers 2025-05-21 18:19:23 +03:00
alfred-mk
d7987bf460 use the updated ussd-data-service and sarafu-api with TokenDetails 2025-05-21 18:18:15 +03:00
alfred-mk
21da4b976b use the updated sarafu-api that removes the publicKey from the GetPoolSwappableVouchers
Some checks failed
release / docker (push) Has been cancelled
2025-05-21 15:45:17 +03:00
alfred-mk
169d85e2cb added the INCLUDE_STABLES_PARAM env variable 2025-05-21 15:39:19 +03:00
alfred-mk
3ab76b3d22 use updated sarafu-api with corrected JSON mapping for checkTokenInPool and getSwapFromTokenMaxLimit
Some checks failed
release / docker (push) Has been cancelled
2025-05-20 15:53:12 +03:00
alfred-mk
b428e2e22d add log for the CheckTokenInPool
Some checks failed
release / docker (push) Has been cancelled
2025-05-20 14:02:41 +03:00
1d23a0cc4b Merge pull request 'default-env-pool' (#76) from default-env-pool into master
Some checks failed
release / docker (push) Has been cancelled
Reviewed-on: #76
2025-05-20 12:18:30 +02:00
42 changed files with 371 additions and 117 deletions

View File

@@ -28,3 +28,4 @@ DEFAULT_POOL_SYMBOL=ROLA
DEFAULT_POOL_CONTRACT_ADDRESS=0x48a953cA5cf5298bc6f6Af3C608351f537AAcb9e
DEFAULT_LIMITER_ADDRESS=
DEFAULT_VOUCHER_REGISTRY=
INCLUDE_STABLES_PARAM=false

View File

@@ -15,7 +15,7 @@ import (
const (
changeHeadSrc = `LOAD reset_account_authorized 0
LOAD reset_incorrect 0
LOAD reset_incorrect_pin 0
CATCH incorrect_pin flag_incorrect_pin 1
CATCH pin_entry flag_account_authorized 0
`

6
go.mod
View File

@@ -3,14 +3,14 @@ module git.grassecon.net/grassrootseconomics/sarafu-vise
go 1.23.4
require (
git.defalsify.org/vise.git v0.3.2-0.20250507172020-cb22240f1cb9
git.defalsify.org/vise.git v0.3.2-0.20250507135825-a170e8a79da0
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.20250517114512-050998ff82b1
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250606194235-b5ccaea57560
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.4-beta
github.com/grassrootseconomics/ussd-data-service v1.4.7-beta
github.com/jackc/pgx/v5 v5.7.1
github.com/peteole/testdata-loader v0.3.0
github.com/stretchr/testify v1.9.0

16
go.sum
View File

@@ -1,5 +1,7 @@
git.defalsify.org/vise.git v0.3.2-0.20250425131748-8b84f59792ce h1:Uke2jQ4wG97gQKnTzxPyBGyhosrU1IWnRNFHtKVrmrk=
git.defalsify.org/vise.git v0.3.2-0.20250425131748-8b84f59792ce/go.mod h1:jyBMe1qTYUz3mmuoC9JQ/TvFeW0vTanCUcPu3H8p4Ck=
git.defalsify.org/vise.git v0.3.2-0.20250507135825-a170e8a79da0 h1:ByRD+b39zVOjOzOf3I1z2L6MSiMZCZT8y0P8MMAAorY=
git.defalsify.org/vise.git v0.3.2-0.20250507135825-a170e8a79da0/go.mod h1:flN+Gu+0BX0F6trZc5VpJcO4e6mj/+HUw0Z/UqVTFhk=
git.defalsify.org/vise.git v0.3.2-0.20250507172020-cb22240f1cb9 h1:4kjbYw25MHZe9fqSbujPzpFXrYutFfVipvLrcWYnYks=
git.defalsify.org/vise.git v0.3.2-0.20250507172020-cb22240f1cb9/go.mod h1:jyBMe1qTYUz3mmuoC9JQ/TvFeW0vTanCUcPu3H8p4Ck=
git.grassecon.net/grassrootseconomics/common v0.9.0-beta.1.0.20250417111317-2953f4c2f32e h1:DcC9qkNl9ny3hxQmsMK6W81+5R/j4ZwYUbvewMI/rlc=
@@ -12,6 +14,18 @@ git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250517113706-
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/sarafu-api v0.9.0-beta.1.0.20250520125035-81fbc2574c30 h1:iZVmR/4xSoxahtkMBXs/z1HUSSnr+x1m+9AxXIgTvb0=
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250520125035-81fbc2574c30/go.mod h1:R7+mt27mhm4/LCutAHHv87pOsLxXEPO93KXnhP8H6W0=
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250521124206-343d30a2f27c h1:c2cA5hcYnLgs7kR4fF5tp42W6VQClTukbfKoaeskZu0=
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250521124206-343d30a2f27c/go.mod h1:R7+mt27mhm4/LCutAHHv87pOsLxXEPO93KXnhP8H6W0=
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250521141246-6c3719e3b652 h1:MmUQRL6Fjq/jlL53h+cUbJ7WwQ+q/yNy/zT05c7bgNg=
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250521141246-6c3719e3b652/go.mod h1:wKHPy1mpOCr9ahkRltwm1yi9qH/3m9xb8hMCX5C0L1o=
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250522123108-24224e553de5 h1:7gVnkpybzg5lC7C8Rl4dejTbmBVpu5xfMMfda+d498U=
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250522123108-24224e553de5/go.mod h1:wKHPy1mpOCr9ahkRltwm1yi9qH/3m9xb8hMCX5C0L1o=
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250605174108-bf830e92dea2 h1:8cxGb7lSoNGJxjauIRGskp//EqisuOOZPoPj4oDwswE=
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250605174108-bf830e92dea2/go.mod h1:wKHPy1mpOCr9ahkRltwm1yi9qH/3m9xb8hMCX5C0L1o=
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250606194235-b5ccaea57560 h1:14QVGEgLdl1LyVS/mJFDVRGXHsH5IgmloNPKfk26K6M=
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250606194235-b5ccaea57560/go.mod h1:wKHPy1mpOCr9ahkRltwm1yi9qH/3m9xb8hMCX5C0L1o=
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=
@@ -74,6 +88,8 @@ github.com/grassrootseconomics/ussd-data-service v1.4.0-beta h1:4fMd/3h2ZIhRg4Gd
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/grassrootseconomics/ussd-data-service v1.4.7-beta h1:yAe1YaOBsdxW2m20jnVU4F0kLmFr+mK/gHCWEdHmE90=
github.com/grassrootseconomics/ussd-data-service v1.4.7-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=

View File

@@ -630,21 +630,11 @@ func (h *MenuHandlers) incrementIncorrectPINAttempts(ctx context.Context, sessio
// resetIncorrectPINAttempts resets the number of incorrect PIN attempts after a correct PIN entry
func (h *MenuHandlers) resetIncorrectPINAttempts(ctx context.Context, sessionId string) error {
store := h.userdataStore
currentWrongPinAttempts, err := store.ReadEntry(ctx, sessionId, storedb.DATA_INCORRECT_PIN_ATTEMPTS)
err := store.WriteEntry(ctx, sessionId, storedb.DATA_INCORRECT_PIN_ATTEMPTS, []byte(string("0")))
if err != nil {
if db.IsNotFound(err) {
return nil
}
logg.ErrorCtxf(ctx, "failed to reset incorrect PIN attempts ", "key", storedb.DATA_INCORRECT_PIN_ATTEMPTS, "error", err)
return err
}
currentWrongPinAttemptsCount, _ := strconv.ParseUint(string(currentWrongPinAttempts), 0, 64)
if currentWrongPinAttemptsCount <= uint64(pin.AllowedPINAttempts) {
err = store.WriteEntry(ctx, sessionId, storedb.DATA_INCORRECT_PIN_ATTEMPTS, []byte(string("0")))
if err != nil {
logg.ErrorCtxf(ctx, "failed to reset incorrect PIN attempts ", "key", storedb.DATA_INCORRECT_PIN_ATTEMPTS, "value", pin.AllowedPINAttempts, "error", err)
return err
}
}
return nil
}
@@ -1371,7 +1361,13 @@ func (h *MenuHandlers) Authorize(ctx context.Context, sym string, input []byte)
flag_incorrect_pin, _ := h.flagManager.GetFlag("flag_incorrect_pin")
flag_account_authorized, _ := h.flagManager.GetFlag("flag_account_authorized")
flag_allow_update, _ := h.flagManager.GetFlag("flag_allow_update")
flag_invalid_pin, _ := h.flagManager.GetFlag("flag_invalid_pin")
pinInput := string(input)
if !pin.IsValidPIN(pinInput) {
res.FlagReset = append(res.FlagReset, flag_account_authorized, flag_allow_update)
return res, nil
}
store := h.userdataStore
AccountPin, err := store.ReadEntry(ctx, sessionId, storedb.DATA_ACCOUNT_PIN)
@@ -1379,40 +1375,28 @@ func (h *MenuHandlers) Authorize(ctx context.Context, sym string, input []byte)
logg.ErrorCtxf(ctx, "failed to read AccountPin entry with", "key", storedb.DATA_ACCOUNT_PIN, "error", err)
return res, err
}
str := string(input)
_, err = strconv.Atoi(str)
if len(input) == 4 && err == nil {
if pin.VerifyPIN(string(AccountPin), string(input)) {
if h.st.MatchFlag(flag_account_authorized, false) {
res.FlagReset = append(res.FlagReset, flag_incorrect_pin)
res.FlagSet = append(res.FlagSet, flag_allow_update, flag_account_authorized)
err := h.resetIncorrectPINAttempts(ctx, sessionId)
if err != nil {
return res, err
}
} else {
res.FlagSet = append(res.FlagSet, flag_allow_update)
res.FlagReset = append(res.FlagReset, flag_account_authorized)
err := h.resetIncorrectPINAttempts(ctx, sessionId)
if err != nil {
return res, err
}
}
} else {
err = h.incrementIncorrectPINAttempts(ctx, sessionId)
if err != nil {
return res, err
}
res.FlagSet = append(res.FlagSet, flag_incorrect_pin)
res.FlagReset = append(res.FlagReset, flag_account_authorized)
return res, nil
// verify that the user provided the correct PIN
if pin.VerifyPIN(string(AccountPin), pinInput) {
// set the required flags for a valid PIN
res.FlagSet = append(res.FlagSet, flag_allow_update, flag_account_authorized)
res.FlagReset = append(res.FlagReset, flag_incorrect_pin)
err := h.resetIncorrectPINAttempts(ctx, sessionId)
if err != nil {
return res, err
}
} else {
if string(input) != "0" {
res.FlagSet = append(res.FlagSet, flag_invalid_pin)
// set the required flags for an incorrect PIN
res.FlagSet = append(res.FlagSet, flag_incorrect_pin)
res.FlagReset = append(res.FlagReset, flag_account_authorized, flag_allow_update)
err = h.incrementIncorrectPINAttempts(ctx, sessionId)
if err != nil {
return res, err
}
return res, nil
}
return res, nil
}
@@ -2261,6 +2245,110 @@ func (h *MenuHandlers) GetVoucherDetails(ctx context.Context, sym string, input
return res, nil
}
// GetDefaultPool returns the current user's Pool. If none is set, it returns the default config pool.
func (h *MenuHandlers) GetDefaultPool(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
activePoolSym, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_POOL_SYM)
if err != nil {
if db.IsNotFound(err) {
// set the default as the response
res.Content = config.DefaultPoolSymbol()
return res, nil
}
logg.ErrorCtxf(ctx, "failed to read the activePoolSym entry with", "key", storedb.DATA_ACTIVE_POOL_SYM, "error", err)
return res, err
}
res.Content = string(activePoolSym)
return res, nil
}
// ViewPool retrieves the pool details from the user store
// and displays it to the user for them to select it.
func (h *MenuHandlers) ViewPool(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_pool, _ := h.flagManager.GetFlag("flag_incorrect_pool")
inputStr := string(input)
poolData, err := store.GetPoolData(ctx, h.userdataStore, sessionId, inputStr)
if err != nil {
return res, fmt.Errorf("failed to retrieve pool data: %v", err)
}
if poolData == nil {
flag_api_error, _ := h.flagManager.GetFlag("flag_api_call_error")
// no match found. Call the API using the inputStr as the symbol
poolResp, err := h.accountService.RetrievePoolDetails(ctx, inputStr)
if err != nil {
res.FlagSet = append(res.FlagSet, flag_api_error)
return res, nil
}
if len(poolResp.PoolSymbol) == 0 {
// If the API does not return the data, set the flag
res.FlagSet = append(res.FlagSet, flag_incorrect_pool)
return res, nil
}
poolData = poolResp
}
if err := store.StoreTemporaryPool(ctx, h.userdataStore, sessionId, poolData); err != nil {
logg.ErrorCtxf(ctx, "failed on StoreTemporaryPool", "error", err)
return res, err
}
res.FlagReset = append(res.FlagReset, flag_incorrect_pool)
res.Content = l.Get("Name: %s\nSymbol: %s", poolData.PoolName, poolData.PoolSymbol)
return res, nil
}
// SetPool retrieves the temp pool data and sets it as the active data.
func (h *MenuHandlers) SetPool(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")
}
// Get temporary data
tempData, err := store.GetTemporaryPoolData(ctx, h.userdataStore, sessionId)
if err != nil {
logg.ErrorCtxf(ctx, "failed on GetTemporaryPoolData", "error", err)
return res, err
}
// Set as active and clear temporary data
if err := store.UpdatePoolData(ctx, h.userdataStore, sessionId, tempData); err != nil {
logg.ErrorCtxf(ctx, "failed on UpdatePoolData", "error", err)
return res, err
}
res.Content = tempData.PoolSymbol
return res, nil
}
// CheckTransactions retrieves the transactions from the API using the "PublicKey" and stores to prefixDb.
func (h *MenuHandlers) CheckTransactions(ctx context.Context, sym string, input []byte) (resource.Result, error) {
var res resource.Result
@@ -2707,11 +2795,6 @@ func (h *MenuHandlers) LoadSwapToList(ctx context.Context, sym string, input []b
}
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
}
// get the active address and symbol
activeAddress, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_ADDRESS)
@@ -2737,31 +2820,35 @@ func (h *MenuHandlers) LoadSwapToList(ctx context.Context, sym string, input []b
return res, nil
}
defaultPool := dataserviceapi.PoolDetails{
PoolName: config.DefaultPoolName(),
PoolSymbol: config.DefaultPoolSymbol(),
PoolContractAdrress: config.DefaultPoolAddress(),
LimiterAddress: "",
VoucherRegistry: "",
}
activePoolAddress := defaultPool.PoolContractAdrress
// set the active pool contract address
err = userStore.WriteEntry(ctx, sessionId, storedb.DATA_ACTIVE_POOL_ADDRESS, []byte(activePoolAddress))
// Get active pool address or fall back to default
var activePoolAddress []byte
activePoolAddress, err = userStore.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_POOL_ADDRESS)
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
if db.IsNotFound(err) {
defaultPoolAddress := config.DefaultPoolAddress()
// store the default as the active pool address
err = userStore.WriteEntry(ctx, sessionId, storedb.DATA_ACTIVE_POOL_ADDRESS, []byte(defaultPoolAddress))
if err != nil {
logg.ErrorCtxf(ctx, "failed to write default PoolContractAdrress", "key", storedb.DATA_ACTIVE_POOL_ADDRESS, "value", defaultPoolAddress, "error", err)
return res, err
}
activePoolAddress = []byte(defaultPoolAddress)
} else {
logg.ErrorCtxf(ctx, "failed to read active PoolContractAdrress", "key", storedb.DATA_ACTIVE_POOL_ADDRESS, "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))
r, err := h.accountService.CheckTokenInPool(ctx, string(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
}
logg.InfoCtxf(ctx, "CheckTokenInPool", "response", r, "active_pool_address", activePoolAddress, "address", activeAddress)
if !r.CanSwapFrom {
res.FlagSet = append(res.FlagSet, flag_incorrect_voucher)
res.Content = l.Get(
@@ -2774,8 +2861,8 @@ func (h *MenuHandlers) LoadSwapToList(ctx context.Context, sym string, input []b
res.FlagReset = append(res.FlagReset, flag_incorrect_voucher)
// call the api using the activePoolAddress and publicKey to get a list of SwapToSymbolsData
swapToList, err := h.accountService.GetPoolSwappableVouchers(ctx, string(activePoolAddress), string(publicKey))
// call the api using the activePoolAddress to get a list of SwapToSymbolsData
swapToList, err := h.accountService.GetPoolSwappableVouchers(ctx, string(activePoolAddress))
if err != nil {
res.FlagSet = append(res.FlagSet, flag_api_error)
logg.ErrorCtxf(ctx, "failed on FetchTransactions", "error", err)
@@ -2787,9 +2874,9 @@ func (h *MenuHandlers) LoadSwapToList(ctx context.Context, sym string, input []b
return res, nil
}
data := store.ProcessVouchers(swapToList)
data := store.ProcessTokens(swapToList)
// Store all to list voucher data
// Store all swap_to tokens data
dataMap := map[storedb.DataTyp]string{
storedb.DATA_POOL_TO_SYMBOLS: data.Symbols,
storedb.DATA_POOL_TO_BALANCES: data.Balances,
@@ -2822,8 +2909,7 @@ func (h *MenuHandlers) SwapMaxLimit(ctx context.Context, sym string, input []byt
flag_api_error, _ := h.flagManager.GetFlag("flag_api_error")
flag_low_swap_amount, _ := h.flagManager.GetFlag("flag_low_swap_amount")
res.FlagReset = append(res.FlagReset, flag_incorrect_voucher)
res.FlagReset = append(res.FlagSet, flag_low_swap_amount)
res.FlagReset = append(res.FlagReset, flag_incorrect_voucher, flag_low_swap_amount)
inputStr := string(input)
if inputStr == "0" {
@@ -2944,6 +3030,12 @@ func (h *MenuHandlers) SwapPreview(ctx context.Context, sym string, input []byte
logg.ErrorCtxf(ctx, "failed to write swap amount entry with", "key", storedb.DATA_ACTIVE_SWAP_AMOUNT, "value", finalAmountStr, "error", err)
return res, err
}
// store the user's input amount in the temporary value
err = userStore.WriteEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE, []byte(inputStr))
if err != nil {
logg.ErrorCtxf(ctx, "failed to write swap amount entry with", "key", storedb.DATA_ACTIVE_SWAP_AMOUNT, "value", finalAmountStr, "error", err)
return res, err
}
// call the API to get the quote
r, err := h.accountService.GetPoolSwapQuote(ctx, finalAmountStr, swapData.PublicKey, swapData.ActiveSwapFromAddress, swapData.ActivePoolAddress, swapData.ActiveSwapToAddress)
@@ -3019,7 +3111,7 @@ func (h *MenuHandlers) InitiateSwap(ctx context.Context, sym string, input []byt
res.Content = l.Get(
"Your request has been sent. You will receive an SMS when your %s %s has been swapped for %s.",
swapAmountStr,
swapData.TemporaryValue,
swapData.ActiveSwapFromSym,
swapData.ActiveSwapToSym,
)

View File

@@ -1116,7 +1116,6 @@ func TestAuthorize(t *testing.T) {
flag_incorrect_pin, _ := fm.GetFlag("flag_incorrect_pin")
flag_account_authorized, _ := fm.GetFlag("flag_account_authorized")
flag_allow_update, _ := fm.GetFlag("flag_allow_update")
flag_invalid_pin, _ := fm.GetFlag("flag_invalid_pin")
// Set 1234 is the correct account pin
accountPIN := "1234"
@@ -1134,7 +1133,7 @@ func TestAuthorize(t *testing.T) {
expectedResult resource.Result
}{
{
name: "Test with correct pin",
name: "Test with correct PIN",
input: []byte("1234"),
expectedResult: resource.Result{
FlagReset: []uint32{flag_incorrect_pin},
@@ -1142,18 +1141,18 @@ func TestAuthorize(t *testing.T) {
},
},
{
name: "Test with incorrect pin",
name: "Test with incorrect PIN",
input: []byte("1235"),
expectedResult: resource.Result{
FlagReset: []uint32{flag_account_authorized},
FlagReset: []uint32{flag_account_authorized, flag_allow_update},
FlagSet: []uint32{flag_incorrect_pin},
},
},
{
name: "Test with pin that is not a 4 digit",
name: "Test with PIN that is not a 4 digit",
input: []byte("1235aqds"),
expectedResult: resource.Result{
FlagSet: []uint32{flag_invalid_pin},
FlagReset: []uint32{flag_account_authorized, flag_allow_update},
},
},
}

View File

@@ -91,7 +91,7 @@ func (ls *LocalHandlerService) GetHandler(accountService remote.AccountService)
ls.DbRs.AddLocalFunc("get_recipient", appHandlers.GetRecipient)
ls.DbRs.AddLocalFunc("get_sender", appHandlers.GetSender)
ls.DbRs.AddLocalFunc("get_amount", appHandlers.GetAmount)
ls.DbRs.AddLocalFunc("reset_incorrect", appHandlers.ResetIncorrectPin)
ls.DbRs.AddLocalFunc("reset_incorrect_pin", appHandlers.ResetIncorrectPin)
ls.DbRs.AddLocalFunc("save_firstname", appHandlers.SaveFirstname)
ls.DbRs.AddLocalFunc("save_familyname", appHandlers.SaveFamilyname)
ls.DbRs.AddLocalFunc("save_gender", appHandlers.SaveGender)
@@ -112,6 +112,10 @@ func (ls *LocalHandlerService) GetHandler(accountService remote.AccountService)
ls.DbRs.AddLocalFunc("view_voucher", appHandlers.ViewVoucher)
ls.DbRs.AddLocalFunc("set_voucher", appHandlers.SetVoucher)
ls.DbRs.AddLocalFunc("get_voucher_details", appHandlers.GetVoucherDetails)
ls.DbRs.AddLocalFunc("get_default_pool", appHandlers.GetDefaultPool)
ls.DbRs.AddLocalFunc("get_pools", appHandlers.GetPools)
ls.DbRs.AddLocalFunc("view_pool", appHandlers.ViewPool)
ls.DbRs.AddLocalFunc("set_pool", appHandlers.SetPool)
ls.DbRs.AddLocalFunc("validate_blocked_number", appHandlers.ValidateBlockedNumber)
ls.DbRs.AddLocalFunc("retrieve_blocked_number", appHandlers.RetrieveBlockedNumber)
ls.DbRs.AddLocalFunc("reset_unregistered_number", appHandlers.ResetUnregisteredNumber)
@@ -130,7 +134,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("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)

View File

@@ -1 +1 @@
Failed to connect to the custodial service. Please try again.
Your request failed. Please try again later.

View File

@@ -1 +1 @@
Imeshindwa kuunganisha kwenye huduma ya uangalizi. Tafadhali jaribu tena.
Ombi lako halikufaulu. Tafadhali jaribu tena baadaye.

View File

@@ -1,5 +1,5 @@
LOAD reset_account_authorized 0
LOAD reset_incorrect 0
LOAD reset_incorrect_pin 0
CATCH incorrect_pin flag_incorrect_pin 1
CATCH pin_entry flag_account_authorized 0
MOUT english 1

View File

@@ -1,4 +1,4 @@
LOAD reset_incorrect 6
LOAD reset_incorrect_pin 6
LOAD fetch_community_balance 0
CATCH api_failure flag_api_call_error 1
MAP fetch_community_balance

View File

@@ -1 +1 @@
Incorrect PIN. You have: {{.reset_incorrect}} remaining attempt(s).
Incorrect PIN. You have: {{.reset_incorrect_pin}} remaining attempt(s).

View File

@@ -1,6 +1,6 @@
LOAD reset_incorrect 0
RELOAD reset_incorrect
MAP reset_incorrect
LOAD reset_incorrect_pin 0
RELOAD reset_incorrect_pin
MAP reset_incorrect_pin
CATCH blocked_account flag_account_blocked 1
MOUT retry 1
MOUT quit 9

View File

@@ -1 +1 @@
PIN ulioeka sio sahihi, una majaribio: {{.reset_incorrect}} yaliyobaki
PIN ulioeka sio sahihi, una majaribio: {{.reset_incorrect_pin}} yaliyobaki

View File

@@ -38,4 +38,7 @@ msgid "%s balance: %s\n"
msgstr "%s salio: %s\n"
msgid "%s is not in %s. Please update your voucher and try again."
msgstr "%s haipo kwenye %s. Tafadhali badilisha sarafu yako na ujaribu tena."
msgstr "%s haipo kwenye %s. Tafadhali badilisha sarafu yako na ujaribu tena."
msgid "Name: %s\nSymbol: %s"
msgstr "Jina: %s\nSarafu: %s"

View File

@@ -2,10 +2,9 @@ LOAD clear_temporary_value 2
RELOAD clear_temporary_value
LOAD manage_vouchers 160
RELOAD manage_vouchers
CATCH api_failure flag_api_call_error 1
CATCH api_failure flag_api_call_error 1
LOAD check_balance 128
RELOAD check_balance
CATCH api_failure flag_api_call_error 1
MAP check_balance
MOUT send 1
MOUT swap 2

View File

@@ -1,4 +1,4 @@
LOAD reset_incorrect 6
LOAD reset_incorrect_pin 6
LOAD check_balance 0
CATCH api_failure flag_api_call_error 1
MAP check_balance

View File

@@ -2,8 +2,11 @@ LOAD reset_account_authorized 16
RELOAD reset_account_authorized
MOUT select_voucher 1
MOUT voucher_details 2
MOUT select_pool 3
MOUT back 0
HALT
INCMP _ 0
INCMP select_voucher 1
INCMP voucher_details 2
INCMP select_pool 3
INCMP . *

View File

@@ -1,4 +1,4 @@
RELOAD reset_incorrect
RELOAD reset_incorrect_pin
MOUT back 0
HALT
INCMP _ 0

View File

@@ -2,7 +2,7 @@ LOAD set_back 6
LOAD authorize_account 16
LOAD reset_allow_update 4
LOAD save_temporary_pin 1
LOAD reset_incorrect 0
LOAD reset_incorrect_pin 0
LOAD reset_invalid_pin 6
MOUT change_pin 1
MOUT reset_pin 2

View File

@@ -0,0 +1 @@
Success! {{.set_pool}} is now your active pool.

View File

@@ -0,0 +1,10 @@
LOAD reset_incorrect_pin 6
CATCH _ flag_account_authorized 0
LOAD set_pool 20
MAP set_pool
MOUT back 0
MOUT quit 9
HALT
INCMP ^ 0
INCMP quit 9
INCMP ^ *

View File

@@ -0,0 +1 @@
Hongera! {{.set_pool}} ni bwawa la Sarafu linalotumika sasa.

View File

@@ -0,0 +1,3 @@
Enter number or symbol to set the default pool:
Current: {{.get_default_pool}}
{{.get_pools}}

View File

@@ -0,0 +1,20 @@
CATCH no_voucher flag_no_active_voucher 1
LOAD get_pools 0
MAP get_pools
LOAD get_default_pool 20
RELOAD get_default_pool
MAP get_default_pool
MOUT back 0
MOUT quit 99
MNEXT next 88
MPREV prev 98
HALT
INCMP > 88
INCMP < 98
INCMP _ 0
INCMP quit 99
LOAD view_pool 80
RELOAD view_pool
CATCH api_failure flag_api_call_error 1
CATCH . flag_incorrect_pool 1
INCMP view_pool *

View File

@@ -0,0 +1 @@
Select pool

View File

@@ -0,0 +1 @@
Chagua Bwawa

View File

@@ -0,0 +1,3 @@
Chagua nambari au ishara kuweka bwawa la sarafu:
La sasa: {{.get_default_pool}}
{{.get_pools}}

View File

@@ -1,3 +1,4 @@
LOAD reset_incorrect 6
LOAD reset_incorrect_pin 6
CATCH _ flag_account_authorized 0
LOAD initiate_swap 0
HALT

View File

@@ -1,4 +1,3 @@
RELOAD swap_max_limit
MAP swap_max_limit
MOUT back 0
HALT

View File

@@ -1,12 +1,12 @@
LOAD swap_preview 0
MAP swap_preview
CATCH api_failure flag_api_call_error 1
MOUT back 0
MOUT quit 9
LOAD authorize_account 6
HALT
RELOAD authorize_account
CATCH incorrect_pin flag_incorrect_pin 1
CATCH . flag_account_authorized 0
INCMP _ 0
INCMP quit 9
INCMP swap_initiated *

View File

@@ -1,3 +1,4 @@
CATCH no_voucher flag_no_active_voucher 1
LOAD swap_to_list 0
RELOAD swap_to_list
MAP swap_to_list
@@ -6,6 +7,7 @@ MOUT back 0
HALT
LOAD swap_max_limit 64
RELOAD swap_max_limit
CATCH api_failure flag_api_call_error 1
CATCH . flag_incorrect_voucher 1
CATCH low_swap_amount flag_low_swap_amount 1
INCMP _ 0

View File

@@ -1,5 +1,4 @@
LOAD reset_incorrect 6
CATCH incorrect_pin flag_incorrect_pin 1
LOAD reset_incorrect_pin 6
CATCH _ flag_account_authorized 0
RELOAD get_amount
MAP get_amount

View File

@@ -0,0 +1,2 @@
Enter PIN to confirm selection:
{{.view_pool}}

View File

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

View File

@@ -0,0 +1,2 @@
Weka PIN ili kuthibitisha chaguo:
{{.view_pool}}

View File

@@ -1,6 +1,6 @@
LOAD get_profile_info 0
MAP get_profile_info
LOAD reset_incorrect 6
LOAD reset_incorrect_pin 6
CATCH incorrect_pin flag_incorrect_pin 1
CATCH pin_entry flag_account_authorized 0
MOUT back 0

View File

@@ -1,5 +1,4 @@
LOAD reset_incorrect 6
CATCH incorrect_pin flag_incorrect_pin 1
LOAD reset_incorrect_pin 6
CATCH _ flag_account_authorized 0
LOAD set_voucher 12
MAP set_voucher

View File

@@ -85,6 +85,10 @@ const (
DATA_ACTIVE_SWAP_MAX_AMOUNT
// Holds the active swap amount for the swap
DATA_ACTIVE_SWAP_AMOUNT
// Holds the active pool name for the swap
DATA_ACTIVE_POOL_NAME
// Holds the active pool symbol for the swap
DATA_ACTIVE_POOL_SYM
)
const (

View File

@@ -91,3 +91,52 @@ func MatchPool(input, names, symbols, addresses string) (name, symbol, address s
}
return
}
// StoreTemporaryPool saves pool metadata as temporary entries in the DataStore.
func StoreTemporaryPool(ctx context.Context, store DataStore, sessionId string, data *dataserviceapi.PoolDetails) error {
tempData := fmt.Sprintf("%s,%s,%s", data.PoolName, data.PoolSymbol, data.PoolContractAdrress)
if err := store.WriteEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE, []byte(tempData)); err != nil {
return err
}
return nil
}
// GetTemporaryPoolData retrieves temporary pool metadata from the DataStore.
func GetTemporaryPoolData(ctx context.Context, store DataStore, sessionId string) (*dataserviceapi.PoolDetails, error) {
temp_data, err := store.ReadEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE)
if err != nil {
return nil, err
}
values := strings.SplitN(string(temp_data), ",", 3)
data := &dataserviceapi.PoolDetails{}
data.PoolName = values[0]
data.PoolSymbol = values[1]
data.PoolContractAdrress = values[2]
return data, nil
}
// UpdatePoolData updates the active pool data in the DataStore.
func UpdatePoolData(ctx context.Context, store DataStore, sessionId string, data *dataserviceapi.PoolDetails) error {
logg.TraceCtxf(ctx, "dtal", "data", data)
// Active pool data entry
activeEntries := map[storedb.DataTyp][]byte{
storedb.DATA_ACTIVE_POOL_NAME: []byte(data.PoolName),
storedb.DATA_ACTIVE_POOL_SYM: []byte(data.PoolSymbol),
storedb.DATA_ACTIVE_POOL_ADDRESS: []byte(data.PoolContractAdrress),
}
// Write active data
for key, value := range activeEntries {
if err := store.WriteEntry(ctx, sessionId, key, value); err != nil {
return err
}
}
return nil
}

View File

@@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"reflect"
"strconv"
storedb "git.grassecon.net/grassrootseconomics/sarafu-vise/store/db"
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
@@ -21,6 +22,7 @@ type SwapData struct {
}
type SwapPreviewData struct {
TemporaryValue string
PublicKey string
ActiveSwapMaxAmount string
ActiveSwapFromDecimal string
@@ -64,6 +66,7 @@ func ReadSwapData(ctx context.Context, store DataStore, sessionId string) (SwapD
func ReadSwapPreviewData(ctx context.Context, store DataStore, sessionId string) (SwapPreviewData, error) {
data := SwapPreviewData{}
fieldToKey := map[string]storedb.DataTyp{
"TemporaryValue": storedb.DATA_TEMPORARY_VALUE,
"PublicKey": storedb.DATA_PUBLIC_KEY,
"ActiveSwapMaxAmount": storedb.DATA_ACTIVE_SWAP_MAX_AMOUNT,
"ActiveSwapFromDecimal": storedb.DATA_ACTIVE_DECIMAL,
@@ -129,8 +132,8 @@ func GetSwapFromVoucherData(ctx context.Context, store DataStore, sessionId stri
}, nil
}
// GetSwapToVoucherData retrieves and matches voucher data
func GetSwapToVoucherData(ctx context.Context, store DataStore, sessionId string, input string) (*dataserviceapi.TokenHoldings, error) {
// GetSwapToVoucherData retrieves and matches token data
func GetSwapToVoucherData(ctx context.Context, store DataStore, sessionId string, input string) (*dataserviceapi.TokenDetails, error) {
keys := []storedb.DataTyp{
storedb.DATA_POOL_TO_SYMBOLS,
storedb.DATA_POOL_TO_BALANCES,
@@ -147,7 +150,7 @@ func GetSwapToVoucherData(ctx context.Context, store DataStore, sessionId string
data[key] = string(value)
}
symbol, balance, decimal, address := MatchVoucher(input,
symbol, _, decimal, address := MatchVoucher(input,
data[storedb.DATA_POOL_TO_SYMBOLS],
data[storedb.DATA_POOL_TO_BALANCES],
data[storedb.DATA_POOL_TO_DECIMALS],
@@ -158,22 +161,31 @@ func GetSwapToVoucherData(ctx context.Context, store DataStore, sessionId string
return nil, nil
}
return &dataserviceapi.TokenHoldings{
TokenSymbol: string(symbol),
Balance: string(balance),
TokenDecimals: string(decimal),
ContractAddress: string(address),
decimalInt, err := strconv.ParseUint(decimal, 0, 64)
if err != nil {
logg.ErrorCtxf(ctx, "Failed to parse decimal to Uint:", "sessionId", sessionId, "decimal", decimal, "error", err)
return nil, nil
}
return &dataserviceapi.TokenDetails{
TokenSymbol: string(symbol),
TokenDecimals: uint8(decimalInt),
TokenAddress: string(address),
}, nil
}
// UpdateSwapToVoucherData updates the active swap to voucher data in the DataStore.
func UpdateSwapToVoucherData(ctx context.Context, store DataStore, sessionId string, data *dataserviceapi.TokenHoldings) error {
logg.TraceCtxf(ctx, "dtal", "data", data)
func UpdateSwapToVoucherData(ctx context.Context, store DataStore, sessionId string, data *dataserviceapi.TokenDetails) error {
logg.TraceCtxf(ctx, "UpdateSwapToVoucherData", "data", data)
// Convert TokenDecimals (uint8) to string
tokenDecimalsStr := strconv.FormatUint(uint64(data.TokenDecimals), 10)
// Active swap to voucher data entries
activeEntries := map[storedb.DataTyp][]byte{
storedb.DATA_ACTIVE_SWAP_TO_SYM: []byte(data.TokenSymbol),
storedb.DATA_ACTIVE_SWAP_TO_DECIMAL: []byte(data.TokenDecimals),
storedb.DATA_ACTIVE_SWAP_TO_ADDRESS: []byte(data.ContractAddress),
storedb.DATA_ACTIVE_SWAP_TO_DECIMAL: []byte(tokenDecimalsStr),
storedb.DATA_ACTIVE_SWAP_TO_ADDRESS: []byte(data.TokenAddress),
}
// Write active data

View File

@@ -47,6 +47,24 @@ func ProcessVouchers(holdings []dataserviceapi.TokenHoldings) VoucherMetadata {
return data
}
// ProcessTokens converts swappable tokens into formatted strings
func ProcessTokens(holdings []dataserviceapi.TokenDetails) VoucherMetadata {
var data VoucherMetadata
var symbols, decimals, addresses []string
for i, h := range holdings {
symbols = append(symbols, fmt.Sprintf("%d:%s", i+1, h.TokenSymbol))
decimals = append(decimals, fmt.Sprintf("%d:%d", i+1, h.TokenDecimals))
addresses = append(addresses, fmt.Sprintf("%d:%s", i+1, h.TokenAddress))
}
data.Symbols = strings.Join(symbols, "\n")
data.Decimals = strings.Join(decimals, "\n")
data.Addresses = strings.Join(addresses, "\n")
return data
}
func ScaleDownBalance(balance, decimals string) string {
// Convert balance and decimals to big.Float
bal := new(big.Float)