diff --git a/models/account_response.go b/models/account_response.go deleted file mode 100644 index dc8e758..0000000 --- a/models/account_response.go +++ /dev/null @@ -1,6 +0,0 @@ -package models - -type AccountResult struct { - PublicKey string `json:"publicKey"` - TrackingId string `json:"trackingId"` -} diff --git a/models/balance_response.go b/models/balance_response.go deleted file mode 100644 index 88e9ce9..0000000 --- a/models/balance_response.go +++ /dev/null @@ -1,8 +0,0 @@ -package models - -import "encoding/json" - -type BalanceResult struct { - Balance string `json:"balance"` - Nonce json.Number `json:"nonce"` -} diff --git a/models/profile.go b/models/profile.go deleted file mode 100644 index d698318..0000000 --- a/models/profile.go +++ /dev/null @@ -1,18 +0,0 @@ -package models - -type Profile struct { - ProfileItems []string - Max int -} - -func (p *Profile) InsertOrShift(index int, value string) { - if index < len(p.ProfileItems) { - p.ProfileItems = append(p.ProfileItems[:index], value) - } else { - for len(p.ProfileItems) < index { - p.ProfileItems = append(p.ProfileItems, "0") - } - p.ProfileItems = append(p.ProfileItems, "0") - p.ProfileItems[index] = value - } -} diff --git a/models/token_transfer_response.go b/models/token_transfer_response.go deleted file mode 100644 index b4d6dc3..0000000 --- a/models/token_transfer_response.go +++ /dev/null @@ -1,5 +0,0 @@ -package models - -type TokenTransferResponse struct { - TrackingId string `json:"trackingId"` -} diff --git a/models/track_status_response.go b/models/track_status_response.go deleted file mode 100644 index 0c3c230..0000000 --- a/models/track_status_response.go +++ /dev/null @@ -1,18 +0,0 @@ -package models - -import ( - "encoding/json" - "time" -) - -type Transaction struct { - CreatedAt time.Time `json:"createdAt"` - Status string `json:"status"` - TransferValue json.Number `json:"transferValue"` - TxHash string `json:"txHash"` - TxType string `json:"txType"` -} - -type TrackStatusResult struct { - Active bool `json:"active"` -} diff --git a/models/voucher_data_result.go b/models/voucher_data_result.go deleted file mode 100644 index 9a10831..0000000 --- a/models/voucher_data_result.go +++ /dev/null @@ -1,10 +0,0 @@ -package models - -type VoucherDataResult struct { - TokenName string `json:"tokenName"` - TokenSymbol string `json:"tokenSymbol"` - TokenDecimals int `json:"tokenDecimals"` - SinkAddress string `json:"sinkAddress"` - TokenCommodity string `json:"tokenCommodity"` - TokenLocation string `json:"tokenLocation"` -} diff --git a/remote/account_service.go b/remote/account_service.go deleted file mode 100644 index 3fbaaf0..0000000 --- a/remote/account_service.go +++ /dev/null @@ -1,294 +0,0 @@ -package remote - -import ( - "bytes" - "context" - "encoding/json" - "errors" - "io" - "log" - "net/http" - "net/url" - - "git.grassecon.net/grassrootseconomics/visedriver/config" - "git.grassecon.net/grassrootseconomics/visedriver/models" - "github.com/grassrootseconomics/eth-custodial/pkg/api" - dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api" -) - -type AccountServiceInterface interface { - CheckBalance(ctx context.Context, publicKey string) (*models.BalanceResult, error) - CreateAccount(ctx context.Context) (*models.AccountResult, error) - TrackAccountStatus(ctx context.Context, publicKey string) (*models.TrackStatusResult, error) - FetchVouchers(ctx context.Context, publicKey string) ([]dataserviceapi.TokenHoldings, error) - FetchTransactions(ctx context.Context, publicKey string) ([]dataserviceapi.Last10TxResponse, error) - VoucherData(ctx context.Context, address string) (*models.VoucherDataResult, error) - TokenTransfer(ctx context.Context, amount, from, to, tokenAddress string) (*models.TokenTransferResponse, error) - CheckAliasAddress(ctx context.Context, alias string) (*dataserviceapi.AliasAddress, error) -} - -type AccountService struct { -} - -// Parameters: -// - trackingId: A unique identifier for the account.This should be obtained from a previous call to -// 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) TrackAccountStatus(ctx context.Context, publicKey string) (*models.TrackStatusResult, error) { - var r models.TrackStatusResult - - ep, err := url.JoinPath(config.TrackURL, publicKey) - if err != nil { - return nil, err - } - - req, err := http.NewRequest("GET", ep, nil) - if err != nil { - return nil, err - } - - _, err = doRequest(ctx, req, &r) - if err != nil { - return nil, err - } - - return &r, 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. -func (as *AccountService) CheckBalance(ctx context.Context, publicKey string) (*models.BalanceResult, error) { - var balanceResult models.BalanceResult - - ep, err := url.JoinPath(config.BalanceURL, publicKey) - if err != nil { - return nil, err - } - - req, err := http.NewRequest("GET", ep, nil) - if err != nil { - return nil, err - } - - _, err = doRequest(ctx, req, &balanceResult) - return &balanceResult, err -} - -// 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. -// - 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(ctx context.Context) (*models.AccountResult, error) { - var r models.AccountResult - // Create a new request - req, err := http.NewRequest("POST", config.CreateAccountURL, nil) - if err != nil { - return nil, err - } - _, err = doRequest(ctx, req, &r) - if err != nil { - return nil, err - } - - return &r, nil -} - -// FetchVouchers retrieves the token holdings for a given public key from the data indexer API endpoint -// Parameters: -// - publicKey: The public key associated with the account. -func (as *AccountService) FetchVouchers(ctx context.Context, publicKey string) ([]dataserviceapi.TokenHoldings, error) { - var r struct { - Holdings []dataserviceapi.TokenHoldings `json:"holdings"` - } - - ep, err := url.JoinPath(config.VoucherHoldingsURL, publicKey) - if err != nil { - return nil, err - } - - req, err := http.NewRequest("GET", ep, nil) - if err != nil { - return nil, err - } - - _, err = doRequest(ctx, req, &r) - if err != nil { - return nil, err - } - - return r.Holdings, nil -} - -// FetchTransactions retrieves the last 10 transactions for a given public key from the data indexer API endpoint -// Parameters: -// - publicKey: The public key associated with the account. -func (as *AccountService) FetchTransactions(ctx context.Context, publicKey string) ([]dataserviceapi.Last10TxResponse, error) { - var r struct { - Transfers []dataserviceapi.Last10TxResponse `json:"transfers"` - } - - ep, err := url.JoinPath(config.VoucherTransfersURL, publicKey) - if err != nil { - return nil, err - } - - req, err := http.NewRequest("GET", ep, nil) - if err != nil { - return nil, err - } - - _, err = doRequest(ctx, req, &r) - if err != nil { - return nil, err - } - - return r.Transfers, nil -} - -// VoucherData retrieves voucher metadata from the data indexer API endpoint. -// Parameters: -// - address: The voucher address. -func (as *AccountService) VoucherData(ctx context.Context, address string) (*models.VoucherDataResult, error) { - var r struct { - TokenDetails models.VoucherDataResult `json:"tokenDetails"` - } - - ep, err := url.JoinPath(config.VoucherDataURL, address) - if err != nil { - return nil, err - } - - req, err := http.NewRequest("GET", ep, nil) - if err != nil { - return nil, err - } - - _, err = doRequest(ctx, req, &r) - return &r.TokenDetails, err -} - -// TokenTransfer creates a new token transfer in the custodial system. -// Returns: -// - *models.TokenTransferResponse: A pointer to an TokenTransferResponse struct containing the trackingId. -// 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) TokenTransfer(ctx context.Context, amount, from, to, tokenAddress string) (*models.TokenTransferResponse, error) { - var r models.TokenTransferResponse - - // Create request payload - payload := map[string]string{ - "amount": amount, - "from": from, - "to": to, - "tokenAddress": tokenAddress, - } - - payloadBytes, err := json.Marshal(payload) - if err != nil { - return nil, err - } - - // Create a new request - req, err := http.NewRequest("POST", config.TokenTransferURL, bytes.NewBuffer(payloadBytes)) - if err != nil { - return nil, err - } - _, err = doRequest(ctx, req, &r) - if err != nil { - return nil, err - } - - return &r, nil -} - -// CheckAliasAddress retrieves the address of an alias from the API endpoint. -// Parameters: -// - alias: The alias of the user. -func (as *AccountService) CheckAliasAddress(ctx context.Context, alias string) (*dataserviceapi.AliasAddress, error) { - var r dataserviceapi.AliasAddress - - ep, err := url.JoinPath(config.CheckAliasURL, alias) - if err != nil { - return nil, err - } - - req, err := http.NewRequest("GET", ep, nil) - if err != nil { - return nil, err - } - - _, err = doRequest(ctx, req, &r) - return &r, err -} - -func doRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKResponse, error) { - var okResponse api.OKResponse - var errResponse api.ErrResponse - - req.Header.Set("Authorization", "Bearer "+config.BearerToken) - req.Header.Set("Content-Type", "application/json") - - logRequestDetails(req) - - resp, err := http.DefaultClient.Do(req) - if err != nil { - log.Printf("Failed to make %s request to endpoint: %s with reason: %s", req.Method, req.URL, err.Error()) - errResponse.Description = err.Error() - return nil, err - } - defer resp.Body.Close() - - log.Printf("Received response for %s: Status Code: %d | Content-Type: %s", req.URL, resp.StatusCode, resp.Header.Get("Content-Type")) - body, err := io.ReadAll(resp.Body) - if err != nil { - return nil, err - } - if resp.StatusCode >= http.StatusBadRequest { - err := json.Unmarshal([]byte(body), &errResponse) - if err != nil { - return nil, err - } - return nil, errors.New(errResponse.Description) - } - err = json.Unmarshal([]byte(body), &okResponse) - if err != nil { - return nil, err - } - if len(okResponse.Result) == 0 { - return nil, errors.New("Empty api result") - } - - v, err := json.Marshal(okResponse.Result) - if err != nil { - return nil, err - } - - err = json.Unmarshal(v, &rcpt) - return &okResponse, err -} - -func logRequestDetails(req *http.Request) { - var bodyBytes []byte - contentType := req.Header.Get("Content-Type") - if req.Body != nil { - bodyBytes, err := io.ReadAll(req.Body) - if err != nil { - log.Printf("Error reading request body: %s", err) - return - } - req.Body = io.NopCloser(bytes.NewBuffer(bodyBytes)) - } else { - bodyBytes = []byte("-") - } - - log.Printf("URL: %s | Content-Type: %s | Method: %s| Request Body: %s", req.URL, contentType, req.Method, string(bodyBytes)) -} diff --git a/testutil/mocks/servicemock.go b/testutil/mocks/servicemock.go deleted file mode 100644 index 9033376..0000000 --- a/testutil/mocks/servicemock.go +++ /dev/null @@ -1,54 +0,0 @@ -package mocks - -import ( - "context" - - "git.grassecon.net/grassrootseconomics/visedriver/models" - dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api" - "github.com/stretchr/testify/mock" -) - -// MockAccountService implements AccountServiceInterface for testing -type MockAccountService struct { - mock.Mock -} - -func (m *MockAccountService) CreateAccount(ctx context.Context) (*models.AccountResult, error) { - args := m.Called() - return args.Get(0).(*models.AccountResult), args.Error(1) -} - -func (m *MockAccountService) CheckBalance(ctx context.Context, publicKey string) (*models.BalanceResult, error) { - args := m.Called(publicKey) - return args.Get(0).(*models.BalanceResult), args.Error(1) -} - -func (m *MockAccountService) TrackAccountStatus(ctx context.Context, trackingId string) (*models.TrackStatusResult, error) { - args := m.Called(trackingId) - return args.Get(0).(*models.TrackStatusResult), args.Error(1) -} - -func (m *MockAccountService) FetchVouchers(ctx context.Context, publicKey string) ([]dataserviceapi.TokenHoldings, error) { - args := m.Called(publicKey) - return args.Get(0).([]dataserviceapi.TokenHoldings), args.Error(1) -} - -func (m *MockAccountService) FetchTransactions(ctx context.Context, publicKey string) ([]dataserviceapi.Last10TxResponse, error) { - args := m.Called(publicKey) - return args.Get(0).([]dataserviceapi.Last10TxResponse), args.Error(1) -} - -func (m *MockAccountService) VoucherData(ctx context.Context, address string) (*models.VoucherDataResult, error) { - args := m.Called(address) - return args.Get(0).(*models.VoucherDataResult), args.Error(1) -} - -func (m *MockAccountService) TokenTransfer(ctx context.Context, amount, from, to, tokenAddress string) (*models.TokenTransferResponse, error) { - args := m.Called() - return args.Get(0).(*models.TokenTransferResponse), args.Error(1) -} - -func (m *MockAccountService) CheckAliasAddress(ctx context.Context, alias string) (*dataserviceapi.AliasAddress, error) { - args := m.Called(alias) - return args.Get(0).(*dataserviceapi.AliasAddress), args.Error(1) -} diff --git a/testutil/testservice/account_service.go b/testutil/testservice/account_service.go deleted file mode 100644 index 300ef52..0000000 --- a/testutil/testservice/account_service.go +++ /dev/null @@ -1,62 +0,0 @@ -package testservice - -import ( - "context" - "encoding/json" - - "git.grassecon.net/grassrootseconomics/visedriver/models" - dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api" -) - -type TestAccountService struct { -} - -func (tas *TestAccountService) CreateAccount(ctx context.Context) (*models.AccountResult, error) { - return &models.AccountResult{ - TrackingId: "075ccc86-f6ef-4d33-97d5-e91cfb37aa0d", - PublicKey: "0x623EFAFa8868df4B934dd12a8B26CB3Dd75A7AdD", - }, nil -} - -func (tas *TestAccountService) CheckBalance(ctx context.Context, publicKey string) (*models.BalanceResult, error) { - balanceResponse := &models.BalanceResult{ - Balance: "0.003 CELO", - Nonce: json.Number("0"), - } - return balanceResponse, nil -} - -func (tas *TestAccountService) TrackAccountStatus(ctx context.Context, publicKey string) (*models.TrackStatusResult, error) { - return &models.TrackStatusResult{ - Active: true, - }, nil -} - -func (tas *TestAccountService) FetchVouchers(ctx context.Context, publicKey string) ([]dataserviceapi.TokenHoldings, error) { - return []dataserviceapi.TokenHoldings{ - dataserviceapi.TokenHoldings{ - ContractAddress: "0x6CC75A06ac72eB4Db2eE22F781F5D100d8ec03ee", - TokenSymbol: "SRF", - TokenDecimals: "6", - Balance: "2745987", - }, - }, nil -} - -func (tas *TestAccountService) FetchTransactions(ctx context.Context, publicKey string) ([]dataserviceapi.Last10TxResponse, error) { - return []dataserviceapi.Last10TxResponse{}, nil -} - -func (m TestAccountService) VoucherData(ctx context.Context, address string) (*models.VoucherDataResult, error) { - return &models.VoucherDataResult{}, nil -} - -func (tas *TestAccountService) TokenTransfer(ctx context.Context, amount, from, to, tokenAddress string) (*models.TokenTransferResponse, error) { - return &models.TokenTransferResponse{ - TrackingId: "e034d147-747d-42ea-928d-b5a7cb3426af", - }, nil -} - -func (m TestAccountService) CheckAliasAddress(ctx context.Context, alias string) (*dataserviceapi.AliasAddress, error) { - return &dataserviceapi.AliasAddress{}, nil -} diff --git a/testutil/testtag/offlinetest.go b/testutil/testtag/offlinetest.go deleted file mode 100644 index cfaa5e6..0000000 --- a/testutil/testtag/offlinetest.go +++ /dev/null @@ -1,12 +0,0 @@ -// +build !online - -package testtag - -import ( - "git.grassecon.net/grassrootseconomics/visedriver/remote" - accountservice "git.grassecon.net/grassrootseconomics/visedriver/testutil/testservice" -) - -var ( - AccountService remote.AccountServiceInterface = &accountservice.TestAccountService{} -) diff --git a/testutil/testtag/onlinetest.go b/testutil/testtag/onlinetest.go deleted file mode 100644 index 46a0f88..0000000 --- a/testutil/testtag/onlinetest.go +++ /dev/null @@ -1,9 +0,0 @@ -// +build online - -package testtag - -import "git.grassecon.net/grassrootseconomics/visedriver/remote" - -var ( - AccountService remote.AccountServiceInterface -)