UNPKG

@tezwell/michelson-sdk

Version:

Michelson SDK is a framework for generating Michelson values and types from Javascript.

289 lines (286 loc) 9.74 kB
var __defProp = Object.defineProperty; var __defProps = Object.defineProperties; var __getOwnPropDescs = Object.getOwnPropertyDescriptors; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); var __accessCheck = (obj, member, msg) => { if (!member.has(obj)) throw TypeError("Cannot " + msg); }; var __privateGet = (obj, member, getter) => { __accessCheck(obj, member, "read from private field"); return getter ? getter.call(obj) : member.get(obj); }; var __privateAdd = (obj, member, value) => { if (member.has(obj)) throw TypeError("Cannot add the same private member more than once"); member instanceof WeakSet ? member.add(obj) : member.set(obj, value); }; var __privateSet = (obj, member, value, setter) => { __accessCheck(obj, member, "write to private field"); setter ? setter.call(obj, value) : member.set(obj, value); return value; }; // src/misc/utils.ts var parenthesis = (str) => `(${str})`; var composeRightCombLayout = (fields) => { if (fields.length > 2) { return [fields[0], composeRightCombLayout(fields.slice(1))]; } return fields; }; // src/type.ts var _annotation; var Michelson_Type = class { constructor(type, ...innerTypes) { this.type = type; __privateAdd(this, _annotation, void 0); this.innerTypes = innerTypes; } setAnnotation(annotation) { __privateSet(this, _annotation, annotation); return this; } toMicheline() { const annot = __privateGet(this, _annotation) ? `%${__privateGet(this, _annotation)}` : ""; switch (this.type) { case "unit" /* unit */: case "int" /* int */: case "nat" /* nat */: case "mutez" /* mutez */: case "timestamp" /* timestamp */: case "string" /* string */: case "address" /* address */: case "chain_id" /* chain_id */: case "bool" /* bool */: case "bytes" /* bytes */: case "bls12_381_fr" /* bls12_381_fr */: case "bls12_381_g1" /* bls12_381_g1 */: case "bls12_381_g2" /* bls12_381_g2 */: case "key" /* key */: case "key_hash" /* key_hash */: case "signature" /* signature */: case "operation" /* operation */: case "never" /* never */: return annot ? parenthesis(`${this.type} ${annot}`) : this.type; case "list" /* list */: case "set" /* set */: case "pair" /* pair */: case "or" /* or */: case "option" /* option */: case "map" /* map */: case "big_map" /* big_map */: case "lambda" /* lambda */: case "ticket" /* ticket */: case "contract" /* contract */: case "sapling_state" /* sapling_state */: case "sapling_transaction" /* sapling_transaction */: return parenthesis([this.type, ...annot ? [annot] : [], ...this.innerTypes.map((t) => t.toMicheline())].join(" ")); } } toJSON() { const obj = __privateGet(this, _annotation) ? { annots: [`%${__privateGet(this, _annotation)}`] } : {}; switch (this.type) { case "unit" /* unit */: case "int" /* int */: case "nat" /* nat */: case "mutez" /* mutez */: case "timestamp" /* timestamp */: case "string" /* string */: case "address" /* address */: case "chain_id" /* chain_id */: case "bool" /* bool */: case "bytes" /* bytes */: case "bls12_381_fr" /* bls12_381_fr */: case "bls12_381_g1" /* bls12_381_g1 */: case "bls12_381_g2" /* bls12_381_g2 */: case "key" /* key */: case "key_hash" /* key_hash */: case "signature" /* signature */: case "operation" /* operation */: case "never" /* never */: return __spreadProps(__spreadValues({}, obj), { prim: this.type }); case "list" /* list */: case "set" /* set */: case "pair" /* pair */: case "or" /* or */: case "option" /* option */: case "map" /* map */: case "big_map" /* big_map */: case "lambda" /* lambda */: case "ticket" /* ticket */: case "contract" /* contract */: case "sapling_state" /* sapling_state */: case "sapling_transaction" /* sapling_transaction */: return __spreadProps(__spreadValues({}, obj), { prim: this.type, args: this.innerTypes.map((t) => t.toJSON()) }); } } [Symbol.toPrimitive]() { return this.toJSON(); } }; _annotation = new WeakMap(); var _annotation2, _params; var Michelson_Type_With_Param = class { constructor(type, ...params) { this.type = type; __privateAdd(this, _annotation2, void 0); __privateAdd(this, _params, void 0); __privateSet(this, _params, params); } setAnnotation(annotation) { __privateSet(this, _annotation2, annotation); return this; } toMicheline() { return `(${[this.type, ...__privateGet(this, _params).map(String)].join(" ")})`; } toJSON() { const obj = __privateGet(this, _annotation2) ? { annots: [`%${__privateGet(this, _annotation2)}`] } : {}; return __spreadProps(__spreadValues({}, obj), { prim: this.type, args: __privateGet(this, _params).map((p) => ({ int: String(p) })) }); } [Symbol.toPrimitive]() { return this.toJSON(); } }; _annotation2 = new WeakMap(); _params = new WeakMap(); var buildRecordVariantType = (fields, layout, container) => { const buildBranch = (branch) => { if (typeof branch === "string") { return fields[branch].setAnnotation(branch); } const [left, right] = branch; return container(buildBranch(left), buildBranch(right)); }; return Object.assign(buildBranch(layout), { fields, layout }); }; var TNat = () => new Michelson_Type("nat" /* nat */); var TInt = () => new Michelson_Type("int" /* int */); var TMutez = () => new Michelson_Type("mutez" /* mutez */); var TString = () => new Michelson_Type("string" /* string */); var TBool = () => new Michelson_Type("bool" /* bool */); var TAddress = () => new Michelson_Type("address" /* address */); var TTimestamp = () => new Michelson_Type("timestamp" /* timestamp */); var TChain_id = () => new Michelson_Type("chain_id" /* chain_id */); var TBytes = () => new Michelson_Type("bytes" /* bytes */); var TBls12_381_fr = () => new Michelson_Type("bls12_381_fr" /* bls12_381_fr */); var TBls12_381_g1 = () => new Michelson_Type("bls12_381_g1" /* bls12_381_g1 */); var TBls12_381_g2 = () => new Michelson_Type("bls12_381_g2" /* bls12_381_g2 */); var TKey = () => new Michelson_Type("key" /* key */); var TKey_hash = () => new Michelson_Type("key_hash" /* key_hash */); var TSignature = () => new Michelson_Type("signature" /* signature */); var TUnit = () => new Michelson_Type("unit" /* unit */); var TOperation = () => new Michelson_Type("operation" /* operation */); var TNever = () => new Michelson_Type("never" /* never */); var TList = (innerType) => new Michelson_Type("list" /* list */, innerType); var TSet = (innerType) => new Michelson_Type("set" /* set */, innerType); var TOption = (innerType) => new Michelson_Type("option" /* option */, innerType); var TPair = (leftType, rightType) => new Michelson_Type("pair" /* pair */, leftType, rightType); var TOr = (leftType, rightType) => new Michelson_Type("or" /* or */, leftType, rightType); var TMap = (keyType, valueType) => new Michelson_Type("map" /* map */, keyType, valueType); var TBig_map = (keyType, valueType) => new Michelson_Type("big_map" /* big_map */, keyType, valueType); var TLambda = (inType, outType) => new Michelson_Type("lambda" /* lambda */, inType, outType); var TTicket = (innerType) => new Michelson_Type("ticket" /* ticket */, innerType); var TContract = (innerType) => new Michelson_Type("contract" /* contract */, innerType); var TSapling_state = (memoSize) => new Michelson_Type_With_Param("sapling_state" /* sapling_state */, memoSize); var TSapling_transaction = (memoSize) => new Michelson_Type_With_Param("sapling_transaction" /* sapling_transaction */, memoSize); var TRecord = (fields, layout) => buildRecordVariantType(fields, layout || composeRightCombLayout(Object.keys(fields)), TPair); var TVariant = (fields, layout) => buildRecordVariantType(fields, layout || composeRightCombLayout(Object.keys(fields)), TOr); var Types = { TUnit, TNat, TInt, TMutez, TString, TBool, TBytes, TAddress, TTimestamp, TChain_id, TBls12_381_fr, TBls12_381_g1, TBls12_381_g2, TKey, TKey_hash, TSignature, TOperation, TNever, TList, TSet, TOption, TPair, TOr, TMap, TBig_map, TLambda, TTicket, TContract, TSapling_state, TSapling_transaction, TRecord, TVariant }; var type_default = Types; export { Michelson_Type, Michelson_Type_With_Param, TAddress, TBig_map, TBls12_381_fr, TBls12_381_g1, TBls12_381_g2, TBool, TBytes, TChain_id, TContract, TInt, TKey, TKey_hash, TLambda, TList, TMap, TMutez, TNat, TNever, TOperation, TOption, TOr, TPair, TRecord, TSapling_state, TSapling_transaction, TSet, TSignature, TString, TTicket, TTimestamp, TUnit, TVariant, buildRecordVariantType, type_default as default };