paint-brush
ከመይ ጌርና dRPC API Keyን Endpointን ብምጥቃም Smart Contract ናብ Ethereum Network ነዋፍር@ileolami
3,010 ንባባት
3,010 ንባባት

ከመይ ጌርና dRPC API Keyን Endpointን ብምጥቃም Smart Contract ናብ Ethereum Network ነዋፍር

Ileolami23m2024/09/11
Read on Terminal Reader

ኣዝዩ ነዊሕ፤ ንምንባብ

ኣብዚ ጽሑፍ እዚ፡ dRPC endpointን API keyን ተጠቒምኩም፡ ናይ ቡን ክፍሊት ስማርት ውዕል ናብ Ethereum Sepolia Testnet ክትጽሕፉ፡ ከተዋህልሉ፡ ክትፍትኑን ከተዋፍሩን ኢኹም። እቶም ፍሉይነታት ድማ፡ ክፍሊት ቡን፡ ዋጋ ቡን ምግምጋም፡ ምውሳድ ጠቕላላ ቁጽሪ ዝተሸጠ ቡንን ጠቕላላ ዝተገብረ ገንዘብን ይርከብዎም።
featured image - ከመይ ጌርና dRPC API Keyን Endpointን ብምጥቃም Smart Contract ናብ Ethereum Network ነዋፍር
Ileolami HackerNoon profile picture
0-item

መእተዊ

ካብ ምርዳእ tech stack for Web3 DApp development ን web3 dApp development ዝኸውን core tech stack፣ ተራ RPC ኣብ dApp development፣ ከምኡ እውን dRPC ከመይ ጌርካ ኣካውንት ንምፍጣር፣ API key ንምፍጣር፣ endpoints፣ endpoints analytics ትጥቀም ተማሂርካ ክትከውን ኣለካ ፣ ኣብ dRPC ኣካውንትካ ገንዘብ ወስኽ፣ ሚዛንካ ድማ መርምር።


ተራ dRPC ኣብ ምዝርጋሕ ስማርት ውዕላት ንመስርሕ ምድላው Ethereum node ምቕላል እዩ፣ እዚ ድማ ንደቨሎፐራት ብሓደ መስመር ኮድ ጥራይ ንኽራኸቡን ንኽዝርግሑን ቀሊል ይገብሮም።


ኣብዚ ጽሑፍ እዚ፡ dRPC endpointን API keyን ተጠቒምካ፡ ናይ ቡን ክፍሊት ስማርት ውዕል ናብ Ethereum Sepolia Testnet ክትጽሕፍ፡ ከተዋህልል፡ ክትፍትኖን ከተዋፍርን ኢኻ።


እቶም ባህርያት ድማ፤

  1. ክፍሊት ንቡን
  2. ዋጋ ቡን ምግምጋም
  3. ምውሳድ ጠቕላላ ቁጽሪ ዝተሸጠ ቡንን ጠቕላላ ዝተገብረ መጠን ገንዘብን።


ኣእዳውኩም ንረክስ።

ቅድመ ኩነት

  1. ድሮ ንሕሳብኩም ብሴፖልያ ፋውሰት ምወላ።
  2. ዋለተ ይሃልኻ ንኣብነት ሜታማስክ።
  3. ኣዳላዊ ኮድ።
  4. ድሮ ዝኾነ ዝመረጽካዮ Js libraries ወይ frameworks (ንኣብነት React.js, Next.js ወዘተ) ተኺለካ።
  5. ዕትሮ ማይ።

ዘድልዩ ቴክኖሎጂታትን መሳርሕታትን

  1. ጽንዓት።
  2. React.js Vite.js(Typescript) ብምጥቃም
  3. ሃርድሃት።
  4. Web3.js.
  5. ዶተንቭ.
  6. dRPC ኤፒኣይ መፍትሕን መወዳእታ ነጥቢን።
  7. ናይ ኣካውንትካ ናይ ብሕቲ መፍትሕ።
  8. ሜታማስክ

