Skip to content

Commit

Permalink
improve-tooling (#15)
Browse files Browse the repository at this point in the history
* Delete .travis.yml

* Create node.js.yml

* Adding GitHub Actions

* Update .gitignore

* Update tsconfig.json

* Delete webpack.config.js

* Update tsconfig.json

* Adding in Tsdx

* Remove bad export

* Remove unused vars

* Modernize class property declaration

* Fix Object is possibly undefined errors

* Fix last object is undefined error
  • Loading branch information
mirshko authored Jun 19, 2021
1 parent 6c4416c commit 8983c53
Show file tree
Hide file tree
Showing 10 changed files with 9,370 additions and 177 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: CI
on: [push]
jobs:
build:
name: Build, lint, and test on Node ${{ matrix.node }} and ${{ matrix.os }}

runs-on: ${{ matrix.os }}
strategy:
matrix:
node: ['10.x', '12.x', '14.x']
os: [ubuntu-latest, windows-latest, macOS-latest]

steps:
- name: Checkout repo
uses: actions/checkout@v2

- name: Use Node ${{ matrix.node }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node }}

- name: Install deps and build (with cache)
uses: bahmutov/npm-install@v1

- name: Lint
run: yarn lint

- name: Test
run: yarn test --ci --coverage --maxWorkers=2

- name: Build
run: yarn build
12 changes: 12 additions & 0 deletions .github/workflows/size.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: size
on: [pull_request]
jobs:
size:
runs-on: ubuntu-latest
env:
CI_JOB_NUMBER: 1
steps:
- uses: actions/checkout@v1
- uses: andresz1/size-limit-action@v1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
104 changes: 2 additions & 102 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,104 +1,4 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
.DS_Store
node_modules
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port
20 changes: 0 additions & 20 deletions .travis.yml

This file was deleted.

