Skip go main kontent
Change page

Smart Kontract Languages

One ogbonge part about Ethereum bi sey dem fit program smart kontracts yusin languajis wey divelopas sabi wella. If yu don sabi Python wella abi any languaj wey dey yus curly-bracket(opens in a new tab)bifor, yu fit find one languaj wey get syntax yu sabi.

Di two langusjis wey dey aktive and wey dem dey maintain pass na:

  • Solidity
  • Vyper

Remis IDE dey provide ogbonge divelopment environment to dey kreate and dey test kontracts for Solidity and Vyper. Try di in-browsa Remix IDE(opens in a new tab) to start to dey code.

Divelopas wey sabi pass fit wont make yu yus Yul, one intamediate languaj for di Ethereum Virtual Machine,, abi Yul+, one ekstenshon to Yul.

If yu dey kurios and laik to helep test new languajis wey still dey onda heavy divelopment yu fit eksperiment wit Fe, one smart kontract languaj wey dey emerge wey just dey start.

Prerequisites

Knowlej of programming languaj wey dey bifor, espeshialy of JavaScript abi Python, fit helep yu make sense of difrens in smart kontract languajis. Wi also rekomend make yu ondastand smart kontracts as one konsept bifor diggin deep-deep into di languaj komparisons. Intro to smart kontracts.

Solidity

  • Object-oriented, high-level languaj to dey implement smart kontracts.
  • Curly-bracket languaj wey C++ don influens wella.
  • Statikally typed (dem don sabi di type of one variabol at kompile taim).
  • Supports:
    • Inheritans (wey yu fit ekstend oda kontracts).
    • Libraries (yu fit kreate kode wey yu fit yus again wey yu fit koll from difren kontracts - laik statik funshons for one statik klass in oda programming languajis wey base on object).
    • Komplex user-defined types.

Kontract Eksampol

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}
Show mi evrytin
Kopy

Dis eksampol suppose give yu one sense of wetin Solidity kontract syntax bi laik. For more detailed diskripshon of di funshons and variabols, see di docs(opens in a new tab).

Vyper

  • Pythonic programming languaj
  • Strong typing
  • Smoll and kompiler code wey dem fit ondastand
  • Efficient bytecode generashon
  • Deliberately get less feashures pass Solidity wit di aim to dey make kontracts more sekure and izy pass to audit. Vyper nor dey support:
    • Modifiers
    • Inheritans
    • Inline assembly
    • Funshon wey dey ovaload
    • Operator wey dey ovaload
    • Recursive kolling
    • Infinite-length loops
    • Binary fixed points

For more informashon, read di Vyper rashonale(opens in a new tab).

Eksampol

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)
Show mi evrytin
Kopy

Dis eksampol suppose give yu one sense of wetin Vyper kontract syntax bi laik. For more full diskripshon of di funshons and variabols, see di docs(opens in a new tab).

Yul and Yul+

If yu dey new to Ethereum and neva do any coding wit smart kontract languajis yet, wi rekomend to start wit Solidity abi Vyper. Only look into Yul abi Yul+ wons yu dey familiar wit smart kontract sekurity best praktis and di spesifiks of working wit di EVM.

Yul

Yul+

  • One low-level, very effishient ekstenshon to Yul.
  • Dem first disign am for optimistik rollup kontract.
  • Yu fit look Yul+ as eksperimental upgrade proposal to Yul, as dem dey add new tins to am.

Kontract Eksampol

Dis simpol eksampol dey show hau to do pawa funshon. Dem fit gada am to dey yus solc --strict-assembly --bin input.yul. Yu suppose store di eksampol in di 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}
Show mi evrytin

If yu don already sabi smart kontracts wella, yu fit find one full ERC20 implimentashon in Yul here(opens in a new tab).

Fe

  • Na languaj wey dem statikaly type for di Ethereum Virtual Machine (EVM).
  • Python and Rust inspaya am.
  • Dem wont make am izy to learn -- even for di divelopas wey just dey enta Ethereum ekosystem.
  • Fe divelopment just dey start, di languaj just get im first alpha riliz for January 2021.

Kontract Eksampol

Di followin na simpol kontract wey dem put inside 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
Show mi evrytin

Hau yu fit shuse

Just laik any oda programming languaj, na to dey shuse di koret tool for di koret work and wetin pesin laik.

Here na some tins to tink about if yu neva try any of di languajis:

Wetin dey great about Solidity?

  • If yu bi otondo, plenti tutorials and tools to yus learn dey. Make yu shek di Learn by Coding sekshon for more.
  • Tools for ogbonge developa dey afailabol.
  • Solidity get big divelopa komunity, wey mean sey yu go kwik find ansa to yor kweshon.

Wetin dey great about Vyper?

  • Na great way to start for Python devs wey wan write smart kontracts.
  • Vyper get smoll numba of features wey make am great for idia wey yu fit kopy.
  • Vyper dey make am izy to audit and make humans fit read am wella.

Wetin dey great about Yul and Yul+?

  • Na low-level languaj wey simpol and dey funshon.
  • E dey allow make pesin dey klose to raw EVM, wey fit helep optimize di gas wey yor kontracts dey yus.

Languaj komparisons

If yu wan kompia basik syntax, di kontract lifecycle, intafaces, operators, data strukshures, funshons, kontrol flow, and more tins, make yu shek dis cheatsheet wey Auditless do(opens in a new tab)

Further reading

Shey dis artikol dey helep?