forked from urdt/ussd
WIP refactor models usage
This commit is contained in:
parent
4e81e2d869
commit
1e638238ed
@ -1,18 +1,67 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import "git.grassecon.net/urdt/ussd/initializers"
|
import (
|
||||||
|
"net/url"
|
||||||
|
|
||||||
|
"git.grassecon.net/urdt/ussd/initializers"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
createAccountPath = "/api/v2/account/create"
|
||||||
|
trackStatusPath = "/api/track"
|
||||||
|
balancePathPrefix = "/api/account"
|
||||||
|
trackPath = "/api/v2/account/status"
|
||||||
|
voucherHoldingsPathPrefix = "/api/v1/holdings"
|
||||||
|
voucherTransfersPathPrefix = "/api/v1/transfers/last10"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
custodialURLBase string
|
||||||
|
dataURLBase string
|
||||||
|
CustodialAPIKey string
|
||||||
|
DataAPIKey string
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
CreateAccountURL string
|
CreateAccountURL string
|
||||||
TrackStatusURL string
|
TrackStatusURL string
|
||||||
BalanceURL string
|
BalanceURL string
|
||||||
TrackURL string
|
TrackURL string
|
||||||
|
VoucherHoldingsURL string
|
||||||
|
VoucherTransfersURL string
|
||||||
)
|
)
|
||||||
|
|
||||||
// LoadConfig initializes the configuration values after environment variables are loaded.
|
func setBase() error {
|
||||||
func LoadConfig() {
|
var err error
|
||||||
CreateAccountURL = initializers.GetEnv("CREATE_ACCOUNT_URL", "http://localhost:5003/api/v2/account/create")
|
|
||||||
TrackStatusURL = initializers.GetEnv("TRACK_STATUS_URL", "https://custodial.sarafu.africa/api/track/")
|
custodialURLBase = initializers.GetEnv("CUSTODIAL_URL_BASE", "http://localhost:5003")
|
||||||
BalanceURL = initializers.GetEnv("BALANCE_URL", "https://custodial.sarafu.africa/api/account/status/")
|
dataURLBase = initializers.GetEnv("DATA_URL_BASE", "http://localhost:5006")
|
||||||
TrackURL = initializers.GetEnv("TRACK_URL", "http://localhost:5003/api/v2/account/status")
|
CustodialAPIKey = initializers.GetEnv("CUSTODIAL_API_KEY", "xd")
|
||||||
|
DataAPIKey = initializers.GetEnv("DATA_API_KEY", "xd")
|
||||||
|
|
||||||
|
_, err = url.JoinPath(custodialURLBase, "/foo")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = url.JoinPath(dataURLBase, "/bar")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// LoadConfig initializes the configuration values after environment variables are loaded.
|
||||||
|
func LoadConfig() error {
|
||||||
|
err := setBase()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
CreateAccountURL, _ = url.JoinPath(custodialURLBase, createAccountPath)
|
||||||
|
TrackStatusURL, _ = url.JoinPath(custodialURLBase, trackStatusPath)
|
||||||
|
BalanceURL, _ = url.JoinPath(custodialURLBase, balancePathPrefix)
|
||||||
|
TrackURL, _ = url.JoinPath(custodialURLBase, trackPath)
|
||||||
|
VoucherHoldingsURL, _ = url.JoinPath(dataURLBase, voucherHoldingsPathPrefix)
|
||||||
|
VoucherTransfersURL, _ = url.JoinPath(dataURLBase, voucherTransfersPathPrefix)
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -149,12 +149,12 @@ 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 {
|
func (h *Handlers) createAccountNoExist(ctx context.Context, sessionId string, res *resource.Result) error {
|
||||||
flag_account_created, _ := h.flagManager.GetFlag("flag_account_created")
|
flag_account_created, _ := h.flagManager.GetFlag("flag_account_created")
|
||||||
okResponse, err := h.accountService.CreateAccount(ctx)
|
r, err := h.accountService.CreateAccount(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
trackingId := okResponse.Result["trackingId"].(string)
|
trackingId := r.TrackingId
|
||||||
publicKey := okResponse.Result["publicKey"].(string)
|
publicKey := r.PublicKey
|
||||||
|
|
||||||
data := map[common.DataTyp]string{
|
data := map[common.DataTyp]string{
|
||||||
common.DATA_TRACKING_ID: trackingId,
|
common.DATA_TRACKING_ID: trackingId,
|
||||||
@ -682,6 +682,7 @@ func (h *Handlers) CheckBalance(ctx context.Context, sym string, input []byte) (
|
|||||||
|
|
||||||
func (h *Handlers) FetchCustodialBalances(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
func (h *Handlers) FetchCustodialBalances(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||||
var res resource.Result
|
var res resource.Result
|
||||||
|
|
||||||
flag_api_error, _ := h.flagManager.GetFlag("flag_api_call_error")
|
flag_api_error, _ := h.flagManager.GetFlag("flag_api_call_error")
|
||||||
|
|
||||||
sessionId, ok := ctx.Value("SessionId").(string)
|
sessionId, ok := ctx.Value("SessionId").(string)
|
||||||
@ -699,14 +700,13 @@ func (h *Handlers) FetchCustodialBalances(ctx context.Context, sym string, input
|
|||||||
|
|
||||||
balanceResponse, err := h.accountService.CheckBalance(ctx, string(publicKey))
|
balanceResponse, err := h.accountService.CheckBalance(ctx, string(publicKey))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, nil
|
|
||||||
}
|
|
||||||
if !balanceResponse.Ok {
|
|
||||||
res.FlagSet = append(res.FlagSet, flag_api_error)
|
res.FlagSet = append(res.FlagSet, flag_api_error)
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
res.FlagReset = append(res.FlagReset, flag_api_error)
|
res.FlagReset = append(res.FlagReset, flag_api_error)
|
||||||
balance := balanceResponse.Result.Balance
|
|
||||||
|
//balance := balanceResponse.Result.Balance
|
||||||
|
balance := balanceResponse.Balance
|
||||||
|
|
||||||
switch balanceType {
|
switch balanceType {
|
||||||
case "my":
|
case "my":
|
||||||
@ -1066,13 +1066,13 @@ func (h *Handlers) SetDefaultVoucher(ctx context.Context, sym string, input []by
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Return if there is no voucher
|
// Return if there is no voucher
|
||||||
if len(vouchersResp.Result.Holdings) == 0 {
|
if len(vouchersResp) == 0 {
|
||||||
res.FlagSet = append(res.FlagSet, flag_no_active_voucher)
|
res.FlagSet = append(res.FlagSet, flag_no_active_voucher)
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use only the first voucher
|
// Use only the first voucher
|
||||||
firstVoucher := vouchersResp.Result.Holdings[0]
|
firstVoucher := vouchersResp[0]
|
||||||
defaultSym := firstVoucher.TokenSymbol
|
defaultSym := firstVoucher.TokenSymbol
|
||||||
defaultBal := firstVoucher.Balance
|
defaultBal := firstVoucher.Balance
|
||||||
|
|
||||||
@ -1119,7 +1119,7 @@ func (h *Handlers) CheckVouchers(ctx context.Context, sym string, input []byte)
|
|||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
data := utils.ProcessVouchers(vouchersResp.Result.Holdings)
|
data := utils.ProcessVouchers(vouchersResp)
|
||||||
|
|
||||||
// Store all voucher data
|
// Store all voucher data
|
||||||
dataMap := map[string]string{
|
dataMap := map[string]string{
|
||||||
|
@ -1159,7 +1159,6 @@ func TestCheckAccountStatus(t *testing.T) {
|
|||||||
|
|
||||||
// Assert that expectations were met
|
// Assert that expectations were met
|
||||||
mockDataStore.AssertExpectations(t)
|
mockDataStore.AssertExpectations(t)
|
||||||
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,10 +1,6 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
type AccountResponse struct {
|
type AccountResult struct {
|
||||||
Ok bool `json:"ok"`
|
|
||||||
Description string `json:"description"` // Include the description field
|
|
||||||
Result struct {
|
|
||||||
PublicKey string `json:"publicKey"`
|
PublicKey string `json:"publicKey"`
|
||||||
TrackingId string `json:"trackingId"`
|
TrackingId string `json:"trackingId"`
|
||||||
} `json:"result"`
|
|
||||||
}
|
}
|
||||||
|
@ -3,10 +3,15 @@ package models
|
|||||||
import "encoding/json"
|
import "encoding/json"
|
||||||
|
|
||||||
|
|
||||||
type BalanceResponse struct {
|
//type BalanceResponse struct {
|
||||||
Ok bool `json:"ok"`
|
// Ok bool `json:"ok"`
|
||||||
Result struct {
|
// Result struct {
|
||||||
|
// Balance string `json:"balance"`
|
||||||
|
// Nonce json.Number `json:"nonce"`
|
||||||
|
// } `json:"result"`
|
||||||
|
//}
|
||||||
|
//
|
||||||
|
type BalanceResult struct {
|
||||||
Balance string `json:"balance"`
|
Balance string `json:"balance"`
|
||||||
Nonce json.Number `json:"nonce"`
|
Nonce json.Number `json:"nonce"`
|
||||||
} `json:"result"`
|
|
||||||
}
|
}
|
||||||
|
@ -4,23 +4,18 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
type Transaction struct {
|
//type Transaction struct {
|
||||||
CreatedAt time.Time `json:"createdAt"`
|
// CreatedAt time.Time `json:"createdAt"`
|
||||||
Status string `json:"status"`
|
// Status string `json:"status"`
|
||||||
TransferValue json.Number `json:"transferValue"`
|
// TransferValue json.Number `json:"transferValue"`
|
||||||
TxHash string `json:"txHash"`
|
// TxHash string `json:"txHash"`
|
||||||
TxType string `json:"txType"`
|
// TxType string `json:"txType"`
|
||||||
}
|
//}
|
||||||
|
|
||||||
type TrackStatusResponse struct {
|
type TrackStatusResult struct {
|
||||||
Ok bool `json:"ok"`
|
|
||||||
Result struct {
|
|
||||||
Transaction struct {
|
|
||||||
CreatedAt time.Time `json:"createdAt"`
|
CreatedAt time.Time `json:"createdAt"`
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
TransferValue json.Number `json:"transferValue"`
|
TransferValue json.Number `json:"transferValue"`
|
||||||
TxHash string `json:"txHash"`
|
TxHash string `json:"txHash"`
|
||||||
TxType string `json:"txType"`
|
TxType string `json:"txType"`
|
||||||
}
|
}
|
||||||
} `json:"result"`
|
|
||||||
}
|
|
||||||
|
@ -1,14 +1 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
|
||||||
|
|
||||||
type VoucherHoldingResponse struct {
|
|
||||||
Ok bool `json:"ok"`
|
|
||||||
Description string `json:"description"`
|
|
||||||
Result VoucherResult `json:"result"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// VoucherResult holds the list of token holdings
|
|
||||||
type VoucherResult struct {
|
|
||||||
Holdings []dataserviceapi.TokenHoldings `json:"holdings"`
|
|
||||||
}
|
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
|
|
||||||
"git.grassecon.net/urdt/ussd/internal/models"
|
"git.grassecon.net/urdt/ussd/internal/models"
|
||||||
"github.com/grassrootseconomics/eth-custodial/pkg/api"
|
"github.com/grassrootseconomics/eth-custodial/pkg/api"
|
||||||
|
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
||||||
"github.com/stretchr/testify/mock"
|
"github.com/stretchr/testify/mock"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -13,28 +14,28 @@ type MockAccountService struct {
|
|||||||
mock.Mock
|
mock.Mock
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockAccountService) CreateAccount(ctx context.Context) (*api.OKResponse, error) {
|
func (m *MockAccountService) CreateAccount(ctx context.Context) (*models.AccountResult, error) {
|
||||||
args := m.Called()
|
args := m.Called()
|
||||||
return args.Get(0).(*api.OKResponse), args.Error(1)
|
return args.Get(0).(*models.AccountResult), args.Error(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockAccountService) CheckBalance(ctx context.Context, publicKey string) (*models.BalanceResponse, error) {
|
func (m *MockAccountService) CheckBalance(ctx context.Context, publicKey string) (*models.BalanceResult, error) {
|
||||||
args := m.Called(publicKey)
|
args := m.Called(publicKey)
|
||||||
return args.Get(0).(*models.BalanceResponse), args.Error(1)
|
return args.Get(0).(*models.BalanceResult), args.Error(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockAccountService) CheckAccountStatus(ctx context.Context, trackingId string) (*models.TrackStatusResponse, error) {
|
func (m *MockAccountService) TrackAccountStatus(ctx context.Context, trackingId string) (*api.OKResponse, error) {
|
||||||
args := m.Called(trackingId)
|
args := m.Called(trackingId)
|
||||||
return args.Get(0).(*models.TrackStatusResponse), args.Error(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockAccountService) TrackAccountStatus(ctx context.Context,publicKey string) (*api.OKResponse, error) {
|
|
||||||
args := m.Called(publicKey)
|
|
||||||
return args.Get(0).(*api.OKResponse), args.Error(1)
|
return args.Get(0).(*api.OKResponse), args.Error(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *MockAccountService) CheckAccountStatus(ctx context.Context,publicKey string) (*models.TrackStatusResult, error) {
|
||||||
func (m *MockAccountService) FetchVouchers(ctx context.Context, publicKey string) (*models.VoucherHoldingResponse, error) {
|
|
||||||
args := m.Called(publicKey)
|
args := m.Called(publicKey)
|
||||||
return args.Get(0).(*models.VoucherHoldingResponse), args.Error(1)
|
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)
|
||||||
}
|
}
|
||||||
|
@ -13,54 +13,29 @@ import (
|
|||||||
type TestAccountService struct {
|
type TestAccountService struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tas *TestAccountService) CreateAccount(ctx context.Context) (*api.OKResponse, error) {
|
func (tas *TestAccountService) CreateAccount(ctx context.Context) (*models.AccountResult, error) {
|
||||||
return &api.OKResponse{
|
return &models.AccountResult {
|
||||||
Ok: true,
|
TrackingId: "075ccc86-f6ef-4d33-97d5-e91cfb37aa0d",
|
||||||
Description: "Account creation succeeded",
|
PublicKey: "0x623EFAFa8868df4B934dd12a8B26CB3Dd75A7AdD",
|
||||||
Result: map[string]any{
|
|
||||||
"trackingId": "075ccc86-f6ef-4d33-97d5-e91cfb37aa0d",
|
|
||||||
"publicKey": "0x623EFAFa8868df4B934dd12a8B26CB3Dd75A7AdD",
|
|
||||||
},
|
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tas *TestAccountService) CheckBalance(ctx context.Context, publicKey string) (*models.BalanceResponse, error) {
|
func (tas *TestAccountService) CheckBalance(ctx context.Context, publicKey string) (*models.BalanceResult, error) {
|
||||||
balanceResponse := &models.BalanceResponse{
|
balanceResponse := &models.BalanceResult {
|
||||||
Ok: true,
|
|
||||||
Result: struct {
|
|
||||||
Balance string `json:"balance"`
|
|
||||||
Nonce json.Number `json:"nonce"`
|
|
||||||
}{
|
|
||||||
Balance: "0.003 CELO",
|
Balance: "0.003 CELO",
|
||||||
Nonce: json.Number("0"),
|
Nonce: json.Number("0"),
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return balanceResponse, nil
|
return balanceResponse, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tas *TestAccountService) CheckAccountStatus(ctx context.Context, trackingId string) (*models.TrackStatusResponse, error) {
|
func (tas *TestAccountService) CheckAccountStatus(ctx context.Context, trackingId string) (*models.TrackStatusResult, error) {
|
||||||
trackResponse := &models.TrackStatusResponse{
|
return &models.TrackStatusResult {
|
||||||
Ok: true,
|
|
||||||
Result: struct {
|
|
||||||
Transaction struct {
|
|
||||||
CreatedAt time.Time "json:\"createdAt\""
|
|
||||||
Status string "json:\"status\""
|
|
||||||
TransferValue json.Number "json:\"transferValue\""
|
|
||||||
TxHash string "json:\"txHash\""
|
|
||||||
TxType string "json:\"txType\""
|
|
||||||
}
|
|
||||||
}{
|
|
||||||
Transaction: models.Transaction{
|
|
||||||
CreatedAt: time.Now(),
|
CreatedAt: time.Now(),
|
||||||
Status: "SUCCESS",
|
Status: "SUCCESS",
|
||||||
TransferValue: json.Number("0.5"),
|
TransferValue: json.Number("0.5"),
|
||||||
TxHash: "0x123abc456def",
|
TxHash: "0x123abc456def",
|
||||||
TxType: "transfer",
|
TxType: "transfer",
|
||||||
},
|
}, nil
|
||||||
},
|
|
||||||
}
|
|
||||||
return trackResponse, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tas *TestAccountService) TrackAccountStatus(ctx context.Context, publicKey string) (*api.OKResponse, error) {
|
func (tas *TestAccountService) TrackAccountStatus(ctx context.Context, publicKey string) (*api.OKResponse, error) {
|
||||||
@ -73,18 +48,13 @@ func (tas *TestAccountService) TrackAccountStatus(ctx context.Context, publicKey
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tas *TestAccountService) FetchVouchers(ctx context.Context, publicKey string) (*models.VoucherHoldingResponse, error) {
|
func (tas *TestAccountService) FetchVouchers(ctx context.Context, publicKey string) ([]dataserviceapi.TokenHoldings, error) {
|
||||||
return &models.VoucherHoldingResponse{
|
return []dataserviceapi.TokenHoldings {
|
||||||
Ok: true,
|
dataserviceapi.TokenHoldings {
|
||||||
Result: models.VoucherResult{
|
|
||||||
Holdings: []dataserviceapi.TokenHoldings{
|
|
||||||
{
|
|
||||||
ContractAddress: "0x6CC75A06ac72eB4Db2eE22F781F5D100d8ec03ee",
|
ContractAddress: "0x6CC75A06ac72eB4Db2eE22F781F5D100d8ec03ee",
|
||||||
TokenSymbol: "SRF",
|
TokenSymbol: "SRF",
|
||||||
TokenDecimals: "6",
|
TokenDecimals: "6",
|
||||||
Balance: "2745987",
|
Balance: "2745987",
|
||||||
},
|
},
|
||||||
},
|
|
||||||
},
|
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
@ -4,27 +4,29 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"net/url"
|
||||||
|
|
||||||
|
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
||||||
|
"github.com/grassrootseconomics/eth-custodial/pkg/api"
|
||||||
"git.grassecon.net/urdt/ussd/config"
|
"git.grassecon.net/urdt/ussd/config"
|
||||||
"git.grassecon.net/urdt/ussd/internal/models"
|
"git.grassecon.net/urdt/ussd/internal/models"
|
||||||
"github.com/grassrootseconomics/eth-custodial/pkg/api"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
okResponse api.OKResponse
|
|
||||||
errResponse api.ErrResponse
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type AccountServiceInterface interface {
|
type AccountServiceInterface interface {
|
||||||
CheckBalance(ctx context.Context, publicKey string) (*models.BalanceResponse, error)
|
//CheckBalance(ctx context.Context, publicKey string) (*models.BalanceResponse, error)
|
||||||
CreateAccount(ctx context.Context) (*api.OKResponse, error)
|
CheckBalance(ctx context.Context, publicKey string) (*models.BalanceResult, error)
|
||||||
CheckAccountStatus(ctx context.Context, trackingId string) (*models.TrackStatusResponse, error)
|
CreateAccount(ctx context.Context) (*models.AccountResult, error)
|
||||||
|
//CheckAccountStatus(ctx context.Context, trackingId string) (*models.TrackStatusResponse, error)
|
||||||
|
// TODO: poorly named method seems to be checking transaction
|
||||||
|
CheckAccountStatus(ctx context.Context, trackingId string) (*models.TrackStatusResult, error)
|
||||||
TrackAccountStatus(ctx context.Context, publicKey string) (*api.OKResponse, error)
|
TrackAccountStatus(ctx context.Context, publicKey string) (*api.OKResponse, error)
|
||||||
FetchVouchers(ctx context.Context, publicKey string) (*models.VoucherHoldingResponse, error)
|
FetchVouchers(ctx context.Context, publicKey string) ([]dataserviceapi.TokenHoldings, error)
|
||||||
|
//FetchVouchers(ctx context.Context, publicKey string) (*api.OKResponse, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type AccountService struct {
|
type AccountService struct {
|
||||||
@ -39,32 +41,38 @@ type AccountService struct {
|
|||||||
// - 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.
|
// - 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.
|
// - 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
|
// If no error occurs, this will be nil
|
||||||
func (as *AccountService) CheckAccountStatus(ctx context.Context, trackingId string) (*models.TrackStatusResponse, error) {
|
func (as *AccountService) CheckAccountStatus(ctx context.Context, trackingId string) (*models.TrackStatusResult, error) {
|
||||||
resp, err := http.Get(config.BalanceURL + trackingId)
|
var r models.TrackStatusResult
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
body, err := io.ReadAll(resp.Body)
|
ep, err := url.JoinPath(config.TrackURL, trackingId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var trackResp models.TrackStatusResponse
|
req, err := http.NewRequest("GET", ep, nil)
|
||||||
err = json.Unmarshal(body, &trackResp)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &trackResp, nil
|
|
||||||
|
|
||||||
|
_, err = doCustodialRequest(ctx, req, &r)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &r, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (as *AccountService) TrackAccountStatus(ctx context.Context, publicKey string) (*api.OKResponse, error) {
|
func (as *AccountService) TrackAccountStatus(ctx context.Context, publicKey string) (*api.OKResponse, error) {
|
||||||
|
var okResponse api.OKResponse
|
||||||
|
var errResponse api.ErrResponse
|
||||||
var err error
|
var err error
|
||||||
// Construct the URL with the path parameter
|
|
||||||
url := fmt.Sprintf("%s/%s", config.TrackURL, publicKey)
|
ep, err := url.JoinPath(config.TrackURL, publicKey)
|
||||||
req, err := http.NewRequest("GET", url, nil)
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
req, err := http.NewRequest("GET", ep, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -104,41 +112,83 @@ func (as *AccountService) TrackAccountStatus(ctx context.Context, publicKey stri
|
|||||||
// CheckBalance retrieves the balance for a given public key from the custodial balance API endpoint.
|
// CheckBalance retrieves the balance for a given public key from the custodial balance API endpoint.
|
||||||
// Parameters:
|
// Parameters:
|
||||||
// - publicKey: The public key associated with the account whose balance needs to be checked.
|
// - publicKey: The public key associated with the account whose balance needs to be checked.
|
||||||
func (as *AccountService) CheckBalance(ctx context.Context, publicKey string) (*models.BalanceResponse, error) {
|
func (as *AccountService) CheckBalance(ctx context.Context, publicKey string) (*models.BalanceResult, error) {
|
||||||
resp, err := http.Get(config.BalanceURL + publicKey)
|
var balanceResult models.BalanceResult
|
||||||
|
|
||||||
|
ep, err := url.JoinPath(config.BalanceURL, publicKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
|
||||||
body, err := io.ReadAll(resp.Body)
|
req, err := http.NewRequest("GET", ep, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
var balanceResp models.BalanceResponse
|
|
||||||
err = json.Unmarshal(body, &balanceResp)
|
_, err = doCustodialRequest(ctx, req, &balanceResult)
|
||||||
if err != nil {
|
return &balanceResult, err
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &balanceResp, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// CreateAccount creates a new account in the custodial system.
|
// CreateAccount creates a new account in the custodial system.
|
||||||
// Returns:
|
// Returns:
|
||||||
// - *models.AccountResponse: A pointer to an AccountResponse struct containing the details of the created account.
|
// - *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.
|
// 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.
|
// - 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.
|
// If no error occurs, this will be nil.
|
||||||
func (as *AccountService) CreateAccount(ctx context.Context) (*api.OKResponse, error) {
|
func (as *AccountService) CreateAccount(ctx context.Context) (*models.AccountResult, error) {
|
||||||
var err error
|
var r models.AccountResult
|
||||||
|
|
||||||
// Create a new request
|
// Create a new request
|
||||||
req, err := http.NewRequest("POST", config.CreateAccountURL, nil)
|
req, err := http.NewRequest("POST", config.CreateAccountURL, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
req.Header.Set("Content-Type", "application/json")
|
|
||||||
req.Header.Set("X-GE-KEY", "xd")
|
|
||||||
|
|
||||||
|
_, err = doCustodialRequest(ctx, req, &r)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &r, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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(ctx context.Context, publicKey string) ([]dataserviceapi.TokenHoldings, error) {
|
||||||
|
var r []dataserviceapi.TokenHoldings
|
||||||
|
|
||||||
|
req, err := http.NewRequest("GET", config.VoucherHoldingsURL, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = doDataRequest(ctx, req, r)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return r, nil
|
||||||
|
|
||||||
|
//
|
||||||
|
// file, err := os.Open("sample_tokens.json")
|
||||||
|
// if err != nil {
|
||||||
|
// return nil, err
|
||||||
|
// }
|
||||||
|
// defer file.Close()
|
||||||
|
// var holdings models.VoucherHoldingResponse
|
||||||
|
//
|
||||||
|
// if err := json.NewDecoder(file).Decode(&holdings); err != nil {
|
||||||
|
// return nil, err
|
||||||
|
// }
|
||||||
|
// return &holdings, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func doRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKResponse, error) {
|
||||||
|
var okResponse api.OKResponse
|
||||||
|
var errResponse api.ErrResponse
|
||||||
|
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
resp, err := http.DefaultClient.Do(req)
|
resp, err := http.DefaultClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errResponse.Description = err.Error()
|
errResponse.Description = err.Error()
|
||||||
@ -165,21 +215,22 @@ func (as *AccountService) CreateAccount(ctx context.Context) (*api.OKResponse, e
|
|||||||
return nil, errors.New("Empty api result")
|
return nil, errors.New("Empty api result")
|
||||||
}
|
}
|
||||||
return &okResponse, nil
|
return &okResponse, nil
|
||||||
}
|
|
||||||
|
|
||||||
// FetchVouchers retrieves the token holdings for a given public key from the custodial holdings API endpoint
|
v, err := json.Marshal(okResponse.Result)
|
||||||
// Parameters:
|
|
||||||
// - publicKey: The public key associated with the account.
|
|
||||||
func (as *AccountService) FetchVouchers(ctx context.Context, publicKey string) (*models.VoucherHoldingResponse, error) {
|
|
||||||
file, err := os.Open("sample_tokens.json")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer file.Close()
|
|
||||||
var holdings models.VoucherHoldingResponse
|
|
||||||
|
|
||||||
if err := json.NewDecoder(file).Decode(&holdings); err != nil {
|
err = json.Unmarshal(v, &rcpt)
|
||||||
return nil, err
|
return &okResponse, err
|
||||||
}
|
}
|
||||||
return &holdings, nil
|
|
||||||
|
func doCustodialRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKResponse, error) {
|
||||||
|
req.Header.Set("X-GE-KEY", config.CustodialAPIKey)
|
||||||
|
return doRequest(ctx, req, rcpt)
|
||||||
|
}
|
||||||
|
|
||||||
|
func doDataRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKResponse, error) {
|
||||||
|
req.Header.Set("X-GE-KEY", config.DataAPIKey)
|
||||||
|
return doRequest(ctx, req, rcpt)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user