Skip to content

Commit

Permalink
Fix contract example doctests
Browse files Browse the repository at this point in the history
  • Loading branch information
kclowes committed May 1, 2019
1 parent 4210ae0 commit 6a68160
Showing 1 changed file with 62 additions and 156 deletions.
218 changes: 62 additions & 156 deletions docs/contracts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,178 +19,84 @@ To run this example, you will need to install a few extra features:
- The ``solc`` solidity compiler. See `Installing the Solidity Compiler
<http://solidity.readthedocs.io/en/latest/installing-solidity.html#binary-packages>`_

.. code-block:: python
.. doctest::

import json
>>> import json

from web3 import Web3
from solc import compile_standard
from web3.contract import ConciseContract
>>> from web3 import Web3
>>> from solc import compile_standard

# Solidity source code
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"
]
}
}
}
}
)
>>> 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']
>>> bytecode = compiled_sol['contracts']['Greeter.sol']['Greeter']['evm']['bytecode']['object']

# get abi
abi = json.loads(compiled_sol['contracts']['Greeter.sol']['Greeter']['metadata'])['output']['abi']
>>> abi = json.loads(compiled_sol['contracts']['Greeter.sol']['Greeter']['metadata'])['output']['abi']

# Instantiate and deploy contract
Greeter = w3.eth.contract(abi=abi, bytecode=bytecode)
>>> 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=abi
)
# Display the default greeting from the contract
print('Default contract greeting: {}'.format(
greeter.functions.greet().call()
))
print('Setting the greeting to Nihao...')
tx_hash = greeter.functions.setGreeting('Nihao').transact()
# Wait for transaction to be mined...
w3.eth.waitForTransactionReceipt(tx_hash)
# Display the new greeting value
print('Updated contract greeting: {}'.format(
greeter.functions.greet().call()
))
# When issuing a lot of reads, try this more concise reader:
reader = ConciseContract(greeter)
assert reader.greet() == "Nihao"
.. testsetup::
import json
from web3 import Web3
from solc import compile_standard
from web3.contract import ConciseContract
# Solidity source code
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())
# set pre-funded account as sender
w3.eth.defaultAccount = 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']
>>> tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash)

greeter = w3.eth.contract(
address=tx_receipt.contractAddress,
abi=abi
)
>>> greeter = w3.eth.contract(
... address=tx_receipt.contractAddress,
... abi=abi
... )

.. doctest::
>>> greeter.functions.greet().call()
'Hello'

>>> greeter.functions.greet().call()
"Hello"
>>> tx_hash = greeter.functions.setGreeting('Nihao').transact()
>>> w3.eth.waitForTransactionReceipt(tx_hash)
>>> greeter.functions.greet().call()
"Nihao"
>>> tx_hash = greeter.functions.setGreeting('Nihao').transact()
>>> tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash)
>>> greeter.functions.greet().call()
'Nihao'


Contract Factories
Expand Down Expand Up @@ -861,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 @@ -873,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 @@ -882,7 +788,7 @@ For example:

It can also be invoked using parentheses:

.. doctest::
.. doctest:: contractcaller

>>> twentyone = myContract.caller().multiply7(3)
>>> twentyone
Expand All @@ -891,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

0 comments on commit 6a68160

Please sign in to comment.