تخطٍ إلى المحتوى الرئيسي
Change page

Smart contract languages

آخر تعديل: @wackerow(opens in a new tab), ١٧ يونيو ٢٠٢٤

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.

Example contract

1// SPDX-License-Identifier: GPL-3.0
2pragma solidity >= 0.7.0;
3
4contract Coin {
5 // The keyword "public" makes variables
6 // accessible from other contracts
7 address public minter;
8 mapping (address => uint) public balances;
9
10 // Events allow clients to react to specific
11 // contract changes you declare
12 event Sent(address from, address to, uint amount);
13
14 // Constructor code is only run when the contract
15 // is created
16 constructor() {
17 minter = msg.sender;
18 }
19
20 // Sends an amount of newly created coins to an address
21 // Can only be called by the contract creator
22 function mint(address receiver, uint amount) public {
23 require(msg.sender == minter);
24 require(amount < 1e60);
25 balances[receiver] += amount;
26 }
27
28 // Sends an amount of existing coins
29 // from any caller to an address
30 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}
إظهار الكل
نسخ

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).

Example

1# Open Auction
2
3# Auction params
4# Beneficiary receives money from the highest bidder
5beneficiary: public(address)
6auctionStart: public(uint256)
7auctionEnd: public(uint256)
8
9# Current state of auction
10highestBidder: public(address)
11highestBid: public(uint256)
12
13# Set to true at the end, disallows any change
14ended: public(bool)
15
16# Keep track of refunded bids so we can follow the withdraw pattern
17pendingReturns: public(HashMap[address, uint256])
18
19# Create a simple auction with `_bidding_time`
20# seconds bidding time on behalf of the
21# beneficiary address `_beneficiary`.
22@external
23def __init__(_beneficiary: address, _bidding_time: uint256):
24 self.beneficiary = _beneficiary
25 self.auctionStart = block.timestamp
26 self.auctionEnd = self.auctionStart + _bidding_time
27
28# Bid on the auction with the value sent
29# together with this transaction.
30# The value will only be refunded if the
31# auction is not won.
32@external
33@payable
34def bid():
35 # Check if bidding period is over.
36 assert block.timestamp < self.auctionEnd
37 # Check if bid is high enough
38 assert msg.value > self.highestBid
39 # Track the refund for the previous high bidder
40 self.pendingReturns[self.highestBidder] += self.highestBid
41 # Track new high bid
42 self.highestBidder = msg.sender
43 self.highestBid = msg.value
44
45# Withdraw a previously refunded bid. The withdraw pattern is
46# used here to avoid a security issue. If refunds were directly
47# sent as part of bid(), a malicious bidding contract could block
48# those refunds and thus block new higher bids from coming in.
49@external
50def withdraw():
51 pending_amount: uint256 = self.pendingReturns[msg.sender]
52 self.pendingReturns[msg.sender] = 0
53 send(msg.sender, pending_amount)
54
55# End the auction and send the highest bid
56# to the beneficiary.
57@external
58def endAuction():
59 # It is a good guideline to structure functions that interact
60 # with other contracts (i.e. they call functions or send ether)
61 # into three phases:
62 # 1. checking conditions
63 # 2. performing actions (potentially changing conditions)
64 # 3. interacting with other contracts
65 # If these phases are mixed up, the other contract could call
66 # back into the current contract and modify the state or cause
67 # effects (ether payout) to be performed multiple times.
68 # If functions called internally include interaction with external
69 # contracts, they also have to be considered interaction with
70 # external contracts.
71
72 # 1. Conditions
73 # Check if auction endtime has been reached
74 assert block.timestamp >= self.auctionEnd
75 # Check if this function has already been called
76 assert not self.ended
77
78 # 2. Effects
79 self.ended = True
80
81 # 3. Interaction
82 send(self.beneficiary, self.highestBid)
إظهار الكل
نسخ

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

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.

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) -> result
3 {
4 switch exponent
5 case 0 { result := 1 }
6 case 1 { result := base }
7 default
8 {
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}
إظهار الكل

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.

Example contract

The following is a simple contract implemented in Fe.

1type BookMsg = bytes[100]
2
3contract GuestBook:
4 pub guest_book: map<address, BookMsg>
5
6 event Signed:
7 book_msg: BookMsg
8
9 pub def sign(book_msg: BookMsg):
10 self.guest_book[msg.sender] = book_msg
11
12 emit Signed(book_msg=book_msg)
13
14 pub def get_msg(addr: address) -> BookMsg:
15 return self.guest_book[addr].to_mem()
16
إظهار الكل

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)

Further reading

هل كانت هذه المقالة مفيدة؟