Code coverage report for ethereum.js/lib/web3/function.js

Statements: 100% (46 / 46)      Branches: 100% (6 / 6)      Functions: 100% (11 / 11)      Lines: 100% (46 / 46)      Ignored: none     

All files » ethereum.js/lib/web3/ » function.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150                                            1 1 1         1 20 27   20 13   20 20 20                   1 6 6 6 4   6 6 6                 1 6                   1 3 3 3                 1 3 3                 1 20                 1 20               1 4     4 2       2                 1 20 20 20 20 20 19   20     1    
/*
    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
 */
 
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, address) {
    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);
    this._address = address;
};
 
/**
 * 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 = {};
    if (utils.isObject(args[args.length -1])) {
        options = args.pop();
    }
    options.to = this._address;
    options.data = '0x' + this.signature() + coder.encodeParams(this._inputTypes, args);
    return options;
};
 
/**
 * 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 () {
    var payload = this.toPayload.apply(this, Array.prototype.slice.call(arguments));
    var output = web3.eth.call(payload);
    return coder.decodeParams(this._outputTypes, output);
};
 
/**
 * Should be used to sendTransaction to solidity function
 *
 * @method sendTransaction
 * @param {Object} options
 */
SolidityFunction.prototype.sendTransaction = function () {
    var payload = this.toPayload.apply(this, Array.prototype.slice.call(arguments));
    web3.eth.sendTransaction(payload);
};
 
/**
 * 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 () {
    var transaction = !this._constant;
    
    // send transaction
    if (transaction) {
        return this.sendTransaction.apply(this, Array.prototype.slice.call(arguments));
    }
 
    // call
    return this.call.apply(this, Array.prototype.slice.call(arguments));
};
 
/**
 * Should be called to attach function to contract
 *
 * @method attachToContract
 * @param {Contract}
 */
SolidityFunction.prototype.attachToContract = function (contract) {
    var execute = this.execute.bind(this);
    execute.call = this.call.bind(this);
    execute.sendTransaction = this.sendTransaction.bind(this);
    var displayName = this.displayName();
    if (!contract[displayName]) {
        contract[displayName] = execute;
    }
    contract[displayName][this.typeName()] = execute; // circular!!!!
};
 
module.exports = SolidityFunction;