http://www.7klian.com

solidity与library(库)的之间干系与应用

constructor () public {



}
*/


}
由于区块链的稳定性,开拓没有裂痕的智能合约很是重要。因此,对付以太坊智能合约的开拓,开拓人员应该始终实验重用和依赖已经被很多开拓人员审查、测试和利用过的代码,譬喻利用solidity库。

self._items.push(element);
}
address是Solidity中的一种非凡数据范例,具有20个字节的值,并提供诸如transfer和balance之类的内置函数。 由于每个帐户和智能合约都通过独一的地点暗示,因此险些每个智能合约项目都需要利用地点。

distributed under the License is distributed on an "AS IS" BASIS,
uint256 public totalSupply;
* @notice remove an address from the array
require(amount > 0, "amount is not bigger than zero");
* @notice push an address to the array
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

包括库,TokenContract和测试说明的存储库可在此链接下的Github上找到。https://github.com/51nodes/address-library-sample
在solidity中利用库
contract TokenContract is Ownable {

我们建设布局范例为AddrArrayLib.Addresses的私有变量,该私有变量暗示合约的状态变量并存储地点。之后,我们有一个结构函数和三个合约函数:

library SafeMath {
}
* @param self Storage array containing address type variables
* @param index the index in the array
如前所述,库是没有存储空间,可是它们可以修改链接智能合约的存储空间,要领是将要害字存储空间添加到函数的第一个参数中,并利用struct将状态变量封装在库中,如safemath库的修改实现所示。我们将在地点数组库的实现中利用它。
* @param self Storage array containing address type variables
using AddrArrayLib for AddrArrayLib.Addresses;

在上面的库代码中,我们有addresses布局,它包括address范例的数组和打点数组凡是需要的函数,譬喻:
trustedMinters.removeAddress(minter);

require(c >= a, "SafeMath: addition overflow");
See the License for the specific language governing permissions and

}

solidity与Libraries(库)

pragma solidity >= 0.4.25 < 0.6.0;
}
return true;
return c;

import './AddrArrayLib.sol';
return self._items.length;
*/


/**
uint256 currentBalance = 10;
require(trustedMinters.exists(msg.sender), 'The sender address is not registered as a Minter');

*/
*/
}

/**
function pushAddress(Addresses storage self, address element) internal {
为什么需要一个地点数组?

for (uint i = 0; i < self.size(); i++) {

import '../node_modules/openzeppelin solidity/contracts/ownership/Ownable.sol';
/**
}
if (self._items[i] == element) {

· getAddressAtIndex:获取给定索引处的地点。


struct Addresses {
* @param element the element to add in the array
mapping (address => uint256) balances;
AddrArrayLib.Addresses trustedMinters;

function add(Num storage self, uint256 amount) public {
pragma solidity >= 0.4.25 < 0.6.0;
return self._items;

· exists:假如数组中存在地点,返回true。
function exists(Addresses storage self, address element) internal view returns (bool) {
· removeMinter函数。

库就像合约,我们利用library要害字来建设它们。可是库没有存储空间,不能生存任何故太坊,也不能担任或被担任。尚有两种差异范例的库,具有外部成果的库称为链接库,它们应该首先陈设,而且它们的地点应该通过陈设进程添加到合约的bytecode中。只有内部函数的库称为嵌入式库。它们与合约一起陈设,因此它们的bytecode成为合约bytecode的一部门,也可以通过jump呼吁会见。
pragma solidity >=0.4.25 <0.6.0;
要利用该库,我们首先利用指令将uint256范例附加到该库



* @param self Storage array containing address type variables
function pay(uint256 amount) public {
using AddrArrayLib for Addresses;

Unless required by applicable law or agreed to in writing, software
if (self._items[i] == element) {
struct Balance { uint256 _balance; }
}
}
return self._items[index];
}

示例实现
简朴地迭代一个完整的映射是不行能的,因此在很多环境下,开拓人员必需将地点生存在数组中,出格是当涉及到在事务数据中不能给出地点的合约之间的链上交互时。最近我们在一个项目中事情,我们需要存储和打点一个数组中链上投资者的地点。在没有找到任何可用的民众库之后,我们实现了本身的库来打点地点数组。
· addMinter函数吸收新的Minter的地点,并挪用trustMinters.pushAddress函数,该函数从存储中传入布局实例。
}

· size:返回数组的巨细。
*/
balances[to] = balances[to]. add(amount);
self._items[i] = self._items[self.size() - 1];

}
totalSupply = totalSupply.add(amount);

