Skip to content

Commit 7635de9

Browse files
authored
Add Infura example to middleware docs (#2410)
* Add Infura example to middleware docs * Add newsfragment
1 parent 533e292 commit 7635de9

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

docs/middleware.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,19 @@ this middleware to have any effect.
458458
>>> w3.middleware_onion.add(construct_sign_and_send_raw_middleware(acct))
459459
>>> w3.eth.default_account = acct.address
460460
461+
:ref:`Hosted nodes<local_vs_hosted>` (like Infura or Alchemy) only support signed transactions. This often results in ``send_raw_transaction`` being used repeatedly. Instead, we can automate this process with ``construct_sign_and_send_raw_middleware(private_key_or_account)``.
462+
463+
.. code-block:: python
464+
465+
>>> from web3 import Web3
466+
>>> w3 = Web3(Web3.HTTPProvider('HTTP_ENDPOINT'))
467+
>>> from web3.middleware import construct_sign_and_send_raw_middleware
468+
>>> from eth_account import Account
469+
>>> import os
470+
>>> acct = w3.eth.account.from_key(os.environ.get('PRIVATE_KEY'))
471+
>>> w3.middleware_onion.add(construct_sign_and_send_raw_middleware(acct))
472+
>>> w3.eth.default_account = acct.address
473+
461474
Now you can send a transaction from acct.address without having to build and sign each raw transaction.
462475

463476
When making use of this signing middleware, when sending dynamic fee transactions (recommended over legacy transactions),

newsfragments/2410.doc.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add example for the ``construct_sign_and_send_raw_middleware`` when connected to a hosted node

0 commit comments

Comments
 (0)