能自己寫 library 以及認識了像是 SafeMath 這類很常見的 library 之後,下一步就是讓我們學會更加模組化與輕鬆地使用他們吧!(本影片為 11/2 在機場預錄,因此有噪音QQ)
本日合約:
browser/Set.sol
pragma solidity ^0.4.25;
library Set {
struct Data {
mapping(int => bool) data;
}
function Insert(Data storage self, int key) public returns (bool) {
if (self.data[key])
return false; // Key exists.
self.data[key] = true;
return true;
}
function Remove(Data storage self, int key) public returns (bool) {
if (!self.data[key])
return false; // Key does not exist.
self.data[key] = false;
return true;
}
function Contain(Data storage self, int key) public view returns (bool) {
return self.data[key];
}
}
browser/Using.sol
pragma solidity ^0.4.25;
import "browser/Set.sol";
contract Main {
using Set for Set.Data;
Set.Data mySet;
function insert(int key) public returns (bool) {
return mySet.Insert(key);
}
function remove(int key) public returns (bool) {
return mySet.Remove(key);
}
function contain(int key) public view returns (bool) {
return mySet.Contain(key);
}
}
本日影片:
https://youtu.be/5QiyKikpKOk
Smart Contract 實戰教學播放清單:
https://www.youtube.com/playlist?list=PLHmOMPRfmOxSJcrlwyandWYiuP9ZAMYoF