Skip to content

Commit

Permalink
Move to multichain
Browse files Browse the repository at this point in the history
  • Loading branch information
jarrel-b committed Apr 29, 2024
1 parent 7077eea commit 96c2af6
Show file tree
Hide file tree
Showing 18 changed files with 1,182 additions and 1,181 deletions.
457 changes: 0 additions & 457 deletions server/inject.go

This file was deleted.

110 changes: 55 additions & 55 deletions service/multichain/alchemy/alchemy.go

Large diffs are not rendered by default.

112 changes: 112 additions & 0 deletions service/multichain/common/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package common

import (
"context"
"fmt"

"github.com/mikeydub/go-gallery/service/persist"
)

// ChainAgnosticIdentifiers identify tokens despite their chain
type ChainAgnosticIdentifiers struct {
ContractAddress persist.Address `json:"contract_address"`
TokenID persist.HexTokenID `json:"token_id"`
}

func (t ChainAgnosticIdentifiers) String() string {
return fmt.Sprintf("token(address=%s, tokenID=%s)", t.ContractAddress, t.TokenID.ToDecimalTokenID())
}

// Verifier can verify that a signature is signed by a given key
type Verifier interface {
VerifySignature(ctx context.Context, pubKey persist.PubKey, walletType persist.WalletType, nonce string, sig string) (bool, error)
}

type TokenIdentifierOwnerFetcher interface {
GetTokenByTokenIdentifiersAndOwner(context.Context, ChainAgnosticIdentifiers, persist.Address) (ChainAgnosticToken, ChainAgnosticContract, error)
}

type TokensByContractWalletFetcher interface {
GetTokensByContractWallet(ctx context.Context, contract persist.ChainAddress, wallet persist.Address) ([]ChainAgnosticToken, ChainAgnosticContract, error)
}

type TokensByTokenIdentifiersFetcher interface {
GetTokensByTokenIdentifiers(context.Context, ChainAgnosticIdentifiers) ([]ChainAgnosticToken, ChainAgnosticContract, error)
}

// TokensIncrementalOwnerFetcher supports fetching tokens for syncing incrementally
type TokensIncrementalOwnerFetcher interface {
// NOTE: implementation MUST close the rec channel
GetTokensIncrementallyByWalletAddress(ctx context.Context, address persist.Address) (<-chan ChainAgnosticTokensAndContracts, <-chan error)
}

// TokensIncrementalContractFetcher supports fetching tokens by contract for syncing incrementally
type TokensIncrementalContractFetcher interface {
// NOTE: implementations MUST close the rec channel
// maxLimit is not for pagination, it is to make sure we don't fetch a bajilion tokens from an omnibus contract
GetTokensIncrementallyByContractAddress(ctx context.Context, address persist.Address, maxLimit int) (<-chan ChainAgnosticTokensAndContracts, <-chan error)
}

type ContractFetcher interface {
GetContractByAddress(ctx context.Context, contract persist.Address) (ChainAgnosticContract, error)
}

type ContractsCreatorFetcher interface {
GetContractsByCreatorAddress(ctx context.Context, owner persist.Address) ([]ChainAgnosticContract, error)
}

// TokenMetadataFetcher supports fetching token metadata
type TokenMetadataFetcher interface {
GetTokenMetadataByTokenIdentifiers(ctx context.Context, ti ChainAgnosticIdentifiers) (persist.TokenMetadata, error)
}

type TokenMetadataBatcher interface {
GetTokenMetadataByTokenIdentifiersBatch(ctx context.Context, tIDs []ChainAgnosticIdentifiers) ([]persist.TokenMetadata, error)
}

type TokenDescriptorsFetcher interface {
GetTokenDescriptorsByTokenIdentifiers(ctx context.Context, ti ChainAgnosticIdentifiers) (ChainAgnosticTokenDescriptors, ChainAgnosticContractDescriptors, error)
}

// ChainAgnosticToken is a token that is agnostic to the chain it is on
type ChainAgnosticToken struct {
Descriptors ChainAgnosticTokenDescriptors `json:"descriptors"`
TokenType persist.TokenType `json:"token_type"`
TokenURI persist.TokenURI `json:"token_uri"`
TokenID persist.HexTokenID `json:"token_id"`
Quantity persist.HexString `json:"quantity"`
OwnerAddress persist.Address `json:"owner_address"`
TokenMetadata persist.TokenMetadata `json:"metadata"`
ContractAddress persist.Address `json:"contract_address"`
FallbackMedia persist.FallbackMedia `json:"fallback_media"`
ExternalURL string `json:"external_url"`
BlockNumber persist.BlockNumber `json:"block_number"`
IsSpam *bool `json:"is_spam"`
}

