mirror of
https://github.com/status-im/web3.js.git
synced 2025-02-23 19:48:13 +00:00
inputFormatter is an aray of functions
This commit is contained in:
parent
6b07920aa5
commit
4d1cf0bd9f
46
dist/ethereum.js
vendored
46
dist/ethereum.js
vendored
@ -1128,7 +1128,7 @@ var web3Methods = function () {
|
||||
name: 'sha3',
|
||||
call: 'web3_sha3',
|
||||
params: 1,
|
||||
inputFormatter: function (args) { return [utils.toHex(args[0])]; }
|
||||
inputFormatter:[utils.toHex]
|
||||
});
|
||||
|
||||
return [sha3];
|
||||
@ -1775,6 +1775,7 @@ var getBalance = new Method({
|
||||
name: 'getBalance',
|
||||
call: 'eth_getBalance',
|
||||
params: 2,
|
||||
inputFormatter: [null, formatters.inputBlockNumberFormatter],
|
||||
outputFormatter: formatters.inputNumberFormatter
|
||||
});
|
||||
|
||||
@ -2277,15 +2278,18 @@ module.exports = filter;
|
||||
var utils = require('../utils/utils');
|
||||
|
||||
/**
|
||||
* Should the input to a big number
|
||||
* Should the format output to a big number
|
||||
*
|
||||
* @method inputNumberFormatter
|
||||
* @method outputNumberFormatter
|
||||
* @param {String|Number|BigNumber}
|
||||
* @returns {BigNumber} object
|
||||
*/
|
||||
var inputNumberFormatter = function (args) {
|
||||
args[0] = utils.toBigNumber(args[0]);
|
||||
return args;
|
||||
var outputNumberFormatter = function (number) {
|
||||
return utils.toBigNumber(number);
|
||||
};
|
||||
|
||||
var inputBlockNumberFormatter = function (blockNumber) {
|
||||
return blockNumber === null ? -1 : blockNumber;
|
||||
};
|
||||
|
||||
/**
|
||||
@ -2295,8 +2299,7 @@ var inputNumberFormatter = function (args) {
|
||||
* @param {Object} transaction options
|
||||
* @returns object
|
||||
*/
|
||||
var inputTransactionFormatter = function (args){
|
||||
var options = args[0];
|
||||
var inputTransactionFormatter = function (options){
|
||||
|
||||
// make code -> data
|
||||
if (options.code) {
|
||||
@ -2308,7 +2311,7 @@ var inputTransactionFormatter = function (args){
|
||||
options[key] = utils.fromDecimal(options[key]);
|
||||
});
|
||||
|
||||
return args;
|
||||
return options;
|
||||
};
|
||||
|
||||
/**
|
||||
@ -2332,16 +2335,15 @@ var outputTransactionFormatter = function (tx){
|
||||
* @param {Object} transaction options
|
||||
* @returns object
|
||||
*/
|
||||
var inputCallFormatter = function (args){
|
||||
var options = args[0];
|
||||
|
||||
var inputCallFormatter = function (options){
|
||||
|
||||
// make code -> data
|
||||
if (options.code) {
|
||||
options.data = options.code;
|
||||
delete options.code;
|
||||
}
|
||||
|
||||
return args;
|
||||
return options;
|
||||
};
|
||||
|
||||
var inputBlockFormatter = function (args) {
|
||||
@ -2410,8 +2412,7 @@ var outputLogFormatter = function(log){
|
||||
* @param {Object} transaction object
|
||||
* @returns {Object}
|
||||
*/
|
||||
var inputPostFormatter = function(args){
|
||||
var post = args[0];
|
||||
var inputPostFormatter = function(post){
|
||||
|
||||
post.payload = utils.toHex(post.payload);
|
||||
post.ttl = utils.fromDecimal(post.ttl);
|
||||
@ -2426,7 +2427,7 @@ var inputPostFormatter = function(args){
|
||||
return utils.fromAscii(topic);
|
||||
});
|
||||
|
||||
return args;
|
||||
return post;
|
||||
};
|
||||
|
||||
/**
|
||||
@ -2460,12 +2461,13 @@ var outputPostFormatter = function(post){
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
inputNumberFormatter: inputNumberFormatter,
|
||||
inputBlockNumberFormatter: inputBlockNumberFormatter,
|
||||
inputTransactionFormatter: inputTransactionFormatter,
|
||||
inputCallFormatter: inputCallFormatter,
|
||||
inputPostFormatter: inputPostFormatter,
|
||||
inputBlockFormatter: inputBlockFormatter,
|
||||
inputUncleFormatter: inputUncleFormatter,
|
||||
outputNumberFormatter: outputNumberFormatter,
|
||||
outputTransactionFormatter: outputTransactionFormatter,
|
||||
outputBlockFormatter: outputBlockFormatter,
|
||||
outputLogFormatter: outputLogFormatter,
|
||||
@ -2695,7 +2697,13 @@ Method.prototype.validateArgs = function (args) {
|
||||
* @return {Array}
|
||||
*/
|
||||
Method.prototype.formatInput = function (args) {
|
||||
return this.inputFormatter ? this.inputFormatter(args) : args;
|
||||
if (!this.inputFormatter) {
|
||||
return args;
|
||||
}
|
||||
|
||||
return this.inputFormatter.map(function (formatter, index) {
|
||||
return formatter ? formatter(args[index]) : args[index];
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
@ -2706,7 +2714,7 @@ Method.prototype.formatInput = function (args) {
|
||||
* @return {Object}
|
||||
*/
|
||||
Method.prototype.formatOutput = function (result) {
|
||||
return this.outputFormatter && !!result ? this.outputFormatter(result) : result;
|
||||
return this.outputFormatter && result !== null ? this.outputFormatter(result) : result;
|
||||
};
|
||||
|
||||
/**
|
||||
|
10
dist/ethereum.js.map
vendored
10
dist/ethereum.js.map
vendored
File diff suppressed because one or more lines are too long
2
dist/ethereum.min.js
vendored
2
dist/ethereum.min.js
vendored
File diff suppressed because one or more lines are too long
@ -43,7 +43,7 @@ var web3Methods = function () {
|
||||
name: 'sha3',
|
||||
call: 'web3_sha3',
|
||||
params: 1,
|
||||
inputFormatter: function (args) { return [utils.toHex(args[0])]; }
|
||||
inputFormatter:[utils.toHex]
|
||||
});
|
||||
|
||||
return [sha3];
|
||||
|
@ -78,6 +78,7 @@ var getBalance = new Method({
|
||||
name: 'getBalance',
|
||||
call: 'eth_getBalance',
|
||||
params: 2,
|
||||
inputFormatter: [null, formatters.inputBlockNumberFormatter],
|
||||
outputFormatter: formatters.inputNumberFormatter
|
||||
});
|
||||
|
||||
|
@ -24,15 +24,18 @@
|
||||
var utils = require('../utils/utils');
|
||||
|
||||
/**
|
||||
* Should the input to a big number
|
||||
* Should the format output to a big number
|
||||
*
|
||||
* @method inputNumberFormatter
|
||||
* @method outputNumberFormatter
|
||||
* @param {String|Number|BigNumber}
|
||||
* @returns {BigNumber} object
|
||||
*/
|
||||
var inputNumberFormatter = function (args) {
|
||||
args[0] = utils.toBigNumber(args[0]);
|
||||
return args;
|
||||
var outputNumberFormatter = function (number) {
|
||||
return utils.toBigNumber(number);
|
||||
};
|
||||
|
||||
var inputBlockNumberFormatter = function (blockNumber) {
|
||||
return blockNumber === null ? -1 : blockNumber;
|
||||
};
|
||||
|
||||
/**
|
||||
@ -42,8 +45,7 @@ var inputNumberFormatter = function (args) {
|
||||
* @param {Object} transaction options
|
||||
* @returns object
|
||||
*/
|
||||
var inputTransactionFormatter = function (args){
|
||||
var options = args[0];
|
||||
var inputTransactionFormatter = function (options){
|
||||
|
||||
// make code -> data
|
||||
if (options.code) {
|
||||
@ -55,7 +57,7 @@ var inputTransactionFormatter = function (args){
|
||||
options[key] = utils.fromDecimal(options[key]);
|
||||
});
|
||||
|
||||
return args;
|
||||
return options;
|
||||
};
|
||||
|
||||
/**
|
||||
@ -79,16 +81,15 @@ var outputTransactionFormatter = function (tx){
|
||||
* @param {Object} transaction options
|
||||
* @returns object
|
||||
*/
|
||||
var inputCallFormatter = function (args){
|
||||
var options = args[0];
|
||||
|
||||
var inputCallFormatter = function (options){
|
||||
|
||||
// make code -> data
|
||||
if (options.code) {
|
||||
options.data = options.code;
|
||||
delete options.code;
|
||||
}
|
||||
|
||||
return args;
|
||||
return options;
|
||||
};
|
||||
|
||||
var inputBlockFormatter = function (args) {
|
||||
@ -157,8 +158,7 @@ var outputLogFormatter = function(log){
|
||||
* @param {Object} transaction object
|
||||
* @returns {Object}
|
||||
*/
|
||||
var inputPostFormatter = function(args){
|
||||
var post = args[0];
|
||||
var inputPostFormatter = function(post){
|
||||
|
||||
post.payload = utils.toHex(post.payload);
|
||||
post.ttl = utils.fromDecimal(post.ttl);
|
||||
@ -173,7 +173,7 @@ var inputPostFormatter = function(args){
|
||||
return utils.fromAscii(topic);
|
||||
});
|
||||
|
||||
return args;
|
||||
return post;
|
||||
};
|
||||
|
||||
/**
|
||||
@ -207,12 +207,13 @@ var outputPostFormatter = function(post){
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
inputNumberFormatter: inputNumberFormatter,
|
||||
inputBlockNumberFormatter: inputBlockNumberFormatter,
|
||||
inputTransactionFormatter: inputTransactionFormatter,
|
||||
inputCallFormatter: inputCallFormatter,
|
||||
inputPostFormatter: inputPostFormatter,
|
||||
inputBlockFormatter: inputBlockFormatter,
|
||||
inputUncleFormatter: inputUncleFormatter,
|
||||
outputNumberFormatter: outputNumberFormatter,
|
||||
outputTransactionFormatter: outputTransactionFormatter,
|
||||
outputBlockFormatter: outputBlockFormatter,
|
||||
outputLogFormatter: outputLogFormatter,
|
||||
|
@ -77,7 +77,13 @@ Method.prototype.validateArgs = function (args) {
|
||||
* @return {Array}
|
||||
*/
|
||||
Method.prototype.formatInput = function (args) {
|
||||
return this.inputFormatter ? this.inputFormatter(args) : args;
|
||||
if (!this.inputFormatter) {
|
||||
return args;
|
||||
}
|
||||
|
||||
return this.inputFormatter.map(function (formatter, index) {
|
||||
return formatter ? formatter(args[index]) : args[index];
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
@ -88,7 +94,7 @@ Method.prototype.formatInput = function (args) {
|
||||
* @return {Object}
|
||||
*/
|
||||
Method.prototype.formatOutput = function (result) {
|
||||
return this.outputFormatter && !!result ? this.outputFormatter(result) : result;
|
||||
return this.outputFormatter && result !== null ? this.outputFormatter(result) : result;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -7,21 +7,21 @@ describe('formatters', function () {
|
||||
it('should return the correct value', function () {
|
||||
|
||||
// input as strings and numbers
|
||||
assert.deepEqual(formatters.inputPostFormatter([{
|
||||
assert.deepEqual(formatters.inputPostFormatter({
|
||||
from: '0x00000',
|
||||
to: '0x00000',
|
||||
payload: {test: 'test'},
|
||||
ttl: 200,
|
||||
priority: 1000,
|
||||
topics: ['hello','mytopics']
|
||||
}]), [{
|
||||
}), {
|
||||
from: '0x00000',
|
||||
to: '0x00000',
|
||||
payload: '0x7b2274657374223a2274657374227d',
|
||||
ttl: '0xc8',
|
||||
priority: '0x3e8',
|
||||
topics: ['0x68656c6c6f','0x6d79746f70696373']
|
||||
}]);
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
@ -7,21 +7,21 @@ describe('formatters', function () {
|
||||
describe('inputTransactionFormatter', function () {
|
||||
it('should return the correct value', function () {
|
||||
|
||||
assert.deepEqual(formatters.inputTransactionFormatter([{
|
||||
assert.deepEqual(formatters.inputTransactionFormatter({
|
||||
data: '0x34234kjh23kj4234',
|
||||
value: new BigNumber(100),
|
||||
from: '0x00000',
|
||||
to: '0x00000',
|
||||
gas: 1000,
|
||||
gasPrice: new BigNumber(1000),
|
||||
}]), [{
|
||||
}), {
|
||||
data: '0x34234kjh23kj4234',
|
||||
value: '0x64',
|
||||
from: '0x00000',
|
||||
to: '0x00000',
|
||||
gas: '0x3e8',
|
||||
gasPrice: '0x3e8',
|
||||
}]);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -7,14 +7,12 @@ describe('method', function () {
|
||||
it('should format plain input', function () {
|
||||
|
||||
// given
|
||||
var formatter = function (args) {
|
||||
return args.map(function (arg) {
|
||||
return arg + '*';
|
||||
});
|
||||
var star = function (arg) {
|
||||
return arg + '*';
|
||||
};
|
||||
|
||||
var method = new Method({
|
||||
inputFormatter: formatter
|
||||
inputFormatter: [star, star, star]
|
||||
});
|
||||
var args = ['1','2','3'];
|
||||
var expectedArgs = ['1*', '2*', '3*'];
|
||||
|
Loading…
x
Reference in New Issue
Block a user