From 3d8ac7c6f78d330aeaca87647f12c67f0ae6a68d Mon Sep 17 00:00:00 2001 From: Serhii Volovyk Date: Wed, 31 Aug 2022 16:53:32 +0300 Subject: [PATCH] ft example refactored --- examples/__tests__/test-fungible-token.ava.js | 1 - examples/src/fungible-token-helper.js | 1 - examples/src/fungible-token.js | 25 +++++++++---------- lib/build-tools/near-bindgen-exporter.js | 2 +- src/build-tools/near-bindgen-exporter.js | 3 ++- 5 files changed, 15 insertions(+), 17 deletions(-) diff --git a/examples/__tests__/test-fungible-token.ava.js b/examples/__tests__/test-fungible-token.ava.js index 857d6f177..6e147449b 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 c03c6879a..68a076e41 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 11d82289a..0c3c1f392 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 4b81cc125..4dfc8be74 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 227271590..8cd2780fa 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`); }