Blog About
Table of Contents
  • เริ่ม
  • interface
  • visibility
  • Function Modifiers

Solidity Blockchain Idcard call from other contract

Apisit N.
25 Oct 2021

จากบทความที่แล้วเราได้ contract ของบัตรประจำตัวกันแล้ว ในทความนี้เราลองมาสร้าง contract ใหม่แล้วเรียกใช้งาน contract ในบทความที่แล้วดู

1
0x7EF2e0048f5bAeDe046f6BF797943daF4ED8CB47

contract address จากบทความที่แล้ว ได้มาจากตอน deploy

เริ่ม

สร้างไฟล์ชื่อว่า IdcardCall.sol ในโฟลเดอร์ contract

1
//SPDX-License-Identifier: MIT
2
3
pragma solidity ^0.8.0;

ประกาศ license กำหนดให้ไฟล์นี้ใช้ compiler version อะไรแล้วสร้าง

1
interface Iidcardd {
2
function getId() external view returns(uint);
3
function getName(uint _id13) external view returns(string memory);
4
function getLastname(uint _id13) external view returns(string memory);
5
function getBirthdate(uint _id13) external view returns(uint);
6
function getIskyc(uint _id13) external view returns(bool);
7
}

สร้าง interface ให้มีฟังก์ชันแบบเดิมแต่ไม่ต้องกำหนดส่วนของ body วงเล็บ นั้นแหละ กำหนด visibility จาก public เป็น external

จากนั้นสร้าง contract ใหม่ตั้งชื่อว่า MyCont แล้วเขียนโค้ดตามด้านล่างนี้

1
contract MyCont {
2
address ckAddress = 0x7EF2e0048f5bAeDe046f6BF797943daF4ED8CB47;
3
Iidcardd cardContract = Iidcardd(ckAddress);
4
5
function getId() public view returns(uint) {
6
uint resId;
7
(resId) = cardContract.getId();
8
return (resId);
9
}
10
11
function getName(uint _id13) public view returns(string memory) {
12
string memory resName;
13
(resName) = cardContract.getName(_id13);
14
return (resName);
15
}
16
17
function getLastname(uint _id13) public view returns(string memory) {
18
string memory resLastname;
19
(resLastname) = cardContract.getLastname(_id13);
20
return (resLastname);
21
}
22
23
function getBirthdate(uint _id13) public view returns(uint) {
24
uint resBirthdate;
25
(resBirthdate) = cardContract.getBirthdate(_id13);
26
return (resBirthdate);
27
}
28
29
function getIskyc(uint _id13) public view returns(bool) {
30
bool resIskyc;
31
(resIskyc) = cardContract.getIskyc(_id13);
32
return (resIskyc);
33
}
34
}

จากโค้ดด้านบน จะเห็นได้ว่า มีทั้ง 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 ดูครับ

this is alt tag

this is alt tag

ลองใส่ข้อมูลที่เราเพิ่มในบทความที่แล้วดูครับ ผลที่ออกมาก็ตามภาพ Ryze Leef

สุดท้าย ก็หวังว่าบทความนี้จะเป็นประโยชน์กับใครหลาย ๆ คนนะครับ

© 2025 Apisit N.