> For the complete documentation index, see [llms.txt](https://8olidity.gitbook.io/qu-kuai-lian-bi-ji/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://8olidity.gitbook.io/qu-kuai-lian-bi-ji/ji-ben-zhi-shi/openzeppelin-sheng-ji-ti-yan.md).

# openzeppelin升级体验

### 创建项目

```
npm install -g @openzeppelin/cli
oz --version
mkdir hello-oz  
cd hello-oz
npm init -y
oz init
npm install @openzeppelin/upgrades --save
```

### 部署

在contracts文件夹中创建HelloOz.sol文件

```
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.6.0;
import "@openzeppelin/upgrades/contracts/Initializable.sol";


contract HelloOz is Initializable {
    string public name;
    uint256 public dec;
    uint256 public inc;

    function initialize(string memory _name) initializer public {
        name = _name;
    }

    function decrement(uint256 x) public returns (uint256) {
        return dec = x - 1;
    }

    function increment(uint256 x) public returns (uint256) {
        return inc = x + 1;
    }
}
```

编译合约

```
oz compile
```

部署合约上链

```
oz create
```

这里根据networks.js随便跑一个节点，这里用hardhat代替

![](/files/ui0qr1ul79WEyICDkmHp)

部署完之后可以在`.openzeppelin/dev-.json`中看到部署的信息。其中implementation就是逻辑合约的地址。<br>

![](/files/k9P6HUwrNXdNm3yO48hp)

这里模拟溢出做演示。\
首先使用

```
oz send-tx
```

设置x为0，并计算。然后

```
oz call
```

查看计算结果<br>

![](/files/rLyUy1RNTbJb1UvWW0LT)

可以看到，0-1结果溢出了。然后我们来升级合约。

### 升级

安装一个库

```
oz link @openzeppelin/contracts-ethereum-package
```

修改代码如下

```
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.6.0;
import "@openzeppelin/upgrades/contracts/Initializable.sol";
import "@openzeppelin/contracts-ethereum-package/contracts/math/SafeMath.sol";
contract HelloOz is Initializable {
    string public name;
    uint256 public dec;
    uint256 public inc;
    using SafeMath for uint256;
    function initialize(string memory _name) initializer public {
        name = _name;
    }
    function decrement(uint256 x) public returns (uint256) {
            return dec = x.sub(1);
        }
    function increment(uint256 x) public returns (uint256) {
            return dec = x.add(1);
        }
}
```

使用

```
oz upgrade
```

进行更新

![](/files/8E7QhCr0kH73PapEDw56)

提示我们`0xa85233C63b9Ee964Add6F2cffe00Fd84eb32338f`已经更新。再用刚才的例子测试

![](/files/4ZOPSXPTRkO1WDa4ad2C)

发现它提示溢出了。可以看到，合约地址不变。但是逻辑已经更新。

参考:\
[How to create an upgradeable smart contract using OpenZeppelin SDK | by Paulina Błaszkiewicz | Coinmonks | Medium](https://medium.com/coinmonks/how-to-create-an-upgradeable-smart-contract-using-openzeppelin-sdk-example-of-fixing-smart-260dfbfd5bae)


---

# 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://8olidity.gitbook.io/qu-kuai-lian-bi-ji/ji-ben-zhi-shi/openzeppelin-sheng-ji-ti-yan.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.