ምጽሓፍ ናይ ቡን ክፍሊት ስማርት ውዕል

  1. ኣብ ትሕቲ ሱር ማህደርካ Folder ፍጠር፣ contracts ዝብል ስም ድማ ስመዮ።


  2. ኣብ ትሕቲ contracts ዝብል ማህደር ፋይል ፍጠር፣ coffee.sol ዝብል ስም ድማ ስመዮ።

    ሓደ ፋይል ማህደር "contracts" ዝብል ስም ዘለዎ ፎልደርን ኣብ ውሽጢ እቲ ፎልደር "coffee.sol" ዝበሃል ፋይልን ይርአ።

ነቲ ስማርት ውዕል ንምጽሓፍ solidity ክትጥቀም ኢኻ። ፋይላት ሶሊዲቲ ብ .sol ኤክስቴንሽን ዝተሰየሙ ምኽንያቱ ንሶሊዲቲ ምንጪ ኮድ ስታንዳርድ ፋይል ኤክስቴንሽን ስለዝኾነ።


  1. ኣብ 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; } }

ፕራግማ


  • pragma solidity >=0.8.0 <0.9.0; : እቲ ኮድ ኣብ መንጎ 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) : ዕድጊ ቡን ይምዝግብ።

ተግባራት

ተግባራት ንሓደ ፍሉይ ዕማም ዝፍጽሙ ርእሶም ዝኽእሉ ሞዱላት ኮድ እዮም። ተመሳሳሊ ቁራጽ ኮድ ዳግማይ ምጽሓፍ ዝብል ትርፊ የወግዱ። ኣብ ክንድኡስ፡ devs ኣብቲ ፕሮግራም ንዘሎ ፋንክሽን ኣድላዪ ኣብ ዝኾነሉ እዋን ክጽውዑ ይኽእሉ።

 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) : ጠቕላላ ዝተቐበለ ኢተር ይመልስ።

ምጥርናፍ ናይ ቡን ክፍሊት ስማርት ውዕል

ኣብዚ፡ ነቲ ስማርት ውዕል ንምጥርናፍ ሃርድሃት ክትጥቀሙ ኢኹም።


  1. ነዚ ዝስዕብ ትእዛዝ መመልከቲ ተጠቒምካ Hardhat ንጽዓኖ።

     npm install --save-dev hardhat


    ድሕሪ ዕዉት ምትካል እዚ ኣብ ታሕቲ ዘሎ መልሲ ክትረኽቡ ኢኹም።

    ውጽኢት ምትካል ሃርድሃት ዘርኢ ተርሚናል..


  2. ኣብ ተመሳሳሊ ማህደር ነዚ ናይ ትእዛዝ ምልክት ተጠቂምካ hardhat ትጅምረሉ፤

     npx hardhat init


  3. ነቲ ፍላጻ ንታሕቲ ዝብል ቁልፊ ብምጥቃም Create a Javascript project ምረጽ እሞ enter ንጽቀጥ።

  4. ኣብቲ ሱር ማህደር ንምትካል enter ንጽቀጥ

  5. ኩሎም ኣብ ቁልፊ ሰሌዳኻ y ብምጥቃም ዝቐርቡ ምልክታት ሓዊስካ @nomicfoundation/hardhat-toolbox ጽግዕተኛታት ተቐበል

  6. እዚ ኣብ ታሕቲ ዘሎ መልሲ ብዓወት ከም ዝጀመርካ ዘርኢ ትርእዮ

ገለ ሓደስቲ ማህደርን ፋይላትን ኣብ ፕሮጀክትካ ከም ዝተወሰኹ ከተስተብህል ኢኻ። ንኣብነት Lock.sol ፡ iginition iginition/modules ፡ test test/Lock.js ከምኡ’ውን hardhat.config.cjs ። ብዛዕባኦም ኣይትጨነቑ።


እቶም እንኮ ጠቐምቲ ድማ iginition/modules hardhat.config.cjs እዮም ። ንእንታይ ከም ዝውዕሉ ድሒርካ ክትፈልጥ ኢኻ። Lock.sol ኣብ ትሕቲ contracts folder ከምኡ ውን Lock.js ኣብ ትሕቲ iginition/modules folder ክትድምስሱ ናጻ ትኾኑ


  1. ነዚ ዝስዕብ ትእዛዝ ምልክት ተጠቒምካ ነቲ ውዕል ኣወሃህዶ፤

     npx hardhat compile 

  1. ከምዚኦም ዝኣመሰሉ ተወሳኺ ማህደርን ፋይላትን ትርኢ።

  1. ኣብ ውሽጢ 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. cjs. ኣብ ውሽጢ እቲ ፋይል ነዚ ኮድ ኣብ ታሕቲ ንለጥፎ :

 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 }; }
  • Deploys the Coffee contract : ሓድሽ ናይ ውዕል ምሳሌ ይፈጥርን ናይቲ ኣዋፋሪ ኣካውንት ተጠቒሙ የዋፍሮን።
  • ጋዝ ይግምት : ንምውፋር ዘድሊ ጋዝ ይግምት።
  • ምምላስ : እቲ ዝተዋፈረ ውዕል ኣብነት፣ ኣዋፋሪን ዓዳጊን ሕሳባት።

ናይ ምውፋር ፈተነታት

 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");  }); });
  • ናይ መጀመርታ ክብርታት ይምርምር : ድሕሪ ምዝርጋሕ totalCoffeesSoldtotalEtherReceived ን ናብ ዜሮ ከምዝተቐመጡ የረጋግጽ።

ናይ ቡን መርመራታት ምግዛእ

 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 ፍጻመ ከም ዝፍንዎን ይፍትን።
  • ኣብ ዜሮ ብዝሒ ምምላስ : እቲ ብዝሒ ዜሮ እንተኾይኑ እቲ ትራንዛክሽን ከም ዝምለስ የረጋግጽ።
  • ኩነታት ብትኽክል ምዕራፍ : ድሕሪ ዕድጊ totalCoffeesSoldtotalEtherReceived ን ብትኽክል ከምዝተመሓየሹ የረጋግጽ።

ፈተና ፋንክሽን ፋልባክ

 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");  }); });
  • ኣብ ቀጥታዊ ምትሕልላፍ ኢተር ምምላስ : ኢተር ብቐጥታ ናብቲ ውዕል ምልኣኽ (ንፋንክሽን ከይጸዋዕካ) ነቲ ትራንዛክሽን ከም ዝምለስ የረጋግጽ።

ምፍታን ስማርት ኮንትራክት

እቲ ናይ ፈተና ስክሪፕት ምስጸሓፍካ ድማ :

  1. ውዕላትካን ፈተነታትካን ኣብ ሃርድሃት ኔትወርክ ከተካይድ ከለኻ ፡ ካብ ኮድ ሶሊዲቲካ console.log() ዝጽውዑ ናይ ምዝገባ መልእኽትታትን ናይ ውዕል ተለዋዋጢ ዋጋታትን ክትሕትም ትኽእል። ንኽትጥቀመሉ ድማ ኣብ ናይ ውዕል ኮድካ hardhat/console.sol ከምዚ ጌርካ ከተእቱ ኣለካ፤
 //SPDX-License-Identifier: MIT pragma solidity >=0.8.0 <0.9.0; import "hardhat/console.sol"; contract Coffee { //... }
  1. ነቲ ውዕል ንምፍታን ኣብ ተርሚናልካ ነዚ ዝስዕብ ትእዛዝ ኣካይድ፤

     npx hardhat test


  2. ከምዚ ኣብ ታሕቲ ዘሎ ውጽኢት ክህልወካ ይግባእ፤

እዚ ድማ እቲ ስማርት ውዕልካ በቲ ትጽቢት ዝግበረሉ መንገዲ ከም ዝሰርሕ ዘርኢ እዩ።

npx hardhat test እንተ ኣካይድካ ብኣውቶማቲክ ነቲ ስማርት ውዕል ኣኪቡ ይፍትኖ። ክትፍትንዎ ትኽእሉ ኢኹም ኣብ ክፍሊ ኮመንት ድማ ክትሕብሩኒ ትኽእሉ ኢኹም።

ምውፋር ስማርት ኮንትራክት

