Compare commits
6 Commits
f643aa4d14
...
acd0af25c6
Author | SHA1 | Date | |
---|---|---|---|
acd0af25c6 | |||
66d2e2e838 | |||
4beeb9986a | |||
d81bc0eefb | |||
5b0a383513 | |||
367d3f0593 |
12
go.mod
12
go.mod
@ -1,27 +1,33 @@
|
||||
module git.grassecon.net/urdt/ussd
|
||||
|
||||
go 1.22.6
|
||||
go 1.23.0
|
||||
|
||||
toolchain go1.23.2
|
||||
|
||||
require (
|
||||
git.defalsify.org/vise.git v0.1.0-rc.3.0.20240923162317-c20d557a3dbb
|
||||
github.com/alecthomas/assert/v2 v2.2.2
|
||||
github.com/peteole/testdata-loader v0.3.0
|
||||
gopkg.in/leonelquinteros/gotext.v1 v1.3.1
|
||||
|
||||
)
|
||||
|
||||
require github.com/grassrootseconomics/eth-custodial v1.3.0-beta // indirect
|
||||
|
||||
require (
|
||||
github.com/alecthomas/participle/v2 v2.0.0 // indirect
|
||||
github.com/alecthomas/repr v0.2.0 // indirect
|
||||
github.com/barbashov/iso639-3 v0.0.0-20211020172741-1f4ffb2d8d1c // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
|
||||
github.com/fxamacker/cbor/v2 v2.4.0 // indirect
|
||||
github.com/gofrs/uuid v4.4.0+incompatible
|
||||
github.com/graygnuorg/go-gdbm v0.0.0-20220711140707-71387d66dce4 // indirect
|
||||
github.com/hexops/gotextdiff v1.0.3 // indirect
|
||||
github.com/mattn/kinako v0.0.0-20170717041458-332c0a7e205a // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
|
||||
github.com/stretchr/objx v0.5.2 // indirect
|
||||
github.com/stretchr/testify v1.9.0
|
||||
github.com/x448/float16 v0.8.4 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
|
||||
)
|
||||
|
6
go.sum
6
go.sum
@ -10,10 +10,14 @@ github.com/barbashov/iso639-3 v0.0.0-20211020172741-1f4ffb2d8d1c h1:H9Nm+I7Cg/YV
|
||||
github.com/barbashov/iso639-3 v0.0.0-20211020172741-1f4ffb2d8d1c/go.mod h1:rGod7o6KPeJ+hyBpHfhi4v7blx9sf+QsHsA7KAsdN6U=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/fxamacker/cbor/v2 v2.4.0 h1:ri0ArlOR+5XunOP8CRUowT0pSJOwhW098ZCUyskZD88=
|
||||
github.com/fxamacker/cbor/v2 v2.4.0/go.mod h1:TA1xS00nchWmaBnEIxPSE5oHLuJBAVvqrtAnWBwBCVo=
|
||||
github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA=
|
||||
github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
|
||||
github.com/grassrootseconomics/eth-custodial v1.3.0-beta h1:twrMBhl89GqDUL9PlkzQxMP/6OST1BByrNDj+rqXDmU=
|
||||
github.com/grassrootseconomics/eth-custodial v1.3.0-beta/go.mod h1:7uhRcdnJplX4t6GKCEFkbeDhhjlcaGJeJqevbcvGLZo=
|
||||
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=
|
||||
@ -24,6 +28,8 @@ github.com/peteole/testdata-loader v0.3.0 h1:8jckE9KcyNHgyv/VPoaljvKZE0Rqr8+dPVY
|
||||
github.com/peteole/testdata-loader v0.3.0/go.mod h1:Mt0ZbRtb56u8SLJpNP+BnQbENljMorYBpqlvt3cS83U=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
|
||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
|
||||
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
|
@ -9,18 +9,19 @@ import (
|
||||
|
||||
"git.grassecon.net/urdt/ussd/config"
|
||||
"git.grassecon.net/urdt/ussd/internal/models"
|
||||
"github.com/grassrootseconomics/eth-custodial/pkg/api"
|
||||
)
|
||||
|
||||
var apiResponse struct {
|
||||
type ApiResponse struct {
|
||||
Ok bool `json:"ok"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
type AccountServiceInterface interface {
|
||||
CheckBalance(publicKey string) (*models.BalanceResponse, error)
|
||||
CreateAccount() (*OKResponse, *ErrResponse)
|
||||
CreateAccount() (*api.OKResponse, *api.ErrResponse)
|
||||
CheckAccountStatus(trackingId string) (*models.TrackStatusResponse, error)
|
||||
TrackAccountStatus(publicKey string) (*OKResponse, *ErrResponse)
|
||||
TrackAccountStatus(publicKey string) (*api.OKResponse, *api.ErrResponse)
|
||||
}
|
||||
|
||||
type AccountService struct {
|
||||
@ -59,9 +60,9 @@ func (as *AccountService) CheckAccountStatus(trackingId string) (*models.TrackSt
|
||||
|
||||
}
|
||||
|
||||
func (as *AccountService) TrackAccountStatus(publicKey string) (*OKResponse, *ErrResponse) {
|
||||
var errResponse ErrResponse
|
||||
var okResponse OKResponse
|
||||
func (as *AccountService) TrackAccountStatus(publicKey string) (*api.OKResponse, *api.ErrResponse) {
|
||||
var errResponse api.ErrResponse
|
||||
var okResponse api.OKResponse
|
||||
var err error
|
||||
// Construct the URL with the path parameter
|
||||
url := fmt.Sprintf("%s/%s", config.TrackURL, publicKey)
|
||||
@ -90,6 +91,7 @@ func (as *AccountService) TrackAccountStatus(publicKey string) (*OKResponse, *Er
|
||||
}
|
||||
|
||||
// Step 2: Unmarshal into the generic struct
|
||||
var apiResponse ApiResponse
|
||||
err = json.Unmarshal([]byte(body), &apiResponse)
|
||||
if err != nil {
|
||||
errResponse.Description = err.Error()
|
||||
@ -139,10 +141,10 @@ func (as *AccountService) CheckBalance(publicKey string) (*models.BalanceRespons
|
||||
// If there is an error during the request or processing, this will be nil.
|
||||
// - 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) CreateAccount() (*OKResponse, *ErrResponse) {
|
||||
func (as *AccountService) CreateAccount() (*api.OKResponse, *api.ErrResponse) {
|
||||
|
||||
var errResponse ErrResponse
|
||||
var okResponse OKResponse
|
||||
var errResponse api.ErrResponse
|
||||
var okResponse api.OKResponse
|
||||
var err error
|
||||
|
||||
// Create a new request
|
||||
@ -166,6 +168,7 @@ func (as *AccountService) CreateAccount() (*OKResponse, *ErrResponse) {
|
||||
errResponse.Description = err.Error()
|
||||
return nil, &errResponse
|
||||
}
|
||||
var apiResponse ApiResponse
|
||||
err = json.Unmarshal([]byte(body), &apiResponse)
|
||||
if err != nil {
|
||||
return nil, &errResponse
|
||||
@ -187,8 +190,8 @@ func (as *AccountService) CreateAccount() (*OKResponse, *ErrResponse) {
|
||||
}
|
||||
}
|
||||
|
||||
func (tas *TestAccountService) CreateAccount() (*OKResponse, *ErrResponse) {
|
||||
return &OKResponse{
|
||||
func (tas *TestAccountService) CreateAccount() (*api.OKResponse, *api.ErrResponse) {
|
||||
return &api.OKResponse{
|
||||
Ok: true,
|
||||
Description: "Account creation request received successfully",
|
||||
Result: map[string]any{"publicKey": "0x48ADca309b5085852207FAaf2816eD72B52F527C", "trackingId": "28ebe84d-b925-472c-87ae-bbdfa1fb97be"},
|
||||
@ -212,8 +215,8 @@ func (tas *TestAccountService) CheckBalance(publicKey string) (*models.BalanceRe
|
||||
return balanceResponse, nil
|
||||
}
|
||||
|
||||
func (tas *TestAccountService) TrackAccountStatus(publicKey string) (*OKResponse, *ErrResponse) {
|
||||
return &OKResponse{
|
||||
func (tas *TestAccountService) TrackAccountStatus(publicKey string) (*api.OKResponse, *api.ErrResponse) {
|
||||
return &api.OKResponse{
|
||||
Ok: true,
|
||||
Description: "Account creation succeeded",
|
||||
Result: map[string]any{
|
||||
|
@ -1,60 +0,0 @@
|
||||
package server
|
||||
|
||||
type (
|
||||
OKResponse struct {
|
||||
Ok bool `json:"ok"`
|
||||
Description string `json:"description"`
|
||||
Result map[string]any `json:"result"`
|
||||
}
|
||||
|
||||
ErrResponse struct {
|
||||
Ok bool `json:"ok"`
|
||||
Description string `json:"description"`
|
||||
ErrCode string `json:"errorCode"`
|
||||
}
|
||||
|
||||
TransferRequest struct {
|
||||
From string `json:"from" validate:"required,eth_addr_checksum"`
|
||||
To string `json:"to" validate:"required,eth_addr_checksum"`
|
||||
TokenAddress string `json:"tokenAddress" validate:"required,eth_addr_checksum"`
|
||||
Amount string `json:"amount" validate:"required,number,gt=0"`
|
||||
}
|
||||
|
||||
PoolSwapRequest struct {
|
||||
From string `json:"from" validate:"required,eth_addr_checksum"`
|
||||
FromTokenAddress string `json:"fromTokenAddress" validate:"required,eth_addr_checksum"`
|
||||
ToTokenAddress string `json:"toTokenAddress" validate:"required,eth_addr_checksum"`
|
||||
PoolAddress string `json:"poolAddress" validate:"required,eth_addr_checksum"`
|
||||
Amount string `json:"amount" validate:"required,number,gt=0"`
|
||||
}
|
||||
|
||||
PoolDepositRequest struct {
|
||||
From string `json:"from" validate:"required,eth_addr_checksum"`
|
||||
TokenAddress string `json:"tokenAddress" validate:"required,eth_addr_checksum"`
|
||||
PoolAddress string `json:"poolAddress" validate:"required,eth_addr_checksum"`
|
||||
Amount string `json:"amount" validate:"required,number,gt=0"`
|
||||
}
|
||||
|
||||
AccountAddressParam struct {
|
||||
Address string `param:"address" validate:"required,eth_addr_checksum"`
|
||||
}
|
||||
|
||||
TrackingIDParam struct {
|
||||
TrackingID string `param:"trackingId" validate:"required,uuid"`
|
||||
}
|
||||
|
||||
OTXByAccountRequest struct {
|
||||
Address string `param:"address" validate:"required,eth_addr_checksum"`
|
||||
PerPage int `query:"perPage" validate:"required,number,gt=0"`
|
||||
Cursor int `query:"cursor" validate:"number"`
|
||||
Next bool `query:"next"`
|
||||
}
|
||||
)
|
||||
|
||||
const (
|
||||
ErrCodeInternalServerError = "E01"
|
||||
ErrCodeInvalidJSON = "E02"
|
||||
ErrCodeInvalidAPIKey = "E03"
|
||||
ErrCodeValidationFailed = "E04"
|
||||
ErrCodeAccountNotExists = "E05"
|
||||
)
|
@ -3,6 +3,7 @@ package ussd
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"path"
|
||||
"regexp"
|
||||
@ -10,6 +11,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"git.defalsify.org/vise.git/asm"
|
||||
"github.com/grassrootseconomics/eth-custodial/pkg/api"
|
||||
|
||||
"git.defalsify.org/vise.git/cache"
|
||||
"git.defalsify.org/vise.git/db"
|
||||
@ -27,8 +29,8 @@ var (
|
||||
logg = logging.NewVanilla().WithDomain("ussdmenuhandler")
|
||||
scriptDir = path.Join("services", "registration")
|
||||
translationDir = path.Join(scriptDir, "locale")
|
||||
okResponse *server.OKResponse
|
||||
errResponse *server.ErrResponse
|
||||
okResponse *api.OKResponse
|
||||
errResponse *api.ErrResponse
|
||||
)
|
||||
|
||||
// FlagManager handles centralized flag management
|
||||
@ -138,12 +140,19 @@ func (h *Handlers) SetLanguage(ctx context.Context, sym string, input []byte) (r
|
||||
}
|
||||
|
||||
func (h *Handlers) createAccountNoExist(ctx context.Context, sessionId string, res *resource.Result) error {
|
||||
flag_account_created, _ := h.flagManager.GetFlag("flag_account_created")
|
||||
flag_api_call_error, _ := h.flagManager.GetFlag("flag_api_call_error")
|
||||
okResponse, errResponse := h.accountService.CreateAccount()
|
||||
if errResponse != nil {
|
||||
return nil
|
||||
if !errResponse.Ok {
|
||||
res.FlagSet = append(res.FlagSet, flag_api_call_error)
|
||||
return nil
|
||||
}
|
||||
return errors.New(errResponse.Description)
|
||||
}
|
||||
trackingId := okResponse.Result["trackingId"].(string)
|
||||
publicKey := okResponse.Result["publicKey"].(string)
|
||||
res.FlagReset = append(res.FlagReset, flag_api_call_error)
|
||||
|
||||
data := map[utils.DataTyp]string{
|
||||
utils.DATA_TRACKING_ID: trackingId,
|
||||
@ -157,7 +166,6 @@ func (h *Handlers) createAccountNoExist(ctx context.Context, sessionId string, r
|
||||
return err
|
||||
}
|
||||
}
|
||||
flag_account_created, _ := h.flagManager.GetFlag("flag_account_created")
|
||||
res.FlagSet = append(res.FlagSet, flag_account_created)
|
||||
return nil
|
||||
|
||||
@ -546,6 +554,9 @@ func (h *Handlers) CheckAccountStatus(ctx context.Context, sym string, input []b
|
||||
}
|
||||
okResponse, errResponse = h.accountService.TrackAccountStatus(string(publicKey))
|
||||
if errResponse != nil {
|
||||
if !errResponse.Ok {
|
||||
res.FlagSet = append(res.FlagSet, flag_api_error)
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
res.FlagReset = append(res.FlagReset, flag_api_error)
|
||||
|
@ -20,6 +20,7 @@ import (
|
||||
"git.grassecon.net/urdt/ussd/internal/models"
|
||||
"git.grassecon.net/urdt/ussd/internal/utils"
|
||||
"github.com/alecthomas/assert/v2"
|
||||
"github.com/grassrootseconomics/eth-custodial/pkg/api"
|
||||
testdataloader "github.com/peteole/testdata-loader"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
@ -74,6 +75,8 @@ func TestCreateAccount(t *testing.T) {
|
||||
}
|
||||
// Create required mocks
|
||||
flag_account_created, err := fm.GetFlag("flag_account_created")
|
||||
//flag_api_call_error, err := fm.GetFlag("flag_api_call_error,")
|
||||
flag_api_call_error, _ := fm.GetFlag("flag_api_call_error")
|
||||
if err != nil {
|
||||
t.Logf(err.Error())
|
||||
}
|
||||
@ -85,12 +88,12 @@ func TestCreateAccount(t *testing.T) {
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
serverResponse *server.OKResponse
|
||||
serverResponse *api.OKResponse
|
||||
expectedResult resource.Result
|
||||
}{
|
||||
{
|
||||
name: "Test account creation success",
|
||||
serverResponse: &server.OKResponse{
|
||||
serverResponse: &api.OKResponse{
|
||||
Ok: true,
|
||||
Description: "Account creation successed",
|
||||
Result: map[string]any{
|
||||
@ -100,6 +103,7 @@ func TestCreateAccount(t *testing.T) {
|
||||
},
|
||||
expectedResult: resource.Result{
|
||||
FlagSet: []uint32{flag_account_created},
|
||||
FlagReset: []uint32{flag_api_call_error},
|
||||
},
|
||||
},
|
||||
}
|
||||
@ -1073,14 +1077,14 @@ func TestCheckAccountStatus(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input []byte
|
||||
serverResponse *server.OKResponse
|
||||
serverResponse *api.OKResponse
|
||||
response *models.TrackStatusResponse
|
||||
expectedResult resource.Result
|
||||
}{
|
||||
{
|
||||
name: "Test when account is on the Sarafu network",
|
||||
input: []byte("TrackingId1234"),
|
||||
serverResponse: &server.OKResponse{
|
||||
serverResponse: &api.OKResponse{
|
||||
Ok: true,
|
||||
Description: "Account creation succeeded",
|
||||
Result: map[string]any{
|
||||
@ -1135,7 +1139,7 @@ func TestCheckAccountStatus(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
serverResponse: &server.OKResponse{
|
||||
serverResponse: &api.OKResponse{
|
||||
Ok: true,
|
||||
Description: "Account creation succeeded",
|
||||
Result: map[string]any{
|
||||
|
@ -1,8 +1,8 @@
|
||||
package mocks
|
||||
|
||||
import (
|
||||
"git.grassecon.net/urdt/ussd/internal/handlers/server"
|
||||
"git.grassecon.net/urdt/ussd/internal/models"
|
||||
"github.com/grassrootseconomics/eth-custodial/pkg/api"
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
@ -11,10 +11,10 @@ type MockAccountService struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
func (m *MockAccountService) CreateAccount() (*server.OKResponse, *server.ErrResponse) {
|
||||
func (m *MockAccountService) CreateAccount() (*api.OKResponse, *api.ErrResponse) {
|
||||
args := m.Called()
|
||||
okResponse, ok := args.Get(0).(*server.OKResponse)
|
||||
errResponse, err := args.Get(1).(*server.ErrResponse)
|
||||
okResponse, ok := args.Get(0).(*api.OKResponse)
|
||||
errResponse, err := args.Get(1).(*api.ErrResponse)
|
||||
|
||||
if ok {
|
||||
return okResponse, nil
|
||||
@ -36,10 +36,10 @@ func (m *MockAccountService) CheckAccountStatus(trackingId string) (*models.Trac
|
||||
return args.Get(0).(*models.TrackStatusResponse), args.Error(1)
|
||||
}
|
||||
|
||||
func (m *MockAccountService) TrackAccountStatus(publicKey string) (*server.OKResponse, *server.ErrResponse) {
|
||||
func (m *MockAccountService) TrackAccountStatus(publicKey string) (*api.OKResponse, *api.ErrResponse) {
|
||||
args := m.Called(publicKey)
|
||||
okResponse, ok := args.Get(0).(*server.OKResponse)
|
||||
errResponse, err := args.Get(1).(*server.ErrResponse)
|
||||
okResponse, ok := args.Get(0).(*api.OKResponse)
|
||||
errResponse, err := args.Get(1).(*api.ErrResponse)
|
||||
if ok {
|
||||
return okResponse, nil
|
||||
}
|
||||
|
@ -1,4 +1,6 @@
|
||||
LOAD create_account 0
|
||||
RELOAD create_account
|
||||
CATCH api_failure flag_api_call_error 1
|
||||
CATCH account_creation_failed flag_account_creation_failed 1
|
||||
MOUT exit 0
|
||||
HALT
|
||||
|
Loading…
Reference in New Issue
Block a user