> For the complete documentation index, see [llms.txt](https://kademliachain.gitbook.io/kad/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kademliachain.gitbook.io/kad/zhi-neng-he-yue/zhi-neng-he-yue-ku.md).

# 智能合约库

## 资料库中的内容 <a href="#whats-in-a-library" id="whats-in-a-library"></a>

***

您通常可以在智能合约库中找到两种构建模块：可以添加到合约中的可复用代码，与各种标准的实现。

### 行为 <a href="#behaviors" id="behaviors"></a>

当编写智能合约时，您很可能会发现自己在写重复的代码。 比如说在智能合约中指派一个*管理员*地址执行受保护的操作，或添加一个紧急*暂停*按钮以应对预料不到的问题。

智能合约库通常提供这些行为的可复用实现方式为[标准库(opens in a new tab)](https://solidity.readthedocs.io/en/v0.7.2/contracts.html#libraries)或在 solidity 中通过[继承(opens in a new tab)](https://solidity.readthedocs.io/en/v0.7.2/contracts.html#inheritance)的方式实现。

例如，以下是[`不可拥有的`合约的简化版本(opens in a new tab)](https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v3.2.0/contracts/access/Ownable.sol)来自 [OpenZeppelin 合约库(opens in a new tab)](https://github.com/OpenZeppelin/openzeppelin-contracts)，它设计了一个作为合约所有者的地址，并且提供了一个修饰者来限制该所有者获得一种方法。

{% code lineNumbers="true" %}

```vim
contract Ownable {
    address public owner;

    constructor() internal {
        owner = msg.sender;
    }

    modifier onlyOwner() {
        require(owner == msg.sender, "Ownable: caller is not the owner");
        _;
    }
}

```

{% endcode %}

在您的合约中使用这个构建模块，您需要先导入它，然后在您自己的合约中扩展它。 这个将会允许您使用 `Ownable` 合约提供的修饰符来保护您的函数。

{% code lineNumbers="true" %}

```vim
import ".../Ownable.sol"; // Path to the imported library

contract MyContract is Ownable {
    // The following function can only be called by the owner
    function secured() onlyOwner public {
        msg.sender.transfer(1 ether);
    }
}

```

{% endcode %}

另一个比较受欢迎的例子是 [SafeMath(opens in a new tab)](https://docs.openzeppelin.com/contracts/3.x/utilities#math) 或[DsMath(opens in a new tab)](https://dappsys.readthedocs.io/en/latest/ds_math.html)。 这些库（与基础合约不同）提供了语言本身不具有的带有溢出检查的算术函数。 使用这些库而不是本地的算术操作可以来防止您的合约出现溢出错误，这些错误可能会导致灾难性的后果！

### 标准 <a href="#standards" id="standards"></a>

## 如何添加库 <a href="#how-to" id="how-to"></a>

***

始终参考您所包含的库的文档，以获得关于如何将其包含在您的项目中的具体说明 一些 Solidity 合约库使用 `npm` 来打包，所以您可以直接 `npm` 安装它们。 大多数编译合约的工具会在您的 node\_modules 中查找智能合约库，所以您可以做以下工作。

{% code lineNumbers="true" %}

```vim
// This will load the @openzeppelin/contracts library from your node_modules
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";

contract MyNFT is ERC721 {
    constructor() ERC721("MyNFT", "MNFT") public { }
}

```

{% endcode %}

无论您使用哪种方法，当包括一个库时，总是要注意语言的版本。 何时使用

## 何时使用 <a href="#when-to-use" id="when-to-use"></a>

***

为您的项目使用智能合约库有几个好处。 首先，它为您提供了现成的构建模块，您可以将其纳入您的系统，而不必自己编码，从而节省了您的时间。

安全性也是一个重要的优点。 开源智能合约库也经常受到严格审查。 鉴于许多项目都依赖于它们，社区有强烈的动机来对它们持续审计。 在应用程序代码中发现错误比在可重用的合约库中发现错误要常见得多。 一些库还接受了[外部审计(opens in a new tab)](https://github.com/OpenZeppelin/openzeppelin-contracts/tree/master/audit)，以提高安全性。

然而，使用智能合约库有可能将您不熟悉的代码纳入您的项目。 导入一个合约并将其直接包含在您的项目中是很诱人的，但如果没有很好地理解该合约的作用，您可能会由于一个意外的行为而无意中在您的系统中引入一个问题。 一定要确保阅读您要导入的代码的文档，然后在使其成为您的项目的一部分之前审查代码本身。

最后，在决定是否包括一个库时，要考虑其总体使用情况。 一个被广泛采用的方案的好处是有一个更大的社区和更多的眼睛来关注它的问题。 在使用智能合约进行建设时，安全应该是您的首要关注点！

## 相关工具 <a href="#related-tools" id="related-tools"></a>

***

**OpenZeppelin 合约-** ***安全的智能合约开发库。***

* [相关文档(opens in a new tab)](https://docs.openzeppelin.com/contracts/)
* [GitHub(opens in a new tab)](https://github.com/OpenZeppelin/openzeppelin-contracts)
* [社区论坛(opens in a new tab)](https://forum.openzeppelin.com/c/general/16)

**DappSys -** ***安全、简单、灵活的智能合约构建模块。***

* [相关文档(opens in a new tab)](https://dappsys.readthedocs.io/)
* [GitHub(opens in a new tab)](https://github.com/dapphub/dappsys)

**HQ20 -** ***一个带有合约、库和案例的 Solidity 项目，帮助您为现实世界建立功能齐全的分布式应用。***

* [GitHub(opens in a new tab)](https://github.com/HQ20/contracts)

**thirdweb Solidity SDK -** ***提供了构建自定义智能合约所需的工具，能够高效地完成***

* [相关文档(opens in a new tab)](https://portal.thirdweb.com/solidity/)
* [GitHub(opens in a new tab)](https://github.com/thirdweb-dev/contracts)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://kademliachain.gitbook.io/kad/zhi-neng-he-yue/zhi-neng-he-yue-ku.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
