Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update contract docs to use compile_standard #1263

Merged
merged 1 commit into from
May 2, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 66 additions & 55 deletions docs/contracts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,72 +21,83 @@ To run this example, you will need to install a few extra features:

.. code-block:: python
kclowes marked this conversation as resolved.
Show resolved Hide resolved

import json
import web3
>>> import json

from web3 import Web3
from solc import compile_source
>>> from web3 import Web3
>>> from solc import compile_standard

# Solidity source code
contract_source_code = """
pragma solidity ^0.4.21;

contract Greeter {
string public greeting;

function Greeter() public {
greeting = 'Hello';
}

function setGreeting(string _greeting) public {
greeting = _greeting;
}

function greet() view public returns (string) {
return greeting;
}
}
"""

compiled_sol = compile_source(contract_source_code) # Compiled source code
contract_interface = compiled_sol['<stdin>:Greeter']
>>> compiled_sol = compile_standard({
... "language": "Solidity",
... "sources": {
... "Greeter.sol": {
... "content": '''
... pragma solidity ^0.5.0;
...
... contract Greeter {
... string public greeting;
...
... constructor() public {
... greeting = 'Hello';
... }
...
... function setGreeting(string memory _greeting) public {
... greeting = _greeting;
... }
...
... function greet() view public returns (string memory) {
... return greeting;
... }
... }
... '''
... }
... },
... "settings":
... {
... "outputSelection": {
... "*": {
... "*": [
... "metadata", "evm.bytecode"
... , "evm.bytecode.sourceMap"
... ]
... }
... }
... }
... })

# web3.py instance
w3 = Web3(Web3.EthereumTesterProvider())
>>> w3 = Web3(Web3.EthereumTesterProvider())

# set pre-funded account as sender
w3.eth.defaultAccount = w3.eth.accounts[0]
>>> w3.eth.defaultAccount = w3.eth.accounts[0]

# get bytecode
>>> bytecode = compiled_sol['contracts']['Greeter.sol']['Greeter']['evm']['bytecode']['object']

# Instantiate and deploy contract
Greeter = w3.eth.contract(abi=contract_interface['abi'], bytecode=contract_interface['bin'])
# 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
tx_hash = Greeter.constructor().transact()
>>> tx_hash = Greeter.constructor().transact()

# Wait for the transaction to be mined, and get the transaction receipt
tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash)

# Create the contract instance with the newly-deployed address
greeter = w3.eth.contract(
address=tx_receipt.contractAddress,
abi=contract_interface['abi'],
)
>>> tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like you're missing a few lines here:

    Greeter = w3.eth.contract(abi=abi, bytecode=bytecode)
    # Submit the transaction that deploys the contract
    tx_hash = Greeter.constructor().transact()
    # Wait for the transaction to be mined, and get the transaction receipt
    tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash)

# Display the default greeting from the contract
print('Default contract greeting: {}'.format(
greeter.functions.greet().call()
))
>>> greeter = w3.eth.contract(
... address=tx_receipt.contractAddress,
... abi=abi
... )

print('Setting the greeting to Nihao...')
tx_hash = greeter.functions.setGreeting('Nihao').transact()
>>> greeter.functions.greet().call()
'Hello'

# Wait for transaction to be mined...
w3.eth.waitForTransactionReceipt(tx_hash)
>>> tx_hash = greeter.functions.setGreeting('Nihao').transact()
>>> tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash)
>>> greeter.functions.greet().call()
'Nihao'

# Display the new greeting value
print('Updated contract greeting: {}'.format(
greeter.functions.greet().call()
))

Contract Factories
------------------
Expand Down Expand Up @@ -743,7 +754,7 @@ Utils
'_transactionData': b'',
'_debatingPeriod': 604800,
'_newCurator': True})

ContractCaller
--------------

Expand All @@ -756,7 +767,7 @@ There are a number of different ways to invoke the ``ContractCaller``.

For example:

.. testsetup::
.. testsetup:: contractcaller

import json
from web3 import Web3
Expand All @@ -768,7 +779,7 @@ For example:
deploy_receipt = w3.eth.waitForTransactionReceipt(deploy_txn)
address = deploy_receipt.contractAddress

.. doctest::
.. doctest:: contractcaller

>>> myContract = w3.eth.contract(address=address, abi=ABI)
>>> twentyone = myContract.caller.multiply7(3)
Expand All @@ -777,7 +788,7 @@ For example:

It can also be invoked using parentheses:

.. doctest::
.. doctest:: contractcaller

>>> twentyone = myContract.caller().multiply7(3)
>>> twentyone
Expand All @@ -786,7 +797,7 @@ It can also be invoked using parentheses:
And a transaction dictionary, with or without the ``transaction`` keyword.
You can also optionally include a block identifier. For example:

.. doctest::
.. doctest:: contractcaller

>>> from_address = w3.eth.accounts[1]
>>> twentyone = myContract.caller({'from': from_address}).multiply7(3)
Expand Down