2023-04-17 11:42:01 +00:00
|
|
|
package alchemy
|
|
|
|
|
|
|
|
import (
|
2023-11-14 17:16:39 +00:00
|
|
|
"context"
|
2023-04-17 11:42:01 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2023-08-03 12:24:23 +00:00
|
|
|
"strings"
|
2023-04-17 11:42:01 +00:00
|
|
|
"time"
|
|
|
|
|
2023-10-06 12:31:54 +00:00
|
|
|
"github.com/cenkalti/backoff/v4"
|
|
|
|
|
2023-04-17 11:42:01 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2023-10-03 18:53:36 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2023-10-06 12:31:54 +00:00
|
|
|
|
2023-04-18 14:33:59 +00:00
|
|
|
walletCommon "github.com/status-im/status-go/services/wallet/common"
|
2023-09-22 13:18:42 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/connection"
|
2023-04-17 11:42:01 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/thirdparty"
|
|
|
|
)
|
|
|
|
|
2023-08-03 12:24:23 +00:00
|
|
|
const nftMetadataBatchLimit = 100
|
|
|
|
const contractMetadataBatchLimit = 100
|
2023-07-31 23:34:53 +00:00
|
|
|
|
2023-07-18 15:01:53 +00:00
|
|
|
func getBaseURL(chainID walletCommon.ChainID) (string, error) {
|
|
|
|
switch uint64(chainID) {
|
2023-04-18 14:33:59 +00:00
|
|
|
case walletCommon.EthereumMainnet:
|
2023-04-17 11:42:01 +00:00
|
|
|
return "https://eth-mainnet.g.alchemy.com", nil
|
2023-04-18 14:33:59 +00:00
|
|
|
case walletCommon.EthereumGoerli:
|
2023-04-17 11:42:01 +00:00
|
|
|
return "https://eth-goerli.g.alchemy.com", nil
|
2023-04-18 14:33:59 +00:00
|
|
|
case walletCommon.EthereumSepolia:
|
2023-04-17 11:42:01 +00:00
|
|
|
return "https://eth-sepolia.g.alchemy.com", nil
|
2023-04-18 14:33:59 +00:00
|
|
|
case walletCommon.OptimismMainnet:
|
2023-04-17 11:42:01 +00:00
|
|
|
return "https://opt-mainnet.g.alchemy.com", nil
|
2023-04-18 14:33:59 +00:00
|
|
|
case walletCommon.OptimismGoerli:
|
2023-04-17 11:42:01 +00:00
|
|
|
return "https://opt-goerli.g.alchemy.com", nil
|
2023-12-04 13:37:41 +00:00
|
|
|
case walletCommon.OptimismSepolia:
|
|
|
|
return "https://opt-sepolia.g.alchemy.com", nil
|
2023-04-18 14:33:59 +00:00
|
|
|
case walletCommon.ArbitrumMainnet:
|
2023-04-17 11:42:01 +00:00
|
|
|
return "https://arb-mainnet.g.alchemy.com", nil
|
2023-04-18 14:33:59 +00:00
|
|
|
case walletCommon.ArbitrumGoerli:
|
2023-04-17 11:42:01 +00:00
|
|
|
return "https://arb-goerli.g.alchemy.com", nil
|
2023-11-16 20:46:19 +00:00
|
|
|
case walletCommon.ArbitrumSepolia:
|
|
|
|
return "https://arb-sepolia.g.alchemy.com", nil
|
2023-04-17 11:42:01 +00:00
|
|
|
}
|
|
|
|
|
2023-07-31 19:41:14 +00:00
|
|
|
return "", thirdparty.ErrChainIDNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Client) ID() string {
|
2023-07-31 23:34:53 +00:00
|
|
|
return AlchemyID
|
2023-07-31 19:41:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Client) IsChainSupported(chainID walletCommon.ChainID) bool {
|
|
|
|
_, err := getBaseURL(chainID)
|
|
|
|
return err == nil
|
2023-04-17 11:42:01 +00:00
|
|
|
}
|
|
|
|
|
2023-09-22 13:18:42 +00:00
|
|
|
func (o *Client) IsConnected() bool {
|
|
|
|
return o.connectionStatus.IsConnected()
|
|
|
|
}
|
|
|
|
|
2023-04-17 11:42:01 +00:00
|
|
|
func getAPIKeySubpath(apiKey string) string {
|
|
|
|
if apiKey == "" {
|
|
|
|
return "demo"
|
|
|
|
}
|
|
|
|
return apiKey
|
|
|
|
}
|
|
|
|
|
2023-07-18 15:01:53 +00:00
|
|
|
func getNFTBaseURL(chainID walletCommon.ChainID, apiKey string) (string, error) {
|
2023-04-17 11:42:01 +00:00
|
|
|
baseURL, err := getBaseURL(chainID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2023-08-01 23:16:57 +00:00
|
|
|
return fmt.Sprintf("%s/nft/v3/%s", baseURL, getAPIKeySubpath(apiKey)), nil
|
2023-04-17 11:42:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Client struct {
|
2023-07-05 09:33:48 +00:00
|
|
|
thirdparty.CollectibleContractOwnershipProvider
|
2023-09-22 13:18:42 +00:00
|
|
|
client *http.Client
|
|
|
|
apiKeys map[uint64]string
|
|
|
|
connectionStatus *connection.Status
|
2023-04-17 11:42:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewClient(apiKeys map[uint64]string) *Client {
|
2023-10-03 18:53:36 +00:00
|
|
|
for _, chainID := range walletCommon.AllChainIDs() {
|
|
|
|
if apiKeys[uint64(chainID)] == "" {
|
|
|
|
log.Warn("Alchemy API key not available for", "chainID", chainID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-17 11:42:01 +00:00
|
|
|
return &Client{
|
2023-09-22 13:18:42 +00:00
|
|
|
client: &http.Client{Timeout: time.Minute},
|
|
|
|
apiKeys: apiKeys,
|
|
|
|
connectionStatus: connection.NewStatus(),
|
2023-04-17 11:42:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-14 17:16:39 +00:00
|
|
|
func (o *Client) doQuery(ctx context.Context, url string) (*http.Response, error) {
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
2023-04-17 11:42:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-10-06 12:31:54 +00:00
|
|
|
return o.doWithRetries(req)
|
2023-04-17 11:42:01 +00:00
|
|
|
}
|
|
|
|
|
2023-11-14 17:16:39 +00:00
|
|
|
func (o *Client) doPostWithJSON(ctx context.Context, url string, payload any) (*http.Response, error) {
|
2023-08-03 12:24:23 +00:00
|
|
|
payloadJSON, err := json.Marshal(payload)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
payloadString := string(payloadJSON)
|
|
|
|
payloadReader := strings.NewReader(payloadString)
|
|
|
|
|
2023-11-14 17:16:39 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, "POST", url, payloadReader)
|
2023-08-03 12:24:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Add("accept", "application/json")
|
|
|
|
req.Header.Add("content-type", "application/json")
|
|
|
|
|
2023-10-06 12:31:54 +00:00
|
|
|
return o.doWithRetries(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Client) doWithRetries(req *http.Request) (*http.Response, error) {
|
|
|
|
b := backoff.ExponentialBackOff{
|
|
|
|
InitialInterval: time.Millisecond * 1000,
|
|
|
|
RandomizationFactor: 0.1,
|
|
|
|
Multiplier: 1.5,
|
|
|
|
MaxInterval: time.Second * 32,
|
|
|
|
MaxElapsedTime: time.Second * 128,
|
|
|
|
Clock: backoff.SystemClock,
|
|
|
|
}
|
|
|
|
b.Reset()
|
|
|
|
|
|
|
|
op := func() (*http.Response, error) {
|
|
|
|
resp, err := o.client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, backoff.Permanent(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode == http.StatusOK {
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err = fmt.Errorf("unsuccessful request: %d %s", resp.StatusCode, http.StatusText(resp.StatusCode))
|
|
|
|
if resp.StatusCode == http.StatusTooManyRequests {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return nil, backoff.Permanent(err)
|
2023-08-03 12:24:23 +00:00
|
|
|
}
|
|
|
|
|
2023-10-06 12:31:54 +00:00
|
|
|
return backoff.RetryWithData(op, &b)
|
2023-08-03 12:24:23 +00:00
|
|
|
}
|
|
|
|
|
2023-11-14 17:16:39 +00:00
|
|
|
func (o *Client) FetchCollectibleOwnersByContractAddress(ctx context.Context, chainID walletCommon.ChainID, contractAddress common.Address) (*thirdparty.CollectibleContractOwnership, error) {
|
2023-08-01 23:16:57 +00:00
|
|
|
ownership := thirdparty.CollectibleContractOwnership{
|
|
|
|
ContractAddress: contractAddress,
|
|
|
|
Owners: make([]thirdparty.CollectibleOwner, 0),
|
|
|
|
}
|
|
|
|
|
2023-04-17 11:42:01 +00:00
|
|
|
queryParams := url.Values{
|
|
|
|
"contractAddress": {contractAddress.String()},
|
|
|
|
"withTokenBalances": {"true"},
|
|
|
|
}
|
|
|
|
|
2023-08-01 23:16:57 +00:00
|
|
|
baseURL, err := getNFTBaseURL(chainID, o.apiKeys[uint64(chainID)])
|
2023-04-17 11:42:01 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-08-01 23:16:57 +00:00
|
|
|
for {
|
|
|
|
url := fmt.Sprintf("%s/getOwnersForContract?%s", baseURL, queryParams.Encode())
|
2023-04-17 11:42:01 +00:00
|
|
|
|
2023-11-14 17:16:39 +00:00
|
|
|
resp, err := o.doQuery(ctx, url)
|
2023-08-01 23:16:57 +00:00
|
|
|
if err != nil {
|
2023-11-14 17:16:39 +00:00
|
|
|
if ctx.Err() == nil {
|
|
|
|
o.connectionStatus.SetIsConnected(false)
|
|
|
|
}
|
2023-08-01 23:16:57 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-09-22 13:18:42 +00:00
|
|
|
o.connectionStatus.SetIsConnected(true)
|
2023-08-01 23:16:57 +00:00
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var alchemyOwnership CollectibleContractOwnership
|
|
|
|
err = json.Unmarshal(body, &alchemyOwnership)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ownership.Owners = append(ownership.Owners, alchemyCollectibleOwnersToCommon(alchemyOwnership.Owners)...)
|
|
|
|
|
|
|
|
if alchemyOwnership.PageKey == "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
queryParams["pageKey"] = []string{alchemyOwnership.PageKey}
|
2023-04-17 11:42:01 +00:00
|
|
|
}
|
|
|
|
|
2023-08-01 23:16:57 +00:00
|
|
|
return &ownership, nil
|
|
|
|
}
|
2023-04-17 11:42:01 +00:00
|
|
|
|
2023-11-14 17:16:39 +00:00
|
|
|
func (o *Client) FetchAllAssetsByOwner(ctx context.Context, chainID walletCommon.ChainID, owner common.Address, cursor string, limit int) (*thirdparty.FullCollectibleDataContainer, error) {
|
2023-08-01 23:16:57 +00:00
|
|
|
queryParams := url.Values{}
|
|
|
|
|
2023-11-14 17:16:39 +00:00
|
|
|
return o.fetchOwnedAssets(ctx, chainID, owner, queryParams, cursor, limit)
|
2023-08-01 23:16:57 +00:00
|
|
|
}
|
|
|
|
|
2023-11-14 17:16:39 +00:00
|
|
|
func (o *Client) FetchAllAssetsByOwnerAndContractAddress(ctx context.Context, chainID walletCommon.ChainID, owner common.Address, contractAddresses []common.Address, cursor string, limit int) (*thirdparty.FullCollectibleDataContainer, error) {
|
2023-08-01 23:16:57 +00:00
|
|
|
queryParams := url.Values{}
|
|
|
|
|
|
|
|
for _, contractAddress := range contractAddresses {
|
|
|
|
queryParams.Add("contractAddresses", contractAddress.String())
|
2023-04-17 11:42:01 +00:00
|
|
|
}
|
|
|
|
|
2023-11-14 17:16:39 +00:00
|
|
|
return o.fetchOwnedAssets(ctx, chainID, owner, queryParams, cursor, limit)
|
2023-08-01 23:16:57 +00:00
|
|
|
}
|
|
|
|
|
2023-11-14 17:16:39 +00:00
|
|
|
func (o *Client) fetchOwnedAssets(ctx context.Context, chainID walletCommon.ChainID, owner common.Address, queryParams url.Values, cursor string, limit int) (*thirdparty.FullCollectibleDataContainer, error) {
|
2023-08-01 23:16:57 +00:00
|
|
|
assets := new(thirdparty.FullCollectibleDataContainer)
|
|
|
|
|
|
|
|
queryParams["owner"] = []string{owner.String()}
|
|
|
|
queryParams["withMetadata"] = []string{"true"}
|
|
|
|
|
|
|
|
if len(cursor) > 0 {
|
|
|
|
queryParams["pageKey"] = []string{cursor}
|
|
|
|
assets.PreviousCursor = cursor
|
|
|
|
}
|
2023-10-04 16:21:45 +00:00
|
|
|
assets.Provider = o.ID()
|
2023-08-01 23:16:57 +00:00
|
|
|
|
|
|
|
baseURL, err := getNFTBaseURL(chainID, o.apiKeys[uint64(chainID)])
|
|
|
|
|
2023-04-17 11:42:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-08-01 23:16:57 +00:00
|
|
|
for {
|
|
|
|
url := fmt.Sprintf("%s/getNFTsForOwner?%s", baseURL, queryParams.Encode())
|
|
|
|
|
2023-11-14 17:16:39 +00:00
|
|
|
resp, err := o.doQuery(ctx, url)
|
2023-08-01 23:16:57 +00:00
|
|
|
if err != nil {
|
2023-11-14 17:16:39 +00:00
|
|
|
if ctx.Err() == nil {
|
|
|
|
o.connectionStatus.SetIsConnected(false)
|
|
|
|
}
|
2023-08-01 23:16:57 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-09-22 13:18:42 +00:00
|
|
|
o.connectionStatus.SetIsConnected(true)
|
2023-08-01 23:16:57 +00:00
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// if Json is not returned there must be an error
|
|
|
|
if !json.Valid(body) {
|
|
|
|
return nil, fmt.Errorf("invalid json: %s", string(body))
|
|
|
|
}
|
|
|
|
|
2023-08-03 12:24:23 +00:00
|
|
|
container := OwnedNFTList{}
|
2023-08-01 23:16:57 +00:00
|
|
|
err = json.Unmarshal(body, &container)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-08-03 12:24:23 +00:00
|
|
|
assets.Items = append(assets.Items, alchemyToCollectiblesData(chainID, container.OwnedNFTs)...)
|
2023-08-01 23:16:57 +00:00
|
|
|
assets.NextCursor = container.PageKey
|
|
|
|
|
|
|
|
if len(assets.NextCursor) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
queryParams["cursor"] = []string{assets.NextCursor}
|
|
|
|
|
|
|
|
if limit != thirdparty.FetchNoLimit && len(assets.Items) >= limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return assets, nil
|
2023-04-17 11:42:01 +00:00
|
|
|
}
|
2023-08-03 12:24:23 +00:00
|
|
|
|
|
|
|
func getCollectibleUniqueIDBatches(ids []thirdparty.CollectibleUniqueID) []BatchTokenIDs {
|
|
|
|
batches := make([]BatchTokenIDs, 0)
|
|
|
|
|
|
|
|
for startIdx := 0; startIdx < len(ids); startIdx += nftMetadataBatchLimit {
|
|
|
|
endIdx := startIdx + nftMetadataBatchLimit
|
|
|
|
if endIdx > len(ids) {
|
|
|
|
endIdx = len(ids)
|
|
|
|
}
|
|
|
|
|
|
|
|
pageIDs := ids[startIdx:endIdx]
|
|
|
|
|
|
|
|
batchIDs := BatchTokenIDs{
|
|
|
|
IDs: make([]TokenID, 0, len(pageIDs)),
|
|
|
|
}
|
|
|
|
for _, id := range pageIDs {
|
|
|
|
batchID := TokenID{
|
|
|
|
ContractAddress: id.ContractID.Address,
|
|
|
|
TokenID: id.TokenID,
|
|
|
|
}
|
|
|
|
batchIDs.IDs = append(batchIDs.IDs, batchID)
|
|
|
|
}
|
|
|
|
|
|
|
|
batches = append(batches, batchIDs)
|
|
|
|
}
|
|
|
|
|
|
|
|
return batches
|
|
|
|
}
|
|
|
|
|
2023-11-14 17:16:39 +00:00
|
|
|
func (o *Client) fetchAssetsByBatchTokenIDs(ctx context.Context, chainID walletCommon.ChainID, batchIDs BatchTokenIDs) ([]thirdparty.FullCollectibleData, error) {
|
2023-08-03 12:24:23 +00:00
|
|
|
baseURL, err := getNFTBaseURL(chainID, o.apiKeys[uint64(chainID)])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
url := fmt.Sprintf("%s/getNFTMetadataBatch", baseURL)
|
|
|
|
|
2023-11-14 17:16:39 +00:00
|
|
|
resp, err := o.doPostWithJSON(ctx, url, batchIDs)
|
2023-08-03 12:24:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// if Json is not returned there must be an error
|
|
|
|
if !json.Valid(body) {
|
|
|
|
return nil, fmt.Errorf("invalid json: %s", string(body))
|
|
|
|
}
|
|
|
|
|
|
|
|
assets := NFTList{}
|
|
|
|
err = json.Unmarshal(body, &assets)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := alchemyToCollectiblesData(chainID, assets.NFTs)
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2023-11-14 17:16:39 +00:00
|
|
|
func (o *Client) FetchAssetsByCollectibleUniqueID(ctx context.Context, uniqueIDs []thirdparty.CollectibleUniqueID) ([]thirdparty.FullCollectibleData, error) {
|
2023-08-03 12:24:23 +00:00
|
|
|
ret := make([]thirdparty.FullCollectibleData, 0, len(uniqueIDs))
|
|
|
|
|
|
|
|
idsPerChainID := thirdparty.GroupCollectibleUIDsByChainID(uniqueIDs)
|
|
|
|
|
|
|
|
for chainID, ids := range idsPerChainID {
|
|
|
|
batches := getCollectibleUniqueIDBatches(ids)
|
|
|
|
for _, batch := range batches {
|
2023-11-14 17:16:39 +00:00
|
|
|
assets, err := o.fetchAssetsByBatchTokenIDs(ctx, chainID, batch)
|
2023-08-03 12:24:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = append(ret, assets...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getContractAddressBatches(ids []thirdparty.ContractID) []BatchContractAddresses {
|
|
|
|
batches := make([]BatchContractAddresses, 0)
|
|
|
|
|
|
|
|
for startIdx := 0; startIdx < len(ids); startIdx += contractMetadataBatchLimit {
|
|
|
|
endIdx := startIdx + contractMetadataBatchLimit
|
|
|
|
if endIdx > len(ids) {
|
|
|
|
endIdx = len(ids)
|
|
|
|
}
|
|
|
|
|
|
|
|
pageIDs := ids[startIdx:endIdx]
|
|
|
|
|
|
|
|
batchIDs := BatchContractAddresses{
|
|
|
|
Addresses: make([]common.Address, 0, len(pageIDs)),
|
|
|
|
}
|
|
|
|
for _, id := range pageIDs {
|
|
|
|
batchIDs.Addresses = append(batchIDs.Addresses, id.Address)
|
|
|
|
}
|
|
|
|
|
|
|
|
batches = append(batches, batchIDs)
|
|
|
|
}
|
|
|
|
|
|
|
|
return batches
|
|
|
|
}
|
|
|
|
|
2023-11-14 17:16:39 +00:00
|
|
|
func (o *Client) fetchCollectionsDataByBatchContractAddresses(ctx context.Context, chainID walletCommon.ChainID, batchAddresses BatchContractAddresses) ([]thirdparty.CollectionData, error) {
|
2023-08-03 12:24:23 +00:00
|
|
|
baseURL, err := getNFTBaseURL(chainID, o.apiKeys[uint64(chainID)])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
url := fmt.Sprintf("%s/getContractMetadataBatch", baseURL)
|
|
|
|
|
2023-11-14 17:16:39 +00:00
|
|
|
resp, err := o.doPostWithJSON(ctx, url, batchAddresses)
|
2023-08-03 12:24:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// if Json is not returned there must be an error
|
|
|
|
if !json.Valid(body) {
|
|
|
|
return nil, fmt.Errorf("invalid json: %s", string(body))
|
|
|
|
}
|
|
|
|
|
|
|
|
collections := ContractList{}
|
|
|
|
err = json.Unmarshal(body, &collections)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := alchemyToCollectionsData(chainID, collections.Contracts)
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2023-11-14 17:16:39 +00:00
|
|
|
func (o *Client) FetchCollectionsDataByContractID(ctx context.Context, contractIDs []thirdparty.ContractID) ([]thirdparty.CollectionData, error) {
|
2023-08-03 12:24:23 +00:00
|
|
|
ret := make([]thirdparty.CollectionData, 0, len(contractIDs))
|
|
|
|
|
|
|
|
idsPerChainID := thirdparty.GroupContractIDsByChainID(contractIDs)
|
|
|
|
|
|
|
|
for chainID, ids := range idsPerChainID {
|
|
|
|
batches := getContractAddressBatches(ids)
|
|
|
|
for _, batch := range batches {
|
2023-11-14 17:16:39 +00:00
|
|
|
contractsData, err := o.fetchCollectionsDataByBatchContractAddresses(ctx, chainID, batch)
|
2023-08-03 12:24:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = append(ret, contractsData...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|