Treasury bot + ledger fix
This commit is contained in:
287
api/gateway/tgsettle/internal/service/treasury/ledger/client.go
Normal file
287
api/gateway/tgsettle/internal/service/treasury/ledger/client.go
Normal file
@@ -0,0 +1,287 @@
|
||||
package ledger
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/tech/sendico/pkg/discovery"
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
moneyv1 "github.com/tech/sendico/pkg/proto/common/money/v1"
|
||||
connectorv1 "github.com/tech/sendico/pkg/proto/connector/v1"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
"google.golang.org/protobuf/types/known/structpb"
|
||||
)
|
||||
|
||||
const ledgerConnectorID = "ledger"
|
||||
|
||||
type Config struct {
|
||||
Endpoint string
|
||||
Timeout time.Duration
|
||||
Insecure bool
|
||||
}
|
||||
|
||||
type Account struct {
|
||||
AccountID string
|
||||
Currency string
|
||||
OrganizationRef string
|
||||
}
|
||||
|
||||
type Balance struct {
|
||||
AccountID string
|
||||
Amount string
|
||||
Currency string
|
||||
}
|
||||
|
||||
type PostRequest struct {
|
||||
AccountID string
|
||||
OrganizationRef string
|
||||
Amount string
|
||||
Currency string
|
||||
Reference string
|
||||
IdempotencyKey string
|
||||
}
|
||||
|
||||
type OperationResult struct {
|
||||
Reference string
|
||||
}
|
||||
|
||||
type Client interface {
|
||||
GetAccount(ctx context.Context, accountID string) (*Account, error)
|
||||
GetBalance(ctx context.Context, accountID string) (*Balance, error)
|
||||
ExternalCredit(ctx context.Context, req PostRequest) (*OperationResult, error)
|
||||
ExternalDebit(ctx context.Context, req PostRequest) (*OperationResult, error)
|
||||
Close() error
|
||||
}
|
||||
|
||||
type grpcConnectorClient interface {
|
||||
GetAccount(ctx context.Context, in *connectorv1.GetAccountRequest, opts ...grpc.CallOption) (*connectorv1.GetAccountResponse, error)
|
||||
GetBalance(ctx context.Context, in *connectorv1.GetBalanceRequest, opts ...grpc.CallOption) (*connectorv1.GetBalanceResponse, error)
|
||||
SubmitOperation(ctx context.Context, in *connectorv1.SubmitOperationRequest, opts ...grpc.CallOption) (*connectorv1.SubmitOperationResponse, error)
|
||||
}
|
||||
|
||||
type connectorClient struct {
|
||||
cfg Config
|
||||
conn *grpc.ClientConn
|
||||
client grpcConnectorClient
|
||||
}
|
||||
|
||||
func New(cfg Config) (Client, error) {
|
||||
cfg.Endpoint = strings.TrimSpace(cfg.Endpoint)
|
||||
if cfg.Endpoint == "" {
|
||||
return nil, merrors.InvalidArgument("ledger endpoint is required", "ledger.endpoint")
|
||||
}
|
||||
if normalized, insecure := normalizeEndpoint(cfg.Endpoint); normalized != "" {
|
||||
cfg.Endpoint = normalized
|
||||
if insecure {
|
||||
cfg.Insecure = true
|
||||
}
|
||||
}
|
||||
if cfg.Timeout <= 0 {
|
||||
cfg.Timeout = 5 * time.Second
|
||||
}
|
||||
dialOpts := []grpc.DialOption{}
|
||||
if cfg.Insecure {
|
||||
dialOpts = append(dialOpts, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||
} else {
|
||||
dialOpts = append(dialOpts, grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})))
|
||||
}
|
||||
conn, err := grpc.NewClient(cfg.Endpoint, dialOpts...)
|
||||
if err != nil {
|
||||
return nil, merrors.InternalWrap(err, fmt.Sprintf("ledger: dial %s", cfg.Endpoint))
|
||||
}
|
||||
return &connectorClient{
|
||||
cfg: cfg,
|
||||
conn: conn,
|
||||
client: connectorv1.NewConnectorServiceClient(conn),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *connectorClient) Close() error {
|
||||
if c == nil || c.conn == nil {
|
||||
return nil
|
||||
}
|
||||
return c.conn.Close()
|
||||
}
|
||||
|
||||
func (c *connectorClient) GetAccount(ctx context.Context, accountID string) (*Account, error) {
|
||||
accountID = strings.TrimSpace(accountID)
|
||||
if accountID == "" {
|
||||
return nil, merrors.InvalidArgument("ledger account_id is required", "account_id")
|
||||
}
|
||||
ctx, cancel := c.callContext(ctx)
|
||||
defer cancel()
|
||||
|
||||
resp, err := c.client.GetAccount(ctx, &connectorv1.GetAccountRequest{
|
||||
AccountRef: &connectorv1.AccountRef{
|
||||
ConnectorId: ledgerConnectorID,
|
||||
AccountId: accountID,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
account := resp.GetAccount()
|
||||
if account == nil {
|
||||
return nil, merrors.NoData("ledger account not found")
|
||||
}
|
||||
organizationRef := strings.TrimSpace(account.GetOwnerRef())
|
||||
if organizationRef == "" && account.GetProviderDetails() != nil {
|
||||
if value, ok := account.GetProviderDetails().AsMap()["organization_ref"]; ok {
|
||||
organizationRef = strings.TrimSpace(fmt.Sprint(value))
|
||||
}
|
||||
}
|
||||
return &Account{
|
||||
AccountID: accountID,
|
||||
Currency: strings.ToUpper(strings.TrimSpace(account.GetAsset())),
|
||||
OrganizationRef: organizationRef,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *connectorClient) GetBalance(ctx context.Context, accountID string) (*Balance, error) {
|
||||
accountID = strings.TrimSpace(accountID)
|
||||
if accountID == "" {
|
||||
return nil, merrors.InvalidArgument("ledger account_id is required", "account_id")
|
||||
}
|
||||
ctx, cancel := c.callContext(ctx)
|
||||
defer cancel()
|
||||
|
||||
resp, err := c.client.GetBalance(ctx, &connectorv1.GetBalanceRequest{
|
||||
AccountRef: &connectorv1.AccountRef{
|
||||
ConnectorId: ledgerConnectorID,
|
||||
AccountId: accountID,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
balance := resp.GetBalance()
|
||||
if balance == nil || balance.GetAvailable() == nil {
|
||||
return nil, merrors.Internal("ledger balance is unavailable")
|
||||
}
|
||||
return &Balance{
|
||||
AccountID: accountID,
|
||||
Amount: strings.TrimSpace(balance.GetAvailable().GetAmount()),
|
||||
Currency: strings.ToUpper(strings.TrimSpace(balance.GetAvailable().GetCurrency())),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *connectorClient) ExternalCredit(ctx context.Context, req PostRequest) (*OperationResult, error) {
|
||||
return c.submitExternalOperation(ctx, connectorv1.OperationType_CREDIT, discovery.OperationExternalCredit, req)
|
||||
}
|
||||
|
||||
func (c *connectorClient) ExternalDebit(ctx context.Context, req PostRequest) (*OperationResult, error) {
|
||||
return c.submitExternalOperation(ctx, connectorv1.OperationType_DEBIT, discovery.OperationExternalDebit, req)
|
||||
}
|
||||
|
||||
func (c *connectorClient) submitExternalOperation(ctx context.Context, opType connectorv1.OperationType, operation string, req PostRequest) (*OperationResult, error) {
|
||||
req.AccountID = strings.TrimSpace(req.AccountID)
|
||||
req.OrganizationRef = strings.TrimSpace(req.OrganizationRef)
|
||||
req.Amount = strings.TrimSpace(req.Amount)
|
||||
req.Currency = strings.ToUpper(strings.TrimSpace(req.Currency))
|
||||
req.Reference = strings.TrimSpace(req.Reference)
|
||||
req.IdempotencyKey = strings.TrimSpace(req.IdempotencyKey)
|
||||
|
||||
if req.AccountID == "" {
|
||||
return nil, merrors.InvalidArgument("ledger account_id is required", "account_id")
|
||||
}
|
||||
if req.OrganizationRef == "" {
|
||||
return nil, merrors.InvalidArgument("ledger organization_ref is required", "organization_ref")
|
||||
}
|
||||
if req.Amount == "" || req.Currency == "" {
|
||||
return nil, merrors.InvalidArgument("ledger amount is required", "amount")
|
||||
}
|
||||
if req.IdempotencyKey == "" {
|
||||
return nil, merrors.InvalidArgument("ledger idempotency_key is required", "idempotency_key")
|
||||
}
|
||||
|
||||
params := map[string]any{
|
||||
"organization_ref": req.OrganizationRef,
|
||||
"operation": operation,
|
||||
"description": "tgsettle treasury operation",
|
||||
"metadata": map[string]any{
|
||||
"reference": req.Reference,
|
||||
},
|
||||
}
|
||||
operationReq := &connectorv1.Operation{
|
||||
Type: opType,
|
||||
IdempotencyKey: req.IdempotencyKey,
|
||||
Money: &moneyv1.Money{
|
||||
Amount: req.Amount,
|
||||
Currency: req.Currency,
|
||||
},
|
||||
Params: structFromMap(params),
|
||||
}
|
||||
account := &connectorv1.AccountRef{ConnectorId: ledgerConnectorID, AccountId: req.AccountID}
|
||||
switch opType {
|
||||
case connectorv1.OperationType_CREDIT:
|
||||
operationReq.To = &connectorv1.OperationParty{Ref: &connectorv1.OperationParty_Account{Account: account}}
|
||||
case connectorv1.OperationType_DEBIT:
|
||||
operationReq.From = &connectorv1.OperationParty{Ref: &connectorv1.OperationParty_Account{Account: account}}
|
||||
}
|
||||
|
||||
ctx, cancel := c.callContext(ctx)
|
||||
defer cancel()
|
||||
|
||||
resp, err := c.client.SubmitOperation(ctx, &connectorv1.SubmitOperationRequest{Operation: operationReq})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if resp.GetReceipt() == nil {
|
||||
return nil, merrors.Internal("ledger receipt is unavailable")
|
||||
}
|
||||
if receiptErr := resp.GetReceipt().GetError(); receiptErr != nil {
|
||||
message := strings.TrimSpace(receiptErr.GetMessage())
|
||||
if message == "" {
|
||||
message = "ledger operation failed"
|
||||
}
|
||||
return nil, merrors.InvalidArgument(message)
|
||||
}
|
||||
reference := strings.TrimSpace(resp.GetReceipt().GetOperationId())
|
||||
if reference == "" {
|
||||
reference = req.Reference
|
||||
}
|
||||
return &OperationResult{Reference: reference}, nil
|
||||
}
|
||||
|
||||
func (c *connectorClient) callContext(ctx context.Context) (context.Context, context.CancelFunc) {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
return context.WithTimeout(ctx, c.cfg.Timeout)
|
||||
}
|
||||
|
||||
func structFromMap(values map[string]any) *structpb.Struct {
|
||||
if len(values) == 0 {
|
||||
return nil
|
||||
}
|
||||
result, err := structpb.NewStruct(values)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func normalizeEndpoint(raw string) (string, bool) {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
return "", false
|
||||
}
|
||||
parsed, err := url.Parse(raw)
|
||||
if err != nil || parsed.Scheme == "" || parsed.Host == "" {
|
||||
return raw, false
|
||||
}
|
||||
switch strings.ToLower(strings.TrimSpace(parsed.Scheme)) {
|
||||
case "http", "grpc":
|
||||
return parsed.Host, true
|
||||
case "https", "grpcs":
|
||||
return parsed.Host, false
|
||||
default:
|
||||
return raw, false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,235 @@
|
||||
package ledger
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/url"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/tech/sendico/pkg/discovery"
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
"github.com/tech/sendico/pkg/mlogger"
|
||||
"github.com/tech/sendico/pkg/mservice"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type DiscoveryConfig struct {
|
||||
Logger mlogger.Logger
|
||||
Registry *discovery.Registry
|
||||
Timeout time.Duration
|
||||
}
|
||||
|
||||
type discoveryEndpoint struct {
|
||||
address string
|
||||
insecure bool
|
||||
raw string
|
||||
}
|
||||
|
||||
func (e discoveryEndpoint) key() string {
|
||||
return fmt.Sprintf("%s|%t", e.address, e.insecure)
|
||||
}
|
||||
|
||||
type discoveryClient struct {
|
||||
logger mlogger.Logger
|
||||
registry *discovery.Registry
|
||||
timeout time.Duration
|
||||
|
||||
mu sync.Mutex
|
||||
client Client
|
||||
endpointKey string
|
||||
}
|
||||
|
||||
func NewDiscoveryClient(cfg DiscoveryConfig) (Client, error) {
|
||||
if cfg.Registry == nil {
|
||||
return nil, merrors.InvalidArgument("treasury ledger discovery registry is required", "registry")
|
||||
}
|
||||
if cfg.Timeout <= 0 {
|
||||
cfg.Timeout = 5 * time.Second
|
||||
}
|
||||
logger := cfg.Logger
|
||||
if logger != nil {
|
||||
logger = logger.Named("treasury_ledger_discovery")
|
||||
}
|
||||
return &discoveryClient{
|
||||
logger: logger,
|
||||
registry: cfg.Registry,
|
||||
timeout: cfg.Timeout,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *discoveryClient) Close() error {
|
||||
if c == nil {
|
||||
return nil
|
||||
}
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
if c.client != nil {
|
||||
err := c.client.Close()
|
||||
c.client = nil
|
||||
c.endpointKey = ""
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *discoveryClient) GetAccount(ctx context.Context, accountID string) (*Account, error) {
|
||||
client, err := c.resolveClient(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return client.GetAccount(ctx, accountID)
|
||||
}
|
||||
|
||||
func (c *discoveryClient) GetBalance(ctx context.Context, accountID string) (*Balance, error) {
|
||||
client, err := c.resolveClient(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return client.GetBalance(ctx, accountID)
|
||||
}
|
||||
|
||||
func (c *discoveryClient) ExternalCredit(ctx context.Context, req PostRequest) (*OperationResult, error) {
|
||||
client, err := c.resolveClient(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return client.ExternalCredit(ctx, req)
|
||||
}
|
||||
|
||||
func (c *discoveryClient) ExternalDebit(ctx context.Context, req PostRequest) (*OperationResult, error) {
|
||||
client, err := c.resolveClient(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return client.ExternalDebit(ctx, req)
|
||||
}
|
||||
|
||||
func (c *discoveryClient) resolveClient(_ context.Context) (Client, error) {
|
||||
if c == nil || c.registry == nil {
|
||||
return nil, merrors.Internal("treasury ledger discovery is unavailable")
|
||||
}
|
||||
endpoint, err := c.resolveEndpoint()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
key := endpoint.key()
|
||||
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
if c.client != nil && c.endpointKey == key {
|
||||
return c.client, nil
|
||||
}
|
||||
if c.client != nil {
|
||||
_ = c.client.Close()
|
||||
c.client = nil
|
||||
c.endpointKey = ""
|
||||
}
|
||||
next, err := New(Config{
|
||||
Endpoint: endpoint.address,
|
||||
Timeout: c.timeout,
|
||||
Insecure: endpoint.insecure,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.client = next
|
||||
c.endpointKey = key
|
||||
if c.logger != nil {
|
||||
c.logger.Info("Discovered ledger endpoint selected",
|
||||
zap.String("service", string(mservice.Ledger)),
|
||||
zap.String("invoke_uri", endpoint.raw),
|
||||
zap.String("address", endpoint.address),
|
||||
zap.Bool("insecure", endpoint.insecure))
|
||||
}
|
||||
return c.client, nil
|
||||
}
|
||||
|
||||
func (c *discoveryClient) resolveEndpoint() (discoveryEndpoint, error) {
|
||||
entries := c.registry.List(time.Now(), true)
|
||||
type match struct {
|
||||
entry discovery.RegistryEntry
|
||||
opMatch bool
|
||||
}
|
||||
matches := make([]match, 0, len(entries))
|
||||
requiredOps := discovery.LedgerServiceOperations()
|
||||
for _, entry := range entries {
|
||||
if !matchesService(entry.Service, mservice.Ledger) {
|
||||
continue
|
||||
}
|
||||
matches = append(matches, match{
|
||||
entry: entry,
|
||||
opMatch: discovery.HasAnyOperation(entry.Operations, requiredOps),
|
||||
})
|
||||
}
|
||||
if len(matches) == 0 {
|
||||
return discoveryEndpoint{}, merrors.NoData("discovery: ledger service unavailable")
|
||||
}
|
||||
sort.Slice(matches, func(i, j int) bool {
|
||||
if matches[i].opMatch != matches[j].opMatch {
|
||||
return matches[i].opMatch
|
||||
}
|
||||
if matches[i].entry.RoutingPriority != matches[j].entry.RoutingPriority {
|
||||
return matches[i].entry.RoutingPriority > matches[j].entry.RoutingPriority
|
||||
}
|
||||
if matches[i].entry.ID != matches[j].entry.ID {
|
||||
return matches[i].entry.ID < matches[j].entry.ID
|
||||
}
|
||||
return matches[i].entry.InstanceID < matches[j].entry.InstanceID
|
||||
})
|
||||
return parseDiscoveryEndpoint(matches[0].entry.InvokeURI)
|
||||
}
|
||||
|
||||
func matchesService(service string, candidate mservice.Type) bool {
|
||||
service = strings.TrimSpace(service)
|
||||
if service == "" || strings.TrimSpace(string(candidate)) == "" {
|
||||
return false
|
||||
}
|
||||
return strings.EqualFold(service, strings.TrimSpace(string(candidate)))
|
||||
}
|
||||
|
||||
func parseDiscoveryEndpoint(raw string) (discoveryEndpoint, error) {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
return discoveryEndpoint{}, merrors.InvalidArgument("discovery: invoke uri is required")
|
||||
}
|
||||
|
||||
if !strings.Contains(raw, "://") {
|
||||
if _, _, splitErr := net.SplitHostPort(raw); splitErr != nil {
|
||||
return discoveryEndpoint{}, merrors.InvalidArgument("discovery: invoke uri must include host:port")
|
||||
}
|
||||
return discoveryEndpoint{address: raw, insecure: true, raw: raw}, nil
|
||||
}
|
||||
|
||||
parsed, err := url.Parse(raw)
|
||||
if err != nil || parsed.Scheme == "" {
|
||||
if err != nil {
|
||||
return discoveryEndpoint{}, err
|
||||
}
|
||||
return discoveryEndpoint{}, merrors.InvalidArgument("discovery: invoke uri must include host:port")
|
||||
}
|
||||
|
||||
scheme := strings.ToLower(strings.TrimSpace(parsed.Scheme))
|
||||
switch scheme {
|
||||
case "grpc":
|
||||
address := strings.TrimSpace(parsed.Host)
|
||||
if _, _, splitErr := net.SplitHostPort(address); splitErr != nil {
|
||||
return discoveryEndpoint{}, merrors.InvalidArgument("discovery: invoke uri must include host:port")
|
||||
}
|
||||
return discoveryEndpoint{address: address, insecure: true, raw: raw}, nil
|
||||
case "grpcs":
|
||||
address := strings.TrimSpace(parsed.Host)
|
||||
if _, _, splitErr := net.SplitHostPort(address); splitErr != nil {
|
||||
return discoveryEndpoint{}, merrors.InvalidArgument("discovery: invoke uri must include host:port")
|
||||
}
|
||||
return discoveryEndpoint{address: address, insecure: false, raw: raw}, nil
|
||||
case "dns", "passthrough":
|
||||
return discoveryEndpoint{address: raw, insecure: true, raw: raw}, nil
|
||||
default:
|
||||
return discoveryEndpoint{}, merrors.InvalidArgument("discovery: unsupported invoke uri scheme")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user