Skip to content

Commit e401a9e

Browse files
authored
Refactor integrations into a common package (#171)
1 parent f22a64c commit e401a9e

11 files changed

Lines changed: 89 additions & 80 deletions

File tree

ocp/antispam/guard.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ import (
88
"github.com/code-payments/ocp-server/metrics"
99
"github.com/code-payments/ocp-server/ocp/common"
1010
"github.com/code-payments/ocp-server/ocp/data/swap"
11+
"github.com/code-payments/ocp-server/ocp/integration"
1112
)
1213

1314
type Guard struct {
14-
integration Integration
15+
integration integration.Antispam
1516
}
1617

17-
func NewGuard(integration Integration) *Guard {
18+
func NewGuard(integration integration.Antispam) *Guard {
1819
return &Guard{integration: integration}
1920
}
2021

ocp/antispam/integration.go

Lines changed: 0 additions & 58 deletions
This file was deleted.

ocp/integration/antispam.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package integration
2+
3+
import (
4+
"context"
5+
6+
transactionpb "github.com/code-payments/ocp-protobuf-api/generated/go/transaction/v1"
7+
8+
"github.com/code-payments/ocp-server/ocp/common"
9+
"github.com/code-payments/ocp-server/ocp/data/swap"
10+
)
11+
12+
// Antispam is an antispam guard integration that apps can implement to check
13+
// whether operations of interest are allowed to be performed.
14+
type Antispam interface {
15+
AllowOpenAccounts(ctx context.Context, owner *common.Account, accountSet transactionpb.OpenAccountsMetadata_AccountSet) (bool, string, error)
16+
17+
AllowSendPayment(ctx context.Context, owner, destination *common.Account, isPublic bool) (bool, string, error)
18+
19+
AllowReceivePayments(ctx context.Context, owner *common.Account, isPublic bool) (bool, string, error)
20+
21+
AllowDistribution(ctx context.Context, owner *common.Account, isPublic bool) (bool, string, error)
22+
23+
AllowSwap(ctx context.Context, fundingSource swap.FundingSource, owner, fromMint, toMint *common.Account, amount uint64, initializesMint bool) (bool, string, error)
24+
25+
AllowCurrencyLaunch(ctx context.Context, owner *common.Account, name, symbol string) (bool, string, error)
26+
}
27+
28+
type allowEverythingAntispamIntegration struct {
29+
}
30+
31+
// NewAllowEverythingAntispamIntegration returns a default antispam integration that allows everything
32+
func NewAllowEverythingAntispamIntegration() Antispam {
33+
return &allowEverythingAntispamIntegration{}
34+
}
35+
36+
func (i *allowEverythingAntispamIntegration) AllowOpenAccounts(ctx context.Context, owner *common.Account, accountSet transactionpb.OpenAccountsMetadata_AccountSet) (bool, string, error) {
37+
return true, "", nil
38+
}
39+
40+
func (i *allowEverythingAntispamIntegration) AllowSendPayment(ctx context.Context, owner, destination *common.Account, isPublic bool) (bool, string, error) {
41+
return true, "", nil
42+
}
43+
44+
func (i *allowEverythingAntispamIntegration) AllowReceivePayments(ctx context.Context, owner *common.Account, isPublic bool) (bool, string, error) {
45+
return true, "", nil
46+
}
47+
48+
func (i *allowEverythingAntispamIntegration) AllowDistribution(ctx context.Context, owner *common.Account, isPublic bool) (bool, string, error) {
49+
return true, "", nil
50+
}
51+
52+
func (i *allowEverythingAntispamIntegration) AllowSwap(ctx context.Context, fundingSource swap.FundingSource, owner, fromMint, toMint *common.Account, amount uint64, initializesMint bool) (bool, string, error) {
53+
return true, "", nil
54+
}
55+
56+
func (i *allowEverythingAntispamIntegration) AllowCurrencyLaunch(ctx context.Context, owner *common.Account, name, symbol string) (bool, string, error) {
57+
return true, "", nil
58+
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
package geyser
1+
package integration
22

33
import (
44
"context"
55

66
"github.com/code-payments/ocp-server/ocp/common"
77
)
88

9-
// Integration allows for notifications based on events processed by Geyser
10-
type Integration interface {
9+
// Swap is an integration that hooks into the Geyser worker
10+
type Geyser interface {
11+
// OnDepositReceived allows for notifications for external deposits processed by Geyser
1112
OnDepositReceived(ctx context.Context, owner, mint *common.Account, currencyName string, usdMarketValue float64) error
1213
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package transaction
1+
package integration
22

33
import (
44
"context"
@@ -8,7 +8,8 @@ import (
88
"github.com/code-payments/ocp-server/ocp/data/intent"
99
)
1010

11-
type SubmitIntentIntegration interface {
11+
// SubmitIntent is an integration that hooks into SubmitIntent
12+
type SubmitIntent interface {
1213
// AllowCreation determines whether the new intent creation should be allowed
1314
// with app-specific validation rules
1415
AllowCreation(ctx context.Context, intentRecord *intent.Record, metadata *transactionpb.Metadata, actions []*transactionpb.Action) error
@@ -22,7 +23,7 @@ type defaultSubmitIntentIntegration struct {
2223
}
2324

2425
// NewDefaultSubmitIntentIntegration retuns a SubmitIntentIntegration that allows everything
25-
func NewDefaultSubmitIntentIntegration() SubmitIntentIntegration {
26+
func NewDefaultSubmitIntentIntegration() SubmitIntent {
2627
return &defaultSubmitIntentIntegration{}
2728
}
2829

Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package swap
1+
package integration
22

33
import (
44
"context"
@@ -7,7 +7,8 @@ import (
77
"github.com/code-payments/ocp-server/ocp/common"
88
)
99

10-
// Integration allows for notifications based on events processed by the swap worker
11-
type Integration interface {
10+
// Swap is an integration that hooks into the swap worker
11+
type Swap interface {
12+
// OnSwapFinalized allows for notifications based on events processed by the swap worker
1213
OnSwapFinalized(ctx context.Context, owner *common.Account, isBuy bool, mint *common.Account, currencyName string, region currency.Code, valueReceived float64, isMintInit bool) error
1314
}

ocp/rpc/transaction/server.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
currency_util "github.com/code-payments/ocp-server/ocp/currency"
1717
ocp_data "github.com/code-payments/ocp-server/ocp/data"
1818
"github.com/code-payments/ocp-server/ocp/data/nonce"
19+
"github.com/code-payments/ocp-server/ocp/integration"
1920
"github.com/code-payments/ocp-server/ocp/transaction"
2021
)
2122

@@ -29,7 +30,7 @@ type transactionServer struct {
2930

3031
auth *auth_util.RPCSignatureVerifier
3132

32-
submitIntentIntegration SubmitIntentIntegration
33+
submitIntentIntegration integration.SubmitIntent
3334

3435
antispamGuard *antispam.Guard
3536
amlGuard *aml.Guard
@@ -49,7 +50,7 @@ func NewTransactionServer(
4950
log *zap.Logger,
5051
data ocp_data.Provider,
5152
mintDataProvider *currency_util.MintDataProvider,
52-
submitIntentIntegration SubmitIntentIntegration,
53+
submitIntentIntegration integration.SubmitIntent,
5354
antispamGuard *antispam.Guard,
5455
amlGuard *aml.Guard,
5556
nodeID string,

ocp/worker/geyser/external_deposit.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/code-payments/ocp-server/ocp/data/intent"
2525
"github.com/code-payments/ocp-server/ocp/data/swap"
2626
"github.com/code-payments/ocp-server/ocp/data/transaction"
27+
"github.com/code-payments/ocp-server/ocp/integration"
2728
transaction_util "github.com/code-payments/ocp-server/ocp/transaction"
2829
vm_util "github.com/code-payments/ocp-server/ocp/vm"
2930
"github.com/code-payments/ocp-server/retry"
@@ -43,7 +44,7 @@ var (
4344
syncedDepositCache = cache.NewCache(1_000_000)
4445
)
4546

46-
func fixMissingExternalDeposits(ctx context.Context, data ocp_data.Provider, integration Integration, userAuthority, mint *common.Account) error {
47+
func fixMissingExternalDeposits(ctx context.Context, data ocp_data.Provider, integration integration.Geyser, userAuthority, mint *common.Account) error {
4748
err := maybeInitiateExternalDepositIntoVm(ctx, data, userAuthority, mint)
4849
if err != nil {
4950
return errors.Wrap(err, "error depositing into the vm")
@@ -227,7 +228,7 @@ func findPotentialExternalDepositsIntoVm(ctx context.Context, data ocp_data.Prov
227228
}
228229
}
229230

230-
func processPotentialExternalDepositIntoVm(ctx context.Context, data ocp_data.Provider, integration Integration, signature string, userAuthority, mint *common.Account) error {
231+
func processPotentialExternalDepositIntoVm(ctx context.Context, data ocp_data.Provider, integration integration.Geyser, signature string, userAuthority, mint *common.Account) error {
231232
vmConfig, err := common.GetVmConfigForMint(ctx, data, mint)
232233
if err != nil {
233234
return err

ocp/worker/geyser/handler.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/mr-tron/base58"
88
"github.com/pkg/errors"
99

10+
"github.com/code-payments/ocp-server/ocp/integration"
1011
geyserpb "github.com/code-payments/ocp-server/ocp/worker/geyser/api/gen"
1112

1213
"github.com/code-payments/ocp-server/ocp/common"
@@ -29,10 +30,10 @@ type ProgramAccountUpdateHandler interface {
2930
type TokenProgramAccountHandler struct {
3031
conf *conf
3132
data ocp_data.Provider
32-
integration Integration
33+
integration integration.Geyser
3334
}
3435

35-
func NewTokenProgramAccountHandler(conf *conf, data ocp_data.Provider, integration Integration) ProgramAccountUpdateHandler {
36+
func NewTokenProgramAccountHandler(conf *conf, data ocp_data.Provider, integration integration.Geyser) ProgramAccountUpdateHandler {
3637
return &TokenProgramAccountHandler{
3738
conf: conf,
3839
data: data,
@@ -120,7 +121,7 @@ func (h *TokenProgramAccountHandler) Handle(ctx context.Context, update *geyserp
120121
return nil
121122
}
122123

123-
func initializeProgramAccountUpdateHandlers(conf *conf, data ocp_data.Provider, integration Integration) map[string]ProgramAccountUpdateHandler {
124+
func initializeProgramAccountUpdateHandlers(conf *conf, data ocp_data.Provider, integration integration.Geyser) map[string]ProgramAccountUpdateHandler {
124125
return map[string]ProgramAccountUpdateHandler{
125126
base58.Encode(token.ProgramKey): NewTokenProgramAccountHandler(conf, data, integration),
126127
}

ocp/worker/geyser/runtime.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"go.uber.org/zap"
99

10+
"github.com/code-payments/ocp-server/ocp/integration"
1011
"github.com/code-payments/ocp-server/ocp/worker"
1112
geyserpb "github.com/code-payments/ocp-server/ocp/worker/geyser/api/gen"
1213
timelock_token "github.com/code-payments/ocp-server/solana/timelock/v1"
@@ -25,7 +26,7 @@ type runtime struct {
2526
data ocp_data.Provider
2627
conf *conf
2728

28-
integration Integration
29+
integration integration.Geyser
2930

3031
programUpdatesChan chan *geyserpb.SubscribeUpdateAccount
3132
programUpdateHandlers map[string]ProgramAccountUpdateHandler
@@ -45,7 +46,7 @@ type runtime struct {
4546
backupExternalDepositWorkerStatus bool
4647
}
4748

48-
func New(log *zap.Logger, data ocp_data.Provider, integration Integration, configProvider ConfigProvider) worker.Runtime {
49+
func New(log *zap.Logger, data ocp_data.Provider, integration integration.Geyser, configProvider ConfigProvider) worker.Runtime {
4950
conf := configProvider()
5051
return &runtime{
5152
log: log,

0 commit comments

Comments
 (0)