UNPKG

@superfluid-finance/sdk-core

Version:
114 lines 5.01 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SubgraphQueryHandler = void 0; const ethers_1 = require("ethers"); const lodash_1 = __importDefault(require("lodash")); const Query_1 = require("../Query"); const pagination_1 = require("../pagination"); const utils_1 = require("../utils"); const normalizeSubgraphFilter_1 = require("./normalizeSubgraphFilter"); /** * A base class to handle common Subgraph query logic. */ class SubgraphQueryHandler { constructor() { /** * For every primary address field key there are more fields generated which it should look for addresses. * NOTE: The implementation is a bit "magical" but it rids of a bunch of boilerplate and creates a single point for editing. */ this.getRelevantAddressesFromFilterByAddressFieldKeys = (filter, addressFieldKeys) => addressFieldKeys .map((key) => [ filter[key], filter[`${String(key)}_in`], filter[`${String(key)}_not`], filter[`${String(key)}_not_in`], ] .filter((x) => typeof x !== "undefined") .flat() .filter((x) => !!x)) .flat(); } getRelevantAddressesFromFilter(filter) { if (!filter) { return { tokens: [], accounts: [], }; } const addressFieldKeys = this.getAddressFieldKeysFromFilter(); const tokenAddresses = this.getRelevantAddressesFromFilterByAddressFieldKeys(filter, addressFieldKeys.tokenKeys); const accountAddresses = this.getRelevantAddressesFromFilterByAddressFieldKeys(filter, addressFieldKeys.accountKeys); return { tokens: lodash_1.default.uniq(tokenAddresses), accounts: lodash_1.default.uniq(accountAddresses), }; } getRelevantAddressesFromResult(result) { if (!result) { return { tokens: [], accounts: [], }; } const intermediate = this.getRelevantAddressesFromResultCore(result); return { tokens: lodash_1.default.uniq(intermediate.tokens.flat().filter((x) => !!x)), accounts: lodash_1.default.uniq(intermediate.accounts.flat().filter((x) => !!x)), }; } async get(subgraphClient, query) { var _a; if (!query.id) { return null; } const response = await this.querySubgraph(subgraphClient, { where: { id: ethers_1.ethers.utils.isAddress(query.id) ? query.id.toLowerCase() : query.id, }, skip: 0, take: 1, block: query.block, }); return (_a = this.mapFromSubgraphResponse(response)[0]) !== null && _a !== void 0 ? _a : null; } async list(subgraphClient, query) { var _a; // Note: Could possibly optimize here to not create a new internal function every time. const queryFunction = async (paging) => { var _a, _b, _c; const subgraphFilter = (0, utils_1.typeGuard)((0, normalizeSubgraphFilter_1.normalizeSubgraphFilter)({ ...((_a = query.filter) !== null && _a !== void 0 ? _a : {}), id_gt: paging.lastId, })); const subgraphQueryVariables = (0, utils_1.typeGuard)({ where: (0, normalizeSubgraphFilter_1.normalizeSubgraphFilter)(subgraphFilter), orderBy: (_b = query.order) === null || _b === void 0 ? void 0 : _b.orderBy, orderDirection: (_c = query.order) === null || _c === void 0 ? void 0 : _c.orderDirection, first: (0, pagination_1.takePlusOne)(paging), skip: (0, pagination_1.isPageNumberPaging)(paging) ? (paging.pageNumber - 1) * paging.take : paging.skip, block: query.block, }); const subgraphResponse = await this.querySubgraph(subgraphClient, subgraphQueryVariables); const mappedResult = this.mapFromSubgraphResponse(subgraphResponse); return (0, pagination_1.createPagedResult)(mappedResult, paging); }; if ((0, pagination_1.isAllPaging)(query.pagination)) { return (0, pagination_1.createPagedResult)(await (0, Query_1.listAllResults)(queryFunction), query.pagination); } else { return queryFunction((_a = query.pagination) !== null && _a !== void 0 ? _a : (0, pagination_1.createSkipPaging)()); } } async querySubgraph(subgraphClient, variables) { return await subgraphClient.request(this.requestDocument, variables); } } exports.SubgraphQueryHandler = SubgraphQueryHandler; //# sourceMappingURL=subgraphQueryHandler.js.map