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

document contract function struct args #1860

Merged
merged 1 commit into from
Jan 28, 2021
Merged
Show file tree
Hide file tree
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
61 changes: 58 additions & 3 deletions docs/contracts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ Contracts

.. py:module:: web3.contract

It is worth taking your time to understand all about contracts. To get started,
check out this example:
Smart contracts are programs deployed to the Ethereum network. See the
`ethereum.org docs <https://ethereum.org/en/developers/docs/smart-contracts>`_
for a proper introduction.

.. _contract_example:

Contract Deployment Example
----------------------------------------------
---------------------------

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

Expand Down Expand Up @@ -1211,3 +1212,57 @@ provides methods to interact with contract functions.
Positional and keyword arguments supplied to the contract caller subclass
will be used to find the contract function by signature,
and forwarded to the contract function when applicable.


Contract FAQs
-------------

How do I pass in a struct as a function argument?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Web3.py accepts struct arguments as dictionaries. This format also supports nested structs.
Let's take a look at a quick example. Given the following Solidity contract:

.. code-block:: none

contract Example {
address addr;

struct S1 {
address a1;
address a2;
}

struct S2 {
bytes32 b1;
bytes32 b2;
}

struct X {
S1 s1;
S2 s2;
address[] users;
}

function update(X memory x) public {
addr = x.s1.a2;
}

function retrieve() public view returns (address) {
return addr;
}
}

You can interact with Web3.py contract API as follows:

.. code-block:: python

# deploy or lookup the deployed contract, then:

>>> deployed_contract.functions.retrieve().call()
'0x0000000000000000000000000000000000000000'

>>> deployed_contract.functions.update({'s1': ['0x0000000000000000000000000000000000000001', '0x0000000000000000000000000000000000000002'], 's2': [b'0'*32, b'1'*32], 'users': []}).transact()

>>> deployed_contract.functions.retrieve().call()
'0x0000000000000000000000000000000000000002'
1 change: 1 addition & 0 deletions newsfragments/1860.doc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Document passing a struct into a contract function.