2023-04-17 11:42:01 +00:00
|
|
|
package infura
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2023-04-18 14:33:59 +00:00
|
|
|
walletCommon "github.com/status-im/status-go/services/wallet/common"
|
2023-04-17 11:42:01 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/thirdparty"
|
|
|
|
)
|
|
|
|
|
|
|
|
const baseURL = "https://nft.api.infura.io"
|
2023-07-31 23:34:53 +00:00
|
|
|
const InfuraID = "infura"
|
2023-04-17 11:42:01 +00:00
|
|
|
|
|
|
|
type Client struct {
|
2023-07-05 09:33:48 +00:00
|
|
|
thirdparty.CollectibleContractOwnershipProvider
|
2023-04-17 11:42:01 +00:00
|
|
|
client *http.Client
|
|
|
|
apiKey string
|
|
|
|
apiKeySecret string
|
|
|
|
IsConnected bool
|
|
|
|
IsConnectedLock sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewClient(apiKey string, apiKeySecret string) *Client {
|
|
|
|
return &Client{
|
|
|
|
client: &http.Client{Timeout: time.Minute},
|
|
|
|
apiKey: apiKey,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Client) doQuery(url string) (*http.Response, error) {
|
|
|
|
req, err := http.NewRequest(http.MethodGet, url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
req.SetBasicAuth(o.apiKey, o.apiKeySecret)
|
|
|
|
|
|
|
|
resp, err := o.client.Do(req)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2023-07-31 19:41:14 +00:00
|
|
|
func (o *Client) ID() string {
|
2023-07-31 23:34:53 +00:00
|
|
|
return InfuraID
|
2023-07-31 19:41:14 +00:00
|
|
|
}
|
|
|
|
|
2023-07-18 15:01:53 +00:00
|
|
|
func (o *Client) IsChainSupported(chainID walletCommon.ChainID) bool {
|
|
|
|
switch uint64(chainID) {
|
2023-07-31 23:34:53 +00:00
|
|
|
case walletCommon.EthereumMainnet, walletCommon.ArbitrumMainnet:
|
|
|
|
case walletCommon.EthereumGoerli, walletCommon.EthereumSepolia:
|
2023-04-17 11:42:01 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-07-18 15:01:53 +00:00
|
|
|
func (o *Client) FetchCollectibleOwnersByContractAddress(chainID walletCommon.ChainID, contractAddress common.Address) (*thirdparty.CollectibleContractOwnership, error) {
|
2023-04-17 11:42:01 +00:00
|
|
|
cursor := ""
|
2023-07-05 09:33:48 +00:00
|
|
|
ownersMap := make(map[common.Address][]CollectibleOwner)
|
2023-04-17 11:42:01 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
url := fmt.Sprintf("%s/networks/%d/nfts/%s/owners", baseURL, chainID, contractAddress.String())
|
|
|
|
|
|
|
|
if cursor != "" {
|
|
|
|
url = url + "?cursor=" + cursor
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := o.doQuery(url)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-05 09:33:48 +00:00
|
|
|
var infuraOwnership CollectibleContractOwnership
|
2023-04-17 11:42:01 +00:00
|
|
|
err = json.Unmarshal(body, &infuraOwnership)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, infuraOwner := range infuraOwnership.Owners {
|
|
|
|
ownersMap[infuraOwner.OwnerAddress] = append(ownersMap[infuraOwner.OwnerAddress], infuraOwner)
|
|
|
|
}
|
|
|
|
|
|
|
|
cursor = infuraOwnership.Cursor
|
|
|
|
|
|
|
|
if cursor == "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return infuraOwnershipToCommon(contractAddress, ownersMap)
|
|
|
|
}
|