solidity中库的使用
编写sol文件时,可以用contract关键字声明一个合约,也可以用library声明一个库。
如执行truffle unbox webpack
时,生成的MetaCoin项目中,ConvertLib.sol声明了一个库,在MetaCoin.sol中调用了这个库。
在MetaCoin.sol中,通过import "./ConvertLib.sol"
来导入这个库,在部署合约时,migrations目录下的部署文件写法如下:
// 2_deploy_contracts.js
var ConvertLib = artifacts.require('./ConvertLib.sol')
var MetaCoin = artifacts.require('./MetaCoin.sol')
module.exports = function (deployer) {
deployer.deploy(ConvertLib)
deployer.link(ConvertLib, MetaCoin)
deployer.deploy(MetaCoin)
}
以下再以以太坊基金会提供的以uint为例子的遍历mapping的库iterable_mapping为例。
1. 编写sol文件
contracts目录下,创建IterableMapping.sol文件,编写库:
pragma solidity ^0.4.18;
// mapping 遍历库
library IterableMapping {
// 增、删、改、查
struct itmap {
uint size;
mapping(uint => IndexValue) data;
KeyFlag []keys;
// value
}
// key值的列表
struct KeyFlag {
uint key;
bool deleted;
}
// value
struct IndexValue {
uint KeyIndex;
uint value;
}
// 插入数据
function insert(itmap storage self, uint key, uint value) returns(bool replaced) {
uint keyIdx = self.data[key].KeyIndex;
self.data[key].value = value;
if (keyIdx > 0) {
return true;
}else {
keyIdx = self.keys.length++;
self.data[key].KeyIndex = keyIdx + 1;
self.keys[keyIdx].key = key;
self.size++;
return false;
}
}
// 删除数据(逻辑删除)
function remove(itmap storage self, uint key) returns(bool) {
uint keyIdx = self.data[key].KeyIndex;
if (keyIdx == 0) {
return false;
} else {
delete self.data[key]; //逻辑删除
self.keys[keyIdx - 1].deleted = true;
self.size --;
return true;
}
}
// 获取数据
function iterate_get(itmap storage self, uint KeyIdx) returns(uint key, uint value) {
key = self.keys[KeyIdx].key;
value = self.data[key].value;
}
// 包含
function iterate_contains(itmap storage self, uint key) returns(bool) {
return self.data[key].KeyIndex > 0;
}
// 获取下一个索引
function iterate_next(itmap storage self, uint _keyIndex) returns(uint r_keyIndex) {
_keyIndex++;
while(_keyIndex < self.keys.length && self.keys[_keyIndex].deleted) {
_keyIndex++;
}
return _keyIndex;
}
// 开始遍历
function iterate_start(itmap storage self) returns(uint keyIndex) {
iterate_next(self, uint(-1));
}
// 循环退出条件
function iterate_valid(itmap storage self, uint keyIndex) returns(bool) {
return keyIndex < self.keys.length;
}
}
User.sol文件:
pragma solidity ^0.4.18;
import "./IterableMapping.sol";
// 调用合约
contract User {
IterableMapping.itmap data;
// 插入数据
function insert(uint key, uint value) returns(uint size) {
IterableMapping.insert(data, key, value);
return data.size;
}
// 注意加了view public和没加view public的区别
// 遍历求和
function sum() view public returns(uint s) {
for(var i = IterableMapping.iterate_start(data);
IterableMapping.iterate_valid(data, i);
i = IterableMapping.iterate_next(data, i)) {
var (key ,value) = IterableMapping.iterate_get(data, i);
s += value;
}
}
}
2. 编写部署文件
migrations目录下,创建3_User.js文件,写法参考2_deploy_contracts.js
var IterableMapping = artifacts.require('./IterableMapping.sol')
var User = artifacts.require('./User.sol')
module.exports = function (deployer) {
deployer.deploy(IterableMapping)
deployer.link(IterableMapping, User)
deployer.deploy(User)
}