UNPKG

@taquito/michelson-encoder

Version:

converts michelson data and types into convenient JS/TS objects

95 lines (94 loc) 2.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MutezToken = exports.MutezValidationError = void 0; const token_1 = require("../token"); const bignumber_js_1 = require("bignumber.js"); /** * @category Error * @description Error that indicates a failure happening when parsing encoding/executing Mutez */ class MutezValidationError extends token_1.TokenValidationError { constructor(value, token, message) { super(value, token, message); this.value = value; this.token = token; this.name = 'MutezValidationError'; } } exports.MutezValidationError = MutezValidationError; class MutezToken extends token_1.ComparableToken { constructor(val, idx, fac) { super(val, idx, fac); this.val = val; this.idx = idx; this.fac = fac; } Execute(val) { return new bignumber_js_1.default(val[Object.keys(val)[0]]); } /** * @deprecated ExtractSchema has been deprecated in favor of generateSchema * */ ExtractSchema() { return MutezToken.prim; } generateSchema() { return { __michelsonType: MutezToken.prim, schema: MutezToken.prim, }; } /** * @throws {@link MutezValidationError} */ validate(val) { const bigNumber = new bignumber_js_1.default(val); if (bigNumber.isNaN()) { throw new MutezValidationError(val, this, `Value is not a number: ${val}`); } } /** * @throws {@link MutezValidationError} */ Encode(args) { const val = args.pop(); this.validate(val); return { int: String(val).toString() }; } /** * @throws {@link MutezValidationError} */ EncodeObject(val, semantic) { this.validate(val); if (semantic && semantic[MutezToken.prim]) { return semantic[MutezToken.prim](val); } return { int: String(val).toString() }; } ToBigMapKey(val) { return { key: { int: String(val) }, type: { prim: MutezToken.prim }, }; } ToKey({ int }) { return int; } compare(mutez1, mutez2) { const o1 = Number(mutez1); const o2 = Number(mutez2); if (o1 === o2) { return 0; } return o1 < o2 ? -1 : 1; } findAndReturnTokens(tokenToFind, tokens) { if (MutezToken.prim === tokenToFind) { tokens.push(this); } return tokens; } } exports.MutezToken = MutezToken; MutezToken.prim = 'mutez';