// ChainAgnosticContract is a contract that is agnostic to the chain it is on
type ChainAgnosticContract struct {
Descriptors ChainAgnosticContractDescriptors `json:"descriptors"`
Address persist.Address `json:"address"`
IsSpam *bool `json:"is_spam"`
LatestBlock persist.BlockNumber `json:"latest_block"`
}

type ChainAgnosticTokensAndContracts struct {
Tokens []ChainAgnosticToken `json:"tokens"`
Contracts []ChainAgnosticContract `json:"contracts"`
}

// ChainAgnosticTokenDescriptors are the fields that describe a token but cannot be used to uniquely identify it
type ChainAgnosticTokenDescriptors struct {
Name string `json:"name"`
Description string `json:"description"`
}

type ChainAgnosticContractDescriptors struct {
Symbol string `json:"symbol"`
Name string `json:"name"`
Description string `json:"description"`
ProfileImageURL string `json:"profile_image_url"`
OwnerAddress persist.Address `json:"owner_address"`
}
153 changes: 77 additions & 76 deletions service/multichain/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package multichain

import (
"github.com/mikeydub/go-gallery/service/multichain/common"
"github.com/mikeydub/go-gallery/service/persist"
)

Expand All @@ -18,101 +19,101 @@ type ChainProvider struct {
}

type EthereumProvider struct {
ContractFetcher
ContractsCreatorFetcher
TokenDescriptorsFetcher
TokenIdentifierOwnerFetcher
TokenMetadataBatcher
TokenMetadataFetcher
TokensByContractWalletFetcher
TokensByTokenIdentifiersFetcher
TokensIncrementalContractFetcher
TokensIncrementalOwnerFetcher
Verifier
common.ContractFetcher
common.ContractsCreatorFetcher
common.TokenDescriptorsFetcher
common.TokenIdentifierOwnerFetcher
common.TokenMetadataBatcher
common.TokenMetadataFetcher
common.TokensByContractWalletFetcher
common.TokensByTokenIdentifiersFetcher
common.TokensIncrementalContractFetcher
common.TokensIncrementalOwnerFetcher
common.Verifier
}

type TezosProvider struct {
ContractFetcher
ContractsCreatorFetcher
TokenDescriptorsFetcher
TokenIdentifierOwnerFetcher
TokenMetadataBatcher
TokenMetadataFetcher
TokensByContractWalletFetcher
TokensByTokenIdentifiersFetcher
TokensIncrementalContractFetcher
TokensIncrementalOwnerFetcher
Verifier
common.ContractFetcher
common.ContractsCreatorFetcher
common.TokenDescriptorsFetcher
common.TokenIdentifierOwnerFetcher
common.TokenMetadataBatcher
common.TokenMetadataFetcher
common.TokensByContractWalletFetcher
common.TokensByTokenIdentifiersFetcher
common.TokensIncrementalContractFetcher
common.TokensIncrementalOwnerFetcher
common.Verifier
}

type OptimismProvider struct {
ContractFetcher
ContractsCreatorFetcher
TokenDescriptorsFetcher
TokenIdentifierOwnerFetcher
TokenMetadataBatcher
TokenMetadataFetcher
TokensByContractWalletFetcher
TokensByTokenIdentifiersFetcher
TokensIncrementalContractFetcher
TokensIncrementalOwnerFetcher
common.ContractFetcher
common.ContractsCreatorFetcher
common.TokenDescriptorsFetcher
common.TokenIdentifierOwnerFetcher
common.TokenMetadataBatcher
common.TokenMetadataFetcher
common.TokensByContractWalletFetcher
common.TokensByTokenIdentifiersFetcher
common.TokensIncrementalContractFetcher
common.TokensIncrementalOwnerFetcher
}

type ArbitrumProvider struct {
ContractFetcher
ContractsCreatorFetcher
TokenDescriptorsFetcher
TokenIdentifierOwnerFetcher
TokenMetadataBatcher
TokenMetadataFetcher
TokensByContractWalletFetcher
TokensByTokenIdentifiersFetcher
TokensIncrementalContractFetcher
TokensIncrementalOwnerFetcher
common.ContractFetcher
common.ContractsCreatorFetcher
common.TokenDescriptorsFetcher
common.TokenIdentifierOwnerFetcher
common.TokenMetadataBatcher
common.TokenMetadataFetcher
common.TokensByContractWalletFetcher
common.TokensByTokenIdentifiersFetcher
common.TokensIncrementalContractFetcher
common.TokensIncrementalOwnerFetcher
}

