diff --git a/docs/callbacks-promises-events.rst b/docs/callbacks-promises-events.rst index 6a298e8172b..51d3ef3d72c 100644 --- a/docs/callbacks-promises-events.rst +++ b/docs/callbacks-promises-events.rst @@ -12,11 +12,11 @@ we provide multiple ways to act on asynchronous functions. Most web3.js objects allow a callback as the last parameter, as well as returning promises to chain functions. Ethereum as a blockchain has different levels of finality and therefore needs to return multiple "stages" of an action. -To cope with requirement we return a "promiEvent" for functions like ``web3.eth.sendTransaction`` or contract methods. -This "promiEvent" is a promise combined with an event emitter to allow acting on different stages of action on the blockchain, like a transaction. +To cope with requirement we return a "PromiEvent" for functions like :ref:`web3.eth.sendTransaction ` or contract methods. +These stages are encapsulated into a "PromiEvent", which combines a promise with an event emitter. +The event emitter fires an event for each of the finality stages. -PromiEvents work like a normal promises with added ``on``, ``once`` and ``off`` functions. -This way developers can watch for additional events like on "receipt" or "transactionHash". +An example of a function that benefits from a PromiEvent is the :ref:`web3.eth.sendTransaction ` method. .. code-block:: javascript diff --git a/docs/conf.py b/docs/conf.py index 936c08876f9..7708bdbc2dc 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -49,8 +49,8 @@ # General information about the project. project = u'web3.js' -copyright = u'2016, Ethereum' -author = u'Fabian Vogelsteller, Marek Kotewicz, Jeffrey Wilcke, Marian Oancea, Gav Wood' +copyright = u'2019, Ethereum' +author = u'Samuel Furter, Fabian Vogelsteller, Marek Kotewicz, Jeffrey Wilcke, Marian Oancea, Gav Wood' # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the diff --git a/docs/getting-started.rst b/docs/getting-started.rst index 66fb0f09fa2..2b4865866aa 100644 --- a/docs/getting-started.rst +++ b/docs/getting-started.rst @@ -1,16 +1,15 @@ - .. include:: include_announcement.rst =============== Getting Started =============== -The web3.js library is a collection of modules which contain specific functionality for the ethereum ecosystem. +The web3.js library is a collection of modules which contain specific functionality for the Ethereum ecosystem. -- The ``web3-eth`` is for the ethereum blockchain and smart contracts +- The ``web3-eth`` is for the Ethereum blockchain and smart contracts - The ``web3-shh`` is for the whisper protocol to communicate p2p and broadcast - The ``web3-bzz`` is for the swarm protocol, the decentralized file storage -- The ``web3-utils`` contains useful helper functions for Dapp developers. +- The ``web3-utils`` contains useful helper functions for DApp developers. .. _adding-web3: @@ -19,23 +18,33 @@ Adding web3.js ============== .. index:: npm -.. index:: bower -.. index:: meteor First you need to get web3.js into your project. This can be done using the following methods: - npm: ``npm install web3`` -- meteor: ``meteor add ethereum:web3`` -- pure js: link the ``dist/web3.min.js`` After that you need to create a web3 instance and set a provider. -Ethereum supported Browsers like Mist or MetaMask will have a ``ethereumProvider`` or ``web3.currentProvider`` available. For web3.js, check ``Web3.givenProvider``. -If this property is ``null`` you should connect to a remote/local node. +A Ethereum compatible browser will have a ``window.ethereum`` or ``web3.currentProvider`` available. +For web3.js, check ``Web3.givenProvider``. If this property is ``null`` you should connect to your own local or remote node. .. code-block:: javascript // in node.js use: const Web3 = require('web3'); - const web3 = new Web3(Web3.givenProvider || "ws://localhost:8546"); + // use the given Provider, e.g in the browser with Metamask, or instantiate a new websocket provider + const web3 = new Web3(Web3.givenProvider || 'ws://localhost:8546', null, {}); + + // or + const web3 = new Web3(Web3.givenProvider || new Web3.providers.WebsocketProvider('ws://localhost:8546'), null, {}); + + // Using the IPC provider in node.js + const net = require('net'); + + const web3 = new Web3('/Users/myuser/Library/Ethereum/geth.ipc', net, {}); // mac os path + // or + const web3 = new Web3(new Web3.providers.IpcProvider('/Users/myuser/Library/Ethereum/geth.ipc', net, {})); // mac os path + // on windows the path is: '\\\\.\\pipe\\geth.ipc' + // on linux the path is: '/users/myuser/.ethereum/geth.ipc' + That's it! now you can use the ``web3`` object. diff --git a/docs/glossary.rst b/docs/glossary.rst index 281aae77151..348b189cd6e 100644 --- a/docs/glossary.rst +++ b/docs/glossary.rst @@ -5,19 +5,6 @@ Glossary ======== - - -.. _glossary-json-interface: - ------------------------------------------------------------------------------- - -json interface -===================== - -The json interface is a json object describing the `Application Binary Interface (ABI) `_ for an Ethereum smart contract. - -Using this json interface web3.js is able to create JavaScript object representing the smart contract and its methods and events using the :ref:`web3.eth.Contract object `. - ------- Specification ------- diff --git a/docs/include_package-core.rst b/docs/include_package-core.rst index 68d329a5e17..2004757f809 100644 --- a/docs/include_package-core.rst +++ b/docs/include_package-core.rst @@ -4,6 +4,8 @@ options An Web3 module does provide several options for configuring the transaction confirmation worklfow or for defining default values. These are the currently available option properties on a Web3 module: +.. _web3-module-options: + -------------- Module Options -------------- @@ -43,7 +45,7 @@ Example transactionSigner: new CustomTransactionSigner() } - const web3 = new Web3('http://localhost:8545', options); + const web3 = new Web3('http://localhost:8545', null, options); ------------------------------------------------------------------------------ @@ -59,13 +61,30 @@ defaultBlock web3.shh.defaultBlock ... -The default block which will be used for a requests. +The default block is used for all methods which have a block parameter. +You can override it by passing the block parameter if a block is required. + +Example: + +- :ref:`web3.eth.getBalance() ` +- :ref:`web3.eth.getCode() ` +- :ref:`web3.eth.getTransactionCount() ` +- :ref:`web3.eth.getStorageAt() ` +- :ref:`web3.eth.call() ` +- :ref:`new web3.eth.Contract() -> myContract.methods.myMethod().call() ` ------- Returns ------- -``string|number``: The current value of the defaultBlock property. +The ``defaultBlock`` property can return the following values: + +- ``Number``: A block number +- ``"genesis"`` - ``String``: The genesis block +- ``"latest"`` - ``String``: The latest block (current head of the blockchain) +- ``"pending"`` - ``String``: The currently mined block (including pending transactions) + +Default is ``"latest"`` ------------------------------------------------------------------------------ @@ -81,13 +100,13 @@ defaultAccount web3.shh.defaultAccount ... -The default account which will be used for a requests. +This default address is used as the default ``"from"`` property, if no ``"from"`` property is specified. ------- Returns ------- -``null|string``: The current value of the defaultAccount property. +``String`` - 20 Bytes: Any Ethereum address. You need to have the private key for that address in your node or keystore. (Default is ``undefined``) ------------------------------------------------------------------------------ @@ -148,8 +167,8 @@ transactionBlockTimeout web3.shh.transactionBlockTimeout ... -This can be used with a socket provider and defines the number of blocks until the PromiEvent -rejects with a timeout error. +The ``transactionBlockTimeout`` will be used over a socket based connection. This option does define the amount of new blocks it should wait until the first confirmation happens. +This means the PromiEvent rejects with a timeout error when the timeout got exceeded. ------- @@ -173,7 +192,6 @@ transactionConfirmationBlocks ... This defines the number of blocks it requires until a transaction will be handled as confirmed. -The PromiEvent will resolve with the desired receipt when enough confirmations happened. ------- @@ -197,8 +215,8 @@ transactionPollingTimeout web3.shh.transactionPollingTimeout ... -This defines the polling cycles amount when you send a transaction with the HttpProvider. -The PromiEvent rejects with a timeout error when the timeout got exceeded. (1 cycle == 1sec.). +The ``transactionPollingTimeout`` will be used over a HTTP connection. +This option does define the amount of polls (each second) it should wait until the first confirmation happens. ------- @@ -371,7 +389,7 @@ Will return the given provider by the (browser) environment, otherwise ``null``. Returns ------- -``Object``: The given provider set or ``null``; +``Object``: The given provider set or ``false``. ------- Example @@ -396,14 +414,14 @@ currentProvider web3.bzz.currentProvider ... -Will return the current provider, otherwise ``null``. +Will return the current provider. ------- Returns ------- -``Object``: The current provider set or ``null``; +``Object``: The current provider set. ------- Example @@ -411,7 +429,7 @@ Example .. code-block:: javascript - if(!web3.currentProvider) { + if (!web3.currentProvider) { web3.setProvider('http://localhost:8545'); } @@ -453,6 +471,6 @@ Example const contract = new web3.eth.Contract(abi, address); const batch = new web3.BatchRequest(); - batch.add(web3.eth.getBalance.request('0x0000000000000000000000000000000000000000', 'latest', callback)); - batch.add(contract.methods.balance(address).call.request({from: '0x0000000000000000000000000000000000000000'}, callback2)); - batch.execute(); + batch.add(web3.eth.getBalance.request('0x0000000000000000000000000000000000000000', 'latest')); + batch.add(contract.methods.balance(address).call.request({from: '0x0000000000000000000000000000000000000000'})); + batch.execute().then(...); diff --git a/docs/include_package-net.rst b/docs/include_package-net.rst index 0ff7df86276..ab4c6ba9949 100644 --- a/docs/include_package-net.rst +++ b/docs/include_package-net.rst @@ -30,8 +30,7 @@ Example .. code-block:: javascript - web3.eth.net.getId() - .then(console.log); + web3.eth.net.getId().then(console.log); > 1 ------------------------------------------------------------------------------ @@ -65,8 +64,7 @@ Example .. code-block:: javascript - web3.eth.isListening() - .then(console.log); + web3.eth.isListening().then(console.log); > true ------------------------------------------------------------------------------ @@ -100,6 +98,5 @@ Example .. code-block:: javascript - web3.eth.getPeerCount() - .then(console.log); + web3.eth.getPeerCount().then(console.log); > 25 diff --git a/docs/index.rst b/docs/index.rst index acd21b735ab..cd8e4a5a6e2 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -5,8 +5,8 @@ web3.js - Ethereum JavaScript API ================================= -web3.js is a collection of libraries which allow you to interact with a local or remote ethereum node, -using a HTTP or IPC connection. +web3.js is a collection of libraries which allow you to interact with a local or remote Ethereum node, +using an HTTP, WebSocket or IPC connection. The following documentation will guide you through :ref:`installing and running web3.js `, as well as providing a :ref:`API reference documentation <#id1>` with examples. diff --git a/docs/web3-bzz.rst b/docs/web3-bzz.rst index 703791b3d23..a2772e812fa 100644 --- a/docs/web3-bzz.rst +++ b/docs/web3-bzz.rst @@ -18,7 +18,7 @@ For more see the `Swarm Docs `_. import Web3 from 'web3'; import {Bzz} from 'web3-bzz'; - // will autodetect if the "ethereum" object is present and will either connect to the local swarm node, or the swarm-gateways.net. + // will autodetect if a provider is present and will either connect to the local swarm node, or the swarm-gateways.net. // Optional you can give your own provider URL; If no provider URL is given it will use "http://swarm-gateways.net" const bzz = new Bzz(Web3.givenProvider || 'http://swarm-gateways.net'); @@ -26,7 +26,7 @@ For more see the `Swarm Docs `_. // or using the web3 umbrella package const web3 = new Web3(Web3.givenProvider || 'ws://some.local-or-remote.node:8546'); - // -> web3.bzz.currentProvider // if Web3.givenProvider was an ethereum provider it will set: "http://localhost:8500" otherwise it will set: "http://swarm-gateways.net" + // -> web3.bzz.currentProvider // if Web3.givenProvider was an Ethereum provider it will set: "http://localhost:8500" otherwise it will set: "http://swarm-gateways.net" ------------------------------------------------------------------------------ diff --git a/docs/web3-eth-abi.rst b/docs/web3-eth-abi.rst index 5b9c1db7fdd..8cafdd77a69 100644 --- a/docs/web3-eth-abi.rst +++ b/docs/web3-eth-abi.rst @@ -7,7 +7,7 @@ web3.eth.abi ========= The ``web3-eth-abi`` package allows you to de- and encode parameters from a ABI (Application Binary Interface). -This will be used for function calls to the EVM (Ethereum Virtual Machine). +This will be used for calling functions of a deployed smart-contract. .. code-block:: javascript @@ -19,9 +19,9 @@ This will be used for function calls to the EVM (Ethereum Virtual Machine). // or using the web3 umbrella package - import {Web3} from 'web3'; + import Web3 from 'web3'; - const web3 = new Web3(Web3.givenProvider || 'ws://some.local-or-remote.node:8546', options); + const web3 = new Web3(Web3.givenProvider || 'ws://some.local-or-remote.node:8546', null, options); // -> web3.eth.abi diff --git a/docs/web3-eth-accounts.rst b/docs/web3-eth-accounts.rst index 96596bba4e0..a7fff7b41c4 100644 --- a/docs/web3-eth-accounts.rst +++ b/docs/web3-eth-accounts.rst @@ -8,19 +8,15 @@ web3.eth.accounts The ``web3.eth.accounts`` contains functions to generate Ethereum accounts and sign transactions and data. -.. note:: This package has NOT been audited and might potentially be unsafe. Take precautions to clear memory properly, store the private keys safely, and test transaction receiving and sending functionality properly before using in production! - -To use this package standalone use: - +.. note:: This package got NOT audited until now. Take precautions to clear memory properly, store the private keys safely, and test transaction receiving and sending functionality properly before using in production! .. code-block:: javascript - import {Accounts} from 'web3-eth-accounts; + import {Accounts} from 'web3-eth-accounts'; // Passing in the eth or web3 package is necessary to allow retrieving chainId, gasPrice and nonce automatically // for accounts.signTransaction(). - const accounts = new Accounts('ws://localhost:8546', options); - + const accounts = new Accounts('ws://localhost:8546', null, options); ------------------------------------------------------------------------------ diff --git a/docs/web3-eth-contract.rst b/docs/web3-eth-contract.rst index ec7d05711f8..68af4809ff0 100644 --- a/docs/web3-eth-contract.rst +++ b/docs/web3-eth-contract.rst @@ -6,7 +6,7 @@ web3.eth.Contract ================= -The ``web3.eth.Contract`` object makes it easy to interact with smart contracts on the ethereum blockchain. +The ``web3.eth.Contract`` object makes it easy to interact with smart contracts on the Ethereum blockchain. When you create a new contract object you give it the json interface of the respective smart contract and web3 will auto convert all calls into low level ABI calls over RPC for you. @@ -26,8 +26,7 @@ To use it standalone: options ); - contract.methods.somFunc().send({from: ....}) - .on('receipt', () => { + contract.methods.somFunc().send({from: ....}).on('receipt', () => { ... }); @@ -35,8 +34,8 @@ To use it standalone: ------------------------------------------------------------------------------ -new contract -============ +web3.eth.Contract +================= .. index:: JSON interface @@ -53,10 +52,16 @@ Parameters 1. ``jsonInterface`` - ``Array``: The json interface for the contract to instantiate 2. ``address`` - ``String`` (optional): This address is necessary for transactions and call requests and can also be added later using ``myContract.options.address = '0x1234..'.`` 3. ``options`` - ``Object`` (optional): The options of the contract. Some are used as fallbacks for calls and transactions: - * ``from`` - ``String``: The address transactions should be made from. - * ``gasPrice`` - ``String``: The gas price in wei to use for transactions. It is the wei per unit of gas. - * ``gas`` - ``Number``: The maximum gas provided for a transaction (gas limit). * ``data`` - ``String``: The byte code of the contract. Used when the contract gets :ref:`deployed `. + * ``address`` - ``String``: The address where the contract is deployed. See :ref:`address `. + * :ref:`defaultAccount ` + * :ref:`defaultBlock ` + * :ref:`defaultGas ` + * :ref:`defaultGasPrice ` + * :ref:`transactionBlockTimeout ` + * :ref:`transactionConfirmationBlocks ` + * :ref:`transactionPollingTimeout ` + * :ref:`transactionSigner ` ------- Returns @@ -72,8 +77,8 @@ Example .. code-block:: javascript const myContract = new web3.eth.Contract([...], '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe', { - from: '0x1234567890123456789012345678901234567891', // default from address - gasPrice: '20000000000' // default gas price in wei, 20 gwei in this case + defaultAccount: '0x1234567890123456789012345678901234567891', // default from address + defaultGasPrice: '20000000000' // default gas price in wei, 20 gwei in this case }); @@ -83,62 +88,27 @@ Example = Properties = ========= - ------------------------------------------------------------------------------- +.. _contract-options: options -========= - -.. code-block:: javascript - - myContract.options - -The options ``object`` for the contract instance. ``from``, ``gas`` and ``gasPrice`` are used as fallback values when sending transactions. - -------- -Properties -------- - -``Object`` - options: - -- ``address`` - ``String``: The address where the contract is deployed. See :ref:`options.address `. -- ``jsonInterface`` - ``Array``: The json interface of the contract. See :ref:`options.jsonInterface `. -- ``data`` - ``String``: The byte code of the contract. Used when the contract gets :ref:`deployed `. -- ``from`` - ``String``: The address transactions should be made from. -- ``gasPrice`` - ``String``: The gas price in wei to use for transactions.It is the wei per unit of gas. -- ``gas`` - ``Number``: The maximum gas provided for a transaction (gas limit). +======= +The contract options object has the following properties: -------- -Example -------- - -.. code-block:: javascript - - myContract.options; - > { - address: '0x1234567890123456789012345678901234567891', - jsonInterface: [...], - from: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe', - gasPrice: '10000000000000', - gas: 1000000 - } - - myContract.options.from = '0x1234567890123456789012345678901234567891'; // default from address - myContract.options.gasPrice = '20000000000000'; // default gas price in wei - myContract.options.gas = 5000000; // provide as fallback always 5M gas +- ``data`` - ``String``: The contract bytecode. +- ``address`` - ``String`` (deprecated use ``contract.address``): The address of the contract. ------------------------------------------------------------------------------ .. _contract-address: -options.address +address ========= .. code-block:: javascript - myContract.options.address + myContract.address The address used for this contract instance. All transactions generated by web3.js from this contract will contain this address as the "to". @@ -159,23 +129,23 @@ Example .. code-block:: javascript - myContract.options.address; + myContract.address; > '0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae' // set a new address - myContract.options.address = '0x1234FFDD...'; + myContract.address = '0x1234FFDD...'; ------------------------------------------------------------------------------ .. _contract-json-interface: -options.jsonInterface +jsonInterface ========= .. code-block:: javascript - myContract.options.jsonInterface + myContract.jsonInterface The :ref:`json interface ` object derived from the `ABI `_ of this contract. @@ -184,29 +154,50 @@ The :ref:`json interface ` object derived from the `ABI Property ------- -``jsonInterface`` - ``Array``: The :ref:`json interface ` for this contract. Re-setting this will regenerate the methods and events of the contract instance. - +``jsonInterface`` - ``AbiModel``: The :ref:`json interface ` for this contract. Re-setting this will regenerate the methods and events of the contract instance. -------- -Example -------- +-------- +AbiModel +-------- .. code-block:: javascript - myContract.options.jsonInterface; - > [{ - "type":"function", - "name":"foo", - "inputs": [{"name":"a","type":"uint256"}], - "outputs": [{"name":"b","type":"address"}] - },{ - "type":"event", - "name":"Event", - "inputs": [{"name":"a","type":"uint256","indexed":true},{"name":"b","type":"bytes32","indexed":false}], - }] + interface AbiModel { + getMethod(name: string): AbiItemModel | false; + getMethods(): AbiItemModel[]; + hasMethod(name: string): boolean; + getEvent(name: string): AbiItemModel | false; + getEvents(): AbiItemModel[]; + getEventBySignature(signature: string): AbiItemModel; + hasEvent(name: string): boolean; + } + + interface AbiItemModel { + name: string; + signature: string; + payable: boolean; + anonymous: boolean; + getInputLength(): Number; + getInputs(): AbiInput[]; + getIndexedInputs(): AbiInput[]; + getOutputs(): AbiOutput[]; + isOfType(): boolean; + } + + interface AbiInput { + name: string; + type: string; + indexed?: boolean; + components?: AbiInput[]; + } + + interface AbiOutput { + name: string; + type: string; + components?: AbiOutput[]; + } + - // set a new interface - myContract.options.jsonInterface = [...]; ------------------------------------------------------------------------------ @@ -246,12 +237,12 @@ Example .. code-block:: javascript - const contract1 = new eth.Contract(abi, address, {gasPrice: '12345678', from: fromAddress}); + const contract1 = new eth.Contract(abi, address, {gasPrice: '12345678', defaultAccount: fromAddress}); const contract2 = contract1.clone(); - contract2.options.address = address2; + contract2.address = address2; - (contract1.options.address !== contract2.options.address); + (contract1.address !== contract2.address); > true ------------------------------------------------------------------------------ @@ -626,12 +617,13 @@ Example .. _contract-estimateGas: methods.myMethod.estimateGas -===================== +============================ .. code-block:: javascript myContract.methods.myMethod([param1[, param2[, ...]]]).estimateGas(options[, callback]) + Will call estimate the gas a method execution will take when executed in the EVM without. The estimation can differ from the actual gas used when later sending a transaction, as the state of the smart contract can be different at that time. @@ -679,7 +671,7 @@ Example .. _contract-encodeABI: methods.myMethod.encodeABI -===================== +========================== .. code-block:: javascript @@ -783,7 +775,7 @@ Example .. _contract-events: events -===================== +====== .. code-block:: javascript @@ -871,7 +863,7 @@ Example ------------------------------------------------------------------------------ events.allEvents -===================== +================ .. code-block:: javascript @@ -885,7 +877,7 @@ Optionally the filter property can filter those events. getPastEvents -===================== +============= .. code-block:: javascript diff --git a/docs/web3-eth-ens.rst b/docs/web3-eth-ens.rst index 17bd89b5370..b6aaaf2f775 100644 --- a/docs/web3-eth-ens.rst +++ b/docs/web3-eth-ens.rst @@ -6,7 +6,7 @@ web3.eth.ens ========= -The ``web3.eth.ens`` functions let you interacting with Ens. +The ``web3.eth.ens`` functions let you interacting with the Ens smart contracts. .. code-block:: javascript @@ -17,14 +17,15 @@ The ``web3.eth.ens`` functions let you interacting with Ens. // "Web3.givenProvider" will be set if in an Ethereum supported browser. const eth = new Ens( Web3.givenProvider || 'ws://some.local-or-remote.node:8546', - new Accounts(Web3.givenProvider || 'ws://some.local-or-remote.node:8546', options), + null, options + new Accounts(Web3.givenProvider || 'ws://some.local-or-remote.node:8546', null, options) ); // or using the web3 umbrella package - const web3 = new Web3(Web3.givenProvider || 'ws://some.local-or-remote.node:8546', options); + const web3 = new Web3(Web3.givenProvider || 'ws://some.local-or-remote.node:8546', null, options); // -> web3.eth.ens diff --git a/docs/web3-eth-iban.rst b/docs/web3-eth-iban.rst index 94f5d43930b..c1c03763dc4 100644 --- a/docs/web3-eth-iban.rst +++ b/docs/web3-eth-iban.rst @@ -16,8 +16,8 @@ The ``web3.eth.Iban`` function lets convert Ethereum addresses from and to IBAN // or using the web3 umbrella package - import {Web3 } from 'web3'; - const web3 = new Web3(Web3.givenProvider || 'ws://some.local-or-remote.node:8546', options); + import Web3 from 'web3'; + const web3 = new Web3(Web3.givenProvider || 'ws://some.local-or-remote.node:8546', null, options); // -> new web3.eth.Iban('XE7338O073KYGTWWZN0F2WZ0R8PX5ZPPZS') diff --git a/docs/web3-eth-net.rst b/docs/web3-eth-net.rst index c353067e8ba..36a98b71e78 100644 --- a/docs/web3-eth-net.rst +++ b/docs/web3-eth-net.rst @@ -7,7 +7,7 @@ web3.eth.net ========= -Contains functions to receive information about the current network. +Functions to receive details about the current connected network. ------------------------------------------------------------------------------ @@ -27,7 +27,7 @@ getNetworkType Guesses the chain the node is connected by comparing the genesis hashes. -.. note:: This is not a 100% accurate guess as any private network could use testnet and mainnet genesis blocks and network IDs. +.. note:: It's recommended to use the :ref:`web3.eth.getChainId ` method to detect the currently connected chain. ------- Returns @@ -36,7 +36,9 @@ Returns ``Promise`` returns ``String``: - ``"main"`` for main network - ``"morden"`` for the morden test network - - ``"ropsten"`` for the morden test network + - ``"rinkeby"`` for the rinkeby test network + - ``"ropsten"`` for the ropsten test network + - ``"kovan"`` for the kovan test network - ``"private"`` for undetectable networks. @@ -46,8 +48,7 @@ Example .. code-block:: javascript - web3.eth.net.getNetworkType() - .then(console.log); + web3.eth.net.getNetworkType().then(console.log); > "main" diff --git a/docs/web3-eth-personal.rst b/docs/web3-eth-personal.rst index cd272a57789..089ee29e6fa 100644 --- a/docs/web3-eth-personal.rst +++ b/docs/web3-eth-personal.rst @@ -18,11 +18,11 @@ The ``web3-eth-personal`` package allows you to interact with the Ethereum node' import {Personal} from 'web3-eth-personal'; // "Web3.givenProvider" will be set if in an Ethereum supported browser. - const personal = new Personal(Web3.givenProvider || 'ws://some.local-or-remote.node:8546', options); + const personal = new Personal(Web3.givenProvider || 'ws://some.local-or-remote.node:8546', null, options); // or using the web3 umbrella package - const web3 = new Web3(Web3.givenProvider || 'ws://some.local-or-remote.node:8546', options); + const web3 = new Web3(Web3.givenProvider || 'ws://some.local-or-remote.node:8546', null, options); // -> web3.eth.personal diff --git a/docs/web3-eth.rst b/docs/web3-eth.rst index 34d5ad5d6ff..46682f75949 100644 --- a/docs/web3-eth.rst +++ b/docs/web3-eth.rst @@ -6,7 +6,7 @@ web3.eth ======== -The ``web3-eth`` package allows you to interact with an Ethereum blockchain and Ethereum smart contracts. +The ``web3-eth`` package allows you to interact with an Ethereum blockchain itself and the deployed smart contracts. .. code-block:: javascript @@ -15,12 +15,12 @@ The ``web3-eth`` package allows you to interact with an Ethereum blockchain and import {Eth} from 'web3-eth'; // "Web3.givenProvider" will be set if in an Ethereum supported browser. - const eth = new Eth(Web3.givenProvider || 'ws://some.local-or-remote.node:8546', options); + const eth = new Eth(Web3.givenProvider || 'ws://some.local-or-remote.node:8546', null, options); // or using the web3 umbrella package - const web3 = new Web3(Web3.givenProvider || 'ws://some.local-or-remote.node:8546', options); + const web3 = new Web3(Web3.givenProvider || 'ws://some.local-or-remote.node:8546', null, options); // -> web3.eth @@ -120,96 +120,6 @@ For ``web3.eth.net`` see the :ref:`net reference documentation ` .. include:: include_package-core.rst - ------------------------------------------------------------------------------- - -.. _eth-defaultaccount: - -defaultAccount -===================== - -.. code-block:: javascript - - web3.eth.defaultAccount - -This default address is used as the default ``"from"`` property, if no ``"from"`` property is specified in for the following methods: - -- :ref:`web3.eth.sendTransaction() ` -- :ref:`web3.eth.call() ` -- :ref:`new web3.eth.Contract() -> myContract.methods.myMethod().call() ` -- :ref:`new web3.eth.Contract() -> myContract.methods.myMethod().send() ` - --------- -Property --------- - - -``String`` - 20 Bytes: Any ethereum address. You should have the private key for that address in your node or keystore. (Default is ``undefined``) - - -------- -Example -------- - - -.. code-block:: javascript - - web3.eth.defaultAccount; - > undefined - - // set the default account - web3.eth.defaultAccount = '0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe'; - - ------------------------------------------------------------------------------- - -.. _eth-defaultblock: - -defaultBlock -===================== - -.. code-block:: javascript - - web3.eth.defaultBlock - -The default block is used for certain methods. You can override it by passing in the defaultBlock as last parameter. -The default value is "latest". - -- :ref:`web3.eth.getBalance() ` -- :ref:`web3.eth.getCode() ` -- :ref:`web3.eth.getTransactionCount() ` -- :ref:`web3.eth.getStorageAt() ` -- :ref:`web3.eth.call() ` -- :ref:`new web3.eth.Contract() -> myContract.methods.myMethod().call() ` - ----------- -Property ----------- - - -Default block parameters can be one of the following: - -- ``Number``: A block number -- ``"genesis"`` - ``String``: The genesis block -- ``"latest"`` - ``String``: The latest block (current head of the blockchain) -- ``"pending"`` - ``String``: The currently mined block (including pending transactions) - -Default is ``"latest"`` - - -------- -Example -------- - -.. code-block:: javascript - - web3.eth.defaultBlock; - > "latest" - - // set the default block - web3.eth.defaultBlock = 231; - - ------------------------------------------------------------------------------ getProtocolVersion @@ -219,7 +129,7 @@ getProtocolVersion web3.eth.getProtocolVersion([callback]) -Returns the ethereum protocol version of the node. +Returns the Ethereum protocol version of the node. ------- Returns @@ -234,8 +144,7 @@ Example .. code-block:: javascript - web3.eth.getProtocolVersion() - .then(console.log); + web3.eth.getProtocolVersion().then(console.log); > "63" @@ -290,7 +199,7 @@ getCoinbase .. code-block:: javascript - getCoinbase([callback]) + web3.eth.getCoinbase([callback]) Returns the coinbase address to which mining rewards will go. @@ -307,8 +216,7 @@ Example .. code-block:: javascript - web3.eth.getCoinbase() - .then(console.log); + web3.eth.getCoinbase().then(console.log); > "0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe" @@ -337,8 +245,7 @@ Example .. code-block:: javascript - web3.eth.isMining() - .then(console.log); + web3.eth.isMining().then(console.log); > true @@ -366,8 +273,7 @@ Example .. code-block:: javascript - web3.eth.getHashrate() - .then(console.log); + web3.eth.getHashrate().then(console.log); > 493736 @@ -403,8 +309,7 @@ Example .. code-block:: javascript - web3.eth.getGasPrice() - .then(console.log); + web3.eth.getGasPrice().then(console.log); > "20000000000" @@ -419,8 +324,8 @@ getAccounts web3.eth.getAccounts([callback]) -Returns a list of accounts the node controls by using the provider and calling the RPC method ``eth_accounts``. -If there are unlocked local accounts then it will return them instead of sending a request to the node. +Will return a list of the unlocked accounts in the Web3 wallet or it will return the accounts from the currently connected node. + This means you can add accounts with :ref:`web3.eth.accounts.create() ` and you will get them returned here. ------- @@ -437,8 +342,7 @@ Example .. code-block:: javascript - web3.eth.getAccounts() - .then(console.log); + web3.eth.getAccounts().then(console.log); > ["0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe", "0xDCc6960376d6C6dEa93647383FfB245CfCed97Cf"] @@ -466,8 +370,7 @@ Example .. code-block:: javascript - web3.eth.getBlockNumber() - .then(console.log); + web3.eth.getBlockNumber().then(console.log); > 2744 @@ -508,8 +411,7 @@ Example .. code-block:: javascript - web3.eth.getBalance("0x407d73d8a49eeb85d32cf465507dd71d507100c1") - .then(console.log); + web3.eth.getBalance("0x407d73d8a49eeb85d32cf465507dd71d507100c1").then(console.log); > "1000000000000" @@ -549,8 +451,7 @@ Example .. code-block:: javascript - web3.eth.getStorageAt("0x407d73d8a49eeb85d32cf465507dd71d507100c1", 0) - .then(console.log); + web3.eth.getStorageAt("0x407d73d8a49eeb85d32cf465507dd71d507100c1", 0).then(console.log); > "0x033456732123ffff2342342dd12342434324234234fd234fd23fd4f23d4234" @@ -589,8 +490,7 @@ Example .. code-block:: javascript - web3.eth.getCode("0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8") - .then(console.log); + web3.eth.getCode("0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8").then(console.log); > "0x600160008035811a818181146012578301005b601b6001356025565b8060005260206000f25b600060078202905091905056" @@ -648,9 +548,7 @@ Example .. code-block:: javascript - web3.eth.getBlock(3150) - .then(console.log); - + web3.eth.getBlock(3150).then(console.log); > { "number": 3, "hash": "0xef95f2f1ed3ca60b048b4bf67cde2195961e0bba6f70bcbea9a2c4e133e34b46", @@ -709,8 +607,7 @@ Example .. code-block:: javascript - web3.eth.getBlockTransactionCount("0x407d73d8a49eeb85d32cf465507dd71d507100c1") - .then(console.log); + web3.eth.getBlockTransactionCount("0x407d73d8a49eeb85d32cf465507dd71d507100c1").then(console.log); > 1 @@ -750,8 +647,7 @@ Example .. code-block:: javascript - web3.eth.getUncle(500, 0) - .then(console.log); + web3.eth.getUncle(500, 0).then(console.log); > // see web3.eth.getBlock ------------------------------------------------------------------------------ @@ -801,9 +697,7 @@ Example .. code-block:: javascript - web3.eth.getTransaction('0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b§234') - .then(console.log); - + web3.eth.getTransaction('0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b§234').then(console.log); > { "hash": "0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b", "nonce": 2, @@ -853,8 +747,7 @@ Example .. code-block:: javascript - const transaction = web3.eth.getTransactionFromBlock('0x4534534534', 2) - .then(console.log); + const transaction = web3.eth.getTransactionFromBlock('0x4534534534', 2).then(console.log); > // see web3.eth.getTransaction ------------------------------------------------------------------------------ @@ -907,8 +800,7 @@ Example .. code-block:: javascript const receipt = web3.eth.getTransactionReceipt('0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b') - .then(console.log); - + .then(console.log); > { "status": true, "transactionHash": "0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b", @@ -959,8 +851,7 @@ Example .. code-block:: javascript - web3.eth.getTransactionCount("0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe") - .then(console.log); + web3.eth.getTransactionCount("0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe").then(console.log); > 1 ------------------------------------------------------------------------------ @@ -1168,10 +1059,10 @@ signTransaction .. code-block:: javascript - web3.eth.signTransaction(transactionObject [, callback]) + web3.eth.signTransaction(transactionObject [, address,] [, callback]) -The method ``signTransaction`` signs a transaction with the private key of the given address. -This method does only work if you're connected to a Parity node. +Signs a transaction with the private key of the given address. +If the given address is a local unlocked account, the transaction will be signed locally. ---------- Parameters @@ -1179,6 +1070,7 @@ Parameters 1. ``Object`` - The transaction data to sign :ref:`web3.eth.sendTransaction() ` for more. +1. ``string`` - The address of the account. 3. ``Function`` - (optional) Optional callback, returns an error object as first parameter and the result as second. @@ -1258,8 +1150,7 @@ Example web3.eth.call({ to: "0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe", // contract address data: "0xc6888fa10000000000000000000000000000000000000000000000000000000000000003" - }) - .then(console.log); + }).then(console.log); > "0x000000000000000000000000000000000000000000000000000000000000000a" ------------------------------------------------------------------------------ @@ -1298,8 +1189,7 @@ Example web3.eth.estimateGas({ to: "0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe", data: "0xc6888fa10000000000000000000000000000000000000000000000000000000000000003" - }) - .then(console.log); + }).then(console.log); > "0x0000000000000000000000000000000000000000000000000000000000000015" ------------------------------------------------------------------------------ @@ -1352,9 +1242,7 @@ Example web3.eth.getPastLogs({ address: "0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe", topics: ["0x033456732123ffff2342342dd12342434324234234fd234fd23fd4f23d4234"] - }) - .then(console.log); - + }).then(console.log); > [{ data: '0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385', topics: ['0xfd43ade1c09fade1c0d57a7af66ab4ead7c2c2eb7b11a91ffdd57a7af66ab4ead7', '0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385'] @@ -1401,8 +1289,7 @@ Example .. code-block:: javascript - web3.eth.getWork() - .then(console.log); + web3.eth.getWork().then(console.log); > [ "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef", "0x5EED00000000000000000000000000005EED0000000000000000000000000000", @@ -1488,3 +1375,59 @@ Example > ['0aae0B295369a9FD31d5F28D9Ec85E40f4cb692BAf', 0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe] ------------------------------------------------------------------------------ + +.. _eth-chainId: + +getChainId +========== + +.. code-block:: javascript + + web3.eth.getChainId([callback]) + +Returns the chain ID of the current connected node as described in the `EIP-695 `_. + +------- +Returns +------- + +``Promise`` - Returns chain ID. + +------- +Example +------- + + +.. code-block:: javascript + + web3.eth.getChainId().then(console.log); + > 61 + +------------------------------------------------------------------------------ + +.. _eth-getNodeInfo: + +getNodeInfo +========== + +.. code-block:: javascript + + web3.eth.getNodeInfo([callback]) + +------- +Returns +------- + +``Promise`` - The current client version. + +------- +Example +------- + + +.. code-block:: javascript + + web3.eth.getNodeInfo().then(console.log); + > "Mist/v0.9.3/darwin/go1.4.1" + +------------------------------------------------------------------------------ diff --git a/docs/web3-net.rst b/docs/web3-net.rst index ef64c4fabbf..52ec34f8074 100644 --- a/docs/web3-net.rst +++ b/docs/web3-net.rst @@ -16,11 +16,11 @@ The ``web3-net`` package allows you to interact with the Ethereum nodes network import {Net} from 'web3-net'; // "Personal.providers.givenProvider" will be set if in an Ethereum supported browser. - const net = new Net(Web3.givenProvider || 'ws://some.local-or-remote.node:8546', options); + const net = new Net(Web3.givenProvider || 'ws://some.local-or-remote.node:8546', null, options); // or using the web3 umbrella package - const web3 = new Web3(Web3.givenProvider || 'ws://some.local-or-remote.node:8546', options); + const web3 = new Web3(Web3.givenProvider || 'ws://some.local-or-remote.node:8546', null, options); // -> web3.eth.net // -> web3.bzz.net diff --git a/docs/web3-shh.rst b/docs/web3-shh.rst index 5e7b72b08b2..efbf2047173 100644 --- a/docs/web3-shh.rst +++ b/docs/web3-shh.rst @@ -17,11 +17,11 @@ For more see `Whisper Overview web3.shh @@ -117,8 +117,7 @@ Example .. code-block:: javascript - web3.shh.getInfo() - .then(console.log); + web3.shh.getInfo().then(console.log); > { "minPow": 0.8, "maxMessageSize": 12345, diff --git a/docs/web3.rst b/docs/web3.rst index 935885afac6..463db2e64a6 100644 --- a/docs/web3.rst +++ b/docs/web3.rst @@ -9,10 +9,27 @@ Web3 .. code-block:: javascript - import {Web3} from 'web3'; + new Web3(provider, net, options); + +---------- +Parameters +---------- + +1. ``provider`` - ``string|object``: A URL or one of the Web3 provider classes. +2. ``net`` - ``net.Socket`` (optional): The net NodeJS package. +3. ``options`` - ``object`` (optional) The Web3 :ref:`options ` + + +------- +Example +------- + +.. code-block:: javascript + + import Web3 from 'web3'; // "Web3.providers.givenProvider" will be set if in an Ethereum supported browser. - const web3 = new Web3(Web3.givenProvider || 'ws://some.local-or-remote.node:8546'); + const web3 = new Web3(Web3.givenProvider || 'ws://some.local-or-remote.node:8546', net, options); > web3.eth > web3.shh @@ -25,13 +42,7 @@ Web3 Web3.modules ===================== - Static property of the Web3 class - -.. code-block:: javascript - - Web3.modules - - Will return an object with the classes of all major sub modules, to be able to instantiate them manually. + This Static property will return an object with the classes of all major sub modules, to be able to instantiate them manually. ------- Returns @@ -52,11 +63,11 @@ Example Web3.modules > { - Eth: Eth function(provider, options?, net?), - Net: Net function(provider, options?, net?), - Personal: Personal function(provider, options?, net?), - Shh: Shh function(provider, options?, net?), - Bzz: Bzz function(provider, options?, net?), + Eth(provider, net?, options?), + Net(provider, net?, options?), + Personal(provider, net?, options?), + Shh(provider, net?, options?), + Bzz(provider, net?, options?), } @@ -73,7 +84,6 @@ version .. code-block:: javascript - Web3.version web3.version Contains the version of the ``web3`` wrapper class. diff --git a/packages/web3-eth-contract/src/models/AbiModel.js b/packages/web3-eth-contract/src/models/AbiModel.js index 56056433c5b..fc8f1f4f5d9 100644 --- a/packages/web3-eth-contract/src/models/AbiModel.js +++ b/packages/web3-eth-contract/src/models/AbiModel.js @@ -47,6 +47,17 @@ export default class AbiModel { return false; } + /** + * Returns all methods from this AbiModel + * + * @method getMethods + * + * @returns {Object} + */ + getMethods() { + return this.abi.methods; + } + /** * Checks if the event exists and returns it otherwise it will return false * diff --git a/packages/web3-eth-contract/tests/src/models/AbiModelTest.js b/packages/web3-eth-contract/tests/src/models/AbiModelTest.js index 60242f385ae..a03a700d5a1 100644 --- a/packages/web3-eth-contract/tests/src/models/AbiModelTest.js +++ b/packages/web3-eth-contract/tests/src/models/AbiModelTest.js @@ -25,6 +25,12 @@ describe('AbiModelTest', () => { expect(abiModel.getMethod('my_method')).toEqual(true); }); + it('calls getMethods and returns the expected object', () => { + abiModel.abi.methods['my_method'] = true; + + expect(abiModel.getMethods()).toHaveProperty('my_method', true); + }); + it('calls getMethod and returns false', () => { expect(abiModel.getMethod('my_method')).toEqual(false); }); @@ -42,7 +48,7 @@ describe('AbiModelTest', () => { it('calls getEvents and returns the expected object', () => { abiModel.abi.events['my_event'] = true; - expect(abiModel.getEvents('my_event')).toHaveProperty('my_event', true); + expect(abiModel.getEvents()).toHaveProperty('my_event', true); }); it('calls getEventBySignature and returns the expected object', () => { diff --git a/packages/web3-eth-contract/types/index.d.ts b/packages/web3-eth-contract/types/index.d.ts index 961933ca827..a47ac0f851c 100644 --- a/packages/web3-eth-contract/types/index.d.ts +++ b/packages/web3-eth-contract/types/index.d.ts @@ -19,17 +19,20 @@ import BN = require('bn.js'); import {provider} from 'web3-providers'; -import {AbiItem} from 'web3-utils'; +import {AbiInput, AbiOutput, AbiItem} from 'web3-utils'; import {PromiEvent} from 'web3-core'; export class Contract { constructor( provider: provider, - jsonInterface: AbiItem[] | AbiItem, + abi: AbiItem[], address?: string, options?: ContractOptions ) + address: string; + jsonInterface: AbiModel; + options: Options; clone(): Contract; @@ -49,15 +52,12 @@ export class Contract { getPastEvents(event: string, callback: (error: Error, event: EventData) => void): Promise; } -export class ContractModuleFactory { } // TODO: Define methods +export class ContractModuleFactory { +} // TODO: Define methods export interface Options { address: string; - jsonInterface: AbiItem[]; data: string; - from: string; - gasPrice: string; - gas: number; } export interface DeployOptions { @@ -125,3 +125,36 @@ export interface EventData { blockNumber: number; address: string; } + +export interface AbiModel { + getMethod(name: string): AbiItemModel | false; + + getMethods(): AbiItemModel[]; + + hasMethod(name: string): boolean; + + getEvent(name: string): AbiItemModel | false; + + getEvents(): AbiItemModel[]; + + getEventBySignature(signature: string): AbiItemModel; + + hasEvent(name: string): boolean; +} + +export interface AbiItemModel { + signature: string; + name: string; + payable: boolean; + anonymous: boolean; + + getInputLength(): number; + + getInputs(): AbiInput[]; + + getIndexedInputs(): AbiInput[]; + + getOutputs(): AbiOutput[]; + + isOfType(): boolean; +} diff --git a/packages/web3-eth-contract/types/tests/contract-test.ts b/packages/web3-eth-contract/types/tests/contract-test.ts index c6c43c1b0cb..c0751331835 100644 --- a/packages/web3-eth-contract/types/tests/contract-test.ts +++ b/packages/web3-eth-contract/types/tests/contract-test.ts @@ -19,7 +19,19 @@ import {Contract} from 'web3-eth-contract'; -const contract = new Contract('http://localhost:500', {type: 'constructor'}); +const contract = new Contract('http://localhost:500', []); + +// $ExpectType string +contract.address; + +// $ExpectType string +contract.options.address; + +// $ExpectType string +contract.options.data; + +// $ExpectType AbiModel +contract.jsonInterface; // $ExpectType Contract contract.clone();