Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Interacting with Deployed Contracts | Section
Python for Blockchain Networks

Interacting with Deployed Contracts

Swipe um das Menü anzuzeigen

When interacting with deployed smart contracts on a blockchain, you need to understand the Application Binary Interface (ABI), which defines how your Python code communicates with the contract’s functions and data structures. The ABI is a JSON description that specifies the functions, inputs, outputs, and types available in the contract. Function calls in smart contracts are divided into two main categories:

  • Read (view or pure) operations only query the contract’s state and do not modify the blockchain, so they are free and do not require a transaction;
  • Write (state-changing) operations change the contract’s state, require a transaction, consume gas, and must be signed by a private key.

Knowing when to use each type is crucial for efficient and secure blockchain applications.

from web3 import Web3

# Connect to a local Ethereum node
web3 = Web3(Web3.HTTPProvider("http://127.0.0.1:8545"))

# ABI and contract address (simplified example)
abi = [
    {
        "constant": True,
        "inputs": [],
        "name": "getValue",
        "outputs": [{"name": "", "type": "uint256"}],
        "payable": False,
        "stateMutability": "view",
        "type": "function"
    }
]
contract_address = "0x1234567890abcdef1234567890abcdef12345678"

# Create contract instance
contract = web3.eth.contract(address=contract_address, abi=abi)

# Call the read-only function
value = contract.functions.getValue().call()

print("Current contract value:", value)

Reading contract state involves calling view or pure functions that do not alter the blockchain. These calls are handled locally by the node, and you can retrieve information such as balances, ownership, or configuration parameters. When you need to change the contract’s state—such as transferring tokens, updating records, or triggering events—you must send a transaction. This transaction must be signed with a private key and submitted to the blockchain network, where miners or validators confirm and record the change. After sending a transaction, you typically receive a transaction hash, which you can use to track the transaction’s status and response once it is mined or finalized.

from web3 import Web3

# Connect to a local Ethereum node
web3 = Web3(Web3.HTTPProvider("http://127.0.0.1:8545"))

# ABI and contract address (simplified example)
abi = [
    {
        "constant": False,
        "inputs": [{"name": "newValue", "type": "uint256"}],
        "name": "setValue",
        "outputs": [],
        "payable": False,
        "stateMutability": "nonpayable",
        "type": "function"
    }
]
contract_address = "0x1234567890abcdef1234567890abcdef12345678"

# Create contract instance
contract = web3.eth.contract(address=contract_address, abi=abi)

# Define the sender account and private key (replace with your own for real use)
from_account = web3.eth.accounts[0]
private_key = "0xabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"

# Build the transaction to call setValue
tx = contract.functions.setValue(42).build_transaction({
    "from": from_account,
    "nonce": web3.eth.get_transaction_count(from_account),
    "gas": 200000,
    "gasPrice": web3.to_wei("10", "gwei")
})

# Sign the transaction
signed_tx = web3.eth.account.sign_transaction(tx, private_key=private_key)

# Send the transaction
tx_hash = web3.eth.send_raw_transaction(signed_tx.rawTransaction)

print("Transaction sent with hash:", tx_hash.hex())
question mark

Which statement best describes the difference between read and write operations when interacting with smart contracts?

Wählen Sie die richtige Antwort aus

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 6

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 1. Kapitel 6
some-alt