From ef52312ce79823ffbb53a60fdb1123bf29576d0e Mon Sep 17 00:00:00 2001 From: Dmitry Osipov Date: Tue, 19 Jul 2022 13:11:32 +0300 Subject: [PATCH] Generate simple enum (#845) --- api/CHANGELOG.md | 11 + api/jest.config.js | 22 -- api/jest.config.ts | 14 + api/package-lock.json | 241 +++++++++++++++++- api/package.json | 5 +- api/src/create-type/payload-type-structure.ts | 12 +- api/test/CreateType.test.ts | 4 +- api/test/CreateTypeStructure.test.ts | 95 +++++-- 8 files changed, 345 insertions(+), 59 deletions(-) delete mode 100644 api/jest.config.js create mode 100644 api/jest.config.ts diff --git a/api/CHANGELOG.md b/api/CHANGELOG.md index 2662ff6e03..40b1e057aa 100644 --- a/api/CHANGELOG.md +++ b/api/CHANGELOG.md @@ -1,3 +1,14 @@ +## 0.23.6 + +_07/19/2022_ + +https://github.com/gear-tech/gear-js/pull/845 + +### Changes + +- Adjust simple enum generation + +--- ## 0.23.5 _07/18/2022_ diff --git a/api/jest.config.js b/api/jest.config.js deleted file mode 100644 index dc7a7bf885..0000000000 --- a/api/jest.config.js +++ /dev/null @@ -1,22 +0,0 @@ -/* - * For a detailed explanation regarding each configuration property, visit: - * https://jestjs.io/docs/configuration - */ - -export default { - // Automatically clear mock calls, instances and results before every test - clearMocks: true, - - // Indicates which provider should be used to instrument code for coverage - coverageProvider: 'v8', - - // The test environment that will be used for testing - testEnvironment: 'node', - - // An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation - transformIgnorePatterns: ['node_modules/(?!@polkadot)/'], - verbose: true, - testSequencer: './test/testSequencer.js', - preset: 'ts-jest/presets/js-with-babel', - testTimeout: 15000, -}; diff --git a/api/jest.config.ts b/api/jest.config.ts new file mode 100644 index 0000000000..e8a62a2432 --- /dev/null +++ b/api/jest.config.ts @@ -0,0 +1,14 @@ +import type { Config } from '@jest/types'; + +const config: Config.InitialOptions = { + clearMocks: true, + coverageProvider: 'v8', + testEnvironment: 'node', + transformIgnorePatterns: ['node_modules/(?!@polkadot)/'], + verbose: true, + testSequencer: './test/testSequencer.js', + preset: 'ts-jest/presets/js-with-babel', + testTimeout: 15000, +}; + +export default config; \ No newline at end of file diff --git a/api/package-lock.json b/api/package-lock.json index 94dd1cd196..8fcfe8e6ea 100644 --- a/api/package-lock.json +++ b/api/package-lock.json @@ -1,12 +1,12 @@ { "name": "@gear-js/api", - "version": "0.23.5", + "version": "0.23.6", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@gear-js/api", - "version": "0.23.5", + "version": "0.23.6", "license": "GPL-3.0", "dependencies": { "@polkadot/api": "^8.11.3", @@ -32,6 +32,7 @@ "rollup-plugin-peer-deps-external": "2.2.4", "rollup-plugin-typescript2": "0.31.2", "ts-jest": "28.0.4", + "ts-node": "^10.9.1", "typescript": "4.7.3" } }, @@ -1705,6 +1706,28 @@ "dev": true, "license": "MIT" }, + "node_modules/@cspotcode/source-map-support": { + "version": "0.8.1", + "resolved": "https://registry.npmjs.org/@cspotcode/source-map-support/-/source-map-support-0.8.1.tgz", + "integrity": "sha512-IchNf6dN4tHoMFIn/7OE8LWZ19Y6q/67Bmf6vnGREv8RSbBVb9LPJxEcnwrcwX6ixSvaiGoomAUvu4YSxXrVgw==", + "dev": true, + "dependencies": { + "@jridgewell/trace-mapping": "0.3.9" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/@cspotcode/source-map-support/node_modules/@jridgewell/trace-mapping": { + "version": "0.3.9", + "resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.9.tgz", + "integrity": "sha512-3Belt6tdc8bPgAtbcmdtNJlirVoTmEb5e2gC94PnkwEW9jI6CAHUeoG85tjWP5WquqfavoMtMwiG4P926ZKKuQ==", + "dev": true, + "dependencies": { + "@jridgewell/resolve-uri": "^3.0.3", + "@jridgewell/sourcemap-codec": "^1.4.10" + } + }, "node_modules/@eslint/eslintrc": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/@eslint/eslintrc/-/eslintrc-1.3.0.tgz", @@ -2782,6 +2805,30 @@ "resolved": "https://registry.npmjs.org/@substrate/ss58-registry/-/ss58-registry-1.23.0.tgz", "integrity": "sha512-LuQje7n48GXSsp1aGI6UEmNVtlh7OzQ6CN1Hd9VGUrshADwMB0lRZ5bxnffmqDR4vVugI7h0NN0AONhIW1eHGg==" }, + "node_modules/@tsconfig/node10": { + "version": "1.0.9", + "resolved": "https://registry.npmjs.org/@tsconfig/node10/-/node10-1.0.9.tgz", + "integrity": "sha512-jNsYVVxU8v5g43Erja32laIDHXeoNvFEpX33OK4d6hljo3jDhCBDhx5dhCCTMWUojscpAagGiRkBKxpdl9fxqA==", + "dev": true + }, + "node_modules/@tsconfig/node12": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/@tsconfig/node12/-/node12-1.0.11.tgz", + "integrity": "sha512-cqefuRsh12pWyGsIoBKJA9luFu3mRxCA+ORZvA4ktLSzIuCUtWVxGIuXigEwO5/ywWFMZ2QEGKWvkZG1zDMTag==", + "dev": true + }, + "node_modules/@tsconfig/node14": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/@tsconfig/node14/-/node14-1.0.3.tgz", + "integrity": "sha512-ysT8mhdixWK6Hw3i1V2AeRqZ5WfXg1G43mqoYlM2nc6388Fq5jcXyr5mRsqViLx/GJYdoL0bfXD8nmF+Zn/Iow==", + "dev": true + }, + "node_modules/@tsconfig/node16": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/@tsconfig/node16/-/node16-1.0.3.tgz", + "integrity": "sha512-yOlFc+7UtL/89t2ZhjPvvB/DeAr3r+Dq58IgzsFkOAvVC6NMJXmCGjbptdXdR9qsX7pKcTL+s87FtYREi2dEEQ==", + "dev": true + }, "node_modules/@types/babel__core": { "version": "7.1.19", "dev": true, @@ -3239,6 +3286,15 @@ "acorn": "^6.0.0 || ^7.0.0 || ^8.0.0" } }, + "node_modules/acorn-walk": { + "version": "8.2.0", + "resolved": "https://registry.npmjs.org/acorn-walk/-/acorn-walk-8.2.0.tgz", + "integrity": "sha512-k+iyHEuPgSw6SbuDpGQM+06HQUa04DZ3o+F6CSzXMvvI5KMvnaEqXe+YVe555R9nn6GPt404fos4wcgpw12SDA==", + "dev": true, + "engines": { + "node": ">=0.4.0" + } + }, "node_modules/ajv": { "version": "6.12.6", "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz", @@ -3357,6 +3413,12 @@ "node": ">=0.10.0" } }, + "node_modules/arg": { + "version": "4.1.3", + "resolved": "https://registry.npmjs.org/arg/-/arg-4.1.3.tgz", + "integrity": "sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==", + "dev": true + }, "node_modules/argparse": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz", @@ -4346,6 +4408,12 @@ "license": "MIT", "optional": true }, + "node_modules/create-require": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/create-require/-/create-require-1.1.1.tgz", + "integrity": "sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==", + "dev": true + }, "node_modules/cross-spawn": { "version": "7.0.3", "dev": true, @@ -4528,6 +4596,15 @@ "node": ">=8" } }, + "node_modules/diff": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz", + "integrity": "sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==", + "dev": true, + "engines": { + "node": ">=0.3.1" + } + }, "node_modules/diff-sequences": { "version": "27.5.1", "dev": true, @@ -8860,6 +8937,49 @@ "node": ">=10" } }, + "node_modules/ts-node": { + "version": "10.9.1", + "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-10.9.1.tgz", + "integrity": "sha512-NtVysVPkxxrwFGUUxGYhfux8k78pQB3JqYBXlLRZgdGUqTO5wU/UyHop5p70iEbGhB7q5KmiZiU0Y3KlJrScEw==", + "dev": true, + "dependencies": { + "@cspotcode/source-map-support": "^0.8.0", + "@tsconfig/node10": "^1.0.7", + "@tsconfig/node12": "^1.0.7", + "@tsconfig/node14": "^1.0.0", + "@tsconfig/node16": "^1.0.2", + "acorn": "^8.4.1", + "acorn-walk": "^8.1.1", + "arg": "^4.1.0", + "create-require": "^1.1.0", + "diff": "^4.0.1", + "make-error": "^1.1.1", + "v8-compile-cache-lib": "^3.0.1", + "yn": "3.1.1" + }, + "bin": { + "ts-node": "dist/bin.js", + "ts-node-cwd": "dist/bin-cwd.js", + "ts-node-esm": "dist/bin-esm.js", + "ts-node-script": "dist/bin-script.js", + "ts-node-transpile-only": "dist/bin-transpile.js", + "ts-script": "dist/bin-script-deprecated.js" + }, + "peerDependencies": { + "@swc/core": ">=1.2.50", + "@swc/wasm": ">=1.2.50", + "@types/node": "*", + "typescript": ">=2.7" + }, + "peerDependenciesMeta": { + "@swc/core": { + "optional": true + }, + "@swc/wasm": { + "optional": true + } + } + }, "node_modules/tslib": { "version": "2.4.0", "license": "0BSD" @@ -9149,6 +9269,12 @@ "integrity": "sha512-l8lCEmLcLYZh4nbunNZvQCJc5pv7+RCwa8q/LdUx8u7lsWvPDKmpodJAJNwkAhJC//dFY48KuIEmjtd4RViDrA==", "dev": true }, + "node_modules/v8-compile-cache-lib": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz", + "integrity": "sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg==", + "dev": true + }, "node_modules/v8-to-istanbul": { "version": "9.0.1", "dev": true, @@ -9320,6 +9446,15 @@ "node": ">=12" } }, + "node_modules/yn": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/yn/-/yn-3.1.1.tgz", + "integrity": "sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==", + "dev": true, + "engines": { + "node": ">=6" + } + }, "node_modules/yocto-queue": { "version": "0.1.0", "dev": true, @@ -10355,6 +10490,27 @@ "version": "0.2.3", "dev": true }, + "@cspotcode/source-map-support": { + "version": "0.8.1", + "resolved": "https://registry.npmjs.org/@cspotcode/source-map-support/-/source-map-support-0.8.1.tgz", + "integrity": "sha512-IchNf6dN4tHoMFIn/7OE8LWZ19Y6q/67Bmf6vnGREv8RSbBVb9LPJxEcnwrcwX6ixSvaiGoomAUvu4YSxXrVgw==", + "dev": true, + "requires": { + "@jridgewell/trace-mapping": "0.3.9" + }, + "dependencies": { + "@jridgewell/trace-mapping": { + "version": "0.3.9", + "resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.9.tgz", + "integrity": "sha512-3Belt6tdc8bPgAtbcmdtNJlirVoTmEb5e2gC94PnkwEW9jI6CAHUeoG85tjWP5WquqfavoMtMwiG4P926ZKKuQ==", + "dev": true, + "requires": { + "@jridgewell/resolve-uri": "^3.0.3", + "@jridgewell/sourcemap-codec": "^1.4.10" + } + } + } + }, "@eslint/eslintrc": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/@eslint/eslintrc/-/eslintrc-1.3.0.tgz", @@ -11143,6 +11299,30 @@ "resolved": "https://registry.npmjs.org/@substrate/ss58-registry/-/ss58-registry-1.23.0.tgz", "integrity": "sha512-LuQje7n48GXSsp1aGI6UEmNVtlh7OzQ6CN1Hd9VGUrshADwMB0lRZ5bxnffmqDR4vVugI7h0NN0AONhIW1eHGg==" }, + "@tsconfig/node10": { + "version": "1.0.9", + "resolved": "https://registry.npmjs.org/@tsconfig/node10/-/node10-1.0.9.tgz", + "integrity": "sha512-jNsYVVxU8v5g43Erja32laIDHXeoNvFEpX33OK4d6hljo3jDhCBDhx5dhCCTMWUojscpAagGiRkBKxpdl9fxqA==", + "dev": true + }, + "@tsconfig/node12": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/@tsconfig/node12/-/node12-1.0.11.tgz", + "integrity": "sha512-cqefuRsh12pWyGsIoBKJA9luFu3mRxCA+ORZvA4ktLSzIuCUtWVxGIuXigEwO5/ywWFMZ2QEGKWvkZG1zDMTag==", + "dev": true + }, + "@tsconfig/node14": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/@tsconfig/node14/-/node14-1.0.3.tgz", + "integrity": "sha512-ysT8mhdixWK6Hw3i1V2AeRqZ5WfXg1G43mqoYlM2nc6388Fq5jcXyr5mRsqViLx/GJYdoL0bfXD8nmF+Zn/Iow==", + "dev": true + }, + "@tsconfig/node16": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/@tsconfig/node16/-/node16-1.0.3.tgz", + "integrity": "sha512-yOlFc+7UtL/89t2ZhjPvvB/DeAr3r+Dq58IgzsFkOAvVC6NMJXmCGjbptdXdR9qsX7pKcTL+s87FtYREi2dEEQ==", + "dev": true + }, "@types/babel__core": { "version": "7.1.19", "dev": true, @@ -11463,6 +11643,12 @@ "dev": true, "requires": {} }, + "acorn-walk": { + "version": "8.2.0", + "resolved": "https://registry.npmjs.org/acorn-walk/-/acorn-walk-8.2.0.tgz", + "integrity": "sha512-k+iyHEuPgSw6SbuDpGQM+06HQUa04DZ3o+F6CSzXMvvI5KMvnaEqXe+YVe555R9nn6GPt404fos4wcgpw12SDA==", + "dev": true + }, "ajv": { "version": "6.12.6", "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz", @@ -11543,6 +11729,12 @@ } } }, + "arg": { + "version": "4.1.3", + "resolved": "https://registry.npmjs.org/arg/-/arg-4.1.3.tgz", + "integrity": "sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==", + "dev": true + }, "argparse": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz", @@ -12236,6 +12428,12 @@ "dev": true, "optional": true }, + "create-require": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/create-require/-/create-require-1.1.1.tgz", + "integrity": "sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==", + "dev": true + }, "cross-spawn": { "version": "7.0.3", "dev": true, @@ -12359,6 +12557,12 @@ "version": "3.1.0", "dev": true }, + "diff": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz", + "integrity": "sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==", + "dev": true + }, "diff-sequences": { "version": "27.5.1", "dev": true @@ -15330,6 +15534,27 @@ } } }, + "ts-node": { + "version": "10.9.1", + "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-10.9.1.tgz", + "integrity": "sha512-NtVysVPkxxrwFGUUxGYhfux8k78pQB3JqYBXlLRZgdGUqTO5wU/UyHop5p70iEbGhB7q5KmiZiU0Y3KlJrScEw==", + "dev": true, + "requires": { + "@cspotcode/source-map-support": "^0.8.0", + "@tsconfig/node10": "^1.0.7", + "@tsconfig/node12": "^1.0.7", + "@tsconfig/node14": "^1.0.0", + "@tsconfig/node16": "^1.0.2", + "acorn": "^8.4.1", + "acorn-walk": "^8.1.1", + "arg": "^4.1.0", + "create-require": "^1.1.0", + "diff": "^4.0.1", + "make-error": "^1.1.1", + "v8-compile-cache-lib": "^3.0.1", + "yn": "3.1.1" + } + }, "tslib": { "version": "2.4.0" }, @@ -15522,6 +15747,12 @@ "integrity": "sha512-l8lCEmLcLYZh4nbunNZvQCJc5pv7+RCwa8q/LdUx8u7lsWvPDKmpodJAJNwkAhJC//dFY48KuIEmjtd4RViDrA==", "dev": true }, + "v8-compile-cache-lib": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz", + "integrity": "sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg==", + "dev": true + }, "v8-to-istanbul": { "version": "9.0.1", "dev": true, @@ -15646,6 +15877,12 @@ "version": "21.0.1", "dev": true }, + "yn": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/yn/-/yn-3.1.1.tgz", + "integrity": "sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==", + "dev": true + }, "yocto-queue": { "version": "0.1.0", "dev": true diff --git a/api/package.json b/api/package.json index 97e2aa561a..2bb2b7efe8 100644 --- a/api/package.json +++ b/api/package.json @@ -1,6 +1,6 @@ { "name": "@gear-js/api", - "version": "0.23.5", + "version": "0.23.6", "description": "A JavaScript library that provides functionality to connect GEAR Component APIs.", "main": "cjs/index.js", "module": "index.js", @@ -64,6 +64,7 @@ "rollup-plugin-peer-deps-external": "2.2.4", "rollup-plugin-typescript2": "0.31.2", "ts-jest": "28.0.4", + "ts-node": "^10.9.1", "typescript": "4.7.3" } -} \ No newline at end of file +} diff --git a/api/src/create-type/payload-type-structure.ts b/api/src/create-type/payload-type-structure.ts index cbbd9537f2..33bc7077ff 100644 --- a/api/src/create-type/payload-type-structure.ts +++ b/api/src/create-type/payload-type-structure.ts @@ -83,9 +83,15 @@ function getIfEnum(typeName: string, types: AnyJson, raw: boolean): TypeTree | { if (Object.keys(types[typeName]).length === 1 && Object.keys(types[typeName]).includes('_enum')) { const type = types[typeName]; const value: any = {}; - Object.keys(type['_enum']).forEach((field) => { - value[field] = createPayloadTypeStructure(type['_enum'][field], types, raw); - }); + if (Array.isArray(type['_enum'])) { + type['_enum'].forEach((field) => { + value[field] = createPayloadTypeStructure('Null', types, raw); + }); + } else { + Object.keys(type['_enum']).forEach((field) => { + value[field] = createPayloadTypeStructure(type['_enum'][field], types, raw); + }); + } return raw ? { _enum: value } : generate.Enum(typeName, value); } } diff --git a/api/test/CreateType.test.ts b/api/test/CreateType.test.ts index 9b4fddac26..192fe98c0e 100644 --- a/api/test/CreateType.test.ts +++ b/api/test/CreateType.test.ts @@ -1,4 +1,4 @@ -import { CreateType, getWasmMetadata, decodeHexTypes } from '../src'; +import { CreateType, getWasmMetadata, decodeHexTypes, Hex } from '../src'; import yaml from 'js-yaml'; import fs from 'fs'; import { join } from 'path'; @@ -15,7 +15,7 @@ describe('Create type test', () => { for (const type in testFile.types) { expect(meta[type]).toBe(testFile.types[type]); } - expect(decodeHexTypes(meta.types)).toEqual(testFile.displayed_types); + expect(decodeHexTypes(meta.types as Hex)).toEqual(testFile.displayed_types); const createType = new CreateType(); const encode = testFile.payloads.encode; for (const payloadType in encode) { diff --git a/api/test/CreateTypeStructure.test.ts b/api/test/CreateTypeStructure.test.ts index c4614b3bd1..04890e31a4 100644 --- a/api/test/CreateTypeStructure.test.ts +++ b/api/test/CreateTypeStructure.test.ts @@ -1,37 +1,34 @@ -import { createPayloadTypeStructure, CreateType, decodeHexTypes, getWasmMetadata, Metadata } from '../src'; +import { createPayloadTypeStructure, CreateType, decodeHexTypes, getWasmMetadata, Hex, Metadata } from '../src'; import { TEST_WASM_DIR } from './config'; import { readFileSync } from 'fs'; import { join } from 'path'; describe('Create type structure test', () => { - let types: any; - beforeAll(() => { - types = { - Action: { - _enum: { - AVariant: 'AStruct', - BVar: 'Option', - CVariant: 'BTreeMap', - }, + const types = { + Action: { + _enum: { + AVariant: 'AStruct', + BVar: 'Option', + CVariant: 'BTreeMap', }, - AStruct: { id: 'Bytes', online: 'bool' }, - CustomStructU8: { field: 'u8' }, - CustomStructOption: { field: 'Option<(Option,u128,[u8;3])>' }, - FungibleTokenAction: { - _enum: { - Mint: 'u128', - Burn: 'u128', - Transfer: '{"from":"ActorId","to":"ActorId","amount":"u128"}', - Approve: '{"to":"ActorId","amount":"u128"}', - TotalSupply: 'Null', - BalanceOf: 'ActorId', - }, + }, + AStruct: { id: 'Bytes', online: 'bool' }, + CustomStructU8: { field: 'u8' }, + CustomStructOption: { field: 'Option<(Option,u128,[u8;3])>' }, + FungibleTokenAction: { + _enum: { + Mint: 'u128', + Burn: 'u128', + Transfer: '{"from":"ActorId","to":"ActorId","amount":"u128"}', + Approve: '{"to":"ActorId","amount":"u128"}', + TotalSupply: 'Null', + BalanceOf: 'ActorId', }, - GenericInStruct: - '{"nftContractId":"ActorId","ftContractId":"Option","tokenId":"U256","price":"Option"}', - TupleInStruct: '{"nftContractId":"ActorId","someTuple":"(u128, String)"}', - }; - }); + }, + GenericInStruct: + '{"nftContractId":"ActorId","ftContractId":"Option","tokenId":"U256","price":"Option"}', + TupleInStruct: '{"nftContractId":"ActorId","someTuple":"(u128, String)"}', + }; test('Enum', () => { expect(createPayloadTypeStructure('Action', types)).toEqual({ type: 'Enum', @@ -427,7 +424,7 @@ describe('BTreeSet test', () => { }); test('decodeHexTypes will not fail', () => { - expect(decodeHexTypes(metadata.types)).toEqual({ + expect(decodeHexTypes(metadata.types as Hex)).toEqual({ ActorId: '[u8;32]', TestBTreeSet: { first: 'BTreeSet', second: 'BTreeSet' }, }); @@ -452,3 +449,45 @@ describe('BTreeSet test', () => { }); }); }); + + +describe('Simple enum test', () => { + const types = '0x4000082861756374696f6e5f696f28496e6974436f6e6669670000000004082861756374696f6e5f696f18416374696f6e00010c0c427579000000184372656174650400080130437265617465436f6e66696700010024466f72636553746f700002000008082861756374696f6e5f696f30437265617465436f6e66696700001801546e66745f636f6e74726163745f6163746f725f69640c011c4163746f72496400012c746f6b656e5f6f776e65720c011c4163746f724964000120746f6b656e5f6964180110553235360001387374617274696e675f707269636524011075313238000134646973636f756e745f72617465240110753132380001206475726174696f6e2801204475726174696f6e00000c10106773746418636f6d6d6f6e287072696d6974697665731c4163746f724964000004001001205b75383b2033325d00001000000320000000140014000005030018083c7072696d69746976655f74797065731055323536000004001c01205b7536343b20345d00001c00000304000000200020000005060024000005070028082861756374696f6e5f696f204475726174696f6e00000c01106461797320010c753634000114686f75727320010c75363400011c6d696e7574657320010c75363400002c082861756374696f6e5f696f144576656e740001083841756374696f6e537461727465640c012c746f6b656e5f6f776e65720c011c4163746f724964000114707269636524011075313238000120746f6b656e5f6964180110553235360000003441756374696f6e53746f70656408012c746f6b656e5f6f776e65720c011c4163746f724964000120746f6b656e5f69641801105532353600010000300c3464757463685f61756374696f6e14737461746514537461746500010c28546f6b656e507269636500000020497341637469766500010010496e666f00020000340c3464757463685f61756374696f6e1473746174652853746174655265706c7900010c28546f6b656e50726963650400240110753132380000002049734163746976650400380110626f6f6c00010010496e666f04003c012c41756374696f6e496e666f000200003800000500003c0c3464757463685f61756374696f6e1473746174652c41756374696f6e496e666f00001001546e66745f636f6e74726163745f6163746f725f69640c011c4163746f724964000120746f6b656e5f69641801105532353600012c746f6b656e5f6f776e65720c011c4163746f7249640001387374617274696e675f7072696365240110753132380000'; + test('State (from types of dutch auction)', () => { + const decodedTypes = decodeHexTypes(types); + const structure = createPayloadTypeStructure('State', decodedTypes); + expect(structure).toEqual( + { + 'type': 'Enum', + 'name': 'State', + 'value': { + 'TokenPrice': { + 'type': 'Primitive', + 'name': 'Null', + 'value': 'Null' + }, + 'IsActive': { + 'type': 'Primitive', + 'name': 'Null', + 'value': 'Null' + }, + 'Info': { + 'type': 'Primitive', + 'name': 'Null', + 'value': 'Null' + } + } + } + ); + expect(CreateType.create('State', { 'TokenPrice': null }, { types }).toHex()).toBe('0x00'); + }); + test('Raw State (from types of dutch auction)', () => { + const decodedTypes = decodeHexTypes(types); + const structure = createPayloadTypeStructure('State', decodedTypes, true); + expect(structure).toEqual( + { + '_enum': { 'TokenPrice': 'Null', 'IsActive': 'Null', 'Info': 'Null' } + } + ); + }); +}); \ No newline at end of file