diff --git a/examples/__tests__/test-clean-state.ava.js b/examples/__tests__/test-clean-state.ava.js index 718eb33ef..bb717976e 100644 --- a/examples/__tests__/test-clean-state.ava.js +++ b/examples/__tests__/test-clean-state.ava.js @@ -11,9 +11,6 @@ test.beforeEach(async t => { // Deploy the clean-state contract. const cleanState = await root.devDeploy('./build/clean-state.wasm'); - // Init the contract - await cleanState.call(cleanState, 'init', {}); - // Save state for test runs, it is unique for each test t.context.worker = worker; t.context.accounts = { diff --git a/examples/src/clean-state.js b/examples/src/clean-state.js index cb3f3083f..6b6fbf9bf 100644 --- a/examples/src/clean-state.js +++ b/examples/src/clean-state.js @@ -1,23 +1,19 @@ -import { NearContract, NearBindgen, call, view, near } from 'near-sdk-js' +import { NearBindgen, call, view, near } from 'near-sdk-js' -@NearBindgen -class CleanState extends NearContract { +@NearBindgen({ requireInit: false }) +class CleanState { @call - clean({keys}) { + clean({ keys }) { keys.forEach(key => near.storageRemove(key)) } @call - put({key, value}) { + put({ key, value }) { near.storageWrite(key, value) } @view - get({key}) { + get({ key }) { return near.storageRead(key) } - - default() { - return new CleanState() - } } diff --git a/lib/build-tools/near-bindgen-exporter.js b/lib/build-tools/near-bindgen-exporter.js index ac8aec0c2..c265fc6c3 100644 --- a/lib/build-tools/near-bindgen-exporter.js +++ b/lib/build-tools/near-bindgen-exporter.js @@ -4,7 +4,7 @@ export default function () { visitor: { ClassDeclaration(path) { let classNode = path.node; - if (classNode.decorators && classNode.decorators[0].expression.name == 'NearBindgen') { + if (classNode.decorators && classNode.decorators[0].expression.callee.name == 'NearBindgen') { let classId = classNode.id; let contractMethods = {}; for (let child of classNode.body.body) { diff --git a/lib/near-bindgen.d.ts b/lib/near-bindgen.d.ts index 859abb370..c0afc67c4 100644 --- a/lib/near-bindgen.d.ts +++ b/lib/near-bindgen.d.ts @@ -1,9 +1,9 @@ export declare function initialize(target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor): void; export declare function call(target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor): void; export declare function view(target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor): void; -export declare function NearBindgen(target: T): { +export declare function NearBindgen({ requireInit }: { + requireInit: boolean; +}): {}>(target: T) => { new (...args: any[]): {}; _create(): {}; _getState(): Object; @@ -11,4 +11,5 @@ export declare function NearBindgen { + return class extends target { + static _create() { + return new target(); + } + static _getState() { + const rawState = near.storageRead("STATE"); + return rawState ? this._deserialize(rawState) : null; + } + static _saveToStorage(obj) { + near.storageWrite("STATE", this._serialize(obj)); + } + static _getArgs() { + return JSON.parse(near.input() || "{}"); + } + static _serialize(value) { + return JSON.stringify(value); + } + static _deserialize(value) { + return JSON.parse(value); + } + static _requireInit() { + return requireInit; + } + }; }; } diff --git a/src/build-tools/near-bindgen-exporter.js b/src/build-tools/near-bindgen-exporter.js index 3af11cdea..883367528 100644 --- a/src/build-tools/near-bindgen-exporter.js +++ b/src/build-tools/near-bindgen-exporter.js @@ -4,7 +4,7 @@ export default function () { visitor: { ClassDeclaration(path) { let classNode = path.node; - if (classNode.decorators && classNode.decorators[0].expression.name == 'NearBindgen') { + if (classNode.decorators && classNode.decorators[0].expression.callee.name == 'NearBindgen') { let classId = classNode.id; let contractMethods = {}; for (let child of classNode.body.body) { diff --git a/src/index.ts b/src/index.ts index 9eceedd5c..66b3ad332 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,9 @@ -import { call, view, initialize, NearBindgen } from "./near-bindgen"; +import { + call, + view, + initialize, + NearBindgen +} from "./near-bindgen"; import * as near from "./api"; import { diff --git a/src/near-bindgen.ts b/src/near-bindgen.ts index df998201c..f84242a83 100644 --- a/src/near-bindgen.ts +++ b/src/near-bindgen.ts @@ -9,33 +9,37 @@ export function call(target: Object, key: string | symbol, descriptor: TypedProp export function view(target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor): void { } - -export function NearBindgen(target: T) { - return class extends target { - static _create() { - return new target(); - } - - static _getState(): Object { - const rawState = near.storageRead("STATE"); - return rawState ? this._deserialize(rawState) : null; - } - - static _saveToStorage(obj: Object): void { - near.storageWrite("STATE", this._serialize(obj)); - } - - static _getArgs(): JSON { - return JSON.parse(near.input() || "{}"); - } - - static _serialize(value: Object): string { - return JSON.stringify(value); - } - - static _deserialize(value: string): Object { - return JSON.parse(value); +export function NearBindgen({ requireInit = false }: { requireInit: boolean }) { + return (target: T) => { + return class extends target { + static _create() { + return new target(); + } + + static _getState(): Object { + const rawState = near.storageRead("STATE"); + return rawState ? this._deserialize(rawState) : null; + } + + static _saveToStorage(obj: Object): void { + near.storageWrite("STATE", this._serialize(obj)); + } + + static _getArgs(): JSON { + return JSON.parse(near.input() || "{}"); + } + + static _serialize(value: Object): string { + return JSON.stringify(value); + } + + static _deserialize(value: string): Object { + return JSON.parse(value); + } + + static _requireInit(): boolean { + return requireInit; + } } } -} - +} \ No newline at end of file