Skip to content

Commit

Permalink
feat: fix when arg is undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
AbigailDeng authored and AbigailDeng committed Sep 18, 2023
1 parent 8d90d25 commit 7e330a5
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 32 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
43 changes: 23 additions & 20 deletions src/chain/chainMethod.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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 {
Expand All @@ -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;
});
}
}
30 changes: 21 additions & 9 deletions src/chain/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
});
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/contract/contractMethod.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

Expand Down Expand Up @@ -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;
}
});
Expand Down
7 changes: 7 additions & 0 deletions test/unit/chain/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down

0 comments on commit 7e330a5

Please sign in to comment.