Skip to content

Commit

Permalink
feat: Adds literals (map, big_map)
Browse files Browse the repository at this point in the history
  • Loading branch information
RomarQ committed Jan 26, 2022
1 parent aeda987 commit aa8bde4
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 38 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
```ts
import { List, Nat, TNat } from '@tezwell/michelson-sdk';

const literal = List([Nat(1), Nat(2)], TNat);
const literal = List([Nat(1), Nat(2)], TNat());

// Micheline
console.log(literal.toMicheline()); // { 1 ; 2 }
Expand All @@ -28,7 +28,7 @@ console.log(literal.type.toJSON()); // { prim: 'list', args: [ { prim: '
```ts
import { TList, TNat } from '@tezwell/michelson-sdk';

const list_type = TList(TNat);
const list_type = TList(TNat());

// Micheline
console.log(list_type.toMicheline()); // (list nat)
Expand Down
1 change: 1 addition & 0 deletions src/core/enums/prim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ export enum Prim {
Some = 'Some',
None = 'None',
Pair = 'Pair',
Elt = 'Elt',
}
68 changes: 52 additions & 16 deletions src/core/literal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ import {
TTimestamp,
TUnit,
TPair,
TMap,
} from './type';
import type { Michelson_Type } from './type';
import Utils from '../misc/utils';
import { MichelsonJSON, MichelsonMicheline } from '../typings';
import { ILayout, IType } from '../typings/type';
import { Prim } from './enums/prim';
import { TBig_map } from '.';

export type Michelson_LiteralUnion = Michelson_Literal | Michelson_Literal_C1 | Michelson_Record;

Expand Down Expand Up @@ -195,6 +197,31 @@ export class Michelson_Literal_C1 {
}
}

export class Michelson_Map {
#elements: Michelson_LiteralUnion[][];
type: IType;

constructor(type: Michelson_Type, elements: Michelson_LiteralUnion[][]) {
this.type = type;
this.#elements = elements;
}

private buildMichelineElt = (key: Michelson_LiteralUnion, value: Michelson_LiteralUnion) => {
return `${Prim.Elt} ${key.toMicheline()} ${value.toMicheline()}`;
};

toMicheline(): MichelsonMicheline {
return `{ ${this.#elements.map(([key, value]) => this.buildMichelineElt(key, value)).join(' ; ')} }`;
}

toJSON(): MichelsonJSON {
return this.#elements.map(([key, value]) => ({
prim: Prim.Elt,
args: [key.toJSON(), value.toJSON()],
}));
}
}

class Michelson_Record {
#fields: Record<string, Michelson_LiteralUnion>;
type: IType;
Expand Down Expand Up @@ -273,25 +300,26 @@ class Michelson_Record {
}
}

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

export const Nat = (value: number) => new Michelson_Literal(Prim.nat, TNat, value);
export const Int = (value: number) => new Michelson_Literal(Prim.int, TInt, value);
export const Mutez = (value: number) => new Michelson_Literal(Prim.mutez, TMutez, value);
export const Timestamp = (value: number | string) => new Michelson_Literal(Prim.timestamp, TTimestamp, value);
export const Nat = (value: number) => new Michelson_Literal(Prim.nat, TNat(), value);
export const Int = (value: number) => new Michelson_Literal(Prim.int, TInt(), value);
export const Mutez = (value: number) => new Michelson_Literal(Prim.mutez, TMutez(), value);
export const Timestamp = (value: number | string) => new Michelson_Literal(Prim.timestamp, TTimestamp(), value);

