|
|
|
@ -331,15 +331,37 @@ var fromAbiOutput = function (json, methodName, output) { |
|
|
|
|
return result; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
/// @returns display name for method eg. multiply(uint256) -> multiply
|
|
|
|
|
var methodDisplayName = function (method) { |
|
|
|
|
var length = method.indexOf('(');
|
|
|
|
|
return length !== -1 ? method.substr(0, length) : method; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
/// @returns overloaded part of method's name
|
|
|
|
|
var methodTypeName = function (method) { |
|
|
|
|
/// TODO: make it not vulnerable
|
|
|
|
|
var length = method.indexOf('('); |
|
|
|
|
return length !== -1 ? method.substr(length + 1, method.length - 1 - (length + 1)) : ""; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
/// @param json abi for contract
|
|
|
|
|
/// @returns input parser object for given json abi
|
|
|
|
|
var inputParser = function (json) { |
|
|
|
|
var parser = {}; |
|
|
|
|
json.forEach(function (method) { |
|
|
|
|
parser[method.name] = function () { |
|
|
|
|
var displayName = methodDisplayName(method.name);
|
|
|
|
|
var typeName = methodTypeName(method.name); |
|
|
|
|
|
|
|
|
|
var impl = function () { |
|
|
|
|
var params = Array.prototype.slice.call(arguments); |
|
|
|
|
return toAbiInput(json, method.name, params); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
if (parser[displayName] === undefined) { |
|
|
|
|
parser[displayName] = impl; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
parser[displayName][typeName] = impl; |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
return parser; |
|
|
|
@ -350,9 +372,19 @@ var inputParser = function (json) { |
|
|
|
|
var outputParser = function (json) { |
|
|
|
|
var parser = {}; |
|
|
|
|
json.forEach(function (method) { |
|
|
|
|
parser[method.name] = function (output) { |
|
|
|
|
|
|
|
|
|
var displayName = methodDisplayName(method.name);
|
|
|
|
|
var typeName = methodTypeName(method.name); |
|
|
|
|
|
|
|
|
|
var impl = function (output) { |
|
|
|
|
return fromAbiOutput(json, method.name, output); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
if (parser[displayName] === undefined) { |
|
|
|
|
parser[displayName] = impl; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
parser[displayName][typeName] = impl; |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
return parser; |
|
|
|
@ -362,21 +394,15 @@ var outputParser = function (json) { |
|
|
|
|
/// @param method name for which we want to get method signature
|
|
|
|
|
/// @returns (promise) contract method signature for method with given name
|
|
|
|
|
var methodSignature = function (json, name) { |
|
|
|
|
var method = json[findMethodIndex(json, name)]; |
|
|
|
|
var result = name + '('; |
|
|
|
|
var inputTypes = method.inputs.map(function (inp) { |
|
|
|
|
return inp.type; |
|
|
|
|
}); |
|
|
|
|
result += inputTypes.join(','); |
|
|
|
|
result += ')'; |
|
|
|
|
|
|
|
|
|
return web3.sha3(web3.fromAscii(result)); |
|
|
|
|
return web3.sha3(web3.fromAscii(name)); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
module.exports = { |
|
|
|
|
inputParser: inputParser, |
|
|
|
|
outputParser: outputParser, |
|
|
|
|
methodSignature: methodSignature |
|
|
|
|
methodSignature: methodSignature, |
|
|
|
|
methodDisplayName: methodDisplayName, |
|
|
|
|
methodTypeName: methodTypeName |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -552,12 +578,16 @@ var contract = function (address, desc) { |
|
|
|
|
var contract = {}; |
|
|
|
|
|
|
|
|
|
desc.forEach(function (method) { |
|
|
|
|
contract[method.name] = function () { |
|
|
|
|
|
|
|
|
|
var displayName = abi.methodDisplayName(method.name); |
|
|
|
|
var typeName = abi.methodTypeName(method.name); |
|
|
|
|
|
|
|
|
|
var impl = function () { |
|
|
|
|
var params = Array.prototype.slice.call(arguments); |
|
|
|
|
var parsed = inputParser[method.name].apply(null, params); |
|
|
|
|
var parsed = inputParser[displayName][typeName].apply(null, params); |
|
|
|
|
|
|
|
|
|
var onSuccess = function (result) { |
|
|
|
|
return outputParser[method.name](result); |
|
|
|
|
return outputParser[displayName][typeName](result); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
return { |
|
|
|
@ -579,6 +609,12 @@ var contract = function (address, desc) { |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
if (contract[displayName] === undefined) { |
|
|
|
|
contract[displayName] = impl; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
contract[displayName][typeName] = impl; |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
return contract; |
|
|
|
|