परिचय वेब3 डीएपी विकास के लिए तकनीकी स्टैक को समझने से, आपने वेब3 डीएपी विकास के लिए कोर तकनीकी स्टैक, डीएपी विकास में आरपीसी की भूमिका और खाता बनाने, एपीआई कुंजी, एंडपॉइंट्स, एंडपॉइंट्स एनालिटिक्स बनाने, अपने डीआरपीसी खाते में धनराशि जोड़ने और अपनी शेष राशि की जांच करने के लिए डीआरपीसी का उपयोग कैसे करें, यह सीखा होगा। स्मार्ट कॉन्ट्रैक्ट्स को तैनात करने में dRPC की भूमिका एथेरियम नोड स्थापित करने की प्रक्रिया को सरल बनाना है, जिससे डेवलपर्स के लिए कोड की सिर्फ एक पंक्ति के साथ बातचीत और तैनाती करना आसान हो जाता है। इस लेख में, आप dRPC एंडपॉइंट और API कुंजी का उपयोग करके एथेरियम सेपोलिया टेस्टनेट पर कॉफी भुगतान स्मार्ट अनुबंध लिखेंगे, संकलित करेंगे, परीक्षण करेंगे और तैनात करेंगे। इसकी विशेषताएं इस प्रकार हैं: कॉफ़ी के लिए भुगतान कॉफी की कीमत की समीक्षा बेची गई कॉफ़ी की कुल संख्या और अर्जित कुल धनराशि प्राप्त करना चलो अपने हाथ गंदे करें। आवश्यक शर्तें सेपोलिया नल के साथ अपने खाते को पहले से ही निधिबद्ध करें। एक वॉलेट रखें जैसे, मेटामास्क। कोड संपादक. आपकी पसंद की कोई भी JS लाइब्रेरी या फ्रेमवर्क पहले से इंस्टॉल है (जैसे React.js, Next.js आदि)। पानी का जार. आवश्यक प्रौद्योगिकियाँ और उपकरण दृढ़ता. Vite.js का उपयोग करके React.js(टाइपस्क्रिप्ट) कठोर टोपी। वेब3.js. डोटेनव. dRPC API कुंजी और समापन बिंदु. आपके खाते की निजी कुंजी. मेटामास्क कॉफ़ी भुगतान स्मार्ट अनुबंध लिखना अपनी रूट निर्देशिका के अंतर्गत एक फ़ोल्डर बनाएं, और उसका नाम । contracts फ़ोल्डर के अंतर्गत एक फ़ाइल बनाएं और उसका नाम रखें। contracts coffee.sol आप स्मार्ट कॉन्ट्रैक्ट लिखने के लिए सॉलिडिटी का उपयोग करेंगे। सॉलिडिटी फ़ाइलों को एक्सटेंशन के साथ नामित किया गया है क्योंकि यह सॉलिडिटी स्रोत कोड के लिए मानक फ़ाइल एक्सटेंशन है। .sol में निम्नलिखित स्रोत कोड जोड़ें: coffee.sol // SPDX-License-Identifier: MIT pragma solidity >=0.8.0 <0.9.0; contract Coffee { uint256 public constant coffeePrice = 0.0002 ether; uint256 public totalCoffeesSold; uint256 public totalEtherReceived; // Custom error definitions error QuantityMustBeGreaterThanZero(); error InsufficientEtherSent(uint256 required, uint256 sent); error DirectEtherTransferNotAllowed(); // Event to log coffee purchases event CoffeePurchased(address indexed buyer, uint256 quantity, uint256 totalCost); // Function to buy coffee function buyCoffee(uint256 quantity) external payable { if (quantity <= 0) { revert QuantityMustBeGreaterThanZero(); } uint256 totalCost = coffeePrice * quantity; if (msg.value > totalCost) { revert InsufficientEtherSent(totalCost, msg.value); } // Update the total coffees sold and total ether received totalCoffeesSold += quantity; totalEtherReceived += totalCost; console.log("Total ether received updated:", totalEtherReceived); console.log("Total coffee sold updated:", totalCoffeesSold); // Emit the purchase event emit CoffeePurchased(msg.sender, quantity, totalCost); // Refund excess Ether sent if (msg.value > totalCost) { uint256 refundAmount = msg.value - totalCost; payable(msg.sender).transfer(refundAmount); } } // Fallback function to handle Ether sent directly to the contract receive() external payable { revert DirectEtherTransferNotAllowed(); } // Public view functions to get totals function getTotalCoffeesSold() external view returns (uint256) { console.log("getTotalCoffeesSold :", totalCoffeesSold); return totalCoffeesSold; } function getTotalEtherReceived() external view returns (uint256) { console.log("getTotalEtherReceived :", totalEtherReceived); return totalEtherReceived; } } प्रग्मा : यह लाइसेंस पहचानकर्ता इंगित करता है कि कोड लाइसेंस के अंतर्गत लाइसेंसीकृत है। //SPDX-License-Identifier: MIT मैसाचुसेट्स इंस्टीट्यूट ऑफ टेक्नोलॉजी (MIT) : निर्दिष्ट करता है कि कोड 0.8.0 (समावेशी) और 0.9.0 (अनन्य) के बीच सॉलिडिटी संस्करणों के लिए लिखा गया है। pragma solidity >=0.8.0 <0.9.0; राज्य चर uint256 public constant coffeePrice = 0.0002 ether; uint256 public totalCoffeesSold; uint256 public totalEtherReceived; : के स्थिर मान के रूप में सेट करें। coffeePrice 0.0002 ether : बेची गई कॉफ़ी की संख्या को ट्रैक करता है. totalCoffeesSold : अनुबंध द्वारा प्राप्त कुल ईथर को ट्रैक करता है। totalEtherReceived कस्टम त्रुटियाँ सॉलिडिटी में कस्टम त्रुटियाँ वे उपयोगकर्ता अनुभव को बेहतर बनाने में मदद कर सकते हैं, और स्मार्ट कॉन्ट्रैक्ट को डिबग करने और बनाए रखने में भी मदद कर सकते हैं। त्रुटि संदेश हैं जो किसी विशिष्ट उपयोग के मामले के लिए तैयार किए जाते हैं, न कि प्रोग्रामिंग भाषा द्वारा प्रदान किए जाने वाले डिफ़ॉल्ट त्रुटि संदेश। सॉलिडिटी में कस्टम त्रुटि परिभाषित करने के लिए, आप निम्नलिखित सिंटैक्स का उपयोग कर सकते हैं: : इस कीवर्ड का उपयोग कस्टम त्रुटि को परिभाषित करने के लिए किया जाता है error अद्वितीय नाम: त्रुटि का एक अद्वितीय नाम होना चाहिए पैरामीटर: यदि आप त्रुटि संदेश में विशिष्ट विवरण या पैरामीटर शामिल करना चाहते हैं, तो आप उन्हें त्रुटि नाम के बाद कोष्ठक में जोड़ सकते हैं। error QuantityMustBeGreaterThanZero(); error InsufficientEtherSent(uint256 required, uint256 sent); error DirectEtherTransferNotAllowed(); : यह सुनिश्चित करता है कि मात्रा शून्य से अधिक है। QuantityMustBeGreaterThanZero() : यह सुनिश्चित करता है कि भेजा गया ईथर पर्याप्त है। InsufficientEtherSent(uint256 required, uint256 sent) : अनुबंध में प्रत्यक्ष ईथर स्थानांतरण को रोकता है। DirectEtherTransferNotAllowed() घटनाक्रम इवेंट अनुबंध का एक हिस्सा है जो उत्सर्जित होने पर ट्रांजेक्शन लॉग में पारित तर्कों को संग्रहीत करता है। ईवेंट का उपयोग आमतौर पर EVM की लॉगिंग सुविधा का उपयोग करके अनुबंध की वर्तमान स्थिति के बारे में कॉलिंग एप्लिकेशन को सूचित करने के लिए किया जाता है। वे अनुबंधों में किए गए परिवर्तनों के बारे में अनुप्रयोगों को सूचित करते हैं, जिसका उपयोग तब संबंधित तर्क चलाने के लिए किया जा सकता है। event CoffeePurchased(address indexed buyer, uint256 quantity, uint256 totalCost); : कॉफी खरीदारी लॉग करता है। CoffeePurchased(address indexed buyer, uint256 quantity, uint256 totalCost) कार्य वे कोड के एक ही हिस्से को फिर से लिखने की अनावश्यकता को खत्म करते हैं। इसके बजाय, जब आवश्यक हो तो डेवलपर प्रोग्राम में फ़ंक्शन को कॉल कर सकते हैं। फ़ंक्शन कोड के स्व-निहित मॉड्यूल होते हैं जो किसी विशिष्ट कार्य को पूरा करते हैं। function buyCoffee(uint256 quantity) external payable { if (quantity <= 0) { revert QuantityMustBeGreaterThanZero(); } uint256 totalCost = coffeePrice * quantity; if (msg.value > totalCost) { revert InsufficientEtherSent(totalCost, msg.value); } // Update the total coffees sold and total ether received totalCoffeesSold += quantity; totalEtherReceived += totalCost; console.log("Total ether received updated:", totalEtherReceived); console.log("Total coffee sold updated:", totalCoffeesSold); // Emit the purchase event emit CoffeePurchased(msg.sender, quantity, totalCost); // Refund excess Ether sent if (msg.value > totalCost) { uint256 refundAmount = msg.value - totalCost; payable(msg.sender).transfer(refundAmount); } } receive() external payable { revert DirectEtherTransferNotAllowed(); } function getTotalCoffeesSold() external view returns (uint256) { console.log("getTotalCoffeesSold :", totalCoffeesSold); return totalCoffeesSold; } function getTotalEtherReceived() external view returns (uint256) { console.log("getTotalEtherReceived :", totalEtherReceived); return totalEtherReceived; } : कॉफी खरीद को संभालता है और निम्नलिखित संचालन करता है: buyCoffee(uint256 quantity) external payable जाँच करें कि मात्रा वैध है या नहीं। कुल लागत की गणना करता है. यह सुनिश्चित करता है कि पर्याप्त मात्रा में ईथर भेजा जाए। राज्य चर को अद्यतन करता है. खरीदारी ईवेंट उत्सर्जित करता है. अतिरिक्त ईथर की वापसी। : यदि कोई व्यक्ति सीधे अनुबंध पते पर धन भेजता है तो प्रत्यक्ष ईथर स्थानान्तरण को वापस कर देता है। receive() external payable : बेची गई कुल कॉफ़ी लौटाता है। getTotalCoffeesSold() external view returns (uint256) : प्राप्त कुल ईथर रिटर्न करता है। getTotalEtherReceived() external view returns (uint256) कॉफ़ी भुगतान स्मार्ट अनुबंध का संकलन यहां, आप स्मार्ट अनुबंध को संकलित करने के लिए हार्डहैट का उपयोग करेंगे। निम्नलिखित कमांड प्रॉम्प्ट का उपयोग करके हार्डहैट स्थापित करें। npm install --save-dev hardhat सफल स्थापना के बाद आपको नीचे दिया गया उत्तर मिलेगा। उसी निर्देशिका में जहां आप इस कमांड प्रॉम्प्ट का उपयोग करके हार्डहैट को आरंभ करते हैं: npx hardhat init और एंटर दबाएं। नीचे तीर बटन का उपयोग करके Create a Javascript project चयन करें रूट फ़ोल्डर में इंस्टॉल करने के लिए एंटर दबाएं जिसमें भी शामिल हैं अपने कीबोर्ड पर y का उपयोग करके सभी संकेतों को स्वीकार करें, @nomicfoundation/hardhat-toolbox निर्भरताएं आप नीचे यह प्रतिक्रिया देख रहे हैं जो दर्शाती है कि आपने सफलतापूर्वक आरंभीकरण कर लिया है आप देखेंगे कि आपके प्रोजेक्ट में कुछ नए फ़ोल्डर और फ़ाइलें जोड़ी गई हैं। उदाहरण के लिए, Lock.sol , iginition/modules , test/Lock.js और hardhat.config.cjs । इनके बारे में चिंता न करें। ही उपयोगी हैं और हटाने के लिए स्वतंत्र हैं। केवल iginition/modules और hardhat.config.cjs । आपको बाद में पता चलेगा कि इनका क्या उपयोग है। आप contracts फ़ोल्डर के अंतर्गत Lock.sol iginition/modules फ़ोल्डर के अंतर्गत Lock.js निम्नलिखित कमांड प्रॉम्प्ट का उपयोग करके अनुबंध संकलित करें: npx hardhat compile आपको इस तरह के अतिरिक्त फ़ोल्डर्स और फ़ाइलें दिखाई देंगी। फ़ाइल के Coffee.json अंदर JSON प्रारूप में ABI कोड है जिसे आप स्मार्ट कॉन्ट्रैक्ट के साथ इंटरैक्ट करते समय कॉल करेंगे। { "_format": "hh-sol-artifact-1", "contractName": "Coffee", "sourceName": "contracts/coffee.sol", "abi": [ { "inputs": [], "name": "DirectEtherTransferNotAllowed", "type": "error" }, { "inputs": [ { "internalType": "uint256", "name": "required", "type": "uint256" }, { "internalType": "uint256", "name": "sent", "type": "uint256" } ], "name": "InsufficientEtherSent", "type": "error" }, { "inputs": [], "name": "QuantityMustBeGreaterThanZero", "type": "error" }, { "anonymous": false, "inputs": [ { "indexed": true, "internalType": "address", "name": "buyer", "type": "address" }, { "indexed": false, "internalType": "uint256", "name": "quantity", "type": "uint256" }, { "indexed": false, "internalType": "uint256", "name": "totalCost", "type": "uint256" } ], "name": "CoffeePurchased", "type": "event" }, { "inputs": [ { "internalType": "uint256", "name": "quantity", "type": "uint256" } ], "name": "buyCoffee", "outputs": [], "stateMutability": "payable", "type": "function" }, { "inputs": [], "name": "coffeePrice", "outputs": [ { "internalType": "uint256", "name": "", "type": "uint256" } ], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "getTotalCoffeesSold", "outputs": [ { "internalType": "uint256", "name": "", "type": "uint256" } ], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "getTotalEtherReceived", "outputs": [ { "internalType": "uint256", "name": "", "type": "uint256" } ], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "totalCoffeesSold", "outputs": [ { "internalType": "uint256", "name": "", "type": "uint256" } ], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "totalEtherReceived", "outputs": [ { "internalType": "uint256", "name": "", "type": "uint256" } ], "stateMutability": "view", "type": "function" }, { "stateMutability": "payable", "type": "receive" } ], "bytecode": "", "deployedBytecode": "", "linkReferences": {}, "deployedLinkReferences": {} } स्मार्ट कॉन्ट्रैक्ट का परीक्षण अपने स्मार्ट कॉन्ट्रैक्ट को बनाते समय एक स्वचालित परीक्षण स्क्रिप्ट लिखना महत्वपूर्ण है और इसकी अत्यधिक अनुशंसा की जाती है। यह दो-कारक प्रमाणीकरण (2FA) की तरह काम करता है, यह सुनिश्चित करता है कि आपका स्मार्ट कॉन्ट्रैक्ट लाइव नेटवर्क पर तैनात होने से पहले अपेक्षित रूप से प्रदर्शन करे। फ़ोल्डर रखें। फ़ाइल के अंदर, नीचे दिया गया कोड पेस्ट करें: test के अंतर्गत एक नई फ़ाइल बनाएँ, और उसका नाम Coffee. const { loadFixture } = require("@nomicfoundation/hardhat-toolbox/network-helpers.js"); const { expect } = require("chai"); const pkg = require("hardhat"); const ABI = require('../artifacts/contracts/coffee.sol/Coffee.json'); const { web3 } = pkg; describe("Coffee Contract", function () { // Fixture to deploy the Coffee contract async function deployCoffeeFixture() { const coffeeContract = new web3.eth.Contract(ABI.abi); coffeeContract.handleRevert = true; const [deployer, buyer] = await web3.eth.getAccounts(); const rawContract = coffeeContract.deploy({ data: ABI.bytecode, }); // Estimate gas for the deployment const estimateGas = await rawContract.estimateGas({ from: deployer }); // Deploy the contract const coffee = await rawContract.send({ from: deployer, gas: estimateGas.toString(), gasPrice: "10000000000", }); console.log("Coffee contract deployed to: ", coffee.options.address); return { coffee, deployer, buyer, rawContract }; } describe("Deployment", function () { // Test to check initial values after deployment it("Should set the initial values correctly", async function () { const { coffee } = await loadFixture(deployCoffeeFixture); const totalCoffeesSold = await coffee.methods.totalCoffeesSold().call(); const totalEtherReceived = await coffee.methods.totalEtherReceived().call(); expect(totalCoffeesSold).to.equal("0"); expect(totalEtherReceived).to.equal("0"); }); }); describe("Buying Coffee", function () { // Test to check coffee purchase and event emission it("Should purchase coffee and emit an event", async function () { const { coffee, buyer } = await loadFixture(deployCoffeeFixture); const quantity = 3; const totalCost = web3.utils.toWei("0.0006", "ether"); // Buyer purchases coffee const receipt = await coffee.methods.buyCoffee(quantity).send({ from: buyer, value: totalCost }); // Check event const event = receipt.events.CoffeePurchased; expect(event).to.exist; expect(event.returnValues.buyer).to.equal(buyer); expect(event.returnValues.quantity).to.equal(String(quantity)); expect(event.returnValues.totalCost).to.equal(totalCost); }); // Test to check revert when quantity is zero it("Should revert if the quantity is zero", async function () { const { coffee, buyer } = await loadFixture(deployCoffeeFixture); expect( coffee.methods.buyCoffee(0).send({ from: buyer, value: web3.utils.toWei("0.0002", "ether") }) ).to.be.revertedWith("QuantityMustBeGreaterThanZero"); }); // Test to check if totalCoffeesSold and totalEtherReceived are updated correctly it("Should update totalCoffeesSold and totalEtherReceived correctly", async function () { const { coffee, buyer } = await loadFixture(deployCoffeeFixture); const quantity = 5; const totalCost = web3.utils.toWei("0.001", "ether"); await coffee.methods.buyCoffee(quantity).send({ from: buyer, value: totalCost }); const totalCoffeesSold = await coffee.methods.totalCoffeesSold().call(); const totalEtherReceived = await coffee.methods.totalEtherReceived().call(); expect(totalCoffeesSold).to.equal(String(quantity)); expect(totalEtherReceived).to.equal(totalCost); }); }); describe("Fallback function", function () { // Test to check revert when ether is sent directly to the contract it("Should revert if ether is sent directly to the contract", async function () { const { coffee, buyer } = await loadFixture(deployCoffeeFixture); expect( web3.eth.sendTransaction({ from: buyer, to: coffee.options.address, value: web3.utils.toWei("0.001", "ether"), }) ).to.be.revertedWith("DirectEtherTransferNotAllowed"); }); }); }); यह कोड कॉफ़ी स्मार्ट कॉन्ट्रैक्ट की कार्यक्षमता का परीक्षण करता है। इसमें तैनाती, कॉफ़ी खरीदना और कॉन्ट्रैक्ट में सीधे ईथर ट्रांसफ़र को संभालने के लिए परीक्षण शामिल हैं। यहाँ इसका विवरण दिया गया है: फिक्सचर फ़ंक्शन: deployCoffeeFixture async function deployCoffeeFixture() { const coffeeContract = new web3.eth.Contract(ABI.abi); coffeeContract.handleRevert = true; const [deployer, buyer] = await web3.eth.getAccounts(); const rawContract = coffeeContract.deploy({ data: ABI.bytecode, }); const estimateGas = await rawContract.estimateGas({ from: deployer }); const coffee = await rawContract.send({ from: deployer, gas: estimateGas.toString(), gasPrice: "10000000000", }); console.log("Coffee contract deployed to: ", coffee.options.address); return { coffee, deployer, buyer, rawContract }; } : एक नया अनुबंध उदाहरण बनाता है और इसे तैनातीकर्ता के खाते का उपयोग करके तैनात करता है। कॉफी अनुबंध को तैनात करता है : तैनाती के लिए आवश्यक गैस का अनुमान लगाता है। गैस का अनुमान : परिनियोजित अनुबंध इंस्टैंस, परिनियोजनकर्ता, और क्रेता खाते. रिटर्न परिनियोजन परीक्षण describe("Deployment", function () { it("Should set the initial values correctly", async function () { const { coffee } = await loadFixture(deployCoffeeFixture); const totalCoffeesSold = await coffee.methods.totalCoffeesSold().call(); const totalEtherReceived = await coffee.methods.totalEtherReceived().call(); expect(totalCoffeesSold).to.equal("0"); expect(totalEtherReceived).to.equal("0"); }); }); : सुनिश्चित करता है कि तैनाती के बाद और शून्य पर सेट हैं। प्रारंभिक मानों की जाँच करता है totalCoffeesSold totalEtherReceived कॉफ़ी खरीदने का परीक्षण describe("Buying Coffee", function () { it("Should purchase coffee and emit an event", async function () { const { coffee, buyer } = await loadFixture(deployCoffeeFixture); const quantity = 3; const totalCost = web3.utils.toWei("0.0006", "ether"); const receipt = await coffee.methods.buyCoffee(quantity).send({ from: buyer, value: totalCost }); const event = receipt.events.CoffeePurchased; expect(event).to.exist; expect(event.returnValues.buyer).to.equal(buyer); expect(event.returnValues.quantity).to.equal(String(quantity)); expect(event.returnValues.totalCost).to.equal(totalCost); }); it("Should revert if the quantity is zero", async function () { const { coffee, buyer } = await loadFixture(deployCoffeeFixture); expect( coffee.methods.buyCoffee(0).send({ from: buyer, value: web3.utils.toWei("0.0002", "ether") }) ).to.be.revertedWith("QuantityMustBeGreaterThanZero"); }); it("Should update totalCoffeesSold and totalEtherReceived correctly", async function () { const { coffee, buyer } = await loadFixture(deployCoffeeFixture); const quantity = 5; const totalCost = web3.utils.toWei("0.001", "ether"); await coffee.methods.buyCoffee(quantity).send({ from: buyer, value: totalCost }); const totalCoffeesSold = await coffee.methods.totalCoffeesSold().call(); const totalEtherReceived = await coffee.methods.totalEtherReceived().call(); expect(totalCoffeesSold).to.equal(String(quantity)); expect(totalEtherReceived).to.equal(totalCost); }); }); : परीक्षण करता है कि कॉफी खरीदने से स्थिति अपडेट होती है और ईवेंट उत्सर्जित होता है। कॉफी खरीदना और एक ईवेंट उत्सर्जित करना CoffeePurchased : यह सुनिश्चित करता है कि यदि मात्रा शून्य है तो लेनदेन वापस लौट आए। शून्य मात्रा पर वापस लौटना : सत्यापित करता है कि खरीदारी के बाद और सही ढंग से अद्यतन किया गया है। स्थिति को सही ढंग से अद्यतन करना totalCoffeesSold totalEtherReceived फ़ॉलबैक फ़ंक्शन परीक्षण describe("Fallback function", function () { it("Should revert if ether is sent directly to the contract", async function () { const { coffee, buyer } = await loadFixture(deployCoffeeFixture); expect( web3.eth.sendTransaction({ from: buyer, to: coffee.options.address, value: web3.utils.toWei("0.001", "ether"), }) ).to.be.revertedWith("DirectEtherTransferNotAllowed"); }); }); : यह सुनिश्चित करता है कि अनुबंध में सीधे ईथर भेजने से (फ़ंक्शन को कॉल किए बिना) लेनदेन प्रतिवर्ती हो जाता है। प्रत्यक्ष ईथर स्थानांतरण पर प्रतिवर्ती स्मार्ट कॉन्ट्रैक्ट का परीक्षण परीक्षण स्क्रिप्ट लिखने के बाद, आप : हार्डहैट नेटवर्क पर अपने अनुबंध और परीक्षण चलाते समय, आप अपने सॉलिडिटी कोड से इसका उपयोग करने के लिए, आपको अपने अनुबंध कोड में इस तरह से console.log() को कॉल करके लॉगिंग संदेश और अनुबंध चर प्रिंट कर सकते हैं। hardhat/console.sol आयात करना होगा : //SPDX-License-Identifier: MIT pragma solidity >=0.8.0 <0.9.0; import "hardhat/console.sol"; contract Coffee { //... } अनुबंध का परीक्षण करने के लिए, अपने टर्मिनल में निम्नलिखित कमांड चलाएँ: npx hardhat test आपके पास नीचे दिए गए जैसा आउटपुट होना चाहिए: इससे पता चलता है कि आपका स्मार्ट अनुबंध अपेक्षित तरीके से कार्य करता है। चलाते हैं यदि आप npx hardhat test तो यह स्वचालित रूप से स्मार्ट कॉन्ट्रैक्ट को संकलित और परीक्षण करता है। आप इसे आज़मा सकते हैं और मुझे टिप्पणी अनुभाग में बता सकते हैं। स्मार्ट कॉन्ट्रैक्ट को लागू करना यहाँ, आप अपने स्मार्ट कॉन्ट्रैक्ट को टेस्टनेट आपको अपने स्मार्ट कॉन्ट्रैक्ट को ऐसे वातावरण में परीक्षण करने की अनुमति देता है जो बिना किसी महत्वपूर्ण लागत के एथेरियम मेननेट की नकल करता है। यदि आप dApp के कार्य से अच्छे हैं, तो आप एथेरियम मेननेट पर फिर से तैनात कर सकते हैं। सेपोलिया टेस्टनेट पर तैनात करेंगे। Dotenv पैकेज और इन निर्भरताओं को स्थापित करें। npm install dotenv npm install --save-dev @nomicfoundation/hardhat-web3-v4 'web3@4' यह Web3.Js और Dotenv को 'node_modules' फ़ोल्डर में शामिल करके आपके प्रोजेक्ट में जोड़ देगा। में आयात करें उन्हें अपनी hardhat.config.cjs फ़ाइल require('dotenv').config(); require("@nomicfoundation/hardhat-toolbox"); require("@nomicfoundation/hardhat-web3-v4"); const HardhatUserConfig = require("hardhat/config"); module.exports = { solidity: "0.8.24", } }; एक फ़ाइल । अपने रूट फ़ोल्डर में .env बनाएँ अपने मेटामास्क वॉलेट और dRPC API कुंजी से अपने खाते की निजी कुंजी प्राप्त करें। इन्हें अपनी .env फ़ाइल में संग्रहीत करें. DRPC_API_KEY=your_drpc_api_key PRIVATE_KEY=your_wallet_private_key फ़ाइल को : Sepolia टेस्टनेट कॉन्फ़िगरेशन को शामिल करने के लिए hardhat.config.cjs अपडेट करें require('dotenv').config(); require("@nomicfoundation/hardhat-toolbox"); require("@nomicfoundation/hardhat-web3-v4"); const HardhatUserConfig = require("hardhat/config"); const dRPC_API_KEY = process.env.VITE_dRPC_API_KEY; const PRIVATE_KEY = process.env.VITE_PRIVATE_KEY; module.exports = { solidity: "0.8.24", networks: { sepolia: { url: `https://lb.drpc.org/ogrpc?network=sepolia&dkey=${dRPC_API_KEY}`, accounts: [`0x${PRIVATE_KEY}`], } } }; फ़ोल्डर नाम दें ignition/module के अंतर्गत एक नई स्क्रिप्ट फ़ाइल बनाएँ , और इसे deploy.cjs । अपने स्मार्ट कॉन्ट्रैक्ट को तैनात करने के लिए निम्न कोड जोड़ें: const { buildModule } = require("@nomicfoundation/hardhat-ignition/modules"); const CoffeeModule = buildModule("CoffeeModule", (m) => { const coffee = m.contract("Coffee"); return { coffee }; }); module.exports = CoffeeModule; अपने टर्मिनल में निम्नलिखित कमांड चलाकर स्मार्ट कॉन्ट्रैक्ट तैनात करें: npx hardhat ignition deploy ./ignition/modules/deploy.cjs --network sepolia कमांड प्रॉम्प्ट चलाने के बाद, आपसे पूछा जाएगा कि , टाइप करें। सफल तैनाती पर आपको टर्मिनल में अपने तैनात स्मार्ट कॉन्ट्रैक्ट का पता देखना चाहिए। Confirm deploy to network sepolia (11155111)? (y/n) y आप फ़ाइल में अनुबंध पते तक भी पहुँच सकते हैं। deployed_addresses.json बधाई हो, आपने अपने स्मार्ट अनुबंध को सेपोलिया टेस्टनेट पर सफलतापूर्वक तैनात कर दिया है। निष्कर्ष इस लेख में आपको हार्डहैट CLI का उपयोग करके भुगतान स्मार्ट अनुबंध लिखना, परीक्षण करना, संकलित करना और स्मार्ट अनुबंध को तैनात करना सिखाया गया है। अगले लेख में, आप इस dApp के लिए फ्रंट एंड बनाना सीखेंगे। इस UI में निम्न शामिल होंगे: खरीदी गई कॉफ़ी की संख्या के लिए इनपुट फ़ील्ड. एक बटन जो भुगतान लेनदेन को सक्रिय करता है और आपके खाते से उसे काट लेता है। कुल खरीदी गई कॉफ़ी और ईथर और USD में प्राप्त राशि प्रदर्शित करें कॉफी की कीमत ईथर और अमरीकी डॉलर दोनों में। संदर्भ डिफ़ॉल्ट संदेशों से परे: सॉलिडिटी में कस्टम त्रुटियों पर नियंत्रण सॉलिडिटी में कस्टम त्रुटियों पर काबू पाना: अपने स्मार्ट कॉन्ट्रैक्ट्स को डिफ़ॉल्ट संदेशों से आगे ले जाना सॉलिडिटी फंक्शन क्या हैं? सॉलिडिटी में घटनाएँ क्या हैं? ← पिछला लेख अगला लेख →