स्मार्ट कॉन्ट्रॅक्ट भाषा
पृष्ठ अखेरचे अद्यतन: २६ फेब्रुवारी, २०२६
Ethereum बद्दल एक उत्तम गोष्ट ही आहे की स्मार्ट कॉन्ट्रॅक्ट्स तुलनेने विकसक-अनुकूल भाषा वापरून प्रोग्राम केले जाऊ शकतात. तुम्ही Python किंवा कोणत्याही कर्ली-ब्रॅकेट भाषेमध्ये (opens in a new tab) अनुभवी असाल, तर तुम्हाला परिचित सिंटॅक्स असलेली भाषा सापडू शकते.
दोन सर्वात सक्रिय आणि देखरेख केलेल्या भाषा आहेत:
- Solidity
- Vyper
Remix IDE हे Solidity आणि Vyper या दोन्हीमधील कॉन्ट्रॅक्ट्स तयार करण्यासाठी आणि तपासण्यासाठी एक सर्वसमावेशक विकास वातावरण प्रदान करते. कोडिंग सुरू करण्यासाठी ब्राउझर-मधील Remix IDE वापरून पाहा (opens in a new tab).
अधिक अनुभवी विकसक Ethereum Virtual Machine साठी एक मध्यस्थ भाषा 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 Matrix चॅटरूम (opens in a new tab) जे Solidity Matrix चॅटरूम (opens in a new tab) शी जोडलेले आहे
- चीट शीट (opens in a new tab)
- Solidity ब्लॉग (opens in a new tab)
- Solidity ट्विटर (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
- पायथॉनिक प्रोग्रामिंग भाषा
- स्ट्रॉंग टायपिंग
- लहान आणि समजण्याजोगा कंपाइलर कोड
- कार्यक्षम बाईटकोड निर्मिती
- 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)
- GitHub (opens in a new tab)
- Vyper कम्युनिटी डिस्कॉर्ड चॅट (opens in a new tab)
- चीट शीट (opens in a new tab)
- Vyper साठी स्मार्ट कॉन्ट्रॅक्ट डेव्हलपमेंट फ्रेमवर्क आणि टूल्स
- VyperPunk - 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+
तुम्ही Ethereum साठी नवीन असाल आणि अद्याप स्मार्ट कॉन्ट्रॅक्ट भाषांसह कोडिंग केले नसेल, तर आम्ही Solidity किंवा Vyper सह प्रारंभ करण्याची शिफारस करतो. स्मार्ट कॉन्ट्रॅक्ट सुरक्षा सर्वोत्तम पद्धती आणि EVM सह काम करण्याच्या तपशीलांशी तुम्ही परिचित झाल्यावरच Yul किंवा Yul+ चा विचार करा.
Yul
- Ethereum साठी मध्यस्थ भाषा.
- EVM आणि Ewasm (opens in a new tab), एक Ethereum फ्लेवर्ड वेबअसेंब्ली, ला समर्थन देते आणि दोन्ही प्लॅटफॉर्म्सचा वापरण्यायोग्य समान विभाजक म्हणून डिझाइन केले आहे.
- उच्च-स्तरीय ऑप्टिमायझेशन टप्प्यांसाठी चांगले लक्ष्य जे EVM आणि Ewasm दोन्ही प्लॅटफॉर्मला समान फायदा देऊ शकतात.
Yul+
- Yul चे एक निम्न-स्तरीय, अत्यंत कार्यक्षम विस्तारीकरण.
- सुरुवातीला optimistic rollup कॉन्ट्रॅक्टसाठी डिझाइन केलेले.
- Yul+ ला Yul साठी एक प्रायोगिक अपग्रेड प्रस्ताव म्हणून पाहिले जाऊ शकते, जे त्यात नवीन वैशिष्ट्ये जोडते.
महत्वाच्या लिंक्स
- 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
- Ethereum व्हर्च्युअल मशीन (EVM) साठी स्टॅटिकली टाइप केलेली भाषा.
- Python आणि Rust पासून प्रेरित.
- अगदी Ethereum इकोसिस्टममध्ये नवीन असलेल्या विकसकांसाठीही शिकण्यास सोपे बनविण्याचे उद्दिष्ट आहे.
- Fe चा विकास अजूनही सुरुवातीच्या टप्प्यात आहे, या भाषेची अल्फा आवृत्ती जानेवारी २०२१ मध्ये प्रसिद्ध झाली.
महत्वाच्या लिंक्स
- GitHub (opens in a new tab)
- Fe घोषणा (opens in a new tab)
- Fe 2021 रोडमॅप (opens in a new tab)
- Fe डिस्कॉर्ड चॅट (opens in a new tab)
- Fe ट्विटर (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) पहा