type PoapProvider struct {
TokenDescriptorsFetcher
TokenMetadataFetcher
TokensIncrementalOwnerFetcher
TokenIdentifierOwnerFetcher
common.TokenDescriptorsFetcher
common.TokenMetadataFetcher
common.TokensIncrementalOwnerFetcher
common.TokenIdentifierOwnerFetcher
}

type ZoraProvider struct {
ContractFetcher
ContractsCreatorFetcher
TokenDescriptorsFetcher
TokenIdentifierOwnerFetcher
TokenMetadataBatcher
TokenMetadataFetcher
TokensByContractWalletFetcher
TokensByTokenIdentifiersFetcher
TokensIncrementalContractFetcher
TokensIncrementalOwnerFetcher
common.ContractFetcher
common.ContractsCreatorFetcher
common.TokenDescriptorsFetcher
common.TokenIdentifierOwnerFetcher
common.TokenMetadataBatcher
common.TokenMetadataFetcher
common.TokensByContractWalletFetcher
common.TokensByTokenIdentifiersFetcher
common.TokensIncrementalContractFetcher
common.TokensIncrementalOwnerFetcher
}

type BaseProvider struct {
ContractFetcher
ContractsCreatorFetcher
TokenDescriptorsFetcher
TokenIdentifierOwnerFetcher
TokenMetadataBatcher
TokenMetadataFetcher
TokensByContractWalletFetcher
TokensByTokenIdentifiersFetcher
TokensIncrementalContractFetcher
TokensIncrementalOwnerFetcher
common.ContractFetcher
common.ContractsCreatorFetcher
common.TokenDescriptorsFetcher
common.TokenIdentifierOwnerFetcher
common.TokenMetadataBatcher
common.TokenMetadataFetcher
common.TokensByContractWalletFetcher
common.TokensByTokenIdentifiersFetcher
common.TokensIncrementalContractFetcher
common.TokensIncrementalOwnerFetcher
}

type PolygonProvider struct {
ContractFetcher
ContractsCreatorFetcher
TokenDescriptorsFetcher
TokenIdentifierOwnerFetcher
TokenMetadataBatcher
TokenMetadataFetcher
TokensByContractWalletFetcher
TokensByTokenIdentifiersFetcher
TokensIncrementalContractFetcher
TokensIncrementalOwnerFetcher
common.ContractFetcher
common.ContractsCreatorFetcher
common.TokenDescriptorsFetcher
common.TokenIdentifierOwnerFetcher
common.TokenMetadataBatcher
common.TokenMetadataFetcher
common.TokensByContractWalletFetcher
common.TokensByTokenIdentifiersFetcher
common.TokensIncrementalContractFetcher
common.TokensIncrementalOwnerFetcher
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package multichain
package custom

import (
"bytes"
Expand All @@ -15,7 +15,7 @@ import (

svg "github.com/ajstarks/svgo"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/everFinance/goar"
shell "github.com/ipfs/go-ipfs-api"
Expand All @@ -25,6 +25,7 @@ import (
"github.com/mikeydub/go-gallery/service/eth"
"github.com/mikeydub/go-gallery/service/logger"
"github.com/mikeydub/go-gallery/service/media"
"github.com/mikeydub/go-gallery/service/multichain/common"
"github.com/mikeydub/go-gallery/service/persist"
"github.com/mikeydub/go-gallery/service/rpc"
"github.com/mikeydub/go-gallery/util"
Expand Down Expand Up @@ -81,15 +82,15 @@ func (c *CustomMetadataHandlers) HandlerFor(t persist.TokenIdentifiers) metadata
}
}

func (c *CustomMetadataHandlers) AddToToken(ctx context.Context, chain persist.Chain, t ChainAgnosticToken) ChainAgnosticToken {
tID := ChainAgnosticIdentifiers{ContractAddress: t.ContractAddress, TokenID: t.TokenID}
func (c *CustomMetadataHandlers) AddToToken(ctx context.Context, chain persist.Chain, t common.ChainAgnosticToken) common.ChainAgnosticToken {
tID := common.ChainAgnosticIdentifiers{ContractAddress: t.ContractAddress, TokenID: t.TokenID}
m := c.Load(ctx, chain, tID, t.TokenMetadata)
t.TokenMetadata = m
return t
}

func (c *CustomMetadataHandlers) AddToPage(ctx context.Context, chain persist.Chain, recCh <-chan ChainAgnosticTokensAndContracts, errIn <-chan error) (<-chan ChainAgnosticTokensAndContracts, <-chan error) {
outCh := make(chan ChainAgnosticTokensAndContracts, 2*10)
func (c *CustomMetadataHandlers) AddToPage(ctx context.Context, chain persist.Chain, recCh <-chan common.ChainAgnosticTokensAndContracts, errIn <-chan error) (<-chan common.ChainAgnosticTokensAndContracts, <-chan error) {
outCh := make(chan common.ChainAgnosticTokensAndContracts, 2*10)
errOut := make(chan error)
go func() {
defer close(outCh)
Expand All @@ -116,7 +117,7 @@ func (c *CustomMetadataHandlers) AddToPage(ctx context.Context, chain persist.Ch
return outCh, errOut
}

func (c *CustomMetadataHandlers) Load(ctx context.Context, chain persist.Chain, t ChainAgnosticIdentifiers, oldMetadata ...persist.TokenMetadata) persist.TokenMetadata {
func (c *CustomMetadataHandlers) Load(ctx context.Context, chain persist.Chain, t common.ChainAgnosticIdentifiers, oldMetadata ...persist.TokenMetadata) persist.TokenMetadata {
tID := persist.NewTokenIdentifiers(t.ContractAddress, t.TokenID, chain)
h := c.HandlerFor(tID)
if h == nil {
Expand All @@ -130,12 +131,12 @@ func (c *CustomMetadataHandlers) Load(ctx context.Context, chain persist.Chain,
return m
}

func (c *CustomMetadataHandlers) LoadMetadataAll(ctx context.Context, chain persist.Chain, tokens []ChainAgnosticToken) []persist.TokenMetadata {
func (c *CustomMetadataHandlers) LoadMetadataAll(ctx context.Context, chain persist.Chain, tokens []common.ChainAgnosticToken) []persist.TokenMetadata {
tokens = c.LoadAll(ctx, chain, tokens)
return util.MapWithoutError(tokens, func(t ChainAgnosticToken) persist.TokenMetadata { return t.TokenMetadata })
return util.MapWithoutError(tokens, func(t common.ChainAgnosticToken) persist.TokenMetadata { return t.TokenMetadata })
}

func (c *CustomMetadataHandlers) LoadAll(ctx context.Context, chain persist.Chain, tokens []ChainAgnosticToken) []ChainAgnosticToken {
func (c *CustomMetadataHandlers) LoadAll(ctx context.Context, chain persist.Chain, tokens []common.ChainAgnosticToken) []common.ChainAgnosticToken {
for i, t := range tokens {
tokens[i] = c.AddToToken(ctx, chain, t)
}
Expand Down Expand Up @@ -517,7 +518,7 @@ func newEnsHandler() metadataHandler {

func newCryptopunkHandler(ethClient *ethclient.Client) metadataHandler {
return func(ctx context.Context, t persist.TokenIdentifiers, _ ...persist.TokenMetadata) (persist.TokenMetadata, error) {
dataContract, err := contracts.NewCryptopunksDataCaller(common.HexToAddress("0x16f5a35647d6f03d5d3da7b35409d65ba03af3b2"), ethClient)
dataContract, err := contracts.NewCryptopunksDataCaller(ethcommon.HexToAddress("0x16f5a35647d6f03d5d3da7b35409d65ba03af3b2"), ethClient)
if err != nil {
return persist.TokenMetadata{}, err
}
Expand All @@ -542,7 +543,7 @@ func newCryptopunkHandler(ethClient *ethclient.Client) metadataHandler {

func newZoraHandler(ethClient *ethclient.Client, ipfsClient *shell.Shell, arweaveClient *goar.Client) metadataHandler {
return func(ctx context.Context, t persist.TokenIdentifiers, _ ...persist.TokenMetadata) (persist.TokenMetadata, error) {
metadataContract, err := contracts.NewZoraCaller(common.HexToAddress(t.ContractAddress.String()), ethClient)
metadataContract, err := contracts.NewZoraCaller(ethcommon.HexToAddress(t.ContractAddress.String()), ethClient)
if err != nil {
return persist.TokenMetadata{}, err
}
Expand Down
Loading

0 comments on commit 96c2af6

Please sign in to comment.