Skip to content

Commit 79aba6d

Browse files
authored
Merge pull request #1617 from njgheorghita/contract-instantiation-examples
Add examples of how to use ethpm for contract factories/instances
2 parents f5f9260 + 3a8a822 commit 79aba6d

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

docs/examples.rst

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,75 @@ Output:
301301
'transactionHash': HexBytes('0x3ac3518cc59d1698aa03a0bab7fb8191a4ef017aeda7429b11e8c6462b20a62a'),
302302
'transactionIndex': 0}
303303
304+
Working with Contracts via ethPM
305+
--------------------------------
306+
307+
`ethPM <http://www.ethpm.com/>`__ packages contain configured contracts ready for use. Web3's ``ethpm`` module (``web3.pm``)
308+
extends Web3's native ``Contract`` module, with a few modifications for how you instantiate ``Contract`` factories and instances.
309+
310+
All you need is the package name, version and ethPM registry address for the package you wish to use.
311+
An ethPM registry is essentially an on-chain datastore for the release data associated with an ethPM package. You can find some sample registries to explore in the `ethPM explorer <http://explorer.ethpm.com/>`__. Remember, you should only use packages from registries whose maintainer you trust not to inject malicious code!
312+
313+
In this example we will use the ``[email protected]`` package sourced from the ``ens.snakecharmers.eth`` registry.
314+
315+
``web3.pm`` uses the ``Package`` class to represent an ethPM package. This object houses all of the contract assets
316+
within a package, and exposes them via an API. So, before we can interact with our package, we need to generate
317+
it as a ``Package`` instance.
318+
319+
.. code-block:: python3
320+
321+
from web3.auto.infura import w3
322+
323+
# Note. To use the web3.pm module, you will need to instantiate your w3 instance
324+
# with a web3 provider connected to the chain on which your registry lives.
325+
326+
# The ethPM module is still experimental and subject to change,
327+
# so for now we need to enable it via a temporary flag.
328+
w3.enable_unstable_package_management_api()
329+
330+
# Then we need to set the registry address that we want to use.
331+
# This should be an ENS address, but can also be a checksummed contract address.
332+
w3.pm.set_registry("ens.snakecharmers.eth")
333+
334+
# This generates a Package instance of the target ethPM package.
335+
ens_package = w3.pm.get_package("ethregistrar", "1.0.1")
336+
337+
338+
Now that we have a ``Package`` representation of our target ethPM package, we can generate contract factories
339+
and instances from this ``Package``. However, it's important to note that some packages might be missing
340+
the necessary contract assets needed to generate an instance or a factory. You can use the
341+
`ethPM explorer <http://explorer.ethpm.com/>`__ to figure out the names of the contract types and deployments
342+
available within an ethPM package.
343+
344+
.. code-block:: python3
345+
346+
# To interact with a deployment located in an ethPM package.
347+
# Note. This will only expose deployments located on the
348+
# chain of the connected provider (in this example, mainnet)
349+
mainnet_registrar = ens_package.deployments.get_instance("BaseRegistrarImplementation")
350+
351+
# Now you can treat mainnet_registrar like any other Web3 Contract instance!
352+
mainnet_registrar.caller.balanceOf("0x123...")
353+
> 0
354+
355+
mainnet_registrar.functions.approve("0x123", 100000).transact()
356+
> 0x123abc... # tx_hash
357+
358+
# To create a contract factory from a contract type located in an ethPM package.
359+
registrar_factory = ens_package.get_contract_factory("BaseRegistrarImplementation")
360+
361+
# Now you can treat registrar_factory like any other Web3 Contract factory to deploy new instances!
362+
# Note. This will deploy new instances to the chain of the connected provider (in this example, mainnet)
363+
registrar_factory.constructor(...).transact()
364+
> 0x456def... # tx_hash
365+
366+
# To connect your Package to a new chain - simply pass it a new Web3 instance
367+
# connected to your provider of choice. Now your factories will automatically
368+
# deploy to this new chain, and the deployments available on a package will
369+
# be automatically filtered to those located on the new chain.
370+
from web3.auto.infura.goerli import w3 as goerli_w3
371+
goerli_registrar = ens_package.update_w3(goerli_w3)
372+
304373
305374
Working with an ERC20 Token Contract
306375
------------------------------------

newsfragments/1617.doc.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add examples for using web3.contract via the ethpm module.

0 commit comments

Comments
 (0)