46 changes: 43 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,23 @@
"version": "0.2.0",
"description": "The weiroll planner in JS",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"typings": "dist/index.d.ts",
"files": [
"dist",
"src"
],
"engines": {
"node": ">=10"
},
"scripts": {
"test": "env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha -r ts-node/register 'tests/**/*.ts'",
"build": "tsc"
"start": "tsdx watch",
"build": "tsdx build",
"tsdx-test": "tsdx test",
"lint": "tsdx lint",
"prepare": "tsdx build",
"size": "size-limit",
"analyze": "size-limit --why"
},
"repository": {
"type": "git",
Expand All @@ -17,16 +30,43 @@
"bugs": {
"url": "https://github.com/weiroll/weiroll.js/issues"
},
"peerDependencies": {},
"husky": {
"hooks": {
"pre-commit": "tsdx lint"
}
},
"prettier": {
"printWidth": 80,
"semi": true,
"singleQuote": true,
"trailingComma": "es5"
},
"size-limit": [
{
"path": "dist/weiroll.cjs.production.min.js",
"limit": "10 KB"
},
{
"path": "dist/weiroll.esm.js",
"limit": "10 KB"
}
],
"homepage": "https://github.com/weiroll/weiroll.js#readme",
"devDependencies": {
"@size-limit/preset-small-lib": "^4.12.0",
"@types/chai": "^4.2.18",
"@types/mocha": "^8.2.2",
"@types/node": "^15.12.2",
"chai": "^4.3.4",
"husky": "^6.0.0",
"mocha": "^9.0.0",
"size-limit": "^4.12.0",
"ts-loader": "^9.2.3",
"ts-node": "^10.0.0",
"typescript": "^4.3.2"
"tsdx": "^0.14.1",
"tslib": "^2.3.0",
"typescript": "^4.3.3"
},
"dependencies": {
"ethers": "^5.3.1",
Expand Down
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"use strict";

export {
isReturnValue,
Contract,
ContractFunction,
FunctionCall,
Expand Down
43 changes: 23 additions & 20 deletions src/planner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { defineReadOnly, getStatic } from '@ethersproject/properties';
import { hexConcat, hexDataSlice, hexlify } from '@ethersproject/bytes';
import { Heap } from 'heap-js';

const maxInputs = 7;

export interface Value {
readonly param: ParamType;
}
Expand All @@ -21,8 +19,8 @@ export class LiteralValue implements Value {
readonly value: string;

constructor(param: ParamType, value: string) {
defineReadOnly(this, "param", param);
defineReadOnly(this, "value", value);
this.param = param;
this.value = value;
}
}

Expand All @@ -32,17 +30,17 @@ export class ReturnValue implements Value {
readonly commandIndex: number; // Index of the command in the array of planned commands

constructor(param: ParamType, planner: Planner, commandIndex: number) {
defineReadOnly(this, "param", param);
defineReadOnly(this, "planner", planner);
defineReadOnly(this, "commandIndex", commandIndex);
this.param = param;
this.planner = planner;
this.commandIndex = commandIndex;
}
}

export class StateValue implements Value {
readonly param: ParamType;

constructor() {
defineReadOnly(this, "param", ParamType.from('bytes[]'));
this.param = ParamType.from('bytes[]');
}
}

Expand All @@ -54,10 +52,9 @@ export interface FunctionCall {

export type ContractFunction = (...args: Array<any>) => FunctionCall;

const paramTypeBytes = new RegExp(/^bytes([0-9]*)$/);
const paramTypeNumber = new RegExp(/^(u?int)([0-9]*)$/);
export function isDynamicType(param?: ParamType): boolean {
if (typeof param === "undefined") return false

export function isDynamicType(param: ParamType): boolean {
return ["string", "bytes", "array", "tuple"].includes(param.baseType);
}

Expand Down Expand Up @@ -95,9 +92,9 @@ class BaseContract {
readonly functions: { [ name: string ]: ContractFunction };

constructor(address: string, contractInterface: ContractInterface) {
defineReadOnly(this, "interface", getStatic<(contractInterface: ContractInterface) => Interface>(new.target, "getInterface")(contractInterface));
defineReadOnly(this, "address", address);
defineReadOnly(this, "functions", {});
this.interface = getStatic<(contractInterface: ContractInterface) => Interface>(new.target, "getInterface")(contractInterface);
this.address = address;
this.functions = {};

const uniqueNames: { [ name: string ]: Array<string> } = { };
const uniqueSignatures: { [ signature: string ]: boolean } = { };
Expand Down Expand Up @@ -174,7 +171,7 @@ export class Planner {
calls: {call: FunctionCall, replacesState: boolean}[];

constructor() {
defineReadOnly(this, "state", new StateValue());
this.state = new StateValue();
this.calls = [];
}

Expand All @@ -190,7 +187,7 @@ export class Planner {
const commandIndex = this.calls.length;
this.calls.push({call, replacesState: false});

if(call.fragment.outputs.length != 1) {
if(call.fragment.outputs?.length != 1) {
return null;
}
return new ReturnValue(call.fragment.outputs[0], this, commandIndex);
Expand All @@ -205,7 +202,7 @@ export class Planner {
}
}

if(call.fragment.outputs.length != 1 || call.fragment.outputs[0].type != 'bytes[]') {
if(call.fragment.outputs?.length != 1 || call.fragment.outputs[0].type != 'bytes[]') {
throw new Error("Function replacing state must return a bytes[]");
}

Expand Down Expand Up @@ -283,9 +280,15 @@ export class Planner {
}
ret = state.length;

const topNode = nextDeadSlot.peek();

// Is there a spare state slot?
if(nextDeadSlot.peek().dies <= i) {
ret = nextDeadSlot.pop().slot;
if(typeof topNode !== "undefined" && topNode.dies <= i) {
const extractedTopNode = nextDeadSlot.pop();

if (extractedTopNode) {
ret = extractedTopNode?.slot;
}
}

// Store the slot mapping
Expand All @@ -298,7 +301,7 @@ export class Planner {
state.push('0x');
}

if(isDynamicType(call.fragment.outputs[0])) {
if(isDynamicType(call.fragment.outputs?.[0])) {
ret |= 0x80;
}
} else if(replacesState) {
Expand Down
Loading

0 comments on commit 8983c53

Please sign in to comment.