# all files / packages/web3-eth-contract/src/ index.js

93.48% Statements 258/276
81.72% Branches 152/186
100% Functions 42/42
93.48% Lines 258/276

    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
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553
    554
    555
    556
    557
    558
    559
    560
    561
    562
    563
    564
    565
    566
    567
    568
    569
    570
    571
    572
    573
    574
    575
    576
    577
    578
    579
    580
    581
    582
    583
    584
    585
    586
    587
    588
    589
    590
    591
    592
    593
    594
    595
    596
    597
    598
    599
    600
    601
    602
    603
    604
    605
    606
    607
    608
    609
    610
    611
    612
    613
    614
    615
    616
    617
    618
    619
    620
    621
    622
    623
    624
    625
    626
    627
    628
    629
    630
    631
    632
    633
    634
    635
    636
    637
    638
    639
    640
    641
    642
    643
    644
    645
    646
    647
    648
    649
    650
    651
    652
    653
    654
    655
    656
    657
    658
    659
    660
    661
    662
    663
    664
    665
    666
    667
    668
    669
    670
    671
    672
    673
    674
    675
    676
    677
    678
    679
    680
    681
    682
    683
    684
    685
    686
    687
    688
    689
    690
    691
    692
    693
    694
    695
    696
    697
    698
    699
    700
    701
    702
    703
    704
    705
    706
    707
    708
    709
    710
    711
    712
    713
    714
    715
    716
    717
    718
    719
    720
    721
    722
    723
    724
    725
    726
    727
    728
    729
    730
    731
    732
    733
    734
    735
    736
    737
    738
    739
    740
    741
    742
    743
    744
    745
    746
    747
    748
    749
    750
    751
    752
    753
    754
    755
    756
    757
    758
    759
    760
    761
    762
    763
    764
    765
    766
    767
    768
    769
    770
    771
    772
    773
    774
    775
    776
    777
    778
    779
    780
    781
    782
    783
    784
    785
    786
    787
    788
    789
    790
    791
    792
    793
    794
    795
    796
    797
    798
    799
    800
    801
    802
    803
    804
    805
    806
    807
    808

    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×

    1×
    72×

    72×

    72×

    72×

    72×
    72×
    6×

    6×
    5×
    1×

    71×

    74×
    67×

    119×

    71×

    72×
    72×

    72×
    385×

    385×
    333×

    385×
    215×
    215×

    215×
    215×

    215×

    215×

    170×
    106×
    106×

    106×
    106×

    106×

    106×

    385×

    72×

    72×

    59×

    71×
    71×

    71×
    71×

    71×
    70×

    1×

    1×
    44×
    21×

    1×
    16×

    1×
    43×
    43×

    42×

    42×
    42×
    42×

    42×

    42×

    1×
    21×
    21×

    21×
    42×

    7×

    21×

    21×

    21×
    18×

    21×
    20×
    58×

    35×
    35×
    17×

    18×
    2×
    4×

    16×

    20×

    21×
    1×

    21×
    21×

    21×

    1×
    23×

    23×
    23×
    23×

    23×
    6×
    39×

    23×

    23×

    23×
    23×

    23×

    23×

    23×
    23×

    23×

    1×
    35×

    35×

    215×

    35×

    35×
    30×

    35×
    53×

    35×

    35×
    5×

    5×

    30×

    30×

    30×

    1×
    14×

    14×
    14×

    14×
    13×

    1×
    1×

    1×

    5×

    5×
    5×

    5×

    5×
    5×

    5×

    5×

    1×
    9×

    9×

    9×

    9×
    9×

    48×

    9×

    9×

    9×

    1×
    2×

    1×
    3×

    3×

    3×
    1×

    2×
    2×

    2×
    2×
    2×
    2×

    2×

    1×
    8×

    8×
    8×

    8×

    10×
    1×

    9×

    10×
    10×

    8×

    8×

    1×
    1×

    1×

    1×
    1×

    1×

    1×

    1×
    34×

    34×

    29×
    29×

    34×
    34×
    34×
    34×

    34×

    34×
    1×

    33×

    33×
    33×

    33×
    5×

    33×

    1×
    32×

    32×

    32×

    32×
    5×

    32×

    32×

    32×
    32×

    32×

    32×
    29×

    32×

    32×

    1×
    32×

    32×

    5×

    5×
    5×
    5×

    5×

    27×

    3×

    10×

    10×
    8×

    10×

    10×
    3×

    10×

    10×

    14×

    14×
    1×

    13×

    4×

    2×
    4×

    2×
    2×
    2×
    4×
    4×

    2×

    4×

    1×
    1×
    1×

    13×

    1×
     /*
        This file is part of web3.js.

        web3.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.

        web3.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 web3.js.  If not, see <http://www.gnu.org/licenses/>.
    */
    /**
     * @file contract.js
     *
     * To initialize a contract use:
     *
     *  var Contract = require('web3-eth-contract');
     *  Contract.prototype._eth = needsAEthInstance;
     *  var contract = new Contract(abi, address, ...);
     *
     * @author Fabian Vogelsteller <fabian@frozeman.de>
     * @date 2017
     */

    "use strict";

    var _ = require('underscore');
    var Method = require('web3-core-method');
    var utils = require('web3-utils');
    var Subscription = require('web3-core-subscriptions').subscription;
    var formatters = require('web3-core-helpers').formatters;
    var errors = require('web3-core-helpers').errors;
    var promiEvent = require('web3-core-promiEvent');
    var abi = require('web3-eth-abi');

    /**
     * Should be called to create new contract instance
     *
     * @method Contract
     * @constructor
     * @param {Array} jsonInterface
     * @param {String} address
     * @param {Object} options
     */
    var Contract = function Contract(jsonInterface, address, options) {
        var _this = this,
            args = Array.prototype.slice.call(arguments);

        Iif(!(this instanceof Contract)) {
            throw new Error('Please use the "new" keyword to instantiate a web3.eth.contract() object!');
        }

        Iif(!jsonInterface || !(jsonInterface instanceof Array)) {
            throw new Error('You must provide the json interface of the contract when instatiating a contract object.');
        }

        // create the options object
        this.options = {};

        var lastArg = args[args.length - 1];
        if(_.isObject(lastArg) && !_.isArray(lastArg)) {
            options = lastArg;

            this.options = _.extend(this.options, this._getOrSetDefaultOptions(options));
            if(_.isObject(address)) {
                address = null;
            }
        }

        // set address
        Object.defineProperty(this.options, 'address', {
            set: function(value){
                if(value) {
                    _this._address = utils.toChecksumAddress(formatters.inputAddressFormatter(value));
                }
            },
            get: function(){
                return _this._address;
            },
            enumerable: true
        });

        // add method and event signatures, when the jsonInterface gets set
        Object.defineProperty(this.options, 'jsonInterface', {
            set: function(value){
                _this.methods = {};
                _this.events = {};

                _this._jsonInterface = value.map(function(method) {
                    var func,
                        funcName;

                    if (method.name) {
                        funcName = utils._jsonInterfaceMethodToString(method);
                    }

                    // function
                    if (method.type === 'function') {
                        method.signature = abi.encodeFunctionSignature(funcName);
                        func = _this._createTxObject.bind({
                            method: method,
                            parent: _this
                        });

                        // add method only if not one already exists
                        Eif(!_this.methods[method.name])
                            _this.methods[method.name] = func;

                        // definitely add the method based on its signature
                        _this.methods[method.signature] = func;

                        // add method by name
                        _this.methods[funcName] = func;

                    // event
                    } else if (method.type === 'event') {
                        method.signature = abi.encodeEventSignature(funcName);
                        var event = _this._on.bind(_this, method.signature);

                        // add method only if not already exists
                        Eif(!_this.events[method.name] || _this.events[method.name].name === 'bound ')
                            _this.events[method.name] = event;

                        // definitely add the method based on its signature
                        _this.events[method.signature] = event;

                        // add event by name
                        _this.events[funcName] = event;
                    }

                    return method;
                });

                // add allEvents
                _this.events.allEvents = _this._on.bind(_this, 'allevents');

                return _this._jsonInterface;
            },
            get: function(){
                return _this._jsonInterface;
            },
            enumerable: true
        });

        // properties
        this.methods = {};
        this.events = {};

        this._address = null;
        this._jsonInterface = [];

        // set getter/setter properties
        this.options.address = address;
        this.options.jsonInterface = jsonInterface;

    };

    Contract.prototype._eth = {}; // eth is attached here in web3-eth/src/index.js

    /**
     * Get the callback and modiufy the array if necessary
     *
     * @method _getCallback
     * @param {Array} args
     * @return {Function} the callback
     */
    Contract.prototype._getCallback = function getCallback(args) {
        if (_.isFunction(args[args.length - 1])) {
            return args.pop(); // modify the args array!
        }
    };

    /**
     * Checks that no listener with name "newListener" or "removeListener" is added.
     *
     * @method _checkListener
     * @param {String} type
     * @param {String} event
     * @return {Object} the contract instance
     */
    Contract.prototype._checkListener = function(type, event){
        Iif(event === type) {
            throw new Error('The event "'+ type +'" is a reserved event name, you can\'t use it.');
        }
    };

    /**
     * Use default values, if options are not available
     *
     * @method _getOrSetDefaultOptions
     * @param {Object} options the options gived by the user
     * @return {Object} the options with gaps filled by defaults
     */
    Contract.prototype._getOrSetDefaultOptions = function getOrSetDefaultOptions(options) {
        var gasPrice = options.gasPrice ? String(options.gasPrice): null;
        var from = options.from ? utils.toChecksumAddress(formatters.inputAddressFormatter(options.from)) : null;

        options.data = options.data || this.options.data;

        options.from = from || this.options.from;
        options.gasPrice = gasPrice || this.options.gasPrice;
        options.gas = options.gas || options.gasLimit || this.options.gas;

        // TODO replace with only gasLimit?
        delete options.gasLimit;

        return options;
    };

    /**
     * Should be used to encode indexed params and options to one final object
     *
     * @method _encodeEventABI
     * @param {Object} event
     * @param {Object} options
     * @return {Object} everything combined together and encoded
     */
    Contract.prototype._encodeEventABI = function (event, options) {
        options = options || {};
        var filter = options.filter || {},
            result = {};

        ['fromBlock', 'toBlock'].filter(function (f) {
            return options[f] !== undefined;
        }).forEach(function (f) {
            result[f] = formatters.inputBlockNumberFormatter(options[f]);
        });

        // use given topics
        Iif(_.isArray(options.topics)) {
            result.topics = options.topics;

        // create topics based on filter
        } else {

            result.topics = [];

            // add event signature
            if (event && !event.anonymous && event.name !== 'ALLEVENTS') {
                result.topics.push(event.signature);
            }

            // add event topics (indexed arguments)
            if (event.name !== 'ALLEVENTS') {
                var indexedTopics = event.inputs.filter(function (i) {
                    return i.indexed === true;
                }).map(function (i) {
                    var value = filter[i.name];
                    if (!value) {
                        return null;
                    }

                    if (_.isArray(value)) {
                        return value.map(function (v) {
                            return abi.encodeParameter(i.type, v);
                        });
                    }
                    return abi.encodeParameter(i.type, value);
                });

                result.topics = result.topics.concat(indexedTopics);
            }

            if(!result.topics.length)
                delete result.topics;
        }

        Eif(this.options.address) {
            result.address = this.options.address.toLowerCase();
        }

        return result;
    };

    /**
     * Should be used to decode indexed params and options
     *
     * @method _decodeEventABI
     * @param {Object} data
     * @return {Object} result object with decoded indexed && not indexed params
     */
    Contract.prototype._decodeEventABI = function (data) {
        var event = this;

        data.data = data.data || '';
        data.topics = data.topics || [];
        var result = formatters.outputLogFormatter(data);

        // if allEvents get the right event
        if(event.name === 'ALLEVENTS') {
            event = event.jsonInterface.find(function (intf) {
                return (intf.signature === data.topics[0]);
            }) || {anonymous: true};
        }

        // create empty inputs if none are present (e.g. anonymous events on allEvents)
        event.inputs = event.inputs || [];

        var argTopics = event.anonymous ? data.topics : data.topics.slice(1);

        result.returnValues = abi.decodeLog(event.inputs, data.data, argTopics);
        delete result.returnValues.__length__;

        result.event = event.name;

        // move the data and topics to "raw"
        result.raw = {
            data: result.data,
            topics: result.topics
        };
        delete result.data;
        delete result.topics;

        return result;
    };

    /**
     * Encodes an ABI for a method, including signature or the method.
     * Or when constructor encodes only the constructor parameters.
     *
     * @method _encodeMethodABI
     * @param {Mixed} args the arguments to encode
     * @param {String} the encoded ABI
     */
    Contract.prototype._encodeMethodABI = function _encodeMethodABI() {
        var methodSignature = this._method.signature,
            args = this.arguments;

        var signature = false,
            paramsABI = this._parent.options.jsonInterface.filter(function (json) {
                return ((methodSignature === 'constructor' && json.type === methodSignature) ||
                    ((json.signature === methodSignature || json.signature === methodSignature.replace('0x','') || json.name === methodSignature) && json.type === 'function'));
            }).map(function (json) {
                Iif(json.inputs.length !== args.length) {
                    throw new Error('The number of arguments is not matching the methods required number. You need to pass '+ json.inputs.length +' arguments.');
                }

                if(json.type === 'function') {
                    signature = json.signature;
                }
                return json.inputs.map(function (input) {
                    return input.type;
                });
            }).map(function (types) {
                return abi.encodeParameters(types, args).replace('0x','');
            })[0] || '';

        // return constructor
        if(methodSignature === 'constructor') {
            Iif(!this._deployData)
                throw new Error('The contract has no contract data option set. This is necessary to append the constructor parameters.');

            return this._deployData + paramsABI;

        // return method
        } else {

            var returnValue = (signature) ? signature + paramsABI : paramsABI;

            Iif(!returnValue) {
                throw new Error('Couldn\'t find a matching contract method named "'+ this._method.name +'".');
            } else {
                return returnValue;
            }
        }

    };

    /**
     * Decode method return values
     *
     * @method _decodeMethodReturn
     * @param {Array} outputs
     * @param {String} returnValues
     * @return {Object} decoded output return values
     */
    Contract.prototype._decodeMethodReturn = function (outputs, returnValues) {
        Iif (!returnValues) {
            return null;
        }

        returnValues = returnValues.length >= 2 ? returnValues.slice(2) : returnValues;
        var result = abi.decodeParameters(outputs, returnValues);

        if (result.__length__ === 1) {
            return result[0];
        } else {
            delete result.__length__;
            return result;
        }
    };

    /**
     * Deploys a contract and fire events based on its state: transactionHash, receipt
     *
     * All event listeners will be removed, once the last possible event is fired ("error", or "receipt")
     *
     * @method deploy
     * @param {Object} options
     * @param {Function} callback
     * @return {Object} EventEmitter possible events are "error", "transactionHash" and "receipt"
     */
    Contract.prototype.deploy = function(options, callback){

        options = options || {};

        options.arguments = options.arguments || [];
        options = this._getOrSetDefaultOptions(options);

        // return error, if no "data" is specified
        Iif(!options.data) {
            return utils._fireError(new Error('No "data" specified in neither the given options, nor the default options.'), null, null, callback);
        }

        var constructor = _.find(this.options.jsonInterface, function (method) {
            return (method.type === 'constructor');
        }) || {};
        constructor.signature = 'constructor';

        return this._createTxObject.apply({
            method: constructor,
            parent: this,
            deployData: options.data
        }, options.arguments);

    };

    /**
     * Gets the event signature and outputformatters
     *
     * @method _generateEventOptions
     * @param {Object} event
     * @param {Object} options
     * @param {Function} callback
     * @return {Object} the event options object
     */
    Contract.prototype._generateEventOptions = function() {
        var args = Array.prototype.slice.call(arguments);

        // get the callback
        var callback = this._getCallback(args);

        // get the options
        var options = (_.isObject(args[args.length - 1])) ? args.pop() : {};

        var event = (_.isString(args[0])) ? args[0] : 'allevents';
        event = (event.toLowerCase() === 'allevents') ? {
                name: 'ALLEVENTS',
                jsonInterface: this.options.jsonInterface
            } : this.options.jsonInterface.find(function (json) {
                return (json.type === 'event' && (json.name === event || json.signature === '0x'+ event.replace('0x','')));
            });

        Iif (!event) {
            throw new Error('Event "' + event.name + '" doesn\'t exist in this contract.');
        }

        Iif (!utils.isAddress(this.options.address)) {
            throw new Error('This contract object doesn\'t have address set yet, please set an address first.');
        }

        return {
            params: this._encodeEventABI(event, options),
            event: event,
            callback: callback
        };
    };

    /**
     * Adds event listeners and creates a subscription, and remove it once its fired.
     *
     * @method clone
     * @return {Object} the event subscription
     */
    Contract.prototype.clone = function() {
        return new Contract(this.options.jsonInterface, this.options.address, this.options);
    };

    /**
     * Adds event listeners and creates a subscription, and remove it once its fired.
     *
     * @method once
     * @param {String} event
     * @param {Object} options
     * @param {Function} callback
     * @return {Object} the event subscription
     */
    Contract.prototype.once = function(event, options, callback) {
        var args = Array.prototype.slice.call(arguments);

        // get the callback
        callback = this._getCallback(args);

        if (!callback) {
            throw new Error('Once requires a callback as the second parameter.');
        }

        // don't allow fromBlock
        Eif (options)
            delete options.fromBlock;

        // don't return as once shouldn't provide "on"
        this._on(event, options, function (err, res, sub) {
            sub.unsubscribe();
            Eif(_.isFunction(callback)){
                callback(err, res, sub);
            }
        });

        return undefined;
    };

    /**
     * Adds event listeners and creates a subscription.
     *
     * @method _on
     * @param {String} event
     * @param {Object} options
     * @param {Function} callback
     * @return {Object} the event subscription
     */
    Contract.prototype._on = function(){
        var subOptions = this._generateEventOptions.apply(this, arguments);

        // prevent the event "newListener" and "removeListener" from being overwritten
        this._checkListener('newListener', subOptions.event.name, subOptions.callback);
        this._checkListener('removeListener', subOptions.event.name, subOptions.callback);

        // TODO check if listener already exists? and reuse subscription if options are the same.

        // create new subscription
        var subscription = new Subscription({
            subscription: {
                params: 1,
                inputFormatter: [formatters.inputLogFormatter],
                outputFormatter: this._decodeEventABI.bind(subOptions.event),
                // DUBLICATE, also in web3-eth
                subscriptionHandler: function (output) {
                    if(output.removed) {
                        this.emit('changed', output);
                    } else {
                        this.emit('data', output);
                    }

                    Eif (_.isFunction(this.callback)) {
                        this.callback(null, output, this);
                    }
                }
            },
            type: 'eth',
            requestManager: this._eth._requestManager
        });
        subscription.subscribe('logs', subOptions.params, subOptions.callback || function () {});

        return subscription;
    };

    /**
     * Get past events from contracts
     *
     * @method getPastEvents
     * @param {String} event
     * @param {Object} options
     * @param {Function} callback
     * @return {Object} the promievent
     */
    Contract.prototype.getPastEvents = function(){
        var subOptions = this._generateEventOptions.apply(this, arguments);

        var getPastLogs = new Method({
            name: 'getPastLogs',
            call: 'eth_getLogs',
            params: 1,
            inputFormatter: [formatters.inputLogFormatter],
            outputFormatter: this._decodeEventABI.bind(subOptions.event)
        });
        getPastLogs.setRequestManager(this._eth._requestManager);
        var call = getPastLogs.buildCall();

        getPastLogs = null;

        return call(subOptions.params, subOptions.callback);
    };

    /**
     * returns the an object with call, send, estimate functions
     *
     * @method _createTxObject
     * @returns {Object} an object with functions to call the methods
     */
    Contract.prototype._createTxObject =  function _createTxObject(){
        var txObject = {};

        if(this.method.type === 'function') {

            txObject.call = this.parent._executeMethod.bind(txObject, 'call');
            txObject.call.request = this.parent._executeMethod.bind(txObject, 'call', true); // to make batch requests

        }

        txObject.send = this.parent._executeMethod.bind(txObject, 'send');
        txObject.send.request = this.parent._executeMethod.bind(txObject, 'send', true); // to make batch requests
        txObject.encodeABI = this.parent._encodeMethodABI.bind(txObject);
        txObject.estimateGas = this.parent._executeMethod.bind(txObject, 'estimate');

        Eif (arguments.length) {

            if (arguments.length !== this.method.inputs.length) {
                throw errors.InvalidNumberOfParams(arguments.length, this.method.inputs.length, this.method.name);
            }

            txObject.arguments = arguments;
        }
        txObject._method = this.method;
        txObject._parent = this.parent;

        if(this.deployData)
            txObject._deployData = this.deployData;

        return txObject;
    };

    /**
     * Generates the options for the execute call
     *
     * @method _processExecuteArguments
     * @param {Array} args
     * @param {Promise} defer
     */
    Contract.prototype._processExecuteArguments = function _processExecuteArguments(args, defer) {
        var processedArgs = {};

        processedArgs.type = args.shift();

        // get the callback
        processedArgs.callback = this._parent._getCallback(args);

        // get block number to use for call
        if(processedArgs.type === 'call' && args[args.length - 1] !== true && (_.isString(args[args.length - 1]) || isFinite(args[args.length - 1])))
            processedArgs.defaultBlock = args.pop();

        // get the options
        processedArgs.options = (_.isObject(args[args.length - 1])) ? args.pop() : {};

        // get the generateRequest argument for batch requests
        processedArgs.generateRequest = (args[args.length - 1] === true)? args.pop() : false;

        processedArgs.options = this._parent._getOrSetDefaultOptions(processedArgs.options);
        processedArgs.options.data = this.encodeABI();

        // add contract address
        Iif(!this._deployData && !utils.isAddress(this._parent.options.address))
            throw new Error('This contract object doesn\'t have address set yet, please set an address first.');

        if(!this._deployData)
            processedArgs.options.to = this._parent.options.address;

        // return error, if no "data" is specified
        Iif(!processedArgs.options.data)
            return utils._fireError(new Error('Couldn\'t find a matching contract method, or the number of parameters is wrong.'), defer.eventEmitter, defer.reject, processedArgs.callback);

        return processedArgs;
    };

    /**
     * Executes a call, transact or estimateGas on a contract function
     *
     * @method _executeMethod
     * @param {String} type the type this execute function should execute
     * @param {Boolean} makeRequest if true, it simply returns the request parameters, rather than executing it
     */
    Contract.prototype._executeMethod = function _executeMethod(){
        var _this = this,
            args = this._parent._processExecuteArguments.call(this, Array.prototype.slice.call(arguments), defer),
            defer = promiEvent((args.type !== 'send'));

        // simple return request for batch requests
        if(args.generateRequest) {

            var payload = {
                params: [formatters.inputCallFormatter(args.options), formatters.inputDefaultBlockNumberFormatter(args.defaultBlock)],
                callback: args.callback
            };

            Eif(args.type === 'call') {
                payload.method = 'eth_call';
                payload.format = this._parent._decodeMethodReturn.bind(null, this._method.outputs);
            } else {
                payload.method = 'eth_sendTransaction';
            }

            return payload;

        } else {

            switch (args.type) {
                case 'estimate':

                    return this._parent._eth.estimateGas(args.options, args.callback);

                case 'call':

                    // TODO check errors: missing "from" should give error on deploy and send, call ?

                    this._parent._eth.call(args.options, args.defaultBlock, function (err, result) {

                        // decode result
                        if(result) {
                            result = _this._parent._decodeMethodReturn(_this._method.outputs, result);
                        }

                        // throw error
                        Iif(err) {
                            return utils._fireError(err, null, defer.reject, args.callback);
                        }

                        if(_.isFunction(args.callback)) {
                            args.callback(null, result);
                        }
                        defer.resolve(result);
                    });

                    return defer.eventEmitter;

                case 'send':

                    // return error, if no "from" is specified
                    Iif(!utils.isAddress(args.options.from)) {
                        return utils._fireError(new Error('No "from" address specified in neither the given options, nor the default options.'), defer.eventEmitter, defer.reject, args.callback);
                    }

                    if (_.isBoolean(this._method.payable) && !this._method.payable && args.options.value && args.options.value > 0) {
                        return utils._fireError(new Error('Can not send value to non-payable contract method or constructor'), defer.eventEmitter, defer.reject, args.callback);
                    }

                    // make sure receipt logs are decoded
                    var extraFormatters = {
                        receiptFormatter: function (receipt) {
                            if (_.isArray(receipt.logs)) {

                                // decode logs
                                var events = _.map(receipt.logs, function(log) {
                                    return _this._parent._decodeEventABI.call({
                                        name: 'ALLEVENTS',
                                        jsonInterface: _this._parent.options.jsonInterface
                                    }, log);
                                });

                                // make log names keys
                                receipt.events = {};
                                var count = 0;
                                events.forEach(function (ev) {
                                    Eif (ev.event) {
                                        receipt.events[ev.event] = ev;
                                    } else {
                                        receipt.events[count] = ev;
                                        count++;
                                    }
                                });

                                delete receipt.logs;
                            }
                            return receipt;
                        },
                        contractDeployFormatter: function (receipt) {
                            var newContract = _this._parent.clone();
                            newContract.options.address = receipt.contractAddress;
                            return newContract;
                        }
                    };

                    return this._parent._eth.sendTransaction.apply(extraFormatters, [args.options, args.callback]);

            }

        }

    };

    module.exports = Contract;