mirror of
https://github.com/hanzo-fi/payments.git
synced 2026-10-30 20:31:20 +00:00
115 lines
3.0 KiB
Go
115 lines
3.0 KiB
Go
package fireblocks
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/hanzo-fi/go-libs/v5/pkg/observe/log"
|
|
"github.com/hanzo-fi/payments/ee/plugins/fireblocks/client"
|
|
"github.com/hanzo-fi/payments/pkg/domain/models"
|
|
pkgplugins "github.com/hanzo-fi/payments/pkg/domain/plugins"
|
|
)
|
|
|
|
const (
|
|
ProviderName = "fireblocks"
|
|
MetadataPrefix = "com.fireblocks.spec/"
|
|
|
|
assetRefreshInterval = 24 * time.Hour
|
|
)
|
|
|
|
var Registration = pkgplugins.Registration{
|
|
PluginType: models.PluginTypePSP,
|
|
CreateFunc: func(_ models.ConnectorID, name string, logger logging.Logger, rm json.RawMessage) (models.Plugin, error) {
|
|
return New(name, logger, rm)
|
|
},
|
|
Capabilities: capabilities,
|
|
RawConf: Config{},
|
|
PageSize: PAGE_SIZE,
|
|
}
|
|
|
|
type Plugin struct {
|
|
models.Plugin
|
|
|
|
name string
|
|
logger logging.Logger
|
|
|
|
client client.Client
|
|
config Config
|
|
|
|
assetsMu sync.RWMutex
|
|
assetsRefreshMu sync.Mutex
|
|
assetsLastSync time.Time
|
|
assets map[string]assetInfo // keyed by uppercased legacyId
|
|
}
|
|
|
|
func New(name string, logger logging.Logger, rawConfig json.RawMessage) (*Plugin, error) {
|
|
config, err := unmarshalAndValidateConfig(rawConfig)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
c := client.New(ProviderName, config.APIKey, config.privateKey, config.Endpoint)
|
|
|
|
return &Plugin{
|
|
Plugin: pkgplugins.NewBasePlugin(),
|
|
|
|
name: name,
|
|
logger: logger,
|
|
client: c,
|
|
config: config,
|
|
}, nil
|
|
}
|
|
|
|
func (p *Plugin) Name() string {
|
|
return p.name
|
|
}
|
|
|
|
func (p *Plugin) Config() models.PluginInternalConfig {
|
|
return p.config
|
|
}
|
|
|
|
func (p *Plugin) Install(ctx context.Context, req models.InstallRequest) (models.InstallResponse, error) {
|
|
if err := p.loadAssets(ctx); err != nil {
|
|
return models.InstallResponse{}, err
|
|
}
|
|
|
|
return models.InstallResponse{
|
|
Workflow: workflow(),
|
|
}, nil
|
|
}
|
|
|
|
func (p *Plugin) Uninstall(ctx context.Context, req models.UninstallRequest) (models.UninstallResponse, error) {
|
|
return models.UninstallResponse{}, nil
|
|
}
|
|
|
|
func (p *Plugin) FetchNextAccounts(ctx context.Context, req models.FetchNextAccountsRequest) (models.FetchNextAccountsResponse, error) {
|
|
if p.client == nil {
|
|
return models.FetchNextAccountsResponse{}, pkgplugins.ErrNotYetInstalled
|
|
}
|
|
return p.fetchNextAccounts(ctx, req)
|
|
}
|
|
|
|
func (p *Plugin) FetchNextBalances(ctx context.Context, req models.FetchNextBalancesRequest) (models.FetchNextBalancesResponse, error) {
|
|
if p.client == nil {
|
|
return models.FetchNextBalancesResponse{}, pkgplugins.ErrNotYetInstalled
|
|
}
|
|
if err := p.ensureAssetsFresh(ctx); err != nil {
|
|
return models.FetchNextBalancesResponse{}, err
|
|
}
|
|
return p.fetchNextBalances(ctx, req)
|
|
}
|
|
|
|
func (p *Plugin) FetchNextPayments(ctx context.Context, req models.FetchNextPaymentsRequest) (models.FetchNextPaymentsResponse, error) {
|
|
if p.client == nil {
|
|
return models.FetchNextPaymentsResponse{}, pkgplugins.ErrNotYetInstalled
|
|
}
|
|
if err := p.ensureAssetsFresh(ctx); err != nil {
|
|
return models.FetchNextPaymentsResponse{}, err
|
|
}
|
|
return p.fetchNextPayments(ctx, req)
|
|
}
|
|
|
|
var _ models.Plugin = &Plugin{}
|