UNPKG

airgap-coin-lib

Version:

The airgap-coin-lib is a protocol agnostic library to prepare, sign and broadcast cryptocurrency transactions.

333 lines 17.8 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 (_) 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 __spreadArrays = (this && this.__spreadArrays) || function () { for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; for (var r = Array(s), k = 0, i = 0; i < il; i++) for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) r[k] = a[j]; return r; }; Object.defineProperty(exports, "__esModule", { value: true }); var index_1 = require("../../../dependencies/src/axios-0.19.0/index"); var MichelsonOr_1 = require("../types/michelson/generics/MichelsonOr"); var MichelsonType_1 = require("../types/michelson/MichelsonType"); var MichelsonTypeMeta_1 = require("../types/michelson/MichelsonTypeMeta"); var utils_1 = require("../types/utils"); var TezosContractCall_1 = require("./TezosContractCall"); var TezosContractEntrypoint_1 = require("./TezosContractEntrypoint"); var TezosContract = /** @class */ (function () { function TezosContract(address, nodeRPCURL, conseilAPIURL, conseilNetwork, conseilAPIKey) { this.address = address; this.nodeRPCURL = nodeRPCURL; this.conseilAPIURL = conseilAPIURL; this.conseilNetwork = conseilNetwork; this.conseilAPIKey = conseilAPIKey; } TezosContract.prototype.bigMapValues = function (request) { if (request === void 0) { request = {}; } var _a, _b, _c, _d; return __awaiter(this, void 0, void 0, function () { var bigMapID, _e, predicates; return __generator(this, function (_f) { switch (_f.label) { case 0: _b = (_a = request) === null || _a === void 0 ? void 0 : _a.bigMapID; if (!(_b !== null && _b !== void 0)) return [3 /*break*/, 1]; _e = _b; return [3 /*break*/, 3]; case 1: return [4 /*yield*/, this.getBigMapID(request.bigMapFilter)]; case 2: _e = (_f.sent()); _f.label = 3; case 3: bigMapID = ((_e)); predicates = __spreadArrays([ { field: 'big_map_id', operation: 'eq', set: [bigMapID] } ], (_c = request.predicates, (_c !== null && _c !== void 0 ? _c : []))); return [2 /*return*/, this.apiRequest('big_map_contents', { fields: ['key', 'key_hash', 'value'], predicates: predicates, limit: (_d = request.limit, (_d !== null && _d !== void 0 ? _d : 100000)) })]; } }); }); }; TezosContract.prototype.createContractCall = function (entrypointName, value) { var _a; return __awaiter(this, void 0, void 0, function () { var entrypoint; return __generator(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, this.waitForEntrypoints()]; case 1: _b.sent(); entrypoint = (_a = this.entrypoints) === null || _a === void 0 ? void 0 : _a.get(entrypointName); if (!entrypoint) { return [2 /*return*/, this.createDefaultContractCall(value)]; } return [2 /*return*/, this.createEntrypointContractCall(entrypoint, value)]; } }); }); }; TezosContract.prototype.parseContractCall = function (json) { var _a; return __awaiter(this, void 0, void 0, function () { var entrypoint; return __generator(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, this.waitForEntrypoints()]; case 1: _b.sent(); entrypoint = (_a = this.entrypoints) === null || _a === void 0 ? void 0 : _a.get(json.entrypoint); if (!entrypoint) { return [2 /*return*/, Promise.reject("Couldn't parse the contract call, unknown entrypoint: " + json.entrypoint)]; } return [2 /*return*/, this.createEntrypointContractCall(entrypoint, json.value, { lazyEval: false, onNext: function (meta, _raw, value) { var argName = meta.getAnnotation(MichelsonTypeMeta_1.MichelsonAnnotationPrefix.FIELD, MichelsonTypeMeta_1.MichelsonAnnotationPrefix.TYPE); if (argName) { value.setName(argName); } } })]; } }); }); }; TezosContract.prototype.normalizeContractCallParameters = function (json, fallbackEntrypoint) { var _a; return __awaiter(this, void 0, void 0, function () { var entrypoint, value, defaultEntrypoint, normalizedEntrypoint; return __generator(this, function (_b) { switch (_b.label) { case 0: entrypoint = 'entrypoint' in json ? json.entrypoint : undefined; value = 'value' in json ? json.value : json; if (entrypoint !== undefined && entrypoint !== TezosContract.DEFAULT_ENTRYPOINT) { return [2 /*return*/, { entrypoint: entrypoint, value: value }]; } if (!MichelsonOr_1.MichelsonOr.isOr(value)) { return [2 /*return*/, { entrypoint: (fallbackEntrypoint !== null && fallbackEntrypoint !== void 0 ? fallbackEntrypoint : TezosContract.DEFAULT_ENTRYPOINT), value: value }]; } return [4 /*yield*/, this.waitForEntrypoints()]; case 1: _b.sent(); defaultEntrypoint = (_a = this.entrypoints) === null || _a === void 0 ? void 0 : _a.get(TezosContract.DEFAULT_ENTRYPOINT); if (!defaultEntrypoint) { throw new Error('Could not fetch default entrypoint.'); } defaultEntrypoint.type.createValue(value, { beforeNext: function (meta, raw) { var entrypointName = meta.getAnnotation(MichelsonTypeMeta_1.MichelsonAnnotationPrefix.FIELD); if (entrypointName && utils_1.isMichelineNode(raw)) { normalizedEntrypoint = [entrypointName, raw]; } }, onNext: function (_meta, _raw, michelsonValue) { if (michelsonValue instanceof MichelsonOr_1.MichelsonOr) { michelsonValue.eval(); } } }); return [2 /*return*/, { entrypoint: normalizedEntrypoint ? normalizedEntrypoint[0] : (fallbackEntrypoint !== null && fallbackEntrypoint !== void 0 ? fallbackEntrypoint : defaultEntrypoint.name), value: normalizedEntrypoint ? normalizedEntrypoint[1] : value }]; } }); }); }; TezosContract.prototype.createDefaultContractCall = function (value) { return new TezosContractCall_1.TezosContractCall(TezosContract.DEFAULT_ENTRYPOINT, value instanceof MichelsonType_1.MichelsonType ? value : undefined); }; TezosContract.prototype.createEntrypointContractCall = function (entrypoint, value, configuration) { if (configuration === void 0) { configuration = {}; } return new TezosContractCall_1.TezosContractCall(entrypoint.name, entrypoint.type.createValue(value, configuration)); }; TezosContract.prototype.getBigMapID = function (predicates) { var _a; return __awaiter(this, void 0, void 0, function () { var response; return __generator(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, this.waitForBigMapIDs()]; case 1: _b.sent(); if (((_a = this.bigMapIDs) === null || _a === void 0 ? void 0 : _a.length) === 1) { return [2 /*return*/, this.bigMapIDs[0]]; } if (!predicates) { throw new Error('Contract has more that one BigMap, provide ID or predicates to select one.'); } return [4 /*yield*/, this.apiRequest('big_maps', { fields: ['big_map_id'], predicates: __spreadArrays([ { field: 'big_map_id', operation: 'in', set: this.bigMapIDs } ], predicates) })]; case 2: response = _b.sent(); if (response.length === 0) { throw new Error('BigMap ID not found'); } if (response.length > 1) { throw new Error('More than one BigMap ID has been found for the predicates.'); } return [2 /*return*/, response[0].big_map_id]; } }); }); }; TezosContract.prototype.waitForBigMapIDs = function () { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { if (this.bigMapIDs !== undefined) { return [2 /*return*/]; } if (this.bigMapIDsPromise === undefined) { this.bigMapIDsPromise = this.apiRequest('originated_account_maps', { fields: ['big_map_id'], predicates: [ { field: 'account_id', operation: 'eq', set: [this.address] } ] }) .then(function (response) { if (response.length === 0) { throw new Error('BigMap IDs not found'); } _this.bigMapIDs = response.map(function (entry) { return entry.big_map_id; }); }) .finally(function () { _this.bigMapIDsPromise = undefined; }); } return [2 /*return*/, this.bigMapIDsPromise]; }); }); }; TezosContract.prototype.waitForEntrypoints = function () { return __awaiter(this, void 0, void 0, function () { var codePromise, entrypointsPromise; var _this = this; return __generator(this, function (_a) { if (this.entrypoints !== undefined) { return [2 /*return*/]; } if (this.entrypointsPromise === undefined) { codePromise = this.nodeRequest('script'); entrypointsPromise = this.nodeRequest('entrypoints'); this.entrypointsPromise = Promise.all([codePromise, entrypointsPromise]) .then(function (_a) { var codeResponse = _a[0], entrypointsResponse = _a[1]; if (entrypointsResponse.entrypoints[TezosContract.DEFAULT_ENTRYPOINT] === undefined) { var parameter = codeResponse.code.find(function (primitiveApplication) { return primitiveApplication.prim === 'parameter'; }); if (parameter) { entrypointsResponse.entrypoints[TezosContract.DEFAULT_ENTRYPOINT] = parameter.args ? parameter.args[0] : []; } } _this.entrypoints = new Map(TezosContractEntrypoint_1.TezosContractEntrypoint.fromJSON(entrypointsResponse.entrypoints).map(function (entrypoint) { return [ entrypoint.name, entrypoint ]; })); }) .finally(function () { _this.entrypointsPromise = undefined; }); } return [2 /*return*/, this.entrypointsPromise]; }); }); }; TezosContract.prototype.nodeRequest = function (endpoint) { return __awaiter(this, void 0, void 0, function () { var response; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, index_1.default.get(this.nodeRPCURL + "/chains/main/blocks/head/context/contracts/" + this.address + "/" + endpoint)]; case 1: response = _a.sent(); return [2 /*return*/, response.data]; } }); }); }; TezosContract.prototype.apiRequest = function (endpoint, body) { return __awaiter(this, void 0, void 0, function () { var response; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, index_1.default.post(this.conseilAPIURL + "/v2/data/tezos/" + this.conseilNetwork + "/" + endpoint, body, { headers: { 'Content-Type': 'application/json', apiKey: this.conseilAPIKey } })]; case 1: response = _a.sent(); return [2 /*return*/, response.data]; } }); }); }; TezosContract.DEFAULT_ENTRYPOINT = 'default'; return TezosContract; }()); exports.TezosContract = TezosContract; //# sourceMappingURL=TezosContract.js.map