Skip to content

Commit

Permalink
feat: Improve typing on expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
RomarQ committed Mar 21, 2022
1 parent 4d68eb0 commit ba19cff
Show file tree
Hide file tree
Showing 33 changed files with 222 additions and 175 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 2 additions & 2 deletions src/core/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -209,7 +209,7 @@ export class Contract {
return this;
}

public setConfig(options?: { initialBalance?: ILiteral<ValueAtom.mutez>; flags?: Flag[] }) {
public setConfig(options?: { initialBalance?: ILiteral<MichelsonType.mutez>; flags?: Flag[] }) {
if (options?.flags) {
this.#options.flags = options.flags;
}
Expand Down
6 changes: 0 additions & 6 deletions src/core/enums/literal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
35 changes: 34 additions & 1 deletion src/core/enums/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}
26 changes: 14 additions & 12 deletions src/core/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends ValueAtom> implements IExpression<T> {
export class Expression<T extends MichelsonType> implements IExpression<T> {
_isExpression = true as const;
// Used for type checking
_type = {} as T;
Expand Down Expand Up @@ -38,12 +38,16 @@ export class Expression<T extends ValueAtom> implements IExpression<T> {
}
}

export class LiteralExpression<T extends ValueAtom> implements ILiteral<T> {
export class LiteralExpression<T extends MichelsonType> implements ILiteral<T> {
_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) {
Expand Down Expand Up @@ -80,10 +84,10 @@ export class LiteralExpression<T extends ValueAtom> implements ILiteral<T> {
}
}

export class RecordLiteral implements ILiteral<ValueAtom.record> {
export class RecordLiteral implements ILiteral<MichelsonType.pair> {
_isExpression = true as const;
// Used for type checking
_type = {} as ValueAtom.record;
_type = MichelsonType.pair as const;

constructor(private fields: Record<string, IExpression>, private line: LineInfo) {}

Expand All @@ -96,13 +100,12 @@ export class RecordLiteral implements ILiteral<ValueAtom.record> {
}
}

export class MapLiteral<T extends ValueAtom.map | ValueAtom.big_map> implements ILiteral<T> {
export class MapLiteral<T extends MichelsonType.map | MichelsonType.big_map> implements ILiteral<T> {
_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,
Expand All @@ -118,11 +121,10 @@ export class MapLiteral<T extends ValueAtom.map | ValueAtom.big_map> implements
}
}

export class LambdaLiteral implements ILiteral<ValueAtom.lambda> {
export class LambdaLiteral implements ILiteral<MichelsonType.lambda> {
_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';
Expand Down
2 changes: 1 addition & 1 deletion src/core/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends TypeAtom> implements IType<T> {
// Used for type checking
Expand Down
30 changes: 14 additions & 16 deletions src/expression/blockchain_properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -17,7 +17,7 @@ import { IExpression } from '../typings/expression';
*
* @returns {IExpression} An expression
*/
export const GetAmount = (): IExpression<ValueAtom.mutez> => new Expression(ExpressionAtom.amount);
export const GetAmount = () => new Expression<MichelsonType.mutez>(ExpressionAtom.amount);

/**
* Get the contract balance.
Expand All @@ -31,7 +31,7 @@ export const GetAmount = (): IExpression<ValueAtom.mutez> => new Expression(Expr
*
* @returns {IExpression} An expression
*/
export const GetBalance = (): IExpression<ValueAtom.mutez> => new Expression(ExpressionAtom.balance);
export const GetBalance = () => new Expression<MichelsonType.mutez>(ExpressionAtom.balance);

/**
* Get an entrypoint of the current contract.
Expand All @@ -45,8 +45,8 @@ export const GetBalance = (): IExpression<ValueAtom.mutez> => new Expression(Exp
*
* @returns {IExpression} An expression
*/
export const GetSelf = (entry_point: string, line = new LineInfo()): IExpression<ValueAtom.contract> =>
new Expression(ExpressionAtom.self, `"${entry_point}"`, line);
export const GetSelf = (entry_point: string, line = new LineInfo()) =>
new Expression<MichelsonType.contract>(ExpressionAtom.self, `"${entry_point}"`, line);

/**
* Get current contract address.
Expand All @@ -60,7 +60,7 @@ export const GetSelf = (entry_point: string, line = new LineInfo()): IExpression
*
* @returns {IExpression} An expression
*/
export const GetSelfAddress = (): IExpression<ValueAtom.address> => new Expression(ExpressionAtom.self_address);
export const GetSelfAddress = () => new Expression<MichelsonType.address>(ExpressionAtom.self_address);

/**
* Get transaction sender.
Expand All @@ -74,7 +74,7 @@ export const GetSelfAddress = (): IExpression<ValueAtom.address> => new Expressi
*
* @returns {IExpression} An expression
*/
export const GetSender = (): IExpression<ValueAtom.address> => new Expression(ExpressionAtom.sender);
export const GetSender = () => new Expression<MichelsonType.address>(ExpressionAtom.sender);

/**
* Get transaction source.
Expand All @@ -88,7 +88,7 @@ export const GetSender = (): IExpression<ValueAtom.address> => new Expression(Ex
*
* @returns {IExpression} An expression
*/
export const GetSource = (): IExpression<ValueAtom.address> => new Expression(ExpressionAtom.source);
export const GetSource = () => new Expression<MichelsonType.address>(ExpressionAtom.source);

/**
* Get the chain identifier.
Expand All @@ -102,7 +102,7 @@ export const GetSource = (): IExpression<ValueAtom.address> => new Expression(Ex
*
* @returns {IExpression} An expression
*/
export const GetChain_id = (): IExpression<ValueAtom.chain_id> => new Expression(ExpressionAtom.chain_id);
export const GetChain_id = () => new Expression<MichelsonType.chain_id>(ExpressionAtom.chain_id);

/**
* Get the head block level.
Expand All @@ -116,7 +116,7 @@ export const GetChain_id = (): IExpression<ValueAtom.chain_id> => new Expression
*
* @returns {IExpression} An expression
*/
export const GetLevel = (): IExpression<ValueAtom.nat> => new Expression(ExpressionAtom.level);
export const GetLevel = () => new Expression<MichelsonType.nat>(ExpressionAtom.level);

/**
* Get the head block timestamp.
Expand All @@ -130,7 +130,7 @@ export const GetLevel = (): IExpression<ValueAtom.nat> => new Expression(Express
*
* @returns {IExpression} An expression
*/
export const GetTimestamp = (): IExpression<ValueAtom.timestamp> => new Expression(ExpressionAtom.now);
export const GetTimestamp = (): IExpression => new Expression<MichelsonType.timestamp>(ExpressionAtom.now);

/**
* Get total voting power.
Expand All @@ -144,7 +144,7 @@ export const GetTimestamp = (): IExpression<ValueAtom.timestamp> => new Expressi
*
* @returns {IExpression} An expression
*/
export const GetTotalVotingPower = (): IExpression<ValueAtom.nat> => new Expression(ExpressionAtom.total_voting_power);
export const GetTotalVotingPower = () => new Expression<MichelsonType.nat>(ExpressionAtom.total_voting_power);

/**
* Get the voting power of a given implicit account.
Expand All @@ -161,7 +161,5 @@ export const GetTotalVotingPower = (): IExpression<ValueAtom.nat> => new Express
*
* @returns {IExpression} An expression
*/
export const GetVotingPower = (
key_hash: ILiteral<ValueAtom.key_hash>,
line = new LineInfo(),
): IExpression<ValueAtom.nat> => new Expression(ExpressionAtom.voting_power, `${key_hash}`, line);
export const GetVotingPower = (key_hash: ILiteral<MichelsonType.key_hash>, line = new LineInfo()) =>
new Expression<MichelsonType.nat>(ExpressionAtom.voting_power, `${key_hash}`, line);
6 changes: 3 additions & 3 deletions src/expression/bls12_381.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -27,5 +27,5 @@ import ValueAtom from '../core/enums/literal';
*
* @returns {IExpression} An expression of types `TBool()`.
*/
export const PairingCheck = (pairs: IExpression<ValueAtom.list>, line = new LineInfo()) =>
new Expression<ValueAtom.bool>(ExpressionAtom.pairing_check, pairs, line);
export const PairingCheck = (pairs: IExpression<MichelsonType.list>, line = new LineInfo()) =>
new Expression<MichelsonType.bool>(ExpressionAtom.pairing_check, pairs, line);
9 changes: 5 additions & 4 deletions src/expression/comparison.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<MichelsonType.bool>(ExpressionAtom.lt, left, right, line);

/**
* Check if a value is greater than another value.
Expand All @@ -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<MichelsonType.bool>(ExpressionAtom.gt, left, right, line);

/**
* Check if a value is less than or equal another value.
Expand All @@ -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<MichelsonType.bool>(ExpressionAtom.le, left, right, line);

/**
* Check if a value is greater than or equal another value.
Expand All @@ -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<MichelsonType.bool>(ExpressionAtom.ge, left, right, line);

export const Comparison = {
LessThan,
Expand Down
Loading

0 comments on commit ba19cff

Please sign in to comment.