diff --git a/pkg/timestamp/.npmignore b/pkg/timestamp/.npmignore new file mode 120000 index 00000000..b4359f69 --- /dev/null +++ b/pkg/timestamp/.npmignore @@ -0,0 +1 @@ +../../.npmignore \ No newline at end of file diff --git a/pkg/timestamp/package.json b/pkg/timestamp/package.json new file mode 100644 index 00000000..b38c10d0 --- /dev/null +++ b/pkg/timestamp/package.json @@ -0,0 +1,30 @@ +{ + "name": "@slangroom/timestamp", + "version": "1.19.0", + "dependencies": { + "@slangroom/core": "workspace:*", + "@slangroom/shared": "workspace:*" + }, + "repository": "https://github.com/dyne/slangroom", + "license": "AGPL-3.0-only", + "type": "module", + "main": "./build/esm/src/index.js", + "types": "./build/esm/src/index.d.ts", + "exports": { + ".": { + "import": { + "types": "./build/esm/src/index.d.ts", + "default": "./build/esm/src/index.js" + } + }, + "./*": { + "import": { + "types": "./build/esm/src/*.d.ts", + "default": "./build/esm/src/*.js" + } + } + }, + "publishConfig": { + "access": "public" + } +} diff --git a/pkg/timestamp/src/index.ts b/pkg/timestamp/src/index.ts new file mode 100644 index 00000000..6d6b58e2 --- /dev/null +++ b/pkg/timestamp/src/index.ts @@ -0,0 +1 @@ +export * from '@slangroom/timestamp/plugin'; diff --git a/pkg/timestamp/src/plugin.ts b/pkg/timestamp/src/plugin.ts new file mode 100644 index 00000000..b966838f --- /dev/null +++ b/pkg/timestamp/src/plugin.ts @@ -0,0 +1,15 @@ +import { Plugin } from '@slangroom/core'; + +const p = new Plugin(); + +export const milliseconds = p.new('fetch the local timestamp in milliseconds', async (ctx) => { + const t = Date.now(); + return ctx.pass(t.toString()); +}); + +export const seconds = p.new('fetch the local timestamp in seconds', async (ctx) => { + const t = Math.floor(Date.now() / 1000); + return ctx.pass(t.toString()); +}); + +export const timestamp = p; diff --git a/pkg/timestamp/test/e2e.ts b/pkg/timestamp/test/e2e.ts new file mode 100644 index 00000000..11cb6c46 --- /dev/null +++ b/pkg/timestamp/test/e2e.ts @@ -0,0 +1,29 @@ +import test from 'ava'; +import { Slangroom } from '@slangroom/core'; +import { timestamp } from '@slangroom/timestamp'; + +test('fetch timestamp in seconds', async (t) => { + const contract = `Rule unknown ignore +Given I fetch the local timestamp in seconds and output into 'output' +Given I have a 'time' named 'output' +Then print data +` + const sl = new Slangroom(timestamp); + const res = await sl.execute(contract); + const ts = Date.now() / 1000; + t.truthy(res.result['output']); + t.true(Math.abs(Number(res.result['output']) - ts) < 100); +}); + +test('fetch timestamp in milliseconds', async (t) => { + const contract = `Rule unknown ignore +Given I fetch the local timestamp in milliseconds and output into 'output' +Given I have a 'string' named 'output' +Then print data +` + const sl = new Slangroom(timestamp); + const res = await sl.execute(contract); + const ts = Date.now(); + t.truthy(res.result['output']); + t.true(Math.abs(Number(res.result['output']) - ts) < 100000); +}); diff --git a/pkg/timestamp/tsconfig.json b/pkg/timestamp/tsconfig.json new file mode 120000 index 00000000..fd0e4743 --- /dev/null +++ b/pkg/timestamp/tsconfig.json @@ -0,0 +1 @@ +../../tsconfig.json \ No newline at end of file diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 705ba41a..47e14a50 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -263,6 +263,15 @@ importers: specifier: ^8.0.1 version: 8.0.1 + pkg/timestamp: + dependencies: + '@slangroom/core': + specifier: workspace:* + version: link:../core + '@slangroom/shared': + specifier: workspace:* + version: link:../shared + pkg/wallet: dependencies: '@meeco/sd-jwt':