Smart contract languages
Huling pag-edit: @wackerow(opens in a new tab), Hunyo 17, 2024
A great aspect about Ethereum is that smart contracts can be programmed using relatively developer-friendly languages. If you're experienced with Python or any curly-bracket language(opens in a new tab), you can find a language with familiar syntax.
The two most active and maintained languages are:
- Solidity
- Vyper
Remix IDE provides a comprehensive development environment for creating and testing contracts in both Solidity and Vyper. Try the in-browser Remix IDE(opens in a new tab) to start coding.
More experienced developers also might want to use Yul, an intermediate language for the Ethereum Virtual Machine, or Yul+, an extension to Yul.
If you're curious and like to help test new languages that are still under heavy development you can experiment with Fe, an emerging smart contract language which is currently still in its infancy.
Prerequisites
Previous knowledge of programming languages, especially of JavaScript or Python, can help you make sense of differences in smart contract languages. We also recommend you understand smart contracts as a concept before digging too deep into the language comparisons. Intro to smart contracts.
Solidity
- Object-oriented, high-level language for implementing smart contracts.
- Curly-bracket language that has been most profoundly influenced by C++.
- Statically typed (the type of a variable is known at compile time).
- Supports:
- Inheritance (you can extend other contracts).
- Libraries (you can create reusable code that you can call from different contracts – like static functions in a static class in other object oriented programming languages).
- Complex user-defined types.
Important links
- Documentation(opens in a new tab)
- Solidity Language Portal(opens in a new tab)
- Solidity by Example(opens in a new tab)
- GitHub(opens in a new tab)
- Solidity Gitter Chatroom(opens in a new tab) bridged to Solidity Matrix Chatroom(opens in a new tab)
- Cheat Sheet(opens in a new tab)
- Solidity Blog(opens in a new tab)
- Solidity Twitter(opens in a new tab)
Example contract
1// SPDX-License-Identifier: GPL-3.02pragma solidity >= 0.7.0;34contract Coin {5 // The keyword "public" makes variables6 // accessible from other contracts7 address public minter;8 mapping (address => uint) public balances;910 // Events allow clients to react to specific11 // contract changes you declare12 event Sent(address from, address to, uint amount);1314 // Constructor code is only run when the contract15 // is created16 constructor() {17 minter = msg.sender;18 }1920 // Sends an amount of newly created coins to an address21 // Can only be called by the contract creator22 function mint(address receiver, uint amount) public {23 require(msg.sender == minter);24 require(amount < 1e60);25 balances[receiver] += amount;26 }2728 // Sends an amount of existing coins29 // from any caller to an address30 function send(address receiver, uint amount) public {31 require(amount <= balances[msg.sender], "Insufficient balance.");32 balances[msg.sender] -= amount;33 balances[receiver] += amount;34 emit Sent(msg.sender, receiver, amount);35 }36}Ipakita lahatKopyahin
This example should give you a sense of what Solidity contract syntax is like. For a more detailed description of the functions and variables, see the docs(opens in a new tab).
Vyper
- Pythonic programming language
- Strong typing
- Small and understandable compiler code
- Efficient bytecode generation
- Deliberately has less features than Solidity with the aim of making contracts more secure and easier to audit. Vyper does not support:
- Modifiers
- Inheritance
- Inline assembly
- Function overloading
- Operator overloading
- Recursive calling
- Infinite-length loops
- Binary fixed points
For more information, read the Vyper rationale(opens in a new tab).
Important links
- Documentation(opens in a new tab)
- Vyper by Example(opens in a new tab)
- More Vyper by Example(opens in a new tab)
- GitHub(opens in a new tab)
- Vyper community Discord chat(opens in a new tab)
- Cheat Sheet(opens in a new tab)
- Smart contract development frameworks and tools for Vyper
- VyperPunk - learn to secure and hack Vyper smart contracts(opens in a new tab)
- VyperExamples - Vyper vulnerability examples(opens in a new tab)
- Vyper Hub for development(opens in a new tab)
- Vyper greatest hits smart contract examples(opens in a new tab)
- Awesome Vyper curated resources(opens in a new tab)
Example
1# Open Auction23# Auction params4# Beneficiary receives money from the highest bidder5beneficiary: public(address)6auctionStart: public(uint256)7auctionEnd: public(uint256)89# Current state of auction10highestBidder: public(address)11highestBid: public(uint256)1213# Set to true at the end, disallows any change14ended: public(bool)1516# Keep track of refunded bids so we can follow the withdraw pattern17pendingReturns: public(HashMap[address, uint256])1819# Create a simple auction with `_bidding_time`20# seconds bidding time on behalf of the21# beneficiary address `_beneficiary`.22@external23def __init__(_beneficiary: address, _bidding_time: uint256):24 self.beneficiary = _beneficiary25 self.auctionStart = block.timestamp26 self.auctionEnd = self.auctionStart + _bidding_time2728# Bid on the auction with the value sent29# together with this transaction.30# The value will only be refunded if the31# auction is not won.32@external33@payable34def bid():35 # Check if bidding period is over.36 assert block.timestamp < self.auctionEnd37 # Check if bid is high enough38 assert msg.value > self.highestBid39 # Track the refund for the previous high bidder40 self.pendingReturns[self.highestBidder] += self.highestBid41 # Track new high bid42 self.highestBidder = msg.sender43 self.highestBid = msg.value4445# Withdraw a previously refunded bid. The withdraw pattern is46# used here to avoid a security issue. If refunds were directly47# sent as part of bid(), a malicious bidding contract could block48# those refunds and thus block new higher bids from coming in.49@external50def withdraw():51 pending_amount: uint256 = self.pendingReturns[msg.sender]52 self.pendingReturns[msg.sender] = 053 send(msg.sender, pending_amount)5455# End the auction and send the highest bid56# to the beneficiary.57@external58def endAuction():59 # It is a good guideline to structure functions that interact60 # with other contracts (i.e. they call functions or send ether)61 # into three phases:62 # 1. checking conditions63 # 2. performing actions (potentially changing conditions)64 # 3. interacting with other contracts65 # If these phases are mixed up, the other contract could call66 # back into the current contract and modify the state or cause67 # effects (ether payout) to be performed multiple times.68 # If functions called internally include interaction with external69 # contracts, they also have to be considered interaction with70 # external contracts.7172 # 1. Conditions73 # Check if auction endtime has been reached74 assert block.timestamp >= self.auctionEnd75 # Check if this function has already been called76 assert not self.ended7778 # 2. Effects79 self.ended = True8081 # 3. Interaction82 send(self.beneficiary, self.highestBid)Ipakita lahatKopyahin
This example should give you a sense of what Vyper contract syntax is like. For a more detailed description of the functions and variables, see the docs(opens in a new tab).
Yul and Yul+
If you're new to Ethereum and haven't done any coding with smart contract languages yet, we recommend getting started with Solidity or Vyper. Only look into Yul or Yul+ once you're familiar with smart contract security best practices and the specifics of working with the EVM.
Yul
- Intermediate language for Ethereum.
- Supports the EVM and Ewasm(opens in a new tab), an Ethereum flavored WebAssembly, and is designed to be a usable common denominator of both platforms.
- Good target for high-level optimisation stages that can benefit both EVM and Ewasm platforms equally.
Yul+
- A low-level, highly efficient extension to Yul.
- Initially designed for an optimistic rollup contract.
- Yul+ can be looked at as an experimental upgrade proposal to Yul, adding new features to it.
Important links
- Yul Documentation(opens in a new tab)
- Yul+ Documentation(opens in a new tab)
- Yul+ Playground(opens in a new tab)
- Yul+ Introduction Post(opens in a new tab)
Example contract
The following simple example implements a power function. It can be compiled using solc --strict-assembly --bin input.yul
. The example should
be stored in the input.yul file.
1{2 function power(base, exponent) -> result3 {4 switch exponent5 case 0 { result := 1 }6 case 1 { result := base }7 default8 {9 result := power(mul(base, base), div(exponent, 2))10 if mod(exponent, 2) { result := mul(base, result) }11 }12 }13 let res := power(calldataload(0), calldataload(32))14 mstore(0, res)15 return(0, 32)16}Ipakita lahat
If you are already well experienced with smart contracts, a full ERC20 implementation in Yul can be found here(opens in a new tab).
Fe
- Statically typed language for the Ethereum Virtual Machine (EVM).
- Inspired by Python and Rust.
- Aims to be easy to learn -- even for developers who are new to the Ethereum ecosystem.
- Fe development is still in its early stages, the language had its alpha release in January 2021.
Important links
- GitHub(opens in a new tab)
- Fe Announcement(opens in a new tab)
- Fe 2021 Roadmap(opens in a new tab)
- Fe Discord Chat(opens in a new tab)
- Fe Twitter(opens in a new tab)
Example contract
The following is a simple contract implemented in Fe.
1type BookMsg = bytes[100]23contract GuestBook:4 pub guest_book: map<address, BookMsg>56 event Signed:7 book_msg: BookMsg89 pub def sign(book_msg: BookMsg):10 self.guest_book[msg.sender] = book_msg1112 emit Signed(book_msg=book_msg)1314 pub def get_msg(addr: address) -> BookMsg:15 return self.guest_book[addr].to_mem()16Ipakita lahat
How to choose
As with any other programming language, it's mostly about choosing the right tool for the right job as well as personal preferences.
Here are a few things to consider if you haven't tried any of the languages yet:
What is great about Solidity?
- If you are a beginner, there are many tutorials and learning tools out there. See more about that in the Learn by Coding section.
- Good developer tooling available.
- Solidity has a big developer community, which means you'll most likely find answers to your questions quite quickly.
What is great about Vyper?
- Great way to get started for Python devs that want to write smart contracts.
- Vyper has a smaller number of features which makes it great for quick prototyping of ideas.
- Vyper aims to be easy to audit and maximally human-readable.
What is great about Yul and Yul+?
- Simplistic and functional low-level language.
- Allows to get much closer to raw EVM, which can help to optimize the gas usage of your contracts.
Language comparisons
For comparisons of basic syntax, the contract lifecycle, interfaces, operators, data structures, functions, control flow, and more check out this cheatsheet by Auditless(opens in a new tab)