Compare commits
35 Commits
project-ti
...
v1.4.2-rc
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7973ecdfd9 | ||
|
|
c90d3cd731
|
||
|
|
cda2d49f3e
|
||
|
|
4492f8087a
|
||
|
|
0f8c2f9270
|
||
|
|
5a09d33be0
|
||
|
|
0c67efedea
|
||
|
|
14d493475e
|
||
|
|
0e4dfe1baf
|
||
|
|
7e1042c6a9
|
||
|
|
e274967c8e
|
||
|
2cd875eb4d
|
|||
|
|
5b82afa768
|
||
|
|
b6de057cc4
|
||
|
|
758463ee8c
|
||
|
|
f441b3b2af
|
||
|
|
4d687cac2e
|
||
| 3d57150465 | |||
|
|
bd8631eb05
|
||
|
|
c51fd89ad5
|
||
| bcccd79e70 | |||
|
|
193c9c119a
|
||
|
|
36a3c59287
|
||
|
|
1234723e10
|
||
|
|
61f48abb7d
|
||
|
|
3e40a09d39
|
||
|
|
7e90124199
|
||
|
|
e27905765c
|
||
|
|
4ce9baa379
|
||
|
|
e47415cc22
|
||
|
|
1f4d810e14
|
||
| 35d9d13442 | |||
|
|
72d038c076
|
||
|
|
058d802c0f
|
||
|
|
da64c3b80e |
@@ -21,7 +21,7 @@ RUN make VISE_PATH=/build/go-vise -B
|
||||
WORKDIR /build/sarafu-vise
|
||||
RUN echo "Building on $BUILDPLATFORM, building for $TARGETPLATFORM"
|
||||
RUN go mod download
|
||||
RUN go build -tags logdebug,online -o sarafu-at -ldflags="-X main.build=${BUILD} -s -w" cmd/africastalking/main.go
|
||||
RUN go build -tags logtrace,online -o sarafu-at -ldflags="-X main.build=${BUILD} -s -w" cmd/africastalking/main.go
|
||||
|
||||
FROM debian:bookworm-slim
|
||||
|
||||
|
||||
@@ -83,6 +83,7 @@ func (h *MenuHandlers) CheckAccountCreated(ctx context.Context, sym string, inpu
|
||||
// CheckBlockedStatus:
|
||||
// 1. Checks whether the DATA_SELF_PIN_RESET is 1 and sets the flag_account_pin_reset
|
||||
// 2. resets the account blocked flag if the PIN attempts have been reset by an admin.
|
||||
// 3. Sets key flags (language and PIN) if the data exists
|
||||
func (h *MenuHandlers) CheckBlockedStatus(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
var res resource.Result
|
||||
store := h.userdataStore
|
||||
@@ -90,11 +91,30 @@ func (h *MenuHandlers) CheckBlockedStatus(ctx context.Context, sym string, input
|
||||
flag_account_blocked, _ := h.flagManager.GetFlag("flag_account_blocked")
|
||||
flag_account_pin_reset, _ := h.flagManager.GetFlag("flag_account_pin_reset")
|
||||
|
||||
flag_pin_set, _ := h.flagManager.GetFlag("flag_pin_set")
|
||||
flag_language_set, _ := h.flagManager.GetFlag("flag_language_set")
|
||||
pinFlagSet := h.st.MatchFlag(flag_pin_set, true)
|
||||
languageFlagSet := h.st.MatchFlag(flag_language_set, true)
|
||||
|
||||
sessionId, ok := ctx.Value("SessionId").(string)
|
||||
if !ok {
|
||||
return res, fmt.Errorf("missing session")
|
||||
}
|
||||
|
||||
// only check the data if the flag isn't set
|
||||
if !pinFlagSet {
|
||||
accountPin, _ := store.ReadEntry(ctx, sessionId, storedb.DATA_ACCOUNT_PIN)
|
||||
if len(accountPin) > 0 {
|
||||
res.FlagSet = append(res.FlagSet, flag_pin_set)
|
||||
}
|
||||
}
|
||||
if !languageFlagSet {
|
||||
languageCode, _ := store.ReadEntry(ctx, sessionId, storedb.DATA_SELECTED_LANGUAGE_CODE)
|
||||
if len(languageCode) > 0 {
|
||||
res.FlagSet = append(res.FlagSet, flag_language_set)
|
||||
}
|
||||
}
|
||||
|
||||
res.FlagReset = append(res.FlagReset, flag_account_pin_reset)
|
||||
|
||||
selfPinReset, err := store.ReadEntry(ctx, sessionId, storedb.DATA_SELF_PIN_RESET)
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"git.defalsify.org/vise.git/resource"
|
||||
"git.defalsify.org/vise.git/state"
|
||||
"git.grassecon.net/grassrootseconomics/sarafu-api/models"
|
||||
"git.grassecon.net/grassrootseconomics/sarafu-api/testutil/mocks"
|
||||
storedb "git.grassecon.net/grassrootseconomics/sarafu-vise/store/db"
|
||||
@@ -93,16 +94,24 @@ func TestCheckBlockedStatus(t *testing.T) {
|
||||
}
|
||||
flag_account_blocked, _ := fm.GetFlag("flag_account_blocked")
|
||||
flag_account_pin_reset, _ := fm.GetFlag("flag_account_pin_reset")
|
||||
flag_pin_set, _ := fm.GetFlag("flag_pin_set")
|
||||
flag_language_set, _ := fm.GetFlag("flag_language_set")
|
||||
|
||||
// Set the flag in the State
|
||||
mockState := state.NewState(128)
|
||||
|
||||
h := &MenuHandlers{
|
||||
userdataStore: store,
|
||||
flagManager: fm,
|
||||
st: mockState,
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
currentWrongPinAttempts string
|
||||
expectedResult resource.Result
|
||||
languageSet bool
|
||||
PinSet bool
|
||||
}{
|
||||
{
|
||||
name: "Currently blocked account",
|
||||
@@ -118,6 +127,16 @@ func TestCheckBlockedStatus(t *testing.T) {
|
||||
FlagReset: []uint32{flag_account_pin_reset, flag_account_blocked},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Valid account with reset language and PIN flags",
|
||||
currentWrongPinAttempts: "0",
|
||||
languageSet: true,
|
||||
PinSet: true,
|
||||
expectedResult: resource.Result{
|
||||
FlagReset: []uint32{flag_account_pin_reset, flag_account_blocked},
|
||||
FlagSet: []uint32{flag_pin_set, flag_language_set},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
@@ -126,6 +145,18 @@ func TestCheckBlockedStatus(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if tt.languageSet {
|
||||
if err := store.WriteEntry(ctx, sessionId, storedb.DATA_SELECTED_LANGUAGE_CODE, []byte("eng")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
if tt.PinSet {
|
||||
if err := store.WriteEntry(ctx, sessionId, storedb.DATA_ACCOUNT_PIN, []byte("hasedPinValue")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
res, err := h.CheckBlockedStatus(ctx, "", []byte(""))
|
||||
|
||||
assert.NoError(t, err)
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"git.grassecon.net/grassrootseconomics/sarafu-vise/config"
|
||||
"git.grassecon.net/grassrootseconomics/sarafu-vise/store"
|
||||
storedb "git.grassecon.net/grassrootseconomics/sarafu-vise/store/db"
|
||||
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
||||
"gopkg.in/leonelquinteros/gotext.v1"
|
||||
)
|
||||
|
||||
@@ -36,9 +37,26 @@ func (h *MenuHandlers) GetPools(ctx context.Context, sym string, input []byte) (
|
||||
return res, nil
|
||||
}
|
||||
|
||||
data := store.ProcessPools(topPools)
|
||||
activePoolSymStr := ""
|
||||
|
||||
// Store all Pool data
|
||||
activePoolSym, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_POOL_SYM)
|
||||
if err != nil {
|
||||
activePoolSymStr = config.DefaultPoolSymbol()
|
||||
} else {
|
||||
activePoolSymStr = string(activePoolSym)
|
||||
}
|
||||
|
||||
// Filter out the active pool from topPools
|
||||
filteredPools := make([]dataserviceapi.PoolDetails, 0, len(topPools))
|
||||
for _, p := range topPools {
|
||||
if p.PoolSymbol != activePoolSymStr {
|
||||
filteredPools = append(filteredPools, p)
|
||||
}
|
||||
}
|
||||
|
||||
data := store.ProcessPools(filteredPools)
|
||||
|
||||
// Store the filtered Pool data
|
||||
dataMap := map[storedb.DataTyp]string{
|
||||
storedb.DATA_POOL_NAMES: data.PoolNames,
|
||||
storedb.DATA_POOL_SYMBOLS: data.PoolSymbols,
|
||||
@@ -100,6 +118,10 @@ func (h *MenuHandlers) ViewPool(ctx context.Context, sym string, input []byte) (
|
||||
flag_incorrect_pool, _ := h.flagManager.GetFlag("flag_incorrect_pool")
|
||||
|
||||
inputStr := string(input)
|
||||
if inputStr == "0" || inputStr == "99" || inputStr == "88" || inputStr == "98" {
|
||||
res.FlagReset = append(res.FlagReset, flag_incorrect_pool)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
poolData, err := store.GetPoolData(ctx, h.userdataStore, sessionId, inputStr)
|
||||
if err != nil {
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"git.grassecon.net/grassrootseconomics/sarafu-vise/config"
|
||||
"git.grassecon.net/grassrootseconomics/sarafu-vise/store"
|
||||
storedb "git.grassecon.net/grassrootseconomics/sarafu-vise/store/db"
|
||||
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
||||
"gopkg.in/leonelquinteros/gotext.v1"
|
||||
)
|
||||
|
||||
@@ -121,7 +122,16 @@ func (h *MenuHandlers) LoadSwapToList(ctx context.Context, sym string, input []b
|
||||
return res, nil
|
||||
}
|
||||
|
||||
data := store.ProcessVouchers(swapToList)
|
||||
// Filter out the active voucher from swapToList
|
||||
filteredSwapToList := make([]dataserviceapi.TokenHoldings, 0, len(swapToList))
|
||||
for _, s := range swapToList {
|
||||
if s.TokenSymbol != string(activeSym) {
|
||||
filteredSwapToList = append(filteredSwapToList, s)
|
||||
}
|
||||
}
|
||||
|
||||
// Store filtered swap to list data (excluding the current active voucher)
|
||||
data := store.ProcessVouchers(filteredSwapToList)
|
||||
|
||||
logg.InfoCtxf(ctx, "ProcessVouchers", "data", data)
|
||||
|
||||
@@ -161,7 +171,7 @@ func (h *MenuHandlers) SwapMaxLimit(ctx context.Context, sym string, input []byt
|
||||
res.FlagReset = append(res.FlagReset, flag_incorrect_voucher, flag_low_swap_amount)
|
||||
|
||||
inputStr := string(input)
|
||||
if inputStr == "0" {
|
||||
if inputStr == "0" || inputStr == "99" || inputStr == "88" || inputStr == "98" {
|
||||
return res, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -10,132 +10,183 @@ import (
|
||||
"git.defalsify.org/vise.git/resource"
|
||||
"git.grassecon.net/grassrootseconomics/common/identity"
|
||||
"git.grassecon.net/grassrootseconomics/common/phone"
|
||||
"git.grassecon.net/grassrootseconomics/sarafu-api/models"
|
||||
"git.grassecon.net/grassrootseconomics/sarafu-vise/config"
|
||||
"git.grassecon.net/grassrootseconomics/sarafu-vise/store"
|
||||
storedb "git.grassecon.net/grassrootseconomics/sarafu-vise/store/db"
|
||||
"github.com/grassrootseconomics/ethutils"
|
||||
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
||||
"gopkg.in/leonelquinteros/gotext.v1"
|
||||
)
|
||||
|
||||
// ValidateRecipient validates that the given input is valid.
|
||||
//
|
||||
// TODO: split up functino
|
||||
func (h *MenuHandlers) ValidateRecipient(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
var res resource.Result
|
||||
var AliasAddressResult string
|
||||
var AliasAddress *models.AliasAddress
|
||||
store := h.userdataStore
|
||||
flag_invalid_recipient, _ := h.flagManager.GetFlag("flag_invalid_recipient")
|
||||
|
||||
sessionId, ok := ctx.Value("SessionId").(string)
|
||||
if !ok {
|
||||
return res, fmt.Errorf("missing session")
|
||||
}
|
||||
flag_invalid_recipient, _ := h.flagManager.GetFlag("flag_invalid_recipient")
|
||||
flag_invalid_recipient_with_invite, _ := h.flagManager.GetFlag("flag_invalid_recipient_with_invite")
|
||||
flag_api_error, _ := h.flagManager.GetFlag("flag_api_call_error")
|
||||
|
||||
// remove white spaces
|
||||
recipient := strings.ReplaceAll(string(input), " ", "")
|
||||
if recipient == "0" {
|
||||
return res, nil
|
||||
}
|
||||
|
||||
if recipient != "0" {
|
||||
recipientType, err := identity.CheckRecipient(recipient)
|
||||
if err != nil {
|
||||
// Invalid recipient format (not a phone number, address, or valid alias format)
|
||||
res.FlagSet = append(res.FlagSet, flag_invalid_recipient)
|
||||
recipientType, err := identity.CheckRecipient(recipient)
|
||||
if err != nil {
|
||||
// Invalid recipient format (not a phone number, address, or valid alias format)
|
||||
res.FlagSet = append(res.FlagSet, flag_invalid_recipient)
|
||||
res.Content = recipient
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// save the recipient as the temporaryRecipient
|
||||
err = store.WriteEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE, []byte(recipient))
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "failed to write temporaryRecipient entry with", "key", storedb.DATA_TEMPORARY_VALUE, "value", recipient, "error", err)
|
||||
return res, err
|
||||
}
|
||||
|
||||
switch recipientType {
|
||||
case "phone number":
|
||||
return h.handlePhoneNumber(ctx, sessionId, recipient, &res)
|
||||
case "address":
|
||||
return h.handleAddress(ctx, sessionId, recipient, &res)
|
||||
case "alias":
|
||||
return h.handleAlias(ctx, sessionId, recipient, &res)
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (h *MenuHandlers) handlePhoneNumber(ctx context.Context, sessionId, recipient string, res *resource.Result) (resource.Result, error) {
|
||||
store := h.userdataStore
|
||||
flag_invalid_recipient_with_invite, _ := h.flagManager.GetFlag("flag_invalid_recipient_with_invite")
|
||||
|
||||
formattedNumber, err := phone.FormatPhoneNumber(recipient)
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "Failed to format phone number", "recipient", recipient, "error", err)
|
||||
return *res, err
|
||||
}
|
||||
|
||||
publicKey, err := store.ReadEntry(ctx, formattedNumber, storedb.DATA_PUBLIC_KEY)
|
||||
if err != nil {
|
||||
if db.IsNotFound(err) {
|
||||
logg.InfoCtxf(ctx, "Unregistered phone number", "recipient", recipient)
|
||||
res.FlagSet = append(res.FlagSet, flag_invalid_recipient_with_invite)
|
||||
res.Content = recipient
|
||||
|
||||
return res, nil
|
||||
return *res, nil
|
||||
}
|
||||
logg.ErrorCtxf(ctx, "Failed to read publicKey", "error", err)
|
||||
return *res, err
|
||||
}
|
||||
|
||||
// save the recipient as the temporaryRecipient
|
||||
err = store.WriteEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE, []byte(recipient))
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "failed to write temporaryRecipient entry with", "key", storedb.DATA_TEMPORARY_VALUE, "value", recipient, "error", err)
|
||||
return res, err
|
||||
if err := store.WriteEntry(ctx, sessionId, storedb.DATA_RECIPIENT, publicKey); err != nil {
|
||||
logg.ErrorCtxf(ctx, "Failed to write recipient", "value", string(publicKey), "error", err)
|
||||
return *res, err
|
||||
}
|
||||
|
||||
senderActiveAddress, err := store.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_ADDRESS)
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "Failed to read sender senderActiveAddress", "error", err)
|
||||
return *res, err
|
||||
}
|
||||
|
||||
recipientActiveAddress, _ := store.ReadEntry(ctx, formattedNumber, storedb.DATA_ACTIVE_ADDRESS)
|
||||
|
||||
txType := "swap"
|
||||
|
||||
// recipient has no active token → normal transaction
|
||||
if recipientActiveAddress == nil {
|
||||
txType = "normal"
|
||||
} else if senderActiveAddress != nil && string(senderActiveAddress) == string(recipientActiveAddress) {
|
||||
// recipient has active token same as sender → normal transaction
|
||||
txType = "normal"
|
||||
}
|
||||
|
||||
// save transaction type
|
||||
if err := store.WriteEntry(ctx, sessionId, storedb.DATA_SEND_TRANSACTION_TYPE, []byte(txType)); err != nil {
|
||||
logg.ErrorCtxf(ctx, "Failed to write tx type", "type", txType, "error", err)
|
||||
return *res, err
|
||||
}
|
||||
|
||||
// save the recipient's phone number
|
||||
if err := store.WriteEntry(ctx, sessionId, storedb.DATA_RECIPIENT_PHONE_NUMBER, []byte(formattedNumber)); err != nil {
|
||||
logg.ErrorCtxf(ctx, "Failed to write recipient's phone number", "type", txType, "error", err)
|
||||
return *res, err
|
||||
}
|
||||
|
||||
return *res, nil
|
||||
}
|
||||
|
||||
func (h *MenuHandlers) handleAddress(ctx context.Context, sessionId, recipient string, res *resource.Result) (resource.Result, error) {
|
||||
store := h.userdataStore
|
||||
|
||||
address := ethutils.ChecksumAddress(recipient)
|
||||
if err := store.WriteEntry(ctx, sessionId, storedb.DATA_RECIPIENT, []byte(address)); err != nil {
|
||||
logg.ErrorCtxf(ctx, "Failed to write recipient address", "error", err)
|
||||
return *res, err
|
||||
}
|
||||
if err := store.WriteEntry(ctx, sessionId, storedb.DATA_SEND_TRANSACTION_TYPE, []byte("normal")); err != nil {
|
||||
logg.ErrorCtxf(ctx, "Failed to write tx type for address", "error", err)
|
||||
return *res, err
|
||||
}
|
||||
|
||||
return *res, nil
|
||||
}
|
||||
|
||||
func (h *MenuHandlers) handleAlias(ctx context.Context, sessionId, recipient string, res *resource.Result) (resource.Result, error) {
|
||||
store := h.userdataStore
|
||||
flag_invalid_recipient, _ := h.flagManager.GetFlag("flag_invalid_recipient")
|
||||
flag_api_error, _ := h.flagManager.GetFlag("flag_api_call_error")
|
||||
|
||||
var AliasAddressResult string
|
||||
|
||||
if strings.Contains(recipient, ".") {
|
||||
alias, err := h.accountService.CheckAliasAddress(ctx, recipient)
|
||||
if err == nil {
|
||||
AliasAddressResult = alias.Address
|
||||
} else {
|
||||
logg.ErrorCtxf(ctx, "Failed to resolve alias", "alias", recipient, "error", err)
|
||||
}
|
||||
} else {
|
||||
for _, domain := range config.SearchDomains() {
|
||||
fqdn := fmt.Sprintf("%s.%s", recipient, domain)
|
||||
logg.InfoCtxf(ctx, "Trying alias", "fqdn", fqdn)
|
||||
|
||||
switch recipientType {
|
||||
case "phone number":
|
||||
// format the phone number
|
||||
formattedNumber, err := phone.FormatPhoneNumber(recipient)
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "Failed to format the phone number: %s", recipient, "error", err)
|
||||
return res, err
|
||||
}
|
||||
|
||||
// Check if the phone number is registered
|
||||
publicKey, err := store.ReadEntry(ctx, formattedNumber, storedb.DATA_PUBLIC_KEY)
|
||||
if err != nil {
|
||||
if db.IsNotFound(err) {
|
||||
logg.InfoCtxf(ctx, "Unregistered phone number: %s", recipient)
|
||||
res.FlagSet = append(res.FlagSet, flag_invalid_recipient_with_invite)
|
||||
res.Content = recipient
|
||||
return res, nil
|
||||
}
|
||||
|
||||
logg.ErrorCtxf(ctx, "failed to read publicKey entry with", "key", storedb.DATA_PUBLIC_KEY, "error", err)
|
||||
return res, err
|
||||
}
|
||||
|
||||
// Save the publicKey as the recipient
|
||||
err = store.WriteEntry(ctx, sessionId, storedb.DATA_RECIPIENT, publicKey)
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "failed to write recipient entry with", "key", storedb.DATA_RECIPIENT, "value", string(publicKey), "error", err)
|
||||
return res, err
|
||||
}
|
||||
|
||||
case "address":
|
||||
// checksum the address
|
||||
address := ethutils.ChecksumAddress(recipient)
|
||||
|
||||
// Save the valid Ethereum address as the recipient
|
||||
err = store.WriteEntry(ctx, sessionId, storedb.DATA_RECIPIENT, []byte(address))
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "failed to write recipient entry with", "key", storedb.DATA_RECIPIENT, "value", recipient, "error", err)
|
||||
return res, err
|
||||
}
|
||||
|
||||
case "alias":
|
||||
if strings.Contains(recipient, ".") {
|
||||
AliasAddress, err = h.accountService.CheckAliasAddress(ctx, recipient)
|
||||
if err == nil {
|
||||
AliasAddressResult = AliasAddress.Address
|
||||
} else {
|
||||
logg.ErrorCtxf(ctx, "failed to resolve alias", "alias", recipient, "error_alias_check", err)
|
||||
}
|
||||
alias, err := h.accountService.CheckAliasAddress(ctx, fqdn)
|
||||
if err == nil {
|
||||
res.FlagReset = append(res.FlagReset, flag_api_error)
|
||||
AliasAddressResult = alias.Address
|
||||
break
|
||||
} else {
|
||||
//Perform a search for each search domain,break on first match
|
||||
for _, domain := range config.SearchDomains() {
|
||||
fqdn := fmt.Sprintf("%s.%s", recipient, domain)
|
||||
logg.InfoCtxf(ctx, "Resolving with fqdn alias", "alias", fqdn)
|
||||
AliasAddress, err = h.accountService.CheckAliasAddress(ctx, fqdn)
|
||||
if err == nil {
|
||||
res.FlagReset = append(res.FlagReset, flag_api_error)
|
||||
AliasAddressResult = AliasAddress.Address
|
||||
continue
|
||||
} else {
|
||||
res.FlagSet = append(res.FlagSet, flag_api_error)
|
||||
logg.ErrorCtxf(ctx, "failed to resolve alias", "alias", recipient, "error_alias_check", err)
|
||||
return res, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
if AliasAddressResult == "" {
|
||||
res.Content = recipient
|
||||
res.FlagSet = append(res.FlagSet, flag_invalid_recipient)
|
||||
return res, nil
|
||||
} else {
|
||||
err = store.WriteEntry(ctx, sessionId, storedb.DATA_RECIPIENT, []byte(AliasAddressResult))
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "failed to write recipient entry with", "key", storedb.DATA_RECIPIENT, "value", AliasAddressResult, "error", err)
|
||||
return res, err
|
||||
}
|
||||
res.FlagSet = append(res.FlagSet, flag_api_error)
|
||||
logg.ErrorCtxf(ctx, "Alias resolution failed", "alias", fqdn, "error", err)
|
||||
return *res, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return res, nil
|
||||
if AliasAddressResult == "" {
|
||||
res.FlagSet = append(res.FlagSet, flag_invalid_recipient)
|
||||
res.Content = recipient
|
||||
return *res, nil
|
||||
}
|
||||
|
||||
if err := store.WriteEntry(ctx, sessionId, storedb.DATA_RECIPIENT, []byte(AliasAddressResult)); err != nil {
|
||||
logg.ErrorCtxf(ctx, "Failed to store alias recipient", "error", err)
|
||||
return *res, err
|
||||
}
|
||||
if err := store.WriteEntry(ctx, sessionId, storedb.DATA_SEND_TRANSACTION_TYPE, []byte("normal")); err != nil {
|
||||
logg.ErrorCtxf(ctx, "Failed to write tx type for alias", "error", err)
|
||||
return *res, err
|
||||
}
|
||||
|
||||
return *res, nil
|
||||
}
|
||||
|
||||
// TransactionReset resets the previous transaction data (Recipient and Amount)
|
||||
@@ -162,6 +213,16 @@ func (h *MenuHandlers) TransactionReset(ctx context.Context, sym string, input [
|
||||
return res, nil
|
||||
}
|
||||
|
||||
err = store.WriteEntry(ctx, sessionId, storedb.DATA_SEND_TRANSACTION_TYPE, []byte(""))
|
||||
if err != nil {
|
||||
return res, nil
|
||||
}
|
||||
|
||||
err = store.WriteEntry(ctx, sessionId, storedb.DATA_RECIPIENT_PHONE_NUMBER, []byte(""))
|
||||
if err != nil {
|
||||
return res, nil
|
||||
}
|
||||
|
||||
res.FlagReset = append(res.FlagReset, flag_invalid_recipient, flag_invalid_recipient_with_invite)
|
||||
|
||||
return res, nil
|
||||
@@ -189,29 +250,192 @@ func (h *MenuHandlers) ResetTransactionAmount(ctx context.Context, sym string, i
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// MaxAmount gets the current balance from the API and sets it as
|
||||
// MaxAmount checks the transaction type to determine the displayed max amount.
|
||||
// If the transaction type is "swap", it checks the max swappable amount and sets this as the content.
|
||||
// If the transaction type is "normal", gets the current sender's balance from the store and sets it as
|
||||
// the result content.
|
||||
func (h *MenuHandlers) MaxAmount(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")
|
||||
}
|
||||
store := h.userdataStore
|
||||
|
||||
activeBal, err := store.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_BAL)
|
||||
flag_api_error, _ := h.flagManager.GetFlag("flag_api_error")
|
||||
flag_swap_transaction, _ := h.flagManager.GetFlag("flag_swap_transaction")
|
||||
userStore := h.userdataStore
|
||||
|
||||
// Fetch session data
|
||||
transactionType, activeBal, activeSym, activeAddress, publicKey, activeDecimal, err := h.getSessionData(ctx, sessionId)
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "failed to read activeBal entry with", "key", storedb.DATA_ACTIVE_BAL, "error", err)
|
||||
return res, err
|
||||
}
|
||||
|
||||
res.Content = string(activeBal)
|
||||
// Format the active balance amount to 2 decimal places
|
||||
formattedBalance, _ := store.TruncateDecimalString(string(activeBal), 2)
|
||||
|
||||
// If normal transaction, return balance
|
||||
if string(transactionType) == "normal" {
|
||||
res.FlagReset = append(res.FlagReset, flag_swap_transaction)
|
||||
res.Content = fmt.Sprintf("%s %s", formattedBalance, string(activeSym))
|
||||
return res, nil
|
||||
}
|
||||
|
||||
res.FlagSet = append(res.FlagSet, flag_swap_transaction)
|
||||
|
||||
// Get the recipient's phone number to read other data items
|
||||
recipientPhoneNumber, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_RECIPIENT_PHONE_NUMBER)
|
||||
if err != nil {
|
||||
// invalid state
|
||||
return res, err
|
||||
}
|
||||
recipientActiveSym, recipientActiveAddress, recipientActiveDecimal, err := h.getRecipientData(ctx, string(recipientPhoneNumber))
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
// Resolve active pool address
|
||||
activePoolAddress, err := h.resolveActivePoolAddress(ctx, sessionId)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
// Check if sender token is swappable
|
||||
canSwap, err := h.accountService.CheckTokenInPool(ctx, string(activePoolAddress), string(activeAddress))
|
||||
if err != nil || !canSwap.CanSwapFrom {
|
||||
if err != nil {
|
||||
res.FlagSet = append(res.FlagSet, flag_api_error)
|
||||
logg.ErrorCtxf(ctx, "failed on CheckTokenInPool", "error", err)
|
||||
}
|
||||
res.Content = fmt.Sprintf("%s %s", formattedBalance, string(activeSym))
|
||||
return res, err
|
||||
}
|
||||
|
||||
// Calculate max swappable amount
|
||||
maxStr, err := h.calculateSwapMaxAmount(ctx, activePoolAddress, activeAddress, recipientActiveAddress, publicKey, activeDecimal)
|
||||
if err != nil {
|
||||
res.FlagSet = append(res.FlagSet, flag_api_error)
|
||||
return res, err
|
||||
}
|
||||
|
||||
// Fallback if below minimum
|
||||
maxFloat, _ := strconv.ParseFloat(maxStr, 64)
|
||||
if maxFloat < 0.1 {
|
||||
res.Content = fmt.Sprintf("%s %s", formattedBalance, string(activeSym))
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// Save max swap amount and return
|
||||
err = userStore.WriteEntry(ctx, sessionId, storedb.DATA_ACTIVE_SWAP_MAX_AMOUNT, []byte(maxStr))
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "failed to write swap max amount", "value", maxStr, "error", err)
|
||||
return res, err
|
||||
}
|
||||
|
||||
// save swap related data for the swap preview
|
||||
metadata := &dataserviceapi.TokenHoldings{
|
||||
TokenSymbol: string(recipientActiveSym),
|
||||
Balance: formattedBalance, //not used
|
||||
TokenDecimals: string(recipientActiveDecimal),
|
||||
TokenAddress: string(recipientActiveAddress),
|
||||
}
|
||||
|
||||
// Store the active swap_to data
|
||||
if err := store.UpdateSwapToVoucherData(ctx, userStore, sessionId, metadata); err != nil {
|
||||
logg.ErrorCtxf(ctx, "failed on UpdateSwapToVoucherData", "error", err)
|
||||
return res, err
|
||||
}
|
||||
|
||||
res.Content = fmt.Sprintf("%s %s", maxStr, string(activeSym))
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (h *MenuHandlers) getSessionData(ctx context.Context, sessionId string) (transactionType, activeBal, activeSym, activeAddress, publicKey, activeDecimal []byte, err error) {
|
||||
store := h.userdataStore
|
||||
|
||||
transactionType, err = store.ReadEntry(ctx, sessionId, storedb.DATA_SEND_TRANSACTION_TYPE)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
activeBal, err = store.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_BAL)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
activeAddress, err = store.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_ADDRESS)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
activeSym, err = store.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_SYM)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
publicKey, err = store.ReadEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
activeDecimal, err = store.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_DECIMAL)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (h *MenuHandlers) getRecipientData(ctx context.Context, sessionId string) (recipientActiveSym, recipientActiveAddress, recipientActiveDecimal []byte, err error) {
|
||||
store := h.userdataStore
|
||||
|
||||
recipientActiveSym, err = store.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_SYM)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
recipientActiveAddress, err = store.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_ADDRESS)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
recipientActiveDecimal, err = store.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_DECIMAL)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (h *MenuHandlers) resolveActivePoolAddress(ctx context.Context, sessionId string) ([]byte, error) {
|
||||
store := h.userdataStore
|
||||
addr, err := store.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_POOL_ADDRESS)
|
||||
if err == nil {
|
||||
return addr, nil
|
||||
}
|
||||
if db.IsNotFound(err) {
|
||||
defaultAddr := []byte(config.DefaultPoolAddress())
|
||||
if err := store.WriteEntry(ctx, sessionId, storedb.DATA_ACTIVE_POOL_ADDRESS, defaultAddr); err != nil {
|
||||
logg.ErrorCtxf(ctx, "failed to write default pool address", "error", err)
|
||||
return nil, err
|
||||
}
|
||||
return defaultAddr, nil
|
||||
}
|
||||
logg.ErrorCtxf(ctx, "failed to read active pool address", "error", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func (h *MenuHandlers) calculateSwapMaxAmount(ctx context.Context, poolAddress, fromAddress, toAddress, publicKey, decimal []byte) (string, error) {
|
||||
swapLimit, err := h.accountService.GetSwapFromTokenMaxLimit(
|
||||
ctx,
|
||||
string(poolAddress),
|
||||
string(fromAddress),
|
||||
string(toAddress),
|
||||
string(publicKey),
|
||||
)
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "failed on GetSwapFromTokenMaxLimit", "error", err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
scaled := store.ScaleDownBalance(swapLimit.Max, string(decimal))
|
||||
|
||||
formattedAmount, _ := store.TruncateDecimalString(string(scaled), 2)
|
||||
return formattedAmount, nil
|
||||
}
|
||||
|
||||
// ValidateAmount ensures that the given input is a valid amount and that
|
||||
// it is not more than the current balance.
|
||||
func (h *MenuHandlers) ValidateAmount(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
@@ -283,7 +507,7 @@ func (h *MenuHandlers) GetRecipient(ctx context.Context, sym string, input []byt
|
||||
recipient, _ := store.ReadEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE)
|
||||
if len(recipient) == 0 {
|
||||
logg.ErrorCtxf(ctx, "recipient is empty", "key", storedb.DATA_TEMPORARY_VALUE)
|
||||
return res, fmt.Errorf("Data error encountered")
|
||||
return res, fmt.Errorf("data error encountered")
|
||||
}
|
||||
|
||||
res.Content = string(recipient)
|
||||
@@ -378,3 +602,185 @@ func (h *MenuHandlers) InitiateTransaction(ctx context.Context, sym string, inpu
|
||||
res.FlagReset = append(res.FlagReset, flag_account_authorized)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// TransactionSwapPreview displays the send swap preview and estimates
|
||||
func (h *MenuHandlers) TransactionSwapPreview(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")
|
||||
|
||||
code := codeFromCtx(ctx)
|
||||
l := gotext.NewLocale(translationDir, code)
|
||||
l.AddDomain("default")
|
||||
|
||||
userStore := h.userdataStore
|
||||
|
||||
recipientPhoneNumber, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_RECIPIENT_PHONE_NUMBER)
|
||||
if err != nil {
|
||||
// invalid state
|
||||
return res, err
|
||||
}
|
||||
|
||||
swapData, err := store.ReadSwapPreviewData(ctx, userStore, sessionId)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
maxValue, err := strconv.ParseFloat(swapData.ActiveSwapMaxAmount, 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 {
|
||||
res.FlagSet = append(res.FlagSet, flag_invalid_amount)
|
||||
res.Content = inputStr
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// Format the amount to 2 decimal places
|
||||
formattedAmount, err := store.TruncateDecimalString(inputStr, 2)
|
||||
if err != nil {
|
||||
res.FlagSet = append(res.FlagSet, flag_invalid_amount)
|
||||
res.Content = inputStr
|
||||
return res, nil
|
||||
}
|
||||
|
||||
finalAmountStr, err := store.ParseAndScaleAmount(formattedAmount, swapData.ActiveSwapFromDecimal)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
err = userStore.WriteEntry(ctx, sessionId, storedb.DATA_ACTIVE_SWAP_AMOUNT, []byte(finalAmountStr))
|
||||
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
|
||||
}
|
||||
// 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)
|
||||
if err != nil {
|
||||
flag_api_error, _ := h.flagManager.GetFlag("flag_api_call_error")
|
||||
res.FlagSet = append(res.FlagSet, flag_api_error)
|
||||
res.Content = l.Get("Your request failed. Please try again later.")
|
||||
logg.ErrorCtxf(ctx, "failed on poolSwap", "error", err)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// Scale down the quoted amount
|
||||
quoteAmountStr := store.ScaleDownBalance(r.OutValue, swapData.ActiveSwapToDecimal)
|
||||
|
||||
fmt.Println("the quoteAmountStr is:", quoteAmountStr)
|
||||
qouteAmount, err := strconv.ParseFloat(quoteAmountStr, 64)
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "failed to parse quoteAmountStr as float", "value", quoteAmountStr, "error", err)
|
||||
return res, err
|
||||
}
|
||||
|
||||
// Format to 2 decimal places
|
||||
qouteStr := fmt.Sprintf("%.2f", qouteAmount)
|
||||
|
||||
res.Content = fmt.Sprintf(
|
||||
"%s will receive %s %s",
|
||||
string(recipientPhoneNumber), qouteStr, swapData.ActiveSwapToSym,
|
||||
)
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// TransactionInitiateSwap calls the poolSwap and returns a confirmation based on the result.
|
||||
func (h *MenuHandlers) TransactionInitiateSwap(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")
|
||||
flag_swap_transaction, _ := h.flagManager.GetFlag("flag_swap_transaction")
|
||||
|
||||
code := codeFromCtx(ctx)
|
||||
l := gotext.NewLocale(translationDir, code)
|
||||
l.AddDomain("default")
|
||||
|
||||
userStore := h.userdataStore
|
||||
|
||||
swapData, err := store.ReadSwapPreviewData(ctx, userStore, sessionId)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
swapAmount, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_SWAP_AMOUNT)
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "failed to read swapAmount entry with", "key", storedb.DATA_ACTIVE_SWAP_AMOUNT, "error", err)
|
||||
return res, err
|
||||
}
|
||||
|
||||
swapAmountStr := string(swapAmount)
|
||||
|
||||
// Call the poolSwap API
|
||||
poolSwap, err := h.accountService.PoolSwap(ctx, swapAmountStr, swapData.PublicKey, swapData.ActiveSwapFromAddress, swapData.ActivePoolAddress, swapData.ActiveSwapToAddress)
|
||||
if err != nil {
|
||||
flag_api_error, _ := h.flagManager.GetFlag("flag_api_call_error")
|
||||
res.FlagSet = append(res.FlagSet, flag_api_error)
|
||||
res.Content = l.Get("Your request failed. Please try again later.")
|
||||
logg.ErrorCtxf(ctx, "failed on poolSwap", "error", err)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
swapTrackingId := poolSwap.TrackingId
|
||||
logg.InfoCtxf(ctx, "poolSwap", "swapTrackingId", swapTrackingId)
|
||||
|
||||
// Initiate a send
|
||||
recipientPublicKey, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_RECIPIENT)
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "failed to read swapAmount entry with", "key", storedb.DATA_ACTIVE_SWAP_AMOUNT, "error", err)
|
||||
return res, err
|
||||
}
|
||||
recipientPhoneNumber, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_RECIPIENT_PHONE_NUMBER)
|
||||
if err != nil {
|
||||
// invalid state
|
||||
return res, err
|
||||
}
|
||||
|
||||
// Call TokenTransfer
|
||||
tokenTransfer, err := h.accountService.TokenTransfer(ctx, swapAmountStr, swapData.PublicKey, string(recipientPublicKey), swapData.ActiveSwapToAddress)
|
||||
if err != nil {
|
||||
flag_api_error, _ := h.flagManager.GetFlag("flag_api_call_error")
|
||||
res.FlagSet = append(res.FlagSet, flag_api_error)
|
||||
res.Content = l.Get("Your request failed. Please try again later.")
|
||||
logg.ErrorCtxf(ctx, "failed on TokenTransfer", "error", err)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
trackingId := tokenTransfer.TrackingId
|
||||
logg.InfoCtxf(ctx, "TokenTransfer", "trackingId", trackingId)
|
||||
|
||||
res.Content = l.Get(
|
||||
"Your request has been sent. %s will receive %s %s from %s.",
|
||||
string(recipientPhoneNumber),
|
||||
swapData.TemporaryValue,
|
||||
swapData.ActiveSwapToSym,
|
||||
sessionId,
|
||||
)
|
||||
|
||||
res.FlagReset = append(res.FlagReset, flag_account_authorized, flag_swap_transaction)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
@@ -51,6 +51,9 @@ func (h *MenuHandlers) ManageVouchers(ctx context.Context, sym string, input []b
|
||||
|
||||
res.FlagReset = append(res.FlagReset, flag_no_active_voucher)
|
||||
|
||||
// add a variable to filter out the active voucher
|
||||
activeSymStr := ""
|
||||
|
||||
// Check if user has an active voucher with proper error handling
|
||||
activeSym, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_SYM)
|
||||
if err != nil {
|
||||
@@ -62,6 +65,8 @@ func (h *MenuHandlers) ManageVouchers(ctx context.Context, sym string, input []b
|
||||
defaultDec := firstVoucher.TokenDecimals
|
||||
defaultAddr := firstVoucher.TokenAddress
|
||||
|
||||
activeSymStr = defaultSym
|
||||
|
||||
// Scale down the balance
|
||||
scaledBalance := store.ScaleDownBalance(defaultBal, defaultDec)
|
||||
|
||||
@@ -89,10 +94,8 @@ func (h *MenuHandlers) ManageVouchers(ctx context.Context, sym string, input []b
|
||||
return res, err
|
||||
}
|
||||
} else {
|
||||
// Update active voucher balance
|
||||
activeSymStr := string(activeSym)
|
||||
|
||||
// Find the matching voucher data
|
||||
activeSymStr = string(activeSym)
|
||||
var activeData *dataserviceapi.TokenHoldings
|
||||
for _, voucher := range vouchersResp {
|
||||
if voucher.TokenSymbol == activeSymStr {
|
||||
@@ -102,9 +105,10 @@ func (h *MenuHandlers) ManageVouchers(ctx context.Context, sym string, input []b
|
||||
}
|
||||
|
||||
if activeData == nil {
|
||||
logg.ErrorCtxf(ctx, "activeSym not found in vouchers, setting the first voucher as the default", "activeSym", activeSymStr)
|
||||
logg.InfoCtxf(ctx, "activeSym not found in vouchers, setting the first voucher as the default", "activeSym", activeSymStr)
|
||||
firstVoucher := vouchersResp[0]
|
||||
activeData = &firstVoucher
|
||||
activeSymStr = string(activeData.TokenSymbol)
|
||||
}
|
||||
|
||||
// Scale down the balance
|
||||
@@ -120,8 +124,17 @@ func (h *MenuHandlers) ManageVouchers(ctx context.Context, sym string, input []b
|
||||
}
|
||||
}
|
||||
|
||||
// Store all voucher data
|
||||
data := store.ProcessVouchers(vouchersResp)
|
||||
// Filter out the active voucher from vouchersResp
|
||||
filteredVouchers := make([]dataserviceapi.TokenHoldings, 0, len(vouchersResp))
|
||||
for _, v := range vouchersResp {
|
||||
if v.TokenSymbol != activeSymStr {
|
||||
filteredVouchers = append(filteredVouchers, v)
|
||||
}
|
||||
}
|
||||
|
||||
// Store all voucher data (excluding the current active voucher)
|
||||
data := store.ProcessVouchers(filteredVouchers)
|
||||
|
||||
dataMap := map[storedb.DataTyp]string{
|
||||
storedb.DATA_VOUCHER_SYMBOLS: data.Symbols,
|
||||
storedb.DATA_VOUCHER_BALANCES: data.Balances,
|
||||
@@ -140,7 +153,7 @@ func (h *MenuHandlers) ManageVouchers(ctx context.Context, sym string, input []b
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// GetVoucherList fetches the list of vouchers and formats them.
|
||||
// GetVoucherList fetches the list of vouchers from the store and formats them.
|
||||
func (h *MenuHandlers) GetVoucherList(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
var res resource.Result
|
||||
sessionId, ok := ctx.Value("SessionId").(string)
|
||||
@@ -191,6 +204,10 @@ func (h *MenuHandlers) ViewVoucher(ctx context.Context, sym string, input []byte
|
||||
flag_incorrect_voucher, _ := h.flagManager.GetFlag("flag_incorrect_voucher")
|
||||
|
||||
inputStr := string(input)
|
||||
if inputStr == "0" || inputStr == "99" || inputStr == "88" || inputStr == "98" {
|
||||
res.FlagReset = append(res.FlagReset, flag_incorrect_voucher)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
metadata, err := store.GetVoucherData(ctx, h.userdataStore, sessionId, inputStr)
|
||||
if err != nil {
|
||||
@@ -207,8 +224,16 @@ func (h *MenuHandlers) ViewVoucher(ctx context.Context, sym string, input []byte
|
||||
return res, err
|
||||
}
|
||||
|
||||
// Format the balance to 2 decimal places
|
||||
formattedAmount, err := store.TruncateDecimalString(metadata.Balance, 2)
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "failed to TruncateDecimalString on ViewVoucher", "error", err)
|
||||
res.FlagSet = append(res.FlagSet, flag_incorrect_voucher)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
res.FlagReset = append(res.FlagReset, flag_incorrect_voucher)
|
||||
res.Content = l.Get("Symbol: %s\nBalance: %s", metadata.TokenSymbol, metadata.Balance)
|
||||
res.Content = l.Get("Symbol: %s\nBalance: %s", metadata.TokenSymbol, formattedAmount)
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
@@ -73,8 +73,8 @@ func TestManageVouchers(t *testing.T) {
|
||||
Balance: "100",
|
||||
},
|
||||
},
|
||||
expectedVoucherSymbols: []byte("1:TOKEN1"),
|
||||
expectedUpdatedAddress: []byte("0x123"),
|
||||
expectedVoucherSymbols: []byte(""),
|
||||
expectedUpdatedAddress: []byte(""),
|
||||
expectedResult: resource.Result{
|
||||
FlagReset: []uint32{flag_api_error, flag_no_active_voucher},
|
||||
},
|
||||
@@ -86,7 +86,7 @@ func TestManageVouchers(t *testing.T) {
|
||||
{TokenAddress: "0x41c188d63Qa", TokenSymbol: "MILO", TokenDecimals: "4", Balance: "200"},
|
||||
},
|
||||
storedActiveVoucher: "SRF",
|
||||
expectedVoucherSymbols: []byte("1:SRF\n2:MILO"),
|
||||
expectedVoucherSymbols: []byte("1:MILO"),
|
||||
expectedUpdatedAddress: []byte("0xd4c288865Ce"),
|
||||
expectedResult: resource.Result{
|
||||
FlagReset: []uint32{flag_api_error, flag_no_active_voucher},
|
||||
|
||||
@@ -136,6 +136,9 @@ func (ls *LocalHandlerService) GetHandler(accountService remote.AccountService)
|
||||
ls.DbRs.AddLocalFunc("swap_max_limit", appHandlers.SwapMaxLimit)
|
||||
ls.DbRs.AddLocalFunc("swap_preview", appHandlers.SwapPreview)
|
||||
ls.DbRs.AddLocalFunc("initiate_swap", appHandlers.InitiateSwap)
|
||||
ls.DbRs.AddLocalFunc("transaction_swap_preview", appHandlers.TransactionSwapPreview)
|
||||
ls.DbRs.AddLocalFunc("transaction_initiate_swap", appHandlers.TransactionInitiateSwap)
|
||||
|
||||
ls.first = appHandlers.Init
|
||||
|
||||
return appHandlers, nil
|
||||
|
||||
@@ -4,6 +4,7 @@ RELOAD max_amount
|
||||
MAP max_amount
|
||||
MOUT back 0
|
||||
HALT
|
||||
CATCH transaction_swap flag_swap_transaction 1
|
||||
LOAD validate_amount 64
|
||||
RELOAD validate_amount
|
||||
CATCH api_failure flag_api_call_error 1
|
||||
|
||||
@@ -9,14 +9,16 @@ MAP check_balance
|
||||
MOUT send 1
|
||||
MOUT swap 2
|
||||
MOUT vouchers 3
|
||||
MOUT account 4
|
||||
MOUT help 5
|
||||
MOUT select_pool 4
|
||||
MOUT account 5
|
||||
MOUT help 6
|
||||
MOUT quit 9
|
||||
HALT
|
||||
INCMP send 1
|
||||
INCMP swap_to_list 2
|
||||
INCMP my_vouchers 3
|
||||
INCMP my_account 4
|
||||
INCMP help 5
|
||||
INCMP select_pool 4
|
||||
INCMP my_account 5
|
||||
INCMP help 6
|
||||
INCMP quit 9
|
||||
INCMP . *
|
||||
|
||||
@@ -2,11 +2,9 @@ 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 . *
|
||||
|
||||
@@ -35,3 +35,4 @@ flag,flag_account_pin_reset,41,this is set on an account when an admin triggers
|
||||
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_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
|
||||
|
||||
|
@@ -4,11 +4,17 @@ RELOAD swap_to_list
|
||||
MAP swap_to_list
|
||||
CATCH missing_voucher flag_incorrect_voucher 1
|
||||
MOUT back 0
|
||||
MOUT quit 99
|
||||
MNEXT next 88
|
||||
MPREV prev 98
|
||||
HALT
|
||||
INCMP > 88
|
||||
INCMP < 98
|
||||
INCMP _ 0
|
||||
INCMP quit 99
|
||||
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
|
||||
INCMP swap_limit *
|
||||
|
||||
3
services/registration/transaction_swap
Normal file
3
services/registration/transaction_swap
Normal file
@@ -0,0 +1,3 @@
|
||||
{{.transaction_swap_preview}}
|
||||
|
||||
Please enter your PIN to confirm:
|
||||
12
services/registration/transaction_swap.vis
Normal file
12
services/registration/transaction_swap.vis
Normal file
@@ -0,0 +1,12 @@
|
||||
LOAD transaction_swap_preview 0
|
||||
MAP transaction_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
|
||||
INCMP _ 0
|
||||
INCMP quit 9
|
||||
INCMP transaction_swap_initiated *
|
||||
4
services/registration/transaction_swap_initiated.vis
Normal file
4
services/registration/transaction_swap_initiated.vis
Normal file
@@ -0,0 +1,4 @@
|
||||
LOAD reset_incorrect_pin 6
|
||||
CATCH _ flag_account_authorized 0
|
||||
LOAD transaction_initiate_swap 0
|
||||
HALT
|
||||
@@ -89,6 +89,10 @@ const (
|
||||
DATA_ACTIVE_POOL_NAME
|
||||
// Holds the active pool symbol for the swap
|
||||
DATA_ACTIVE_POOL_SYM
|
||||
// Holds the send transaction type
|
||||
DATA_SEND_TRANSACTION_TYPE
|
||||
// Holds the recipient formatted phone number
|
||||
DATA_RECIPIENT_PHONE_NUMBER
|
||||
)
|
||||
|
||||
const (
|
||||
|
||||
Reference in New Issue
Block a user