Skip to content

Commit

Permalink
fix: Fixes blockchain operations and adds tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RomarQ committed Feb 3, 2022
1 parent 59f326c commit 3dbda4d
Show file tree
Hide file tree
Showing 9 changed files with 491 additions and 74 deletions.
7 changes: 5 additions & 2 deletions src/core/blockchain_operations.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { LineInfo } from '../misc/utils';
import { ILiteral } from '../typings/literal';
import { Expression } from './expression';

/**
Expand All @@ -23,7 +25,7 @@ export const GetTimestamp = () => new Expression('now');
/**
* @see https://tezos.gitlab.io/michelson-reference/#instr-SELF_ADDRESS
*/
export const GetSelf = () => new Expression('self');
export const GetSelf = (entry_point = '', line = new LineInfo()) => new Expression('self', `"${entry_point}"`, line);
/**
* @see https://tezos.gitlab.io/michelson-reference/#instr-SELF_ADDRESS
*/
Expand All @@ -43,4 +45,5 @@ export const GetTotalVotingPower = () => new Expression('total_voting_power');
/**
* @see https://tezos.gitlab.io/michelson-reference/#instr-VOTING_POWER
*/
export const GetVotingPower = (key_hash: string) => new Expression('voting_power', key_hash);
export const GetVotingPower = (key_hash: ILiteral<'key_hash'>, line = new LineInfo()) =>
new Expression('voting_power', `${key_hash}`, line);
2 changes: 1 addition & 1 deletion src/core/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class C_Verify implements IStatement {
return `(verify ${this.condition} ${this.errorMsg} ${this.line})`;
}
}
export const Require = (condition: IExpression, errorMsg: ILiteral | IExpression, line = new LineInfo()) =>
export const Require = (condition: IExpression, errorMsg: ILiteral<unknown> | IExpression, line = new LineInfo()) =>
new C_Verify(condition, errorMsg, line);

class C_SetValue implements IStatement {
Expand Down
2 changes: 1 addition & 1 deletion src/core/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class Expression implements IExpression {
_isExpression = true as const;
args;

constructor(public name: string, ...args: (IExpression | LineInfo | IType | string | ILiteral)[]) {
constructor(public name: string, ...args: (IExpression | LineInfo | IType | string | ILiteral<unknown>)[]) {
this.args = args || [];
}

Expand Down
4 changes: 2 additions & 2 deletions src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class Contract {
return this;
}

public setStorage(storage: ILiteral) {
public setStorage(storage: ILiteral<unknown>) {
this.#storage = storage;
return this;
}
Expand All @@ -109,7 +109,7 @@ export class Contract {
return this;
}

public setConfig(options?: { initialBalance?: ILiteral; flags?: Flag[] }) {
public setConfig(options?: { initialBalance?: ILiteral<unknown>; flags?: Flag[] }) {
if (options?.flags) {
this.#options.flags = options.flags;
}
Expand Down
40 changes: 19 additions & 21 deletions src/core/literal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ import {
TRecord,
TMap,
TBig_map,
TKey_hash,
} from '../type';
import { Prim } from '../enums/prim';
import { IExpression, IExpressionKind } from '../../typings/expression';
import { Layout } from '../enums/layout';

class Literal implements ILiteral {
class Literal<T extends string> implements ILiteral<T> {
_isExpression = true as const;
_isLiteral = true as const;

constructor(
public name: string,
public name: T,
public value: number | string | boolean | undefined,
public type: IType,
public line: LineInfo,
Expand All @@ -47,11 +47,10 @@ class Literal implements ILiteral {
}
}

class ListLiteral implements ILiteral {
class ListLiteral<T> implements ILiteral<T> {
_isExpression = true as const;
_isLiteral = true as const;

constructor(public name: string, public items: IExpressionKind[], public type: IType, public line: LineInfo) {}
constructor(public name: T, public items: IExpressionKind[], public type: IType, public line: LineInfo) {}

toString() {
return `(${this.name} ${this.line} ${this.items.map((item) => item.toString()).join(' ')})`;
Expand All @@ -62,9 +61,9 @@ class ListLiteral implements ILiteral {
}
}

class OptionLiteral implements ILiteral {
class OptionLiteral implements ILiteral<'option'> {
_isExpression = true as const;
_isLiteral = true as const;
name = 'option' as const;

type: IType;

Expand All @@ -86,13 +85,13 @@ class OptionLiteral implements ILiteral {
}
}

class RecordLiteral implements ILiteral {
class RecordLiteral implements ILiteral<'record'> {
_isExpression = true as const;
_isLiteral = true as const;
name = 'record' as const;

type: IType;

constructor(public fields: Record<string, ILiteral>, public line: LineInfo) {
constructor(public fields: Record<string, ILiteral<unknown>>, public line: LineInfo) {
// Compute the record type (use rightcombs by default)
this.type = TRecord(
Object.entries(fields).reduce(
Expand All @@ -119,19 +118,14 @@ class RecordLiteral implements ILiteral {
}
}

class MapLiteral implements ILiteral {
class MapLiteral<T extends Prim.map | Prim.big_map> implements ILiteral<T> {
_isExpression = true as const;
_isLiteral = true as const;
name: T;

type: IType;

constructor(
public prim: Prim.map | Prim.big_map,
public rows: IExpression[][],
keyType: IType,
valueType: IType,
public line: LineInfo,
) {
constructor(public prim: T, public rows: IExpression[][], keyType: IType, valueType: IType, public line: LineInfo) {
this.name = prim;
if (prim === Prim.map) {
this.type = TMap(keyType, valueType);
} else {
Expand Down Expand Up @@ -173,6 +167,9 @@ export const ChainID = (chainID: string, line = new LineInfo()) =>

export const Bytes = (bytes: string, line = new LineInfo()) => new Literal(Prim.bytes, bytes, TBytes(), line);

export const Key_hash = (key_hash: string, line = new LineInfo()) =>
new Literal<'key_hash'>(Prim.key_hash, key_hash, TKey_hash(), line);

// Containers
export const List = (items: IExpressionKind[], innerType: IType, line = new LineInfo()) =>
new ListLiteral(Prim.list, items, TList(innerType), line);
Expand All @@ -181,7 +178,8 @@ export const Some = (value: IExpressionKind, innerType?: IType, line = new LineI
export const None = (innerType?: IType, line = new LineInfo()) =>
new OptionLiteral(Prim.None, undefined, innerType, line);

export const Record = (fields: Record<string, ILiteral>, line = new LineInfo()) => new RecordLiteral(fields, line);
export const Record = (fields: Record<string, ILiteral<unknown>>, line = new LineInfo()) =>
new RecordLiteral(fields, line);

export const Map = (
rows: IExpression[][] = [],
Expand Down
2 changes: 1 addition & 1 deletion src/typings/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ export interface IExpression extends IToString {
_isExpression: true;
}

export type IExpressionKind = string | IExpression | ILiteral;
export type IExpressionKind = string | IExpression | ILiteral<any>;
4 changes: 2 additions & 2 deletions src/typings/literal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { IType } from './type';

export type ILayout = (string | ILayout)[];

export interface ILiteral extends IExpression {
_isLiteral: true;
export interface ILiteral<T> extends IExpression {
name: T;
type: IType;
}
Loading

0 comments on commit 3dbda4d

Please sign in to comment.