Smart Kontract Languages
Last edit: @boluwatife_4523(opens in a new tab), 17 Jun 2024
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.
Important links
- Dokumentashon(opens in a new tab)
- Solidity Languaj Portal(opens in a new tab)
- Solidity by Eksampol(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)
Kontract Eksampol
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}Show mi evrytinKopy
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).
Important links
- Dokumentashon(opens in a new tab)
- Vyper by Eksampol(opens in a new tab)
- More Vyper by Eksampol(opens in a new tab)
- GitHub(opens in a new tab)
- Vyper komunity Discord chat(opens in a new tab)
- Cheat Sheet(opens in a new tab)
- Smart kontract divelopment frameworks and tools for Vyper
- VyperPunk - learn hau yu fit sekure and hack Vyper smart kontracts(opens in a new tab)
- VyperEksampols - Vyper vulnerability eksampols(opens in a new tab)
- Vyper Hub for divelopment(opens in a new tab)
- Vyper hits smart kontracts eksampols wey great pass(opens in a new tab)
- Awesome Vyper don kurate risorsis(opens in a new tab)
Eksampol
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)Show mi evrytinKopy
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
- Intamediate languaj for Ethereum.
- Make yu support di EVM and Ewasm(opens in a new tab), one Ethereum flavored WebAssembly, and dey design to bi yusabol komon denonimator of di two platfoms.
- Good target for high-level optimizashon stages wey fit benefit EVM and Ewasm platfoms di same way.
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.
Important links
- Yul Dokumentashon(opens in a new tab)
- Yul+ Dokumentashon(opens in a new tab)
- Yul+ Playground(opens in a new tab)
- Yul+ Intro Post(opens in a new tab)
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) -> 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}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.
Important links
- GitHub(opens in a new tab)
- Fe Anounsment(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)
Kontract Eksampol
Di followin na simpol kontract wey dem put inside 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()16Show 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)