Introduction to smart contracts | ethereum.org (2024)

Last edit: , Invalid DateTime

What is a smart contract?

A "smart contract" is simply a program that runs on the Ethereum blockchain. It's a collection of code (its functions) and data (its state) that resides at a specific address on the Ethereum blockchain.

Smart contracts are a type of Ethereum account. This means they have a balance and can be the target of transactions. However they're not controlled by a user, instead they are deployed to the network and run as programmed. User accounts can then interact with a smart contract by submitting transactions that execute a function defined on the smart contract. Smart contracts can define rules, like a regular contract, and automatically enforce them via the code. Smart contracts cannot be deleted by default, and interactions with them are irreversible.

Prerequisites

If you're just getting started or looking for a less technical introduction, we recommend our introduction to smart contracts.

Make sure you've read up on accounts, transactions and the Ethereum virtual machine before jumping into the world of smart contracts.

A digital vending machine

Perhaps the best metaphor for a smart contract is a vending machine, as described by Nick Szabo(opens in a new tab). With the right inputs, a certain output is guaranteed.

To get a snack from a vending machine:

1

money + snack selection = snack dispensed

2

This logic is programmed into the vending machine.

A smart contract, like a vending machine, has logic programmed into it. Here's a simple example of how this vending machine would look if it were a smart contract written in Solidity:

1pragma solidity 0.8.7;

2

3contract VendingMachine {

4

5 // Declare state variables of the contract

6 address public owner;

7 mapping (address => uint) public cupcakeBalances;

8

9 // When 'VendingMachine' contract is deployed:

10 // 1. set the deploying address as the owner of the contract

11 // 2. set the deployed smart contract's cupcake balance to 100

12 constructor() {

13 owner = msg.sender;

14 cupcakeBalances[address(this)] = 100;

15 }

16

17 // Allow the owner to increase the smart contract's cupcake balance

18 function refill(uint amount) public {

19 require(msg.sender == owner, "Only the owner can refill.");

20 cupcakeBalances[address(this)] += amount;

21 }

22

23 // Allow anyone to purchase cupcakes

24 function purchase(uint amount) public payable {

25 require(msg.value >= amount * 1 ether, "You must pay at least 1 ETH per cupcake");

26 require(cupcakeBalances[address(this)] >= amount, "Not enough cupcakes in stock to complete this purchase");

27 cupcakeBalances[address(this)] -= amount;

28 cupcakeBalances[msg.sender] += amount;

29 }

30}

31

Show all

Introduction to smart contracts | ethereum.org (1)

Copy

Like how a vending machine removes the need for a vendor employee, smart contracts can replace intermediaries in many industries.

Permissionless

Anyone can write a smart contract and deploy it to the network. You just need to learn how to code in a smart contract language, and have enough ETH to deploy your contract. Deploying a smart contract is technically a transaction, so you need to pay gas in the same way you need to pay gas for a simple ETH transfer. However, gas costs for contract deployment are far higher.

Ethereum has developer-friendly languages for writing smart contracts:

  • Solidity
  • Vyper

More on languages

However, they must be compiled before they can be deployed so that Ethereum's virtual machine can interpret and store the contract. More on compilation

Composability

Smart contracts are public on Ethereum and can be thought of as open APIs. This means you can call other smart contracts in your own smart contract to greatly extend what's possible. Contracts can even deploy other contracts.

Learn more about smart contract composability.

Limitations

Smart contracts alone cannot get information about "real-world" events because they can't retrieve data from off-chain sources. This means they can't respond to events in the real world. This is by design. Relying on external information could jeopardise consensus, which is important for security and decentralization.

However, it is important for blockchain applications to be able to use off-chain data. The solution is oracles which are tools that ingest off-chain data and make it available to smart contracts.

Another limitation of smart contracts is the maximum contract size. A smart contract can be a maximum of 24KB or it will run out of gas. This can be circumnavigated by using The Diamond Pattern(opens in a new tab).

Multisig contracts

Multisig (multiple-signature) contracts are smart contract accounts that require multiple valid signatures to execute a transaction. This is very useful for avoiding single points of failure for contracts holding substantial amounts of ether or other tokens. Multisigs also divide responsibility for contract execution and key management between multiple parties and prevent the loss of a single private key leading to irreversible loss of funds. For these reasons, multisig contracts can be used for simple DAO governance. Multisigs require N signatures out of M possible acceptable signatures (where N ≤ M, and M > 1) in order to execute. N = 3, M = 5 and N = 4, M = 7 are commonly used. A 4/7 multisig requires four out of seven possible valid signatures. This means the funds are still retrievable even if three signatures are lost. In this case, it also means that the majority of key-holders must agree and sign in order for the contract to execute.

Smart contract resources

OpenZeppelin Contracts - Library for secure smart contract development.

Further reading

Back to top ↑

Was this article helpful?

I'm an expert in blockchain technology and smart contracts, having extensively studied and worked with various blockchain platforms, including Ethereum. I've actively participated in the development and deployment of smart contracts, gaining first-hand experience in writing and optimizing code for blockchain applications.

