Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Introduction to Smart Contracts | Section
Python for Blockchain Networks

Introduction to Smart Contracts

Pyyhkäise näyttääksesi valikon

Smart contracts are self-executing programs stored on a blockchain that automatically enforce the terms of an agreement without the need for intermediaries. Their primary purpose is to automate transactions and business logic, ensuring that all participants in a blockchain network can trust the outcome without relying on a central authority. When certain conditions defined within the contract are met, the smart contract triggers actions such as transferring tokens, recording data, or updating state on the blockchain. This automation increases transparency, reduces the risk of fraud, and can save both time and costs.

# Simple smart contract pseudocode (Python-like)

class EscrowContract:
    def __init__(self, buyer, seller, amount):
        self.buyer = buyer
        self.seller = seller
        self.amount = amount
        self.funds_held = False

    def deposit(self, from_address, value):
        if from_address == self.buyer and value == self.amount:
            self.funds_held = True

    def release(self, to_address):
        if self.funds_held and to_address == self.seller:
            # Transfer funds to seller
            self.funds_held = False
            return "Funds released"
        return "Conditions not met"

You can find smart contracts at the heart of many blockchain applications. For example:

  • Tokens are often managed by contracts that define rules for minting, transferring, and burning digital assets;
  • In voting systems, smart contracts securely collect and count votes, ensuring transparency and immutability;
  • Escrow contracts, like the one illustrated above, hold funds until both buyer and seller meet agreed-upon conditions, automating trust in digital transactions.

The structure of these contracts typically includes functions to handle deposits, releases, and checks for required conditions, all without manual intervention.

# Interacting with a smart contract ABI using Python

from web3 import Web3

# Connect to a blockchain node (e.g., local Ethereum node)
w3 = Web3(Web3.HTTPProvider("http://localhost:8545"))

# Example ABI for a simple contract
contract_abi = [
    {
        "constant": True,
        "inputs": [],
        "name": "getBalance",
        "outputs": [{"name": "", "type": "uint256"}],
        "type": "function"
    }
]

# Contract address (example)
contract_address = "0x1234567890abcdef1234567890abcdef12345678"

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

# Call a contract function
balance = contract.functions.getBalance().call()
print("Contract balance:", balance)
question mark

Which of the following best describes the core features of smart contracts and their typical use cases on blockchain platforms?

Valitse kaikki oikeat vastaukset

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 4

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Osio 1. Luku 4
some-alt