Skip to content

Commit

Permalink
Merge pull request #133 from AElfProject/feature/view
Browse files Browse the repository at this point in the history
feat: getChainStatus
  • Loading branch information
hzz780 authored Nov 7, 2023
2 parents 79e9d83 + dcc7e74 commit fc26b52
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 43 deletions.
30 changes: 23 additions & 7 deletions dist/aelf.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -33063,13 +33063,18 @@ function () {
}
}, {
key: "prepareParametersAsync",
value: function prepareParametersAsync(args) {
value: function prepareParametersAsync(args, isView) {
var _this = this;

var filterArgs = args.filter(function (arg) {
return !isFunction(arg) && !isBoolean(arg.sync);
});
var encoded = this.packInput(filterArgs[0]);

if (isView) {
return Promise.resolve(this.handleTransaction('', '', encoded));
}

return this._chain.getChainStatus().then(function (status) {
var BestChainHeight = status.BestChainHeight,
BestChainHash = status.BestChainHash;
Expand All @@ -33078,12 +33083,16 @@ function () {
}
}, {
key: "prepareParameters",
value: function prepareParameters(args) {
value: function prepareParameters(args, isView) {
var filterArgs = args.filter(function (arg) {
return !isFunction(arg) && !isBoolean(arg.sync);
});
var encoded = this.packInput(filterArgs[0]);

if (isView) {
return this.handleTransaction('', '', encoded);
}

var _this$_chain$getChain = this._chain.getChainStatus({
sync: true
}),
Expand Down Expand Up @@ -33140,14 +33149,14 @@ function () {
var argsObject = this.extractArgumentsIntoObject(args);

if (argsObject.isSync) {
var parameters = this.prepareParameters(args);
var parameters = this.prepareParameters(args, true);
return this.unpackOutput(this._chain.callReadOnly(parameters, {
sync: true
}));
} // eslint-disable-next-line arrow-body-style


return this.prepareParametersAsync(args).then(function (parameters) {
return this.prepareParametersAsync(args, true).then(function (parameters) {
return _this3._chain.callReadOnly(parameters, function (error, result) {
argsObject.callback(error, _this3.unpackOutput(result));
}).then(_this3.unpackOutput);
Expand Down Expand Up @@ -33207,9 +33216,16 @@ function () {
key: "getRawTx",
value: function getRawTx(blockHeightInput, blockHashInput, packedInput) {
var rawTx = getTransaction(this._wallet.address, this._contractAddress, this._name, packedInput);
rawTx.refBlockNumber = blockHeightInput;
var blockHash = blockHashInput.match(/^0x/) ? blockHashInput.substring(2) : blockHashInput;
rawTx.refBlockPrefix = Buffer.from(blockHash, 'hex').slice(0, 4);

if (blockHeightInput) {
rawTx.refBlockNumber = blockHeightInput;
}

if (blockHashInput) {
var blockHash = blockHashInput.match(/^0x/) ? blockHashInput.substring(2) : blockHashInput;
rawTx.refBlockPrefix = Buffer.from(blockHash, 'hex').slice(0, 4);
}

return rawTx;
}
}, {
Expand Down
2 changes: 1 addition & 1 deletion dist/aelf.cjs.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/aelf.umd.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/aelf.umd.js.map

Large diffs are not rendered by default.

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.3.0",
"version": "3.4.0",
"description": "aelf-sdk js library",
"main": "dist/aelf.cjs.js",
"browser": "dist/aelf.umd.js",
Expand Down
90 changes: 58 additions & 32 deletions src/contract/contractMethod.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,20 @@
* @file contract method
* @author atom-yang
*/
import {
getTransaction,
Transaction
} from '../util/proto';
import { getTransaction, Transaction } from '../util/proto';
import {
transformArrayToMap,
transformMapToArray,
transform,
INPUT_TRANSFORMERS,
OUTPUT_TRANSFORMERS
OUTPUT_TRANSFORMERS,
} from '../util/transform';
import {
isBoolean,
isFunction,
noop,
uint8ArrayToHex,
unpackSpecifiedTypeData
unpackSpecifiedTypeData,
} from '../util/utils';
import wallet from '../wallet';

Expand Down Expand Up @@ -61,7 +58,7 @@ export default class ContractMethod {
}
const result = unpackSpecifiedTypeData({
data: inputPacked,
dataType: this._inputType
dataType: this._inputType,
});
let params = transform(this._inputType, result, OUTPUT_TRANSFORMERS);
params = transformArrayToMap(this._inputType, params);
Expand All @@ -74,7 +71,7 @@ export default class ContractMethod {
}
let result = unpackSpecifiedTypeData({
data: output,
dataType: this._outputType
dataType: this._outputType,
});
result = transform(this._outputType, result, OUTPUT_TRANSFORMERS);
result = transformArrayToMap(this._outputType, result);
Expand Down Expand Up @@ -103,29 +100,42 @@ export default class ContractMethod {
return uint8ArrayToHex(tx);
}

prepareParametersAsync(args) {
const filterArgs = args.filter(arg => !isFunction(arg) && !isBoolean(arg.sync));
prepareParametersAsync(args, isView) {
const filterArgs = args.filter(
arg => !isFunction(arg) && !isBoolean(arg.sync)
);
const encoded = this.packInput(filterArgs[0]);

if (isView) {
return Promise.resolve(this.handleTransaction('', '', encoded));
}
return this._chain.getChainStatus().then(status => {
const { BestChainHeight, BestChainHash } = status;
return this.handleTransaction(BestChainHeight, BestChainHash, encoded);
});
}

prepareParameters(args) {
const filterArgs = args.filter(arg => !isFunction(arg) && !isBoolean(arg.sync));
prepareParameters(args, isView) {
const filterArgs = args.filter(
arg => !isFunction(arg) && !isBoolean(arg.sync)
);
const encoded = this.packInput(filterArgs[0]);

if (isView) {
return this.handleTransaction('', '', encoded);
}

const { BestChainHeight, BestChainHash } = this._chain.getChainStatus({
sync: true
sync: true,
});

return this.handleTransaction(BestChainHeight, BestChainHash, encoded);
}

prepareParametersWithBlockInfo(args) {
const filterArgs = args.filter(arg => !isFunction(arg) && !isBoolean(arg.sync));
const filterArgs = args.filter(
arg => !isFunction(arg) && !isBoolean(arg.sync)
);
const encoded = this.packInput(filterArgs[0]);

const { height, hash } = filterArgs[1]; // blockInfo
Expand All @@ -138,7 +148,7 @@ export default class ContractMethod {
if (argsObject.isSync) {
const parameters = this.prepareParameters(args);
return this._chain.sendTransaction(parameters, {
sync: true
sync: true,
});
}
// eslint-disable-next-line arrow-body-style
Expand All @@ -150,23 +160,27 @@ export default class ContractMethod {
callReadOnly(...args) {
const argsObject = this.extractArgumentsIntoObject(args);
if (argsObject.isSync) {
const parameters = this.prepareParameters(args);
return this.unpackOutput(this._chain.callReadOnly(parameters, {
sync: true
}));
const parameters = this.prepareParameters(args, true);
return this.unpackOutput(
this._chain.callReadOnly(parameters, {
sync: true,
})
);
}
// eslint-disable-next-line arrow-body-style
return this.prepareParametersAsync(args).then(parameters => {
return this._chain.callReadOnly(parameters, (error, result) => {
argsObject.callback(error, this.unpackOutput(result));
}).then(this.unpackOutput);
return this.prepareParametersAsync(args, true).then(parameters => {
return this._chain
.callReadOnly(parameters, (error, result) => {
argsObject.callback(error, this.unpackOutput(result));
})
.then(this.unpackOutput);
});
}

extractArgumentsIntoObject(args) {
const result = {
callback: noop,
isSync: false
isSync: false,
};
if (args.length === 0) {
// has no callback, default to be async mode
Expand All @@ -176,7 +190,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 All @@ -185,7 +199,9 @@ export default class ContractMethod {

// getData(...args) {
getSignedTx(...args) {
const filterArgs = args.filter(arg => !isFunction(arg) && !isBoolean(arg.sync));
const filterArgs = args.filter(
arg => !isFunction(arg) && !isBoolean(arg.sync)
);

if (filterArgs[1]) {
const { height, hash } = filterArgs[1]; // blockInfo
Expand All @@ -199,11 +215,21 @@ export default class ContractMethod {
}

getRawTx(blockHeightInput, blockHashInput, packedInput) {
const rawTx = getTransaction(this._wallet.address, this._contractAddress, this._name, packedInput);

rawTx.refBlockNumber = blockHeightInput;
const blockHash = blockHashInput.match(/^0x/) ? blockHashInput.substring(2) : blockHashInput;
rawTx.refBlockPrefix = (Buffer.from(blockHash, 'hex')).slice(0, 4);
const rawTx = getTransaction(
this._wallet.address,
this._contractAddress,
this._name,
packedInput
);
if (blockHeightInput) {
rawTx.refBlockNumber = blockHeightInput;
}
if (blockHashInput) {
const blockHash = blockHashInput.match(/^0x/)
? blockHashInput.substring(2)
: blockHashInput;
rawTx.refBlockPrefix = Buffer.from(blockHash, 'hex').slice(0, 4);
}
return rawTx;
}

Expand All @@ -214,7 +240,7 @@ export default class ContractMethod {
method: 'broadcast_tx',
callback,
params,
format: this.unpackOutput
format: this.unpackOutput,
};
}

Expand Down

0 comments on commit fc26b52

Please sign in to comment.