From 7e330a5e461c5f38a6c36ab172c269229a1e206e Mon Sep 17 00:00:00 2001 From: AbigailDeng Date: Mon, 18 Sep 2023 17:28:29 +0800 Subject: [PATCH] feat: fix when arg is undefined --- package.json | 2 +- src/chain/chainMethod.js | 43 ++++++++++++++++++---------------- src/chain/index.js | 30 +++++++++++++++++------- src/contract/contractMethod.js | 4 ++-- test/unit/chain/index.test.js | 7 ++++++ 5 files changed, 54 insertions(+), 32 deletions(-) diff --git a/package.json b/package.json index fe9e5a4d..98378e7d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "aelf-sdk", - "version": "3.2.44", + "version": "3.2.45-beta.1", "description": "aelf-sdk js library", "main": "dist/aelf.cjs.js", "browser": "dist/aelf.umd.js", diff --git a/src/chain/chainMethod.js b/src/chain/chainMethod.js index b7381ec1..229dc8e5 100644 --- a/src/chain/chainMethod.js +++ b/src/chain/chainMethod.js @@ -5,16 +5,14 @@ import { isFunction, noop, isBoolean } from '../util/utils'; export default class ChainMethod { - constructor( - { - name, - call, - method = 'GET', - params = [], - inputFormatter = [], - outputFormatter = null - } - ) { + constructor({ + name, + call, + method = 'GET', + params = [], + inputFormatter = [], + outputFormatter = null, + }) { this.name = name; this.call = call; this.requestMethod = method; @@ -41,7 +39,9 @@ export default class ChainMethod { } formatOutput(result) { - return this.outputFormatter && result ? this.outputFormatter(result) : result; + return this.outputFormatter && result + ? this.outputFormatter(result) + : result; } extractArgumentsIntoObject(args) { @@ -53,7 +53,7 @@ export default class ChainMethod { requestMethod: this.requestMethod, isSync: false, callback: noop, - params: {} + params: {}, }; this.formatInput(args).forEach((arg, index) => { if (index > this.params.length - 1) { @@ -63,7 +63,7 @@ export default class ChainMethod { result.callback = arg; result.isSync = false; } - if (isBoolean(arg.sync)) { + if (isBoolean(arg?.sync)) { result.isSync = arg.sync; } } else { @@ -79,12 +79,15 @@ export default class ChainMethod { if (argsObj.isSync) { return this.formatOutput(this.requestManager.send(argsObj)); } - return this.requestManager.sendAsync(argsObj).then(result => { - argsObj.callback(null, this.formatOutput(result)); - return this.formatOutput(result); - }).catch(err => { - argsObj.callback(err); - throw err; - }); + return this.requestManager + .sendAsync(argsObj) + .then(result => { + argsObj.callback(null, this.formatOutput(result)); + return this.formatOutput(result); + }) + .catch(err => { + argsObj.callback(err); + throw err; + }); } } diff --git a/src/chain/index.js b/src/chain/index.js index 41563a40..a37dc2bb 100644 --- a/src/chain/index.js +++ b/src/chain/index.js @@ -28,7 +28,7 @@ export default class Chain { extractArgumentsIntoObject(args) { const result = { callback: noop, - isSync: false + isSync: false, }; if (args.length === 0) { // has no callback, default to be async mode @@ -38,7 +38,7 @@ export default class Chain { result.callback = args[args.length - 1]; } args.forEach(arg => { - if (isBoolean((arg.sync))) { + if (isBoolean(arg?.sync)) { result.isSync = arg.sync; } }); @@ -49,7 +49,7 @@ export default class Chain { const { callback, isSync } = this.extractArgumentsIntoObject(args); if (isSync) { const fds = this.getContractFileDescriptorSet(address, { - sync: true + sync: true, }); if (fds && fds.file && fds.file.length > 0) { const factory = new ContractFactory(this, fds, wallet); @@ -77,19 +77,26 @@ export default class Chain { const { isSync } = this.extractArgumentsIntoObject(args); if (isSync) { const block = this.getBlockByHeight(height, true, { - sync: true + sync: true, }); const { BlockHash, Body } = block; const txIds = Body.Transactions; const txIndex = txIds.findIndex(id => id === txId); if (txIndex === -1) { - throw new Error(`txId ${txId} has no correspond transaction in the block with height ${height}`); + throw new Error( + `txId ${txId} has no correspond transaction in the block with height ${height}` + ); } - const txResults = this.getTxResults(BlockHash, 0, txIds.length, { sync: true }); + const txResults = this.getTxResults(BlockHash, 0, txIds.length, { + sync: true, + }); const nodes = txResults.map((result, index) => { const id = txIds[index]; const status = result.Status; - const buffer = Buffer.concat([Buffer.from(id.replace('0x', ''), 'hex'), Buffer.from(status, 'utf8')]); + const buffer = Buffer.concat([ + Buffer.from(id.replace('0x', ''), 'hex'), + Buffer.from(status, 'utf8'), + ]); return merkleTree.node(buffer); }); return merkleTree.getMerklePath(txIndex, nodes); @@ -99,13 +106,18 @@ export default class Chain { const txIds = Body.Transactions; const txIndex = txIds.findIndex(id => id === txId); if (txIndex === -1) { - throw new Error(`txId ${txId} has no correspond transaction in the block with height ${height}`); + throw new Error( + `txId ${txId} has no correspond transaction in the block with height ${height}` + ); } return this.getTxResults(BlockHash, 0, txIds.length).then(results => { const nodes = results.map((result, index) => { const id = txIds[index]; const status = result.Status; - const buffer = Buffer.concat([Buffer.from(id.replace('0x', ''), 'hex'), Buffer.from(status, 'utf8')]); + const buffer = Buffer.concat([ + Buffer.from(id.replace('0x', ''), 'hex'), + Buffer.from(status, 'utf8'), + ]); return merkleTree.node(buffer); }); return merkleTree.getMerklePath(txIndex, nodes); diff --git a/src/contract/contractMethod.js b/src/contract/contractMethod.js index b5f5a381..2ec074d2 100644 --- a/src/contract/contractMethod.js +++ b/src/contract/contractMethod.js @@ -106,7 +106,7 @@ export default class ContractMethod { prepareParametersAsync(args) { const filterArgs = args.filter( - arg => !isFunction(arg) && !isBoolean(arg.sync) + arg => !isFunction(arg) && !isBoolean(arg?.sync) ); const encoded = this.packInput(filterArgs[0]); @@ -187,7 +187,7 @@ export default class ContractMethod { result.callback = args[args.length - 1]; } args.forEach(arg => { - if (isBoolean(arg.sync)) { + if (isBoolean(arg?.sync)) { result.isSync = arg.sync; } }); diff --git a/test/unit/chain/index.test.js b/test/unit/chain/index.test.js index c52976dc..4edfe17f 100644 --- a/test/unit/chain/index.test.js +++ b/test/unit/chain/index.test.js @@ -2,6 +2,7 @@ import Chain from '../../../src/chain/index'; import RequestManager from '../../../src/util/requestManage'; import HttpProvider from '../../../src/util/httpProvider'; import AElf from '../../../src'; +import { noop } from '../../../src/util/utils'; const stageEndpoint = 'https://explorer-test-tdvw.aelf.io/chain'; let httpProvider, requestManager, chain; @@ -20,6 +21,12 @@ describe('chain should work', () => { const result = chain.extractArgumentsIntoObject(args); expect(result.callback).toBe(fn); }); + test('test undefined argument into object ', () => { + const args = [undefined]; + const result = chain.extractArgumentsIntoObject(args); + expect(result.callback).toBe(noop); + expect(result.isSync).toBe(false); + }); test('test sync argument into object', () => { const args = [{ sync: true }]; const result = chain.extractArgumentsIntoObject(args);