From 6a68160e8bcd681df069a6911481d3129afc2860 Mon Sep 17 00:00:00 2001 From: Keri Date: Wed, 1 May 2019 12:37:09 -0600 Subject: [PATCH] Fix contract example doctests --- docs/contracts.rst | 218 +++++++++++++-------------------------------- 1 file changed, 62 insertions(+), 156 deletions(-) diff --git a/docs/contracts.rst b/docs/contracts.rst index 63082cc59b..c4cc97dacc 100644 --- a/docs/contracts.rst +++ b/docs/contracts.rst @@ -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 `_ -.. 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 @@ -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 @@ -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) @@ -882,7 +788,7 @@ For example: It can also be invoked using parentheses: -.. doctest:: +.. doctest:: contractcaller >>> twentyone = myContract.caller().multiply7(3) >>> twentyone @@ -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)