Skip to content

Commit

Permalink
ft example refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
volovyks committed Aug 31, 2022
1 parent 589f789 commit 3d8ac7c
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 17 deletions.
1 change: 0 additions & 1 deletion examples/__tests__/test-fungible-token.ava.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
1 change: 0 additions & 1 deletion examples/src/fungible-token-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { NearBindgen, call, view } from "near-sdk-js";
@NearBindgen({})
class FungibleTokenHelper {
constructor() {
super();
this.data = "";
}

Expand Down
25 changes: 12 additions & 13 deletions examples/src/fungible-token.js
Original file line number Diff line number Diff line change
@@ -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 }) {
Expand Down Expand Up @@ -71,8 +74,4 @@ class FungibleToken extends NearContract {
ftBalanceOf({ accountId }) {
return this.accounts.get(accountId) || '0'
}

default() {
return new FungibleToken({ prefix: '', totalSupply: 0 })
}
}
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.

3 changes: 2 additions & 1 deletion src/build-tools/near-bindgen-exporter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as t from "@babel/types";

export default function () {
return {
visitor: {
Expand Down Expand Up @@ -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`);
}
Expand Down

0 comments on commit 3d8ac7c

Please sign in to comment.