/**

function size(Addresses storage self) internal view returns (uint256) {
* @dev finds the element, swaps it with the last element, and then deletes it;
function removeAddress(Addresses storage self, address element) internal returns (bool) {
function removeMinter(address minter) public onlyOwner() {
* @param self Storage array containing address type variables

* returns a boolean whether the element was found and deleted

在这个合约中,我们利用了两个嵌入式库,第一个是基于openzeppelin实现的嵌入式safemath库,它提供了添加的逻辑,但不会直接变动合约的状态,第二个是我们之前界说并通过其.sol文件导入的addrarraylib库。

}
}
}
return true;
}
library SafeMath {
· pushAddress:仅在地点不存在时将其添加到列表中。不答允利用反复地点,因为这会使数组更大,操纵更巨大并增加利用gas。因此,我们假定对条约代码中的此类地点举办简朴的非凡处理惩罚会更有效。
solidity是在以太坊网络和任何支持evm(以太坊虚拟机)的区块链上实现智能合约的高级编程语言之一。它的javascript语法和C语言气势气魄的数据范例使它成为生态系统中最受接待和支持的语言。
* @dev revert if the index is out of bounds
uint256 c = a + b;
· mintToken它通过挪用trustMinters.exists(msg.sender)来查抄msg.sender是否存在于受信任的造币者数组中,并在乐成验证之后建设该令牌。
}
此刻我们将建设一个利用这个库来打点用户地点的示例合约,用户可以建设一个代币。
trustedMinters.pushAddress(minter);
return false;
totalSupply = 100;


此后的事情重点是测试、改造现有成果和实现新成果。另外,我们还将阐明gas本钱,并将该库的链接版本与嵌入式版本举办较量。最后颠末很多有履历的开拓人员的审查,这个库将被陈设在以太坊区块链上,并可用于新的项目。

}


*/
下面是一个简朴的例子的safemath库,它有一个函数来添加两个uint256范例的数字,并防备大概的溢堕落误。
· removeAddress:从列表中删除地点,假如地点已删除,则返回true。假如该地点不存在,则该函数将返回false。该要领将找到该元素,将其与最后一个元素互换,然后将其删除。
在solidity中,库雷同于合约,它们实现操纵逻辑,而且可以陈设一次以供不受限制数量的其他智能合约利用,,而无需一次又一次地陈设沟通的逻辑。这有利于节减陈设所需的gas用度。然而,假如多个合约依赖于同一段代码的话,那么假如该段代码存在裂痕,则大概会发生不良效果。因此,这长短常要害的挪用,您应该很是审慎地选择在智能合约中利用的库。库的长处不只限于可重用性,它们还可以在必然水平上提高代码的可维护性、可读性甚至可进级性。
import '../node_modules/openzeppelin-solidity/contracts/math/SafeMath.sol';

function add(uint256 a, uint256 b) public pure returns (uint256){


balances[msg.sender] = 100;



在本文中,我们简腹地表明白库,为什么利用它们以及如何利用它们。另外,我们还先容了一个用于打点数组中地点的库的实现,我们认为这个库是智能合约常常需要的构建块。
}
require(index < size(self), "the index is out of bounds");


然后,uint256范例的任何变量都可以利用一个非凡的delegatecall挪用函数add(),个中发送方和值不会变动,只是从父浸染域流传到子浸染域,并且存储仍然引用挪用协定。
* @param element the element to check if it exists in the array
uint256 currentBalance;
* @dev if the address already exists, it will not be added again
在所有函数中,我们利用要害字作为存储通报addresses参数,因此evm从智能合约的存储中通过引用通报它,而不是在内存中建设它的副本。
currentBalance = currentBalance.add(amount);
using SafeMath for uint256

* @notice get the size of the array
uint256 newBalance = self._ balance + amount;
library AddrArrayLib {
return false;
pragma solidity >= 0.4.25 < 0.6.0;
function mintToken(address to, uint256 amount) external {
* @param self Storage array containing address type variables
for (uint i = 0; i < self.size(); i++) {
// List of trusted addresses which can mint tokens
limitations under the License.
/**
require(newBalance >= self._ balance, "addition overflow");
总结与将来事情
if (!exists(self, element)) {
* @notice check if an element exist in the array
}

* @param element the element to remove from the array
* @param self Storage array containing address type variables
/**
self._items.pop();
function getAddressAtIndex(Addresses storage self, uint256 index) internal view returns (address) {

* @notice get the array
using SafeMath for uint256;
}
/*

* @notice get the address at a specific index from array
function addMinter(address minter) public onlyOwner() {
*/



}
address[] _items;


self._ balance = newBalance;
function getAllAddresses(Addresses storage self) internal view returns(address[] memory) {

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。

相关文章阅读