|
| 1 | +Smart Contracts |
| 2 | +=============== |
| 3 | + |
| 4 | +Developers have the choice of three languages for writing smart contracts: |
| 5 | + |
| 6 | +`Solidity <https://solidity.readthedocs.io/>`_ |
| 7 | + The flagship language of Ethereum, and most popular language for smart contracts. |
| 8 | + |
| 9 | +`Serpent <https://github.com/ethereum/wiki/wiki/Serpent>`_ |
| 10 | + A Python like language for writing smart contracts. |
| 11 | + |
| 12 | +LISP Like Language (LLL) |
| 13 | + A low level language, Serpent provides a superset of LLL. There's not a great deal of information |
| 14 | + for working with LLL, the following blog `/var/log/syrinx <http://blog.syrinx.net/>`_ and |
| 15 | + associated `Github <https://github.com/zigguratt/lll-resurrected>`_ is a good place to start. |
| 16 | + |
| 17 | + |
| 18 | +In order to deploy a smart contract onto the Ethereum blockchain, it must first be compiled into |
| 19 | +a bytecode format, then it can be sent as part of a transaction request as detailed in |
| 20 | +:ref:`creation-of-smart-contract`. |
| 21 | + |
| 22 | +Given that Solidity is the language of choice for writing smart contracts, it is the language |
| 23 | +supported by web3j, and is used for all subsequent examples. |
| 24 | + |
| 25 | + |
| 26 | +Getting started with Solidity |
| 27 | +----------------------------- |
| 28 | + |
| 29 | +An overview of Solidity is beyond the scope of these docs, however, the following resources are a |
| 30 | +good place to start: |
| 31 | + |
| 32 | +- `Contract Tutorial <https://github.com/ethereum/go-ethereum/wiki/Contract-Tutorial>`_ on the Go |
| 33 | + Ethereum Wiki |
| 34 | +- `Introduction to Smart Contracts <http://solidity.readthedocs.io/en/develop/introduction-to-smart-contracts.html>`_ |
| 35 | + in the Solidity project documentation |
| 36 | +- `Writing a contract <https://ethereum-homestead.readthedocs.io/en/latest/contracts-and-transactions/contracts.html#writing-a-contract>`_ |
| 37 | + in the Ethereum Homestead Guide |
| 38 | + |
| 39 | + |
| 40 | +Compiling Solidity source code |
| 41 | +------------------------------ |
| 42 | + |
| 43 | +Compilation to bytecode is performed by the Solidity compiler, *solc*. You can install the compiler, |
| 44 | +locally following the instructions as per |
| 45 | +`the project documentation <https://solidity.readthedocs.io/en/latest/installing-solidity.html>`_. |
| 46 | + |
| 47 | +To compile the solidity code run: |
| 48 | + |
| 49 | +.. code-block:: bash |
| 50 | +
|
| 51 | + $ solc <contract>.sol --bin --abi --optimize -o <output-dir>/ |
| 52 | +
|
| 53 | +**Note**: there are issues with installing solc via homebrew on OS X currently, please see the |
| 54 | +following `thread <https://github.com/ethereum/cpp-ethereum/issues/3173#issuecomment-255991056>`_ |
| 55 | +for further information. |
| 56 | + |
| 57 | +The *--bin* and *--abi* compiler arguments are both required to take full advantage of working |
| 58 | +with smart contracts from web3j. |
| 59 | + |
| 60 | +*--bin* |
| 61 | + Outputs a Solidity binary file containing the hex-encoded binary to provide with the transaction |
| 62 | + request (as per :ref:`creation-of-smart-contract`). |
| 63 | + |
| 64 | +*--abi* |
| 65 | + Outputs a solidity application binary interface (ABI) file which details all of the publicly |
| 66 | + acessible contract methods and their associated parameters. These details along with the |
| 67 | + contract address are crucial for interacting with smart contracts. The ABI file is also used for |
| 68 | + the generation of :ref:`smart-contract-wrappers`. |
| 69 | + |
| 70 | +There is also a *--gas* argument for providing estimates of the :ref:`gas` required to create a |
| 71 | +contract and transact with its methods. |
| 72 | + |
| 73 | + |
| 74 | +Alternatively, you can write and compile solidity code in your browser via the |
| 75 | +`Browser-Solidity <https://ethereum.github.io/browser-solidity/>`_. Browser-Solidity is great for |
| 76 | +smaller smart contracts, but you may run into issues working with larger contracts. |
| 77 | + |
| 78 | +You can also compile solidity code via Ethereum clients such as Geth and Parity, using the JSON-RPC |
| 79 | +method `eth_compileSolidity <https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_compilesolidity>`_ |
| 80 | +which is also supported in web3j. However, the Solidity compiler must be installed on the client |
| 81 | +for this to work. |
| 82 | + |
| 83 | +There are further options available, please refer to the |
| 84 | +`relevant section <https://ethereum-homestead.readthedocs.io/en/latest/contracts-and-transactions/contracts.html#compiling-a-contract>`_ |
| 85 | +in the Homestead documentation. |
| 86 | + |
| 87 | + |
| 88 | +Deploying and interacting with smart contracts |
| 89 | +------------------------------------- |
| 90 | + |
| 91 | +Please refer to the sections :ref:`creation-of-smart-contract`, :ref:`transacting-with-contract` |
| 92 | +and :ref:`querying-state` for details. |
| 93 | + |
| 94 | + |
| 95 | +Smart contract examples |
| 96 | +----------------------- |
| 97 | + |
| 98 | +web3j provides a number of smart contract examples in the project directory |
| 99 | +`src/test/resources/solidity <https://github.com/web3j/web3j/tree/master/src/test/resources/solidity>`_ |
| 100 | + |
| 101 | +It also provides integration tests for demonstrating the deploying and working with those smart |
| 102 | +contracts in the project directory |
| 103 | +`src/integration-test/java/org/web3j/protocol/scenarios <https://github.com/web3j/web3j/tree/master/src/integration-test/java/org/web3j/protocol/scenarios>`_. |
| 104 | + |
| 105 | + |
| 106 | +EIP-20 Ethereum token standard smart contract |
| 107 | +--------------------------------------------- |
| 108 | + |
| 109 | +There is an active `Ethereum Improvement Proposal (EIP) <https://github.com/ethereum/EIPs>`_, |
| 110 | +`EIP-20 <https://github.com/ethereum/EIPs/issues/20>`_ that has been created to define the standard |
| 111 | +functions that a smart contract providing tokens can implement. |
| 112 | + |
| 113 | +The EIP-20 proposal provides function definitions, but does not provide an implementation example. |
| 114 | +However, there is an implementation provided in |
| 115 | +`src/test/resources/solidity/contracts <https://github.com/web3j/web3j/tree/master/src/test/resources/solidity/contracts>`_, |
| 116 | +which has been taken from Consensys' implementation on |
| 117 | +`GitHub <https://github.com/ConsenSys/Tokens>`_. |
| 118 | + |
| 119 | +The integration test |
| 120 | +`HumanStandardTokenIT <https://github.com/web3j/web3j/tree/master/src/integration-test/java/org/web3j/protocol/scenarios/HumanStandardTokenIT.java>`_. |
| 121 | +has been been written to fully demonstrate the functionality of this token standard smart contract. |
| 122 | + |
| 123 | + |
| 124 | +.. _smart-contract-wrappers: |
| 125 | + |
| 126 | +Solidity smart contract wrappers |
| 127 | +-------------------------------- |
| 128 | + |
| 129 | +web3j supports the auto-generation of smart contract function wrappers in Java from Solidity ABI files. |
| 130 | + |
| 131 | +This can be achieved by running: |
| 132 | + |
| 133 | +.. code-block:: bash |
| 134 | +
|
| 135 | + org.web3j.codegen.SolidityFunctionWrapperGenerator /path/to/<smart-contract>.abi -o /path/to/src/dir/java -p com.your.organisation.name |
| 136 | +
|
| 137 | +See `FunctionWrappersIT <https://github.com/web3j/web3j/blob/master/src/integration-test/java/org/web3j/protocol/scenarios/FunctionWrappersIT.java>`_ |
| 138 | +for an example of using a generated smart contract Java wrapper. |
| 139 | + |
| 140 | +**Note:** at present the wrappers invoke smart contracts via |
| 141 | +`EthCall <https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_call>`_, |
| 142 | +so a transaction does not take place. Transaction support is imminent. |
| 143 | + |
| 144 | +For an example of how to call a smart contracts via a transaction, refer to |
| 145 | +`DeployContractIT <https://github.com/web3j/web3j/blob/master/src/integration-test/java/org/web3j/protocol/scenarios/DeployContractIT.java>`_. |
0 commit comments