diff --git a/examples/__tests__/test-fungible-token.ava.js b/examples/__tests__/test-fungible-token.ava.js index 857d6f17..6e147449 100644 --- a/examples/__tests__/test-fungible-token.ava.js +++ b/examples/__tests__/test-fungible-token.ava.js @@ -18,7 +18,6 @@ test.beforeEach(async (t) => { // Init the contracts await ft.call(ft, 'init', { prefix: 'a', totalSupply: '1000' }); - await xcc.call(xcc, 'init', {}); // Create test accounts const ali = await root.createSubAccount('ali'); diff --git a/examples/src/fungible-token-helper.js b/examples/src/fungible-token-helper.js index c03c6879..68a076e4 100644 --- a/examples/src/fungible-token-helper.js +++ b/examples/src/fungible-token-helper.js @@ -3,7 +3,6 @@ import { NearBindgen, call, view } from "near-sdk-js"; @NearBindgen({}) class FungibleTokenHelper { constructor() { - super(); this.data = ""; } diff --git a/examples/src/fungible-token.js b/examples/src/fungible-token.js index 11d82289..0c3c1f39 100644 --- a/examples/src/fungible-token.js +++ b/examples/src/fungible-token.js @@ -1,24 +1,27 @@ import { - NearContract, NearBindgen, call, view, + initialize, near, LookupMap, assert } from 'near-sdk-js' -@NearBindgen -class FungibleToken extends NearContract { - constructor({ prefix, totalSupply }) { - super() - this.accounts = new LookupMap(prefix) - this.totalSupply = totalSupply - // In a real world Fungible Token contract, storage management is required to denfense drain-storage attack + +@NearBindgen({ initRequired: true }) +class FungibleToken { + constructor() { + this.accounts = new LookupMap('a') + this.totalSupply = 0 } - init() { + @initialize + init({ prefix, totalSupply }) { + this.accounts = new LookupMap(prefix) + this.totalSupply = totalSupply this.accounts.set(near.signerAccountId(), this.totalSupply) + // In a real world Fungible Token contract, storage management is required to denfense drain-storage attack } internalDeposit({ accountId, amount }) { @@ -71,8 +74,4 @@ class FungibleToken extends NearContract { ftBalanceOf({ accountId }) { return this.accounts.get(accountId) || '0' } - - default() { - return new FungibleToken({ prefix: '', totalSupply: 0 }) - } } diff --git a/lib/build-tools/near-bindgen-exporter.js b/lib/build-tools/near-bindgen-exporter.js index 4b81cc12..4dfc8be7 100644 --- a/lib/build-tools/near-bindgen-exporter.js +++ b/lib/build-tools/near-bindgen-exporter.js @@ -49,7 +49,7 @@ export default function () { t.expressionStatement(t.callExpression(t.memberExpression(classId, t.identifier('_saveToStorage')), [t.identifier('_contract')])) : 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(classId, t.identifier('_serialize')), [t.identifier('_result')])]))), + t.ifStatement(t.binaryExpression('!==', t.identifier('_result'), t.identifier('undefined')), t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('env'), t.identifier('value_return')), [t.callExpression(t.memberExpression(classId, t.identifier('_serialize')), [t.identifier('_result')])]))), ])))); console.log(`Babel ${method} method export done`); } diff --git a/src/build-tools/near-bindgen-exporter.js b/src/build-tools/near-bindgen-exporter.js index 22727159..8cd2780f 100644 --- a/src/build-tools/near-bindgen-exporter.js +++ b/src/build-tools/near-bindgen-exporter.js @@ -1,4 +1,5 @@ import * as t from "@babel/types"; + export default function () { return { visitor: { @@ -50,7 +51,7 @@ export default function () { t.expressionStatement(t.callExpression(t.memberExpression(classId, t.identifier('_saveToStorage')), [t.identifier('_contract')])) : 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(classId, t.identifier('_serialize')), [t.identifier('_result')])]))), + t.ifStatement(t.binaryExpression('!==', t.identifier('_result'), t.identifier('undefined')), t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('env'), t.identifier('value_return')), [t.callExpression(t.memberExpression(classId, t.identifier('_serialize')), [t.identifier('_result')])]))), ])))); console.log(`Babel ${method} method export done`); }