web3.js 8.03 KB
Newer Older
Marian Oancea's avatar
Marian Oancea committed
1
/*
Marian Oancea's avatar
Marian Oancea committed
2
    This file is part of ethereum.js.
Marian Oancea's avatar
Marian Oancea committed
3

Marian Oancea's avatar
Marian Oancea committed
4 5 6 7
    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.
Marian Oancea's avatar
Marian Oancea committed
8

Marian Oancea's avatar
Marian Oancea committed
9 10 11 12
    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.
Marian Oancea's avatar
Marian Oancea committed
13

Marian Oancea's avatar
Marian Oancea committed
14 15
    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/>.
Marian Oancea's avatar
Marian Oancea committed
16
*/
17
/** @file web3.js
18 19 20 21
 * @authors:
 *   Jeffrey Wilcke <jeff@ethdev.com>
 *   Marek Kotewicz <marek@ethdev.com>
 *   Marian Oancea <marian@ethdev.com>
Gav Wood's avatar
Gav Wood committed
22
 *   Gav Wood <g@ethdev.com>
23 24
 * @date 2014
 */
Marian Oancea's avatar
Marian Oancea committed
25

26 27 28 29
if (process.env.NODE_ENV !== 'build') {
    var BigNumber = require('bignumber.js');
}

30 31
var utils = require('./utils');

32
/// @returns an array of objects describing web3 api methods
Gav Wood's avatar
Gav Wood committed
33 34 35 36 37 38
var web3Methods = function () {
    return [
    { name: 'sha3', call: 'web3_sha3' }
    ];
};

39
/// @returns an array of objects describing web3.eth api methods
Marek Kotewicz's avatar
Marek Kotewicz committed
40 41 42
var ethMethods = function () {
    var blockCall = function (args) {
        return typeof args[0] === "string" ? "eth_blockByHash" : "eth_blockByNumber";
Marian Oancea's avatar
Marian Oancea committed
43 44
    };

Marek Kotewicz's avatar
Marek Kotewicz committed
45
    var transactionCall = function (args) {
Gav Wood's avatar
Gav Wood committed
46
        return typeof args[0] === "string" ? 'eth_transactionByHash' : 'eth_transactionByNumber';
Marian Oancea's avatar
Marian Oancea committed
47 48
    };

Marek Kotewicz's avatar
Marek Kotewicz committed
49
    var uncleCall = function (args) {
Gav Wood's avatar
Gav Wood committed
50
        return typeof args[0] === "string" ? 'eth_uncleByHash' : 'eth_uncleByNumber';
Marian Oancea's avatar
Marian Oancea committed
51 52
    };

Marek Kotewicz's avatar
Marek Kotewicz committed
53 54 55
    var methods = [
    { name: 'balanceAt', call: 'eth_balanceAt' },
    { name: 'stateAt', call: 'eth_stateAt' },
Marek Kotewicz's avatar
Marek Kotewicz committed
56
    { name: 'storageAt', call: 'eth_storageAt' },
Marek Kotewicz's avatar
Marek Kotewicz committed
57 58 59 60 61 62 63
    { name: 'countAt', call: 'eth_countAt'},
    { name: 'codeAt', call: 'eth_codeAt' },
    { name: 'transact', call: 'eth_transact' },
    { name: 'call', call: 'eth_call' },
    { name: 'block', call: blockCall },
    { name: 'transaction', call: transactionCall },
    { name: 'uncle', call: uncleCall },
64
    { name: 'compilers', call: 'eth_compilers' },
Gav Wood's avatar
Gav Wood committed
65
    { name: 'flush', call: 'eth_flush' },
66
    { name: 'lll', call: 'eth_lll' },
67
    { name: 'solidity', call: 'eth_solidity' },
Marek Kotewicz's avatar
Marek Kotewicz committed
68 69
    { name: 'serpent', call: 'eth_serpent' },
    { name: 'logs', call: 'eth_logs' }
Marek Kotewicz's avatar
Marek Kotewicz committed
70 71 72 73
    ];
    return methods;
};

74
/// @returns an array of objects describing web3.eth api properties
Marek Kotewicz's avatar
Marek Kotewicz committed
75 76 77 78 79 80 81 82 83 84 85 86 87
var ethProperties = function () {
    return [
    { name: 'coinbase', getter: 'eth_coinbase', setter: 'eth_setCoinbase' },
    { name: 'listening', getter: 'eth_listening', setter: 'eth_setListening' },
    { name: 'mining', getter: 'eth_mining', setter: 'eth_setMining' },
    { name: 'gasPrice', getter: 'eth_gasPrice' },
    { name: 'accounts', getter: 'eth_accounts' },
    { name: 'peerCount', getter: 'eth_peerCount' },
    { name: 'defaultBlock', getter: 'eth_defaultBlock', setter: 'eth_setDefaultBlock' },
    { name: 'number', getter: 'eth_number'}
    ];
};

