Solidity Blockchain Idcard call from other contract


จากบทความที่แล้วเราได้ contract ของบัตรประจำตัวกันแล้ว ในทความนี้เราลองมาสร้าง contract ใหม่แล้วเรียกใช้งาน contract ในบทความที่แล้วดู
0x7EF2e0048f5bAeDe046f6BF797943daF4ED8CB47
contract address จากบทความที่แล้ว ได้มาจากตอน deploy
เริ่ม
สร้างไฟล์ชื่อว่า IdcardCall.sol ในโฟลเดอร์ contract
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
ประกาศ license กำหนดให้ไฟล์นี้ใช้ compiler version อะไรแล้วสร้าง
interface Iidcardd { function getId() external view returns(uint); function getName(uint _id13) external view returns(string memory); function getLastname(uint _id13) external view returns(string memory); function getBirthdate(uint _id13) external view returns(uint); function getIskyc(uint _id13) external view returns(bool);}
สร้าง interface ให้มีฟังก์ชันแบบเดิมแต่ไม่ต้องกำหนดส่วนของ body วงเล็บ นั้นแหละ กำหนด visibility จาก public เป็น external
จากนั้นสร้าง contract ใหม่ตั้งชื่อว่า MyCont แล้วเขียนโค้ดตามด้านล่างนี้
contract MyCont { address ckAddress = 0x7EF2e0048f5bAeDe046f6BF797943daF4ED8CB47; Iidcardd cardContract = Iidcardd(ckAddress);
function getId() public view returns(uint) { uint resId; (resId) = cardContract.getId(); return (resId); }
function getName(uint _id13) public view returns(string memory) { string memory resName; (resName) = cardContract.getName(_id13); return (resName); }
function getLastname(uint _id13) public view returns(string memory) { string memory resLastname; (resLastname) = cardContract.getLastname(_id13); return (resLastname); }
function getBirthdate(uint _id13) public view returns(uint) { uint resBirthdate; (resBirthdate) = cardContract.getBirthdate(_id13); return (resBirthdate); }
function getIskyc(uint _id13) public view returns(bool) { bool resIskyc; (resIskyc) = cardContract.getIskyc(_id13); return (resIskyc); }}
จากโค้ดด้านบน จะเห็นได้ว่า มีทั้ง interface และ contract ในส่วนของ interface แต่ละฟังก์ชันไม่มีโค้ดที่ดำเนินการเลย
interface
Interfaces are similar to abstract contracts, but they cannot have any functions implemented. There are further restrictions:
- They cannot inherit from other contracts, but they can inherit from other interfaces.
- All declared functions must be external.
- They cannot declare a constructor.
- They cannot declare state variables.
- They cannot declare modifiers.
https://docs.soliditylang.org/en/v0.8.9/contracts.html#interfaces
… ผมก็แปรไม่ค่อยได้เหมือนกันนะ ก็คือคล้ายกับฟังก์ชันธรรมดานั้นแหละ แต่ไม่ต้องเขียนโค้ดส่วนของ body วงเล็บ นั้นแหละ เดี๋ยว compile solidiy จะรู้เองว่าเป็น interface
- ทุกฟังก์ชันต้องกำหนด visibility เป็น external เท่านั้น
- เราไม่สามารถ ประกาศ constructor
- เราไม่สามารถ ประกาศ ตัวแปร
- เราไม่สามารถ ประกาศ modifiers
เริ่มงงละ อะไรคือ visibility แล้วอะไรคือ modifiers
visibility
- public หมายถึงสามารถถูกเรียกจากที่ใดก็ได้ไม่ว่าจากภายในหรือภายนอกก็ตาม
- private แปลว่าฟังก์ชั่นจะถูกเรียกได้จากฟังก์ชั่นอื่น ๆ ภายใน contract เท่านั้น
- internal เป็นเหมือนกัน private แต่จะสามารถถูกเรียกได้จาก contract ที่มีการ inherit มาจากตัวมัน
- external สามารถเรียกฟังก์ชั่นนี้ได้จากภายนอก contract เท่านั้น
Function Modifiers
Modifiers can be used to change the behaviour of functions in a declarative way. For example, you can use a modifier to automatically check a condition prior to executing the function.
… คือ prior to executing the function ตรวจสอบเงื่อนไขก่อนดำเนินการฟังก์ชันนั้นแหละ
ลอง deploy ดูครับ
ลองใส่ข้อมูลที่เราเพิ่มในบทความที่แล้วดูครับ ผลที่ออกมาก็ตามภาพ Ryze Leef
สุดท้าย ก็หวังว่าบทความนี้จะเป็นประโยชน์กับใครหลาย ๆ คนนะครับ