मुख्य सामग्री पर जाएँ
Change page

स्मार्ट अनुबंध भाषाएं

एथेरियम के बारे में एक बड़ा पहलू यह है कि स्मार्ट अनुबंध को अपेक्षाकृत डेवलपर के अनुकूल भाषाओं का उपयोग करके प्रोग्राम किया जा सकता है। यदि आप 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++ से सबसे अधिक गहराई से प्रभावित हुई है।
  • स्टैटिक रूप से टाइप किया गया (वेरिएबल का प्रकार संकलन समय पर जाना जाता है)।
  • समर्थन करता है:
    • विरासत (आप अन्य अनुबंधों का विस्तार कर सकते हैं)।
    • लाइब्रेरी (आप पुन: प्रयोज्य कोड बना सकते हैं जिसे आप विभिन्न अनुबंधों से कॉल कर सकते हैं – जैसे अन्य ऑब्जेक्ट ओरिएंटेड प्रोग्रामिंग भाषाओं में स्टैटिक वर्ग में स्टैटिक फंक्शंस)।
    • जटिल यूज़र-परिभाषित प्रकार।

उदाहरण अनुबंध

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}
सभी दिखाएँ
कॉपी करें

इस उदाहरण से आपको यह पता चल जाना चाहिए कि Solidity अनुबंध वाक्यविन्यास कैसा है। फंक्शंस और वेरिएबल्स के ज़्यादा विस्तृत विवरण के लिए, डॉक्स देखें(opens in a new tab)

Vyper

  • Pythonic प्रोग्रामिंग भाषा
  • मजबूत टाइपिंग
  • छोटा और समझने योग्य कंपाइलर कोड
  • कुशल बाइटकोड जेनरेशन
  • अनुबंधों को अधिक सुरक्षित और ऑडिट करना आसान बनाने के उद्देश्य से Solidity की तुलना में जानबूझकर कम विशेषताएं दी गई हैं। Vyper इनका समर्थन नहीं करता है:
    • संशोधक
    • इनहेरिटेंस
    • इनलाइन असेंबली
    • फंक्शन ओवरलोडिंग
    • ऑपरेटर ओवरलोडिंग
    • पुनरावर्ती कॉलिंग
    • अनंत-लंबाई के लूप
    • बाइनरी निश्चित बिंदु

अधिक जानकारी के लिए, Vyper तर्क पढ़ें(opens in a new tab)

उदाहरण

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)
सभी दिखाएँ
कॉपी करें

इस उदाहरण से आपको यह पता चल जाना चाहिए कि 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 के लिए एक प्रयोगात्मक अपग्रेड प्रस्ताव के रूप में देखा जा सकता है, इसमें नई सुविधाएँ जोड़ सकते हैं।

उदाहरण अनुबंध

निम्नलिखित सरल उदाहरण एक पावर फंक्शन को लागू करता है। इसे solc --strict-assembly --bin input.yul का उपयोग करके संकलित किया जा सकता है। उदाहरण input.yul फ़ाइल में स्टोर किया जाना चाहिए।

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}
सभी दिखाएँ

यदि आप पहले से ही स्मार्ट अनुबंधों के साथ अच्छी तरह से अनुभवी हैं, तो Yul में एक पूर्ण ERC20 कार्यान्वयन यहां(opens in a new tab) पाया जा सकता है।

Fe

  • एथेरियम वर्चुअल मशीन (EVM) के लिए स्टैटिक रूप से टाइप की गई भाषा।
  • Python और Rust से प्रेरित।
  • सीखने में आसान होने का लक्ष्य है -- यहां तक कि उन डेवलपर्स के लिए भी जो एथेरियम पारिस्थितिकी इकोसिस्टम में नए हैं।
  • Fe का विकास अभी भी अपने शुरुआती चरण में है, जनवरी 2021 में भाषा की अल्फा रिलीज़ हुई थी।

उदाहरण अनुबंध

निम्नलिखित 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
सभी दिखाएँ

कैसे चुनें

किसी भी अन्य प्रोग्रामिंग भाषा की तरह, यह ज्यादातर सही काम के साथ-साथ व्यक्तिगत प्राथमिकताओं के लिए सही उपकरण चुनने के बारे में है।

यदि आपने अभी तक किसी भी भाषा को आजमाया नहीं है, तो यहां कुछ बातें दी गई हैं जिन पर आपको विचार करना चाहिए:

Solidity के बारे में क्या अच्छा है?

  • यदि आप एक नौसिखिया हैं, तो कई ट्यूटोरियल और सीखने के उपकरण उपलब्ध हैं। इसके बारे में अधिक जानकारी के लिए कोडिंग द्वारा सीखें सेक्शन देखें।
  • अच्छा डेवलपर टूलींग उपलब्ध है।
  • Solidity में एक बड़ा डिवेलपर समुदाय है, जिसका अर्थ है कि आपको अपने प्रश्नों के उत्तर बहुत जल्दी मिल जाएंगे।

Vyper के बारे में क्या बढ़िया है?

  • Python डेवलपर के लिए आरंभ करने का शानदार तरीका जो स्मार्ट अनुबंध लिखना चाहते हैं।
  • Vyper में कम विशेषताएं हैं जो विचारों के त्वरित प्रोटोटाइप के लिए इसे शानदार बनाती हैं।
  • Vyper का उद्देश्य ऑडिट करना आसान और अधिकतम मानव-पठनीय होना है।

Yul और Yul+ के बारे में क्या बढ़िया है?

  • सरलीकृत और कार्यात्मक निम्न-स्तरीय भाषा।
  • अधूरी EVM के बहुत करीब पहुंचने देता है, जो आपके अनुबंधों के गैस उपयोग को अनुकूलित करने में मदद कर सकता है।

भाषा की तुलना

मूल वाक्यविन्यास, अनुबंध जीवनचक्र, इंटरफेस, ऑपरेटर, डेटा संरचनाएं, फंक्शंस, नियंत्रण प्रवाह आदि की तुलना के लिए Auditless द्वारा इस चीटशीट(opens in a new tab) को देखें

अग्रिम पठन

क्या यह लेख सहायक था?