Skip to content

Commit

Permalink
(closes #2020) Update the contract deployment docs to use py-solc-x.
Browse files Browse the repository at this point in the history
- Modernization of contract deployment docs. Use py-solc-x to download and install precompiled solidity compiler solc since py solc library is no longer maintained.
  • Loading branch information
fselmo committed Jun 14, 2021
1 parent f4add46 commit 588e0c0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 43 deletions.
88 changes: 45 additions & 43 deletions docs/contracts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,68 +16,70 @@ Contract Deployment Example

To run this example, you will need to install a few extra features:

- The sandbox node provided by eth-tester. You can install it with ``pip install -U web3[tester]``.
- The ``solc`` solidity compiler. See `Installing the Solidity Compiler
<http://solidity.readthedocs.io/en/latest/installing-solidity.html#binary-packages>`_
- The sandbox node provided by eth-tester. You can install it with:

.. code-block:: bash
$ pip install -U "web3[tester]"
- ``py-solc-x``. This is the supported route to installing the solidity compiler ``solc``. You can install it with:

.. code-block:: bash
$ pip install py-solc-x
After ``py-solc-x`` is installed, you will need to install a version of ``solc``. You can install the latest version via a new REPL with:

.. code-block:: python
>>> import json
>>> from solcx import install_solc
>>> install_solc(version='latest')
You should now be set up to run the contract deployment example below:

.. code-block:: python
>>> from web3 import Web3
>>> from solc import compile_standard
>>> from solcx import compile_source
# Solidity source code
>>> compiled_sol = compile_standard({
... "language": "Solidity",
... "sources": {
... "Greeter.sol": {
... "content": '''
... pragma solidity ^0.5.0;
...
... contract Greeter {
... string public greeting;
>>> compiled_sol = compile_source(
... '''
... pragma solidity >0.5.0;
...
... constructor() public {
... greeting = 'Hello';
... }
... contract Greeter {
... string public greeting;
...
... function setGreeting(string memory _greeting) public {
... greeting = _greeting;
... }
... constructor() public {
... greeting = 'Hello';
... }
...
... function greet() view public returns (string memory) {
... return greeting;
... }
... }
... '''
... function setGreeting(string memory _greeting) public {
... greeting = _greeting;
... }
... },
... "settings":
... {
... "outputSelection": {
... "*": {
... "*": [
... "metadata", "evm.bytecode"
... , "evm.bytecode.sourceMap"
... ]
... }
... }
...
.. . function greet() view public returns (string memory) {
... return greeting;
... }
... })
... }
... '''
... )
# retrieve the contract interface
>>> contract_id, contract_interface = compiled_sol.popitem()
# get bytecode / bin
>>> bytecode = contract_interface['bin']
# get abi
>>> abi = contract_interface['abi']
# web3.py instance
>>> w3 = Web3(Web3.EthereumTesterProvider())
# set pre-funded account as sender
>>> w3.eth.default_account = w3.eth.accounts[0]
# get bytecode
>>> bytecode = compiled_sol['contracts']['Greeter.sol']['Greeter']['evm']['bytecode']['object']
# get abi
>>> abi = json.loads(compiled_sol['contracts']['Greeter.sol']['Greeter']['metadata'])['output']['abi']
>>> Greeter = w3.eth.contract(abi=abi, bytecode=bytecode)
# Submit the transaction that deploys the contract
Expand Down
1 change: 1 addition & 0 deletions newsfragments/2020.doc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Update "Contract Deployment Example" docs to use ``py-solc-x`` as ``solc`` is no longer maintained.

0 comments on commit 588e0c0

Please sign in to comment.