เขียน Smart Contract บน Blockchain ของ Ethereum ด้วยภาษา Solidity พร้อมตัวอย่างระบบ Vote

Sharing is caring!

Blog นี้เงียบเหงามานานมากเพราะที่หายไปเน้นไปเขียน Blog ที่ medium.com เป็นหลักแต่เลือกที่จะเขียน content นี้ที่นี่เพราะอยากให้มีอะไรอัพเดทที่ Blog นี้บ้าง

satang pro referral

ที่มาสำหรับบทความนี้

codemania 1001 ที่เพิ่งผ่านพ้นไป ได้เลือกที่จะเข้าร่วม session Blockchain Dev เป็น workshop ในช่วงเวลา 2 ชั่วโมงที่คุ้มค่ามาก ถึงแม้จะไม่ได้นำ Laptop ไปแต่ก็ไม่พลาดที่จะจดเนื้อหาแล้วเอามาเขียนในบทความนี้

อธิบายหลักการที่ความเข้าใจส่วนตัวหลังจากได้เข้าร่วม workshop Blockchain Dev ในเวลา 2 ชม.

  1. Ehtereum จะมีเครือข่ายโดยเฉพาะที่กระจายอยู่แต่ละที่โดยจะใช้คำเรียกว่า Node ซึ่งมีอยู่ทั่วโลก
  2. การทำ Transaction บน Ethereum จำเป็นต้องมีการจ่ายค่าธรรมเนียมที่เรียกว่าค่า “Gas”
  3. การที่จะทำ Transaction ได้นั้นจำเป็นต้องมีกระเป๋า “Wallet” เพื่อใช้เอาไว้เก็บ Coin โดยจะมีสิ่งที่สำคัญอยู่ 2 อย่างคือ public key ,private key
    1. private key เปรียบได้กับรหัสบัตรประชาชนของเราซึ่งจะไม่มีทางซ้ำกับคนอื่นได้แน่นอน
    2. public key (address) เปรียบได้กับบัญชีธนาคารซึ่งการที่จะสร้างบัญชีใหม่ได้จำเป็นต้องใช้ private key เป็นส่วนประกอบในการสร้าง เปิดเผยได้และใช้รหัสสำหรับอ้างถึงบัญชีรับ

ทำความรู้จักเครื่องมือต่าง ๆ ในสำหรับการเริ่มพัฒน

  • Remix Ethereum IDE Online เป็นเครื่องมือใช้เขียน Code ,Compile ,Deploy
  • MetaMask (Browser Extension) ใช้เป็น Program แทนกระเป๋าเงิน (Chrome ,Firefox ,Opera)
  • Web Server for Chrome เอาไว้ใช้เป็น web server run static web (เป็นตัวเลือก ถ้ามี tool อื่น run static web ได้ก็แทนได้เหมือนกัน)

เริ่ม workshop กัน

    1. ติดตั้ง MetaMask Chrome Extension และ Register ให้เรียบร้อย ขั้นตอนตรงนี้ขอข้ามไปนะ 
    2. เมื่อได้ Create account ที่ Metamask เรียบร้อยแล้ว จะเริ่มขั้นตอนการเติม Coin เข้า Wallet ช่องทางการเติมเงิน (ETH coin) จะร้องขอได้ที่ Ethereum Node Test  เช่น Kovan ,Rinkeby ,Kulap (Ethereum สัญาชาติไทย) เป็นต้น
      1. ต้องเปลี่ยน Network ของ Ehtereum Node เป็นของ Rinkeby Test Network
      2. เข้าลิ้ง https://www.rinkeby.io/#faucet โดยทาง Node จะให้เราแชร์ public key (address) บน Social Network (Facebook,Twitter,…) แบบเปิด public post จากนั้น copy link ของ post นั้นมาใส่
        1. 3 Ethers / 8 hours
        2. 7.5 Ethers / 1 day
        3. 18.75 Ethers / 3 days
      3. รอ.. สังเกตุ wallet เราก็จะมี token เพิ่มเข้ามา เราก็พร้อมจะไป step ถัดไป
    3. เริ่มเขียน code เข้าไปที่ลิ้ง https://remix.ethereum.org เลือก code ด้วย solidity
    4. เลือกเมนู File Explorers กดบวก สร้างไฟล์ ตั้งชื่อไฟล์ว่า “SimpleVoting.sol” และใส่ code ข้างล่างนี้
      pragma solidity ^0.5.0;
      pragma experimental ABIEncoderV2;
      // specifies what version of compiler this code will be compiled with
      contract Voting {
      /* the mapping field below is equivalent to an associative array or hash.
      */
      mapping (string => uint256) votesReceived;
      /* Solidity doesn't let you pass in an array of strings in the constructor (yet).
      We will use an array of bytes32 instead to store the list of candidates
      */
      string[] public candidateList;
      /* This is the constructor which will be called once and only once - when you
      deploy the contract to the blockchain. When we deploy the contract,
      we will pass an array of candidates who will be contesting in the election
      */
      constructor(string[] memory candidateNames) public {
      candidateList = candidateNames;
      }
      // This function returns the total votes a candidate has received so far
      function totalVotesFor(string memory candidate) public view returns (uint256) {
      return votesReceived[candidate];
      }
      // This function increments the vote count for the specified candidate. This
      // is equivalent to casting a vote
      function voteForCandidate(string memory candidate) public {
      votesReceived[candidate] += 1;
      }
      function candidateCount() public view returns (uint256) {
      return candidateList.length;
      }
      }
    5. กด Crtl + S เพื่อ save file
      1. Deploy ใส่ input ที่ใน code จะให้ใส่ค่าเป็น Array ให้ใส่ [“Google”,”Line”,”Facebook”] เพื่อใช้เป็น Choice Vote จากนั้นกด Deploy
        1. VoteForCan… สำหรับ Test Vote
        2. candidateC…
        3. candidateList get Choice Vote โดยส่ง index จะได้ item ใน List
        4. totalVotesFor ดูสถานะ Total Vote รวม
      2. ทดสอบ Vote
        1. ใส่ “Google” กด Vote ไป 2 ครั้ง
        2. ตรวจสอบสถานะของ “Google” จาก totalVotesFor จะได้ 2

ทั้งหมดนี้ของบทความต้องขอขอบคุณตัวอย่าง code workshop จาก Kulapio Githup

ใส่ความเห็น

อีเมลของคุณจะไม่แสดงให้คนอื่นเห็น ช่องข้อมูลจำเป็นถูกทำเครื่องหมาย *