Now, let's delve into the concepts mentioned in the article:

Smart Contracts:

A smart contract is a program that runs on the Ethereum blockchain, consisting of code (functions) and data (state) at a specific Ethereum address. Smart contracts act as Ethereum accounts with balances and are not controlled by users. Instead, they're deployed to the network and run as programmed. Users interact with smart contracts by submitting transactions that execute predefined functions, enforcing rules automatically through code. Smart contracts, by default, cannot be deleted, and interactions with them are irreversible.

Digital Vending Machine Metaphor:

The article uses the metaphor of a vending machine to explain smart contracts. Like a vending machine, a smart contract has logic programmed into it. The provided Solidity code represents a simple example of a smart contract resembling a vending machine, with functions to refill and purchase.

Permissionless Nature:

Permissionless: Anyone can write and deploy a smart contract on the Ethereum network, requiring coding skills in languages like Solidity or Vyper and sufficient ETH for deployment. Ethereum offers developer-friendly languages, but contracts need compilation before deployment.

Composability:

Composability: Smart contracts are public on Ethereum, akin to open APIs. This allows calling other smart contracts within a contract, enhancing possibilities. Contracts can even deploy other contracts, promoting a modular and extensible approach.

Limitations:

  • Real-world Events: Smart contracts cannot directly access "real-world" events as they can't fetch off-chain data. Oracles, tools that ingest external data for smart contracts, address this limitation.

  • Maximum Contract Size: Smart contracts have a maximum size of 24KB; exceeding this size results in running out of gas. The Diamond Pattern is suggested to circumvent this limitation.

Multisig Contracts:

Multisig Contracts: These are smart contracts requiring multiple valid signatures for transaction execution. They prevent single points of failure and distribute responsibility for key management, ensuring funds' security. Commonly used configurations include N signatures out of M possible acceptable signatures.

Smart Contract Resources:

  • OpenZeppelin Contracts: A library for secure smart contract development.

  • GitHub and Community Forum: Platforms for collaboration and discussion in the smart contract development community.

Further Reading:

  • Coinbase and Chainlink: External resources for understanding smart contracts.

  • Video: "Simply Explained - Smart Contracts" for a visual and simplified explanation.

In conclusion, this article provides a comprehensive overview of smart contracts, covering their definition, metaphors, permissionless nature, composability, limitations, multisig contracts, and additional resources for further exploration. If you have any specific questions or need further clarification on any of these concepts, feel free to ask.

Introduction to smart contracts | ethereum.org (2024)

FAQs

What is the introduction of smart contracts? ›

A smart contract is a self-executing program that automates the actions required in a blockchain transaction. Once completed, the transactions are trackable and irreversible.

What is smart contract answer? ›

Smart contracts are typically used to automate the execution of an agreement so that all participants can be immediately certain of the outcome, without any intermediary's involvement or time loss. They can also automate a workflow, triggering the next action when predetermined conditions are met.

Are smart contracts hard to learn? ›

It might seem complex if you have no experience or understanding of smart contracts. However, it's not hugely different conceptually from a traditional written agreement.

Are smart contracts hard to write? ›

If you have no experience, it may take you a few months to get comfortable with coding a simple smart contract. Developers with more experience might need just days or weeks.

What is a smart contract with an example? ›

Smart contracts eliminate intermediaries by automatically enforcing terms once conditions are met. Think of a smart contract like a vending machine. When you insert a dollar, you get a co*ke. The machine follows built-in rules, similar to if-then statements in code.

What is the point of a smart contract? ›

On blockchain, the goal of a smart contract is to simplify business and trade between both anonymous and identified parties, sometimes without the need for a middleman. A smart contract scales down on formality and costs associated with traditional methods, without compromising on authenticity and credibility.

Are smart contracts a good idea? ›

Pros of Smart Contracts

Trustworthiness: If stored on a decentralised blockchain, such as Ethereum, there's no risk of tampering or altering the contract once deployed. Both parties can have complete trust in the contract's execution.

What are the top 10 smart contracts? ›

The top 10 best smart contract platforms in 2024 are Ethereum, Binance Smart Chain (BSC), TRON, Arbitrum, Cardano, Solana, Polygon, Algorand, Avalanche, and Tezos.

What are the problems with smart contracts? ›

Conclusion. Smart contracts have the potential to revolutionize various industries by enabling automation, trust, and efficiency. However, their development is fraught with challenges, including complexity, coding errors, security vulnerabilities, and legal uncertainties.

How long does it take to learn smart contracts? ›

On average, it might take a few weeks to become comfortable with the basics of Solidity and start writing simple smart contracts. To become proficient and tackle more complex projects, it could take several months to a year or more of consistent learning and practice.

Is Solidity worth learning in 2024? ›

If you feel certain about dedicating a few years of your life to learning Solidity, give it a try. It's quite probable that you will have a reliable constant career for years to come.

How much do smart contract developers make in the US? ›

