Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Deploying a Simple Smart Contract | Smart Contracts with Python
Practice
Projects
Quizzes & Challenges
Quizze
Challenges
/
Blockchain Foundations with Python

bookDeploying a Simple Smart Contract

Swipe um das Menü anzuzeigen

To successfully deploy a smart contract using Python, you need to understand several essential concepts and tools. The deployment process involves preparing your smart contract code, connecting to a blockchain network, creating a deployment transaction, and sending it to the network for mining. You typically use a blockchain development framework, such as web3.py, which enables Python applications to interact with Ethereum-compatible blockchains.

Before deploying to a live network, it is best practice to use a test network (testnet). Test networks, such as Ropsten or Goerli, simulate the main blockchain environment but use test tokens instead of real cryptocurrency. This allows you to test and debug your smart contract without risking real assets or incurring high transaction fees.

To deploy a contract, you will need:

  • The compiled bytecode and ABI (Application Binary Interface) of your smart contract;
  • Access to a blockchain node (either local or via a service provider);
  • A funded account on the testnet to pay for gas;
  • Python libraries such as web3.py to handle transactions and communicate with the node.

Once you have your environment ready, you can proceed to deploy your contract by constructing a deployment transaction, signing it with your account's private key, and sending it to the network.

from web3 import Web3

# Connect to a testnet node (e.g., Infura)
w3 = Web3(Web3.HTTPProvider('https://goerli.infura.io/v3/YOUR_INFURA_PROJECT_ID'))

# Your account details
private_key = 'YOUR_PRIVATE_KEY'
account = w3.eth.account.from_key(private_key)
nonce = w3.eth.get_transaction_count(account.address)

# Contract ABI and bytecode (replace with your contract's actual data)
contract_abi = [
    {
        "inputs": [],
        "name": "getValue",
        "outputs": [{"internalType": "uint256", "name": "", "type": "uint256"}],
        "stateMutability": "view",
        "type": "function"
    }
]
contract_bytecode = '0x6080604052348015600f57600080fd5b50607f80601d6000396000f3fe6080604052600080fdfea2646970667358221220...'

# Create the contract instance
SimpleContract = w3.eth.contract(abi=contract_abi, bytecode=contract_bytecode)

# Build the deployment transaction
transaction = SimpleContract.constructor().build_transaction({
    'from': account.address,
    'nonce': nonce,
    'gas': 2000000,
    'gasPrice': w3.to_wei('10', 'gwei')
})

# Sign the transaction
signed_txn = w3.eth.account.sign_transaction(transaction, private_key=private_key)

# Send the transaction
tx_hash = w3.eth.send_raw_transaction(signed_txn.rawTransaction)

print(f"Deployment transaction sent. Transaction hash: {tx_hash.hex()}")

After submitting the deployment transaction, the blockchain network will process and mine it. You need to wait for the transaction to be included in a block before the contract is considered deployed. The transaction receipt provides critical information, including the contract address and the status of the deployment.

import time

# Wait for the transaction to be mined and get the receipt
print("Waiting for deployment transaction to be mined...")
tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)

# Check deployment status and contract address
if tx_receipt.status == 1:
    print(f"Contract deployed successfully at address: {tx_receipt.contractAddress}")
else:
    print("Contract deployment failed.")

To interpret the receipt, first, retrieve it using the transaction hash. The receipt includes the contractAddress, confirming the location of your deployed contract on the blockchain. If the status field is 1, the deployment succeeded; if it is 0, the deployment failed. Always verify the contract address to ensure future interactions target the correct contract instance.

question mark

Which of the following best describes the steps involved in deploying a smart contract using Python, and why is the transaction receipt important?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 2

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Abschnitt 2. Kapitel 2
some-alt