forked from urdt/ussd
Add voucherdata endpoint
This commit is contained in:
parent
a237b615f2
commit
0506a8c452
@ -2,6 +2,8 @@ package common
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
|
||||
"git.defalsify.org/vise.git/logging"
|
||||
)
|
||||
|
||||
type DataTyp uint16
|
||||
@ -33,6 +35,10 @@ const (
|
||||
DATA_TRANSACTIONS
|
||||
)
|
||||
|
||||
var (
|
||||
logg = logging.NewVanilla().WithDomain("urdt-common")
|
||||
)
|
||||
|
||||
func typToBytes(typ DataTyp) []byte {
|
||||
var b [2]byte
|
||||
binary.BigEndian.PutUint16(b[:], uint16(typ))
|
||||
|
@ -84,6 +84,7 @@ func MatchVoucher(input, symbols, balances, decimals, addresses string) (symbol,
|
||||
decList := strings.Split(decimals, "\n")
|
||||
addrList := strings.Split(addresses, "\n")
|
||||
|
||||
logg.Tracef("found" , "symlist", symList, "syms", symbols, "input", input)
|
||||
for i, sym := range symList {
|
||||
parts := strings.SplitN(sym, ":", 2)
|
||||
|
||||
@ -136,6 +137,7 @@ func GetTemporaryVoucherData(ctx context.Context, store DataStore, sessionId str
|
||||
|
||||
// UpdateVoucherData sets the active voucher data and clears the temporary voucher data in the DataStore.
|
||||
func UpdateVoucherData(ctx context.Context, store DataStore, sessionId string, data *dataserviceapi.TokenHoldings) error {
|
||||
logg.TraceCtxf(ctx, "dtal", "data", data)
|
||||
// Active voucher data entries
|
||||
activeEntries := map[DataTyp][]byte{
|
||||
DATA_ACTIVE_SYM: []byte(data.TokenSymbol),
|
||||
|
@ -13,6 +13,7 @@ const (
|
||||
trackPath = "/api/v2/account/status"
|
||||
voucherHoldingsPathPrefix = "/api/v1/holdings"
|
||||
voucherTransfersPathPrefix = "/api/v1/transfers/last10"
|
||||
voucherDataPathPrefix = "/api/v1/token"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -29,6 +30,7 @@ var (
|
||||
TrackURL string
|
||||
VoucherHoldingsURL string
|
||||
VoucherTransfersURL string
|
||||
VoucherDataURL string
|
||||
)
|
||||
|
||||
func setBase() error {
|
||||
@ -62,6 +64,7 @@ func LoadConfig() error {
|
||||
TrackURL, _ = url.JoinPath(custodialURLBase, trackPath)
|
||||
VoucherHoldingsURL, _ = url.JoinPath(dataURLBase, voucherHoldingsPathPrefix)
|
||||
VoucherTransfersURL, _ = url.JoinPath(dataURLBase, voucherTransfersPathPrefix)
|
||||
VoucherDataURL, _ = url.JoinPath(dataURLBase, voucherDataPathPrefix)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -2,13 +2,20 @@ 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"`
|
||||
}
|
||||
//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"`
|
||||
}
|
||||
|
||||
type VoucherDataResult struct {
|
||||
TokenName string `json:"tokenName"`
|
||||
TokenSymbol string `json:"tokenSymbol"`
|
||||
TokenDecimals string `json:"tokenDecimals"`
|
||||
SinkAddress string `json:"sinkAddress"`
|
||||
}
|
||||
|
@ -23,6 +23,7 @@ type AccountServiceInterface interface {
|
||||
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)
|
||||
}
|
||||
|
||||
type AccountService struct {
|
||||
@ -151,6 +152,26 @@ func (as *AccountService) FetchTransactions(ctx context.Context, publicKey strin
|
||||
}
|
||||
|
||||
|
||||
// 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 voucherDataResult models.VoucherDataResult
|
||||
|
||||
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 = doCustodialRequest(ctx, req, &voucherDataResult)
|
||||
return &voucherDataResult, err
|
||||
}
|
||||
|
||||
func doRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKResponse, error) {
|
||||
var okResponse api.OKResponse
|
||||
var errResponse api.ErrResponse
|
||||
|
Loading…
Reference in New Issue
Block a user