Commit ea7c2fc6 authored by Marek Kotewicz's avatar Marek Kotewicz

abi function type

parent 63d9c070
...@@ -66,6 +66,14 @@ var getMethodWithName = function (json, methodName) { ...@@ -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) { ...@@ -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) { ...@@ -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 = { ...@@ -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) { ...@@ -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) { ...@@ -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);
......
This diff is collapsed.
This diff is collapsed.
...@@ -65,6 +65,14 @@ var getMethodWithName = function (json, methodName) { ...@@ -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) { ...@@ -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) { ...@@ -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 = { ...@@ -405,6 +413,7 @@ module.exports = {
methodSignature: methodSignature, methodSignature: methodSignature,
methodDisplayName: methodDisplayName, methodDisplayName: methodDisplayName,
methodTypeName: methodTypeName, methodTypeName: methodTypeName,
getMethodWithName: getMethodWithName getMethodWithName: getMethodWithName,
filterFunctions: filterFunctions
}; };
...@@ -51,6 +51,7 @@ var contract = function (address, desc) { ...@@ -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) { ...@@ -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);
......
...@@ -5,6 +5,7 @@ var clone = function (object) { return JSON.parse(JSON.stringify(object)); }; ...@@ -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() { ...@@ -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() { ...@@ -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" }]
}]; }];
......
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);
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment