@quicknode/sdk 1.0.0-beta.1 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -835
- package/cjs/index.js +342 -41
- package/esm/package.json +2 -1
- package/esm/package.json.js +3 -0
- package/esm/src/api/api.js +85 -0
- package/esm/src/api/controllers/contracts.js +36 -0
- package/esm/src/api/controllers/events.js +121 -0
- package/esm/src/api/controllers/nfts.js +244 -0
- package/esm/src/api/controllers/tokens.js +98 -0
- package/esm/src/api/controllers/transactions.js +125 -0
- package/esm/src/api/controllers/utils.js +55 -0
- package/esm/src/api/graphql/customUrqlClient.js +23 -0
- package/esm/src/api/graphql/generatedTypes.js +62 -0
- package/esm/src/api/graphql/schema.json.js +3 -0
- package/esm/src/api/index.d.ts +2687 -0
- package/esm/src/api/index.js +2 -0
- package/esm/src/api/types/chains.js +7 -0
- package/esm/src/api/types/contracts/getContractDetails.js +12 -0
- package/esm/src/api/types/events/getAll.js +7 -0
- package/esm/src/api/types/events/getByContract.js +12 -0
- package/esm/src/api/types/nfts/getByContractAddress.js +10 -0
- package/esm/src/api/types/nfts/getByWalletAddress.js +18 -0
- package/esm/src/api/types/nfts/getCollectionDetails.js +11 -0
- package/esm/src/api/types/nfts/getCollectionEvents.js +12 -0
- package/esm/src/api/types/nfts/getNFTDetails.js +9 -0
- package/esm/src/api/types/nfts/getNFTEvents.js +13 -0
- package/esm/src/api/types/nfts/getTrendingCollections.js +7 -0
- package/esm/src/api/types/nfts/verifyOwnershipByAddress.js +12 -0
- package/esm/src/api/types/tokens/getBalancesByWalletAddress.js +12 -0
- package/esm/src/api/types/transactions/getByHash.js +11 -0
- package/esm/src/api/types/transactions/getBySearch.js +7 -0
- package/esm/src/api/types/utils/gasPrices.js +12 -0
- package/esm/src/api/utils/constants.js +3 -0
- package/esm/src/api/utils/helpers.js +11 -0
- package/esm/src/api/utils/isValidENSAddress.js +21 -0
- package/esm/src/api/utils/postQueryFormatter.js +24 -0
- package/esm/src/api/utils/removeNodesAndEdges.js +51 -0
- package/esm/src/client/client.js +9 -0
- package/esm/src/client/index.js +2 -0
- package/esm/src/core/addOns/nftTokenV2/actions.js +107 -0
- package/esm/src/core/addOns/nftTokenV2/types/qn_fetchNFTCollectionDetails.js +10 -0
- package/esm/src/core/addOns/nftTokenV2/types/qn_fetchNFTs.js +13 -0
- package/esm/src/core/addOns/nftTokenV2/types/qn_fetchNFTsByCollection.js +13 -0
- package/esm/src/core/addOns/nftTokenV2/types/qn_getTokenMetadataByContractAddress.js +10 -0
- package/esm/src/core/addOns/nftTokenV2/types/qn_getTokenMetadataBySymbol.js +11 -0
- package/esm/src/core/addOns/nftTokenV2/types/qn_getTransactionsByAddress.js +18 -0
- package/esm/src/core/addOns/nftTokenV2/types/qn_getTransfersByNFT.js +12 -0
- package/esm/src/core/addOns/nftTokenV2/types/qn_getWalletTokenBalance.js +12 -0
- package/esm/src/core/addOns/nftTokenV2/types/qn_getWalletTokenTransactions.js +14 -0
- package/esm/src/core/addOns/nftTokenV2/types/qn_verifyNFTsOwner.js +11 -0
- package/esm/src/core/addOns/shared/helpers.js +10 -0
- package/esm/src/core/chains.js +60 -0
- package/esm/src/core/core.js +24 -0
- package/esm/src/core/index.d.ts +555 -0
- package/esm/src/core/index.js +2 -0
- package/esm/src/index.js +10 -0
- package/esm/src/lib/errors/QNChainNotSupported.js +7 -0
- package/esm/src/lib/errors/QNInputValidationError.js +10 -0
- package/esm/src/lib/errors/QNInvalidEnpointUrl.js +7 -0
- package/esm/src/lib/helpers/globalFetch.js +13 -0
- package/esm/src/lib/validation/ValidateInput.js +35 -0
- package/esm/src/lib/validation/codegenDerivedValidators.js +28 -0
- package/esm/src/lib/validation/validators.js +105 -0
- package/index.d.ts +612 -89
- package/package.json +25 -3
- package/esm/index.js +0 -1172
package/esm/package.json
CHANGED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
var name="@quicknode/sdk";var repository={type:"git",url:"https://github.com/quiknode-labs/qn-oss.git",directory:"packages/libs/sdk"};var license="MIT";var version="1.1.0";var main="./cjs/index.js";var module="./esm/src/index.js";var types="./index.d.ts";var sideEffects=false;var dependencies={"@urql/core":"^4.0.7","@urql/exchange-graphcache":"^6.0.4","cross-fetch":"^3.1.6",viem:"^1.2.0",tslib:"^2.5.3",zod:"^3.21.4"};var devDependencies={"@graphql-codegen/cli":"4.0.1","@graphql-codegen/fragment-matcher":"^3.3.1","@graphql-codegen/introspection":"^3.0.1","@graphql-codegen/typed-document-node":"^4.0.1","@graphql-codegen/typescript":"2.8.0","@graphql-codegen/typescript-operations":"^2.5.5","@pollyjs/adapter-node-http":"^6.0.5","@pollyjs/core":"^6.0.5","@pollyjs/persister-fs":"^6.0.5","@types/jest":"^29.5.1","@types/mocha":"^10.0.1","@types/node":"^18.13.0","@types/supertest":"^2.0.12",dotenv:"^16.0.3","eslint-plugin-no-only-tests":"^3.1.0",graphql:"^16.6.0",supertest:"^6.3.3"};var scripts={codegen:"npx graphql-codegen --require dotenv/config"};var exports={".":{"import":"./esm/src/index.js",require:"./cjs/index.js"},"./api":{"import":"./esm/src/api/index.js","default":"./cjs/src/api/index.js",types:"./esm/src/api/index.d.ts"},"./core":{"import":"./esm/src/core/index.js","default":"./cjs/src/core/index.js",types:"./esm/src/core/index.d.ts"}};var typesVersions={"*":{api:["./esm/src/api/index.d.ts"],core:["./esm/src/core/index.d.ts"]}};var packageJson = {name:name,repository:repository,license:license,version:version,main:main,module:module,types:types,sideEffects:sideEffects,dependencies:dependencies,devDependencies:devDependencies,scripts:scripts,exports:exports,typesVersions:typesVersions};
|
|
2
|
+
|
|
3
|
+
export { packageJson as default, dependencies, devDependencies, exports, license, main, module, name, repository, scripts, sideEffects, types, typesVersions, version };
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import fetch from 'cross-fetch';
|
|
2
|
+
import { CustomUrqlClient } from './graphql/customUrqlClient.js';
|
|
3
|
+
import { Client, fetchExchange } from '@urql/core';
|
|
4
|
+
import { cacheExchange } from '@urql/exchange-graphcache';
|
|
5
|
+
import { NftsController } from './controllers/nfts.js';
|
|
6
|
+
import { TokensController } from './controllers/tokens.js';
|
|
7
|
+
import { UtilsController } from './controllers/utils.js';
|
|
8
|
+
import { ContractsController } from './controllers/contracts.js';
|
|
9
|
+
import { EventsController } from './controllers/events.js';
|
|
10
|
+
import { DEFAULT_CHAIN } from './utils/constants.js';
|
|
11
|
+
import schema from './graphql/schema.json.js';
|
|
12
|
+
import { TransactionsController } from './controllers/transactions.js';
|
|
13
|
+
import packageJson from '../../package.json.js';
|
|
14
|
+
|
|
15
|
+
class API {
|
|
16
|
+
constructor({ graphApiKey, additionalHeaders, defaultChain, } = {}) {
|
|
17
|
+
if (!graphApiKey) {
|
|
18
|
+
console.warn('QuickNode SDK warning: no apiKey provided. Access with no apiKey is heavily rate limited and intended for development use only. For higher rate limits or production usage, create an account on https://www.quicknode.com/');
|
|
19
|
+
}
|
|
20
|
+
this.graphApiKey = graphApiKey;
|
|
21
|
+
this.additionalHeaders = additionalHeaders;
|
|
22
|
+
this.urqlClient = this.createUrqlClient();
|
|
23
|
+
this.customUrqlClient = new CustomUrqlClient(this.urqlClient);
|
|
24
|
+
this.defaultChain = defaultChain || DEFAULT_CHAIN;
|
|
25
|
+
this.nfts = new NftsController(this.customUrqlClient, this.defaultChain);
|
|
26
|
+
this.tokens = new TokensController(this.customUrqlClient, this.defaultChain);
|
|
27
|
+
this.utils = new UtilsController(this.customUrqlClient, this.defaultChain);
|
|
28
|
+
this.contracts = new ContractsController(this.customUrqlClient, this.defaultChain);
|
|
29
|
+
this.transactions = new TransactionsController(this.customUrqlClient, this.defaultChain);
|
|
30
|
+
this.events = new EventsController(this.customUrqlClient, this.defaultChain);
|
|
31
|
+
// Re-export the Urql client configured to use the Graph API for use with custom queries
|
|
32
|
+
this.graphApiClient = this.urqlClient;
|
|
33
|
+
}
|
|
34
|
+
createUrqlClient() {
|
|
35
|
+
const headers = { ...this.additionalHeaders };
|
|
36
|
+
if (this.graphApiKey)
|
|
37
|
+
headers['x-api-key'] = this.graphApiKey;
|
|
38
|
+
headers['x-quicknode-sdk'] = 'js-sdk';
|
|
39
|
+
headers['x-quicknode-sdk-version'] = packageJson?.version || 'n/a';
|
|
40
|
+
const useNftKey = (data) => `${data['contractAddress']}:${data['tokenId']}`;
|
|
41
|
+
const useAddressAsKey = (data) => `${data['address']}`;
|
|
42
|
+
const useTransactionHashAndIndex = (data) => `${data['transactionHash']}:${data['transferIndex']}`;
|
|
43
|
+
const urqlCache = cacheExchange({
|
|
44
|
+
schema,
|
|
45
|
+
keys: {
|
|
46
|
+
EVMSchemaType: () => null,
|
|
47
|
+
Collection: useAddressAsKey,
|
|
48
|
+
CollectionOHLCVChart: () => null,
|
|
49
|
+
Contract: useAddressAsKey,
|
|
50
|
+
ERC721NFT: useNftKey,
|
|
51
|
+
ERC721Collection: useAddressAsKey,
|
|
52
|
+
ERC1155NFT: useNftKey,
|
|
53
|
+
ERC1155Collection: useAddressAsKey,
|
|
54
|
+
GasPrice: () => null,
|
|
55
|
+
NFT: useNftKey,
|
|
56
|
+
NFTContract: useAddressAsKey,
|
|
57
|
+
TokenAttribute: () => null,
|
|
58
|
+
TokenContract: useAddressAsKey,
|
|
59
|
+
TokenEvent: useTransactionHashAndIndex,
|
|
60
|
+
TokenMintEvent: useTransactionHashAndIndex,
|
|
61
|
+
TokenBurnEvent: useTransactionHashAndIndex,
|
|
62
|
+
TokenSaleEvent: useTransactionHashAndIndex,
|
|
63
|
+
TokenSwapEvent: useTransactionHashAndIndex,
|
|
64
|
+
TokenTransferEvent: useTransactionHashAndIndex,
|
|
65
|
+
TokenUpload: () => null,
|
|
66
|
+
OpenSeaMetadata: () => null,
|
|
67
|
+
Transaction: (data) => `${data['hash']}`,
|
|
68
|
+
TrendingCollection: () => null,
|
|
69
|
+
Wallet: (data) => `${data['address']}`,
|
|
70
|
+
WalletNFT: () => null,
|
|
71
|
+
WalletTokenBalance: () => null,
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
const client = new Client({
|
|
75
|
+
fetch,
|
|
76
|
+
url: process.env['NX_GRAPHQL_API_URI'] ||
|
|
77
|
+
'https://api.quicknode.com/graphql',
|
|
78
|
+
exchanges: [urqlCache, fetchExchange],
|
|
79
|
+
fetchOptions: () => ({ headers }),
|
|
80
|
+
});
|
|
81
|
+
return client;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export { API };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { __decorate, __metadata } from 'tslib';
|
|
2
|
+
import { DEFAULT_CHAIN } from '../utils/constants.js';
|
|
3
|
+
import { contractDetailsValidator } from '../types/contracts/getContractDetails.js';
|
|
4
|
+
import { CodegenEthMainnetContractDetailsDocument, CodegenPolygonMainnetContractDetailsDocument, CodegenEthSepoliaContractDetailsDocument } from '../graphql/generatedTypes.js';
|
|
5
|
+
import { ValidateInput } from '../../lib/validation/ValidateInput.js';
|
|
6
|
+
|
|
7
|
+
class ContractsController {
|
|
8
|
+
constructor(client, defaultChain = DEFAULT_CHAIN) {
|
|
9
|
+
this.client = client;
|
|
10
|
+
this.defaultChain = defaultChain;
|
|
11
|
+
}
|
|
12
|
+
async getDetails(variables) {
|
|
13
|
+
const { chain, ...queryVariables } = variables;
|
|
14
|
+
const userChain = chain || this.defaultChain;
|
|
15
|
+
const query = {
|
|
16
|
+
ethereum: CodegenEthMainnetContractDetailsDocument,
|
|
17
|
+
polygon: CodegenPolygonMainnetContractDetailsDocument,
|
|
18
|
+
ethereumSepolia: CodegenEthSepoliaContractDetailsDocument,
|
|
19
|
+
};
|
|
20
|
+
const { data: { [userChain]: { contract }, }, } = await this.client.query({
|
|
21
|
+
variables: queryVariables,
|
|
22
|
+
query: query[userChain],
|
|
23
|
+
});
|
|
24
|
+
if (contract)
|
|
25
|
+
return { contract };
|
|
26
|
+
return { contract: null };
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
__decorate([
|
|
30
|
+
ValidateInput(contractDetailsValidator),
|
|
31
|
+
__metadata("design:type", Function),
|
|
32
|
+
__metadata("design:paramtypes", [Object]),
|
|
33
|
+
__metadata("design:returntype", Promise)
|
|
34
|
+
], ContractsController.prototype, "getDetails", null);
|
|
35
|
+
|
|
36
|
+
export { ContractsController };
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { __decorate, __metadata } from 'tslib';
|
|
2
|
+
import { contractEventsValidator } from '../types/events/getByContract.js';
|
|
3
|
+
import { collectionEventsValidator } from '../types/nfts/getCollectionEvents.js';
|
|
4
|
+
import { nftEventsValidator } from '../types/nfts/getNFTEvents.js';
|
|
5
|
+
import { allEventsValidator } from '../types/events/getAll.js';
|
|
6
|
+
import { CodegenEthereumMainnetEventsByContractDocument, CodegenPolygonMainnetEventsByContractDocument, CodegenEthereumSepoliaEventsByContractDocument, CodegenEthMainnetEventsByCollectionDocument, CodegenPolygonMainnetEventsByCollectionDocument, CodegenEthSepoliaEventsByCollectionDocument, CodegenEthereumMainnetEventsByNftDocument, CodegenPolygonMainnetEventsByNftDocument, CodegenEthSepoliaEventsByNftDocument, CodegenEthereumMainnetEventsGetAllDocument, CodegenPolygonMainnetEventsGetAllDocument, CodegenEthereumSepoliaEventsGetAllDocument } from '../graphql/generatedTypes.js';
|
|
7
|
+
import { formatQueryResult } from '../utils/postQueryFormatter.js';
|
|
8
|
+
import { emptyPageInfo } from '../utils/helpers.js';
|
|
9
|
+
import { DEFAULT_CHAIN } from '../utils/constants.js';
|
|
10
|
+
import { ValidateInput } from '../../lib/validation/ValidateInput.js';
|
|
11
|
+
|
|
12
|
+
class EventsController {
|
|
13
|
+
constructor(client, defaultChain = DEFAULT_CHAIN) {
|
|
14
|
+
this.client = client;
|
|
15
|
+
this.defaultChain = defaultChain;
|
|
16
|
+
}
|
|
17
|
+
async getByContract(variables) {
|
|
18
|
+
const { chain, ...queryVariables } = variables;
|
|
19
|
+
const userChain = chain || this.defaultChain;
|
|
20
|
+
const query = {
|
|
21
|
+
ethereum: CodegenEthereumMainnetEventsByContractDocument,
|
|
22
|
+
polygon: CodegenPolygonMainnetEventsByContractDocument,
|
|
23
|
+
ethereumSepolia: CodegenEthereumSepoliaEventsByContractDocument,
|
|
24
|
+
};
|
|
25
|
+
const { data: { [userChain]: { contract }, }, } = await this.client.query({
|
|
26
|
+
query: query[userChain],
|
|
27
|
+
variables: queryVariables,
|
|
28
|
+
});
|
|
29
|
+
if (!contract?.tokenEvents?.length) {
|
|
30
|
+
return {
|
|
31
|
+
results: [],
|
|
32
|
+
pageInfo: emptyPageInfo,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
const formattedResult = formatQueryResult(contract, 'tokenEvents', 'tokenEventsPageInfo');
|
|
36
|
+
return formattedResult;
|
|
37
|
+
}
|
|
38
|
+
async getByNFTCollection(variables) {
|
|
39
|
+
const { chain, ...queryVariables } = variables;
|
|
40
|
+
const userChain = chain || this.defaultChain;
|
|
41
|
+
const query = {
|
|
42
|
+
ethereum: CodegenEthMainnetEventsByCollectionDocument,
|
|
43
|
+
polygon: CodegenPolygonMainnetEventsByCollectionDocument,
|
|
44
|
+
ethereumSepolia: CodegenEthSepoliaEventsByCollectionDocument,
|
|
45
|
+
};
|
|
46
|
+
const { data: { [userChain]: { collection }, }, } = await this.client.query({
|
|
47
|
+
query: query[userChain],
|
|
48
|
+
variables: queryVariables,
|
|
49
|
+
});
|
|
50
|
+
if (!collection?.tokenEvents?.length)
|
|
51
|
+
return { results: [], pageInfo: emptyPageInfo };
|
|
52
|
+
function removeKeyFields(results) {
|
|
53
|
+
const { address, ...newResults } = results;
|
|
54
|
+
return newResults;
|
|
55
|
+
}
|
|
56
|
+
const formattedResult = formatQueryResult(collection, 'tokenEvents', 'tokenEventsPageInfo', null, removeKeyFields);
|
|
57
|
+
return formattedResult;
|
|
58
|
+
}
|
|
59
|
+
async getByNFT(variables) {
|
|
60
|
+
const { chain, ...queryVariables } = variables;
|
|
61
|
+
const userChain = chain || this.defaultChain;
|
|
62
|
+
const query = {
|
|
63
|
+
ethereum: CodegenEthereumMainnetEventsByNftDocument,
|
|
64
|
+
polygon: CodegenPolygonMainnetEventsByNftDocument,
|
|
65
|
+
ethereumSepolia: CodegenEthSepoliaEventsByNftDocument,
|
|
66
|
+
};
|
|
67
|
+
const { data: { [userChain]: { nft }, }, } = await this.client.query({
|
|
68
|
+
query: query[userChain],
|
|
69
|
+
variables: queryVariables,
|
|
70
|
+
});
|
|
71
|
+
if (!nft?.tokenEvents?.length)
|
|
72
|
+
return { results: [], pageInfo: emptyPageInfo };
|
|
73
|
+
function removeKeyFields(results) {
|
|
74
|
+
const { contractAddress, tokenId, ...newResults } = results;
|
|
75
|
+
return newResults;
|
|
76
|
+
}
|
|
77
|
+
const formattedResult = formatQueryResult(nft, 'tokenEvents', 'tokenEventsPageInfo', null, removeKeyFields);
|
|
78
|
+
return formattedResult;
|
|
79
|
+
}
|
|
80
|
+
async getAll(variables) {
|
|
81
|
+
const { chain, ...queryVariables } = variables;
|
|
82
|
+
const userChain = chain || this.defaultChain;
|
|
83
|
+
const query = {
|
|
84
|
+
ethereum: CodegenEthereumMainnetEventsGetAllDocument,
|
|
85
|
+
polygon: CodegenPolygonMainnetEventsGetAllDocument,
|
|
86
|
+
ethereumSepolia: CodegenEthereumSepoliaEventsGetAllDocument,
|
|
87
|
+
};
|
|
88
|
+
const { data: { [userChain]: tokenEvents }, } = await this.client.query({
|
|
89
|
+
query: query[userChain],
|
|
90
|
+
variables: queryVariables,
|
|
91
|
+
});
|
|
92
|
+
const formattedResult = formatQueryResult(tokenEvents, 'tokenEvents', 'tokenEventsPageInfo');
|
|
93
|
+
return formattedResult;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
__decorate([
|
|
97
|
+
ValidateInput(contractEventsValidator),
|
|
98
|
+
__metadata("design:type", Function),
|
|
99
|
+
__metadata("design:paramtypes", [Object]),
|
|
100
|
+
__metadata("design:returntype", Promise)
|
|
101
|
+
], EventsController.prototype, "getByContract", null);
|
|
102
|
+
__decorate([
|
|
103
|
+
ValidateInput(collectionEventsValidator),
|
|
104
|
+
__metadata("design:type", Function),
|
|
105
|
+
__metadata("design:paramtypes", [Object]),
|
|
106
|
+
__metadata("design:returntype", Promise)
|
|
107
|
+
], EventsController.prototype, "getByNFTCollection", null);
|
|
108
|
+
__decorate([
|
|
109
|
+
ValidateInput(nftEventsValidator),
|
|
110
|
+
__metadata("design:type", Function),
|
|
111
|
+
__metadata("design:paramtypes", [Object]),
|
|
112
|
+
__metadata("design:returntype", Promise)
|
|
113
|
+
], EventsController.prototype, "getByNFT", null);
|
|
114
|
+
__decorate([
|
|
115
|
+
ValidateInput(allEventsValidator),
|
|
116
|
+
__metadata("design:type", Function),
|
|
117
|
+
__metadata("design:paramtypes", [Object]),
|
|
118
|
+
__metadata("design:returntype", Promise)
|
|
119
|
+
], EventsController.prototype, "getAll", null);
|
|
120
|
+
|
|
121
|
+
export { EventsController };
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import { __decorate, __metadata } from 'tslib';
|
|
2
|
+
import { walletByAddressValidator } from '../types/nfts/getByWalletAddress.js';
|
|
3
|
+
import { nftDetailsValidator } from '../types/nfts/getNFTDetails.js';
|
|
4
|
+
import { nftCollectionDetailsValidator } from '../types/nfts/getCollectionDetails.js';
|
|
5
|
+
import { nftTrendingCollectionsValidator } from '../types/nfts/getTrendingCollections.js';
|
|
6
|
+
import { nftsByContractAddressValidator } from '../types/nfts/getByContractAddress.js';
|
|
7
|
+
import { verifyOwnershipValidator } from '../types/nfts/verifyOwnershipByAddress.js';
|
|
8
|
+
import { CodegenEthMainnetWalletNFTsByEnsDocument, CodegenPolygonMainnetWalletNFTsByEnsDocument, CodegenEthSepoliaWalletNFTsByEnsDocument, CodegenEthMainnetWalletNFTsByAddressDocument, CodegenPolygonMainnetWalletNFTsByAddressDocument, CodegenEthSepoliaWalletNFTsByAddressDocument, CodegenEthMainnetTrendingCollectionsDocument, CodegenPolygonMainnetTrendingCollectionsDocument, CodegenEthSepoliaTrendingCollectionsDocument, CodegenEthMainnetWalletNFTsByContractAddressDocument, CodegenPolygonMainnetNFTsByContractAddressDocument, CodegenEthSepoliaWalletNFTsByContractAddressDocument, CodegenEthMainnetNFTDetailsDocument, CodegenPolygonMainnetNFTDetailsDocument, CodegenEthSepoliaNFTDetailsDocument, CodegenEthMainnetNftCollectionDetailsDocument, CodegenPolygonMainnetNftCollectionDetailsDocument, CodegenEthSepoliaNftCollectionDetailsDocument, CodegenEthMainnetVerifyOwnershipByAddressDocument, CodegenPolygonMainnetVerifyOwnershipByAddressDocument, CodegenEthSepoliaVerifyOwnershipByAddressDocument, CodegenEthMainnetVerifyOwnershipByENSDocument, CodegenPolygonMainnetVerifyOwnershipByENSDocument, CodegenEthSepoliaVerifyOwnershipByENSDocument } from '../graphql/generatedTypes.js';
|
|
9
|
+
import { formatQueryResult } from '../utils/postQueryFormatter.js';
|
|
10
|
+
import { emptyPageInfo } from '../utils/helpers.js';
|
|
11
|
+
import { DEFAULT_CHAIN } from '../utils/constants.js';
|
|
12
|
+
import { isValidENSAddress } from '../utils/isValidENSAddress.js';
|
|
13
|
+
import { ValidateInput } from '../../lib/validation/ValidateInput.js';
|
|
14
|
+
|
|
15
|
+
class NftsController {
|
|
16
|
+
constructor(client, defaultChain = DEFAULT_CHAIN) {
|
|
17
|
+
this.client = client;
|
|
18
|
+
this.defaultChain = defaultChain;
|
|
19
|
+
}
|
|
20
|
+
async getByWallet(variables) {
|
|
21
|
+
const { address, ...allVariables } = variables;
|
|
22
|
+
if (isValidENSAddress(address)) {
|
|
23
|
+
return this.getByWalletENS({
|
|
24
|
+
ensName: address,
|
|
25
|
+
...allVariables,
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
return this.getByWalletAddress({
|
|
29
|
+
address,
|
|
30
|
+
...allVariables,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
async getByWalletENS(variables) {
|
|
34
|
+
const { chain, ...queryVariables } = variables;
|
|
35
|
+
const userChain = chain || this.defaultChain;
|
|
36
|
+
const query = {
|
|
37
|
+
ethereum: CodegenEthMainnetWalletNFTsByEnsDocument,
|
|
38
|
+
polygon: CodegenPolygonMainnetWalletNFTsByEnsDocument,
|
|
39
|
+
ethereumSepolia: CodegenEthSepoliaWalletNFTsByEnsDocument,
|
|
40
|
+
};
|
|
41
|
+
const { data: { [userChain]: { walletByENS }, }, } = await this.client.query({
|
|
42
|
+
query: query[userChain],
|
|
43
|
+
variables: queryVariables,
|
|
44
|
+
});
|
|
45
|
+
if (!walletByENS?.walletNFTs?.length) {
|
|
46
|
+
// Address can still be valid ENS name, but not have any NFTs
|
|
47
|
+
const address = walletByENS?.address || '';
|
|
48
|
+
const ensName = walletByENS?.ensName || '';
|
|
49
|
+
return {
|
|
50
|
+
address: address,
|
|
51
|
+
ensName: ensName,
|
|
52
|
+
results: [],
|
|
53
|
+
pageInfo: emptyPageInfo,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
const formattedResult = formatQueryResult(walletByENS, 'walletNFTs', 'walletNFTsPageInfo', 'nft');
|
|
57
|
+
return formattedResult;
|
|
58
|
+
}
|
|
59
|
+
async getByWalletAddress(variables) {
|
|
60
|
+
const { chain, ...queryVariables } = variables;
|
|
61
|
+
const userChain = chain || this.defaultChain;
|
|
62
|
+
const query = {
|
|
63
|
+
ethereum: CodegenEthMainnetWalletNFTsByAddressDocument,
|
|
64
|
+
polygon: CodegenPolygonMainnetWalletNFTsByAddressDocument,
|
|
65
|
+
ethereumSepolia: CodegenEthSepoliaWalletNFTsByAddressDocument,
|
|
66
|
+
};
|
|
67
|
+
const { data: { [userChain]: { walletByAddress }, }, } = await this.client.query({
|
|
68
|
+
query: query[userChain],
|
|
69
|
+
variables: queryVariables,
|
|
70
|
+
});
|
|
71
|
+
if (!walletByAddress?.walletNFTs?.length) {
|
|
72
|
+
const address = walletByAddress?.address || '';
|
|
73
|
+
const ensName = walletByAddress?.ensName || '';
|
|
74
|
+
return {
|
|
75
|
+
address: address,
|
|
76
|
+
ensName: ensName,
|
|
77
|
+
results: [],
|
|
78
|
+
pageInfo: emptyPageInfo,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
const formattedResult = formatQueryResult(walletByAddress, 'walletNFTs', 'walletNFTsPageInfo', 'nft');
|
|
82
|
+
return formattedResult;
|
|
83
|
+
}
|
|
84
|
+
async getTrendingCollections(variables) {
|
|
85
|
+
const { chain, ...queryVariables } = variables;
|
|
86
|
+
const userChain = chain || this.defaultChain;
|
|
87
|
+
const query = {
|
|
88
|
+
ethereum: CodegenEthMainnetTrendingCollectionsDocument,
|
|
89
|
+
polygon: CodegenPolygonMainnetTrendingCollectionsDocument,
|
|
90
|
+
ethereumSepolia: CodegenEthSepoliaTrendingCollectionsDocument,
|
|
91
|
+
};
|
|
92
|
+
const { data: { [userChain]: trendingCollections }, } = await this.client.query({
|
|
93
|
+
query: query[userChain],
|
|
94
|
+
variables: queryVariables,
|
|
95
|
+
});
|
|
96
|
+
const formattedResult = formatQueryResult(trendingCollections, 'trendingCollections', 'trendingCollectionsPageInfo', 'collection');
|
|
97
|
+
return formattedResult;
|
|
98
|
+
}
|
|
99
|
+
async getByContractAddress(variables) {
|
|
100
|
+
const { chain, ...queryVariables } = variables;
|
|
101
|
+
const userChain = chain || this.defaultChain;
|
|
102
|
+
const query = {
|
|
103
|
+
ethereum: CodegenEthMainnetWalletNFTsByContractAddressDocument,
|
|
104
|
+
polygon: CodegenPolygonMainnetNFTsByContractAddressDocument,
|
|
105
|
+
ethereumSepolia: CodegenEthSepoliaWalletNFTsByContractAddressDocument,
|
|
106
|
+
};
|
|
107
|
+
const { data: { [userChain]: { collection }, }, } = await this.client.query({
|
|
108
|
+
query: query[userChain],
|
|
109
|
+
variables: queryVariables,
|
|
110
|
+
});
|
|
111
|
+
if (!collection?.nfts?.length) {
|
|
112
|
+
return {
|
|
113
|
+
standard: null,
|
|
114
|
+
results: [],
|
|
115
|
+
pageInfo: emptyPageInfo,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
const setErcStandard = (results) => {
|
|
119
|
+
const standardMap = {
|
|
120
|
+
ERC1155Collection: 'ERC1155',
|
|
121
|
+
ERC721Collection: 'ERC721',
|
|
122
|
+
};
|
|
123
|
+
// Remove address too since it was only used as a key field
|
|
124
|
+
const { __typename, address, ...newResults } = results;
|
|
125
|
+
return {
|
|
126
|
+
...newResults,
|
|
127
|
+
standard: standardMap[results['__typename']] || null,
|
|
128
|
+
};
|
|
129
|
+
};
|
|
130
|
+
const formattedResult = formatQueryResult(collection, 'nfts', 'nftsPageInfo', null, setErcStandard);
|
|
131
|
+
return formattedResult;
|
|
132
|
+
}
|
|
133
|
+
async getNFTDetails(variables) {
|
|
134
|
+
const { chain, ...queryVariables } = variables;
|
|
135
|
+
const userChain = chain || this.defaultChain;
|
|
136
|
+
const query = {
|
|
137
|
+
ethereum: CodegenEthMainnetNFTDetailsDocument,
|
|
138
|
+
polygon: CodegenPolygonMainnetNFTDetailsDocument,
|
|
139
|
+
ethereumSepolia: CodegenEthSepoliaNFTDetailsDocument,
|
|
140
|
+
};
|
|
141
|
+
const { data: { [userChain]: { nft }, }, } = await this.client.query({
|
|
142
|
+
query: query[userChain],
|
|
143
|
+
variables: queryVariables,
|
|
144
|
+
});
|
|
145
|
+
if (nft)
|
|
146
|
+
return { nft };
|
|
147
|
+
return { nft: null };
|
|
148
|
+
}
|
|
149
|
+
async getCollectionDetails(variables) {
|
|
150
|
+
const { chain, ...queryVariables } = variables;
|
|
151
|
+
const userChain = chain || this.defaultChain;
|
|
152
|
+
const query = {
|
|
153
|
+
ethereum: CodegenEthMainnetNftCollectionDetailsDocument,
|
|
154
|
+
polygon: CodegenPolygonMainnetNftCollectionDetailsDocument,
|
|
155
|
+
ethereumSepolia: CodegenEthSepoliaNftCollectionDetailsDocument,
|
|
156
|
+
};
|
|
157
|
+
const { data: { [userChain]: { collection }, }, } = await this.client.query({
|
|
158
|
+
query: query[userChain],
|
|
159
|
+
variables: queryVariables,
|
|
160
|
+
});
|
|
161
|
+
if (collection)
|
|
162
|
+
return { collection };
|
|
163
|
+
return { collection: null };
|
|
164
|
+
}
|
|
165
|
+
async verifyOwnership(variables) {
|
|
166
|
+
const { address, ...allVariables } = variables;
|
|
167
|
+
if (isValidENSAddress(address)) {
|
|
168
|
+
return this.verifyOwnershipByENS({
|
|
169
|
+
ensName: address,
|
|
170
|
+
...allVariables,
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
return this.verifyOwnershipByAddress({
|
|
174
|
+
address,
|
|
175
|
+
...allVariables,
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
async verifyOwnershipByAddress(variables) {
|
|
179
|
+
const { chain, address, nfts } = variables;
|
|
180
|
+
const userChain = chain || this.defaultChain;
|
|
181
|
+
const query = {
|
|
182
|
+
ethereum: CodegenEthMainnetVerifyOwnershipByAddressDocument,
|
|
183
|
+
polygon: CodegenPolygonMainnetVerifyOwnershipByAddressDocument,
|
|
184
|
+
ethereumSepolia: CodegenEthSepoliaVerifyOwnershipByAddressDocument,
|
|
185
|
+
};
|
|
186
|
+
const { data: { [userChain]: { walletByAddress }, }, } = await this.client.query({
|
|
187
|
+
query: query[userChain],
|
|
188
|
+
variables: { address, filter: { contractTokens: nfts } },
|
|
189
|
+
});
|
|
190
|
+
return !!walletByAddress?.walletNFTs?.length;
|
|
191
|
+
}
|
|
192
|
+
async verifyOwnershipByENS(variables) {
|
|
193
|
+
const { chain, ensName, nfts } = variables;
|
|
194
|
+
const userChain = chain || this.defaultChain;
|
|
195
|
+
const query = {
|
|
196
|
+
ethereum: CodegenEthMainnetVerifyOwnershipByENSDocument,
|
|
197
|
+
polygon: CodegenPolygonMainnetVerifyOwnershipByENSDocument,
|
|
198
|
+
ethereumSepolia: CodegenEthSepoliaVerifyOwnershipByENSDocument,
|
|
199
|
+
};
|
|
200
|
+
const { data: { [userChain]: { walletByENS }, }, } = await this.client.query({
|
|
201
|
+
query: query[userChain],
|
|
202
|
+
variables: { ensName, filter: { contractTokens: nfts } },
|
|
203
|
+
});
|
|
204
|
+
return !!walletByENS?.walletNFTs?.length;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
__decorate([
|
|
208
|
+
ValidateInput(walletByAddressValidator),
|
|
209
|
+
__metadata("design:type", Function),
|
|
210
|
+
__metadata("design:paramtypes", [Object]),
|
|
211
|
+
__metadata("design:returntype", Promise)
|
|
212
|
+
], NftsController.prototype, "getByWallet", null);
|
|
213
|
+
__decorate([
|
|
214
|
+
ValidateInput(nftTrendingCollectionsValidator),
|
|
215
|
+
__metadata("design:type", Function),
|
|
216
|
+
__metadata("design:paramtypes", [Object]),
|
|
217
|
+
__metadata("design:returntype", Promise)
|
|
218
|
+
], NftsController.prototype, "getTrendingCollections", null);
|
|
219
|
+
__decorate([
|
|
220
|
+
ValidateInput(nftsByContractAddressValidator),
|
|
221
|
+
__metadata("design:type", Function),
|
|
222
|
+
__metadata("design:paramtypes", [Object]),
|
|
223
|
+
__metadata("design:returntype", Promise)
|
|
224
|
+
], NftsController.prototype, "getByContractAddress", null);
|
|
225
|
+
__decorate([
|
|
226
|
+
ValidateInput(nftDetailsValidator),
|
|
227
|
+
__metadata("design:type", Function),
|
|
228
|
+
__metadata("design:paramtypes", [Object]),
|
|
229
|
+
__metadata("design:returntype", Promise)
|
|
230
|
+
], NftsController.prototype, "getNFTDetails", null);
|
|
231
|
+
__decorate([
|
|
232
|
+
ValidateInput(nftCollectionDetailsValidator),
|
|
233
|
+
__metadata("design:type", Function),
|
|
234
|
+
__metadata("design:paramtypes", [Object]),
|
|
235
|
+
__metadata("design:returntype", Promise)
|
|
236
|
+
], NftsController.prototype, "getCollectionDetails", null);
|
|
237
|
+
__decorate([
|
|
238
|
+
ValidateInput(verifyOwnershipValidator),
|
|
239
|
+
__metadata("design:type", Function),
|
|
240
|
+
__metadata("design:paramtypes", [Object]),
|
|
241
|
+
__metadata("design:returntype", Promise)
|
|
242
|
+
], NftsController.prototype, "verifyOwnership", null);
|
|
243
|
+
|
|
244
|
+
export { NftsController };
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { __decorate, __metadata } from 'tslib';
|
|
2
|
+
import { DEFAULT_CHAIN } from '../utils/constants.js';
|
|
3
|
+
import { balancesByWalletAddressValidator } from '../types/tokens/getBalancesByWalletAddress.js';
|
|
4
|
+
import { CodegenEthMainnetBalancesByWalletENSDocument, CodegenPolygonMainnetBalancesByWalletENSDocument, CodegenEthSepoliaBalancesByWalletENSDocument, CodegenEthMainnetBalancesByWalletAddressDocument, CodegenPolygonMainnetBalancesByWalletAddressDocument, CodegenEthSepoliaBalancesByWalletAddressDocument } from '../graphql/generatedTypes.js';
|
|
5
|
+
import { emptyPageInfo } from '../utils/helpers.js';
|
|
6
|
+
import { formatQueryResult } from '../utils/postQueryFormatter.js';
|
|
7
|
+
import { isValidENSAddress } from '../utils/isValidENSAddress.js';
|
|
8
|
+
import { ValidateInput } from '../../lib/validation/ValidateInput.js';
|
|
9
|
+
|
|
10
|
+
class TokensController {
|
|
11
|
+
constructor(client, defaultChain = DEFAULT_CHAIN) {
|
|
12
|
+
this.client = client;
|
|
13
|
+
this.defaultChain = defaultChain;
|
|
14
|
+
}
|
|
15
|
+
async getBalancesByWallet(variables) {
|
|
16
|
+
const { address, ...allVariables } = variables;
|
|
17
|
+
if (isValidENSAddress(address)) {
|
|
18
|
+
return this.getBalancesByWalletENS({
|
|
19
|
+
ensName: address,
|
|
20
|
+
...allVariables,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
return this.getBalancesByWalletAddress({
|
|
24
|
+
address,
|
|
25
|
+
...allVariables,
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
async getBalancesByWalletENS(variables) {
|
|
29
|
+
const { chain, ...queryVariables } = variables;
|
|
30
|
+
const userChain = chain || this.defaultChain;
|
|
31
|
+
const query = {
|
|
32
|
+
ethereum: CodegenEthMainnetBalancesByWalletENSDocument,
|
|
33
|
+
polygon: CodegenPolygonMainnetBalancesByWalletENSDocument,
|
|
34
|
+
ethereumSepolia: CodegenEthSepoliaBalancesByWalletENSDocument,
|
|
35
|
+
};
|
|
36
|
+
const { data: { [userChain]: { walletByENS }, }, } = await this.client.query({
|
|
37
|
+
variables: queryVariables,
|
|
38
|
+
query: query[userChain],
|
|
39
|
+
});
|
|
40
|
+
if (!walletByENS?.tokenBalances?.length) {
|
|
41
|
+
// Address can still be valid ENS name, but not have any balances
|
|
42
|
+
const address = walletByENS?.address || '';
|
|
43
|
+
const ensName = walletByENS?.ensName || '';
|
|
44
|
+
return {
|
|
45
|
+
address: address,
|
|
46
|
+
ensName: ensName,
|
|
47
|
+
results: [],
|
|
48
|
+
pageInfo: emptyPageInfo,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
const formattedResult = formatQueryResult(walletByENS, 'tokenBalances', 'tokenBalancesPageInfo', null, this.flattenBalanceResponses // Remove the "contract" key and move info to balance result body
|
|
52
|
+
);
|
|
53
|
+
return formattedResult;
|
|
54
|
+
}
|
|
55
|
+
async getBalancesByWalletAddress(variables) {
|
|
56
|
+
const { chain, ...queryVariables } = variables;
|
|
57
|
+
const userChain = chain || this.defaultChain;
|
|
58
|
+
const query = {
|
|
59
|
+
ethereum: CodegenEthMainnetBalancesByWalletAddressDocument,
|
|
60
|
+
polygon: CodegenPolygonMainnetBalancesByWalletAddressDocument,
|
|
61
|
+
ethereumSepolia: CodegenEthSepoliaBalancesByWalletAddressDocument,
|
|
62
|
+
};
|
|
63
|
+
const { data: { [userChain]: { walletByAddress }, }, } = await this.client.query({
|
|
64
|
+
variables: queryVariables,
|
|
65
|
+
query: query[userChain],
|
|
66
|
+
});
|
|
67
|
+
if (!walletByAddress?.tokenBalances?.length) {
|
|
68
|
+
// Address can still be valid address, but not have any balances
|
|
69
|
+
const address = walletByAddress?.address || '';
|
|
70
|
+
const ensName = walletByAddress?.ensName || '';
|
|
71
|
+
return {
|
|
72
|
+
address: address,
|
|
73
|
+
ensName: ensName,
|
|
74
|
+
results: [],
|
|
75
|
+
pageInfo: emptyPageInfo,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
const formattedResult = formatQueryResult(walletByAddress, 'tokenBalances', 'tokenBalancesPageInfo', null, this.flattenBalanceResponses // Remove the "contract" key and move info to balance result body
|
|
79
|
+
);
|
|
80
|
+
return formattedResult;
|
|
81
|
+
}
|
|
82
|
+
flattenBalanceResponses(response) {
|
|
83
|
+
const modifiedResults = response.results.map((result) => {
|
|
84
|
+
const { contract: { ...contractInfo }, ...balanceInfo } = result;
|
|
85
|
+
return { ...balanceInfo, ...contractInfo };
|
|
86
|
+
});
|
|
87
|
+
response.results = modifiedResults;
|
|
88
|
+
return response;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
__decorate([
|
|
92
|
+
ValidateInput(balancesByWalletAddressValidator),
|
|
93
|
+
__metadata("design:type", Function),
|
|
94
|
+
__metadata("design:paramtypes", [Object]),
|
|
95
|
+
__metadata("design:returntype", Promise)
|
|
96
|
+
], TokensController.prototype, "getBalancesByWallet", null);
|
|
97
|
+
|
|
98
|
+
export { TokensController };
|