web3py.readthedocs.io
Open in
urlscan Pro
2606:4700::6811:2152
Public Scan
URL:
https://web3py.readthedocs.io/en/stable/transactions.html
Submission: On October 26 via api from US — Scanned from DE
Submission: On October 26 via api from US — Scanned from DE
Form analysis
2 forms found in the DOMGET search.html
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
<input type="text" name="q" placeholder="Search docs">
<input type="hidden" name="check_keywords" value="yes">
<input type="hidden" name="area" value="default">
</form>
GET //readthedocs.org/projects/web3py/search/
<form id="flyout-search-form" class="wy-form" target="_blank" action="//readthedocs.org/projects/web3py/search/" method="get">
<input type="text" name="q" aria-label="Dokumente durchsuchen" placeholder="Dokumente durchsuchen">
</form>
Text Content
web3.py stable Intro * Quickstart * Overview * Release Notes Guides * Your Ethereum Node * Providers * Working with Local Private Keys * Sending Transactions * Chapter 0: w3.eth.send_transaction with eth-tester * Chapter 1: w3.eth.send_transaction + signer middleware * Chapter 2: w3.eth.send_raw_transaction * Chapter 3: Contract transactions * Monitoring Events * Contracts * ABI Types * Middleware * Web3 Internals * ethPM * Ethereum Name Service (ENS) * Examples * Troubleshooting * Migrating your code from v5 to v6 * Migrating your code from v4 to v5 * Migrating your code from v3 to v4 API * Web3 API * web3.eth API * Beacon API * Package Manager API * Net API * Miner API * Geth API * Tracing API * Utils * Gas Price API * ENS API * Constants Community * Resources and Learning Material * Contributing * Code of Conduct web3.py * * Sending Transactions * Edit on GitHub -------------------------------------------------------------------------------- SENDING TRANSACTIONS Note Prefer to view this code in a Jupyter Notebook? View the repo here. There are two methods for sending transactions using web3.py: send_transaction() and send_raw_transaction(). A brief guide: 1. Want to sign a transaction offline or send pre-signed transactions? * use sign_transaction + send_raw_transaction() 2. Are you primarily using the same account for all transactions and would you prefer to save a few lines of code? * configure construct_sign_and_send_raw_middleware(), then * use send_transaction() 3. Otherwise: * load account via eth-account (w3.eth.account.from_key(pk)), then * use send_transaction() Interacting with or deploying a contract? * Option 1: transact() uses send_transaction() under the hood * Option 2: build_transaction() + sign_transaction + send_raw_transaction() An example for each can be found below. CHAPTER 0: W3.ETH.SEND_TRANSACTION WITH ETH-TESTER Many tutorials use eth-tester (via EthereumTesterProvider) for convenience and speed of conveying ideas/building a proof of concept. Transactions sent by test accounts are auto-signed. from web3 import Web3, EthereumTesterProvider w3 = Web3(EthereumTesterProvider()) # eth-tester populates accounts with test ether: acct1 = w3.eth.accounts[0] some_address = "0x0000000000000000000000000000000000000000" # when using one of its generated test accounts, # eth-tester signs the tx (under the hood) before sending: tx_hash = w3.eth.send_transaction({ "from": acct1, "to": some_address, "value": 123123123123123 }) tx = w3.eth.get_transaction(tx_hash) assert tx["from"] == acct1 CHAPTER 1: W3.ETH.SEND_TRANSACTION + SIGNER MIDDLEWARE The send_transaction() method is convenient and to-the-point. If you want to continue using the pattern after graduating from eth-tester, you can utilize web3.py middleware to sign transactions from a particular account: from web3.middleware import construct_sign_and_send_raw_middleware import os # Note: Never commit your key in your code! Use env variables instead: pk = os.environ.get('PRIVATE_KEY') # Instantiate an Account object from your key: acct2 = w3.eth.account.from_key(pk) # For the sake of this example, fund the new account: w3.eth.send_transaction({ "from": acct1, "value": w3.to_wei(3, 'ether'), "to": acct2.address }) # Add acct2 as auto-signer: w3.middleware_onion.add(construct_sign_and_send_raw_middleware(acct2)) # pk also works: w3.middleware_onion.add(construct_sign_and_send_raw_middleware(pk)) # Transactions from `acct2` will then be signed, under the hood, in the middleware: tx_hash = w3.eth.send_transaction({ "from": acct2.address, "value": 3333333333, "to": some_address }) tx = w3.eth.get_transaction(tx_hash) assert tx["from"] == acct2.address # Optionally, you can set a default signer as well: # w3.eth.default_account = acct2.address # Then, if you omit a "from" key, acct2 will be used. CHAPTER 2: W3.ETH.SEND_RAW_TRANSACTION if you don’t opt for the middleware, you’ll need to: * build each transaction, * sign_transaction, and * then use send_raw_transaction(). # 1. Build a new tx transaction = { 'from': acct2.address, 'to': some_address, 'value': 1000000000, 'nonce': w3.eth.get_transaction_count(acct2.address), 'gas': 200000, 'maxFeePerGas': 2000000000, 'maxPriorityFeePerGas': 1000000000, } # 2. Sign tx with a private key signed = w3.eth.account.sign_transaction(transaction, pk) # 3. Send the signed transaction tx_hash = w3.eth.send_raw_transaction(signed.rawTransaction) tx = w3.eth.get_transaction(tx_hash) assert tx["from"] == acct2.address CHAPTER 3: CONTRACT TRANSACTIONS The same concepts apply for contract interactions, at least under the hood. Executing a function on a smart contract requires sending a transaction, which is typically done in one of two ways: * executing the transact() function, or * build_transaction(), then signing and sending the raw transaction. ######################################### #### SMOL CONTRACT FOR THIS EXAMPLE: #### ######################################### # // SPDX-License-Identifier: MIT # pragma solidity 0.8.17; # # contract Billboard { # string public message; # # constructor(string memory _message) { # message = _message; # } # # function writeBillboard(string memory _message) public { # message = _message; # } # } # After compiling the contract, initialize the contract factory: init_bytecode = "60806040523480156200001157600080fd5b5060..." abi = '[{"inputs": [{"internalType": "string","name": "_message",...' Billboard = w3.eth.contract(bytecode=init_bytecode, abi=abi) # Deploy a contract using `transact` + the signer middleware: tx_hash = Billboard.constructor("gm").transact({"from": acct2.address}) receipt = w3.eth.get_transaction_receipt(tx_hash) deployed_addr = receipt["contractAddress"] # Reference the deployed contract: billboard = w3.eth.contract(address=deployed_addr, abi=abi) # Manually build and sign a transaction: unsent_billboard_tx = billboard.functions.writeBillboard("gn").build_transaction({ "from": acct2.address, "nonce": w3.eth.get_transaction_count(acct2.address), }) signed_tx = w3.eth.account.sign_transaction(unsent_billboard_tx, private_key=acct2.key) # Send the raw transaction: assert billboard.functions.message().call() == "gm" tx_hash = w3.eth.send_raw_transaction(signed_tx.rawTransaction) w3.eth.wait_for_transaction_receipt(tx_hash) assert billboard.functions.message().call() == "gn" Previous Next -------------------------------------------------------------------------------- Highly targeted ads for devs Reach your exact developer niche with ML powered ads Ad by EthicalAds · ℹ️ © Copyright 2023, Ethereum Foundation. Revision fa7b6911. Built with Sphinx using a theme provided by Read the Docs. RTD v: stable Versionen latest stable v6.11.1 v6.11.0 v6.10.0 v6.9.0 v6.8.0 v6.7.0 v6.6.1 v6.6.0 v6.5.0 v6.4.0 v6.2.0 v6.1.0 v6.0.0 v5 v4.10.0 Auf Read the Docs Projektstartseite Erstellungsprozesse Downloads Auf GitHub Ansehen Suche -------------------------------------------------------------------------------- Bereitgestellt von Read the Docs · Datenschutz-Bestimmungen