abi function type

This commit is contained in:
Marek Kotewicz 2015-01-28 14:20:36 +01:00
parent 63d9c070ef
commit ea7c2fc673
8 changed files with 209 additions and 12 deletions

18
dist/ethereum.js vendored
View File

@ -66,6 +66,14 @@ var getMethodWithName = function (json, methodName) {
return json[index]; return json[index];
}; };
/// Filters all function from input abi
/// @returns abi array with filtered objects of type 'function'
var filterFunctions = function (json) {
return json.filter(function (current) {
return current.type === 'function';
});
};
/// @param string string to be padded /// @param string string to be padded
/// @param number of characters that result string should have /// @param number of characters that result string should have
/// @param sign, by default 0 /// @param sign, by default 0
@ -352,7 +360,7 @@ var methodTypeName = function (method) {
/// @returns input parser object for given json abi /// @returns input parser object for given json abi
var inputParser = function (json) { var inputParser = function (json) {
var parser = {}; var parser = {};
json.forEach(function (method) { filterFunctions(json).forEach(function (method) {
var displayName = methodDisplayName(method.name); var displayName = methodDisplayName(method.name);
var typeName = methodTypeName(method.name); var typeName = methodTypeName(method.name);
@ -375,7 +383,7 @@ var inputParser = function (json) {
/// @returns output parser for given json abi /// @returns output parser for given json abi
var outputParser = function (json) { var outputParser = function (json) {
var parser = {}; var parser = {};
json.forEach(function (method) { filterFunctions(json).forEach(function (method) {
var displayName = methodDisplayName(method.name); var displayName = methodDisplayName(method.name);
var typeName = methodTypeName(method.name); var typeName = methodTypeName(method.name);
@ -406,7 +414,8 @@ module.exports = {
methodSignature: methodSignature, methodSignature: methodSignature,
methodDisplayName: methodDisplayName, methodDisplayName: methodDisplayName,
methodTypeName: methodTypeName, methodTypeName: methodTypeName,
getMethodWithName: getMethodWithName getMethodWithName: getMethodWithName,
filterFunctions: filterFunctions
}; };
@ -464,6 +473,7 @@ var contract = function (address, desc) {
// workaround for invalid assumption that method.name is the full anonymous prototype of the method. // workaround for invalid assumption that method.name is the full anonymous prototype of the method.
// it's not. it's just the name. the rest of the code assumes it's actually the anonymous // it's not. it's just the name. the rest of the code assumes it's actually the anonymous
// prototype, so we make it so as a workaround. // prototype, so we make it so as a workaround.
// TODO: we may not want to modify input params, maybe use copy instead?
if (method.name.indexOf('(') === -1) { if (method.name.indexOf('(') === -1) {
var displayName = method.name; var displayName = method.name;
var typeName = method.inputs.map(function(i){return i.type; }).join(); var typeName = method.inputs.map(function(i){return i.type; }).join();
@ -497,7 +507,7 @@ var contract = function (address, desc) {
}); });
desc.forEach(function (method) { abi.filterFunctions(desc).forEach(function (method) {
var displayName = abi.methodDisplayName(method.name); var displayName = abi.methodDisplayName(method.name);
var typeName = abi.methodTypeName(method.name); var typeName = abi.methodTypeName(method.name);

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -65,6 +65,14 @@ var getMethodWithName = function (json, methodName) {
return json[index]; return json[index];
}; };
/// Filters all function from input abi
/// @returns abi array with filtered objects of type 'function'
var filterFunctions = function (json) {
return json.filter(function (current) {
return current.type === 'function';
});
};
/// @param string string to be padded /// @param string string to be padded
/// @param number of characters that result string should have /// @param number of characters that result string should have
/// @param sign, by default 0 /// @param sign, by default 0
@ -351,7 +359,7 @@ var methodTypeName = function (method) {
/// @returns input parser object for given json abi /// @returns input parser object for given json abi
var inputParser = function (json) { var inputParser = function (json) {
var parser = {}; var parser = {};
json.forEach(function (method) { filterFunctions(json).forEach(function (method) {
var displayName = methodDisplayName(method.name); var displayName = methodDisplayName(method.name);
var typeName = methodTypeName(method.name); var typeName = methodTypeName(method.name);
@ -374,7 +382,7 @@ var inputParser = function (json) {
/// @returns output parser for given json abi /// @returns output parser for given json abi
var outputParser = function (json) { var outputParser = function (json) {
var parser = {}; var parser = {};
json.forEach(function (method) { filterFunctions(json).forEach(function (method) {
var displayName = methodDisplayName(method.name); var displayName = methodDisplayName(method.name);
var typeName = methodTypeName(method.name); var typeName = methodTypeName(method.name);
@ -405,6 +413,7 @@ module.exports = {
methodSignature: methodSignature, methodSignature: methodSignature,
methodDisplayName: methodDisplayName, methodDisplayName: methodDisplayName,
methodTypeName: methodTypeName, methodTypeName: methodTypeName,
getMethodWithName: getMethodWithName getMethodWithName: getMethodWithName,
filterFunctions: filterFunctions
}; };

View File

@ -51,6 +51,7 @@ var contract = function (address, desc) {
// workaround for invalid assumption that method.name is the full anonymous prototype of the method. // workaround for invalid assumption that method.name is the full anonymous prototype of the method.
// it's not. it's just the name. the rest of the code assumes it's actually the anonymous // it's not. it's just the name. the rest of the code assumes it's actually the anonymous
// prototype, so we make it so as a workaround. // prototype, so we make it so as a workaround.
// TODO: we may not want to modify input params, maybe use copy instead?
if (method.name.indexOf('(') === -1) { if (method.name.indexOf('(') === -1) {
var displayName = method.name; var displayName = method.name;
var typeName = method.inputs.map(function(i){return i.type; }).join(); var typeName = method.inputs.map(function(i){return i.type; }).join();
@ -84,7 +85,7 @@ var contract = function (address, desc) {
}); });
desc.forEach(function (method) { abi.filterFunctions(desc).forEach(function (method) {
var displayName = abi.methodDisplayName(method.name); var displayName = abi.methodDisplayName(method.name);
var typeName = abi.methodTypeName(method.name); var typeName = abi.methodTypeName(method.name);

View File

@ -5,6 +5,7 @@ var clone = function (object) { return JSON.parse(JSON.stringify(object)); };
var description = [{ var description = [{
"name": "test", "name": "test",
"type": "function",
"inputs": [{ "inputs": [{
"name": "a", "name": "a",
"type": "uint256" "type": "uint256"
@ -339,10 +340,12 @@ describe('abi', function() {
// given // given
var d = [{ var d = [{
name: "test", name: "test",
type: "function",
inputs: [{ type: "int" }], inputs: [{ type: "int" }],
outputs: [{ type: "int" }] outputs: [{ type: "int" }]
},{ },{
name: "test2", name: "test2",
type: "function",
inputs: [{ type: "string" }], inputs: [{ type: "string" }],
outputs: [{ type: "string" }] outputs: [{ type: "string" }]
}]; }];
@ -775,10 +778,12 @@ describe('abi', function() {
// given // given
var d = [{ var d = [{
name: "test", name: "test",
type: "function",
inputs: [{ type: "int" }], inputs: [{ type: "int" }],
outputs: [{ type: "int" }] outputs: [{ type: "int" }]
},{ },{
name: "test2", name: "test2",
type: "function",
inputs: [{ type: "string" }], inputs: [{ type: "string" }],
outputs: [{ type: "string" }] outputs: [{ type: "string" }]
}]; }];

172
test/eth.contract.js Normal file
View File

@ -0,0 +1,172 @@
var assert = require('assert');
var contract = require('../lib/contract.js');
describe('contract', function() {
it('should create simple contract with one method from abi with explicit type name', function () {
// given
var description = [{
"name": "test(uint256)",
"type": "function",
"inputs": [{
"name": "a",
"type": "uint256"
}
],
"outputs": [
{
"name": "d",
"type": "uint256"
}
]
}];
// when
var con = contract(null, description);
// then
assert.equal('function', typeof con.test);
assert.equal('function', typeof con.test['uint256']);
});
it('should create simple contract with one method from abi with implicit type name', function () {
// given
var description = [{
"name": "test",
"type": "function",
"inputs": [{
"name": "a",
"type": "uint256"
}
],
"outputs": [
{
"name": "d",
"type": "uint256"
}
]
}];
// when
var con = contract(null, description);
// then
assert.equal('function', typeof con.test);
assert.equal('function', typeof con.test['uint256']);
});
it('should create contract with multiple methods', function () {
// given
var description = [{
"name": "test",
"type": "function",
"inputs": [{
"name": "a",
"type": "uint256"
}
],
"outputs": [
{
"name": "d",
"type": "uint256"
}
],
}, {
"name": "test2",
"type": "function",
"inputs": [{
"name": "a",
"type": "uint256"
}
],
"outputs": [
{
"name": "d",
"type": "uint256"
}
]
}];
// when
var con = contract(null, description);
// then
assert.equal('function', typeof con.test);
assert.equal('function', typeof con.test['uint256']);
assert.equal('function', typeof con.test2);
assert.equal('function', typeof con.test2['uint256']);
});
it('should create contract with overloaded methods', function () {
// given
var description = [{
"name": "test",
"type": "function",
"inputs": [{
"name": "a",
"type": "uint256"
}
],
"outputs": [
{
"name": "d",
"type": "uint256"
}
],
}, {
"name": "test",
"type": "function",
"inputs": [{
"name": "a",
"type": "string"
}
],
"outputs": [
{
"name": "d",
"type": "uint256"
}
]
}];
// when
var con = contract(null, description);
// then
assert.equal('function', typeof con.test);
assert.equal('function', typeof con.test['uint256']);
assert.equal('function', typeof con.test['string']);
});
it('should create contract with no methods', function () {
// given
var description = [{
"name": "test(uint256)",
"type": "event",
"inputs": [{
"name": "a",
"type": "uint256"
}
],
"outputs": [
{
"name": "d",
"type": "uint256"
}
]
}];
// when
var con = contract(null, description);
// then
assert.equal('undefined', typeof con.test);
});
});

0
test/eth.event.js Normal file
View File