diff --git a/_examples/indexer/main.go b/_examples/indexer/main.go new file mode 100644 index 00000000..02e0aebe --- /dev/null +++ b/_examples/indexer/main.go @@ -0,0 +1,28 @@ +package main + +import ( + "context" + "log" + + "github.com/0xsequence/go-sequence/indexer" + "github.com/davecgh/go-spew/spew" +) + +func main() { + seqIndexer := indexer.NewIndexer("https://polygon-indexer.sequence.app", "kzk9Hdk0EJRoj9qgHWncKtHAAAAAAAAA") + + accountAddress := "0x8e3E38fe7367dd3b52D1e281E4e8400447C8d8B9" + includeMetadata := true + metadataOptions := indexer.MetadataOptions{ + VerifiedOnly: true, + } + + _, tokenBalances, err := seqIndexer.GetTokenBalances(context.Background(), &accountAddress, nil, nil, &includeMetadata, &metadataOptions, nil) + if err != nil { + log.Fatal(err) + } + + for _, tokenBalance := range tokenBalances { + spew.Dump(tokenBalance) + } +} diff --git a/_examples/metadata/main.go b/_examples/metadata/main.go new file mode 100644 index 00000000..66fc7486 --- /dev/null +++ b/_examples/metadata/main.go @@ -0,0 +1,25 @@ +package main + +import ( + "context" + "log" + + "github.com/0xsequence/go-sequence/metadata" + "github.com/davecgh/go-spew/spew" +) + +func main() { + seqMetadata := metadata.NewMetadata("kzk9Hdk0EJRoj9qgHWncKtHAAAAAAAAA") + + contractInfo, err := seqMetadata.GetContractInfo(context.Background(), "polygon", "0x631998e91476DA5B870D741192fc5Cbc55F5a52E") + if err != nil { + log.Fatal(err) + } + spew.Dump(contractInfo) + + collectibleInfo, err := seqMetadata.GetTokenMetadata(context.Background(), "polygon", "0x631998e91476DA5B870D741192fc5Cbc55F5a52E", []string{"1", "2"}) + if err != nil { + log.Fatal(err) + } + spew.Dump(collectibleInfo) +} diff --git a/api/api.go b/api/api.go new file mode 100644 index 00000000..4cbf179d --- /dev/null +++ b/api/api.go @@ -0,0 +1,52 @@ +package api + +import ( + "fmt" + "net/http" +) + +type Options struct { + HTTPClient HTTPClient + JWTAuthToken string + APIServiceURL string +} + +func NewAPI(projectAccessKey string, options ...Options) API { + opts := Options{} + if len(options) > 0 { + opts = options[0] + } + + client := &httpclient{ + client: opts.HTTPClient, + projectAccessKey: projectAccessKey, + } + if opts.HTTPClient == nil { + client.client = http.DefaultClient + } + if opts.JWTAuthToken != "" { + client.jwtAuthHeader = fmt.Sprintf("BEARER %s", opts.JWTAuthToken) + } + + apiServiceURL := "https://api.sequence.app" + if opts.APIServiceURL != "" { + apiServiceURL = opts.APIServiceURL + } + return NewAPIClient(apiServiceURL, client) +} + +type httpclient struct { + client HTTPClient + jwtAuthHeader string + projectAccessKey string +} + +func (c *httpclient) Do(req *http.Request) (*http.Response, error) { + if c.projectAccessKey != "" { + req.Header.Set("X-Access-Key", c.projectAccessKey) + } + if c.jwtAuthHeader != "" { + req.Header.Set("Authorization", c.jwtAuthHeader) + } + return c.client.Do(req) +} diff --git a/indexer/indexer.go b/indexer/indexer.go new file mode 100644 index 00000000..95da63ae --- /dev/null +++ b/indexer/indexer.go @@ -0,0 +1,47 @@ +package indexer + +import ( + "fmt" + "net/http" +) + +type Options struct { + HTTPClient HTTPClient + JWTAuthToken string +} + +func NewIndexer(indexerServiceURL string, projectAccessKey string, options ...Options) Indexer { + opts := Options{} + if len(options) > 0 { + opts = options[0] + } + + client := &httpclient{ + client: opts.HTTPClient, + projectAccessKey: projectAccessKey, + } + if opts.HTTPClient == nil { + client.client = http.DefaultClient + } + if opts.JWTAuthToken != "" { + client.jwtAuthHeader = fmt.Sprintf("BEARER %s", opts.JWTAuthToken) + } + + return NewIndexerClient(indexerServiceURL, client) +} + +type httpclient struct { + client HTTPClient + jwtAuthHeader string + projectAccessKey string +} + +func (c *httpclient) Do(req *http.Request) (*http.Response, error) { + if c.projectAccessKey != "" { + req.Header.Set("X-Access-Key", c.projectAccessKey) + } + if c.jwtAuthHeader != "" { + req.Header.Set("Authorization", c.jwtAuthHeader) + } + return c.client.Do(req) +} diff --git a/metadata/metadata.go b/metadata/metadata.go new file mode 100644 index 00000000..a1a76630 --- /dev/null +++ b/metadata/metadata.go @@ -0,0 +1,52 @@ +package metadata + +import ( + "fmt" + "net/http" +) + +type Options struct { + HTTPClient HTTPClient + JWTAuthToken string + MetadataServiceURL string +} + +func NewMetadata(projectAccessKey string, options ...Options) Metadata { + opts := Options{} + if len(options) > 0 { + opts = options[0] + } + + client := &httpclient{ + client: opts.HTTPClient, + projectAccessKey: projectAccessKey, + } + if opts.HTTPClient == nil { + client.client = http.DefaultClient + } + if opts.JWTAuthToken != "" { + client.jwtAuthHeader = fmt.Sprintf("BEARER %s", opts.JWTAuthToken) + } + + metadataServiceURL := "https://metadata.sequence.app" + if opts.MetadataServiceURL != "" { + metadataServiceURL = opts.MetadataServiceURL + } + return NewMetadataClient(metadataServiceURL, client) +} + +type httpclient struct { + client HTTPClient + jwtAuthHeader string + projectAccessKey string +} + +func (c *httpclient) Do(req *http.Request) (*http.Response, error) { + if c.projectAccessKey != "" { + req.Header.Set("X-Access-Key", c.projectAccessKey) + } + if c.jwtAuthHeader != "" { + req.Header.Set("Authorization", c.jwtAuthHeader) + } + return c.client.Do(req) +} diff --git a/relayer/proto/relayer.go b/relayer/proto/relayer.go new file mode 100644 index 00000000..fa604fe5 --- /dev/null +++ b/relayer/proto/relayer.go @@ -0,0 +1,47 @@ +package proto + +import ( + "fmt" + "net/http" +) + +type Options struct { + HTTPClient HTTPClient + JWTAuthToken string +} + +func NewRelayer(relayerServiceURL string, projectAccessKey string, options ...Options) Relayer { + opts := Options{} + if len(options) > 0 { + opts = options[0] + } + + client := &httpclient{ + client: opts.HTTPClient, + projectAccessKey: projectAccessKey, + } + if opts.HTTPClient == nil { + client.client = http.DefaultClient + } + if opts.JWTAuthToken != "" { + client.jwtAuthHeader = fmt.Sprintf("BEARER %s", opts.JWTAuthToken) + } + + return NewRelayerClient(relayerServiceURL, client) +} + +type httpclient struct { + client HTTPClient + jwtAuthHeader string + projectAccessKey string +} + +func (c *httpclient) Do(req *http.Request) (*http.Response, error) { + if c.projectAccessKey != "" { + req.Header.Set("X-Access-Key", c.projectAccessKey) + } + if c.jwtAuthHeader != "" { + req.Header.Set("Authorization", c.jwtAuthHeader) + } + return c.client.Do(req) +}