Skip to content

Commit

Permalink
NearBindgen refactored to factory decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
volovyks committed Aug 30, 2022
1 parent d6bb14f commit b37222e
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 68 deletions.
3 changes: 0 additions & 3 deletions examples/__tests__/test-clean-state.ava.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
16 changes: 6 additions & 10 deletions examples/src/clean-state.js
Original file line number Diff line number Diff line change
@@ -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()
}
}
2 changes: 1 addition & 1 deletion lib/build-tools/near-bindgen-exporter.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions lib/near-bindgen.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 26 additions & 21 deletions lib/near-bindgen.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/build-tools/near-bindgen-exporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
60 changes: 32 additions & 28 deletions src/near-bindgen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,37 @@ export function call(target: Object, key: string | symbol, descriptor: TypedProp
export function view(target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor<Function>): void {
}


export function NearBindgen<T extends { new(...args: any[]): {} }>(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 <T extends { new(...args: any[]): {} }>(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;
}
}
}
}

}

0 comments on commit b37222e

Please sign in to comment.