export const String = (value: string) => new Michelson_Literal(Prim.string, TString, value);
export const Address = (value: string) => new Michelson_Literal(Prim.address, TAddress, value);
export const Bytes = (value: string) => new Michelson_Literal(Prim.bytes, TBytes, value);
export const Chain_id = (value: string) => new Michelson_Literal(Prim.chain_id, TChain_id, value);
export const Bls12_381_fr = (value: string | number) => new Michelson_Literal(Prim.bls12_381_fr, TBls12_381_fr, value);
export const Bls12_381_g1 = (value: string) => new Michelson_Literal(Prim.bls12_381_g1, TBls12_381_g1, value);
export const Bls12_381_g2 = (value: string) => new Michelson_Literal(Prim.bls12_381_g2, TBls12_381_g2, value);
export const Key = (value: string) => new Michelson_Literal(Prim.key, TKey, value);
export const Key_hash = (value: string) => new Michelson_Literal(Prim.key_hash, TKey_hash, value);
export const Signature = (value: string) => new Michelson_Literal(Prim.signature, TSignature, value);
export const String = (value: string) => new Michelson_Literal(Prim.string, TString(), value);
export const Address = (value: string) => new Michelson_Literal(Prim.address, TAddress(), value);
export const Bytes = (value: string) => new Michelson_Literal(Prim.bytes, TBytes(), value);
export const Chain_id = (value: string) => new Michelson_Literal(Prim.chain_id, TChain_id(), value);
export const Bls12_381_fr = (value: string | number) =>
new Michelson_Literal(Prim.bls12_381_fr, TBls12_381_fr(), value);
export const Bls12_381_g1 = (value: string) => new Michelson_Literal(Prim.bls12_381_g1, TBls12_381_g1(), value);
export const Bls12_381_g2 = (value: string) => new Michelson_Literal(Prim.bls12_381_g2, TBls12_381_g2(), value);
export const Key = (value: string) => new Michelson_Literal(Prim.key, TKey(), value);
export const Key_hash = (value: string) => new Michelson_Literal(Prim.key_hash, TKey_hash(), value);
export const Signature = (value: string) => new Michelson_Literal(Prim.signature, TSignature(), value);

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

export const List = (elements: Michelson_LiteralUnion[], innerType: IType) =>
new Michelson_Literal_C1(Prim.list, TList(innerType), elements);
Expand All @@ -308,6 +336,11 @@ export const Pair = (left: Michelson_LiteralUnion, right: Michelson_LiteralUnion
export const Record = (fields: Record<string, Michelson_LiteralUnion>, layout?: ILayout) =>
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);

