UNPKG

@taquito/michelson-encoder

Version:

converts michelson data and types into convenient JS/TS objects

96 lines (95 loc) 2.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SaplingStateToken = exports.SaplingStateValidationError = void 0; const token_1 = require("./token"); /** * @category Error * @description Error that indicates a failure happening when parsing encoding/executing a Sapling State */ class SaplingStateValidationError extends token_1.TokenValidationError { constructor(value, token, message) { super(value, token, message); this.value = value; this.token = token; this.name = 'SaplingStateValidationError'; } } exports.SaplingStateValidationError = SaplingStateValidationError; class SaplingStateToken extends token_1.Token { constructor(val, idx, fac) { super(val, idx, fac); this.val = val; this.idx = idx; this.fac = fac; } isValid(val) { return typeof val === 'object' && Object.keys(val).length === 0; } /** * @throws {@link SaplingStateValidationError} */ Execute(val, semantic) { if (semantic && semantic[SaplingStateToken.prim]) { return semantic[SaplingStateToken.prim](val, this.val); } if ('int' in val) { return val.int; } else { throw new SaplingStateValidationError(val, this, `Sapling state is expecting an object with an int property. Got ${JSON.stringify(val)}`); } } /** * @throws {@link SaplingStateValidationError} */ Encode(args) { const val = args.pop(); if (this.isValid(val)) { return []; } else { throw new SaplingStateValidationError(val, this, `Invalid sapling_state. Received: ${JSON.stringify(val)} while expecting: {}`); } } /** * @throws {@link SaplingStateValidationError} */ EncodeObject(val, semantic) { if (semantic && semantic[SaplingStateToken.prim]) { return semantic[SaplingStateToken.prim](val); } if (this.isValid(val)) { return []; } else { throw new SaplingStateValidationError(val, this, `Invalid sapling_state. Received: ${JSON.stringify(val)} while expecting: {}`); } } /** * @deprecated ExtractSchema has been deprecated in favor of generateSchema * */ ExtractSchema() { return { [SaplingStateToken.prim]: { 'memo-size': Number(this.val.args[0]['int']), }, }; } generateSchema() { return { __michelsonType: SaplingStateToken.prim, schema: { memoSize: this.val.args[0]['int'], }, }; } findAndReturnTokens(tokenToFind, tokens) { if (SaplingStateToken.prim === tokenToFind) { tokens.push(this); } return tokens; } } exports.SaplingStateToken = SaplingStateToken; SaplingStateToken.prim = 'sapling_state';