@taquito/taquito
Version:
High level functionality that builds upon the other packages in the Tezos Typescript Library Suite.
1,115 lines (1,092 loc) • 301 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@taquito/rpc'), require('@taquito/http-utils'), require('rxjs'), require('rxjs/operators'), require('@taquito/michelson-encoder'), require('@taquito/utils'), require('bignumber.js'), require('@taquito/core'), require('@taquito/michel-codec'), require('@taquito/local-forging')) :
typeof define === 'function' && define.amd ? define(['exports', '@taquito/rpc', '@taquito/http-utils', 'rxjs', 'rxjs/operators', '@taquito/michelson-encoder', '@taquito/utils', 'bignumber.js', '@taquito/core', '@taquito/michel-codec', '@taquito/local-forging'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.taquito = {}, global.rpc, global.httpUtils, global.rxjs, global.operators, global.michelsonEncoder, global.utils, global.BigNumber, global.core, global.michelCodec, global.localForging));
})(this, (function (exports, rpc, httpUtils, rxjs, operators, michelsonEncoder, utils, BigNumber, core, michelCodec, localForging) { 'use strict';
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var BigNumber__default = /*#__PURE__*/_interopDefaultLegacy(BigNumber);
/******************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
function __rest(s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
}
function __awaiter(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());
});
}
function __classPrivateFieldGet(receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
}
function __classPrivateFieldSet(receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
}
function formatErrorMessage(error, stringToReplace) {
const body = JSON.parse(error.body);
if (body[0] && body[0].kind && body[0].msg) {
const newBody = JSON.stringify({
kind: body[0].kind,
id: body[0].id,
msg: body[0].msg.replace(stringToReplace, ''),
});
return new httpUtils.HttpResponseError(`Http error response: (${error.status}) ${newBody}`, error.status, error.statusText, newBody, error.url);
}
else {
return error;
}
}
class RpcInjector {
constructor(context) {
this.context = context;
}
inject(signedOperationBytes) {
return __awaiter(this, void 0, void 0, function* () {
let hash;
try {
hash = yield this.context.rpc.injectOperation(signedOperationBytes);
}
catch (error) {
const stringToStrip = '. You may want to use --replace to provide adequate fee and replace it';
if (error instanceof httpUtils.HttpResponseError && error.message.includes(stringToStrip)) {
throw formatErrorMessage(error, stringToStrip);
}
else {
throw error;
}
}
return hash;
});
}
}
/**
* @category Error
* @description Error that indicates the signer has been unconfigured in the TezosToolkit instance
*/
class UnconfiguredSignerError extends Error {
constructor() {
super('No signer has been configured. Please configure one by calling setProvider({signer}) on your TezosToolkit instance.');
this.name = 'UnconfiguredSignerError';
}
}
/**
* @description Default signer implementation which does nothing and produce invalid signature
*/
class NoopSigner {
publicKey() {
return __awaiter(this, void 0, void 0, function* () {
throw new UnconfiguredSignerError();
});
}
publicKeyHash() {
return __awaiter(this, void 0, void 0, function* () {
throw new UnconfiguredSignerError();
});
}
secretKey() {
return __awaiter(this, void 0, void 0, function* () {
throw new UnconfiguredSignerError();
});
}
sign(_bytes, _watermark) {
return __awaiter(this, void 0, void 0, function* () {
throw new UnconfiguredSignerError();
});
}
}
function createObservableFromSubscription(sub) {
return new rxjs.Observable((subscriber) => {
sub.on('data', (data) => {
subscriber.next(data);
});
sub.on('error', (error) => {
subscriber.error(error);
});
sub.on('close', () => {
subscriber.complete();
});
return () => {
sub.close();
};
});
}
exports.DEFAULT_GAS_LIMIT = void 0;
(function (DEFAULT_GAS_LIMIT) {
DEFAULT_GAS_LIMIT[DEFAULT_GAS_LIMIT["DELEGATION"] = 10600] = "DELEGATION";
DEFAULT_GAS_LIMIT[DEFAULT_GAS_LIMIT["ORIGINATION"] = 10600] = "ORIGINATION";
DEFAULT_GAS_LIMIT[DEFAULT_GAS_LIMIT["TRANSFER"] = 10600] = "TRANSFER";
DEFAULT_GAS_LIMIT[DEFAULT_GAS_LIMIT["REVEAL"] = 1100] = "REVEAL";
})(exports.DEFAULT_GAS_LIMIT || (exports.DEFAULT_GAS_LIMIT = {}));
exports.DEFAULT_FEE = void 0;
(function (DEFAULT_FEE) {
DEFAULT_FEE[DEFAULT_FEE["DELEGATION"] = 1257] = "DELEGATION";
DEFAULT_FEE[DEFAULT_FEE["ORIGINATION"] = 10000] = "ORIGINATION";
DEFAULT_FEE[DEFAULT_FEE["TRANSFER"] = 10000] = "TRANSFER";
DEFAULT_FEE[DEFAULT_FEE["REVEAL"] = 374] = "REVEAL";
})(exports.DEFAULT_FEE || (exports.DEFAULT_FEE = {}));
exports.DEFAULT_STORAGE_LIMIT = void 0;
(function (DEFAULT_STORAGE_LIMIT) {
DEFAULT_STORAGE_LIMIT[DEFAULT_STORAGE_LIMIT["DELEGATION"] = 0] = "DELEGATION";
DEFAULT_STORAGE_LIMIT[DEFAULT_STORAGE_LIMIT["ORIGINATION"] = 257] = "ORIGINATION";
DEFAULT_STORAGE_LIMIT[DEFAULT_STORAGE_LIMIT["TRANSFER"] = 257] = "TRANSFER";
DEFAULT_STORAGE_LIMIT[DEFAULT_STORAGE_LIMIT["REVEAL"] = 0] = "REVEAL";
})(exports.DEFAULT_STORAGE_LIMIT || (exports.DEFAULT_STORAGE_LIMIT = {}));
const COST_PER_BYTE = 250;
exports.Protocols = void 0;
(function (Protocols) {
Protocols["Pt24m4xi"] = "Pt24m4xiPbLDhVgVfABUjirbmda3yohdN82Sp9FeuAXJ4eV9otd";
Protocols["PsBABY5H"] = "PsBABY5HQTSkA4297zNHfsZNKtxULfL18y95qb3m53QJiXGmrbU";
Protocols["PsBabyM1"] = "PsBabyM1eUXZseaJdmXFApDSBqj8YBfwELoxZHHW77EMcAbbwAS";
Protocols["PsCARTHA"] = "PsCARTHAGazKbHtnKfLzQg3kms52kSRpgnDY982a9oYsSXRLQEb";
Protocols["PsDELPH1"] = "PsDELPH1Kxsxt8f9eWbxQeRxkjfbxoqM52jvs5Y5fBxWWh4ifpo";
Protocols["PtEdo2Zk"] = "PtEdo2ZkT9oKpimTah6x2embF25oss54njMuPzkJTEi5RqfdZFA";
Protocols["PsFLorena"] = "PsFLorenaUUuikDWvMDr6fGBRG8kt3e3D3fHoXK1j1BFRxeSH4i";
Protocols["PtGRANADs"] = "PtGRANADsDU8R9daYKAgWnQYAJ64omN1o3KMGVCykShA97vQbvV";
Protocols["PtHangz2"] = "PtHangz2aRngywmSRGGvrcTyMbbdpWdpFKuS4uMWxg2RaH9i1qx";
Protocols["PsiThaCa"] = "PsiThaCaT47Zboaw71QWScM8sXeMM7bbQFncK9FLqYc6EKdpjVP";
Protocols["Psithaca2"] = "Psithaca2MLRFYargivpo7YvUr7wUDqyxrdhC5CQq78mRvimz6A";
Protocols["PtJakart2"] = "PtJakart2xVj7pYXJBXrqHgd82rdkLey5ZeeGwDgPp9rhQUbSqY";
Protocols["PtKathman"] = "PtKathmankSpLLDALzWw7CGD2j2MtyveTwboEYokqUCP4a1LxMg";
Protocols["PtLimaPtL"] = "PtLimaPtLMwfNinJi9rCfDPWea8dFgTZ1MeJ9f1m2SRic6ayiwW";
Protocols["PtMumbaii"] = "PtMumbaiiFFEGbew1rRjzSPyzRbA51Tm3RVZL5suHPxSZYDhCEc";
Protocols["PtMumbai2"] = "PtMumbai2TmsJHNGRkD8v8YDbtao7BLUC3wjASn1inAKLFCjaH1";
Protocols["ProtoALpha"] = "ProtoALphaALphaALphaALphaALphaALphaALphaALphaDdp3zK";
})(exports.Protocols || (exports.Protocols = {}));
const protocols = {
'004': [exports.Protocols.Pt24m4xi],
'005': [exports.Protocols.PsBABY5H, exports.Protocols.PsBabyM1],
'006': [exports.Protocols.PsCARTHA],
'007': [exports.Protocols.PsDELPH1],
'008': [exports.Protocols.PtEdo2Zk],
'009': [exports.Protocols.PsFLorena],
'010': [exports.Protocols.PtGRANADs],
'011': [exports.Protocols.PtHangz2],
'012': [exports.Protocols.PsiThaCa, exports.Protocols.Psithaca2],
'013': [exports.Protocols.PtJakart2],
'014': [exports.Protocols.PtKathman],
'015': [exports.Protocols.PtLimaPtL],
'016': [exports.Protocols.PtMumbai2],
'017': [exports.Protocols.ProtoALpha],
};
exports.ChainIds = void 0;
(function (ChainIds) {
ChainIds["MAINNET"] = "NetXdQprcVkpaWU";
ChainIds["CARTHAGENET"] = "NetXjD3HPJJjmcd";
ChainIds["DELPHINET"] = "NetXm8tYqnMWky1";
ChainIds["EDONET"] = "NetXSgo1ZT2DRUG";
ChainIds["FLORENCENET"] = "NetXxkAx4woPLyu";
ChainIds["GRANADANET"] = "NetXz969SFaFn8k";
ChainIds["HANGZHOUNET"] = "NetXZSsxBpMQeAT";
ChainIds["ITHACANET"] = "NetXbhmtAbMukLc";
ChainIds["ITHACANET2"] = "NetXnHfVqm9iesp";
ChainIds["JAKARTANET2"] = "NetXLH1uAxK7CCh";
ChainIds["KATHMANDUNET"] = "NetXazhm4yetmff";
ChainIds["LIMANET"] = "NetXizpkH94bocH";
ChainIds["MUMBAINET"] = "NetXQw6nWSnrJ5t";
ChainIds["MUMBAINET2"] = "NetXgbcrNtXD2yA";
})(exports.ChainIds || (exports.ChainIds = {}));
/**
* @category Error
* @description Error that indicates invalid smart contract parameters being passed or used
*/
class InvalidParameterError extends Error {
constructor(smartContractMethodName, sigs, args) {
super(`${smartContractMethodName} Received ${args.length} arguments while expecting one of the following signatures (${JSON.stringify(sigs)})`);
this.smartContractMethodName = smartContractMethodName;
this.sigs = sigs;
this.args = args;
this.name = 'Invalid parameters error';
}
}
/**
* @category Error
* @description Error that indicates an invalid delegation source contract address being passed or used
*/
class InvalidDelegationSource extends Error {
constructor(source) {
super(`Since Babylon delegation source can no longer be a contract address ${source}. Please use the smart contract abstraction to set your delegate.`);
this.source = source;
this.name = 'Invalid delegation source error';
}
}
/**
* @category Error
* @description Error that indicates an invalid smart contract code parameter being passed or used
*/
class InvalidCodeParameter extends Error {
constructor(message, data) {
super(message);
this.message = message;
this.data = data;
this.name = 'InvalidCodeParameter';
}
}
/**
* @category Error
* @description Error that indicates invalid smart contract init parameter being passed or used
*/
class InvalidInitParameter extends Error {
constructor(message, data) {
super(message);
this.message = message;
this.data = data;
this.name = 'InvalidInitParameter';
}
}
/**
* @category Error
* @description Error that indicates a failure when conducting a view simulation
*/
class ViewSimulationError extends Error {
constructor(message, viewName, failWith, originalError) {
super(message);
this.message = message;
this.viewName = viewName;
this.failWith = failWith;
this.originalError = originalError;
this.name = 'ViewSimulationError';
}
}
const validateAndExtractFailwith = (error) => {
if (isJsonString(error.body)) {
const parsedError = JSON.parse(error.body);
if (Array.isArray(parsedError) && 'with' in parsedError[parsedError.length - 1]) {
return parsedError[parsedError.length - 1].with;
}
}
};
const isJsonString = (str) => {
try {
JSON.parse(str);
}
catch (e) {
return false;
}
return true;
};
/**
* @category Error
* @description Error that indicates invalid or unconfigured context when executing a view
*/
class InvalidViewSimulationContext extends Error {
constructor(info) {
super(`${info} Please configure the context of the view execution in the executeView method.`);
this.info = info;
this.name = 'InvalidViewSimulationContext';
}
}
/**
* @category Error
* @description Error that indicates a mistake happening during the reveal operation
*/
class RevealOperationError extends Error {
constructor(message) {
super(message);
this.message = message;
this.name = 'RevealOperationError';
}
}
/**
* @category Error
* @description Error that indicates a mistake in the parameters in the preparation of an Origination operation
*/
class OriginationParameterError extends Error {
constructor(message) {
super(message);
this.message = message;
this.name = 'OriginationParameterError';
}
}
class IntegerError extends Error {
constructor(message) {
super(message);
this.message = message;
this.name = 'IntegerError';
}
}
const createActivationOperation = ({ pkh, secret }) => __awaiter(void 0, void 0, void 0, function* () {
return {
kind: rpc.OpKind.ACTIVATION,
pkh,
secret,
};
});
const createOriginationOperation = ({ code, init, balance = '0', delegate, storage, fee = exports.DEFAULT_FEE.ORIGINATION, gasLimit = exports.DEFAULT_GAS_LIMIT.ORIGINATION, storageLimit = exports.DEFAULT_STORAGE_LIMIT.ORIGINATION, mutez = false, }) => __awaiter(void 0, void 0, void 0, function* () {
if (storage !== undefined && init !== undefined) {
throw new OriginationParameterError('Storage and Init cannot be set a the same time. Please either use storage or init but not both.');
}
if (!Array.isArray(code)) {
throw new InvalidCodeParameter('Wrong code parameter type, expected an array', code);
}
let contractStorage;
if (storage !== undefined) {
const storageType = code.find((p) => 'prim' in p && p.prim === 'storage');
if ((storageType === null || storageType === void 0 ? void 0 : storageType.args) === undefined) {
throw new InvalidCodeParameter('The storage section is missing from the script', code);
}
const schema = new michelsonEncoder.Schema(storageType.args[0]); // TODO
contractStorage = schema.Encode(storage);
}
else if (init !== undefined && typeof init === 'object') {
contractStorage = init;
}
else {
throw new InvalidInitParameter('Wrong init parameter type, expected JSON Michelson', init);
}
const script = {
code,
storage: contractStorage,
};
if (isNaN(Number(balance))) {
throw new IntegerError(`Unexpected Invalid Integer ${balance}`);
}
const operation = {
kind: rpc.OpKind.ORIGINATION,
fee,
gas_limit: gasLimit,
storage_limit: storageLimit,
balance: mutez ? balance.toString() : utils.format('tz', 'mutez', balance).toString(),
script,
};
if (delegate) {
operation.delegate = delegate;
}
return operation;
});
const createTransferOperation = ({ to, amount, parameter, fee = exports.DEFAULT_FEE.TRANSFER, gasLimit = exports.DEFAULT_GAS_LIMIT.TRANSFER, storageLimit = exports.DEFAULT_STORAGE_LIMIT.TRANSFER, mutez = false, }) => __awaiter(void 0, void 0, void 0, function* () {
const operation = {
kind: rpc.OpKind.TRANSACTION,
fee,
gas_limit: gasLimit,
storage_limit: storageLimit,
amount: mutez ? amount.toString() : utils.format('tz', 'mutez', amount).toString(),
destination: to,
parameters: parameter,
};
return operation;
});
const createSetDelegateOperation = ({ delegate, source, fee = exports.DEFAULT_FEE.DELEGATION, gasLimit = exports.DEFAULT_GAS_LIMIT.DELEGATION, storageLimit = exports.DEFAULT_STORAGE_LIMIT.DELEGATION, }) => __awaiter(void 0, void 0, void 0, function* () {
const operation = {
kind: rpc.OpKind.DELEGATION,
source,
fee,
gas_limit: gasLimit,
storage_limit: storageLimit,
delegate,
};
return operation;
});
const createRegisterDelegateOperation = ({ fee = exports.DEFAULT_FEE.DELEGATION, gasLimit = exports.DEFAULT_GAS_LIMIT.DELEGATION, storageLimit = exports.DEFAULT_STORAGE_LIMIT.DELEGATION, }, source) => __awaiter(void 0, void 0, void 0, function* () {
return {
kind: rpc.OpKind.DELEGATION,
fee,
gas_limit: gasLimit,
storage_limit: storageLimit,
delegate: source,
};
});
const createRevealOperation = ({ fee = exports.DEFAULT_FEE.REVEAL, gasLimit = exports.DEFAULT_GAS_LIMIT.REVEAL, storageLimit = exports.DEFAULT_STORAGE_LIMIT.REVEAL, }, source, publicKey) => __awaiter(void 0, void 0, void 0, function* () {
return {
kind: rpc.OpKind.REVEAL,
fee,
public_key: publicKey,
source,
gas_limit: gasLimit,
storage_limit: storageLimit,
};
});
const createRegisterGlobalConstantOperation = ({ value, source, fee, gasLimit, storageLimit, }) => __awaiter(void 0, void 0, void 0, function* () {
return {
kind: rpc.OpKind.REGISTER_GLOBAL_CONSTANT,
value,
fee,
gas_limit: gasLimit,
storage_limit: storageLimit,
source,
};
});
const createTransferTicketOperation = ({ ticketContents, ticketTy, ticketTicketer, ticketAmount, destination, entrypoint, source, fee, gasLimit, storageLimit, }) => __awaiter(void 0, void 0, void 0, function* () {
return {
kind: rpc.OpKind.TRANSFER_TICKET,
fee,
gas_limit: gasLimit,
storage_limit: storageLimit,
source,
ticket_contents: ticketContents,
ticket_ty: ticketTy,
ticket_ticketer: ticketTicketer,
ticket_amount: ticketAmount,
destination,
entrypoint,
};
});
const createIncreasePaidStorageOperation = ({ source, fee, gasLimit, storageLimit, amount, destination, }) => __awaiter(void 0, void 0, void 0, function* () {
return {
kind: rpc.OpKind.INCREASE_PAID_STORAGE,
source,
fee,
gas_limit: gasLimit,
storage_limit: storageLimit,
amount,
destination,
};
});
const createDrainDelegateOperation = ({ consensus_key, delegate, destination, }) => __awaiter(void 0, void 0, void 0, function* () {
return {
kind: rpc.OpKind.DRAIN_DELEGATE,
consensus_key,
delegate,
destination,
};
});
const createBallotOperation = ({ source, proposal, ballot }) => __awaiter(void 0, void 0, void 0, function* () {
return {
kind: rpc.OpKind.BALLOT,
source,
proposal,
ballot,
};
});
const createProposalsOperation = ({ source, proposals }) => __awaiter(void 0, void 0, void 0, function* () {
return {
kind: rpc.OpKind.PROPOSALS,
source,
proposals,
};
});
const createUpdateConsensusKeyOperation = ({ source, fee, gasLimit, storageLimit, pk, }) => __awaiter(void 0, void 0, void 0, function* () {
return {
kind: rpc.OpKind.UPDATE_CONSENSUS_KEY,
source,
fee,
gas_limit: gasLimit,
storage_limit: storageLimit,
pk,
};
});
const createSmartRollupAddMessagesOperation = ({ source, fee, gasLimit, storageLimit, message, }) => __awaiter(void 0, void 0, void 0, function* () {
return {
kind: rpc.OpKind.SMART_ROLLUP_ADD_MESSAGES,
source,
fee,
gas_limit: gasLimit,
storage_limit: storageLimit,
message,
};
});
const createSmartRollupOriginateOperation = ({ source, fee, gasLimit, storageLimit, pvmKind, kernel, originationProof, parametersType, }) => __awaiter(void 0, void 0, void 0, function* () {
return {
kind: rpc.OpKind.SMART_ROLLUP_ORIGINATE,
source,
fee,
gas_limit: gasLimit,
storage_limit: storageLimit,
pvm_kind: pvmKind,
kernel,
origination_proof: originationProof,
parameters_ty: parametersType,
};
});
const attachKind = (op, kind) => {
return Object.assign(Object.assign({}, op), { kind });
};
const findWithKind = (arr, kind) => {
if (Array.isArray(arr)) {
const found = arr.find((op) => op.kind === kind);
if (found && isKind(found, kind)) {
return found;
}
}
};
const isKind = (op, kind) => {
return op.kind === kind;
};
const isOpWithFee = (op) => {
return ([
'transaction',
'delegation',
'origination',
'reveal',
'register_global_constant',
'increase_paid_storage',
'tx_rollup_origination',
'tx_rollup_submit_batch',
'transfer_ticket',
'update_consensus_key',
'smart_rollup_add_messages',
'smart_rollup_originate',
].indexOf(op.kind) !== -1);
};
const isOpRequireReveal = (op) => {
return ([
'transaction',
'delegation',
'origination',
'register_global_constant',
'increase_paid_storage',
'tx_rollup_origination',
'tx_rollup_submit_batch',
'transfer_ticket',
'update_consensus_key',
'smart_rollup_add_messages',
'smart_rollup_originate',
].indexOf(op.kind) !== -1);
};
const hasMetadata = (op) => {
return 'metadata' in op;
};
const hasMetadataWithResult = (op) => {
return hasMetadata(op) && 'operation_result' in op.metadata;
};
const hasMetadataWithInternalOperationResult = (op) => {
return hasMetadata(op) && 'internal_operation_results' in op.metadata;
};
const isErrorWithMessage = (error) => {
return 'with' in error;
};
/**
* @category Error
* @description Generic tezos error that will be thrown when a mistake occurs when doing an operation; more details here https://tezos.gitlab.io/api/errors.html
*/
class TezosOperationError extends Error {
constructor(errors, errorDetails) {
super();
this.errors = errors;
this.errorDetails = errorDetails;
this.name = 'TezosOperationError';
// Last error is 'often' the one with more detail
const lastError = errors[errors.length - 1];
this.id = lastError.id;
this.kind = lastError.kind;
this.message = `(${this.kind}) ${this.id}`;
if (isErrorWithMessage(lastError)) {
if (lastError.with.string) {
this.message = lastError.with.string;
}
else if (lastError.with.int) {
this.message = lastError.with.int;
}
else {
this.message = JSON.stringify(lastError.with);
}
}
}
}
/**
* @category Error
* @description Tezos error that will be thrown when a mistake happens during the preapply stage
*/
class TezosPreapplyFailureError extends Error {
constructor(result) {
super('Preapply returned an unexpected result');
this.result = result;
this.name = 'TezosPreapplyFailureError';
}
}
// Flatten all operation content results and internal operation results into a single array
// Some cases where we can have multiple operation results or internal operation results are:
// - When an operation includes a reveal operation
// - When an operation is made using the batch API
// - Smart contract call can contains internal operation results when they call other smart contract internally or originate contracts
const flattenOperationResult = (response) => {
const results = Array.isArray(response) ? response : [response];
const returnedResults = [];
for (let i = 0; i < results.length; i++) {
for (let j = 0; j < results[i].contents.length; j++) {
const content = results[i].contents[j];
if (hasMetadataWithResult(content)) {
returnedResults.push(Object.assign({ fee: content.fee }, content.metadata.operation_result));
if (Array.isArray(content.metadata.internal_operation_results)) {
content.metadata.internal_operation_results.forEach((x) => returnedResults.push(x.result));
}
}
}
}
return returnedResults;
};
/***
* @description Flatten all error from preapply response (including internal error)
*/
const flattenErrors = (response, status = 'failed') => {
const results = Array.isArray(response) ? response : [response];
let errors = [];
// Transaction that do not fail will be backtracked in case one failure occur
for (let i = 0; i < results.length; i++) {
for (let j = 0; j < results[i].contents.length; j++) {
const content = results[i].contents[j];
if (hasMetadata(content)) {
if (hasMetadataWithResult(content) && content.metadata.operation_result.status === status) {
errors = errors.concat(content.metadata.operation_result.errors || []);
}
if (hasMetadataWithInternalOperationResult(content) &&
Array.isArray(content.metadata.internal_operation_results)) {
for (const internalResult of content.metadata.internal_operation_results) {
if ('result' in internalResult && internalResult.result.status === status) {
errors = errors.concat(internalResult.result.errors || []);
}
}
}
}
}
}
return errors;
};
/**
* @category Error
* @description Error that indicates a general failure happening during an origination operation
*/
class OriginationOperationError extends Error {
constructor(message) {
super(message);
this.message = message;
this.name = 'OriginationOperationError';
}
}
/**
* @category Error
* @description Error that indicates an invalid estimate value being passed
*/
class InvalidEstimateValueError extends Error {
constructor(message) {
super(message);
this.message = message;
this.name = 'InvalidEstimateValueError';
}
}
/**
* @category Error
* @description Error that indicates invalid confirmation count has been passed or configured
*/
class InvalidConfirmationCountError extends Error {
constructor(message) {
super(message);
this.message = message;
this.name = 'InvalidConfirmationCountError';
}
}
/**
* @category Error
* @description Error that indicates undefined confirmation has not been specified or configured
*/
class ConfirmationUndefinedError extends Error {
constructor(message) {
super(message);
this.message = message;
this.name = 'ConfirmationUndefinedError';
}
}
/**
* @category Error
* @description Error that indicates an invalid filter expression being passed or used
*/
class InvalidFilterExpressionError extends Error {
constructor(message) {
super(message);
this.message = message;
this.name = 'InvalidFilterExpressionError';
}
}
/**
* @category Error
* @description Error that indicates an error being returned from the RPC response
*/
class RPCResponseError extends Error {
constructor(message) {
super(message);
this.message = message;
this.name = 'RPCResponseError';
}
}
/**
* @category Error
* @description Error that indicates invalid Preparation parameters being passed
*/
class PublicKeyNotFoundError extends Error {
constructor() {
super(`Unable to retrieve public key from signer. If you are using a wallet, make sure your account is revealed`);
this.name = 'PublicKeyNotFoundError';
}
}
/**
* @description Utility class to interact with Tezos operations
*/
class Operation {
/**
*
* @param hash Operation hash
* @param raw Raw operation that was injected
* @param context Taquito context allowing access to rpc and signer
* @throws {@link InvalidOperationHashError}
*/
constructor(hash, raw, results, context) {
this.hash = hash;
this.raw = raw;
this.results = results;
this.context = context;
this._pollingConfig$ = new rxjs.ReplaySubject(1);
this.currentHead$ = this._pollingConfig$.pipe(operators.switchMap((config) => {
return new rxjs.BehaviorSubject(config).pipe(operators.timeoutWith(config.timeout * 1000, rxjs.throwError(new Error('Confirmation polling timed out'))));
}), operators.switchMap(() => {
return rxjs.defer(() => createObservableFromSubscription(this.context.stream.subscribeBlock('head'))).pipe(operators.switchMap((newHead) => {
var _a, _b;
const prevHead = (_b = (_a = this.lastHead) === null || _a === void 0 ? void 0 : _a.header.level) !== null && _b !== void 0 ? _b : newHead.header.level - 1;
return rxjs.range(prevHead + 1, newHead.header.level - prevHead - 1).pipe(operators.concatMap((level) => this.context.readProvider.getBlock(level)), operators.endWith(newHead));
}), operators.tap((newHead) => (this.lastHead = newHead)));
}), operators.shareReplay({ refCount: true }));
// Observable that emit once operation is seen in a block
this.confirmed$ = this.currentHead$.pipe(operators.map((head) => {
for (let i = 3; i >= 0; i--) {
head.operations[i].forEach((op) => {
if (op.hash === this.hash) {
this._foundAt = head.header.level;
}
});
}
if (head.header.level - this._foundAt >= 0) {
return this._foundAt;
}
}), operators.filter((x) => x !== undefined), operators.first(), operators.shareReplay());
this._foundAt = Number.POSITIVE_INFINITY;
if (utils.validateOperation(this.hash) !== utils.ValidationResult.VALID) {
throw new core.InvalidOperationHashError(this.hash);
}
this.confirmed$
.pipe(operators.first(), operators.catchError(() => {
return rxjs.of(rxjs.EMPTY);
}))
.subscribe();
}
get includedInBlock() {
return this._foundAt;
}
get revealOperation() {
return (Array.isArray(this.results) &&
this.results.find((op) => op.kind === 'reveal'));
}
get revealStatus() {
if (this.revealOperation) {
return this.revealOperation.metadata.operation_result.status;
}
else {
return 'unknown';
}
}
get status() {
return (this.results.map((result) => {
if (hasMetadataWithResult(result)) {
return result.metadata.operation_result.status;
}
else {
return 'unknown';
}
})[0] || 'unknown');
}
/**
*
* @param confirmations [0] Number of confirmation to wait for
* @param timeout [180] Timeout
*/
confirmation(confirmations, timeout) {
return __awaiter(this, void 0, void 0, function* () {
if (typeof confirmations !== 'undefined' && confirmations < 1) {
throw new InvalidConfirmationCountError('Confirmation count must be at least 1');
}
const { defaultConfirmationCount, confirmationPollingTimeoutSecond } = this.context.config;
this._pollingConfig$.next({
timeout: timeout || confirmationPollingTimeoutSecond,
});
const conf = confirmations !== undefined ? confirmations : defaultConfirmationCount;
return new Promise((resolve, reject) => {
this.confirmed$
.pipe(operators.switchMap(() => this.currentHead$), operators.filter((head) => head.header.level - this._foundAt >= conf - 1), operators.first())
.subscribe((_) => {
resolve(this._foundAt + (conf - 1));
}, reject);
});
});
}
}
class BatchOperation extends Operation {
constructor(hash, params, source, raw, results, context) {
super(hash, raw, results, context);
this.params = params;
this.source = source;
}
sumProp(arr, prop) {
return arr.reduce((prev, current) => {
return prop in current ? Number(current[prop]) + prev : prev;
}, 0);
}
getOriginatedContractAddresses() {
const originationOpResults = this.results.filter((x) => x.kind === 'origination');
let addresses = [];
for (const res of originationOpResults) {
if (res.metadata.operation_result.originated_contracts) {
addresses = [...addresses, ...res.metadata.operation_result.originated_contracts];
}
}
return addresses;
}
get status() {
return (this.results
.filter((result) => BATCH_KINDS.indexOf(result.kind) !== -1)
.map((result) => {
if (hasMetadataWithResult(result)) {
return result.metadata.operation_result.status;
}
else {
return 'unknown';
}
})[0] || 'unknown');
}
get fee() {
return this.sumProp(this.params, 'fee');
}
get gasLimit() {
return this.sumProp(this.params, 'gas_limit');
}
get storageLimit() {
return this.sumProp(this.params, 'storage_limit');
}
get consumedGas() {
BigNumber__default["default"].config({ DECIMAL_PLACES: 0, ROUNDING_MODE: BigNumber__default["default"].ROUND_UP });
return new BigNumber__default["default"](this.consumedMilliGas).dividedBy(1000).toString();
}
get consumedMilliGas() {
return String(this.sumProp(flattenOperationResult({ contents: this.results }), 'consumed_milligas'));
}
get storageDiff() {
return String(this.sumProp(flattenOperationResult({ contents: this.results }), 'paid_storage_size_diff'));
}
get errors() {
return flattenErrors({ contents: this.results });
}
}
class Provider {
constructor(context) {
this.context = context;
}
get rpc() {
return this.context.rpc;
}
get signer() {
return this.context.signer;
}
forge({ opOb: { branch, contents, protocol }, counter }) {
return __awaiter(this, void 0, void 0, function* () {
const forgedBytes = yield this.context.forger.forge({ branch, contents });
return {
opbytes: forgedBytes,
opOb: {
branch,
contents,
protocol,
},
counter,
};
});
}
estimate(_a, estimator) {
var { fee, gasLimit, storageLimit } = _a, rest = __rest(_a, ["fee", "gasLimit", "storageLimit"]);
return __awaiter(this, void 0, void 0, function* () {
let calculatedFee = fee;
let calculatedGas = gasLimit;
let calculatedStorage = storageLimit;
if (calculatedFee && calculatedFee % 1 !== 0) {
throw new InvalidEstimateValueError(`Fee value must not be a decimal: ${calculatedFee}`);
}
if (calculatedGas && calculatedGas % 1 !== 0) {
throw new InvalidEstimateValueError(`Gas Limit value must not be a decimal: ${calculatedGas}`);
}
if (calculatedStorage && calculatedStorage % 1 !== 0) {
throw new InvalidEstimateValueError(`Storage Limit value must not be a decimal: ${calculatedStorage}`);
}
if (fee === undefined || gasLimit === undefined || storageLimit === undefined) {
const estimation = yield estimator(Object.assign({ fee, gasLimit, storageLimit }, rest));
calculatedFee !== null && calculatedFee !== void 0 ? calculatedFee : (calculatedFee = estimation.suggestedFeeMutez);
calculatedGas !== null && calculatedGas !== void 0 ? calculatedGas : (calculatedGas = estimation.gasLimit);
calculatedStorage !== null && calculatedStorage !== void 0 ? calculatedStorage : (calculatedStorage = estimation.storageLimit);
}
return {
fee: calculatedFee,
gasLimit: calculatedGas,
storageLimit: calculatedStorage,
};
});
}
getRPCOp(param) {
return __awaiter(this, void 0, void 0, function* () {
switch (param.kind) {
case rpc.OpKind.TRANSACTION:
return createTransferOperation(Object.assign({}, param));
case rpc.OpKind.ORIGINATION:
return createOriginationOperation(yield this.context.parser.prepareCodeOrigination(Object.assign({}, param)));
case rpc.OpKind.DELEGATION:
return createSetDelegateOperation(Object.assign({}, param));
case rpc.OpKind.REGISTER_GLOBAL_CONSTANT:
return createRegisterGlobalConstantOperation(Object.assign({}, param));
case rpc.OpKind.INCREASE_PAID_STORAGE:
return createIncreasePaidStorageOperation(Object.assign({}, param));
case rpc.OpKind.TRANSFER_TICKET:
return createTransferTicketOperation(Object.assign({}, param));
case rpc.OpKind.SMART_ROLLUP_ADD_MESSAGES:
return createSmartRollupAddMessagesOperation(Object.assign({}, param));
case rpc.OpKind.SMART_ROLLUP_ORIGINATE:
return createSmartRollupOriginateOperation(Object.assign({}, param));
default:
throw new utils.InvalidOperationKindError(param.kind);
}
});
}
simulate(op) {
return __awaiter(this, void 0, void 0, function* () {
return {
opResponse: yield this.rpc.runOperation(op),
op,
context: this.context.clone(),
};
});
}
isRevealOpNeeded(op, pkh) {
return __awaiter(this, void 0, void 0, function* () {
return !(yield this.isAccountRevealRequired(pkh)) || !this.isRevealRequiredForOpType(op)
? false
: true;
});
}
isAccountRevealRequired(publicKeyHash) {
return __awaiter(this, void 0, void 0, function* () {
return !(yield this.context.readProvider.isAccountRevealed(publicKeyHash, 'head'));
});
}
isRevealRequiredForOpType(op) {
let opRequireReveal = false;
for (const operation of op) {
if (isOpRequireReveal(operation)) {
opRequireReveal = true;
}
}
return opRequireReveal;
}
signAndInject(forgedBytes) {
return __awaiter(this, void 0, void 0, function* () {
const signed = yield this.signer.sign(forgedBytes.opbytes, new Uint8Array([3]));
forgedBytes.opbytes = signed.sbytes;
forgedBytes.opOb.signature = signed.prefixSig;
const opResponse = [];
const results = yield this.rpc.preapplyOperations([forgedBytes.opOb]);
if (!Array.isArray(results)) {
throw new TezosPreapplyFailureError(results);
}
for (let i = 0; i < results.length; i++) {
for (let j = 0; j < results[i].contents.length; j++) {
opResponse.push(results[i].contents[j]);
}
}
const errors = flattenErrors(results);
if (errors.length) {
throw new TezosOperationError(errors, 'Error occurred during validation simulation of operation');
}
return {
hash: yield this.context.injector.inject(forgedBytes.opbytes),
forgedBytes,
opResponse,
context: this.context.clone(),
};
});
}
}
class WalletOperationBatch {
constructor(walletProvider, context) {
this.walletProvider = walletProvider;
this.context = context;
this.operations = [];
}
/**
*
* @description Add a transaction operation to the batch
*
* @param params Transfer operation parameter
*/
withTransfer(params) {
const toValidation = utils.validateAddress(params.to);
if (toValidation !== utils.ValidationResult.VALID) {
throw new core.InvalidAddressError(params.to, utils.invalidErrorDetail(toValidation));
}
this.operations.push(Object.assign({ kind: rpc.OpKind.TRANSACTION }, params));
return this;
}
/**
*
* @description Add a contract call to the batch
*
* @param params Call a contract method
* @param options Generic operation parameters
*/
withContractCall(params, options = {}) {
return this.withTransfer(params.toTransferParams(options));
}
/**
*
* @description Add a delegation operation to the batch
*
* @param params Delegation operation parameter
*/
withDelegation(params) {
var _a;
const delegateValidation = utils.validateAddress((_a = params.delegate) !== null && _a !== void 0 ? _a : '');
if (params.delegate && delegateValidation !== utils.ValidationResult.VALID) {
throw new core.InvalidAddressError(params.delegate, utils.invalidErrorDetail(delegateValidation));
}
this.operations.push(Object.assign({ kind: rpc.OpKind.DELEGATION }, params));
return this;
}
/**
*
* @description Add an origination operation to the batch
*
* @param params Origination operation parameter
*/
withOrigination(params) {
this.operations.push(Object.assign({ kind: rpc.OpKind.ORIGINATION }, params));
return this;
}
/**
*
* @description Add an IncreasePaidStorage operation to the batch
*
* @param param IncreasePaidStorage operation parameter
*/
withIncreasePaidStorage(params) {
const destinationValidation = utils.validateAddress(params.destination);
if (destinationValidation !== utils.ValidationResult.VALID) {
throw new core.InvalidAddressError(params.destination, utils.invalidErrorDetail(destinationValidation));
}
this.operations.push(Object.assign({ kind: rpc.OpKind.INCREASE_PAID_STORAGE }, params));
return t