Smart Contract Developer Salary - Jul 2024
PositionAvg Yearly SalaryMax Yearly Salary
Smart Contract Developer$120k$250k
Python Developer$120k$300k
NFT$120k$250k
AI Engineer$110k$180k
31 more rows

Can AI write smart contracts? ›

Developers can leverage machine learning algorithms and AI's analytical capabilities for optimization of the complete smart contract development lifecycle. AI-powered smart contracts can automate time-intensive tasks and minimize human errors, thereby improving efficiency and performance of smart contracts.

What language are smart contracts written in? ›

Solidity is the dominant language for writing smart contracts on the Ethereum Virtual Machine (EVM). Its mature syntax, vast developer community, and abundant learning resources make it an ideal choice for both seasoned developers and newcomers to the smart contract realm.

What are the basics of smart contracts? ›

Smart contracts allow developers to build apps that take advantage of blockchain security, reliability, and accessibility while offering sophisticated peer-to-peer functionality — everything from loans and insurance to logistics and gaming.

What is Ethereum introduction to smart contracts? ›

Smart contracts are self-executing computer programs that run on the Ethereum blockchain and enforce the terms of an agreement automatically. Developers write these contracts in a high-level programming language and compile them into low-level bytecode, which the Ethereum blockchain stores.

What is a smart contract and explain its life cycle? ›

Smart contracts are programs that execute when certain conditions are met and are stored on a blockchain. Typically, smart contracts are used to automate the execution of an agreement so that all parties can be certain of the decision immediately, without the need for intermediaries or time-consuming delays.

What is the primary purpose of a smart contract in Web3 development? ›

The main goal of these contracts is to increase security and reduce transaction costs associated with traditional contracts. Their code contains a set of rules outlining how the parties to the contract agree to interact with each other. If these rules are met, the contract is automatically enforced.

What is the legal meaning of smart contract? ›

First coined by Nick Szabo in 1997, a smart legal contract is defined as a piece of code stored on a blockchain that self-executes contract terms when certain conditions are met.

Top Articles
4 Factors to Consider to Get Your Product Pricing Right
Wall Street Favorites: 3 Cloud Computing Stocks With Strong Buy Ratings for June 2024 
Camera instructions (NEW)
Celebrity Extra
Mopaga Game
Otterbrook Goldens
Red Wing Care Guide | Fat Buddha Store
سریال رویای شیرین جوانی قسمت 338
Palace Pizza Joplin
Derpixon Kemono
Zoebaby222
Spelunking The Den Wow
Bros Movie Wiki
Pro Groom Prices – The Pet Centre
Summoner Class Calamity Guide
RBT Exam: What to Expect
Samsung Galaxy S24 Ultra Negru dual-sim, 256 GB, 12 GB RAM - Telefon mobil la pret avantajos - Abonament - In rate | Digi Romania S.A.
Price Of Gas At Sam's
Moviesda3.Com
60 X 60 Christmas Tablecloths
Extra Virgin Coconut Oil Walmart
Nick Pulos Height, Age, Net Worth, Girlfriend, Stunt Actor
Ruben van Bommel: diepgang en doelgerichtheid als wapens, maar (nog) te weinig rendement
Bing Chilling Words Romanized
Energy Healing Conference Utah
Kcwi Tv Schedule
Great Clips Grandview Station Marion Reviews
Craigslist Apartments Baltimore
Munis Self Service Brockton
Walgreens Bunce Rd
Mta Bus Forums
Star Wars Armada Wikia
Grave Digger Wynncraft
In hunt for cartel hitmen, Texas Ranger's biggest obstacle may be the border itself (2024)
Log in or sign up to view
Basil Martusevich
Rund um die SIM-Karte | ALDI TALK
Shoreone Insurance A.m. Best Rating
The Blackening Showtimes Near Regal Edwards Santa Maria & Rpx
Austin Automotive Buda
Hannibal Mo Craigslist Pets
Raisya Crow on LinkedIn: Breckie Hill Shower Video viral Cucumber Leaks VIDEO Click to watch full…
Academic important dates - University of Victoria
Craigslist Pets Huntsville Alabama
Orion Nebula: Facts about Earth’s nearest stellar nursery
Me Tv Quizzes
Best Restaurants Minocqua
Conan Exiles Armor Flexibility Kit
Secrets Exposed: How to Test for Mold Exposure in Your Blood!
Twizzlers Strawberry - 6 x 70 gram | bol
Billings City Landfill Hours
Noelleleyva Leaks
Latest Posts
Article information

Author: Reed Wilderman

Last Updated:

Views: 6097

Rating: 4.1 / 5 (52 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Reed Wilderman

Birthday: 1992-06-14

Address: 998 Estell Village, Lake Oscarberg, SD 48713-6877

Phone: +21813267449721

Job: Technology Engineer

Hobby: Swimming, Do it yourself, Beekeeping, Lapidary, Cosplaying, Hiking, Graffiti

Introduction: My name is Reed Wilderman, I am a faithful, bright, lucky, adventurous, lively, rich, vast person who loves writing and wants to share my knowledge and understanding with you.