Skip to content

Commit

Permalink
feat(): Adds expression for adding/subtracting seconds, minutes and h…
Browse files Browse the repository at this point in the history
…ours to timestamps
  • Loading branch information
RomarQ committed Jul 16, 2022
1 parent b726bac commit c9be0ca
Show file tree
Hide file tree
Showing 16 changed files with 162 additions and 9 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/visual-tez",
"version": "1.2.1",
"version": "1.2.2",
"description": "A visual programming tool for writing smart contracts in the Tezos blockchain",
"private": true,
"author": {
Expand Down
3 changes: 3 additions & 0 deletions src/blocks/enums/BlockKind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ export enum BlockKind {
read_ticket_expression = 'read_ticket_expression',
join_ticket_expression = 'join_ticket_expression',
split_ticket_expression = 'split_ticket_expression',
add_seconds = 'add_seconds',
add_minutes = 'add_minutes',
add_hours = 'add_hours',

// Statements
return_statement_block = 'return_statement_block',
Expand Down
1 change: 1 addition & 0 deletions src/blocks/expression/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ import './variant';
import './string';
import './address';
import './ticket';
import './timestamp';
5 changes: 3 additions & 2 deletions src/blocks/expression/string/ends_with.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import Blockly from 'blockly';
import type { Block } from 'src/typings/blockly';
import type { IExpression } from '@tezwell/smartts-sdk/typings/expression';
import { StringHelpers } from '@tezwell/smartts-sdk/lib';

import type { Block } from 'src/typings/blockly';
import BlockKind from '../../enums/BlockKind';
import SmartML from '../../generators/SmartML';
import { buildErrorInfo } from 'src/blocks/utils/errorHandling';

Blockly.Blocks[BlockKind.string_ends_with] = {
init: function () {
Expand All @@ -26,6 +27,6 @@ SmartML.addBlock(BlockKind.string_ends_with, {
toValue: (block: Block) => {
const text: IExpression<any> = SmartML.toValue(block, 'TEXT');
const postfix: IExpression<any> = SmartML.toValue(block, 'POSTFIX');
return StringHelpers.EndsWith(text, postfix);
return StringHelpers.EndsWith(text, postfix, buildErrorInfo(block));
},
});
5 changes: 3 additions & 2 deletions src/blocks/expression/string/starts_with.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import Blockly from 'blockly';
import type { Block } from 'src/typings/blockly';
import type { IExpression } from '@tezwell/smartts-sdk/typings/expression';
import { StringHelpers } from '@tezwell/smartts-sdk/lib';

import type { Block } from 'src/typings/blockly';
import BlockKind from '../../enums/BlockKind';
import SmartML from '../../generators/SmartML';
import { buildErrorInfo } from 'src/blocks/utils/errorHandling';

Blockly.Blocks[BlockKind.string_starts_with] = {
init: function () {
Expand All @@ -26,6 +27,6 @@ SmartML.addBlock(BlockKind.string_starts_with, {
toValue: (block: Block) => {
const text: IExpression<any> = SmartML.toValue(block, 'TEXT');
const prefix: IExpression<any> = SmartML.toValue(block, 'PREFIX');
return StringHelpers.StartsWith(text, prefix);
return StringHelpers.StartsWith(text, prefix, buildErrorInfo(block));
},
});
3 changes: 2 additions & 1 deletion src/blocks/expression/ticket/create_ticket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { IExpression } from '@tezwell/smartts-sdk/typings/expression';
import BlockKind from '../../enums/BlockKind';
import SmartML from '../../generators/SmartML';
import { CreateTicket } from '@tezwell/smartts-sdk';
import { buildErrorInfo } from 'src/blocks/utils/errorHandling';

Blockly.Blocks[BlockKind.create_ticket_expression] = {
init: function () {
Expand All @@ -27,6 +28,6 @@ SmartML.addBlock(BlockKind.create_ticket_expression, {
toValue: (block: Block) => {
const content: IExpression<any> = SmartML.toValue(block, 'CONTENT');
const quantity: IExpression<any> = SmartML.toValue(block, 'QUANTITY');
return CreateTicket(content, quantity);
return CreateTicket(content, quantity, buildErrorInfo(block));
},
});
3 changes: 2 additions & 1 deletion src/blocks/expression/ticket/join_ticket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { IExpression } from '@tezwell/smartts-sdk/typings/expression';
import BlockKind from '../../enums/BlockKind';
import SmartML from '../../generators/SmartML';
import { JoinTicket } from '@tezwell/smartts-sdk';
import { buildErrorInfo } from 'src/blocks/utils/errorHandling';

Blockly.Blocks[BlockKind.join_ticket_expression] = {
init: function () {
Expand All @@ -27,6 +28,6 @@ SmartML.addBlock(BlockKind.join_ticket_expression, {
toValue: (block: Block) => {
const ticket1: IExpression<any> = SmartML.toValue(block, 'TICKET1');
const ticket2: IExpression<any> = SmartML.toValue(block, 'TICKET2');
return JoinTicket(ticket1, ticket2);
return JoinTicket(ticket1, ticket2, buildErrorInfo(block));
},
});
3 changes: 2 additions & 1 deletion src/blocks/expression/ticket/read_ticket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { IExpression } from '@tezwell/smartts-sdk/typings/expression';
import BlockKind from '../../enums/BlockKind';
import SmartML from '../../generators/SmartML';
import { ReadTicket } from '@tezwell/smartts-sdk';
import { buildErrorInfo } from 'src/blocks/utils/errorHandling';

Blockly.Blocks[BlockKind.read_ticket_expression] = {
init: function () {
Expand All @@ -25,6 +26,6 @@ Blockly.Blocks[BlockKind.read_ticket_expression] = {
SmartML.addBlock(BlockKind.read_ticket_expression, {
toValue: (block: Block) => {
const ticket: IExpression<any> = SmartML.toValue(block, 'TICKET');
return ReadTicket(ticket);
return ReadTicket(ticket, buildErrorInfo(block));
},
});
3 changes: 2 additions & 1 deletion src/blocks/expression/ticket/split_ticket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { IExpression } from '@tezwell/smartts-sdk/typings/expression';
import BlockKind from '../../enums/BlockKind';
import SmartML from '../../generators/SmartML';
import { SplitTicket } from '@tezwell/smartts-sdk';
import { buildErrorInfo } from 'src/blocks/utils/errorHandling';

Blockly.Blocks[BlockKind.split_ticket_expression] = {
init: function () {
Expand All @@ -29,6 +30,6 @@ SmartML.addBlock(BlockKind.split_ticket_expression, {
const ticket: IExpression<any> = SmartML.toValue(block, 'TICKET');
const quantity1: IExpression<any> = SmartML.toValue(block, 'QUANTITY1');
const quantity2: IExpression<any> = SmartML.toValue(block, 'QUANTITY2');
return SplitTicket(ticket, quantity1, quantity2);
return SplitTicket(ticket, quantity1, quantity2, buildErrorInfo(block));
},
});
32 changes: 32 additions & 0 deletions src/blocks/expression/timestamp/add_hours.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import Blockly from 'blockly';
import type { Block } from 'src/typings/blockly';
import type { IExpression } from '@tezwell/smartts-sdk/typings/expression';

import BlockKind from '../../enums/BlockKind';
import SmartML from '../../generators/SmartML';
import { AddHours } from '@tezwell/smartts-sdk';
import { buildErrorInfo } from 'src/blocks/utils/errorHandling';

Blockly.Blocks[BlockKind.add_hours] = {
init: function () {
this.appendValueInput('HOURS').setCheck(['Int', 'Expression']).appendField('Add hours');
this.appendValueInput('TIMESTAMP').setCheck(['Timescript', 'Expression']).appendField('to timestamp');

this.setInputsInline(true);
this.setOutput(true, ['Expression']);

this.setTooltip(`Add seconds to timestamp.\n-\n(TTimestamp(), TInt()) => TTimestamp()\n`);

this.setColour(200);
this.setPreviousStatement(false);
this.setNextStatement(false);
},
};

SmartML.addBlock(BlockKind.add_hours, {
toValue: (block: Block) => {
const timestamp: IExpression<any> = SmartML.toValue(block, 'TIMESTAMP');
const hours: IExpression<any> = SmartML.toValue(block, 'HOURS');
return AddHours(timestamp, hours, buildErrorInfo(block));
},
});
32 changes: 32 additions & 0 deletions src/blocks/expression/timestamp/add_minutes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import Blockly from 'blockly';
import type { Block } from 'src/typings/blockly';
import type { IExpression } from '@tezwell/smartts-sdk/typings/expression';

import BlockKind from '../../enums/BlockKind';
import SmartML from '../../generators/SmartML';
import { AddMinutes } from '@tezwell/smartts-sdk';
import { buildErrorInfo } from 'src/blocks/utils/errorHandling';

Blockly.Blocks[BlockKind.add_minutes] = {
init: function () {
this.appendValueInput('MINUTES').setCheck(['Int', 'Expression']).appendField('Add minutes');
this.appendValueInput('TIMESTAMP').setCheck(['Timescript', 'Expression']).appendField('to timestamp');

this.setInputsInline(true);
this.setOutput(true, ['Expression']);

this.setTooltip(`Add seconds to timestamp.\n-\n(TTimestamp(), TInt()) => TTimestamp()\n`);

this.setColour(200);
this.setPreviousStatement(false);
this.setNextStatement(false);
},
};

SmartML.addBlock(BlockKind.add_minutes, {
toValue: (block: Block) => {
const timestamp: IExpression<any> = SmartML.toValue(block, 'TIMESTAMP');
const minutes: IExpression<any> = SmartML.toValue(block, 'MINUTES');
return AddMinutes(timestamp, minutes, buildErrorInfo(block));
},
});
32 changes: 32 additions & 0 deletions src/blocks/expression/timestamp/add_seconds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import Blockly from 'blockly';
import type { Block } from 'src/typings/blockly';
import type { IExpression } from '@tezwell/smartts-sdk/typings/expression';

import BlockKind from '../../enums/BlockKind';
import SmartML from '../../generators/SmartML';
import { AddSeconds } from '@tezwell/smartts-sdk';
import { buildErrorInfo } from 'src/blocks/utils/errorHandling';

Blockly.Blocks[BlockKind.add_seconds] = {
init: function () {
this.appendValueInput('SECONDS').setCheck(['Int', 'Expression']).appendField('Add seconds');
this.appendValueInput('TIMESTAMP').setCheck(['Timescript', 'Expression']).appendField('to timestamp');

this.setInputsInline(true);
this.setOutput(true, ['Expression']);

this.setTooltip(`Add seconds to timestamp.\n-\n(TTimestamp(), TInt()) => TTimestamp()\n`);

this.setColour(200);
this.setPreviousStatement(false);
this.setNextStatement(false);
},
};

SmartML.addBlock(BlockKind.add_seconds, {
toValue: (block: Block) => {
const timestamp: IExpression<any> = SmartML.toValue(block, 'TIMESTAMP');
const seconds: IExpression<any> = SmartML.toValue(block, 'SECONDS');
return AddSeconds(timestamp, seconds, buildErrorInfo(block));
},
});
3 changes: 3 additions & 0 deletions src/blocks/expression/timestamp/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import './add_seconds';
import './add_minutes';
import './add_hours';
1 change: 1 addition & 0 deletions src/components/blockly/blocks/expressions/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ export * from './record';
export * from './variables';
export * from './variant';
export * from './ticket';
export * from './timestamp';
30 changes: 30 additions & 0 deletions src/components/blockly/blocks/expressions/timestamp.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from 'react';

import BlockKind from 'src/blocks/enums/BlockKind';
import Block from 'src/components/blockly/Block';
import Shadow from '../../Shadow';
import Value from '../../Value';

export const AddSeconds = () => (
<Block type={BlockKind.add_seconds} tags={['expression', 'timestamp', 'add', 'seconds']}>
<Value name="SECONDS">
<Shadow type={BlockKind.int_literal} />
</Value>
</Block>
);

export const AddMinutes = () => (
<Block type={BlockKind.add_minutes} tags={['expression', 'timestamp', 'add', 'minutes']}>
<Value name="MINUTES">
<Shadow type={BlockKind.int_literal} />
</Value>
</Block>
);

export const AddHours = () => (
<Block type={BlockKind.add_hours} tags={['expression', 'timestamp', 'add', 'hours']}>
<Value name="HOURS">
<Shadow type={BlockKind.int_literal} />
</Value>
</Block>
);
13 changes: 13 additions & 0 deletions src/pages/editor/view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ import {
ReadTicket,
JoinTicket,
SplitTicket,
AddSeconds,
AddHours,
AddMinutes,
} from 'src/components/blockly/blocks/expressions';
import Logger from 'src/utils/logger';
import { validateBlockLocation } from 'src/blocks/utils/workspace';
Expand Down Expand Up @@ -460,6 +463,10 @@ const EditorView: React.FC<EditorViewProps> = ({ workspaceRef, compile, onError
<JoinTicket />
<SplitTicket />

<AddSeconds />
<AddMinutes />
<AddHours />

{/* Statements */}
<AssertStatement />
<IfStatement />
Expand Down Expand Up @@ -626,6 +633,12 @@ const EditorView: React.FC<EditorViewProps> = ({ workspaceRef, compile, onError
<Slice />
</Category>

<Category name="Timestamp" categorystyle="expr_stmt_category">
<AddSeconds />
<AddMinutes />
<AddHours />
</Category>

<Category name="Contract" categorystyle="expr_stmt_category">
<ImplicitAccountExpression />
<GetContractExpression />
Expand Down

0 comments on commit c9be0ca

Please sign in to comment.