/* This file is part of ethereum.js. ethereum.js is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. ethereum.js is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with ethereum.js. If not, see . */ /** * @file function.js * @author Marek Kotewicz * @date 2015 */ var web3 = require('../web3'); var coder = require('../solidity/coder'); var utils = require('../utils/utils'); /** * This prototype should be used to call/sendTransaction to solidity functions */ var SolidityFunction = function (json) { this._inputTypes = json.inputs.map(function (i) { return i.type; }); this._outputTypes = json.outputs.map(function (i) { return i.type; }); this._constant = json.constant; this._name = utils.transformToFullName(json); }; /** * Should be used to get function signature * * @method signature * @return {String} function signature */ SolidityFunction.prototype.signature = function () { return web3.sha3(web3.fromAscii(this._name)).slice(2, 10); }; /** * Should be used to call function * * @method call * @param {Object} options * @return {String} output bytes */ SolidityFunction.prototype.call = function (options) { return web3.eth.call(options); }; /** * Should be used to sendTransaction to solidity function * * @method sendTransaction * @param {Object} options */ SolidityFunction.prototype.sendTransaction = function (options) { web3.eth.sendTransaction(options); }; /** * Should be used to get function display name * * @method displayName * @return {String} display name of the function */ SolidityFunction.prototype.displayName = function () { return utils.extractDisplayName(this._name); }; /** * Should be used to get function type name * * @method typeName * @return {String} type name of the function */ SolidityFunction.prototype.typeName = function () { return utils.extractTypeName(this._name); }; /** * Should be called to execute function * * @method execute */ SolidityFunction.prototype.execute = function (contract) { var args = Array.prototype.slice.call(arguments, 1); var options = contract._options || {}; options.to = contract.address; options.data = '0x' + this.signature() + coder.encodeParams(this._inputTypes, args); var transaction = contract._isTransaction === true || (contract._isTransaction !== false && !this._constant); //reset contract._options = {}; contract._isTransaction = null; // send transaction if (transaction) { return this.sendTransaction(options); } // call var output = this.call(options); return coder.decodeParams(this._outputTypes, output); }; /** * Should be called to attach function to contract * * @method attachToContract * @param {Contract} */ SolidityFunction.prototype.attachToContract = function (contract) { var execute = this.execute.bind(this, contract); var displayName = this.displayName(); if (!contract[displayName]) { contract[displayName] = execute; } contract[displayName][this.typeName()] = this.execute.bind(this, contract); }; module.exports = SolidityFunction;