UNPKG

@harmoniclabs/blockfrost-pluts

Version:

@blockfrost/blockfrost-js extension based on plu-ts types

658 lines (657 loc) 31.9 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 (g && (g = 0, op[0] && (_ = 0)), _) 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 __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BlockfrostPluts = exports.paginationOptsToStr = exports.defaultPaginationOpts = void 0; var buildooor_1 = require("@harmoniclabs/buildooor"); var plutus_data_1 = require("@harmoniclabs/plutus-data"); var cbor_1 = require("@harmoniclabs/cbor"); var uint8array_utils_1 = require("@harmoniclabs/uint8array-utils"); var crypto_1 = require("@harmoniclabs/crypto"); var adaptProtocolParams_1 = require("./utils/adaptProtocolParams.js"); var evaluatePlutusCosts_1 = require("./utils/evaluatePlutusCosts.js"); function forceTxOutRefStr(canResolve) { if (typeof canResolve === "string") return canResolve; if ((0, buildooor_1.isIUTxO)(canResolve)) canResolve = canResolve.utxoRef; if (canResolve instanceof buildooor_1.TxOutRef) return canResolve.toString(); if ((0, buildooor_1.isITxOutRef)(canResolve)) return "".concat(canResolve.id.toString(), "#").concat(canResolve.index); console.error(canResolve); throw new Error('"forceTxOutRefStr" expects a "CanResolveToUTxO"'); } var DEFAULT_PAGINATION_COUNT = 100; var DEFAULT_PAGINATION_PAGE = 1; exports.defaultPaginationOpts = Object.freeze({ count: DEFAULT_PAGINATION_COUNT, page: DEFAULT_PAGINATION_PAGE }); function paginationOptsToStr(_a) { var count = _a.count, page = _a.page, order = _a.order; var str = ""; var hasAny = false; if (typeof count === "number") { str += "?count=" + count.toString(); hasAny = true; } if (typeof page === "number") { str += hasAny ? "&" : "?"; str += "page=" + page.toString(); hasAny = true; } if (order === "asc" || order === "desc") { str += hasAny ? "&" : "?"; str += "order=" + order; } return str; } exports.paginationOptsToStr = paginationOptsToStr; function getUtxoFromBlockfrostJson(_a) { var address = _a.address, tx_hash = _a.tx_hash, output_index = _a.output_index, amount = _a.amount, // block, data_hash = _a.data_hash, inline_datum = _a.inline_datum, reference_script_hash = _a.reference_script_hash; return __awaiter(this, void 0, void 0, function () { var datum, refScript, _b; return __generator(this, function (_c) { switch (_c.label) { case 0: datum = inline_datum ? (0, plutus_data_1.dataFromCbor)(inline_datum) : data_hash ? new buildooor_1.Hash32(data_hash) : undefined; if (!(typeof reference_script_hash === "string")) return [3 /*break*/, 2]; return [4 /*yield*/, this.resolveScriptHash(reference_script_hash)]; case 1: _b = _c.sent(); return [3 /*break*/, 3]; case 2: _b = undefined; _c.label = 3; case 3: refScript = (_b); return [2 /*return*/, new buildooor_1.UTxO({ utxoRef: { id: tx_hash, index: output_index }, resolved: { address: buildooor_1.Address.fromString(address), value: buildooor_1.Value.fromUnits(amount), datum: datum, refScript: refScript } })]; } }); }); } var BlockfrostPluts = /** @class */ (function () { function BlockfrostPluts(_a) { var projectId = _a.projectId, customBackend = _a.customBackend, network = _a.network; if (typeof projectId !== "string" && typeof customBackend !== "string") throw new Error("blockfrost projectId not a string"); network !== null && network !== void 0 ? network : (network = "mainnet"); if (typeof projectId === "string") { network = (projectId.startsWith("mainnet") ? "mainnet" : projectId.startsWith("preprod") ? "preprod" : projectId.startsWith("preview") ? "preview" : projectId.startsWith("sanchonet") ? "sanchonet" : ""); if (network === "") throw new Error("invalid projectId"); } var url = customBackend !== null && customBackend !== void 0 ? customBackend : (network === "mainnet" ? "https://cardano-mainnet.blockfrost.io/api/v0" : network === "preprod" ? "https://cardano-preprod.blockfrost.io/api/v0" : network === "preview" ? "https://cardano-preview.blockfrost.io/api/v0" : "https://cardano-mainnet.blockfrost.io/api/v0"); Object.defineProperties(this, { network: { value: network, writable: false, enumerable: true, configurable: false, }, url: { value: url, writable: false, enumerable: true, configurable: false, }, projectId: { value: projectId !== null && projectId !== void 0 ? projectId : "", writable: false, enumerable: true, configurable: false, } }); } /** @since 0.1.4 */ BlockfrostPluts.prototype.submitTx = function (tx) { return __awaiter(this, void 0, void 0, function () { var res, _a, _b; return __generator(this, function (_c) { switch (_c.label) { case 0: tx = typeof tx === "string" ? buildooor_1.Tx.fromCbor(tx) : tx; return [4 /*yield*/, fetch("".concat(this.url, "/tx/submit"), { method: "POST", headers: { "Content-Type": "application/cbor", "project_id": this.projectId }, body: tx.toCbor().buffer })]; case 1: res = _c.sent(); if (!!res.ok) return [3 /*break*/, 3]; _a = Error.bind; _b = res.statusText; return [4 /*yield*/, res.text()]; case 2: throw new (_a.apply(Error, [void 0, _b + (_c.sent())]))(); case 3: return [2 /*return*/, tx.hash.toString()]; } }); }); }; ; /** @since 0.1.13 */ BlockfrostPluts.prototype.evaluatePlutusCosts = function (tx) { return __awaiter(this, void 0, void 0, function () { var res, _a, _b, _c, _d; return __generator(this, function (_e) { switch (_e.label) { case 0: tx = typeof tx === "string" ? buildooor_1.Tx.fromCbor(tx) : tx; return [4 /*yield*/, fetch("".concat(this.url, "/utils/txs/evaluate"), { method: "POST", headers: { "Content-Type": "application/cbor", "Accept": "application/json", "project_id": this.projectId }, body: tx.toCbor().toString() })]; case 1: res = _e.sent(); if (!!res.ok) return [3 /*break*/, 3]; _a = Error.bind; _b = res.statusText; return [4 /*yield*/, res.text()]; case 2: throw new (_a.apply(Error, [void 0, _b + (_e.sent())]))(); case 3: _c = evaluatePlutusCosts_1.getRealTxRedeemers; _d = [tx]; return [4 /*yield*/, res.json()]; case 4: return [2 /*return*/, _c.apply(void 0, _d.concat([_e.sent()]))]; } }); }); }; ; BlockfrostPluts.prototype.get = function (url) { return __awaiter(this, void 0, void 0, function () { var res, _a; return __generator(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, fetch(url, { headers: { "project_id": this.projectId } })]; case 1: res = _b.sent(); if (!!res.ok) return [3 /*break*/, 3]; _a = res.statusText; return [4 /*yield*/, res.text()]; case 2: throw _a + (_b.sent()); case 3: return [4 /*yield*/, res.json()]; case 4: return [2 /*return*/, _b.sent()]; } }); }); }; /** @since 0.1.12 */ BlockfrostPluts.prototype.getChainTip = function () { return this.get("".concat(this.url, "/blocks/latest")); }; /** @since 0.1.3 */ BlockfrostPluts.prototype.getGenesisInfos = function () { return __awaiter(this, void 0, void 0, function () { var genesis; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.get("".concat(this.url, "/genesis"))]; case 1: genesis = _a.sent(); return [2 /*return*/, { slotLengthInMilliseconds: Number(genesis.slot_length) * 1000, systemStartPOSIX: Number(genesis.system_start) * 1000 }]; } }); }); }; /** @since 0.1.1 */ BlockfrostPluts.prototype.epochsParameters = function (epoch_no) { return __awaiter(this, void 0, void 0, function () { var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: _a = adaptProtocolParams_1.adaptProtocolParams; return [4 /*yield*/, this.get("".concat(this.url, "/epochs/").concat(epoch_no, "/parameters"))]; case 1: return [2 /*return*/, _a.apply(void 0, [_b.sent()])]; } }); }); }; /** @since 0.1.1 */ BlockfrostPluts.prototype.epochsLatestParameters = function () { return this.getProtocolParameters(); }; /** @since 0.1.0 */ BlockfrostPluts.prototype.getProtocolParameters = function () { return __awaiter(this, void 0, void 0, function () { var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: _a = adaptProtocolParams_1.adaptProtocolParams; return [4 /*yield*/, this.get("".concat(this.url, "/epochs/latest/parameters"))]; case 1: return [2 /*return*/, _a.apply(void 0, [_b.sent()])]; } }); }); }; /** @since 0.1.0 */ BlockfrostPluts.prototype.resolveUtxos = function (utxos) { return __awaiter(this, void 0, void 0, function () { var refs, txHashes, txns; var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: refs = utxos.map(function (u) { var _a = __read(forceTxOutRefStr(u).split("#"), 2), id = _a[0], idx = _a[1]; return new buildooor_1.TxOutRef({ id: id, index: parseInt(idx) }); }); txHashes = refs.map(function (ref) { return ref.id.toString(); }) // filter out replicates .filter(function (h, i, thisArr) { return thisArr.indexOf(h) === i; }); return [4 /*yield*/, Promise.all(txHashes.map(function (h) { return _this.get("".concat(_this.url, "/txs/").concat(h, "/utxos")); }))]; case 1: txns = _a.sent(); return [4 /*yield*/, Promise.all(refs.map(function (ref) { return __awaiter(_this, void 0, void 0, function () { var tx, resolved, refScript, _a; return __generator(this, function (_b) { switch (_b.label) { case 0: tx = txns.find(function (tx) { return tx.hash === ref.id.toString(); }); if (!tx) throw new Error("unresolved utxo: " + ref.toString()); resolved = tx.outputs.find(function (out) { return out.output_index === ref.index; }); if (!resolved) throw new Error("unresolved utxo: " + ref.toString()); if (!resolved.reference_script_hash) return [3 /*break*/, 2]; return [4 /*yield*/, this.resolveScriptHash(resolved.reference_script_hash)]; case 1: _a = _b.sent(); return [3 /*break*/, 3]; case 2: _a = undefined; _b.label = 3; case 3: refScript = _a; return [2 /*return*/, new buildooor_1.UTxO({ utxoRef: ref, resolved: { address: buildooor_1.Address.fromString(resolved.address), value: buildooor_1.Value.fromUnits(resolved.amount), datum: resolved.inline_datum ? (0, plutus_data_1.dataFromCbor)(resolved.inline_datum) : resolved.data_hash ? new buildooor_1.Hash32(resolved.data_hash) : undefined, refScript: refScript } })]; } }); }); }))]; case 2: return [2 /*return*/, _a.sent()]; } }); }); }; /** @since 0.1.0 */ BlockfrostPluts.prototype.resolveDatumHashes = function (hashes) { var _this = this; return Promise.all(hashes.map(function (h) { return __awaiter(_this, void 0, void 0, function () { var hStr; var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: hStr = h.toString(); _a = { hash: hStr }; return [4 /*yield*/, this.get("".concat(this.url, "/scripts/datum/").concat(hStr))]; case 1: return [2 /*return*/, (_a.datum = (_b.sent()).cbor, _a)]; } }); }); })); }; /** @since 0.1.1 */ BlockfrostPluts.prototype.addressesInfos = function (address) { return this.addressInfos(address); }; /** @since 0.1.1 */ BlockfrostPluts.prototype.addressInfos = function (address) { return __awaiter(this, void 0, void 0, function () { var response, result; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.get("".concat(this.url, "/addresses/").concat(address.toString()))]; case 1: response = _a.sent(); result = {}; result.address = buildooor_1.Address.fromString(response.address); result.totAmount = buildooor_1.Value.fromUnits(response.amount); result.stakeAddress = response.stake_address ? buildooor_1.StakeAddress.fromString(response.stake_address) : undefined; result.type = response.type; result.script = response.script; return [2 /*return*/, result]; } }); }); }; /** @since 0.1.1 */ BlockfrostPluts.prototype.addressTotalAmount = function (address) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.addressInfos(address)]; case 1: return [2 /*return*/, (_a.sent()).totAmount]; } }); }); }; BlockfrostPluts.prototype.addressTxs = function (addr) { return __awaiter(this, void 0, void 0, function () { var addrStr, pastTxsObjs; return __generator(this, function (_a) { switch (_a.label) { case 0: addrStr = addr.toString(); return [4 /*yield*/, this.get(this.url + "/addresses/".concat(addrStr, "/transactions"))]; case 1: pastTxsObjs = _a.sent(); return [2 /*return*/, (pastTxsObjs.map(function (tx) { return new buildooor_1.Hash32(tx.tx_hash); })).reverse()]; } }); }); }; /** @since 0.1.0 */ BlockfrostPluts.prototype.addressesUtxos = function (address, pagination) { return this.addressUtxos(address, pagination); }; BlockfrostPluts.prototype.getUtxos = function (address, pagination) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.addressUtxos(address, pagination)]; }); }); }; /** @since 0.3.0 */ BlockfrostPluts.prototype._addrUtxoQuery = function (baseUrl, pagination) { var _a, _b; return __awaiter(this, void 0, void 0, function () { var explicitPagination, countPerPage, page, _utxos, _c, _d, _e, _f; return __generator(this, function (_g) { switch (_g.label) { case 0: explicitPagination = !!pagination; pagination = explicitPagination ? __assign(__assign({}, exports.defaultPaginationOpts), pagination) : __assign({}, exports.defaultPaginationOpts); countPerPage = (_a = pagination.count) !== null && _a !== void 0 ? _a : DEFAULT_PAGINATION_COUNT; page = (_b = pagination.page) !== null && _b !== void 0 ? _b : DEFAULT_PAGINATION_PAGE; _utxos = []; if (!explicitPagination) return [3 /*break*/, 2]; _d = (_c = _utxos).concat; return [4 /*yield*/, this.get("".concat(baseUrl).concat(paginationOptsToStr(pagination)))]; case 1: _utxos = _d.apply(_c, [_g.sent()]); return [3 /*break*/, 5]; case 2: _f = (_e = _utxos).concat; return [4 /*yield*/, this.get("".concat(baseUrl).concat(paginationOptsToStr(pagination)))]; case 3: _utxos = _f.apply(_e, [_g.sent()]); page++; pagination.page = page; _g.label = 4; case 4: if (_utxos.length === (countPerPage * (page - 1))) return [3 /*break*/, 2]; _g.label = 5; case 5: return [4 /*yield*/, Promise.all(_utxos.map(getUtxoFromBlockfrostJson.bind(this)))]; case 6: return [2 /*return*/, _g.sent()]; } }); }); }; /** @since 0.3.0 */ BlockfrostPluts.prototype.addressAssetUtxos = function (address, assetUnit, pagination) { return __awaiter(this, void 0, void 0, function () { var baseUrl; return __generator(this, function (_a) { address = address.toString(); baseUrl = "".concat(this.url, "/addresses/").concat(address, "/utxos/").concat(assetUnit); return [2 /*return*/, this._addrUtxoQuery(baseUrl, pagination)]; }); }); }; /** * @since 0.4.1 **/ BlockfrostPluts.prototype.assetAddresses = function (assetUnit, pagination) { return __awaiter(this, void 0, void 0, function () { var baseUrl; return __generator(this, function (_a) { baseUrl = "".concat(this.url, "/assets/").concat(assetUnit, "/addresses"); return [2 /*return*/, this._addressesWithAssetQuery(baseUrl, pagination)]; }); }); }; /** * @since 0.4.1 **/ BlockfrostPluts.prototype.addressesWithAsset = function (assetUnit, pagination) { return __awaiter(this, void 0, void 0, function () { var baseUrl; return __generator(this, function (_a) { baseUrl = "".concat(this.url, "/assets/").concat(assetUnit, "/addresses"); return [2 /*return*/, this._addressesWithAssetQuery(baseUrl, pagination)]; }); }); }; /** * @since 0.4.1 **/ BlockfrostPluts.prototype._addressesWithAssetQuery = function (baseUrl, pagination) { var _a, _b; return __awaiter(this, void 0, void 0, function () { var explicitPagination, countPerPage, page, _addrs, _c, _d, _e, _f, _g, _h, _j, _k; return __generator(this, function (_l) { switch (_l.label) { case 0: explicitPagination = !!pagination; pagination = explicitPagination ? __assign(__assign({}, exports.defaultPaginationOpts), pagination) : __assign({}, exports.defaultPaginationOpts); countPerPage = (_a = pagination.count) !== null && _a !== void 0 ? _a : DEFAULT_PAGINATION_COUNT; page = (_b = pagination.page) !== null && _b !== void 0 ? _b : DEFAULT_PAGINATION_PAGE; _addrs = []; if (!explicitPagination) return [3 /*break*/, 2]; _d = (_c = _addrs.push).apply; _e = [_addrs]; _f = [[]]; return [4 /*yield*/, this.get("".concat(baseUrl).concat(paginationOptsToStr(pagination)))]; case 1: _d.apply(_c, _e.concat([__spreadArray.apply(void 0, _f.concat([__read.apply(void 0, [(_l.sent())]), false]))])); return [3 /*break*/, 5]; case 2: _h = (_g = _addrs.push).apply; _j = [_addrs]; _k = [[]]; return [4 /*yield*/, this.get("".concat(baseUrl).concat(paginationOptsToStr(pagination)))]; case 3: _h.apply(_g, _j.concat([__spreadArray.apply(void 0, _k.concat([__read.apply(void 0, [(_l.sent())]), false]))])); page++; pagination.page = page; _l.label = 4; case 4: if (_addrs.length === (countPerPage * (page - 1))) return [3 /*break*/, 2]; _l.label = 5; case 5: return [2 /*return*/, _addrs]; } }); }); }; /** * @since 0.1.0 * * @version 0.3.0 adds default pagination and full query on missing pagination **/ BlockfrostPluts.prototype.addressUtxos = function (address, pagination) { return __awaiter(this, void 0, void 0, function () { var baseUrl; return __generator(this, function (_a) { address = address.toString(); baseUrl = "".concat(this.url, "/addresses/").concat(address, "/utxos"); return [2 /*return*/, this._addrUtxoQuery(baseUrl, pagination)]; }); }); }; ; /** @since 0.1.0 */ BlockfrostPluts.prototype.scriptsCbor = function (hash) { return this.resolveScriptHash(hash); }; /** * @since 0.1.2 * @version 0.3.2 adds support for plutus v3 **/ BlockfrostPluts.prototype.resolveScriptHash = function (hash) { return __awaiter(this, void 0, void 0, function () { var res, script, cbor, cborBytes, scriptCbor, v1Hash, v2Hash; return __generator(this, function (_a) { switch (_a.label) { case 0: hash = hash.toString(); return [4 /*yield*/, this.get("".concat(this.url, "/scripts/").concat(hash, "/cbor"))]; case 1: res = _a.sent(); if (!res.cbor) { throw new Error("unresolved reference script with hash \"".concat(hash, "\"")); } cbor = cbor_1.Cbor.parse(res.cbor); cborBytes = (0, uint8array_utils_1.fromHex)(res.cbor); if (cbor instanceof cbor_1.CborBytes) { scriptCbor = cbor.bytes; v1Hash = (0, uint8array_utils_1.toHex)((0, crypto_1.blake2b_224)(new Uint8Array(__spreadArray([ 0x01 ], __read(cborBytes), false)))); if (v1Hash === hash) return [2 /*return*/, buildooor_1.Script.plutusV1(scriptCbor)]; v2Hash = (0, uint8array_utils_1.toHex)((0, crypto_1.blake2b_224)(new Uint8Array(__spreadArray([ 0x02 ], __read(cborBytes), false)))); if (v2Hash === hash) return [2 /*return*/, buildooor_1.Script.plutusV2(scriptCbor)]; return [2 /*return*/, buildooor_1.Script.plutusV3(scriptCbor)]; } else { script = new buildooor_1.Script("NativeScript", cborBytes); } return [2 /*return*/, script]; } }); }); }; return BlockfrostPluts; }()); exports.BlockfrostPluts = BlockfrostPluts;