Skip to content

Commit

Permalink
feat: Adds variant type and tests to types without literal
Browse files Browse the repository at this point in the history
  • Loading branch information
RomarQ committed Jan 28, 2022
1 parent ec0af73 commit 072b9e5
Show file tree
Hide file tree
Showing 7 changed files with 569 additions and 28 deletions.
27 changes: 12 additions & 15 deletions src/core/literal.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
Michelson_Type_Record,
Michelson_Type_RecordOrVariant,
TBls12_381_fr,
TBls12_381_g1,
TBls12_381_g2,
Expand Down Expand Up @@ -172,7 +172,7 @@ class Michelson_Record {

constructor(fields: Record<string, Michelson_LiteralUnion>, layout?: PairsOfKeys<keyof typeof fields>) {
this.#fields = fields;
this.#layout = layout || Michelson_Type_Record.composeRightCombLayout(Object.keys(fields));
this.#layout = layout || Michelson_Type_RecordOrVariant.composeRightCombLayout(Object.keys(fields));

this.type = TRecord(
Object.entries(fields).reduce((pv, [key, value]) => {
Expand Down Expand Up @@ -246,14 +246,12 @@ class Michelson_Record {
}
}

export const Unit = () => new Michelson_Literal(Prim.Unit, TUnit());

// Singletons
export const Nat = (value: number) => new Michelson_Literal(Prim.int, TNat(), value);
export const Int = (value: number) => new Michelson_Literal(Prim.int, TInt(), value);
export const Mutez = (value: number) => new Michelson_Literal(Prim.int, TMutez(), value);
export const Timestamp = (value: number | string) =>
new Michelson_Literal(typeof value === 'string' ? Prim.string : Prim.int, TTimestamp(), value);

export const String = (value: string) => new Michelson_Literal(Prim.string, TString(), value);
export const Address = (value: string) => new Michelson_Literal(Prim.string, TAddress(), value);
export const Bytes = (value: string) => new Michelson_Literal(Prim.bytes, TBytes(), value);
Expand All @@ -267,30 +265,28 @@ export const Bls12_381_g2 = (value: string) => new Michelson_Literal(Prim.bytes,
export const Key = (value: string) => new Michelson_Literal(Prim.string, TKey(), value);
export const Key_hash = (value: string) => new Michelson_Literal(Prim.string, TKey_hash(), value);
export const Signature = (value: string) => new Michelson_Literal(Prim.string, TSignature(), value);

export const Bool = (value: boolean) => new Michelson_Literal(Prim.bool, TBool(), value);

export const Unit = () => new Michelson_Literal(Prim.Unit, TUnit());
// Containers
export const List = (elements: Michelson_LiteralUnion[], innerType: IType) =>
new Michelson_Literal_C1(Prim.list, TList(innerType), elements);

export const Set = (elements: Michelson_LiteralUnion[], innerType: IType) =>
new Michelson_Literal_C1(Prim.list, TSet(innerType), elements);

export const None = (innerType: IType) => new Michelson_Literal(Prim.None, TOption(innerType));
export const Some = (element: Michelson_LiteralUnion) =>
new Michelson_Literal_C1(Prim.Some, TOption(element.type), [element]);

export const Pair = (left: Michelson_LiteralUnion, right: Michelson_LiteralUnion) =>
new Michelson_Literal_C1(Prim.Pair, TPair(left.type, right.type), [left, right]);
export const Record = (fields: Record<string, Michelson_LiteralUnion>, layout?: PairsOfKeys<keyof typeof fields>) =>
new Michelson_Record(fields, layout);

export const Map = (elements: Michelson_LiteralUnion[][], keyType: IType, valueType: IType) =>
new Michelson_Map(TMap(keyType, valueType), elements);
export const Big_map = (elements: Michelson_LiteralUnion[][], keyType: IType, valueType: IType) =>
new Michelson_Map(TBig_map(keyType, valueType), elements);
// Artificial containers
export const Record = (fields: Record<string, Michelson_LiteralUnion>, layout?: PairsOfKeys<keyof typeof fields>) =>
new Michelson_Record(fields, layout);

const Literals = {
// Singletons
Unit,
Nat,
Int,
Expand All @@ -307,16 +303,17 @@ const Literals = {
Key,
Key_hash,
Signature,
//
// Containers
List,
Set,
None,
Some,
Pair,
Record,
Map,
Big_map,
// Lambda,
// Artificial containers
Record,
};

export default Literals;
15 changes: 8 additions & 7 deletions src/core/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,17 +172,16 @@ export class Michelson_Type_With_Param implements IType {
}
}

export class Michelson_Type_Record<T extends Record<string, IType> = Record<string, IType>> implements IType {
export class Michelson_Type_RecordOrVariant<T extends Record<string, IType> = Record<string, IType>> implements IType {
_isType = true as const;
#annotation?: string;
#fields: T;
// Default: right combs => https://tezos.gitlab.io/active/michelson.html#operations-on-pairs-and-right-combs
#layout: PairsOfKeys<keyof T>;

constructor(fields: T, layout?: PairsOfKeys<keyof T>) {
constructor(private type: Prim.or | Prim.pair, fields: T, layout?: PairsOfKeys<keyof T>) {
this.#fields = fields;
this.#layout =
layout || Michelson_Type_Record.composeRightCombLayout<keyof T>(Object.keys(fields) as (keyof T)[]);
this.#layout = layout || Michelson_Type_RecordOrVariant.composeRightCombLayout(Object.keys(fields));
}

static composeRightCombLayout = <K>(fields: K[]): PairsOfKeys<K> => {
Expand Down Expand Up @@ -218,7 +217,7 @@ export class Michelson_Type_Record<T extends Record<string, IType> = Record<stri
return fields[layout].toMicheline();
}, '')
.join(' ');
return `(${Prim.pair}${annotation} ${innerTypes})`;
return `(${this.type}${annotation} ${innerTypes})`;
}

/**
Expand All @@ -237,7 +236,7 @@ export class Michelson_Type_Record<T extends Record<string, IType> = Record<stri
*/
private _toJSON(fields: T, layout: PairsOfKeys<keyof T>): MichelsonJSON {
return {
prim: Prim.pair,
prim: this.type,
...(this.#annotation ? { annots: [`%${this.#annotation}`] } : {}),
args: layout.map((layout) => {
if (Array.isArray(layout)) {
Expand Down Expand Up @@ -301,7 +300,9 @@ export const TSapling_transaction = (memoSize: number) =>
new Michelson_Type_With_Param(Prim.sapling_transaction, memoSize);
// Artificial Types
export const TRecord = (fields: Record<string, IType>, layout?: PairsOfKeys<string>) =>
new Michelson_Type_Record(fields, layout);
new Michelson_Type_RecordOrVariant(Prim.pair, fields, layout);
export const TVariant = (fields: Record<string, IType>, layout?: PairsOfKeys<string>) =>
new Michelson_Type_RecordOrVariant(Prim.or, fields, layout);

const Types = {
// Singleton types
Expand Down
Loading

0 comments on commit 072b9e5

Please sign in to comment.