const Literals = {
Unit,
Nat,
Expand All @@ -332,6 +365,9 @@ const Literals = {
Some,
Pair,
Record,
Map,
Big_map,
// Lambda,
};

export default Literals;
32 changes: 16 additions & 16 deletions src/core/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,22 +209,22 @@ export class Michelson_Type_Record implements IType {
}

// Singleton types
export const TUnit = new Michelson_Type(PrimType.unit);
export const TNat = new Michelson_Type(PrimType.nat);
export const TInt = new Michelson_Type(PrimType.int);
export const TMutez = new Michelson_Type(PrimType.mutez);
export const TString = new Michelson_Type(PrimType.string);
export const TBool = new Michelson_Type(PrimType.bool);
export const TAddress = new Michelson_Type(PrimType.address);
export const TTimestamp = new Michelson_Type(PrimType.timestamp);
export const TChain_id = new Michelson_Type(PrimType.chain_id);
export const TBytes = new Michelson_Type(PrimType.bytes);
export const TBls12_381_fr = new Michelson_Type(PrimType.bls12_381_fr);
export const TBls12_381_g1 = new Michelson_Type(PrimType.bls12_381_g1);
export const TBls12_381_g2 = new Michelson_Type(PrimType.bls12_381_g2);
export const TKey = new Michelson_Type(PrimType.key);
export const TKey_hash = new Michelson_Type(PrimType.key_hash);
export const TSignature = new Michelson_Type(PrimType.signature);
export const TUnit = () => new Michelson_Type(PrimType.unit);
export const TNat = () => new Michelson_Type(PrimType.nat);
export const TInt = () => new Michelson_Type(PrimType.int);
export const TMutez = () => new Michelson_Type(PrimType.mutez);
export const TString = () => new Michelson_Type(PrimType.string);
export const TBool = () => new Michelson_Type(PrimType.bool);
export const TAddress = () => new Michelson_Type(PrimType.address);
export const TTimestamp = () => new Michelson_Type(PrimType.timestamp);
export const TChain_id = () => new Michelson_Type(PrimType.chain_id);
export const TBytes = () => new Michelson_Type(PrimType.bytes);
export const TBls12_381_fr = () => new Michelson_Type(PrimType.bls12_381_fr);
export const TBls12_381_g1 = () => new Michelson_Type(PrimType.bls12_381_g1);
export const TBls12_381_g2 = () => new Michelson_Type(PrimType.bls12_381_g2);
export const TKey = () => new Michelson_Type(PrimType.key);
export const TKey_hash = () => new Michelson_Type(PrimType.key_hash);
export const TSignature = () => new Michelson_Type(PrimType.signature);

// Container types
export const TList = (innerType: IType) => new Michelson_Type(PrimType.list, innerType);
Expand Down
54 changes: 54 additions & 0 deletions tests/e2e/__snapshots__/validate_michelson.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,60 @@ Array [
]
`;

exports[`[E2E] - Michelson compilation (Maps) Big Map 1`] = `
Array [
Object {
"args": Array [
Object {
"string": "KEY1",
},
Object {
"int": "1",
},
],
"prim": "Elt",
},
Object {
"args": Array [
Object {
"string": "KEY2",
},
Object {
"int": "2",
},
],
"prim": "Elt",
},
]
`;

exports[`[E2E] - Michelson compilation (Maps) Map 1`] = `
Array [
Object {
"args": Array [
Object {
"int": "1",
},
Object {
"string": "VALUE1",
},
],
"prim": "Elt",
},
Object {
"args": Array [
Object {
"int": "2",
},
Object {
"string": "VALUE2",
},
],
"prim": "Elt",
},
]
`;

exports[`[E2E] - Michelson compilation (Option) None 1`] = `
Object {
"prim": "None",
Expand Down
49 changes: 45 additions & 4 deletions tests/e2e/validate_michelson.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ import {
Key,
Signature,
Key_hash,
Map,
Big_map,
} from '../../src/core/literal';
import { TNat } from '../../src/core/type';
import { TNat, TString } from '../../src/core/type';

const TEZOS_CLIENT_CMD = './tests/e2e/tezos-binaries/tezos-client';

Expand Down Expand Up @@ -285,7 +287,7 @@ if (process.platform == 'linux') {

describe('[E2E] - Michelson compilation (List)', () => {
it('List', () => {
const literal = List([Nat(1), Nat(2)], TNat);
const literal = List([Nat(1), Nat(2)], TNat());
const value = literal.toMicheline();
const type = literal.type.toMicheline();
const jsonValue = literal.toJSON();
Expand All @@ -296,7 +298,7 @@ if (process.platform == 'linux') {
expect(jsonValue).toMatchSnapshot();
});
it('Set', () => {
const literal = Set([Nat(1), Nat(2)], TNat);
const literal = Set([Nat(1), Nat(2)], TNat());
const value = literal.toMicheline();
const type = literal.type.toMicheline();
const jsonValue = literal.toJSON();
Expand All @@ -321,7 +323,7 @@ if (process.platform == 'linux') {
expect(jsonValue).toMatchSnapshot();
});
it('None', () => {
const literal = None(TNat);
const literal = None(TNat());
const value = literal.toMicheline();
const type = literal.type.toMicheline();
const jsonValue = literal.toJSON();
Expand Down Expand Up @@ -412,4 +414,43 @@ if (process.platform == 'linux') {
expect(jsonValue).toMatchSnapshot();
});
});

describe('[E2E] - Michelson compilation (Maps)', () => {
it('Map', () => {
const literal = Map(
[
[Nat(1), String('VALUE1')],
[Nat(2), String('VALUE2')],
],
TNat(),
TString(),
);
const value = literal.toMicheline();
const type = literal.type.toMicheline();
const jsonValue = literal.toJSON();

const result = convertMichelsonToJSON(`'${value}'`, type);

expect(jsonValue).toEqual(JSON.parse(result));
expect(jsonValue).toMatchSnapshot();
});
it('Big Map', () => {
const literal = Big_map(
[
[String('KEY1'), Nat(1)],
[String('KEY2'), Nat(2)],
],
TString(),
TNat(),
);
const value = literal.toMicheline();
const type = literal.type.toMicheline();
const jsonValue = literal.toJSON();

const result = convertMichelsonToJSON(`'${value}'`, type);

expect(jsonValue).toEqual(JSON.parse(result));
expect(jsonValue).toMatchSnapshot();
});
});
}

0 comments on commit aa8bde4

Please sign in to comment.