Skip to content

Commit

Permalink
Abstract routing methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jarrel-b committed Apr 30, 2024
1 parent 42e1fb8 commit 1a6a2fd
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 95 deletions.
3 changes: 1 addition & 2 deletions graphql/fixture_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,12 @@ func useTokenProcessing(t *testing.T) {
t.Helper()
ctx := context.Background()
c := server.ClientInit(ctx)
p, cleanup := server.NewMultichainProvider(ctx, server.SetDefaults)
p := multichain.NewMultichainProvider(ctx, c.Repos, c.Queries)
server := httptest.NewServer(tokenprocessing.CoreInitServer(ctx, c, p))
t.Setenv("TOKEN_PROCESSING_URL", server.URL)
t.Cleanup(func() {
server.Close()
c.Close()
cleanup()
})
}

Expand Down
32 changes: 14 additions & 18 deletions graphql/graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/mikeydub/go-gallery/server"
"github.com/mikeydub/go-gallery/service/auth"
"github.com/mikeydub/go-gallery/service/multichain"
"github.com/mikeydub/go-gallery/service/multichain/common"
"github.com/mikeydub/go-gallery/service/persist"
"github.com/mikeydub/go-gallery/service/tokenmanage"
"github.com/mikeydub/go-gallery/tokenprocessing"
Expand Down Expand Up @@ -172,14 +173,13 @@ func testSuggestedUsersForViewer(t *testing.T) {
userC := newUserFixture(t)
ctx := context.Background()
clients := server.ClientInit(ctx)
provider, cleanup := server.NewMultichainProvider(ctx, server.SetDefaults)
recommender := newStubRecommender(t, []persist.DBID{
userA.ID,
userB.ID,
userC.ID,
})
p := newStubPersonalization(t)
handler := server.CoreInit(ctx, clients, provider, recommender, p)
handler := server.CoreInit(ctx, clients, recommender, p)
c := customHandlerClient(t, handler, withJWTOpt(t, userF.ID))

response, err := viewerQuery(ctx, c)
Expand All @@ -190,7 +190,6 @@ func testSuggestedUsersForViewer(t *testing.T) {
assert.Len(t, suggested, 3)
t.Cleanup(func() {
clients.Close()
cleanup()
})
}

Expand Down Expand Up @@ -929,7 +928,7 @@ func testSyncNewTokensIncrementally(t *testing.T) {
func testSyncNewTokensMultichain(t *testing.T) {
userF := newUserFixture(t)
provider := defaultStubProvider(userF.Wallet.Address)
contract := multichain.ChainAgnosticContract{Address: "0x124", Descriptors: multichain.ChainAgnosticContractDescriptors{Name: "wow"}}
contract := common.ChainAgnosticContract{Address: "0x124", Descriptors: common.ChainAgnosticContractDescriptors{Name: "wow"}}
secondProvider := newStubProvider(withDummyTokenN(contract, userF.Wallet.Address, 10))
providers := multichain.ProviderLookup{persist.ChainETH: provider, persist.ChainOptimism: secondProvider}
h := handlerWithProviders(t, &noopSubmitter{}, providers)
Expand All @@ -946,7 +945,7 @@ func testSyncNewTokensMultichain(t *testing.T) {

func testSyncOnlySubmitsNewTokens(t *testing.T) {
userF := newUserFixture(t)
provider := newStubProvider(withDummyTokenN(multichain.ChainAgnosticContract{Address: "0xdead"}, userF.Wallet.Address, 10))
provider := newStubProvider(withDummyTokenN(common.ChainAgnosticContract{Address: "0xdead"}, userF.Wallet.Address, 10))
providers := multichain.ProviderLookup{persist.ChainETH: provider}
submitter := &recorderSubmitter{}
h := handlerWithProviders(t, submitter, providers)
Expand Down Expand Up @@ -988,7 +987,7 @@ func testSyncKeepsOldTokens(t *testing.T) {
userF := newUserWithTokensFixture(t)
initialTokensLen := len(userF.TokenIDs)
newTokensLen := 4
provider := newStubProvider(withDummyTokenN(multichain.ChainAgnosticContract{Address: "0x1337"}, userF.Wallet.Address, newTokensLen))
provider := newStubProvider(withDummyTokenN(common.ChainAgnosticContract{Address: "0x1337"}, userF.Wallet.Address, newTokensLen))
providers := multichain.ProviderLookup{persist.ChainETH: provider}
h := handlerWithProviders(t, &noopSubmitter{}, providers)
c := customHandlerClient(t, h, withJWTOpt(t, userF.ID))
Expand All @@ -1002,12 +1001,12 @@ func testSyncKeepsOldTokens(t *testing.T) {
func testSyncShouldMergeDuplicatesInProvider(t *testing.T) {
userF := newUserFixture(t)
token := dummyToken(userF.Wallet.Address)
contract := multichain.ChainAgnosticContract{Address: token.ContractAddress, Descriptors: multichain.ChainAgnosticContractDescriptors{
contract := common.ChainAgnosticContract{Address: token.ContractAddress, Descriptors: common.ChainAgnosticContractDescriptors{
Name: "someContract",
}}
provider := newStubProvider(
withContracts([]multichain.ChainAgnosticContract{contract}),
withTokens([]multichain.ChainAgnosticToken{token, token}),
withContracts([]common.ChainAgnosticContract{contract}),
withTokens([]common.ChainAgnosticToken{token, token}),
)
providers := multichain.ProviderLookup{persist.ChainETH: provider}
h := handlerWithProviders(t, &noopSubmitter{}, providers)
Expand Down Expand Up @@ -1602,18 +1601,18 @@ func defaultLayout() CollectionLayoutInput {
}

// dummyToken returns a dummy token owned by the provided address
func dummyToken(ownerAddress persist.Address) multichain.ChainAgnosticToken {
func dummyToken(ownerAddress persist.Address) common.ChainAgnosticToken {
return dummyTokenContract(ownerAddress, "0x123")
}

// dummyTokenContract returns a dummy token owned by the provided address from the provided contract
func dummyTokenContract(ownerAddress, contractAddress persist.Address) multichain.ChainAgnosticToken {
func dummyTokenContract(ownerAddress, contractAddress persist.Address) common.ChainAgnosticToken {
return dummyTokenIDContract(ownerAddress, contractAddress, "1")
}

// dummyTokenIDContract returns a dummy token owned by the provided address from the provided contract with the given tokenID
func dummyTokenIDContract(ownerAddress, contractAddress persist.Address, tokenID persist.HexTokenID) multichain.ChainAgnosticToken {
return multichain.ChainAgnosticToken{
func dummyTokenIDContract(ownerAddress, contractAddress persist.Address, tokenID persist.HexTokenID) common.ChainAgnosticToken {
return common.ChainAgnosticToken{
TokenID: tokenID,
Quantity: "1",
ContractAddress: contractAddress,
Expand All @@ -1634,13 +1633,10 @@ func defaultTokenSettings(tokens []persist.DBID) []CollectionTokenSettingsInput
func defaultHandler(t *testing.T) http.Handler {
ctx := context.Background()
c := server.ClientInit(ctx)
p, cleanup := server.NewMultichainProvider(ctx, server.SetDefaults)
r := newStubRecommender(t, []persist.DBID{})
pnl := newStubPersonalization(t)
handler := server.CoreInit(ctx, c, p, r, pnl)
handler := server.CoreInit(ctx, c, r, newStubPersonalization(t))
t.Cleanup(func() {
c.Close()
cleanup()
})
return handler
}
Expand All @@ -1651,7 +1647,7 @@ func handlerWithProviders(t *testing.T, submitter tokenmanage.Submitter, p multi
c := server.ClientInit(context.Background())
provider := newMultichainProvider(c, submitter, p)

Check failure on line 1648 in graphql/graphql_test.go

View workflow job for this annotation

GitHub Actions / core-tests

provider declared but not used

Check failure on line 1648 in graphql/graphql_test.go

View workflow job for this annotation

GitHub Actions / core-tests

provider declared but not used

Check failure on line 1648 in graphql/graphql_test.go

View workflow job for this annotation

GitHub Actions / syncing-tests

provider declared but not used

Check failure on line 1648 in graphql/graphql_test.go

View workflow job for this annotation

GitHub Actions / syncing-tests

provider declared but not used
t.Cleanup(c.Close)
return server.CoreInit(ctx, c, &provider, newStubRecommender(t, []persist.DBID{}), newStubPersonalization(t))
return server.CoreInit(ctx, c, newStubRecommender(t, []persist.DBID{}), newStubPersonalization(t))
}

// newMultichainProvider a new multichain provider configured with the given providers
Expand Down
40 changes: 20 additions & 20 deletions graphql/stub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"testing"
"time"

"github.com/mikeydub/go-gallery/service/multichain"
"github.com/mikeydub/go-gallery/service/multichain/common"
"github.com/mikeydub/go-gallery/service/persist"
"github.com/mikeydub/go-gallery/service/recommend"
"github.com/mikeydub/go-gallery/service/recommend/userpref"
Expand All @@ -24,18 +24,18 @@ import (

// stubProvider returns a canned set of tokens and contracts
type stubProvider struct {
Contracts []multichain.ChainAgnosticContract
Tokens []multichain.ChainAgnosticToken
Contracts []common.ChainAgnosticContract
Tokens []common.ChainAgnosticToken
FetchMetadata func() (persist.TokenMetadata, error)
RetErr error
}

func (p stubProvider) GetTokensByWalletAddress(ctx context.Context, address persist.Address) ([]multichain.ChainAgnosticToken, []multichain.ChainAgnosticContract, error) {
func (p stubProvider) GetTokensByWalletAddress(ctx context.Context, address persist.Address) ([]common.ChainAgnosticToken, []common.ChainAgnosticContract, error) {
return p.Tokens, p.Contracts, p.RetErr
}

func (p stubProvider) GetTokensIncrementallyByWalletAddress(ctx context.Context, address persist.Address) (<-chan multichain.ChainAgnosticTokensAndContracts, <-chan error) {
recCh := make(chan multichain.ChainAgnosticTokensAndContracts)
func (p stubProvider) GetTokensIncrementallyByWalletAddress(ctx context.Context, address persist.Address) (<-chan common.ChainAgnosticTokensAndContracts, <-chan error) {
recCh := make(chan common.ChainAgnosticTokensAndContracts)
errCh := make(chan error)
go func() {
defer close(recCh)
Expand All @@ -44,27 +44,27 @@ func (p stubProvider) GetTokensIncrementallyByWalletAddress(ctx context.Context,
errCh <- p.RetErr
return
}
recCh <- multichain.ChainAgnosticTokensAndContracts{
recCh <- common.ChainAgnosticTokensAndContracts{
Tokens: p.Tokens,
Contracts: p.Contracts,
}
}()
return recCh, errCh
}

func (p stubProvider) GetTokenMetadataByTokenIdentifiers(ctx context.Context, ti multichain.ChainAgnosticIdentifiers) (persist.TokenMetadata, error) {
func (p stubProvider) GetTokenMetadataByTokenIdentifiers(ctx context.Context, ti common.ChainAgnosticIdentifiers) (persist.TokenMetadata, error) {
return p.FetchMetadata()
}

func (p stubProvider) GetTokensByContractAddress(ctx context.Context, contract persist.Address, limit int, offset int) ([]multichain.ChainAgnosticToken, multichain.ChainAgnosticContract, error) {
func (p stubProvider) GetTokensByContractAddress(ctx context.Context, contract persist.Address, limit int, offset int) ([]common.ChainAgnosticToken, common.ChainAgnosticContract, error) {
panic("not implemented")
}

func (p stubProvider) GetTokensByContractAddressAndOwner(ctx context.Context, owner persist.Address, contract persist.Address, limit, offset int) ([]multichain.ChainAgnosticToken, multichain.ChainAgnosticContract, error) {
func (p stubProvider) GetTokensByContractAddressAndOwner(ctx context.Context, owner persist.Address, contract persist.Address, limit, offset int) ([]common.ChainAgnosticToken, common.ChainAgnosticContract, error) {
panic("not implemented")
}

func (p stubProvider) GetTokenByTokenIdentifiersAndOwner(context.Context, multichain.ChainAgnosticIdentifiers, persist.Address) (multichain.ChainAgnosticToken, multichain.ChainAgnosticContract, error) {
func (p stubProvider) GetTokenByTokenIdentifiersAndOwner(context.Context, common.ChainAgnosticIdentifiers, persist.Address) (common.ChainAgnosticToken, common.ChainAgnosticContract, error) {
panic("not implemented")
}

Expand All @@ -79,14 +79,14 @@ func newStubProvider(opts ...providerOpt) stubProvider {
}

// withContracts configures the stubProvider to return a canned set of contracts
func withContracts(contracts []multichain.ChainAgnosticContract) providerOpt {
func withContracts(contracts []common.ChainAgnosticContract) providerOpt {
return func(p *stubProvider) {
p.Contracts = contracts
}
}

// withTokens configures the stubProvider to return a canned set of tokens
func withTokens(tokens []multichain.ChainAgnosticToken) providerOpt {
func withTokens(tokens []common.ChainAgnosticToken) providerOpt {
return func(p *stubProvider) {
p.Tokens = tokens
}
Expand All @@ -100,26 +100,26 @@ func withReturnError(err error) providerOpt {
}

// withDummyTokenN will generate n dummy tokens from the provided contract
func withDummyTokenN(contract multichain.ChainAgnosticContract, ownerAddress persist.Address, n int) providerOpt {
func withDummyTokenN(contract common.ChainAgnosticContract, ownerAddress persist.Address, n int) providerOpt {
start := 1337
return func(p *stubProvider) {
tokens := []multichain.ChainAgnosticToken{}
tokens := []common.ChainAgnosticToken{}
for i := start; i < start+n; i++ {
tokenID := persist.DecimalTokenID(fmt.Sprint(i))
token := dummyTokenIDContract(ownerAddress, contract.Address, tokenID.ToHexTokenID())
tokens = append(tokens, token)
}
withContracts([]multichain.ChainAgnosticContract{contract})(p)
withContracts([]common.ChainAgnosticContract{contract})(p)
withTokens(tokens)(p)
}
}

// withDummyTokenID will generate a token with the provided token ID
func withDummyTokenID(ownerAddress persist.Address, tokenID persist.HexTokenID) providerOpt {
c := multichain.ChainAgnosticContract{Address: "0x123"}
c := common.ChainAgnosticContract{Address: "0x123"}
return func(p *stubProvider) {
withContracts([]multichain.ChainAgnosticContract{c})(p)
withTokens([]multichain.ChainAgnosticToken{dummyTokenIDContract(ownerAddress, c.Address, tokenID)})(p)
withContracts([]common.ChainAgnosticContract{c})(p)
withTokens([]common.ChainAgnosticToken{dummyTokenIDContract(ownerAddress, c.Address, tokenID)})(p)
}
}

Expand All @@ -132,7 +132,7 @@ func withFetchMetadata(f func() (persist.TokenMetadata, error)) providerOpt {

// defaultStubProvider returns a stubProvider that returns dummy tokens
func defaultStubProvider(ownerAddress persist.Address) stubProvider {
contract := multichain.ChainAgnosticContract{Address: "0x123", Descriptors: multichain.ChainAgnosticContractDescriptors{Name: "testContract"}}
contract := common.ChainAgnosticContract{Address: "0x123", Descriptors: common.ChainAgnosticContractDescriptors{Name: "testContract"}}
return newStubProvider(withDummyTokenN(contract, ownerAddress, 10))
}

Expand Down
32 changes: 16 additions & 16 deletions publicapi/publicapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,11 @@ import (
const apiContextKey = "publicapi.api"

type PublicAPI struct {
repos *postgres.Repositories
queries *db.Queries
loaders *dataloader.Loaders
validator *validator.Validate
APQ *apq.APQCache

repos *postgres.Repositories
queries *db.Queries
loaders *dataloader.Loaders
validator *validator.Validate
APQ *apq.APQCache
Auth *AuthAPI
Collection *CollectionAPI
Gallery *GalleryAPI
Expand All @@ -67,22 +66,23 @@ type PublicAPI struct {
Mint *MintAPI
}

func New(ctx context.Context, disableDataloaderCaching bool, repos *postgres.Repositories, queries *db.Queries, httpClient *http.Client, ethClient *ethclient.Client, ipfsClient *shell.Shell,
arweaveClient *goar.Client, storageClient *storage.Client, taskClient *task.Client, throttler *throttle.Locker, secrets *secretmanager.Client, apq *apq.APQCache, feedCache, socialCache, authRefreshCache, tokenManageCache, oneTimeLoginCache *redis.Cache, magicClient *magicclient.API, neynar *farcaster.NeynarAPI, mintLimiter *limiters.KeyRateLimiter) *PublicAPI {
func New(ctx context.Context, disableDataloaderCaching bool, repos *postgres.Repositories, queries *db.Queries, httpClient *http.Client, ethClient *ethclient.Client, ipfsClient *shell.Shell, arweaveClient *goar.Client, storageClient *storage.Client, taskClient *task.Client, throttler *throttle.Locker, secrets *secretmanager.Client, apq *apq.APQCache, feedCache, socialCache, authRefreshCache, tokenManageCache, oneTimeLoginCache *redis.Cache, magicClient *magicclient.API, neynar *farcaster.NeynarAPI, mintLimiter *limiters.KeyRateLimiter) *PublicAPI {
multichainProvider := multichain.NewMultichainProvider(ctx, repos, queries)
return NewWithMultichainProvider(ctx, disableDataloaderCaching, repos, queries, httpClient, ethClient, ipfsClient, arweaveClient, storageClient, taskClient, throttler, secrets, apq, feedCache, socialCache, authRefreshCache, tokenManageCache, oneTimeLoginCache, magicClient, neynar, mintLimiter, multichainProvider)
}

func NewWithMultichainProvider(ctx context.Context, disableDataloaderCaching bool, repos *postgres.Repositories, queries *db.Queries, httpClient *http.Client, ethClient *ethclient.Client, ipfsClient *shell.Shell, arweaveClient *goar.Client, storageClient *storage.Client, taskClient *task.Client, throttler *throttle.Locker, secrets *secretmanager.Client, apq *apq.APQCache, feedCache, socialCache, authRefreshCache, tokenManageCache, oneTimeLoginCache *redis.Cache, magicClient *magicclient.API, neynar *farcaster.NeynarAPI, mintLimiter *limiters.KeyRateLimiter, multichainProvider *multichain.Provider) *PublicAPI {
loaders := dataloader.NewLoaders(ctx, queries, disableDataloaderCaching, tracing.DataloaderPreFetchHook, tracing.DataloaderPostFetchHook)
validator := validate.WithCustomValidators()
tokenManager := tokenmanage.New(ctx, taskClient, tokenManageCache, nil)
privyClient := privy.NewPrivyClient(httpClient)
highlightProvider := highlight.NewProvider(httpClient)
multichainProvider := multichain.NewMultichainProvider(ctx, repos, queries)

return &PublicAPI{
repos: repos,
queries: queries,
loaders: loaders,
validator: validator,
APQ: apq,

repos: repos,
queries: queries,
loaders: loaders,
validator: validator,
APQ: apq,
Auth: &AuthAPI{repos: repos, queries: queries, loaders: loaders, validator: validator, ethClient: ethClient, multiChainProvider: multichainProvider, magicLinkClient: magicClient, oneTimeLoginCache: oneTimeLoginCache, authRefreshCache: authRefreshCache, privyClient: privyClient, neynarClient: neynar},
Collection: &CollectionAPI{repos: repos, queries: queries, loaders: loaders, validator: validator, ethClient: ethClient},
Gallery: &GalleryAPI{repos: repos, queries: queries, loaders: loaders, validator: validator, ethClient: ethClient},
Expand Down
Loading

0 comments on commit 1a6a2fd

Please sign in to comment.