Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Deploying a Simple Smart Contract | Smart Contracts with Python
Blockchain Foundations with Python

Deploying a Simple Smart Contract

Svep för att visa menyn

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 testnet
w3 = Web3(Web3.HTTPProvider("https://goerli.infura.io/v3/YOUR_INFURA_PROJECT_ID"))

# Account setup
account = w3.eth.account.from_key("YOUR_PRIVATE_KEY")
nonce = w3.eth.get_transaction_count(account.address)

# Minimal contract (ABI + bytecode)
abi = [{
    "name": "getValue",
    "type": "function",
    "inputs": [],
    "outputs": [{"type": "uint256"}],
    "stateMutability": "view"
}]
bytecode = "0x60806040..."  # shortened

# Build deployment transaction
contract = w3.eth.contract(abi=abi, bytecode=bytecode)
tx = contract.constructor().build_transaction({
    "from": account.address,
    "nonce": nonce,
    "gas": 2_000_000,
    "gasPrice": w3.to_wei("10", "gwei"),
})

# Sign and send
signed = w3.eth.account.sign_transaction(tx, account.key)
tx_hash = w3.eth.send_raw_transaction(signed.rawTransaction)

print(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.

# Wait for deployment
print("Waiting for confirmation...")
receipt = w3.eth.wait_for_transaction_receipt(tx_hash)

# Check result
if receipt.status:
    print(f"Deployed at: {receipt.contractAddress}")
else:
    print("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?

Vänligen välj det korrekta svaret

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 2

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Avsnitt 2. Kapitel 2
some-alt