2023-04-17 11:42:01 +00:00
|
|
|
package alchemy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/status-im/status-go/services/wallet/bigint"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
func getBaseURL(chainID uint64) (string, error) {
|
|
|
|
switch 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-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
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", fmt.Errorf("chainID not supported: %d", chainID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAPIKeySubpath(apiKey string) string {
|
|
|
|
if apiKey == "" {
|
|
|
|
return "demo"
|
|
|
|
}
|
|
|
|
return apiKey
|
|
|
|
}
|
|
|
|
|
|
|
|
func getNFTBaseURL(chainID uint64, apiKey string) (string, error) {
|
|
|
|
baseURL, err := getBaseURL(chainID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%s/nft/v2/%s", baseURL, getAPIKeySubpath(apiKey)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type TokenBalance struct {
|
|
|
|
TokenID *bigint.HexBigInt `json:"tokenId"`
|
|
|
|
Balance *bigint.BigInt `json:"balance"`
|
|
|
|
}
|
|
|
|
|
2023-07-05 09:33:48 +00:00
|
|
|
type CollectibleOwner struct {
|
2023-04-17 11:42:01 +00:00
|
|
|
OwnerAddress common.Address `json:"ownerAddress"`
|
|
|
|
TokenBalances []TokenBalance `json:"tokenBalances"`
|
|
|
|
}
|
|
|
|
|
2023-07-05 09:33:48 +00:00
|
|
|
type CollectibleContractOwnership struct {
|
|
|
|
Owners []CollectibleOwner `json:"ownerAddresses"`
|
|
|
|
PageKey string `json:"pageKey"`
|
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
|
|
|
|
apiKeys map[uint64]string
|
|
|
|
IsConnected bool
|
|
|
|
IsConnectedLock sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewClient(apiKeys map[uint64]string) *Client {
|
|
|
|
return &Client{
|
|
|
|
client: &http.Client{Timeout: time.Minute},
|
|
|
|
apiKeys: apiKeys,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Client) doQuery(url string) (*http.Response, error) {
|
|
|
|
resp, err := o.client.Get(url)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Client) IsChainSupported(chainID uint64) bool {
|
|
|
|
_, err := getBaseURL(chainID)
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
2023-07-05 09:33:48 +00:00
|
|
|
func alchemyOwnershipToCommon(contractAddress common.Address, alchemyOwnership CollectibleContractOwnership) (*thirdparty.CollectibleContractOwnership, error) {
|
|
|
|
owners := make([]thirdparty.CollectibleOwner, 0, len(alchemyOwnership.Owners))
|
2023-04-17 11:42:01 +00:00
|
|
|
for _, alchemyOwner := range alchemyOwnership.Owners {
|
|
|
|
balances := make([]thirdparty.TokenBalance, 0, len(alchemyOwner.TokenBalances))
|
|
|
|
|
|
|
|
for _, alchemyBalance := range alchemyOwner.TokenBalances {
|
|
|
|
balances = append(balances, thirdparty.TokenBalance{
|
|
|
|
TokenID: &bigint.BigInt{Int: alchemyBalance.TokenID.Int},
|
|
|
|
Balance: alchemyBalance.Balance,
|
|
|
|
})
|
|
|
|
}
|
2023-07-05 09:33:48 +00:00
|
|
|
owner := thirdparty.CollectibleOwner{
|
2023-04-17 11:42:01 +00:00
|
|
|
OwnerAddress: alchemyOwner.OwnerAddress,
|
|
|
|
TokenBalances: balances,
|
|
|
|
}
|
|
|
|
|
|
|
|
owners = append(owners, owner)
|
|
|
|
}
|
|
|
|
|
2023-07-05 09:33:48 +00:00
|
|
|
ownership := thirdparty.CollectibleContractOwnership{
|
2023-04-17 11:42:01 +00:00
|
|
|
ContractAddress: contractAddress,
|
|
|
|
Owners: owners,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ownership, nil
|
|
|
|
}
|
|
|
|
|
2023-07-05 09:33:48 +00:00
|
|
|
func (o *Client) FetchCollectibleOwnersByContractAddress(chainID uint64, contractAddress common.Address) (*thirdparty.CollectibleContractOwnership, error) {
|
2023-04-17 11:42:01 +00:00
|
|
|
queryParams := url.Values{
|
|
|
|
"contractAddress": {contractAddress.String()},
|
|
|
|
"withTokenBalances": {"true"},
|
|
|
|
}
|
|
|
|
|
|
|
|
url, err := getNFTBaseURL(chainID, o.apiKeys[chainID])
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
url = url + "/getOwnersForCollection?" + queryParams.Encode()
|
|
|
|
|
|
|
|
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 alchemyOwnership CollectibleContractOwnership
|
2023-04-17 11:42:01 +00:00
|
|
|
err = json.Unmarshal(body, &alchemyOwnership)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return alchemyOwnershipToCommon(contractAddress, alchemyOwnership)
|
|
|
|
}
|