menu-voucherlist #101

Merged
lash merged 63 commits from menu-voucherlist into master 2024-10-25 15:59:47 +02:00
7 changed files with 152 additions and 37 deletions
Showing only changes of commit 517f980664 - Show all commits

View File

@ -92,6 +92,7 @@ func (ls *LocalHandlerService) GetHandler() (*ussd.Handlers, error) {
ls.DbRs.AddLocalFunc("verify_new_pin", ussdHandlers.VerifyNewPin)
ls.DbRs.AddLocalFunc("confirm_pin_change", ussdHandlers.ConfirmPinChange)
ls.DbRs.AddLocalFunc("quit_with_help", ussdHandlers.QuitWithHelp)
ls.DbRs.AddLocalFunc("check_vouchers", ussdHandlers.CheckVouchers)
ls.DbRs.AddLocalFunc("get_vouchers", ussdHandlers.GetVoucherList)
return ussdHandlers, nil

View File

@ -13,14 +13,12 @@ type AccountServiceInterface interface {
CheckBalance(publicKey string) (string, error)
CreateAccount() (*models.AccountResponse, error)
CheckAccountStatus(trackingId string) (string, error)
FetchVouchersFromAPI() ([]models.VoucherHolding, error)
FetchVouchers(publicKey string) (*models.VoucherHoldingResponse, error)
}
type AccountService struct {
}
// CheckAccountStatus retrieves the status of an account transaction based on the provided tracking ID.
//
// Parameters:
@ -28,12 +26,10 @@ type AccountService struct {
// CreateAccount or a similar function that returns an AccountResponse. The `trackingId` field in the
// AccountResponse struct can be used here to check the account status during a transaction.
//
//
// Returns:
// - string: The status of the transaction as a string. If there is an error during the request or processing, this will be an empty string.
// - error: An error if any occurred during the HTTP request, reading the response, or unmarshalling the JSON data.
// If no error occurs, this will be nil.
//
func (as *AccountService) CheckAccountStatus(trackingId string) (string, error) {
resp, err := http.Get(config.TrackStatusURL + trackingId)
if err != nil {
@ -57,7 +53,6 @@ func (as *AccountService) CheckAccountStatus(trackingId string) (string, error)
return status, nil
}
// CheckBalance retrieves the balance for a given public key from the custodial balance API endpoint.
// Parameters:
// - publicKey: The public key associated with the account whose balance needs to be checked.
@ -84,8 +79,7 @@ func (as *AccountService) CheckBalance(publicKey string) (string, error) {
return balance, nil
}
//CreateAccount creates a new account in the custodial system.
// CreateAccount creates a new account in the custodial system.
// Returns:
// - *models.AccountResponse: A pointer to an AccountResponse struct containing the details of the created account.
// If there is an error during the request or processing, this will be nil.
@ -112,30 +106,86 @@ func (as *AccountService) CreateAccount() (*models.AccountResponse, error) {
return &accountResp, nil
}
// fetchVouchersFromAPI calls the API to get the list of vouchers belonging to the user
func (as *AccountService) FetchVouchersFromAPI() ([]models.VoucherHolding, error) {
// FetchVouchers retrieves the token holdings for a given public key from the custodial holdings API endpoint
// Parameters:
// - publicKey: The public key associated with the account.
func (as *AccountService) FetchVouchers(publicKey string) (*models.VoucherHoldingResponse, error) {
// TODO replace with the actual request once ready
mockJSON := `[
{
"symbol": "MUMO",
"address": "0x078b3a26596218507781722A4e8825BFB9570Fba"
},
{
"symbol": "SRF",
"address": "0x45d747172e77d55575c197CbA9451bC2CD8F4958"
},
{
"symbol": "HALGAN",
"address": "0x12169Fb5931A599ad1283bb8311Dad54Feb51A28"
mockJSON := `{
"ok": true,
"description": "Token holdings with current balances",
"result": {
"holdings": [
{
"contractAddress": "0x6CC75A06ac72eB4Db2eE22F781F5D100d8ec03ee",
"tokenSymbol": "FSPTST",
"tokenDecimals": "6",
"balance": "8869964242"
},
{
"contractAddress": "0x724F2910D790B54A39a7638282a45B1D83564fFA",
"tokenSymbol": "GEO",
"tokenDecimals": "6",
"balance": "9884"
},
{
"contractAddress": "0x2105a206B7bec31E2F90acF7385cc8F7F5f9D273",
"tokenSymbol": "MFNK",
"tokenDecimals": "6",
"balance": "19788697"
},
{
"contractAddress": "0x63DE2Ac8D1008351Cc69Fb8aCb94Ba47728a7E83",
"tokenSymbol": "MILO",
"tokenDecimals": "6",
"balance": "75"
},
{
"contractAddress": "0xd4c288865Ce0985a481Eef3be02443dF5E2e4Ea9",
"tokenSymbol": "SOHAIL",
"tokenDecimals": "6",
"balance": "27874115"
},
{
"contractAddress": "0x45d747172e77d55575c197CbA9451bC2CD8F4958",
"tokenSymbol": "SRQIF",
"tokenDecimals": "6",
"balance": "2745987"
},
{
"contractAddress": "0x45d747172e77d55575c197CbA9451bC2CD8F4958",
"tokenSymbol": "SRFI",
"tokenDecimals": "6",
"balance": "2745987"
},
{
"contractAddress": "0x45d747172e77d55575c197CbA9451bC2CD8F4958",
"tokenSymbol": "SRFU",
"tokenDecimals": "6",
"balance": "2745987"
},
{
"contractAddress": "0x45d747172e77d55575c197CbA9451bC2CD8F4958",
"tokenSymbol": "SRQF",
"tokenDecimals": "6",
"balance": "2745987"
},
{
"contractAddress": "0x45d747172e77d55575c197CbA9451bC2CD8F4958",
"tokenSymbol": "SREF",
"tokenDecimals": "6",
"balance": "2745987"
}
]
}
]`
}`
// Unmarshal the JSON response
var holdings []models.VoucherHolding
var holdings models.VoucherHoldingResponse
err := json.Unmarshal([]byte(mockJSON), &holdings)
if err != nil {
return nil, err
}
return holdings, nil
return &holdings, nil
}

View File

@ -3,6 +3,7 @@ package ussd
import (
"bytes"
"context"
"encoding/json"
"fmt"
"path"
"regexp"
@ -232,22 +233,36 @@ func (h *Handlers) SaveTemporaryPin(ctx context.Context, sym string, input []byt
}
// GetVoucherList fetches the list of vouchers and formats them
// checks whether they are synced internally before calling the API
// checks whether they are stored internally before calling the API
func (h *Handlers) GetVoucherList(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")
}
// check if the vouchers exist internally and if not
// fetch from the API
// Fetch vouchers from API
vouchers, err := h.accountService.FetchVouchersFromAPI()
// Read vouchers from the store
store := h.userdataStore
voucherData, err := store.ReadEntry(ctx, sessionId, utils.DATA_VOUCHER_LIST)
if err != nil {
return res, fmt.Errorf("error fetching vouchers: %w", err)
return res, err
}
// Unmarshal the stored JSON data into the correct struct
var vouchers []struct {
TokenSymbol string `json:"tokenSymbol"`
}
err = json.Unmarshal(voucherData, &vouchers)
if err != nil {
return res, fmt.Errorf("failed to unmarshal vouchers: %v", err)
}
var numberedVouchers []string
for i, voucher := range vouchers {
numberedVouchers = append(numberedVouchers, fmt.Sprintf("%d:%s", i+1, voucher.Symbol))
numberedVouchers = append(numberedVouchers, fmt.Sprintf("%d:%s", i+1, voucher.TokenSymbol))
}
res.Content = strings.Join(numberedVouchers, "\n")
@ -1007,3 +1022,41 @@ func (h *Handlers) GetProfileInfo(ctx context.Context, sym string, input []byte)
return res, nil
}
// CheckVouchers retrieves the token holdings from the API using the "PublicKey" and stores
// them to gdbm
func (h *Handlers) CheckVouchers(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
publicKey, err := store.ReadEntry(ctx, sessionId, utils.DATA_PUBLIC_KEY)
if err != nil {
return res, nil
}
// Fetch vouchers from the API
vouchersResp, err := h.accountService.FetchVouchers(string(publicKey))
if err != nil {
return res, nil
}
// Convert only the list of holdings (vouchers) to JSON
voucherBytes, err := json.Marshal(vouchersResp.Result.Holdings)
if err != nil {
return res, nil
}
// Store the voucher symbols in the userdataStore
err = store.WriteEntry(ctx, sessionId, utils.DATA_VOUCHER_LIST, voucherBytes)
if err != nil {
return res, nil
}
return res, nil
}

View File

@ -25,7 +25,7 @@ func (m *MockAccountService) CheckAccountStatus(trackingId string) (string, erro
return args.String(0), args.Error(1)
}
func (m *MockAccountService) FetchVouchersFromAPI() ([]models.VoucherHolding, error) {
func (m *MockAccountService) FetchVouchers(publicKey string) (*models.VoucherHoldingResponse, error) {
args := m.Called()
return args.Get(0).([]models.VoucherHolding), args.Error(1)
return args.Get(0).(*models.VoucherHoldingResponse), args.Error(1)
}

View File

@ -1,7 +1,15 @@
package models
// VoucherHolding represents a single voucher holding
type VoucherHolding struct {
Symbol string `json:"symbol"`
Address string `json:"address"`
// VoucherHoldingResponse represents a single voucher holding
type VoucherHoldingResponse struct {
Ok bool `json:"ok"`
Description string `json:"description"`
Result struct {
Holdings []struct {
ContractAddress string `json:"contractAddress"`
TokenSymbol string `json:"tokenSymbol"`
TokenDecimals string `json:"tokenDecimals"`
Balance string `json:"balance"`
} `json:"holdings"`
} `json:"result"`
}

View File

@ -23,6 +23,7 @@ const (
DATA_RECIPIENT
DATA_AMOUNT
DATA_TEMPORARY_PIN
DATA_VOUCHER_LIST
)
func typToBytes(typ DataTyp) []byte {

View File

@ -1,5 +1,7 @@
LOAD check_balance 64
RELOAD check_balance
LOAD check_vouchers 10
RELOAD check_vouchers
MAP check_balance
MOUT send 1
MOUT vouchers 2