88
/// @returns an array of objects describing web3.db api methods
Marek Kotewicz's avatar
Marek Kotewicz committed
89 90 91 92 93 94 95 96 97
var dbMethods = function () {
    return [
    { name: 'put', call: 'db_put' },
    { name: 'get', call: 'db_get' },
    { name: 'putString', call: 'db_putString' },
    { name: 'getString', call: 'db_getString' }
    ];
};

98
/// @returns an array of objects describing web3.shh api methods
Marek Kotewicz's avatar
Marek Kotewicz committed
99 100 101 102 103 104 105 106 107 108
var shhMethods = function () {
    return [
    { name: 'post', call: 'shh_post' },
    { name: 'newIdentity', call: 'shh_newIdentity' },
    { name: 'haveIdentity', call: 'shh_haveIdentity' },
    { name: 'newGroup', call: 'shh_newGroup' },
    { name: 'addToGroup', call: 'shh_addToGroup' }
    ];
};

109
/// @returns an array of objects describing web3.eth.watch api methods
Marek Kotewicz's avatar
Marek Kotewicz committed
110 111 112
var ethWatchMethods = function () {
    var newFilter = function (args) {
        return typeof args[0] === 'string' ? 'eth_newFilterString' : 'eth_newFilter';
Marian Oancea's avatar
Marian Oancea committed
113 114
    };

Marek Kotewicz's avatar
Marek Kotewicz committed
115 116 117
    return [
    { name: 'newFilter', call: newFilter },
    { name: 'uninstallFilter', call: 'eth_uninstallFilter' },
Marek Kotewicz's avatar
Marek Kotewicz committed
118
    { name: 'getMessages', call: 'eth_filterLogs' }
Marek Kotewicz's avatar
Marek Kotewicz committed
119 120 121
    ];
};

122
/// @returns an array of objects describing web3.shh.watch api methods
Marek Kotewicz's avatar
Marek Kotewicz committed
123 124 125 126
var shhWatchMethods = function () {
    return [
    { name: 'newFilter', call: 'shh_newFilter' },
    { name: 'uninstallFilter', call: 'shh_uninstallFilter' },
127
    { name: 'getMessages', call: 'shh_getMessages' }
Marek Kotewicz's avatar
Marek Kotewicz committed
128 129 130
    ];
};

131 132
/// creates methods in a given object based on method description on input
/// setups api calls for these methods
Marek Kotewicz's avatar
Marek Kotewicz committed
133 134 135
var setupMethods = function (obj, methods) {
    methods.forEach(function (method) {
        obj[method.name] = function () {
Marek Kotewicz's avatar
Marek Kotewicz committed
136 137
            var args = Array.prototype.slice.call(arguments);
            var call = typeof method.call === 'function' ? method.call(args) : method.call;
138
            return web3.provider.send({
139 140
                method: call,
                params: args
Marek Kotewicz's avatar
Marek Kotewicz committed
141
            });
Marian Oancea's avatar
Marian Oancea committed
142
        };
Marek Kotewicz's avatar
Marek Kotewicz committed
143 144 145
    });
};

146 147
/// creates properties in a given object based on properties description on input
/// setups api calls for these properties
Marek Kotewicz's avatar
Marek Kotewicz committed
148 149 150 151
var setupProperties = function (obj, properties) {
    properties.forEach(function (property) {
        var proto = {};
        proto.get = function () {
152
            return web3.provider.send({
153
                method: property.getter
Marek Kotewicz's avatar
Marek Kotewicz committed
154 155
            });
        };
156

Marek Kotewicz's avatar
Marek Kotewicz committed
157 158
        if (property.setter) {
            proto.set = function (val) {
159
                return web3.provider.send({
160 161
                    method: property.setter,
                    params: [val]
Marian Oancea's avatar
Marian Oancea committed
162 163
                });
            };
Marek Kotewicz's avatar
Marek Kotewicz committed
164 165 166 167 168
        }
        Object.defineProperty(obj, property.name, proto);
    });
};

