UNPKG

@harmoniclabs/blockfrost-pluts

Version:

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

513 lines (512 loc) 25.4 kB
"use strict"; 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 = void 0; var cardano_ledger_ts_1 = require("@harmoniclabs/cardano-ledger-ts"); 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, cardano_ledger_ts_1.isIUTxO)(canResolve)) canResolve = canResolve.utxoRef; if (canResolve instanceof cardano_ledger_ts_1.TxOutRef) return canResolve.toString(); if ((0, cardano_ledger_ts_1.isITxOutRef)(canResolve)) return "".concat(canResolve.id.toString(), "#").concat(canResolve.index); console.error(canResolve); throw new Error('"forceTxOutRefStr" expects a "CanResolveToUTxO"'); } 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; var BlockfrostPluts = /** @class */ (function () { function BlockfrostPluts(_a) { var projectId = _a.projectId, customBackend = _a.customBackend; if (typeof projectId !== "string") throw new Error("blockfrost projectId not a string"); var 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" : network === "sanchonet" ? "https://cardano-sanchonet.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, 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" ? cardano_ledger_ts_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().toBuffer().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" ? cardano_ledger_ts_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 cardano_ledger_ts_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 cardano_ledger_ts_1.UTxO({ utxoRef: ref, resolved: { address: cardano_ledger_ts_1.Address.fromString(resolved.address), value: cardano_ledger_ts_1.Value.fromUnits(resolved.amount), datum: resolved.inline_datum ? (0, plutus_data_1.dataFromCbor)(resolved.inline_datum) : resolved.data_hash ? new cardano_ledger_ts_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 = cardano_ledger_ts_1.Address.fromString(response.address); result.totAmount = cardano_ledger_ts_1.Value.fromUnits(response.amount); result.stakeAddress = response.stake_address ? cardano_ledger_ts_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 cardano_ledger_ts_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.1.0 */ BlockfrostPluts.prototype.addressUtxos = function (address, pagination) { return __awaiter(this, void 0, void 0, function () { var url, _utxos; return __generator(this, function (_a) { switch (_a.label) { case 0: address = address.toString(); return [4 /*yield*/, this.get("".concat(this.url, "/addresses/").concat(address, "/utxos").concat(paginationOptsToStr(pagination !== null && pagination !== void 0 ? pagination : {})))]; case 1: _utxos = _a.sent(); return [2 /*return*/, _utxos.map(function (_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; var datum = inline_datum ? (0, plutus_data_1.dataFromCbor)(inline_datum) : data_hash ? new cardano_ledger_ts_1.Hash32(data_hash) : undefined; var utxo = new cardano_ledger_ts_1.UTxO({ utxoRef: { id: tx_hash, index: output_index }, resolved: { address: cardano_ledger_ts_1.Address.fromString(address), value: cardano_ledger_ts_1.Value.fromUnits(amount), datum: datum, refScript: undefined } }); if (reference_script_hash) { Object.defineProperty(utxo, "refScriptHash", { value: new cardano_ledger_ts_1.Hash28(reference_script_hash), writable: false, enumerable: true, configurable: false, }); } ; return utxo; })]; } }); }); }; ; /** @since 0.1.0 */ BlockfrostPluts.prototype.scriptsCbor = function (hash) { return this.resolveScriptHash(hash); }; /** @since 0.1.2 */ BlockfrostPluts.prototype.resolveScriptHash = function (hash) { return __awaiter(this, void 0, void 0, function () { var res, script, cbor, cborBytes, scriptCbor, v1Hash; 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.buffer; v1Hash = (0, uint8array_utils_1.toHex)((0, crypto_1.blake2b_224)(new Uint8Array(__spreadArray([ 0x01 ], __read(cborBytes), false)))); if (v1Hash === hash) { script = new cardano_ledger_ts_1.Script("PlutusScriptV1", scriptCbor); } else { script = new cardano_ledger_ts_1.Script("PlutusScriptV2", scriptCbor); } } else { script = new cardano_ledger_ts_1.Script("NativeScript", cborBytes); } return [2 /*return*/, script]; } }); }); }; return BlockfrostPluts; }()); exports.BlockfrostPluts = BlockfrostPluts;