mirror of https://github.com/status-im/web3.js.git
add missing formatters, fixed tests
This commit is contained in:
parent
7511e4c8af
commit
7fb440579d
|
@ -1733,10 +1733,11 @@ module.exports = {
|
|||
* {
|
||||
* name: 'getBlock',
|
||||
* call: blockCall,
|
||||
* params: 2,
|
||||
* outputFormatter: formatters.outputBlockFormatter,
|
||||
* inputFormatter: [ // can be a formatter funciton or an array of functions. Where each item in the array will be used for one parameter
|
||||
* utils.toHex, // formats paramter 1
|
||||
* function(param){ if(!param) return false; } // formats paramter 2
|
||||
* function(param){ return !!param; } // formats paramter 2
|
||||
* ]
|
||||
* },
|
||||
*
|
||||
|
@ -1776,22 +1777,22 @@ var getBalance = new Method({
|
|||
name: 'getBalance',
|
||||
call: 'eth_getBalance',
|
||||
params: 2,
|
||||
inputFormatter: [utils.toHex, formatters.inputBlockNumberFormatter],
|
||||
outputFormatter: formatters.inputNumberFormatter
|
||||
inputFormatter: [utils.toHex, formatters.inputDefaultBlockNumberFormatter],
|
||||
outputFormatter: formatters.outputBigNumberFormatter
|
||||
});
|
||||
|
||||
var getStorageAt = new Method({
|
||||
name: 'getStorageAt',
|
||||
call: 'eth_getStorageAt',
|
||||
params: 3,
|
||||
inputFormatter: [null, null, formatters.inputBlockNumberFormatter]
|
||||
inputFormatter: [null, utils.toHex, formatters.inputDefaultBlockNumberFormatter]
|
||||
});
|
||||
|
||||
var getCode = new Method({
|
||||
name: 'getCode',
|
||||
call: 'eth_getCode',
|
||||
params: 2,
|
||||
inputFormatter: [null, formatters.inputBlockNumberFormatter]
|
||||
inputFormatter: [null, formatters.inputDefaultBlockNumberFormatter]
|
||||
});
|
||||
|
||||
var getBlock = new Method({
|
||||
|
@ -1800,14 +1801,13 @@ var getBlock = new Method({
|
|||
params: 2,
|
||||
inputFormatter: [utils.toHex, function (val) { return !!val; }],
|
||||
outputFormatter: formatters.outputBlockFormatter
|
||||
|
||||
});
|
||||
|
||||
var getUncle = new Method({
|
||||
name: 'getUncle',
|
||||
call: uncleCall,
|
||||
params: 2,
|
||||
inputFormatter: [utils.toHex, utils.toHex],
|
||||
inputFormatter: [utils.toHex, utils.toHex, function (val) { return !!val; }],
|
||||
outputFormatter: formatters.outputBlockFormatter,
|
||||
|
||||
});
|
||||
|
@ -1822,16 +1822,16 @@ var getBlockTransactounCount = new Method({
|
|||
name: 'getBlockTransactionCount',
|
||||
call: getBlockTransactionCountCall,
|
||||
params: 1,
|
||||
outputFormatter: utils.toDecimal,
|
||||
inputFormatter: utils.toHex
|
||||
inputFormatter: [utils.toHex],
|
||||
outputFormatter: utils.toDecimal
|
||||
});
|
||||
|
||||
var getBlockUncleCount = new Method({
|
||||
name: 'getBlockUncleCount',
|
||||
call: uncleCountCall,
|
||||
params: 1,
|
||||
outputFormatter: utils.toDecimal,
|
||||
inputFormatter: utils.toHex
|
||||
inputFormatter: [utils.toHex],
|
||||
outputFormatter: utils.toDecimal
|
||||
});
|
||||
|
||||
var getTransaction = new Method({
|
||||
|
@ -1845,14 +1845,15 @@ var getTransactionFromBlock = new Method({
|
|||
name: 'getTransactionFromBlock',
|
||||
call: transactionFromBlockCall,
|
||||
params: 2,
|
||||
outputFormatter: formatters.outputTransactionFormatter,
|
||||
inputFormatter: utils.toHex // HERE!!!
|
||||
inputFormatter: [utils.toHex, utils.toHex],
|
||||
outputFormatter: formatters.outputTransactionFormatter
|
||||
});
|
||||
|
||||
var getTransactionCount = new Method({
|
||||
name: 'getTransactionCount',
|
||||
call: 'eth_getTransactionCount',
|
||||
params: 2,
|
||||
inputFormatter: [null, formatters.inputDefaultBlockNumberFormatter],
|
||||
outputFormatter: utils.toDecimal
|
||||
});
|
||||
|
||||
|
@ -1860,14 +1861,14 @@ var sendTransaction = new Method({
|
|||
name: 'sendTransaction',
|
||||
call: 'eth_sendTransaction',
|
||||
params: 1,
|
||||
inputFormatter: formatters.inputTransactionFormatter
|
||||
inputFormatter: [formatters.inputTransactionFormatter]
|
||||
});
|
||||
|
||||
var call = new Method({
|
||||
name: 'call',
|
||||
call: 'eth_call',
|
||||
params: 2,
|
||||
inputFormatter: formatters.inputCallFormatter
|
||||
inputFormatter: [formatters.inputCallFormatter, formatters.inputDefaultBlockNumberFormatter]
|
||||
});
|
||||
|
||||
var compileSolidity = new Method({
|
||||
|
@ -2285,16 +2286,16 @@ var utils = require('../utils/utils');
|
|||
/**
|
||||
* Should the format output to a big number
|
||||
*
|
||||
* @method outputNumberFormatter
|
||||
* @method outputBigNumberFormatter
|
||||
* @param {String|Number|BigNumber}
|
||||
* @returns {BigNumber} object
|
||||
*/
|
||||
var outputNumberFormatter = function (number) {
|
||||
var outputBigNumberFormatter = function (number) {
|
||||
return utils.toBigNumber(number);
|
||||
};
|
||||
|
||||
var inputBlockNumberFormatter = function (blockNumber) {
|
||||
return blockNumber === undefined ? "pending" : utils.toHex(blockNumber); // instead use default block number here
|
||||
var inputDefaultBlockNumberFormatter = function (blockNumber) {
|
||||
return blockNumber === undefined ? "latest" : utils.toHex(blockNumber); // instead use default block number here
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -2451,11 +2452,11 @@ var outputPostFormatter = function(post){
|
|||
};
|
||||
|
||||
module.exports = {
|
||||
inputBlockNumberFormatter: inputBlockNumberFormatter,
|
||||
inputDefaultBlockNumberFormatter: inputDefaultBlockNumberFormatter,
|
||||
inputTransactionFormatter: inputTransactionFormatter,
|
||||
inputCallFormatter: inputCallFormatter,
|
||||
inputPostFormatter: inputPostFormatter,
|
||||
outputNumberFormatter: outputNumberFormatter,
|
||||
outputBigNumberFormatter: outputBigNumberFormatter,
|
||||
outputTransactionFormatter: outputTransactionFormatter,
|
||||
outputBlockFormatter: outputBlockFormatter,
|
||||
outputLogFormatter: outputLogFormatter,
|
||||
|
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -35,10 +35,11 @@
|
|||
* {
|
||||
* name: 'getBlock',
|
||||
* call: blockCall,
|
||||
* params: 2,
|
||||
* outputFormatter: formatters.outputBlockFormatter,
|
||||
* inputFormatter: [ // can be a formatter funciton or an array of functions. Where each item in the array will be used for one parameter
|
||||
* utils.toHex, // formats paramter 1
|
||||
* function(param){ if(!param) return false; } // formats paramter 2
|
||||
* function(param){ return !!param; } // formats paramter 2
|
||||
* ]
|
||||
* },
|
||||
*
|
||||
|
@ -78,22 +79,22 @@ var getBalance = new Method({
|
|||
name: 'getBalance',
|
||||
call: 'eth_getBalance',
|
||||
params: 2,
|
||||
inputFormatter: [utils.toHex, formatters.inputBlockNumberFormatter],
|
||||
outputFormatter: formatters.inputNumberFormatter
|
||||
inputFormatter: [utils.toHex, formatters.inputDefaultBlockNumberFormatter],
|
||||
outputFormatter: formatters.outputBigNumberFormatter
|
||||
});
|
||||
|
||||
var getStorageAt = new Method({
|
||||
name: 'getStorageAt',
|
||||
call: 'eth_getStorageAt',
|
||||
params: 3,
|
||||
inputFormatter: [null, null, formatters.inputBlockNumberFormatter]
|
||||
inputFormatter: [null, utils.toHex, formatters.inputDefaultBlockNumberFormatter]
|
||||
});
|
||||
|
||||
var getCode = new Method({
|
||||
name: 'getCode',
|
||||
call: 'eth_getCode',
|
||||
params: 2,
|
||||
inputFormatter: [null, formatters.inputBlockNumberFormatter]
|
||||
inputFormatter: [null, formatters.inputDefaultBlockNumberFormatter]
|
||||
});
|
||||
|
||||
var getBlock = new Method({
|
||||
|
@ -102,14 +103,13 @@ var getBlock = new Method({
|
|||
params: 2,
|
||||
inputFormatter: [utils.toHex, function (val) { return !!val; }],
|
||||
outputFormatter: formatters.outputBlockFormatter
|
||||
|
||||
});
|
||||
|
||||
var getUncle = new Method({
|
||||
name: 'getUncle',
|
||||
call: uncleCall,
|
||||
params: 2,
|
||||
inputFormatter: [utils.toHex, utils.toHex],
|
||||
inputFormatter: [utils.toHex, utils.toHex, function (val) { return !!val; }],
|
||||
outputFormatter: formatters.outputBlockFormatter,
|
||||
|
||||
});
|
||||
|
@ -124,16 +124,16 @@ var getBlockTransactounCount = new Method({
|
|||
name: 'getBlockTransactionCount',
|
||||
call: getBlockTransactionCountCall,
|
||||
params: 1,
|
||||
outputFormatter: utils.toDecimal,
|
||||
inputFormatter: utils.toHex
|
||||
inputFormatter: [utils.toHex],
|
||||
outputFormatter: utils.toDecimal
|
||||
});
|
||||
|
||||
var getBlockUncleCount = new Method({
|
||||
name: 'getBlockUncleCount',
|
||||
call: uncleCountCall,
|
||||
params: 1,
|
||||
outputFormatter: utils.toDecimal,
|
||||
inputFormatter: utils.toHex
|
||||
inputFormatter: [utils.toHex],
|
||||
outputFormatter: utils.toDecimal
|
||||
});
|
||||
|
||||
var getTransaction = new Method({
|
||||
|
@ -147,14 +147,15 @@ var getTransactionFromBlock = new Method({
|
|||
name: 'getTransactionFromBlock',
|
||||
call: transactionFromBlockCall,
|
||||
params: 2,
|
||||
outputFormatter: formatters.outputTransactionFormatter,
|
||||
inputFormatter: utils.toHex // HERE!!!
|
||||
inputFormatter: [utils.toHex, utils.toHex],
|
||||
outputFormatter: formatters.outputTransactionFormatter
|
||||
});
|
||||
|
||||
var getTransactionCount = new Method({
|
||||
name: 'getTransactionCount',
|
||||
call: 'eth_getTransactionCount',
|
||||
params: 2,
|
||||
inputFormatter: [null, formatters.inputDefaultBlockNumberFormatter],
|
||||
outputFormatter: utils.toDecimal
|
||||
});
|
||||
|
||||
|
@ -162,14 +163,14 @@ var sendTransaction = new Method({
|
|||
name: 'sendTransaction',
|
||||
call: 'eth_sendTransaction',
|
||||
params: 1,
|
||||
inputFormatter: formatters.inputTransactionFormatter
|
||||
inputFormatter: [formatters.inputTransactionFormatter]
|
||||
});
|
||||
|
||||
var call = new Method({
|
||||
name: 'call',
|
||||
call: 'eth_call',
|
||||
params: 2,
|
||||
inputFormatter: formatters.inputCallFormatter
|
||||
inputFormatter: [formatters.inputCallFormatter, formatters.inputDefaultBlockNumberFormatter]
|
||||
});
|
||||
|
||||
var compileSolidity = new Method({
|
||||
|
|
|
@ -26,16 +26,16 @@ var utils = require('../utils/utils');
|
|||
/**
|
||||
* Should the format output to a big number
|
||||
*
|
||||
* @method outputNumberFormatter
|
||||
* @method outputBigNumberFormatter
|
||||
* @param {String|Number|BigNumber}
|
||||
* @returns {BigNumber} object
|
||||
*/
|
||||
var outputNumberFormatter = function (number) {
|
||||
var outputBigNumberFormatter = function (number) {
|
||||
return utils.toBigNumber(number);
|
||||
};
|
||||
|
||||
var inputBlockNumberFormatter = function (blockNumber) {
|
||||
return blockNumber === undefined ? "pending" : utils.toHex(blockNumber); // instead use default block number here
|
||||
var inputDefaultBlockNumberFormatter = function (blockNumber) {
|
||||
return blockNumber === undefined ? "latest" : utils.toHex(blockNumber); // instead use default block number here
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -192,11 +192,11 @@ var outputPostFormatter = function(post){
|
|||
};
|
||||
|
||||
module.exports = {
|
||||
inputBlockNumberFormatter: inputBlockNumberFormatter,
|
||||
inputDefaultBlockNumberFormatter: inputDefaultBlockNumberFormatter,
|
||||
inputTransactionFormatter: inputTransactionFormatter,
|
||||
inputCallFormatter: inputCallFormatter,
|
||||
inputPostFormatter: inputPostFormatter,
|
||||
outputNumberFormatter: outputNumberFormatter,
|
||||
outputBigNumberFormatter: outputBigNumberFormatter,
|
||||
outputTransactionFormatter: outputTransactionFormatter,
|
||||
outputBlockFormatter: outputBlockFormatter,
|
||||
outputLogFormatter: outputLogFormatter,
|
||||
|
|
|
@ -0,0 +1,58 @@
|
|||
var chai = require('chai');
|
||||
var assert = require('assert');
|
||||
var utils = require('../lib/utils/utils');
|
||||
|
||||
var getResponseStub = function () {
|
||||
return {
|
||||
jsonrpc: '2.0',
|
||||
id: 1,
|
||||
result: 0
|
||||
};
|
||||
};
|
||||
|
||||
var FakeHttpProvider = function () {
|
||||
this.response = getResponseStub();
|
||||
this.error = null;
|
||||
this.validation = null;
|
||||
};
|
||||
|
||||
FakeHttpProvider.prototype.send = function (payload) {
|
||||
assert.equal(utils.isArray(payload) || utils.isObject(payload), true);
|
||||
// TODO: validate jsonrpc request
|
||||
if (this.error) {
|
||||
throw this.error;
|
||||
}
|
||||
if (this.validation) {
|
||||
this.validation(payload);
|
||||
}
|
||||
return this.response;
|
||||
};
|
||||
|
||||
FakeHttpProvider.prototype.sendAsync = function (payload, callback) {
|
||||
assert.equal(utils.isArray(payload) || utils.isObject(payload), true);
|
||||
assert.equal(utils.isFunction(callback), true);
|
||||
if (this.validation) {
|
||||
this.validation(payload, callback);
|
||||
}
|
||||
callback(this.error, this.response);
|
||||
};
|
||||
|
||||
FakeHttpProvider.prototype.injectResponse = function (response) {
|
||||
this.response = response;
|
||||
};
|
||||
|
||||
FakeHttpProvider.prototype.injectResult = function (result) {
|
||||
this.response = getResponseStub();
|
||||
this.response.result = result;
|
||||
};
|
||||
|
||||
FakeHttpProvider.prototype.injectError = function (error) {
|
||||
this.error = error;
|
||||
};
|
||||
|
||||
FakeHttpProvider.prototype.injectValidation = function (callback) {
|
||||
this.validation = callback;
|
||||
};
|
||||
|
||||
module.exports = FakeHttpProvider;
|
||||
|
|
@ -19,7 +19,7 @@ var tests = [{
|
|||
call: 'eth_getBalance'
|
||||
}, {
|
||||
args: [0x12d],
|
||||
formattedArgs: ['0x12d', 'pending'], // here we might want to get current defaultBlock
|
||||
formattedArgs: ['0x12d', 'latest'], // here we might want to get current defaultBlock
|
||||
result: '0x31981',
|
||||
formattedResult: '0x31981',
|
||||
call: 'eth_getBalance'
|
||||
|
@ -45,7 +45,7 @@ describe('eth', function () {
|
|||
var result = web3.eth[method].apply(null, test.args.slice(0));
|
||||
|
||||
// then
|
||||
assert.equal(test.formattedResult, result);
|
||||
assert.equal(+test.formattedResult, result.toNumber());
|
||||
});
|
||||
|
||||
it('async test: ' + index, function (done) {
|
||||
|
@ -61,7 +61,7 @@ describe('eth', function () {
|
|||
};
|
||||
provider.injectValidation(validation);
|
||||
var callback = function (err, result) {
|
||||
assert.equal(test.formattedResult, result);
|
||||
assert.equal(+test.formattedResult, result.toNumber());
|
||||
done();
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue