Skip to content

Commit

Permalink
NearContract base class deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
volovyks committed Aug 29, 2022
1 parent 422b34b commit 8d6affb
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 201 deletions.
8 changes: 4 additions & 4 deletions examples/src/counter.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { NearContract, NearBindgen, near, call, view } from 'near-sdk-js'
import { NearBindgen, near, call, view, initialize } from 'near-sdk-js'
import { isUndefined } from 'lodash-es'

@NearBindgen
class Counter extends NearContract {
constructor({ initial = 0 }) {
super()
class Counter {
@initialize
init({ initial = 0 }) {
this.count = initial
}

Expand Down
53 changes: 27 additions & 26 deletions lib/build-tools/near-bindgen-exporter.js

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

5 changes: 2 additions & 3 deletions lib/index.d.ts

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

5 changes: 2 additions & 3 deletions lib/index.js

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

9 changes: 7 additions & 2 deletions lib/near-bindgen.d.ts

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

32 changes: 20 additions & 12 deletions lib/near-bindgen.js

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

8 changes: 0 additions & 8 deletions lib/near-contract.d.ts

This file was deleted.

31 changes: 0 additions & 31 deletions lib/near-contract.js

This file was deleted.

95 changes: 40 additions & 55 deletions src/build-tools/near-bindgen-exporter.js
Original file line number Diff line number Diff line change
@@ -1,70 +1,55 @@
import * as t from "@babel/types";

export default function () {
return {
visitor: {
ClassDeclaration(path) {
let classNode = path.node
let classNode = path.node;
if (classNode.decorators && classNode.decorators[0].expression.name == 'NearBindgen') {
let classId = classNode.id
let contractMethods = {}

for(let child of classNode.body.body) {
let classId = classNode.id;
let contractMethods = {};
for (let child of classNode.body.body) {
if (child.type == 'ClassMethod' && child.kind == 'method' && child.decorators) {
if (child.decorators[0].expression.name == 'call') {
let callMethod = child.key.name
contractMethods[callMethod] = 'call'
} else if (child.decorators[0].expression.name == 'view') {
let viewMethod = child.key.name
contractMethods[viewMethod] = 'view'
let callMethod = child.key.name;
contractMethods[callMethod] = 'call';
}
else if (child.decorators[0].expression.name == 'view') {
let viewMethod = child.key.name;
contractMethods[viewMethod] = 'view';
}
else if (child.decorators[0].expression.name == 'initialize') {
let initMethod = child.key.name;
contractMethods[initMethod] = 'initialize';
}
}
}

for (let method of Object.keys(contractMethods)) {
path.insertAfter(
t.exportNamedDeclaration(
t.functionDeclaration(t.identifier(method), [], t.blockStatement([
// let _contract = ContractClass._get()
t.variableDeclaration('let', [t.variableDeclarator(t.identifier('_contract'),
t.callExpression(t.memberExpression(classId, t.identifier('_get')), []))]),
// _contract.deserialize()
t.expressionStatement(
t.callExpression(t.memberExpression(t.identifier('_contract'), t.identifier('deserialize')), [])),
// let args = _contract.constructor.deserializeArgs()
t.variableDeclaration('let', [t.variableDeclarator(t.identifier('args'),
t.callExpression(t.memberExpression(t.memberExpression(t.identifier('_contract'), t.identifier('constructor')), t.identifier('deserializeArgs')), []))]),
// let ret = _contract.method(args)
t.variableDeclaration('let', [t.variableDeclarator(t.identifier('ret'),
t.callExpression(t.memberExpression(t.identifier('_contract'), t.identifier(method)), [t.identifier('args')]))]),
contractMethods[method] == 'call' ?
// _contract.serialize()
t.expressionStatement(
t.callExpression(t.memberExpression(t.identifier('_contract'), t.identifier('serialize')), []))
: t.emptyStatement(),
// if (ret !== undefined)
t.ifStatement(t.binaryExpression('!==', t.identifier('ret'), t.identifier('undefined')),
// env.value_return(_contract.constructor.serializeReturn(ret))
t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('env'), t.identifier('value_return')), [
t.callExpression(t.memberExpression(t.memberExpression(t.identifier('_contract'), t.identifier('constructor')), t.identifier('serializeReturn')), [t.identifier('ret')])
]))
)
])),
[t.exportSpecifier(t.identifier(method), t.identifier(method))]))
path.scope.registerDeclaration(path.getSibling(path.key + 1))
path.insertAfter(t.exportNamedDeclaration(t.functionDeclaration(t.identifier(method), [], t.blockStatement([
// const _state = Counter._getState();
t.variableDeclaration('let', [t.variableDeclarator(t.identifier('_state'), t.callExpression(t.memberExpression(classId, t.identifier('_getState')), []))]),
contractMethods[method] == 'initialize' ?
// if (_state) { throw new Error('Counter already initialized'); }
t.ifStatement(t.identifier('_state'), t.throwStatement(t.newExpression(t.identifier('Error'), [t.stringLiteral('Contract already initialized')])))
: t.emptyStatement(),
// let _contract = Counter._create();
t.variableDeclaration('let', [t.variableDeclarator(t.identifier('_contract'), t.callExpression(t.memberExpression(classId, t.identifier('_create')), []))]),
// Object.assign(_contract, state);
t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('Object'), t.identifier('assign')), [t.identifier('_contract'), t.identifier('_state')])),
// let _args = Counter._getArgs();
t.variableDeclaration('let', [t.variableDeclarator(t.identifier('_args'), t.callExpression(t.memberExpression(classId, t.identifier('_getArgs')), []))]),
// let _result = _contract.method(args);
t.variableDeclaration('let', [t.variableDeclarator(t.identifier('_result'), t.callExpression(t.memberExpression(t.identifier('_contract'), t.identifier(method)), [t.identifier('_args')]))]),
contractMethods[method] == 'initialize' || contractMethods[method] == 'call' ?
// _contract._saveToStorage();
t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('_contract'), t.identifier('_saveToStorage')), []))
: t.emptyStatement(),
// if (_result !== undefined) near.valueReturn(_contract._serialize(result));
t.ifStatement(t.binaryExpression('!==', t.identifier('_result'), t.identifier('undefined')), t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('near'), t.identifier('valueReturn')), [t.callExpression(t.memberExpression(t.identifier('_contract'), t.identifier('_serialize')), [t.identifier('_result')])]))),
]))));
console.log('Near bindgen export done');
}

