From ba19cff9756a74ce7d2dd5211dc45b8b6698fb3d Mon Sep 17 00:00:00 2001 From: Rodrigo Quelhas Date: Mon, 21 Mar 2022 10:08:40 +0000 Subject: [PATCH] feat: Improve typing on expressions --- package.json | 2 +- src/core/contract.ts | 4 +- src/core/enums/literal.ts | 6 -- src/core/enums/type.ts | 35 +++++++++- src/core/expression.ts | 26 +++---- src/core/type.ts | 2 +- src/expression/blockchain_properties.ts | 30 ++++---- src/expression/bls12_381.ts | 6 +- src/expression/comparison.ts | 9 +-- src/expression/contract.ts | 20 +++--- src/expression/crypto.ts | 30 ++++---- src/expression/equality.ts | 6 +- src/expression/integer.ts | 14 ++-- src/expression/lambda.ts | 4 +- src/expression/list.ts | 3 +- src/expression/literal.ts | 70 +++++++++++-------- src/expression/logic.ts | 10 +-- src/expression/map.ts | 8 +-- src/expression/math.ts | 25 ++++--- src/expression/misc.ts | 14 ++-- src/expression/operation.ts | 16 ++--- src/expression/sapling.ts | 8 +-- src/expression/serialization.ts | 6 +- src/expression/variables.ts | 5 +- src/expression/variant.ts | 10 +-- src/expression/view.ts | 6 +- src/misc/proxy.ts | 4 +- src/statement/control.ts | 4 +- src/type/index.ts | 2 +- src/typings/expression.ts | 4 +- src/typings/literal.ts | 4 +- src/typings/type.ts | 2 +- .../__snapshots__/while.test.ts.snap | 2 +- 33 files changed, 222 insertions(+), 175 deletions(-) diff --git a/package.json b/package.json index 54da98b..15ee4fb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@tezwell/smartts-sdk", - "version": "0.7.0", + "version": "0.7.1", "description": "SmartTS SDK is a metaprogramming framework for building Tezos smart contracts from Javascript.", "keywords": [ "Tezos", diff --git a/src/core/contract.ts b/src/core/contract.ts index 2fe5431..e355dd2 100644 --- a/src/core/contract.ts +++ b/src/core/contract.ts @@ -8,7 +8,7 @@ import { Expression } from './expression'; import { Proxied, proxy } from '../misc/proxy'; import { SetType } from '../statement'; import { TUnit, TUnknown } from '../type'; -import ValueAtom from './enums/literal'; +import { MichelsonType } from './enums/type'; abstract class View { public name: string; @@ -209,7 +209,7 @@ export class Contract { return this; } - public setConfig(options?: { initialBalance?: ILiteral; flags?: Flag[] }) { + public setConfig(options?: { initialBalance?: ILiteral; flags?: Flag[] }) { if (options?.flags) { this.#options.flags = options.flags; } diff --git a/src/core/enums/literal.ts b/src/core/enums/literal.ts index d23ae63..84b0f1a 100644 --- a/src/core/enums/literal.ts +++ b/src/core/enums/literal.ts @@ -29,12 +29,6 @@ export enum ValueAtom { ticket = 'ticket', sapling_state = 'sapling_empty_state', lambda = 'lambda', - // - operation = 'operation', - option = 'option', - contract = 'contract', - chain_id = 'chain_id', - sapling_transaction = 'sapling_transaction', } export default ValueAtom; diff --git a/src/core/enums/type.ts b/src/core/enums/type.ts index 277c0c7..7bd0df9 100644 --- a/src/core/enums/type.ts +++ b/src/core/enums/type.ts @@ -34,4 +34,37 @@ export enum TypeAtom { sapling_transaction = 'sapling_transaction', } -export default TypeAtom; +export enum MichelsonType { + unit = 'unit', + never = 'never', + nat = 'nat', + int = 'int', + mutez = 'mutez', + timestamp = 'timestamp', + string = 'string', + address = 'address', + key = 'key', + key_hash = 'key_hash', + signature = 'signature', + option = 'option', + bytes = 'bytes', + chain_id = 'chain_id', + bool = 'bool', + list = 'list', + pair = 'pair', + or = 'or', + set = 'set', + operation = 'operation', + contract = 'contract', + ticket = 'ticket', + lambda = 'lambda', + map = 'map', + big_map = 'big_map', + bls12_381_g1 = 'bls12_381_g1', + bls12_381_g2 = 'bls12_381_g2', + bls12_381_fr = 'bls12_381_fr', + sapling_transaction = 'sapling_transaction', + sapling_state = 'sapling_state', + chest = 'chest', + chest_key = 'chest_key', +} diff --git a/src/core/expression.ts b/src/core/expression.ts index 0443ec3..b1d5141 100644 --- a/src/core/expression.ts +++ b/src/core/expression.ts @@ -5,12 +5,12 @@ import type { IType } from '../typings/type'; import { GetProperty, LambdaArgument } from '../expression/variables'; import { capitalizeBoolean, LineInfo, parenthesis } from '../misc/utils'; import ValueAtom from './enums/literal'; -import TypeAtom from './enums/type'; +import { MichelsonType, TypeAtom } from './enums/type'; import { Proxied } from '../misc/proxy'; import { AsType } from '../expression/type'; import { TLambda, TUnknown } from '../type'; -export class Expression implements IExpression { +export class Expression implements IExpression { _isExpression = true as const; // Used for type checking _type = {} as T; @@ -38,12 +38,16 @@ export class Expression implements IExpression { } } -export class LiteralExpression implements ILiteral { +export class LiteralExpression implements ILiteral { _isExpression = true as const; // Used for type checking _type = {} as T; - constructor(private name: T, private values: (number | string | boolean | IExpression)[], private line: LineInfo) {} + constructor( + private name: ValueAtom, + private values: (number | string | boolean | IExpression)[], + private line: LineInfo, + ) {} toString() { switch (this.name) { @@ -80,10 +84,10 @@ export class LiteralExpression implements ILiteral { } } -export class RecordLiteral implements ILiteral { +export class RecordLiteral implements ILiteral { _isExpression = true as const; // Used for type checking - _type = {} as ValueAtom.record; + _type = MichelsonType.pair as const; constructor(private fields: Record, private line: LineInfo) {} @@ -96,13 +100,12 @@ export class RecordLiteral implements ILiteral { } } -export class MapLiteral implements ILiteral { +export class MapLiteral implements ILiteral { _isExpression = true as const; _type = {} as T; - type = {} as IType; constructor( - private prim: T, + private prim: ValueAtom.map | ValueAtom.big_map, private rows: IExpression[][], keyType: IType, valueType: IType, @@ -118,11 +121,10 @@ export class MapLiteral implements } } -export class LambdaLiteral implements ILiteral { +export class LambdaLiteral implements ILiteral { _isExpression = true as const; // Used for type checking - _type = ValueAtom.lambda as const; - type = {} as IType; + _type = MichelsonType.lambda as const; static idCounter = 0; private identifier: number; private withStorage?: 'read-write' | 'read-only'; diff --git a/src/core/type.ts b/src/core/type.ts index 4b2484b..924dd3d 100644 --- a/src/core/type.ts +++ b/src/core/type.ts @@ -2,7 +2,7 @@ import { composeRightCombLayout, parenthesis } from '../misc/utils'; import { ILayout } from '../typings/literal'; import { IType } from '../typings/type'; import { Layout } from './enums/layout'; -import TypeAtom from './enums/type'; +import { TypeAtom } from './enums/type'; export class SimpleType implements IType { // Used for type checking diff --git a/src/expression/blockchain_properties.ts b/src/expression/blockchain_properties.ts index 0f672b4..d7b5df2 100644 --- a/src/expression/blockchain_properties.ts +++ b/src/expression/blockchain_properties.ts @@ -2,8 +2,8 @@ import type { ILiteral } from '../typings/literal'; import ExpressionAtom from '../core/enums/expression'; import { Expression } from '../core/expression'; import { LineInfo } from '../misc/utils'; -import ValueAtom from '../core/enums/literal'; import { IExpression } from '../typings/expression'; +import { MichelsonType } from '../core/enums/type'; /** * Get the amount sent in the transaction. @@ -17,7 +17,7 @@ import { IExpression } from '../typings/expression'; * * @returns {IExpression} An expression */ -export const GetAmount = (): IExpression => new Expression(ExpressionAtom.amount); +export const GetAmount = () => new Expression(ExpressionAtom.amount); /** * Get the contract balance. @@ -31,7 +31,7 @@ export const GetAmount = (): IExpression => new Expression(Expr * * @returns {IExpression} An expression */ -export const GetBalance = (): IExpression => new Expression(ExpressionAtom.balance); +export const GetBalance = () => new Expression(ExpressionAtom.balance); /** * Get an entrypoint of the current contract. @@ -45,8 +45,8 @@ export const GetBalance = (): IExpression => new Expression(Exp * * @returns {IExpression} An expression */ -export const GetSelf = (entry_point: string, line = new LineInfo()): IExpression => - new Expression(ExpressionAtom.self, `"${entry_point}"`, line); +export const GetSelf = (entry_point: string, line = new LineInfo()) => + new Expression(ExpressionAtom.self, `"${entry_point}"`, line); /** * Get current contract address. @@ -60,7 +60,7 @@ export const GetSelf = (entry_point: string, line = new LineInfo()): IExpression * * @returns {IExpression} An expression */ -export const GetSelfAddress = (): IExpression => new Expression(ExpressionAtom.self_address); +export const GetSelfAddress = () => new Expression(ExpressionAtom.self_address); /** * Get transaction sender. @@ -74,7 +74,7 @@ export const GetSelfAddress = (): IExpression => new Expressi * * @returns {IExpression} An expression */ -export const GetSender = (): IExpression => new Expression(ExpressionAtom.sender); +export const GetSender = () => new Expression(ExpressionAtom.sender); /** * Get transaction source. @@ -88,7 +88,7 @@ export const GetSender = (): IExpression => new Expression(Ex * * @returns {IExpression} An expression */ -export const GetSource = (): IExpression => new Expression(ExpressionAtom.source); +export const GetSource = () => new Expression(ExpressionAtom.source); /** * Get the chain identifier. @@ -102,7 +102,7 @@ export const GetSource = (): IExpression => new Expression(Ex * * @returns {IExpression} An expression */ -export const GetChain_id = (): IExpression => new Expression(ExpressionAtom.chain_id); +export const GetChain_id = () => new Expression(ExpressionAtom.chain_id); /** * Get the head block level. @@ -116,7 +116,7 @@ export const GetChain_id = (): IExpression => new Expression * * @returns {IExpression} An expression */ -export const GetLevel = (): IExpression => new Expression(ExpressionAtom.level); +export const GetLevel = () => new Expression(ExpressionAtom.level); /** * Get the head block timestamp. @@ -130,7 +130,7 @@ export const GetLevel = (): IExpression => new Expression(Express * * @returns {IExpression} An expression */ -export const GetTimestamp = (): IExpression => new Expression(ExpressionAtom.now); +export const GetTimestamp = (): IExpression => new Expression(ExpressionAtom.now); /** * Get total voting power. @@ -144,7 +144,7 @@ export const GetTimestamp = (): IExpression => new Expressi * * @returns {IExpression} An expression */ -export const GetTotalVotingPower = (): IExpression => new Expression(ExpressionAtom.total_voting_power); +export const GetTotalVotingPower = () => new Expression(ExpressionAtom.total_voting_power); /** * Get the voting power of a given implicit account. @@ -161,7 +161,5 @@ export const GetTotalVotingPower = (): IExpression => new Express * * @returns {IExpression} An expression */ -export const GetVotingPower = ( - key_hash: ILiteral, - line = new LineInfo(), -): IExpression => new Expression(ExpressionAtom.voting_power, `${key_hash}`, line); +export const GetVotingPower = (key_hash: ILiteral, line = new LineInfo()) => + new Expression(ExpressionAtom.voting_power, `${key_hash}`, line); diff --git a/src/expression/bls12_381.ts b/src/expression/bls12_381.ts index a393e47..8e6d51b 100644 --- a/src/expression/bls12_381.ts +++ b/src/expression/bls12_381.ts @@ -2,7 +2,7 @@ import { LineInfo } from '../misc/utils'; import { IExpression } from '../typings/expression'; import { Expression } from '../core/expression'; import ExpressionAtom from '../core/enums/expression'; -import ValueAtom from '../core/enums/literal'; +import { MichelsonType } from '../core/enums/type'; /** * Check a BLS12-381 pairing. @@ -27,5 +27,5 @@ import ValueAtom from '../core/enums/literal'; * * @returns {IExpression} An expression of types `TBool()`. */ -export const PairingCheck = (pairs: IExpression, line = new LineInfo()) => - new Expression(ExpressionAtom.pairing_check, pairs, line); +export const PairingCheck = (pairs: IExpression, line = new LineInfo()) => + new Expression(ExpressionAtom.pairing_check, pairs, line); diff --git a/src/expression/comparison.ts b/src/expression/comparison.ts index fdc5090..347c69e 100644 --- a/src/expression/comparison.ts +++ b/src/expression/comparison.ts @@ -2,6 +2,7 @@ import { LineInfo } from '../misc/utils'; import { IExpression } from '../typings/expression'; import { Expression } from '../core/expression'; import ExpressionAtom from '../core/enums/expression'; +import { MichelsonType } from '../core/enums/type'; /** * Check if a value is less than another value. @@ -19,7 +20,7 @@ import ExpressionAtom from '../core/enums/expression'; * @returns {IExpression} An expression */ export const LessThan = (left: IExpression, right: IExpression, line = new LineInfo()) => - new Expression(ExpressionAtom.lt, left, right, line); + new Expression(ExpressionAtom.lt, left, right, line); /** * Check if a value is greater than another value. @@ -37,7 +38,7 @@ export const LessThan = (left: IExpression, right: IExpression, line = new LineI * @returns {IExpression} An expression */ export const GreaterThan = (left: IExpression, right: IExpression, line = new LineInfo()) => - new Expression(ExpressionAtom.gt, left, right, line); + new Expression(ExpressionAtom.gt, left, right, line); /** * Check if a value is less than or equal another value. @@ -55,7 +56,7 @@ export const GreaterThan = (left: IExpression, right: IExpression, line = new Li * @returns {IExpression} An expression */ export const LessThanOrEqual = (left: IExpression, right: IExpression, line = new LineInfo()) => - new Expression(ExpressionAtom.le, left, right, line); + new Expression(ExpressionAtom.le, left, right, line); /** * Check if a value is greater than or equal another value. @@ -73,7 +74,7 @@ export const LessThanOrEqual = (left: IExpression, right: IExpression, line = ne * @returns {IExpression} An expression */ export const GreaterThanOrEqual = (left: IExpression, right: IExpression, line = new LineInfo()) => - new Expression(ExpressionAtom.ge, left, right, line); + new Expression(ExpressionAtom.ge, left, right, line); export const Comparison = { LessThan, diff --git a/src/expression/contract.ts b/src/expression/contract.ts index 0943230..9ef3002 100644 --- a/src/expression/contract.ts +++ b/src/expression/contract.ts @@ -1,12 +1,12 @@ import type { IExpression } from '../typings/expression'; import type { IType } from '../typings/type'; import ExpressionAtom from '../core/enums/expression'; -import ValueAtom from '../core/enums/literal'; import { Expression } from '../core/expression'; import { LineInfo } from '../misc/utils'; import { TUnit } from '../type'; import { String } from './literal'; import { GetSome } from './variant'; +import { MichelsonType } from '../core/enums/type'; /** * Cast an address to a typed contract. @@ -26,12 +26,12 @@ import { GetSome } from './variant'; * @returns {IExpression} An expression of type `TContract()`. */ export const ToContract = ( - address: IExpression, + address: IExpression, entrypoint = 'default', argumentType: IType = TUnit(), errorMsg: IExpression = String('CONTRACT_NOT_FOUND'), line = new LineInfo(), -): IExpression => GetSome(GetContract(address, entrypoint, argumentType, line), errorMsg, line); +): IExpression => GetSome(GetContract(address, entrypoint, argumentType, line), errorMsg, line); /** * Cast an address to a typed contract. @@ -50,11 +50,12 @@ export const ToContract = ( * @returns {IExpression} An expression of type `TOption(TContract(@argumentType))`. */ export const GetContract = ( - address: IExpression, + address: IExpression, entrypoint = 'default', argumentType: IType = TUnit(), line = new LineInfo(), -): IExpression => new Expression(ExpressionAtom.contract, entrypoint, argumentType, address, line); +): IExpression => + new Expression(ExpressionAtom.contract, entrypoint, argumentType, address, line); /** * Get the address of a contract value. @@ -71,9 +72,10 @@ export const GetContract = ( * @returns {IExpression} An expression of type `TAddress()`. */ export const ToAddress = ( - contract: IExpression, + contract: IExpression, line = new LineInfo(), -): IExpression => new Expression(ExpressionAtom.to_address, contract, line); +): IExpression => + new Expression(ExpressionAtom.to_address, contract, line); /** * Create an implicit account. @@ -90,6 +92,6 @@ export const ToAddress = ( * @returns {IExpression} An expression of type `TContract(TUnit())`. */ export const ImplicitAccount = ( - key_hash: IExpression, + key_hash: IExpression, line = new LineInfo(), -): IExpression => new Expression(ExpressionAtom.implicit_account, key_hash, line); +): IExpression => new Expression(ExpressionAtom.implicit_account, key_hash, line); diff --git a/src/expression/crypto.ts b/src/expression/crypto.ts index aad3b7a..e58fa4e 100644 --- a/src/expression/crypto.ts +++ b/src/expression/crypto.ts @@ -2,7 +2,7 @@ import { LineInfo } from '../misc/utils'; import { IExpression } from '../typings/expression'; import { Expression } from '../core/expression'; import ExpressionAtom from '../core/enums/expression'; -import ValueAtom from '../core/enums/literal'; +import { MichelsonType } from '../core/enums/type'; /** * Compute a Blake2B cryptographic hash @@ -18,8 +18,8 @@ import ValueAtom from '../core/enums/literal'; * * @returns {IExpression} An expression */ -export const BLAKE2B = (bytes: IExpression, line = new LineInfo()) => - new Expression(ExpressionAtom.blake2b, bytes, line); +export const BLAKE2B = (bytes: IExpression, line = new LineInfo()) => + new Expression(ExpressionAtom.blake2b, bytes, line); /** * Compute a SHA-256 cryptographic hash @@ -35,8 +35,8 @@ export const BLAKE2B = (bytes: IExpression, line = new LineInfo * * @returns {IExpression} An expression */ -export const SHA256 = (bytes: IExpression, line = new LineInfo()) => - new Expression(ExpressionAtom.sha256, bytes, line); +export const SHA256 = (bytes: IExpression, line = new LineInfo()) => + new Expression(ExpressionAtom.sha256, bytes, line); /** * Compute a SHA-512 cryptographic hash @@ -52,8 +52,8 @@ export const SHA256 = (bytes: IExpression, line = new LineInfo( * * @returns {IExpression} An expression */ -export const SHA512 = (bytes: IExpression, line = new LineInfo()) => - new Expression(ExpressionAtom.sha512, bytes, line); +export const SHA512 = (bytes: IExpression, line = new LineInfo()) => + new Expression(ExpressionAtom.sha512, bytes, line); /** * Compute a SHA3-256 cryptographic hash @@ -69,8 +69,8 @@ export const SHA512 = (bytes: IExpression, line = new LineInfo( * * @returns {IExpression} An expression */ -export const SHA3 = (bytes: IExpression, line = new LineInfo()) => - new Expression(ExpressionAtom.sha3, bytes, line); +export const SHA3 = (bytes: IExpression, line = new LineInfo()) => + new Expression(ExpressionAtom.sha3, bytes, line); /** * Compute a Keccak-256 cryptographic hash @@ -86,8 +86,8 @@ export const SHA3 = (bytes: IExpression, line = new LineInfo()) * * @returns {IExpression} An expression */ -export const KECCAK = (bytes: IExpression, line = new LineInfo()) => - new Expression(ExpressionAtom.keccak, bytes, line); +export const KECCAK = (bytes: IExpression, line = new LineInfo()) => + new Expression(ExpressionAtom.keccak, bytes, line); /** * Verifies that a given sequence of bytes has been signed with a given key. @@ -106,11 +106,11 @@ export const KECCAK = (bytes: IExpression, line = new LineInfo( * @returns {IExpression} An expression of type `TBool()`. */ export const CheckSignature = ( - key: IExpression, - signature: IExpression, - bytes: IExpression, + key: IExpression, + signature: IExpression, + bytes: IExpression, line = new LineInfo(), -) => new Expression(ExpressionAtom.check_signature, key, signature, bytes, line); +) => new Expression(ExpressionAtom.check_signature, key, signature, bytes, line); export const Crypto = { BLAKE2B, diff --git a/src/expression/equality.ts b/src/expression/equality.ts index 8dc206e..6be8629 100644 --- a/src/expression/equality.ts +++ b/src/expression/equality.ts @@ -2,7 +2,7 @@ import { LineInfo } from '../misc/utils'; import { IExpression } from '../typings/expression'; import { Expression } from '../core/expression'; import ExpressionAtom from '../core/enums/expression'; -import ValueAtom from '../core/enums/literal'; +import { MichelsonType } from '../core/enums/type'; /** * Checks if two expressions resolve to equal values @@ -20,7 +20,7 @@ import ValueAtom from '../core/enums/literal'; * @returns {IExpression} An expression */ export const Equal = (left: IExpression, right: IExpression, line = new LineInfo()) => - new Expression(ExpressionAtom.eq, left, right, line); + new Expression(ExpressionAtom.eq, left, right, line); /** * Checks if two expressions resolve to a different values @@ -38,7 +38,7 @@ export const Equal = (left: IExpression, right: IExpression, line = new LineInfo * @returns {IExpression} An expression */ export const NotEqual = (left: IExpression, right: IExpression, line = new LineInfo()) => - new Expression(ExpressionAtom.neq, left, right, line); + new Expression(ExpressionAtom.neq, left, right, line); export const Equality = { Equal, diff --git a/src/expression/integer.ts b/src/expression/integer.ts index cdde6c0..6cce447 100644 --- a/src/expression/integer.ts +++ b/src/expression/integer.ts @@ -3,8 +3,8 @@ import ExpressionAtom from '../core/enums/expression'; import { Expression } from '../core/expression'; import { LineInfo } from '../misc/utils'; import { Unit } from './literal'; -import ValueAtom from '../core/enums/literal'; import { GetSome } from '.'; +import { MichelsonType } from '../core/enums/type'; /** * Convert a value of type `TInt()` to `TOption(TNat())`. @@ -21,7 +21,7 @@ import { GetSome } from '.'; * @returns {IExpression} An expression that evaluates to TOption(TNat()). */ export const IsNat = (expression: IExpression, line = new LineInfo()) => - new Expression(ExpressionAtom.isNat, expression, line); + new Expression(ExpressionAtom.isNat, expression, line); /** * Convert a value of type `TInt()` to `TNat()`. @@ -42,7 +42,7 @@ export const CastToNat = ( expression: IExpression, errorMsg: IExpression = Unit(), line = new LineInfo(), -): IExpression => GetSome(IsNat(expression, line), errorMsg, line); +): IExpression => GetSome(IsNat(expression, line), errorMsg, line); /** * Convert a value of type `TNat()` to `TInt()` @@ -58,8 +58,8 @@ export const CastToNat = ( * * @returns {IExpression} An expression that evaluates to `TNat()`. */ -export const CastToInt = (expression: IExpression, line = new LineInfo()) => - new Expression(ExpressionAtom.toInt, expression, line); +export const CastToInt = (expression: IExpression, line = new LineInfo()) => + new Expression(ExpressionAtom.toInt, expression, line); /** * Obtain the absolute value of an `TInt()` value. @@ -75,5 +75,5 @@ export const CastToInt = (expression: IExpression, line = new Lin * * @returns {IExpression} An expression that evaluates to `TNat()`. */ -export const ABS = (expression: IExpression, line = new LineInfo()) => - new Expression(ExpressionAtom.abs, expression, line); +export const ABS = (expression: IExpression, line = new LineInfo()) => + new Expression(ExpressionAtom.abs, expression, line); diff --git a/src/expression/lambda.ts b/src/expression/lambda.ts index d873474..10e603f 100644 --- a/src/expression/lambda.ts +++ b/src/expression/lambda.ts @@ -1,5 +1,5 @@ import ExpressionAtom from '../core/enums/expression'; -import ValueAtom from '../core/enums/literal'; +import { MichelsonType } from '../core/enums/type'; import { Expression } from '../core/expression'; import { proxy } from '../misc/proxy'; import { LineInfo } from '../misc/utils'; @@ -23,7 +23,7 @@ import { Unit } from './literal'; * @returns {IExpression} An expression. */ export const CallLambda = ( - expression: IExpression, + expression: IExpression, argument: IExpression = Unit(), line = new LineInfo(), ) => proxy(new Expression(ExpressionAtom.call_lambda, expression, argument, line), Expression.proxyHandler); diff --git a/src/expression/list.ts b/src/expression/list.ts index 5738ff5..8ab0b55 100644 --- a/src/expression/list.ts +++ b/src/expression/list.ts @@ -1,4 +1,5 @@ import ExpressionAtom from '../core/enums/expression'; +import { MichelsonType } from '../core/enums/type'; import { Expression } from '../core/expression'; import { LineInfo } from '../misc/utils'; import { IExpression } from '../typings/expression'; @@ -18,5 +19,5 @@ import { IExpression } from '../typings/expression'; * * @returns {IExpression} An expression */ -export const PrependToList = (list: IExpression, value: IExpression, line = new LineInfo()) => +export const PrependToList = (list: IExpression, value: IExpression, line = new LineInfo()) => new Expression(ExpressionAtom.cons, value, list, line); diff --git a/src/expression/literal.ts b/src/expression/literal.ts index a280052..86ed5de 100644 --- a/src/expression/literal.ts +++ b/src/expression/literal.ts @@ -7,6 +7,7 @@ import { TUnknown } from '../type'; import ValueAtom from '../core/enums/literal'; import { LambdaLiteral, LiteralExpression, MapLiteral, RecordLiteral } from '../core/expression'; import { ILiteral } from '../typings/literal'; +import { MichelsonType } from '../core/enums/type'; /** * Build a literal of type unit. @@ -22,7 +23,7 @@ import { ILiteral } from '../typings/literal'; * * @returns {IExpression} An expression */ -export const Unit = (line = new LineInfo()) => new LiteralExpression(ValueAtom.unit, [], line); +export const Unit = (line = new LineInfo()) => new LiteralExpression(ValueAtom.unit, [], line); /** * Build a literal of type nat. @@ -38,7 +39,8 @@ export const Unit = (line = new LineInfo()) => new LiteralExpression(ValueAtom.u * * @returns {IExpression} An expression */ -export const Nat = (value: number, line = new LineInfo()) => new LiteralExpression(ValueAtom.nat, [value], line); +export const Nat = (value: number, line = new LineInfo()) => + new LiteralExpression(ValueAtom.nat, [value], line); /** * Build a literal of type int. @@ -54,7 +56,8 @@ export const Nat = (value: number, line = new LineInfo()) => new LiteralExpressi * * @returns {IExpression} An expression */ -export const Int = (value: number, line = new LineInfo()) => new LiteralExpression(ValueAtom.int, [value], line); +export const Int = (value: number, line = new LineInfo()) => + new LiteralExpression(ValueAtom.int, [value], line); /** * Build a literal of type mutez. @@ -70,7 +73,8 @@ export const Int = (value: number, line = new LineInfo()) => new LiteralExpressi * * @returns {IExpression} An expression */ -export const Mutez = (value: number, line = new LineInfo()) => new LiteralExpression(ValueAtom.mutez, [value], line); +export const Mutez = (value: number, line = new LineInfo()) => + new LiteralExpression(ValueAtom.mutez, [value], line); /** * Build a literal of type string. @@ -86,7 +90,7 @@ export const Mutez = (value: number, line = new LineInfo()) => new LiteralExpres * @returns {IExpression} An expression */ export const String = (value: string, line = new LineInfo()) => - new LiteralExpression(ValueAtom.string, [quote(value)], line); + new LiteralExpression(ValueAtom.string, [quote(value)], line); /** * Build a literal of type bool. @@ -103,7 +107,7 @@ export const String = (value: string, line = new LineInfo()) => * @returns {IExpression} An expression */ export const Bool = (value: boolean, line = new LineInfo()) => - new LiteralExpression(ValueAtom.bool, [capitalizeBoolean(value)], line); + new LiteralExpression(ValueAtom.bool, [capitalizeBoolean(value)], line); /** * Build a literal of type address. @@ -120,7 +124,7 @@ export const Bool = (value: boolean, line = new LineInfo()) => * @returns {IExpression} An expression */ export const Address = (address: string, line = new LineInfo()) => - new LiteralExpression(ValueAtom.address, [address], line); + new LiteralExpression(ValueAtom.address, [address], line); /** * Build a literal of type timestamp. (The input is the number of seconds since Epoch) @@ -137,7 +141,7 @@ export const Address = (address: string, line = new LineInfo()) => * @returns {IExpression} An expression */ export const Timestamp = (timestamp: number, line = new LineInfo()) => - new LiteralExpression(ValueAtom.timestamp, [timestamp], line); + new LiteralExpression(ValueAtom.timestamp, [timestamp], line); /** * Build a literal of type chain_id. (Represents a chain identifier) @@ -154,7 +158,7 @@ export const Timestamp = (timestamp: number, line = new LineInfo()) => * @returns {IExpression} An expression */ export const Chain_id = (chainID: string, line = new LineInfo()) => - new LiteralExpression(ValueAtom.chain_id_cst, [chainID], line); + new LiteralExpression(ValueAtom.chain_id_cst, [chainID], line); /** * Build a literal of type bytes. @@ -170,7 +174,8 @@ export const Chain_id = (chainID: string, line = new LineInfo()) => * * @returns {IExpression} An expression */ -export const Bytes = (bytes: string, line = new LineInfo()) => new LiteralExpression(ValueAtom.bytes, [bytes], line); +export const Bytes = (bytes: string, line = new LineInfo()) => + new LiteralExpression(ValueAtom.bytes, [bytes], line); /** * Build a literal of type bls12_381_fr. @@ -187,7 +192,7 @@ export const Bytes = (bytes: string, line = new LineInfo()) => new LiteralExpres * @returns {IExpression} An expression */ export const Bls12_381_fr = (fr: string | number, line = new LineInfo()) => - new LiteralExpression(ValueAtom.bls12_381_fr, [fr], line); + new LiteralExpression(ValueAtom.bls12_381_fr, [fr], line); /** * Build a literal of type bls12_381_g1. @@ -204,7 +209,7 @@ export const Bls12_381_fr = (fr: string | number, line = new LineInfo()) => * @returns {IExpression} An expression */ export const Bls12_381_g1 = (bytes: string, line = new LineInfo()) => - new LiteralExpression(ValueAtom.bls12_381_g1, [bytes], line); + new LiteralExpression(ValueAtom.bls12_381_g1, [bytes], line); /** * Build a literal of type bls12_381_g2. @@ -221,7 +226,7 @@ export const Bls12_381_g1 = (bytes: string, line = new LineInfo()) => * @returns {IExpression} An expression */ export const Bls12_381_g2 = (bytes: string, line = new LineInfo()) => - new LiteralExpression(ValueAtom.bls12_381_g2, [bytes], line); + new LiteralExpression(ValueAtom.bls12_381_g2, [bytes], line); /** * Build a literal of type key. @@ -237,7 +242,8 @@ export const Bls12_381_g2 = (bytes: string, line = new LineInfo()) => * * @returns {IExpression} An expression */ -export const Key = (key: string, line = new LineInfo()) => new LiteralExpression(ValueAtom.key, [key], line); +export const Key = (key: string, line = new LineInfo()) => + new LiteralExpression(ValueAtom.key, [key], line); /** * Build a literal of type key_hash. @@ -254,7 +260,7 @@ export const Key = (key: string, line = new LineInfo()) => new LiteralExpression * @returns {IExpression} An expression */ export const Key_hash = (key_hash: string, line = new LineInfo()) => - new LiteralExpression(ValueAtom.key_hash, [key_hash], line); + new LiteralExpression(ValueAtom.key_hash, [key_hash], line); /** * Build a literal of type signature. @@ -271,7 +277,7 @@ export const Key_hash = (key_hash: string, line = new LineInfo()) => * @returns {IExpression} An expression */ export const Signature = (signature: string, line = new LineInfo()) => - new LiteralExpression(ValueAtom.signature, [signature], line); + new LiteralExpression(ValueAtom.signature, [signature], line); /** * Build a literal of type list. @@ -287,7 +293,8 @@ export const Signature = (signature: string, line = new LineInfo()) => * * @returns {IExpression} An expression */ -export const List = (items: IExpression[], line = new LineInfo()) => new LiteralExpression(ValueAtom.list, items, line); +export const List = (items: IExpression[], line = new LineInfo()) => + new LiteralExpression(ValueAtom.list, items, line); /** * Build a literal of type set. @@ -303,7 +310,8 @@ export const List = (items: IExpression[], line = new LineInfo()) => new Literal * * @returns {IExpression} An expression */ -export const Set = (items: IExpression[], line = new LineInfo()) => new LiteralExpression(ValueAtom.set, items, line); +export const Set = (items: IExpression[], line = new LineInfo()) => + new LiteralExpression(ValueAtom.set, items, line); /** * Build a literal of type option. (Wraps an existing optional value) @@ -319,8 +327,8 @@ export const Set = (items: IExpression[], line = new LineInfo()) => new LiteralE * * @returns {IExpression} An expression */ -export const Some = (value: IExpression, line = new LineInfo()): ILiteral => - new LiteralExpression(ValueAtom.Some, [value], line) as any; +export const Some = (value: IExpression, line = new LineInfo()): ILiteral => + new LiteralExpression(ValueAtom.Some, [value], line); /** * Build a literal of type option. (Used to represent an absent optional value) @@ -336,8 +344,8 @@ export const Some = (value: IExpression, line = new LineInfo()): ILiteral => - new LiteralExpression(ValueAtom.None, [], line) as any; +export const None = (line = new LineInfo()): ILiteral => + new LiteralExpression(ValueAtom.None, [], line); /** * Build a literal of type map. @@ -361,7 +369,7 @@ export const Map = ( keyType: IType = TUnknown(), valueType: IType = TUnknown(), line = new LineInfo(), -) => new MapLiteral(ValueAtom.map, rows, keyType, valueType, line); +) => new MapLiteral(ValueAtom.map, rows, keyType, valueType, line); /** * Build a literal of type big_map. @@ -385,7 +393,7 @@ export const Big_map = ( keyType: IType = TUnknown(), valueType: IType = TUnknown(), line = new LineInfo(), -) => new MapLiteral(ValueAtom.big_map, rows, keyType, valueType, line); +) => new MapLiteral(ValueAtom.big_map, rows, keyType, valueType, line); /** * Build a literal of type pair. (A binary tuple of values) @@ -402,7 +410,7 @@ export const Big_map = ( * @returns {IExpression} An expression */ export const Pair = (left: IExpression, right: IExpression, line = new LineInfo()) => - new LiteralExpression(ValueAtom.tuple, [left, right], line); + new LiteralExpression(ValueAtom.tuple, [left, right], line); /** * Build a literal of type lambda. @@ -441,8 +449,8 @@ export const Lambda = ( * * @returns {IExpression} An expression */ -export const Ticket = (content: IExpression, amount: LiteralExpression, line = new LineInfo()) => - new LiteralExpression(ValueAtom.ticket, [content, amount], line); +export const Ticket = (content: IExpression, amount: LiteralExpression, line = new LineInfo()) => + new LiteralExpression(ValueAtom.ticket, [content, amount], line); /** * Build a literal of type sapling_state. @@ -460,7 +468,7 @@ export const Ticket = (content: IExpression, amount: LiteralExpression - new LiteralExpression(ValueAtom.sapling_state, [memo], line); + new LiteralExpression(ValueAtom.sapling_state, [memo], line); /** * Build a literal of type or. (Wrap a value in a union. It represents the left branch.) @@ -477,7 +485,7 @@ export const Sapling_state = (memo: number, line = new LineInfo()) => * @returns {IExpression} An expression */ export const Left = (value: IExpression, line = new LineInfo()) => - new LiteralExpression(ValueAtom.variant, [ValueAtom.Left, value], line); + new LiteralExpression(ValueAtom.variant, [ValueAtom.Left, value], line); /** * Build a literal of type or. (Wrap a value in a union. It represents the right branch.) @@ -494,7 +502,7 @@ export const Left = (value: IExpression, line = new LineInfo()) => * @returns {IExpression} An expression */ export const Right = (value: IExpression, line = new LineInfo()) => - new LiteralExpression(ValueAtom.variant, [ValueAtom.Right, value], line); + new LiteralExpression(ValueAtom.variant, [ValueAtom.Right, value], line); /** * An artificial literal of type pair. (Uses nested annotated pair's to simulate an object value) @@ -531,7 +539,7 @@ export const Record = (fields: Record, line = new LineInfo( * @returns {IExpression} An expression */ export const Variant = (field: string, value: IExpression, line = new LineInfo()) => - new LiteralExpression(ValueAtom.variant, [field, value], line); + new LiteralExpression(ValueAtom.variant, [field, value], line); export const Literal = { // Singletons diff --git a/src/expression/logic.ts b/src/expression/logic.ts index 5c5007e..8ca068c 100644 --- a/src/expression/logic.ts +++ b/src/expression/logic.ts @@ -2,7 +2,7 @@ import { LineInfo } from '../misc/utils'; import { IExpression } from '../typings/expression'; import { Expression } from '../core/expression'; import ExpressionAtom from '../core/enums/expression'; -import ValueAtom from '../core/enums/literal'; +import { MichelsonType } from '../core/enums/type'; /** * Boolean OR. (The result is true if at least one of the expressions is true) @@ -19,7 +19,7 @@ import ValueAtom from '../core/enums/literal'; * * @returns {IExpression} An expression */ -export const Or = ( +export const Or = ( left: IExpression, right: IExpression, line = new LineInfo(), @@ -40,11 +40,11 @@ export const Or = ( * * @returns {IExpression} An expression */ -export const And = ( +export const And = ( left: IExpression, right: IExpression, line = new LineInfo(), -) => new Expression(ExpressionAtom.and, left, right, line); +) => new Expression(ExpressionAtom.and, left, right, line); /** * Boolean XOR. @@ -61,7 +61,7 @@ export const And = ( * * @returns {IExpression} An expression */ -export const Xor = ( +export const Xor = ( left: IExpression, right: IExpression, line = new LineInfo(), diff --git a/src/expression/map.ts b/src/expression/map.ts index 5c0fe7e..ff78fd5 100644 --- a/src/expression/map.ts +++ b/src/expression/map.ts @@ -3,7 +3,7 @@ import { LineInfo } from '../misc/utils'; import { IExpression } from '../typings/expression'; import ExpressionAtom from '../core/enums/expression'; import { Expression } from '../core/expression'; -import ValueAtom from '../core/enums/literal'; +import { MichelsonType } from '../core/enums/type'; /** * Get map entries. @@ -27,7 +27,7 @@ import ValueAtom from '../core/enums/literal'; * @returns {IExpression} An expression of type TList(TRecord({ key: ..., value: ... })). */ export const GetMapEntries = (source: IExpression, line = new LineInfo()) => - proxy(new Expression(ExpressionAtom.items, source, line), Expression.proxyHandler); + new Expression(ExpressionAtom.items, source, line); /** * Update an entry on map or big map. @@ -49,7 +49,7 @@ export const GetMapEntries = (source: IExpression, line = new LineInfo()) => * @returns {IExpression} An expression */ export const UpdateMap = (source: IExpression, key: IExpression, value: IExpression, line = new LineInfo()) => - proxy(new Expression(ExpressionAtom.update_map, source, key, value, line), Expression.proxyHandler); + new Expression(ExpressionAtom.update_map, source, key, value, line); /** * Accesss by key the value stored in a map or big map. @@ -107,7 +107,7 @@ export const AccessMapByKey = ( * @returns {IExpression} An expression that resolves to a boolean value. */ export const MapContainsKey = (expression: IExpression, key: IExpression, line = new LineInfo()) => { - return new Expression(ExpressionAtom.contains, expression, key, line); + return new Expression(ExpressionAtom.contains, expression, key, line); }; const MapExpressions = { diff --git a/src/expression/math.ts b/src/expression/math.ts index e553e1a..a02cbe2 100644 --- a/src/expression/math.ts +++ b/src/expression/math.ts @@ -2,7 +2,7 @@ import type { IExpression } from '../typings/expression'; import { LineInfo } from '../misc/utils'; import { Expression } from '../core/expression'; import ExpressionAtom from '../core/enums/expression'; -import ValueAtom from '../core/enums/literal'; +import { MichelsonType } from '../core/enums/type'; /** * Add two numerical values @@ -92,10 +92,10 @@ export const Divide = (left: IExpression, right: IExpression, line = new LineInf * @returns {IExpression} An expression of type `TOption(TPair(@quotient_type, @remainder_type))) */ export const EuclideanDivision = ( - left: IExpression, - right: IExpression, + left: IExpression, + right: IExpression, line = new LineInfo(), -): IExpression => new Expression(ExpressionAtom.ediv, left, right, line); +): IExpression => new Expression(ExpressionAtom.ediv, left, right, line); /** * Modulus @@ -113,8 +113,8 @@ export const EuclideanDivision = ( * @returns {IExpression} An expression. */ export const Mod = ( - left: IExpression, - right: IExpression, + left: IExpression, + right: IExpression, line = new LineInfo(), ) => new Expression(ExpressionAtom.mod, left, right, line); @@ -133,8 +133,11 @@ export const Mod = ( * * @returns {IExpression} An expression. */ -export const ShiftLeft = (left: IExpression, right: IExpression, line = new LineInfo()) => - new Expression(ExpressionAtom.lsl, left, right, line); +export const ShiftLeft = ( + left: IExpression, + right: IExpression, + line = new LineInfo(), +) => new Expression(ExpressionAtom.lsl, left, right, line); /** * Logical right shift @@ -152,10 +155,10 @@ export const ShiftLeft = (left: IExpression, right: IExpression, - right: IExpression, + left: IExpression, + right: IExpression, line = new LineInfo(), -) => new Expression(ExpressionAtom.lsr, left, right, line); +) => new Expression(ExpressionAtom.lsr, left, right, line); export const Math = { Add, diff --git a/src/expression/misc.ts b/src/expression/misc.ts index 5449d58..c31b85c 100644 --- a/src/expression/misc.ts +++ b/src/expression/misc.ts @@ -4,7 +4,7 @@ import { proxy } from '../misc/proxy'; import { LineInfo } from '../misc/utils'; import { IExpression } from '../typings/expression'; import { List } from './literal'; -import ValueAtom from '../core/enums/literal'; +import { MichelsonType } from '../core/enums/type'; /** * Concatenate a list with values of type `TString()` or `TBytes()`. @@ -20,8 +20,10 @@ import ValueAtom from '../core/enums/literal'; * * @returns {IExpression} An expression. */ -export const Concat = (values: IExpression[], line = new LineInfo()) => - proxy(new Expression(ExpressionAtom.concat, List(values), line), Expression.proxyHandler); +export const Concat = ( + values: IExpression[], + line = new LineInfo(), +) => proxy(new Expression(ExpressionAtom.concat, List(values), line), Expression.proxyHandler); /** * Obtain size of values with type `TString()`, `TBytes()`, `TList(...)`, `TSet(...)` and `TMap()`. @@ -38,6 +40,8 @@ export const Concat = (values: IEx * @returns {IExpression} An expression. */ export const SizeOf = ( - value: IExpression, + value: IExpression< + MichelsonType.bytes | MichelsonType.string | MichelsonType.list | MichelsonType.set | MichelsonType.map + >, line = new LineInfo(), -) => proxy(new Expression(ExpressionAtom.size, value, line), Expression.proxyHandler); +) => proxy(new Expression(ExpressionAtom.size, value, line), Expression.proxyHandler); diff --git a/src/expression/operation.ts b/src/expression/operation.ts index 528cd02..0950f54 100644 --- a/src/expression/operation.ts +++ b/src/expression/operation.ts @@ -7,16 +7,16 @@ import { PrependToList } from './list'; import { Mutez, None, Unit } from './literal'; import { Contract } from '../core'; import { GetOperations, GetProperty } from './variables'; -import ValueAtom from '../core/enums/literal'; +import { MichelsonType } from '../core/enums/type'; -class OperationExpression extends Expression { +class OperationExpression extends Expression { send(line = new LineInfo()) { const operations = GetOperations(); return SetValue(operations, PrependToList(operations, this, line), line); } } -class OriginationExpression extends Expression { +class OriginationExpression extends Expression { getAddress() { return GetProperty(this, 'address'); } @@ -52,8 +52,8 @@ class OriginationExpression extends Expression { * @returns {IExpression} An expression */ export const Transfer = ( - contract: IExpression, - amount: IExpression, + contract: IExpression, + amount: IExpression, argument: IExpression = Unit(), line = new LineInfo(), ) => new OperationExpression(ExpressionAtom.transfer, argument, amount, contract, line); @@ -76,7 +76,7 @@ export const Transfer = ( * * @returns {IExpression} An expression */ -export const SetDelegate = (keyHash: IExpression, line = new LineInfo()) => +export const SetDelegate = (keyHash: IExpression, line = new LineInfo()) => new OperationExpression(ExpressionAtom.set_delegate, keyHash, line); /** @@ -103,8 +103,8 @@ export const SetDelegate = (keyHash: IExpression, line = new L export const CreateContract = ( contract: Contract, storage: IExpression, - initial_balance: IExpression = Mutez(0), - delegate: IExpression = None(), + initial_balance: IExpression = Mutez(0), + delegate: IExpression = None(), line = new LineInfo(), ) => { const contract_param = new Expression(ExpressionAtom.contract, contract[Symbol.toPrimitive]()); diff --git a/src/expression/sapling.ts b/src/expression/sapling.ts index cff4161..0b7369c 100644 --- a/src/expression/sapling.ts +++ b/src/expression/sapling.ts @@ -1,5 +1,5 @@ import ExpressionAtom from '../core/enums/expression'; -import ValueAtom from '../core/enums/literal'; +import { MichelsonType } from '../core/enums/type'; import { Expression } from '../core/expression'; import { LineInfo } from '../misc/utils'; import { IExpression } from '../typings/expression'; @@ -37,7 +37,7 @@ export const EmptySaplingState = (memo: number, line = new LineInfo()) => * @returns {IExpression} An expression of type `TOption(TPair(TInt(), TSaplingState(@memo_size)))` */ export const ApplySaplingUpdate = ( - state: IExpression, - transition: IExpression, + state: IExpression, + transition: IExpression, line = new LineInfo(), -): IExpression => new Expression(ExpressionAtom.sapling_verify_update, state, transition, line); +): IExpression => new Expression(ExpressionAtom.sapling_verify_update, state, transition, line); diff --git a/src/expression/serialization.ts b/src/expression/serialization.ts index 598e773..b040faf 100644 --- a/src/expression/serialization.ts +++ b/src/expression/serialization.ts @@ -5,7 +5,7 @@ import { proxy } from '../misc/proxy'; import { LineInfo } from '../misc/utils'; import { IExpression } from '../typings/expression'; import { IType } from '../typings/type'; -import ValueAtom from '../core/enums/literal'; +import { MichelsonType } from '../core/enums/type'; /** * Serializes any value of packable type to its optimized binary representation, of type `TBytes()`. @@ -22,7 +22,7 @@ import ValueAtom from '../core/enums/literal'; * @returns {IExpression} An expression of type `TBytes()`. */ export const Pack = (expression: IExpression, line = new LineInfo()) => - proxy(new Expression(ExpressionAtom.pack, expression, line), Expression.proxyHandler); + proxy(new Expression(ExpressionAtom.pack, expression, line), Expression.proxyHandler); /** * Deserialize a value of type `TBytes()` into the corresponding Michelson value of type `TOption(...)`. @@ -39,7 +39,7 @@ export const Pack = (expression: IExpression, line = new LineInfo()) => * * @returns {IExpression} An expression of type `TOption()` */ -export const Unpack = (expression: IExpression, type: IType = TUnknown(), line = new LineInfo()) => +export const Unpack = (expression: IExpression, type: IType = TUnknown(), line = new LineInfo()) => proxy(new Expression(ExpressionAtom.unpack, expression, type, line), Expression.proxyHandler); export const Serialization = { diff --git a/src/expression/variables.ts b/src/expression/variables.ts index c7a76a5..a18dfb5 100644 --- a/src/expression/variables.ts +++ b/src/expression/variables.ts @@ -4,7 +4,7 @@ import { LineInfo } from '../misc/utils'; import { Expression, LambdaLiteral } from '../core/expression'; import { TUnknown } from '../type'; import { IExpression } from '../typings/expression'; -import ValueAtom from '../core/enums/literal'; +import { MichelsonType } from '../core/enums/type'; /** * Access a property of a record value. @@ -126,4 +126,5 @@ export const LambdaArgument = ( * * @returns {IExpression} An expression */ -export const GetOperations = (line = new LineInfo()) => new Expression(ExpressionAtom.operations, line); +export const GetOperations = (line = new LineInfo()) => + new Expression(ExpressionAtom.operations, line); diff --git a/src/expression/variant.ts b/src/expression/variant.ts index 6ff558f..b551abb 100644 --- a/src/expression/variant.ts +++ b/src/expression/variant.ts @@ -1,10 +1,10 @@ import type { IExpression } from '../typings/expression'; import ExpressionAtom from '../core/enums/expression'; -import ValueAtom from '../core/enums/literal'; import { Expression } from '../core/expression'; import { proxy } from '../misc/proxy'; import { LineInfo } from '../misc/utils'; import { Some } from './literal'; +import { MichelsonType } from '../core/enums/type'; /** * Open a variant @@ -21,7 +21,7 @@ import { Some } from './literal'; * @returns {IExpression} An expression */ export const OpenVariant = ( - variant: IExpression, + variant: IExpression, branch = 'Some', errorMsg?: IExpression, line = new LineInfo(), @@ -45,7 +45,7 @@ export const OpenVariant = ( * @returns {IExpression} An expression */ export const GetSome = ( - variant: IExpression, + variant: IExpression, errorMsg?: IExpression, line = new LineInfo(), ): IExpression => proxy(OpenVariant(variant, 'Some', errorMsg, line), Expression.proxyHandler); @@ -65,10 +65,10 @@ export const GetSome = ( * @returns {IExpression} An expression */ export const IsVariant = ( - variant: IExpression, + variant: IExpression, branch: string, line = new LineInfo(), -): IExpression => new Expression(ExpressionAtom.isVariant, variant, `"${branch}"`, line); +): IExpression => new Expression(ExpressionAtom.isVariant, variant, `"${branch}"`, line); const Variant = { OpenVariant, IsVariant, GetSome }; diff --git a/src/expression/view.ts b/src/expression/view.ts index ee839ce..96c1498 100644 --- a/src/expression/view.ts +++ b/src/expression/view.ts @@ -6,7 +6,7 @@ import { IExpression } from '../typings/expression'; import { IType } from '../typings/type'; import { TUnknown } from '../type'; import { Unit } from './literal'; -import ValueAtom from '../core/enums/literal'; +import { MichelsonType } from '../core/enums/type'; /** * Call a onchain view. @@ -31,8 +31,8 @@ export const CallView = ( argument: IExpression = Unit(), outputType: IType = TUnknown(), line = new LineInfo(), -): Proxied> => +): Proxied> => proxy( - new Expression(ExpressionAtom.view, `"${name}"`, address, argument, outputType, line), + new Expression(ExpressionAtom.view, `"${name}"`, address, argument, outputType, line), Expression.proxyHandler, ); diff --git a/src/misc/proxy.ts b/src/misc/proxy.ts index eda97c4..ad0121e 100644 --- a/src/misc/proxy.ts +++ b/src/misc/proxy.ts @@ -1,4 +1,4 @@ -import ValueAtom from '../core/enums/literal'; +import { MichelsonType } from '../core/enums/type'; import { IExpression } from '../typings/expression'; export type Proxied = T & { [prop: string]: any }; @@ -10,7 +10,7 @@ export type Proxied = T & { [prop: string]: any }; * * @returns A proxied object */ -export const proxy = >( +export const proxy = >( instance: T, handler: ProxyHandler, ): Proxied => new Proxy(instance, handler) as Proxied; diff --git a/src/statement/control.ts b/src/statement/control.ts index 8b73f21..6545701 100644 --- a/src/statement/control.ts +++ b/src/statement/control.ts @@ -12,7 +12,7 @@ import { } from '../core/statement'; import { LineInfo } from '../misc/utils'; import { Unit } from '../expression'; -import ValueAtom from '../core/enums/literal'; +import { MichelsonType } from '../core/enums/type'; /** * Test a condition and interrupt the smart-contract execution if the condition is false. (The whole operation is rollbacked) @@ -74,7 +74,7 @@ export const If = ( * @returns {IStatement} A statement */ export const MatchVariant = ( - variant: IExpression, + variant: IExpression, argumentName = `__MATCH_${VariantMatchStatement.nextID}__`, line = new LineInfo(), ) => new VariantMatchStatement(variant, argumentName, line); diff --git a/src/type/index.ts b/src/type/index.ts index a33b238..b55dd1c 100644 --- a/src/type/index.ts +++ b/src/type/index.ts @@ -2,7 +2,7 @@ import { ILayout } from '../typings/literal'; import { IType } from '../typings/type'; import { Layout } from '../core/enums/layout'; import { ContainerType, SimpleType, Type_VariantOrRecord } from '../core/type'; -import TypeAtom from '../core/enums/type'; +import { TypeAtom } from '../core/enums/type'; /** * An unknown type (It is used when leveraging type inference) diff --git a/src/typings/expression.ts b/src/typings/expression.ts index 377bdaa..c8fccdb 100644 --- a/src/typings/expression.ts +++ b/src/typings/expression.ts @@ -1,7 +1,7 @@ -import ValueAtom from '../core/enums/literal'; +import { MichelsonType } from '../core/enums/type'; import { IToString } from './shared'; -export interface IExpression extends IToString { +export interface IExpression extends IToString { _isExpression: true; _type: T; } diff --git a/src/typings/literal.ts b/src/typings/literal.ts index 45b58ef..892edc3 100644 --- a/src/typings/literal.ts +++ b/src/typings/literal.ts @@ -1,6 +1,6 @@ -import ValueAtom from '../core/enums/literal'; +import { MichelsonType } from '../core/enums/type'; import { IExpression } from './expression'; export type ILayout = (string | ILayout)[]; -export type ILiteral = IExpression; +export type ILiteral = IExpression; diff --git a/src/typings/type.ts b/src/typings/type.ts index e0a2028..45cc5e9 100644 --- a/src/typings/type.ts +++ b/src/typings/type.ts @@ -1,4 +1,4 @@ -import TypeAtom from '../core/enums/type'; +import { TypeAtom } from '../core/enums/type'; import { IToString } from './shared'; export interface IType extends IToString { diff --git a/tests/statements/__snapshots__/while.test.ts.snap b/tests/statements/__snapshots__/while.test.ts.snap index 93e408a..91fd581 100644 --- a/tests/statements/__snapshots__/while.test.ts.snap +++ b/tests/statements/__snapshots__/while.test.ts.snap @@ -6,7 +6,7 @@ exports[`Test (While) statement Loop while condition is true 1`] = ` template_id (static_id 0 (\\"while.test.ts\\" 9)) storage (literal (nat 1) (\\"while.test.ts\\" 10)) storage_type ((unknown 0)) - messages ((ep1 True False False True (\\"while.test.ts\\" 12) ((set_type (params (\\"while.test.ts\\" 12)) \\"nat\\" (\\"while.test.ts\\" 21)) (whileBlock (le (data) (params (\\"while.test.ts\\" 12)) (\\"comparison.ts\\" 57)) ((set (data) (add (data) (literal (nat 1) (\\"while.test.ts\\" 14)) (\\"math.ts\\" 22)) (\\"while.test.ts\\" 14))) (\\"while.test.ts\\" 14))))) + messages ((ep1 True False False True (\\"while.test.ts\\" 12) ((set_type (params (\\"while.test.ts\\" 12)) \\"nat\\" (\\"while.test.ts\\" 21)) (whileBlock (le (data) (params (\\"while.test.ts\\" 12)) (\\"comparison.ts\\" 58)) ((set (data) (add (data) (literal (nat 1) (\\"while.test.ts\\" 14)) (\\"math.ts\\" 22)) (\\"while.test.ts\\" 14))) (\\"while.test.ts\\" 14))))) flags () privates () views ()