2015-04-20 20:48:01 +00:00
|
|
|
/*
|
|
|
|
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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @file function.js
|
|
|
|
* @author Marek Kotewicz <marek@ethdev.com>
|
|
|
|
* @date 2015
|
|
|
|
*/
|
2015-04-20 16:16:15 +00:00
|
|
|
|
|
|
|
var web3 = require('../web3');
|
|
|
|
var coder = require('../solidity/coder');
|
|
|
|
var utils = require('../utils/utils');
|
|
|
|
|
2015-04-20 20:48:01 +00:00
|
|
|
/**
|
|
|
|
* This prototype should be used to call/sendTransaction to solidity functions
|
|
|
|
*/
|
2015-04-21 17:41:57 +00:00
|
|
|
var SolidityFunction = function (json, address) {
|
2015-04-20 16:16:15 +00:00
|
|
|
this._inputTypes = json.inputs.map(function (i) {
|
|
|
|
return i.type;
|
|
|
|
});
|
|
|
|
this._outputTypes = json.outputs.map(function (i) {
|
|
|
|
return i.type;
|
|
|
|
});
|
|
|
|
this._constant = json.constant;
|
2015-04-20 21:01:21 +00:00
|
|
|
this._name = utils.transformToFullName(json);
|
2015-04-21 17:41:57 +00:00
|
|
|
this._address = address;
|
2015-04-20 16:16:15 +00:00
|
|
|
};
|
|
|
|
|
2015-04-21 18:06:44 +00:00
|
|
|
/**
|
|
|
|
* Should be used to create payload from arguments
|
|
|
|
*
|
|
|
|
* @method toPayload
|
|
|
|
* @param {...} solidity function params
|
|
|
|
* @param {Object} optional payload options
|
|
|
|
*/
|
|
|
|
SolidityFunction.prototype.toPayload = function () {
|
|
|
|
var args = Array.prototype.slice.call(arguments);
|
|
|
|
var options = {};
|
2015-04-26 08:08:58 +00:00
|
|
|
if (args.length > this._inputTypes.length && utils.isObject(args[args.length -1])) {
|
2015-04-21 18:06:44 +00:00
|
|
|
options = args.pop();
|
|
|
|
}
|
|
|
|
options.to = this._address;
|
|
|
|
options.data = '0x' + this.signature() + coder.encodeParams(this._inputTypes, args);
|
|
|
|
return options;
|
|
|
|
};
|
|
|
|
|
2015-04-20 20:48:01 +00:00
|
|
|
/**
|
|
|
|
* Should be used to get function signature
|
|
|
|
*
|
|
|
|
* @method signature
|
|
|
|
* @return {String} function signature
|
|
|
|
*/
|
2015-04-20 16:16:15 +00:00
|
|
|
SolidityFunction.prototype.signature = function () {
|
|
|
|
return web3.sha3(web3.fromAscii(this._name)).slice(2, 10);
|
|
|
|
};
|
|
|
|
|
2015-04-20 20:48:01 +00:00
|
|
|
/**
|
|
|
|
* Should be used to call function
|
|
|
|
*
|
|
|
|
* @method call
|
|
|
|
* @param {Object} options
|
|
|
|
* @return {String} output bytes
|
|
|
|
*/
|
2015-04-21 18:06:44 +00:00
|
|
|
SolidityFunction.prototype.call = function () {
|
|
|
|
var payload = this.toPayload.apply(this, Array.prototype.slice.call(arguments));
|
|
|
|
var output = web3.eth.call(payload);
|
|
|
|
return coder.decodeParams(this._outputTypes, output);
|
2015-04-20 16:16:15 +00:00
|
|
|
};
|
|
|
|
|
2015-04-20 20:48:01 +00:00
|
|
|
/**
|
|
|
|
* Should be used to sendTransaction to solidity function
|
|
|
|
*
|
|
|
|
* @method sendTransaction
|
|
|
|
* @param {Object} options
|
|
|
|
*/
|
2015-04-21 18:06:44 +00:00
|
|
|
SolidityFunction.prototype.sendTransaction = function () {
|
|
|
|
var payload = this.toPayload.apply(this, Array.prototype.slice.call(arguments));
|
|
|
|
web3.eth.sendTransaction(payload);
|
2015-04-20 16:16:15 +00:00
|
|
|
};
|
|
|
|
|
2015-04-20 20:48:01 +00:00
|
|
|
/**
|
|
|
|
* Should be used to get function display name
|
|
|
|
*
|
|
|
|
* @method displayName
|
|
|
|
* @return {String} display name of the function
|
|
|
|
*/
|
2015-04-20 16:16:15 +00:00
|
|
|
SolidityFunction.prototype.displayName = function () {
|
|
|
|
return utils.extractDisplayName(this._name);
|
|
|
|
};
|
|
|
|
|
2015-04-20 20:48:01 +00:00
|
|
|
/**
|
|
|
|
* Should be used to get function type name
|
|
|
|
*
|
|
|
|
* @method typeName
|
|
|
|
* @return {String} type name of the function
|
|
|
|
*/
|
2015-04-20 16:16:15 +00:00
|
|
|
SolidityFunction.prototype.typeName = function () {
|
|
|
|
return utils.extractTypeName(this._name);
|
|
|
|
};
|
|
|
|
|
2015-04-20 20:48:01 +00:00
|
|
|
/**
|
|
|
|
* Should be called to execute function
|
|
|
|
*
|
|
|
|
* @method execute
|
|
|
|
*/
|
2015-04-21 17:41:57 +00:00
|
|
|
SolidityFunction.prototype.execute = function () {
|
|
|
|
var transaction = !this._constant;
|
|
|
|
|
2015-04-20 16:16:15 +00:00
|
|
|
// send transaction
|
|
|
|
if (transaction) {
|
2015-04-21 18:06:44 +00:00
|
|
|
return this.sendTransaction.apply(this, Array.prototype.slice.call(arguments));
|
2015-04-20 16:16:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// call
|
2015-04-21 18:06:44 +00:00
|
|
|
return this.call.apply(this, Array.prototype.slice.call(arguments));
|
2015-04-20 16:16:15 +00:00
|
|
|
};
|
|
|
|
|
2015-04-20 20:48:01 +00:00
|
|
|
/**
|
|
|
|
* Should be called to attach function to contract
|
|
|
|
*
|
|
|
|
* @method attachToContract
|
|
|
|
* @param {Contract}
|
|
|
|
*/
|
2015-04-20 16:16:15 +00:00
|
|
|
SolidityFunction.prototype.attachToContract = function (contract) {
|
2015-04-21 17:41:57 +00:00
|
|
|
var execute = this.execute.bind(this);
|
2015-04-21 18:06:44 +00:00
|
|
|
execute.call = this.call.bind(this);
|
|
|
|
execute.sendTransaction = this.sendTransaction.bind(this);
|
2015-04-20 16:16:15 +00:00
|
|
|
var displayName = this.displayName();
|
|
|
|
if (!contract[displayName]) {
|
|
|
|
contract[displayName] = execute;
|
|
|
|
}
|
2015-04-21 18:06:44 +00:00
|
|
|
contract[displayName][this.typeName()] = execute; // circular!!!!
|
2015-04-20 16:16:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = SolidityFunction;
|
|
|
|
|