path.insertAfter(
t.exportNamedDeclaration(
t.functionDeclaration(t.identifier('init'), [], t.blockStatement([
t.expressionStatement(t.callExpression(t.memberExpression(classId, t.identifier('_init')), [])),
])),
[t.exportSpecifier(t.identifier('init'), t.identifier('init'))]))
path.scope.registerDeclaration(path.getSibling(path.key + 1))

console.log('Near bindgen export done')
}
},
},
}
}
}
};
}
6 changes: 2 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { call, view, NearBindgen } from "./near-bindgen";

import { NearContract } from "./near-contract";
import { call, view, initialize, NearBindgen } from "./near-bindgen";

import * as near from "./api";
import {
Expand All @@ -16,8 +14,8 @@ import { bytes, Bytes, assert } from "./utils";
export {
call,
view,
initialize,
NearBindgen,
NearContract,
near,
LookupMap,
Vector,
Expand Down
45 changes: 30 additions & 15 deletions src/near-bindgen.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,41 @@
export function call (target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor<Function>): void {
import * as near from "./api";

export function initialize(target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor<Function>): void {
}

export function call(target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor<Function>): void {
}

export function view (target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor<Function>): void {
export function view(target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor<Function>): void {
}


export function NearBindgen<T extends { new(...args: any[]): {}}>(target: T) {
export function NearBindgen<T extends { new(...args: any[]): {} }>(target: T) {
return class extends target {
static _init() {
// @ts-ignore
let args = target.deserializeArgs()
let ret = new target(args)
// @ts-ignore
ret.init()
// @ts-ignore
ret.serialize()
return ret
static _create() {
return new target();
}

static _getState(): Object {
const rawState = near.storageRead("STATE");
return rawState ? this._deserialize(rawState) : null;
}

static _saveToStorage(): void {
near.storageWrite("STATE", this._serialize(this));
}

static _getArgs(): JSON {
return JSON.parse(near.input() || "{}");
}

static _get() {
let ret = Object.create(target.prototype)
return ret
static _serialize(value: Object): string {
return JSON.stringify(value);
}

static _deserialize(value: string): Object {
return JSON.parse(value);
}
}
}

Loading

0 comments on commit 8d6affb

Please sign in to comment.