Skip to content

Commit

Permalink
Merge pull request #2 from RomarQ/rq@distribution-tests
Browse files Browse the repository at this point in the history
build: Reuse smartML.js on multiple bundles and adds a test for es6 i…
  • Loading branch information
RomarQ committed Feb 2, 2022
2 parents 9ab974c + ee2a379 commit 54646c5
Show file tree
Hide file tree
Showing 13 changed files with 164 additions and 68 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ jobs:
cp package.json dist
cd tests/distributables
yarn
yarn test
yarn test-esm
yarn test-cjs
- name: Coveralls Parallel
uses: coverallsapp/github-action@master
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
yarn-error.log

# Coverage report
coverage

Expand Down
33 changes: 16 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,26 @@
`SmartTS SDK` is a metaprogramming framework for building Tezos smart contracts from Javascript. It uses the SmartPy compiler.

```js
import { DefineVar, Require, SetValue } from '@tezwell/smartts-sdk/core/command';
import { TNat } from '@tezwell/smartts-sdk/core/type';
import { Address, Nat, String } from '@tezwell/smartts-sdk/core/literal';
import { Contract, EntryPoint, GetSender } from '@tezwell/smartts-sdk/core';
import { ContractStorage, Equal, GetLocal } from '@tezwell/smartts-sdk/core/expression';
import SmartML from '@tezwell/smartts-sdk/smartml';
const { DefineVar, Require, SetValue } = require('@tezwell/smartts-sdk/core/command');
const { TNat } = require('@tezwell/smartts-sdk/core/type');
const { Address, Nat, String } = require('@tezwell/smartts-sdk/core/literal');
const { Contract, EntryPoint, GetSender } = require('@tezwell/smartts-sdk');
const { ContractStorage, Equal, GetLocal } = require('@tezwell/smartts-sdk/core/expression');
const SmartML = require('@tezwell/smartts-sdk/smartml');

const contract = new Contract()
.setStorage(Nat(0))
.addEntrypoint(
new EntryPoint('ep1')
.inputType(TNat)
.code((arg) => [
// Define a variable named "some_address"
DefineVar('some_address', Address('tz1')),
// Require sender to be equal to variable "some_address", otherwise fail with "Not Admin!"
Require(Equal(GetLocal('some_address'), GetSender()), String('Not Admin!')),
// Replace the storage value with entry point argument
SetValue(ContractStorage(), arg),
])
).toString();
new EntryPoint('ep1').inputType(TNat).code((arg) => [
// Define a variable named "some_address"
DefineVar('some_address', Address('tz1')),
// Require sender to be equal to variable "some_address", otherwise fail with "Not Admin!"
Require(Equal(GetLocal('some_address'), GetSender()), String('Not Admin!')),
// Replace the storage value with entry point argument
SetValue(ContractStorage(), arg),
]),
)
.toString();

SmartML.compileContract(contract);
```
Expand Down
39 changes: 9 additions & 30 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tezwell/smartts-sdk",
"version": "0.1.25",
"version": "0.2.0",
"description": "SmartTS SDK is a metaprogramming framework for building Tezos smart contracts from Javascript.",
"keywords": [
"Tezos",
Expand All @@ -18,34 +18,13 @@
"access": "public"
},
"exports": {
".": {
"require": "./index.cjs",
"import": "./index.mjs"
},
"./smartml": {
"require": "./smartml/index.cjs",
"import": "./smartml/index.mjs"
},
"./core/literal": {
"require": "./core/literal/index.cjs",
"import": "./core/literal/index.mjs"
},
"./core/type": {
"require": "./core/type/index.cjs",
"import": "./core/type/index.mjs"
},
"./core/expression": {
"require": "./core/expression.cjs",
"import": "./core/expression.mjs"
},
"./core/command": {
"require": "./core/command.cjs",
"import": "./core/command.mjs"
},
"./core/blockchain_operations": {
"require": "./core/blockchain_operations.cjs",
"import": "./core/blockchain_operations.mjs"
}
".": "./index.js",
"./smartml": "./smartml/index.js",
"./core/literal": "./core/literal/index.js",
"./core/type":"./core/type/index.js",
"./core/expression": "./core/expression.js",
"./core/command": "./core/command.js",
"./core/blockchain_operations": "./core/blockchain_operations.js"
},
"scripts": {
"build": "node --max_old_space_size=8192 scripts/build.js && tsc --build",
Expand All @@ -69,7 +48,6 @@
"eslint": "8.8.0",
"eslint-config-prettier": "8.3.0",
"eslint-plugin-prettier": "4.0.0",
"events": "3.3.0",
"jest": "27.4.7",
"jest-html-reporter": "3.4.2",
"jest-junit": "13.0.0",
Expand All @@ -82,6 +60,7 @@
"fs": false,
"constants": false,
"stream": false,
"readable-stream": false,
"tty": false
},
"jest-junit": {
Expand Down
1 change: 0 additions & 1 deletion polyfill/child_process.js

This file was deleted.

18 changes: 9 additions & 9 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,26 @@ generateBundle([
'./src/core/expression.ts',
'./src/core/blockchain_operations.ts',
]);
fs.copyFileSync('package.json', `${DIST_FOLDER}/package.json`);
fs.copyFileSync('src/smartml/smartML.js', `${DIST_FOLDER}/smartml/smartML.js`);

// Fix import
const content = fs.readFileSync(`${DIST_FOLDER}/index.js`, { encoding: 'utf-8' });
fs.writeFileSync(`${DIST_FOLDER}/index.js`, content.replace('./smartML.js', './smartml/smartML.js'), {
encoding: 'utf-8',
});

/**
* Produces the js bundle
*/
function generateBundle(entryPoints) {
esbuild.buildSync({
entryPoints,
bundle: true,
outdir: DIST_FOLDER,
platform: 'browser',
format: 'esm',
outExtension: { '.js': '.mjs' },
});
esbuild.buildSync({
entryPoints,
bundle: true,
outdir: DIST_FOLDER,
platform: 'browser',
format: 'cjs',
outExtension: { '.js': '.cjs' },
external: ['./smartML.js'],
});
}

Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
// Load polyfills needed by SmartML
import './smartml/polyfills';

export * as CompilerAPI from './smartml';
export * from './core';
2 changes: 1 addition & 1 deletion src/smartml/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import './polyfills';

import * as SmartML from './smartML.js';

const compileContract = (expression: string): Record<string, unknown> | string => {
export const compileContract = (expression: string): Record<string, unknown> | string => {
try {
return JSON.parse(SmartML.compile_contract(expression));
} catch (error: any) {
Expand Down
2 changes: 1 addition & 1 deletion src/smartml/smartML.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ e=c.udivmod(h);c=e.quotient;b=f.charAt(ga(e.modulus))+b}while(!Bn(c));if(a.prec>
d=a.prec-b.length;if(d>0)b=SK(d,gG)+b}return aIY(a,b)}function
jFy(a){var
c=Math.exp(a),b=c-1;return Math.abs(a)>1?b:b==0?a:a*b/Math.log(c)}function
l0(a){return a.toUtf16()}if(b4.process&&b4.process.cwd)var
l0(a){return a.toUtf16()}if(b4&&b4.process&&b4.process.cwd)var
Hv=b4.process.cwd().replace(/\\/g,fq);else
var
Hv="/static";if(Hv.slice(-1)!==fq)Hv+=fq;function
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const contract = new Contract()
)
.toString();

assert.deepEqual(SmartML.default.compileContract(contract), [
assert.deepEqual(SmartML.compileContract(contract), [
{
prim: 'storage',
args: [
Expand Down
118 changes: 118 additions & 0 deletions tests/distributables/esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import assert from 'assert';

import SmartTS from '@tezwell/smartts-sdk';
const {
Contract,
EntryPoint,
Address,
Nat,
String,
TNat,
DefineVar,
Require,
SetValue,
ContractStorage,
Equal,
GetLocal,
GetSender,
} = SmartTS;
import SmartML from '@tezwell/smartts-sdk/smartml';

const contract = new Contract()
.setStorage(Nat(0))
.addEntrypoint(
new EntryPoint('ep1').inputType(TNat).code((arg) => [
// Define a variable named "some_address"
DefineVar('some_address', Address('tz1')),
// Require sender to be equal to variable "some_address", otherwise fail with "Not Admin!"
Require(Equal(GetLocal('some_address'), GetSender()), String('Not Admin!')),
// Replace the storage value with entry point argument
SetValue(ContractStorage(), arg),
]),
)
.toString();

assert.deepEqual(SmartML.default.compileContract(contract), [
{
prim: 'storage',
args: [
{
prim: 'nat',
},
],
},
{
prim: 'parameter',
args: [
{
prim: 'nat',
annots: ['%ep1'],
},
],
},
{
prim: 'code',
args: [
[
{
prim: 'CAR',
},
{
prim: 'PUSH',
args: [
{
prim: 'address',
},
{
string: 'tz1',
},
],
},
{
prim: 'SENDER',
},
{
prim: 'COMPARE',
},
{
prim: 'EQ',
},
{
prim: 'IF',
args: [
[],
[
{
prim: 'PUSH',
args: [
{
prim: 'string',
},
{
string: 'Not Admin!',
},
],
},
{
prim: 'FAILWITH',
},
],
],
},
{
prim: 'NIL',
args: [
{
prim: 'operation',
},
],
},
{
prim: 'PAIR',
},
],
],
},
]);

console.info('[Passes] - ESM');
4 changes: 2 additions & 2 deletions tests/distributables/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"ame": "commonjs-dist",
"private": true,
"scripts": {
"test": "node ./commonjs.js"
"test-cjs": "node ./commonjs.cjs",
"test-esm": "node ./esm.mjs"
},
"dependencies": {
"@tezwell/smartts-sdk": "../../dist"
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2627,11 +2627,6 @@ esutils@^2.0.2:
resolved "https://registry.npmjs.org/esutils/-/esutils-2.0.3.tgz"
integrity sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g==

events@3.3.0:
version "3.3.0"
resolved "https://registry.yarnpkg.com/events/-/events-3.3.0.tgz#31a95ad0a924e2d2c419a813aeb2c4e878ea7400"
integrity sha512-mQw+2fkQbALzQ7V0MY0IqdnXNOeTtP4r0lN9z7AAawCXgqea7bDii20AYrIBrFd/Hx0M2Ocz6S111CaFkUcb0Q==

execa@^5.0.0:
version "5.1.1"
resolved "https://registry.npmjs.org/execa/-/execa-5.1.1.tgz"
Expand Down

0 comments on commit 54646c5

Please sign in to comment.