UNPKG

@basementdev/sdk

Version:
328 lines 21 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; exports.__esModule = true; exports.BasementSDK = exports.DEFAULT_ENDPOINT = void 0; var graphql_request_1 = require("graphql-request"); var sdk_1 = require("./sdk"); var isPropertyIncluded_1 = __importDefault(require("./utils/isPropertyIncluded")); var parseIncludeOptions_1 = require("./utils/parseIncludeOptions"); exports.DEFAULT_ENDPOINT = "https://beta.basement.dev/v2/graphql"; var BasementSDK = /** @class */ (function () { function BasementSDK(opts) { var _this = this; this.withWrapper = function ( // eslint-disable-next-line no-unused-vars action) { return __awaiter(_this, void 0, void 0, function () { var headers, result; return __generator(this, function (_a) { switch (_a.label) { case 0: headers = { "X-Basement-SDK": process.env.npm_package_version }; if (this.apiKey) { headers["x-basement-api-key"] = this.apiKey; } return [4 /*yield*/, action(headers)]; case 1: result = _a.sent(); return [2 /*return*/, result]; } }); }); }; var _a = opts || {}, apiKey = _a.apiKey, _b = _a.endpoint, endpoint = _b === void 0 ? exports.DEFAULT_ENDPOINT : _b; this.opts = opts; this.client = new graphql_request_1.GraphQLClient(endpoint); this.opts = opts; this.apiKey = apiKey; this.sdk = (0, sdk_1.getSdk)(this.client, this.withWrapper); } /** * Queries information about a specific token */ BasementSDK.prototype.token = function (_a) { var contract = _a.contract, tokenId = _a.tokenId, include = _a.include; return __awaiter(this, void 0, void 0, function () { var data; return __generator(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, this.sdk.token(__assign({ contract: contract, tokenId: tokenId }, (0, parseIncludeOptions_1.parseTokenOpts)(include)))]; case 1: data = _b.sent(); return [2 /*return*/, data.token]; } }); }); }; /** * Query tokens that satisfy the given filter(s) */ BasementSDK.prototype.tokens = function (params) { return __awaiter(this, void 0, void 0, function () { var _a, before, after, filter, include, limit, includeTotalCount, data; return __generator(this, function (_b) { switch (_b.label) { case 0: _a = params || {}, before = _a.before, after = _a.after, filter = _a.filter, include = _a.include, limit = _a.limit; includeTotalCount = include === null || include === void 0 ? void 0 : include.totalCount; return [4 /*yield*/, this.sdk.tokens(__assign({ before: before, filter: filter, after: after, includeTotalCount: includeTotalCount, limit: limit }, (0, parseIncludeOptions_1.parseTokenOpts)(include)))]; case 1: data = _b.sent(); return [2 /*return*/, data.tokens]; } }); }); }; /** * Queries information about an address */ BasementSDK.prototype.address = function (_a) { var address = _a.address, include = _a.include; return __awaiter(this, void 0, void 0, function () { var includeTokens, tokensLimit, tokensIncludeOptions, includeProfile, includeReverseProfile, data; return __generator(this, function (_b) { switch (_b.label) { case 0: includeTokens = !!(include === null || include === void 0 ? void 0 : include.tokens); tokensLimit = 10; tokensIncludeOptions = {}; if (typeof (include === null || include === void 0 ? void 0 : include.tokens) !== "boolean" && includeTokens) { tokensLimit = include === null || include === void 0 ? void 0 : include.tokens.limit; tokensIncludeOptions = (0, parseIncludeOptions_1.parseTokenOpts)(include === null || include === void 0 ? void 0 : include.tokens); } includeProfile = include === null || include === void 0 ? void 0 : include.profile; includeReverseProfile = include === null || include === void 0 ? void 0 : include.reverseProfile; return [4 /*yield*/, this.sdk.address(__assign({ address: address, includeProfile: includeProfile, includeReverseProfile: includeReverseProfile, includeTokens: includeTokens, tokensLimit: tokensLimit }, tokensIncludeOptions))]; case 1: data = _b.sent(); return [2 /*return*/, data.address]; } }); }); }; /** * Queries information about a transaction */ BasementSDK.prototype.transaction = function (_a) { var hash = _a.hash, include = _a.include; return __awaiter(this, void 0, void 0, function () { var transaction; return __generator(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, this.sdk.transaction(__assign({ hash: hash }, (0, parseIncludeOptions_1.parseTransactionOpts)(include)))]; case 1: transaction = (_b.sent()).transaction; return [2 /*return*/, transaction]; } }); }); }; /** * Query transactions that satisfy the given filter(s) */ BasementSDK.prototype.transactions = function (params) { return __awaiter(this, void 0, void 0, function () { var _a, before, after, filter, include, limit, reversed, includeTotalCount, transactions; return __generator(this, function (_b) { switch (_b.label) { case 0: _a = params || {}, before = _a.before, after = _a.after, filter = _a.filter, include = _a.include, limit = _a.limit, reversed = _a.reversed; includeTotalCount = include === null || include === void 0 ? void 0 : include.totalCount; return [4 /*yield*/, this.sdk.transactions(__assign({ before: before, limit: limit, reversed: reversed, filter: filter, after: after, includeTotalCount: includeTotalCount }, (0, parseIncludeOptions_1.parseTransactionOpts)(include)))]; case 1: transactions = (_b.sent()).transactions; return [2 /*return*/, transactions]; } }); }); }; /** * Query transaction logs that satisfy the given filter(s) */ BasementSDK.prototype.transactionLogs = function (params) { return __awaiter(this, void 0, void 0, function () { var _a, before, after, filter, include, limit, reversed, includeTotalCount, includeContractReverseProfile, includeTransaction, includeBlockHash, transactionOpts, transactionLogs; return __generator(this, function (_b) { switch (_b.label) { case 0: _a = params || {}, before = _a.before, after = _a.after, filter = _a.filter, include = _a.include, limit = _a.limit, reversed = _a.reversed; includeTotalCount = include === null || include === void 0 ? void 0 : include.totalCount; includeContractReverseProfile = !!(include === null || include === void 0 ? void 0 : include.address); includeTransaction = !!(include === null || include === void 0 ? void 0 : include.transaction); includeBlockHash = include === null || include === void 0 ? void 0 : include.blockHash; transactionOpts = {}; if (typeof (include === null || include === void 0 ? void 0 : include.transaction) !== "boolean") { transactionOpts = (0, parseIncludeOptions_1.parseTransactionOpts)(include === null || include === void 0 ? void 0 : include.transaction); } return [4 /*yield*/, this.sdk.transactionLogs(__assign(__assign({ before: before, after: after, filter: filter, limit: limit, reversed: reversed, includeTotalCount: includeTotalCount, includeBlockHash: includeBlockHash, includeContractReverseProfile: includeContractReverseProfile, includeTransaction: includeTransaction }, transactionOpts), { includeTransactionLogs: false }))]; case 1: transactionLogs = (_b.sent()).transactionLogs; return [2 /*return*/, transactionLogs]; } }); }); }; /** * Refreshes metadata of a specific NFT */ BasementSDK.prototype.nonFungibleTokenRefresh = function (_a) { var contract = _a.contract, tokenId = _a.tokenId; return __awaiter(this, void 0, void 0, function () { var nonFungibleTokenRefresh; return __generator(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, this.sdk.nonFungibleTokenRefresh({ contract: contract, tokenId: tokenId })]; case 1: nonFungibleTokenRefresh = (_b.sent()).nonFungibleTokenRefresh; return [2 /*return*/, nonFungibleTokenRefresh]; } }); }); }; /** * Query ERC721 transfers that satisfy the given filter(s) */ BasementSDK.prototype.erc721Transfers = function (params) { return __awaiter(this, void 0, void 0, function () { var _a, include, before, after, filter, limit, includeTransferContract, includeTotalCount, includeToken, includeBlockHash, includeTransferContractReverseProfile, parsedTokenOpts, includeSale, _b, includeErc721TransferSaleMaker, includeErc721TransferSaleTaker, includeErc721TransferSaleMakerReverseProfile, includeErc721TransferSaleTakerReverseProfile, includeTokenSales, includeTransaction, parsedTransactionOpts, parsedTransferAndRecipientOpts, erc721Transfers; return __generator(this, function (_c) { switch (_c.label) { case 0: _a = params || {}, include = _a.include, before = _a.before, after = _a.after, filter = _a.filter, limit = _a.limit; includeTransferContract = !!(include === null || include === void 0 ? void 0 : include.contract); includeTotalCount = include === null || include === void 0 ? void 0 : include.totalCount; includeToken = !!(include === null || include === void 0 ? void 0 : include.token); includeBlockHash = include === null || include === void 0 ? void 0 : include.blockHash; includeTransferContractReverseProfile = (0, isPropertyIncluded_1["default"])(include === null || include === void 0 ? void 0 : include.contract, "reverseProfile"); parsedTokenOpts = {}; if (typeof (include === null || include === void 0 ? void 0 : include.token) !== "boolean" && includeToken) { parsedTokenOpts = (0, parseIncludeOptions_1.parseTokenOpts)(include === null || include === void 0 ? void 0 : include.token); } includeSale = !!(include === null || include === void 0 ? void 0 : include.sale); _b = (0, parseIncludeOptions_1.parseSaleOpts)(include === null || include === void 0 ? void 0 : include.sale, "erc721TransferSale"), includeErc721TransferSaleMaker = _b.includeErc721TransferSaleMaker, includeErc721TransferSaleTaker = _b.includeErc721TransferSaleTaker, includeErc721TransferSaleMakerReverseProfile = _b.includeErc721TransferSaleMakerReverseProfile, includeErc721TransferSaleTakerReverseProfile = _b.includeErc721TransferSaleTakerReverseProfile; includeTokenSales = (0, isPropertyIncluded_1["default"])(include === null || include === void 0 ? void 0 : include.token, "sales"); includeTransaction = !!(include === null || include === void 0 ? void 0 : include.transaction); parsedTransactionOpts = (0, parseIncludeOptions_1.parseTransactionOpts)(include === null || include === void 0 ? void 0 : include.transaction); parsedTransferAndRecipientOpts = (0, parseIncludeOptions_1.parseTransferSenderRecipientOpts)({ from: include === null || include === void 0 ? void 0 : include.from, to: include === null || include === void 0 ? void 0 : include.to }); return [4 /*yield*/, this.sdk.erc721Transfers(__assign(__assign(__assign(__assign({}, parsedTransactionOpts), parsedTokenOpts), parsedTransferAndRecipientOpts), { before: before, after: after, filter: filter, limit: limit, includeSale: includeSale, includeBlockHash: includeBlockHash, includeErc721TransferSaleMaker: includeErc721TransferSaleMaker, includeErc721TransferSaleTaker: includeErc721TransferSaleTaker, includeErc721TransferSaleMakerReverseProfile: includeErc721TransferSaleMakerReverseProfile, includeErc721TransferSaleTakerReverseProfile: includeErc721TransferSaleTakerReverseProfile, includeTotalCount: includeTotalCount, includeToken: includeToken, includeTokenSales: includeTokenSales, includeTransaction: includeTransaction, includeTransferContract: includeTransferContract, includeTransferContractReverseProfile: includeTransferContractReverseProfile }))]; case 1: erc721Transfers = (_c.sent()).erc721Transfers; return [2 /*return*/, erc721Transfers]; } }); }); }; /** * Query ERC20 transfers that satisfy the given filter(s) */ BasementSDK.prototype.erc20Transfers = function (params) { return __awaiter(this, void 0, void 0, function () { var _a, after, before, filter, include, limit, includeTotalCount, includeTransaction, parsedTransactionOpts, parsedTransferAndRecipientOpts, erc20Transfers; return __generator(this, function (_b) { switch (_b.label) { case 0: _a = params || {}, after = _a.after, before = _a.before, filter = _a.filter, include = _a.include, limit = _a.limit; includeTotalCount = include === null || include === void 0 ? void 0 : include.totalCount; includeTransaction = !!(include === null || include === void 0 ? void 0 : include.transaction); parsedTransactionOpts = (0, parseIncludeOptions_1.parseTransactionOpts)(include === null || include === void 0 ? void 0 : include.transaction); parsedTransferAndRecipientOpts = (0, parseIncludeOptions_1.parseTransferSenderRecipientOpts)({ from: include === null || include === void 0 ? void 0 : include.from, to: include === null || include === void 0 ? void 0 : include.to }); return [4 /*yield*/, this.sdk.erc20Transfers(__assign(__assign(__assign({}, parsedTransactionOpts), parsedTransferAndRecipientOpts), { includeTotalCount: includeTotalCount, includeTransaction: includeTransaction, before: before, after: after, filter: filter, limit: limit }))]; case 1: erc20Transfers = (_b.sent()).erc20Transfers; return [2 /*return*/, erc20Transfers]; } }); }); }; /** * Query ERC20 balances from a given address */ BasementSDK.prototype.erc20Balances = function (_a) { var filter = _a.filter, include = _a.include; return __awaiter(this, void 0, void 0, function () { var includeOwner, parsedOwnerOpts, erc20Balances; return __generator(this, function (_b) { switch (_b.label) { case 0: includeOwner = !!(include === null || include === void 0 ? void 0 : include.owner); parsedOwnerOpts = (0, parseIncludeOptions_1.parseOwnerOpts)(include === null || include === void 0 ? void 0 : include.owner); return [4 /*yield*/, this.sdk.erc20Balances(__assign(__assign({}, parsedOwnerOpts), { includeOwner: includeOwner, filter: filter }))]; case 1: erc20Balances = (_b.sent()).erc20Balances; return [2 /*return*/, erc20Balances]; } }); }); }; BasementSDK.prototype.request = function (query, variables) { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { return [2 /*return*/, this.withWrapper(function (wrappedRequestHeaders) { return _this.client.request(query, variables, wrappedRequestHeaders); }, "custom")]; }); }); }; return BasementSDK; }()); exports.BasementSDK = BasementSDK; //# sourceMappingURL=basement.js.map