Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

zos-lib

Package Overview
Dependencies
Maintainers
4
Versions
79
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

zos-lib

zeppelin_os library

  • 0.0.5
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
23
decreased by-42.5%
Maintainers
4
Weekly downloads
 
Created
Source

zeppelin_os library

NPM Package Build Status Coverage Status

:warning: Under heavy development: do not use in production :warning:

zos-lib is a library for writing upgradeable smart contracts on Ethereum. It can be used to create an upgradeable on-chain distributed application and is also used inside the zOS Kernel.

Use this library if you want to programmatically develop, deploy or operate an upgradeable smart contract system.

If you want a CLI-aided development experience, see the zOS CLI.

Getting Started

To install zos-lib simply go to your project's root directory and run:

npm i zos-lib

Next, learn how to:

Develop and deploy a single upgradeable smart contract

Note: This shows a low-level manual method of developing a single upgradeable smart contract. You probably want to use the higher-level CLI guide.

To work with a single upgradeable smart contract, you just need to deal with a simple upgradeability proxy. This is a special contract that will hold the storage of your upgradeable contract and redirect function calls to an implementation contract, which you can change (thus making it upgradeable). To learn more about how proxies work under the hood, read this post on our blog. To simply use them, do the following:

  1. Write the first implementation of your contract. Let's assume it's located in MyContract.sol. Most contracts require some sort of initialization, but upgradeable contracts can't use constructors (for reasons explained in this blog post), so we need to use the Initializable pattern provided in zos-lib:
import "zos-lib/contracts/migrations/Initializable.sol";

contract MyContract is Initializable {
  bool internal initialized;

  uint256 public x;
  
  function initialize(uint256 _x) public {
    require(!initialized);
    x = _x;
    initialized = true;
  }
}
  1. Deploy your first implementation contract:
const implementation_v0 = await MyContract.new();
  1. Now we need to deploy the proxy contract that will manage our contract's upgradeability. We pass the implementation address in the constructor, to set the first version of the behavior.
const proxy = await OwnedUpgradeabilityProxy.new(implementation_v0.address);
  1. Next, we call initialize on the proxy, to initialize the storage variables. Note that we wrap the proxy in a MyContract interface, because all calls will be delegated to the behavior.
let myContract = await MyContract.at(proxy.address);
const x0 = 42;
await myContract.initialize(x0);
console.log(await myContract.x()); // 42
  1. We now want to add a function to our contract, so we edit the MyContract.sol file and add it:
import "zos-lib/contracts/migrations/Initializable.sol";

contract MyContract is Initializable {
  bool internal initialized;

  uint256 public x;
  
  function initialize(uint256 _x) public {
    require(!initialized);
    x = _x;
    initialized = true;
  }

  function y() public pure returns (uint256) {
    return 1337;  
  }

}

Note that when we update our contract's code, we can't change its pre-existing storage structure. This means we can't remove any previously existing contract variable. We can, however, remove functions we don't want to use anymore (in the code shown, all functions were preserved).

  1. Next, we deploy the new implementation contract, and upgrade our proxy to it:
const implementation_v1 = await MyContract.new();
await proxy.upgradeTo(implementation_v1.address);
myContract = await MyContract_v1.at(proxy.address);

console.log(await myContract.x()); // 42
console.log(await myContract.y()); // 1337

Wohoo! We've upgraded our contract's behavior while preserving it's storage.

For a fully working project with this example, see the examples/single folder.

Develop and operate a complex upgradeable app

Most real-world applications require more than a single smart contract. Here's how to build a complex upgradeable app with multiple smart contracts and connect it to the zOS Kernel standard libraries.

Develop a zOS Kernel standard library release.

Keywords

FAQs

Package last updated on 20 Apr 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc