diff --git a/ocp/antispam/guard.go b/ocp/antispam/guard.go index acba395..af99b42 100644 --- a/ocp/antispam/guard.go +++ b/ocp/antispam/guard.go @@ -8,13 +8,14 @@ import ( "github.com/code-payments/ocp-server/metrics" "github.com/code-payments/ocp-server/ocp/common" "github.com/code-payments/ocp-server/ocp/data/swap" + "github.com/code-payments/ocp-server/ocp/integration" ) type Guard struct { - integration Integration + integration integration.Antispam } -func NewGuard(integration Integration) *Guard { +func NewGuard(integration integration.Antispam) *Guard { return &Guard{integration: integration} } diff --git a/ocp/antispam/integration.go b/ocp/antispam/integration.go deleted file mode 100644 index a77801a..0000000 --- a/ocp/antispam/integration.go +++ /dev/null @@ -1,58 +0,0 @@ -package antispam - -import ( - "context" - - transactionpb "github.com/code-payments/ocp-protobuf-api/generated/go/transaction/v1" - - "github.com/code-payments/ocp-server/ocp/common" - "github.com/code-payments/ocp-server/ocp/data/swap" -) - -// Integration is an antispam guard integration that apps can implement to check -// whether operations of interest are allowed to be performed. -type Integration interface { - AllowOpenAccounts(ctx context.Context, owner *common.Account, accountSet transactionpb.OpenAccountsMetadata_AccountSet) (bool, string, error) - - AllowSendPayment(ctx context.Context, owner, destination *common.Account, isPublic bool) (bool, string, error) - - AllowReceivePayments(ctx context.Context, owner *common.Account, isPublic bool) (bool, string, error) - - AllowDistribution(ctx context.Context, owner *common.Account, isPublic bool) (bool, string, error) - - AllowSwap(ctx context.Context, fundingSource swap.FundingSource, owner, fromMint, toMint *common.Account, amount uint64, initializesMint bool) (bool, string, error) - - AllowCurrencyLaunch(ctx context.Context, owner *common.Account, name, symbol string) (bool, string, error) -} - -type allowEverythingIntegration struct { -} - -// NewAllowEverything returns a default antispam integration that allows everything -func NewAllowEverything() Integration { - return &allowEverythingIntegration{} -} - -func (i *allowEverythingIntegration) AllowOpenAccounts(ctx context.Context, owner *common.Account, accountSet transactionpb.OpenAccountsMetadata_AccountSet) (bool, string, error) { - return true, "", nil -} - -func (i *allowEverythingIntegration) AllowSendPayment(ctx context.Context, owner, destination *common.Account, isPublic bool) (bool, string, error) { - return true, "", nil -} - -func (i *allowEverythingIntegration) AllowReceivePayments(ctx context.Context, owner *common.Account, isPublic bool) (bool, string, error) { - return true, "", nil -} - -func (i *allowEverythingIntegration) AllowDistribution(ctx context.Context, owner *common.Account, isPublic bool) (bool, string, error) { - return true, "", nil -} - -func (i *allowEverythingIntegration) AllowSwap(ctx context.Context, fundingSource swap.FundingSource, owner, fromMint, toMint *common.Account, amount uint64, initializesMint bool) (bool, string, error) { - return true, "", nil -} - -func (i *allowEverythingIntegration) AllowCurrencyLaunch(ctx context.Context, owner *common.Account, name, symbol string) (bool, string, error) { - return true, "", nil -} diff --git a/ocp/integration/antispam.go b/ocp/integration/antispam.go new file mode 100644 index 0000000..d278ccb --- /dev/null +++ b/ocp/integration/antispam.go @@ -0,0 +1,58 @@ +package integration + +import ( + "context" + + transactionpb "github.com/code-payments/ocp-protobuf-api/generated/go/transaction/v1" + + "github.com/code-payments/ocp-server/ocp/common" + "github.com/code-payments/ocp-server/ocp/data/swap" +) + +// Antispam is an antispam guard integration that apps can implement to check +// whether operations of interest are allowed to be performed. +type Antispam interface { + AllowOpenAccounts(ctx context.Context, owner *common.Account, accountSet transactionpb.OpenAccountsMetadata_AccountSet) (bool, string, error) + + AllowSendPayment(ctx context.Context, owner, destination *common.Account, isPublic bool) (bool, string, error) + + AllowReceivePayments(ctx context.Context, owner *common.Account, isPublic bool) (bool, string, error) + + AllowDistribution(ctx context.Context, owner *common.Account, isPublic bool) (bool, string, error) + + AllowSwap(ctx context.Context, fundingSource swap.FundingSource, owner, fromMint, toMint *common.Account, amount uint64, initializesMint bool) (bool, string, error) + + AllowCurrencyLaunch(ctx context.Context, owner *common.Account, name, symbol string) (bool, string, error) +} + +type allowEverythingAntispamIntegration struct { +} + +// NewAllowEverythingAntispamIntegration returns a default antispam integration that allows everything +func NewAllowEverythingAntispamIntegration() Antispam { + return &allowEverythingAntispamIntegration{} +} + +func (i *allowEverythingAntispamIntegration) AllowOpenAccounts(ctx context.Context, owner *common.Account, accountSet transactionpb.OpenAccountsMetadata_AccountSet) (bool, string, error) { + return true, "", nil +} + +func (i *allowEverythingAntispamIntegration) AllowSendPayment(ctx context.Context, owner, destination *common.Account, isPublic bool) (bool, string, error) { + return true, "", nil +} + +func (i *allowEverythingAntispamIntegration) AllowReceivePayments(ctx context.Context, owner *common.Account, isPublic bool) (bool, string, error) { + return true, "", nil +} + +func (i *allowEverythingAntispamIntegration) AllowDistribution(ctx context.Context, owner *common.Account, isPublic bool) (bool, string, error) { + return true, "", nil +} + +func (i *allowEverythingAntispamIntegration) AllowSwap(ctx context.Context, fundingSource swap.FundingSource, owner, fromMint, toMint *common.Account, amount uint64, initializesMint bool) (bool, string, error) { + return true, "", nil +} + +func (i *allowEverythingAntispamIntegration) AllowCurrencyLaunch(ctx context.Context, owner *common.Account, name, symbol string) (bool, string, error) { + return true, "", nil +} diff --git a/ocp/worker/geyser/integration.go b/ocp/integration/geyser.go similarity index 50% rename from ocp/worker/geyser/integration.go rename to ocp/integration/geyser.go index 7f7d415..22f15f0 100644 --- a/ocp/worker/geyser/integration.go +++ b/ocp/integration/geyser.go @@ -1,4 +1,4 @@ -package geyser +package integration import ( "context" @@ -6,7 +6,8 @@ import ( "github.com/code-payments/ocp-server/ocp/common" ) -// Integration allows for notifications based on events processed by Geyser -type Integration interface { +// Swap is an integration that hooks into the Geyser worker +type Geyser interface { + // OnDepositReceived allows for notifications for external deposits processed by Geyser OnDepositReceived(ctx context.Context, owner, mint *common.Account, currencyName string, usdMarketValue float64) error } diff --git a/ocp/rpc/transaction/integration.go b/ocp/integration/submit_intent.go similarity index 86% rename from ocp/rpc/transaction/integration.go rename to ocp/integration/submit_intent.go index 398dcc0..b3094ad 100644 --- a/ocp/rpc/transaction/integration.go +++ b/ocp/integration/submit_intent.go @@ -1,4 +1,4 @@ -package transaction +package integration import ( "context" @@ -8,7 +8,8 @@ import ( "github.com/code-payments/ocp-server/ocp/data/intent" ) -type SubmitIntentIntegration interface { +// SubmitIntent is an integration that hooks into SubmitIntent +type SubmitIntent interface { // AllowCreation determines whether the new intent creation should be allowed // with app-specific validation rules AllowCreation(ctx context.Context, intentRecord *intent.Record, metadata *transactionpb.Metadata, actions []*transactionpb.Action) error @@ -22,7 +23,7 @@ type defaultSubmitIntentIntegration struct { } // NewDefaultSubmitIntentIntegration retuns a SubmitIntentIntegration that allows everything -func NewDefaultSubmitIntentIntegration() SubmitIntentIntegration { +func NewDefaultSubmitIntentIntegration() SubmitIntent { return &defaultSubmitIntentIntegration{} } diff --git a/ocp/worker/swap/integration.go b/ocp/integration/swap.go similarity index 61% rename from ocp/worker/swap/integration.go rename to ocp/integration/swap.go index f4b2d56..62261c5 100644 --- a/ocp/worker/swap/integration.go +++ b/ocp/integration/swap.go @@ -1,4 +1,4 @@ -package swap +package integration import ( "context" @@ -7,7 +7,8 @@ import ( "github.com/code-payments/ocp-server/ocp/common" ) -// Integration allows for notifications based on events processed by the swap worker -type Integration interface { +// Swap is an integration that hooks into the swap worker +type Swap interface { + // OnSwapFinalized allows for notifications based on events processed by the swap worker OnSwapFinalized(ctx context.Context, owner *common.Account, isBuy bool, mint *common.Account, currencyName string, region currency.Code, valueReceived float64, isMintInit bool) error } diff --git a/ocp/rpc/transaction/server.go b/ocp/rpc/transaction/server.go index 9457d9e..e5ba1fa 100644 --- a/ocp/rpc/transaction/server.go +++ b/ocp/rpc/transaction/server.go @@ -16,6 +16,7 @@ import ( currency_util "github.com/code-payments/ocp-server/ocp/currency" ocp_data "github.com/code-payments/ocp-server/ocp/data" "github.com/code-payments/ocp-server/ocp/data/nonce" + "github.com/code-payments/ocp-server/ocp/integration" "github.com/code-payments/ocp-server/ocp/transaction" ) @@ -29,7 +30,7 @@ type transactionServer struct { auth *auth_util.RPCSignatureVerifier - submitIntentIntegration SubmitIntentIntegration + submitIntentIntegration integration.SubmitIntent antispamGuard *antispam.Guard amlGuard *aml.Guard @@ -49,7 +50,7 @@ func NewTransactionServer( log *zap.Logger, data ocp_data.Provider, mintDataProvider *currency_util.MintDataProvider, - submitIntentIntegration SubmitIntentIntegration, + submitIntentIntegration integration.SubmitIntent, antispamGuard *antispam.Guard, amlGuard *aml.Guard, nodeID string, diff --git a/ocp/worker/geyser/external_deposit.go b/ocp/worker/geyser/external_deposit.go index 2128700..2cdc2ff 100644 --- a/ocp/worker/geyser/external_deposit.go +++ b/ocp/worker/geyser/external_deposit.go @@ -24,6 +24,7 @@ import ( "github.com/code-payments/ocp-server/ocp/data/intent" "github.com/code-payments/ocp-server/ocp/data/swap" "github.com/code-payments/ocp-server/ocp/data/transaction" + "github.com/code-payments/ocp-server/ocp/integration" transaction_util "github.com/code-payments/ocp-server/ocp/transaction" vm_util "github.com/code-payments/ocp-server/ocp/vm" "github.com/code-payments/ocp-server/retry" @@ -43,7 +44,7 @@ var ( syncedDepositCache = cache.NewCache(1_000_000) ) -func fixMissingExternalDeposits(ctx context.Context, data ocp_data.Provider, integration Integration, userAuthority, mint *common.Account) error { +func fixMissingExternalDeposits(ctx context.Context, data ocp_data.Provider, integration integration.Geyser, userAuthority, mint *common.Account) error { err := maybeInitiateExternalDepositIntoVm(ctx, data, userAuthority, mint) if err != nil { return errors.Wrap(err, "error depositing into the vm") @@ -227,7 +228,7 @@ func findPotentialExternalDepositsIntoVm(ctx context.Context, data ocp_data.Prov } } -func processPotentialExternalDepositIntoVm(ctx context.Context, data ocp_data.Provider, integration Integration, signature string, userAuthority, mint *common.Account) error { +func processPotentialExternalDepositIntoVm(ctx context.Context, data ocp_data.Provider, integration integration.Geyser, signature string, userAuthority, mint *common.Account) error { vmConfig, err := common.GetVmConfigForMint(ctx, data, mint) if err != nil { return err diff --git a/ocp/worker/geyser/handler.go b/ocp/worker/geyser/handler.go index 96dabda..81dec6c 100644 --- a/ocp/worker/geyser/handler.go +++ b/ocp/worker/geyser/handler.go @@ -7,6 +7,7 @@ import ( "github.com/mr-tron/base58" "github.com/pkg/errors" + "github.com/code-payments/ocp-server/ocp/integration" geyserpb "github.com/code-payments/ocp-server/ocp/worker/geyser/api/gen" "github.com/code-payments/ocp-server/ocp/common" @@ -29,10 +30,10 @@ type ProgramAccountUpdateHandler interface { type TokenProgramAccountHandler struct { conf *conf data ocp_data.Provider - integration Integration + integration integration.Geyser } -func NewTokenProgramAccountHandler(conf *conf, data ocp_data.Provider, integration Integration) ProgramAccountUpdateHandler { +func NewTokenProgramAccountHandler(conf *conf, data ocp_data.Provider, integration integration.Geyser) ProgramAccountUpdateHandler { return &TokenProgramAccountHandler{ conf: conf, data: data, @@ -120,7 +121,7 @@ func (h *TokenProgramAccountHandler) Handle(ctx context.Context, update *geyserp return nil } -func initializeProgramAccountUpdateHandlers(conf *conf, data ocp_data.Provider, integration Integration) map[string]ProgramAccountUpdateHandler { +func initializeProgramAccountUpdateHandlers(conf *conf, data ocp_data.Provider, integration integration.Geyser) map[string]ProgramAccountUpdateHandler { return map[string]ProgramAccountUpdateHandler{ base58.Encode(token.ProgramKey): NewTokenProgramAccountHandler(conf, data, integration), } diff --git a/ocp/worker/geyser/runtime.go b/ocp/worker/geyser/runtime.go index becb363..c82250d 100644 --- a/ocp/worker/geyser/runtime.go +++ b/ocp/worker/geyser/runtime.go @@ -7,6 +7,7 @@ import ( "go.uber.org/zap" + "github.com/code-payments/ocp-server/ocp/integration" "github.com/code-payments/ocp-server/ocp/worker" geyserpb "github.com/code-payments/ocp-server/ocp/worker/geyser/api/gen" timelock_token "github.com/code-payments/ocp-server/solana/timelock/v1" @@ -25,7 +26,7 @@ type runtime struct { data ocp_data.Provider conf *conf - integration Integration + integration integration.Geyser programUpdatesChan chan *geyserpb.SubscribeUpdateAccount programUpdateHandlers map[string]ProgramAccountUpdateHandler @@ -45,7 +46,7 @@ type runtime struct { backupExternalDepositWorkerStatus bool } -func New(log *zap.Logger, data ocp_data.Provider, integration Integration, configProvider ConfigProvider) worker.Runtime { +func New(log *zap.Logger, data ocp_data.Provider, integration integration.Geyser, configProvider ConfigProvider) worker.Runtime { conf := configProvider() return &runtime{ log: log, diff --git a/ocp/worker/swap/runtime.go b/ocp/worker/swap/runtime.go index d5c2c61..07f00b0 100644 --- a/ocp/worker/swap/runtime.go +++ b/ocp/worker/swap/runtime.go @@ -11,6 +11,7 @@ import ( ocp_data "github.com/code-payments/ocp-server/ocp/data" "github.com/code-payments/ocp-server/ocp/data/swap" + "github.com/code-payments/ocp-server/ocp/integration" "github.com/code-payments/ocp-server/ocp/worker" ) @@ -19,10 +20,10 @@ type runtime struct { conf *conf data ocp_data.Provider vmIndexerClient indexerpb.IndexerClient - integration Integration + integration integration.Swap } -func New(log *zap.Logger, data ocp_data.Provider, vmIndexerClient indexerpb.IndexerClient, integration Integration, configProvider ConfigProvider) worker.Runtime { +func New(log *zap.Logger, data ocp_data.Provider, vmIndexerClient indexerpb.IndexerClient, integration integration.Swap, configProvider ConfigProvider) worker.Runtime { return &runtime{ log: log, conf: configProvider(),