ኣብዚ፡ ስማርት ውዕልካ ናብ ሰፖልያ ቴስትነት ከተዋፍር ኢኻ። Testnet ንስማርት ውዕልካ ኣብ ንEthereum mainnet ዝመስል ሃዋህው ክትፍትኖ የኽእለካ ርኡይ ወጻኢታት ከየጋጠመካ። ብተግባር ናይቲ dApp ንፉዕ እንተኾንካ፡ ድሕሪኡ ናብ Ethereum Mainnet ዳግማይ ክትዝርግሕ ትኽእል ኢኻ።


  1. ነቲ dotenv packageን እዞም ጽግዕተኛታትን ጽዓኑ።

     npm install dotenv npm install --save-dev @nomicfoundation/hardhat-web3-v4 'web3@4'

    እዚ ድማ ኣብቲ 'node_modules' ዝብል ፎልደር ብምእታው ኣብ ፕሮጀክትካ Web3.Jsን Dotenvን ክውስኸሉ እዩ።


  2. ናብ 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", } };
  3. ኣብ ሱር ማህደርና .env ፋይል ንፍጠር


  4. ካብ ሜታማስክ ቦርሳኻን dRPC ኤፒኣይ መፍትሕካን ናይ ኣካውንትካ ብሕታዊ መፍትሕ ውሰድ።


  5. ኣብ .env ፋይልካ ኣቐምጦም

     DRPC_API_KEY=your_drpc_api_key PRIVATE_KEY=your_wallet_private_key


  6. ነቲ hardhat.config.cjs ፋይል ንSepolia Testnet Configuration ንምሕዋስ ኣዘምኖ

     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}`], } } };
  7. ኣብ ትሕቲ 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;
  8. ኣብ ተርሚናልካ ነዚ ዝስዕብ ትእዛዝ ብምስራሕ ነቲ ስማርት ውዕል ኣዋፍሮ፤

     npx hardhat ignition deploy ./ignition/modules/deploy.cjs --network sepolia


    ድሕሪ ምዝዋር ኮማንድ ፕሮምፕት Confirm deploy to network sepolia (11155111)? (y/n)y ጽሓፍ። ኣብ ዕዉት ምውፋር ኣድራሻ ናይቲ ዝተዋፈረ ስማርት ውዕልካ ኣብቲ ተርሚናል ክትሪኦ ኣለካ።

    ኣብቲ deployed_addresses.json ፋይል እውን ነቲ ናይ ውዕል ኣድራሻ ክትረኽቦ ትኽእል ኢኻ።

    ስእሊ ስክሪን ናይ ሓደ Vite-Project ፋይል ኤክስፕሎረርን ናይ deployed_addresses.json ፋይልን ተኸፊቱ። እንቋዕ ሓጎሰካ፡ ስማርት ውዕልካ ናብ ሰፖልያ ቴስትነት ብዓወት ኣዋፊርካዮ ኣለኻ። 🎉

መደምደምታ

እዚ ጽሑፍ እዚ ከመይ ጌርካ ናይ ክፍሊት ስማርት ውዕል ትጽሕፍ፣ ትፍትኖ፣ ትእክቦን ትዝርግሖን ስማርት ውዕል ብሃርድሃት CLI ተጠቂምካ ምሂሩካ ኣሎ።


ኣብ ዝቕጽል ጽሑፍ፡ ነዚ dApp ዝኸውን ቅድመ-መወዳእታ ምህናጽ ክትመሃሩ ኢኹም። እዚ UI እዚ ዝሓቖፈ ክኸውን እዩ፤

  1. ንብዝሒ ዝተዓደገ ቡን ዝምልከት ናይ ምእታው ዓውዲ።
  2. ናይ ክፍሊት ትራንዛክሽን ዝጅምርን ካብ ሕሳብካ ዝቖርጽን መጠወቒ።
  3. Display ጠቕላላ ዝተዓደገ ቡንን ዝተረኸበ መጠን ገንዘብን ብኢተርን USDን።
  4. ዋጋ ቡን ንባዕሉ ብኤተርን USDን ክልቲኡ።

መጣቀሲ

ኪኖ ነባሪ መልእኽትታት፡ ምምራሕ ብሕታዊ ጌጋታት ኣብ ጽኑዕነት

ኣብ ጽንዓት ብሕታዊ ጌጋታት ምምራሕ፡ ስማርት ውዕላትካ ኪኖ ነባሪ መልእኽትታት ኣልዕል

ሶሊዲቲ ፋንክሽን እንታይ እዮም?

ኣብ ጽኑዕነት ዝርከቡ ፍጻመታት እንታይ እዮም?


← ዝሓለፈ ጽሑፍ ዝቕጽል ጽሑፍ →