169
/// setups web3 object, and it's in-browser executed methods
Marek Kotewicz's avatar
Marek Kotewicz committed
170 171 172 173 174
var web3 = {
    _callbacks: {},
    _events: {},
    providers: {},

Marek Kotewicz's avatar
Marek Kotewicz committed
175
    /// @returns ascii string representation of hex value prefixed with 0x
176
    toAscii: utils.toAscii,
Marek Kotewicz's avatar
Marek Kotewicz committed
177

Marek Kotewicz's avatar
Marek Kotewicz committed
178
    /// @returns hex representation (prefixed by 0x) of ascii string
179
    fromAscii: utils.fromAscii,
Marek Kotewicz's avatar
Marek Kotewicz committed
180

Marek Kotewicz's avatar
Marek Kotewicz committed
181
    /// @returns decimal representaton of hex value prefixed by 0x
Gav Wood's avatar
Gav Wood committed
182
    toDecimal: function (val) {
Marek Kotewicz's avatar
Marek Kotewicz committed
183 184 185
        // remove 0x and place 0, if it's required
        val = val.length > 2 ? val.substring(2) : "0";
        return (new BigNumber(val, 16).toString(10));
Gav Wood's avatar
Gav Wood committed
186 187
    },

Marek Kotewicz's avatar
Marek Kotewicz committed
188
    /// @returns hex representation (prefixed by 0x) of decimal value
Gav Wood's avatar
Gav Wood committed
189
    fromDecimal: function (val) {
190
        return "0x" + (new BigNumber(val).toString(16));
Gav Wood's avatar
Gav Wood committed
191 192
    },

Marek Kotewicz's avatar
Marek Kotewicz committed
193
    /// used to transform value/string to eth string
Marek Kotewicz's avatar
Marek Kotewicz committed
194
    toEth: utils.toEth,
Gav Wood's avatar
Gav Wood committed
195

Marek Kotewicz's avatar
Marek Kotewicz committed
196
    /// eth object prototype
Marek Kotewicz's avatar
Marek Kotewicz committed
197
    eth: {
198 199 200 201 202 203 204 205 206
        contractFromAbi: function (abi) {
            return function(addr) {
                // Default to address of Config. TODO: rremove prior to genesis.
                addr = addr || '0xc6d9d2cd449a754c494264e1809c50e34d64562b';
                var ret = web3.eth.contract(addr, abi);
                ret.address = addr;
                return ret;
            };
        },
Marek Kotewicz's avatar
Marek Kotewicz committed
207 208

        /// @param filter may be a string, object or event
209 210 211
        /// @param indexed is optional, this is an object with optional event indexed params
        /// @param options is optional, this is an object with optional event options ('max'...)
        watch: function (filter, indexed, options) {
Marek Kotewicz's avatar
Marek Kotewicz committed
212
            if (filter._isEvent) {
213
                return filter(indexed, options);
Marek Kotewicz's avatar
Marek Kotewicz committed
214 215
            }
            return new web3.filter(filter, ethWatch);
Marian Oancea's avatar
Marian Oancea committed
216
        }
Marek Kotewicz's avatar
Marek Kotewicz committed
217
    },
Marian Oancea's avatar
Marian Oancea committed
218

Marek Kotewicz's avatar
Marek Kotewicz committed
219 220
    /// db object prototype
    db: {},
Marian Oancea's avatar
Marian Oancea committed
221

Marek Kotewicz's avatar
Marek Kotewicz committed
222
    /// shh object prototype
Marek Kotewicz's avatar
Marek Kotewicz committed
223
    shh: {
Marek Kotewicz's avatar
Marek Kotewicz committed
224 225 226
        
        /// @param filter may be a string, object or event
        watch: function (filter, indexed) {
Marek Kotewicz's avatar
Marek Kotewicz committed
227
            return new web3.filter(filter, shhWatch);
Marian Oancea's avatar
Marian Oancea committed
228
        }
Marek Kotewicz's avatar
Marek Kotewicz committed
229 230 231
    },
};

232
/// setups all api methods
Gav Wood's avatar
Gav Wood committed
233
setupMethods(web3, web3Methods());
234 235
setupMethods(web3.eth, ethMethods());
setupProperties(web3.eth, ethProperties());
Marek Kotewicz's avatar
Marek Kotewicz committed
236 237 238 239 240 241
setupMethods(web3.db, dbMethods());
setupMethods(web3.shh, shhMethods());

var ethWatch = {
    changed: 'eth_changed'
};
242

Marek Kotewicz's avatar
Marek Kotewicz committed
243
setupMethods(ethWatch, ethWatchMethods());
244

Marek Kotewicz's avatar
Marek Kotewicz committed
245 246 247
var shhWatch = {
    changed: 'shh_changed'
};
248

Marek Kotewicz's avatar
Marek Kotewicz committed
249 250 251 252 253 254
setupMethods(shhWatch, shhWatchMethods());

web3.setProvider = function(provider) {
    web3.provider.set(provider);
};

255 256
module.exports = web3;