-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Add examples of how to use ethpm for contract factories/instances #1617
Merged
njgheorghita
merged 2 commits into
ethereum:master
from
njgheorghita:contract-instantiation-examples
Mar 31, 2020
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -301,6 +301,75 @@ Output: | |
'transactionHash': HexBytes('0x3ac3518cc59d1698aa03a0bab7fb8191a4ef017aeda7429b11e8c6462b20a62a'), | ||
'transactionIndex': 0} | ||
|
||
Working with Contracts via ethPM | ||
-------------------------------- | ||
|
||
`ethPM <http://www.ethpm.com/>`__ packages contain configured contracts ready for use. Web3's ``ethpm`` module (``web3.pm``) | ||
extends Web3's native ``Contract`` module, with a few modifications for how you instantiate ``Contract`` factories and instances. | ||
|
||
All you need is the package name, version and ethPM registry address for the package you wish to use. | ||
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! | ||
|
||
In this example we will use the ``[email protected]`` package sourced from the ``ens.snakecharmers.eth`` registry. | ||
|
||
``web3.pm`` uses the ``Package`` class to represent an ethPM package. This object houses all of the contract assets | ||
within a package, and exposes them via an API. So, before we can interact with our package, we need to generate | ||
it as a ``Package`` instance. | ||
|
||
.. code-block:: python3 | ||
|
||
from web3.auto.infura import w3 | ||
|
||
# Note. To use the web3.pm module, you will need to instantiate your w3 instance | ||
# with a web3 provider connected to the chain on which your registry lives. | ||
|
||
# The ethPM module is still experimental and subject to change, | ||
# so for now we need to enable it via a temporary flag. | ||
w3.enable_unstable_package_management_api() | ||
|
||
# Then we need to set the registry address that we want to use. | ||
# This should be an ENS address, but can also be a checksummed contract address. | ||
w3.pm.set_registry("ens.snakecharmers.eth") | ||
|
||
# This generates a Package instance of the target ethPM package. | ||
ens_package = w3.pm.get_package("ethregistrar", "1.0.1") | ||
|
||
|
||
Now that we have a ``Package`` representation of our target ethPM package, we can generate contract factories | ||
and instances from this ``Package``. However, it's important to note that some packages might be missing | ||
the necessary contract assets needed to generate an instance or a factory. You can use the | ||
`ethPM explorer <http://explorer.ethpm.com/>`__ to figure out the names of the contract types and deployments | ||
available within an ethPM package. | ||
|
||
.. code-block:: python3 | ||
|
||
# To interact with a deployment located in an ethPM package. | ||
# Note. This will only expose deployments located on the | ||
# chain of the connected provider (in this example, mainnet) | ||
mainnet_registrar = ens_package.deployments.get_instance("BaseRegistrarImplementation") | ||
|
||
# Now you can treat mainnet_registrar like any other Web3 Contract instance! | ||
mainnet_registrar.caller.balanceOf("0x123...") | ||
> 0 | ||
|
||
mainnet_registrar.functions.approve("0x123", 100000).transact() | ||
> 0x123abc... # tx_hash | ||
|
||
# To create a contract factory from a contract type located in an ethPM package. | ||
registrar_factory = ens_package.get_contract_factory("BaseRegistrarImplementation") | ||
|
||
# Now you can treat registrar_factory like any other Web3 Contract factory to deploy new instances! | ||
# Note. This will deploy new instances to the chain of the connected provider (in this example, mainnet) | ||
registrar_factory.constructor(...).transact() | ||
> 0x456def... # tx_hash | ||
|
||
# To connect your Package to a new chain - simply pass it a new Web3 instance | ||
# connected to your provider of choice. Now your factories will automatically | ||
# deploy to this new chain, and the deployments available on a package will | ||
# be automatically filtered to those located on the new chain. | ||
from web3.auto.infura.goerli import w3 as goerli_w3 | ||
goerli_registrar = ens_package.update_w3(goerli_w3) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ooo, that's cool! |
||
|
||
|
||
Working with an ERC20 Token Contract | ||
------------------------------------ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add examples for using web3.contract via the ethpm module. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think its possible/worth making these doctests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, it's possible - do you know if we have an infura key loaded in the doctest environment? EDIT: nvm I think I found it - i'll look into transforming these to doctest
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update: I've been having some troubles getting this test to work with doctest - so I'm gonna move on for now. I don't think it's impossible, but it's been tricky getting all the pieces to work, but i'm happy to dedicate more time to this if it's important for web3's sake to have consistent doctests amongst the examples - just let me know if you'd like it to be doctested and i'll add it to my todo list!