स्मार्ट अनुबंध भाषाएं
अंतिम संपादन: @wackerow(opens in a new tab), 17 जून 2024
एथेरियम के बारे में एक बड़ा पहलू यह है कि स्मार्ट अनुबंध को अपेक्षाकृत डेवलपर के अनुकूल भाषाओं का उपयोग करके प्रोग्राम किया जा सकता है। यदि आप Python या किसी कर्ली-ब्रैकेट भाषा(opens in a new tab) को जानते हैं, तो आप परिचित वाक्यविन्यास वाली भाषा पा सकते हैं।
दो सबसे सक्रिय और अनुरक्षित भाषाएं हैं:
- Solidity
- Vyper
रीमिक्स IDE Solidity और Vyper दोनों में अनुबंध बनाने और परीक्षण करने के लिए एक व्यापक विकास परिवेश प्रदान करता है। कोडिंग शुरू करने के लिए इन-ब्राउज़र रीमिक्स IDE आज़माएं(opens in a new tab)।
अधिक अनुभवी डेवलपर्स भी Yul का उपयोग करना चाह सकते हैं, यह एथेरियम वर्चुअल मशीन या Yul+ के लिए एक मध्यवर्ती भाषा है, जो Yul का एक्सटेंशन है।
यदि आप उत्सुक हैं और नई भाषाओं का परीक्षण करने में मदद करना चाहते हैं जो अभी भी गहन विकास के अधीन हैं, तो आप Fe के साथ प्रयोग कर सकते हैं, जो उभरती हुई स्मार्ट अनुबंध भाषा है जो अभी भी अपनी प्रारंभिक अवस्था में है।
आवश्यक शर्तें
प्रोग्रामिंग भाषाओं, विशेष रूप से JavaScript या Python का पिछला ज्ञान, आपको स्मार्ट अनुबंध भाषाओं में अंतर की समझ बनाने में मदद कर सकता है। हम यह भी अनुशंसा करते हैं कि आप भाषा की तुलना में बहुत अधिक गहराई में जाने से पहले स्मार्ट अनुबंधों को एक अवधारणा के रूप में समझ लें। स्मार्ट अनुबंधों का परिचय।
Solidity
- स्मार्ट अनुबंधों को लागू करने के लिए ऑब्जेक्ट-ओरिएंटेड, उच्च-स्तरीय भाषा।
- कर्ली-ब्रैकेट भाषा जो C++ से सबसे अधिक गहराई से प्रभावित हुई है।
- स्टैटिक रूप से टाइप किया गया (वेरिएबल का प्रकार संकलन समय पर जाना जाता है)।
- समर्थन करता है:
- विरासत (आप अन्य अनुबंधों का विस्तार कर सकते हैं)।
- लाइब्रेरी (आप पुन: प्रयोज्य कोड बना सकते हैं जिसे आप विभिन्न अनुबंधों से कॉल कर सकते हैं – जैसे अन्य ऑब्जेक्ट ओरिएंटेड प्रोग्रामिंग भाषाओं में स्टैटिक वर्ग में स्टैटिक फंक्शंस)।
- जटिल यूज़र-परिभाषित प्रकार।
जरूरी लिंक
- प्रलेखन(opens in a new tab)
- Solidity भाषा का पोर्टल(opens in a new tab)
- उदाहरण के लिए Solidity(opens in a new tab)
- GitHub(opens in a new tab)
- Solidity Gitter चैटरूम(opens in a new tab) को Solidity Matrix चैटरूम(opens in a new tab) से जोड़ा गया
- चीट शीट(opens in a new tab)
- Solidity ब्लॉग(opens in a new tab)
- Solidity Twitter(opens in a new tab)
उदाहरण अनुबंध
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}सभी दिखाएँकॉपी करें
इस उदाहरण से आपको यह पता चल जाना चाहिए कि Solidity अनुबंध वाक्यविन्यास कैसा है। फंक्शंस और वेरिएबल्स के ज़्यादा विस्तृत विवरण के लिए, डॉक्स देखें(opens in a new tab)।
Vyper
- Pythonic प्रोग्रामिंग भाषा
- मजबूत टाइपिंग
- छोटा और समझने योग्य कंपाइलर कोड
- कुशल बाइटकोड जेनरेशन
- अनुबंधों को अधिक सुरक्षित और ऑडिट करना आसान बनाने के उद्देश्य से Solidity की तुलना में जानबूझकर कम विशेषताएं दी गई हैं। Vyper इनका समर्थन नहीं करता है:
- संशोधक
- इनहेरिटेंस
- इनलाइन असेंबली
- फंक्शन ओवरलोडिंग
- ऑपरेटर ओवरलोडिंग
- पुनरावर्ती कॉलिंग
- अनंत-लंबाई के लूप
- बाइनरी निश्चित बिंदु
अधिक जानकारी के लिए, Vyper तर्क पढ़ें(opens in a new tab)।
जरूरी लिंक
- प्रलेखन(opens in a new tab)
- उदाहरण के तौर पर Vyper(opens in a new tab)
- उदाहरण के तौर पर अधिक Vyper(opens in a new tab)
- गिटहब(opens in a new tab)
- Vyper समुदाय Discord चैट(opens in a new tab)
- चीट शीट(opens in a new tab)
- Vyper के लिए स्मार्ट अनुबंध विकास ढांचे और उपकरण
- VyperPunk - Vyper स्मार्ट अनुबंध को सुरक्षित और हैक करने के तरीके सीखें(opens in a new tab)
- VyperExamples - Vyper कमजोरी के उदाहरण(opens in a new tab)
- विकास के लिए Vyper हब(opens in a new tab)
- Vyper ने स्मार्ट अनुबंध उदाहरणों को सबसे ज्यादा हिट किया(opens in a new tab)
- बहुत बढ़िया Vyper क्यूरेटेड संसाधन(opens in a new tab)
उदाहरण
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)सभी दिखाएँकॉपी करें
इस उदाहरण से आपको यह पता चल जाना चाहिए कि Vyper अनुबंध वाक्यविन्यास कैसा है। फंक्शंस और वेरिएबल्स के ज़्यादा विस्तृत विवरण के लिए, डॉक्स देखें(opens in a new tab)।
Yul और Yul+
यदि आप एथेरियम के लिए नए हैं और अभी तक स्मार्ट अनुबंध भाषाओं के साथ कोई कोडिंग नहीं की है, तो हम अनुशंसा करते हैं कि आप Solidity या Vyper के साथ शुरुआत करें। स्मार्ट अनुबंध सुरक्षा सर्वोत्तम प्रथाओं और EVM के साथ काम करने की बारीकियों से परिचित होने के बाद ही Yul या Yul+ को देखें।
Yul
- एथेरियम के लिए मध्यवर्ती भाषा।
- EVM और Ewasm(opens in a new tab), एक एथेरियम फ्लेवर्ड WebAssembly का समर्थन करता है, और इसे दोनों प्लेटफार्मों के प्रयोग करने योग्य सामान्य भाजक के रूप में डिज़ाइन किया गया है।
- उच्च-स्तरीय अनुकूलन चरणों के लिए अच्छा लक्ष्य जो EVM और Ewasm दोनों प्लेटफार्मों को समान रूप से लाभान्वित कर सकता है।
Yul+
- Yul के लिए एक निम्न-स्तरीय, अत्यधिक कुशल एक्सटेंशन।
- प्रारंभ में आशावादी रोलअप अनुबंध के लिए डिज़ाइन किया गया था।
- Yul+ को Yul के लिए एक प्रयोगात्मक अपग्रेड प्रस्ताव के रूप में देखा जा सकता है, इसमें नई सुविधाएँ जोड़ सकते हैं।
जरूरी लिंक
- Yul प्रलेखन(opens in a new tab)
- Yul+ प्रलेखन(opens in a new tab)
- Yul+ खेल का मैदान(opens in a new tab)
- Yul+ परिचय पोस्ट(opens in a new tab)
उदाहरण अनुबंध
निम्नलिखित सरल उदाहरण एक पावर फंक्शन को लागू करता है। इसे solc --strict-assembly --bin input.yul
का उपयोग करके संकलित किया जा सकता है। उदाहरण input.yul फ़ाइल में स्टोर किया जाना चाहिए।
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}सभी दिखाएँ
यदि आप पहले से ही स्मार्ट अनुबंधों के साथ अच्छी तरह से अनुभवी हैं, तो Yul में एक पूर्ण ERC20 कार्यान्वयन यहां(opens in a new tab) पाया जा सकता है।
Fe
- एथेरियम वर्चुअल मशीन (EVM) के लिए स्टैटिक रूप से टाइप की गई भाषा।
- Python और Rust से प्रेरित।
- सीखने में आसान होने का लक्ष्य है -- यहां तक कि उन डेवलपर्स के लिए भी जो एथेरियम पारिस्थितिकी इकोसिस्टम में नए हैं।
- Fe का विकास अभी भी अपने शुरुआती चरण में है, जनवरी 2021 में भाषा की अल्फा रिलीज़ हुई थी।
जरूरी लिंक
- GitHub(opens in a new tab)
- Fe की घोषणा(opens in a new tab)
- Fe 2021 रोडमैप(opens in a new tab)
- Fe Discord चैट(opens in a new tab)
- Fe Twitter(opens in a new tab)
उदाहरण अनुबंध
निम्नलिखित 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()16सभी दिखाएँ
कैसे चुनें
किसी भी अन्य प्रोग्रामिंग भाषा की तरह, यह ज्यादातर सही काम के साथ-साथ व्यक्तिगत प्राथमिकताओं के लिए सही उपकरण चुनने के बारे में है।
यदि आपने अभी तक किसी भी भाषा को आजमाया नहीं है, तो यहां कुछ बातें दी गई हैं जिन पर आपको विचार करना चाहिए:
Solidity के बारे में क्या अच्छा है?
- यदि आप एक नौसिखिया हैं, तो कई ट्यूटोरियल और सीखने के उपकरण उपलब्ध हैं। इसके बारे में अधिक जानकारी के लिए कोडिंग द्वारा सीखें सेक्शन देखें।
- अच्छा डेवलपर टूलींग उपलब्ध है।
- Solidity में एक बड़ा डिवेलपर समुदाय है, जिसका अर्थ है कि आपको अपने प्रश्नों के उत्तर बहुत जल्दी मिल जाएंगे।
Vyper के बारे में क्या बढ़िया है?
- Python डेवलपर के लिए आरंभ करने का शानदार तरीका जो स्मार्ट अनुबंध लिखना चाहते हैं।
- Vyper में कम विशेषताएं हैं जो विचारों के त्वरित प्रोटोटाइप के लिए इसे शानदार बनाती हैं।
- Vyper का उद्देश्य ऑडिट करना आसान और अधिकतम मानव-पठनीय होना है।
Yul और Yul+ के बारे में क्या बढ़िया है?
- सरलीकृत और कार्यात्मक निम्न-स्तरीय भाषा।
- अधूरी EVM के बहुत करीब पहुंचने देता है, जो आपके अनुबंधों के गैस उपयोग को अनुकूलित करने में मदद कर सकता है।
भाषा की तुलना
मूल वाक्यविन्यास, अनुबंध जीवनचक्र, इंटरफेस, ऑपरेटर, डेटा संरचनाएं, फंक्शंस, नियंत्रण प्रवाह आदि की तुलना के लिए Auditless द्वारा इस चीटशीट(opens in a new tab) को देखें