From 91ebe2cb5720d600d89b6557b769aa0e209968cd Mon Sep 17 00:00:00 2001 From: Eduardo Pereira Date: Thu, 17 Oct 2019 13:45:47 -0300 Subject: [PATCH 1/3] Adding openzeppelin-sdk tutorial --- docs/.vuepress/config.js | 1 + docs/.vuepress/locales/en.json | 1 + docs/en/loom-js-openzeppelin-sdk.md | 199 ++++++++++++++++++++++++++++ 3 files changed, 201 insertions(+) create mode 100644 docs/en/loom-js-openzeppelin-sdk.md diff --git a/docs/.vuepress/config.js b/docs/.vuepress/config.js index 7d94dcdba..ef0dcd7eb 100644 --- a/docs/.vuepress/config.js +++ b/docs/.vuepress/config.js @@ -86,6 +86,7 @@ module.exports = { children: [ ['/loom-js-quickstart', 'NodeJS & Browser Quick Start'], ['/web3js-loom-provider-truffle', 'Web3, LoomProvider and Truffle'], + ['/loom-js-openzeppelin-sdk', 'LoomProvider & OpenZeppelin SDK'] ], }, { diff --git a/docs/.vuepress/locales/en.json b/docs/.vuepress/locales/en.json index 13dfbaf26..b9ddadee0 100644 --- a/docs/.vuepress/locales/en.json +++ b/docs/.vuepress/locales/en.json @@ -48,6 +48,7 @@ "Logging": "Logging", "loom-js-loom-provider-web3": "Loom.js + Web3.js", "Loom.js + Web3.js": "Loom.js + Web3.js", + "loom-js-openzeppelin-sdk": "Loom.js + OpenZeppelin SDK", "loom-js-quickstart": "NodeJS & Browser Quick Start", "NodeJS & Browser Quick Start": "NodeJS & Browser Quick Start", "loom-yaml": "Loom Yaml and Configuration options", diff --git a/docs/en/loom-js-openzeppelin-sdk.md b/docs/en/loom-js-openzeppelin-sdk.md new file mode 100644 index 000000000..d45f0a17f --- /dev/null +++ b/docs/en/loom-js-openzeppelin-sdk.md @@ -0,0 +1,199 @@ +--- +id: loom-js-openzeppelin-sdk +title: Loom.js + OpenZeppelin SDK +sidebar_label: Loom.js + OpenZeppelin SDK +--- + +# Overview + +The `LoomProvider` now has initial support to [OpenZeppelin SDK](https://docs.openzeppelin.com/sdk/2.5), also supporting [Upgradeable Contracts](https://docs.openzeppelin.com/sdk/2.5/writing-contracts), deployed on `Loomchain` (this requires the latest version of Loomchain) + +> Note: Is required to test with builds greater than 1313, to check your loomchain build you can always use the command `loom env` + +# Setup + +## Installing OpenZeppelin SDK and LoomTruffleProvider + +The first step is to install the `OpenZeppelin SDK,` first let's create a directory for the project and install the necessary packages: + +```bash +mkdir my-project +cd my-project + +# Setting up the packages +yarn add -D @openzeppelin/cli@2.5.3 js-sha256 loom-js@dev loom-truffle-provider@dev + +# Intialize OpenZeppelin SDK +yarn openzeppelin init my-project 1.0.0 +``` + +> Note: Here we're using the developer versions of loom-js and loom-truffle-provider + +## Setting Up LoomTruffleProvider + +The next step is to configure the `networks.js` file, and it should be something like that after a fresh install: + +```js +module.exports = { + networks: { + development: { + protocol: 'http', + host: 'localhost', + port: 8545, + gas: 5000000, + gasPrice: 5e9, + networkId: '*', + }, + }, +}; +``` + +Let's replace with: + +```js +const LoomTruffleProvider = require('loom-truffle-provider') +const { sha256 } = require('js-sha256') +const { CryptoUtils } = require('loom-js') + +function getLoomProviderWithMnemonic (seed, chainId, writeUrl, readUrl) { + const privateKeyUint8ArrayFromSeed = CryptoUtils.generatePrivateKeyFromSeed(new Uint8Array(sha256.array(seed))) + const privateKeyB64 = CryptoUtils.Uint8ArrayToB64(privateKeyUint8ArrayFromSeed) + return new LoomTruffleProvider(chainId, writeUrl, readUrl, privateKeyB64) +} + +module.exports = { + networks: { + development: { + provider: function() { + const chainId = 'default' + const writeUrl = 'http://127.0.0.1:46658/rpc' + const readUrl = 'http://127.0.0.1:46658/eth' + const seed = 'gravity top burden flip student usage spell purchase hundred improve check genre' + + const loomTruffleProvider = getLoomProviderWithMnemonic(seed, chainId, writeUrl, readUrl) + + // Quick implementation for web3_clientVersion + loomTruffleProvider.getProviderEngine().addCustomMethod('web3_clientVersion', () => { + return 'loom-js' + }) + + return loomTruffleProvider + }, + networkId: '*', + }, + }, +} +``` + +## Your First Contract + +> Note: The rest of this tutorial is almost identical to the OpenZeppelin tutorial; however, we're going to reproduce the same approach here + +Let's create the `Counter.sol` contract on directory `contracts` and run the command to deploy it on `Loomchain`; it should deploy the contract on `Loomchain`. + +```solidity +pragma solidity ^0.5.0; + +contract Counter { + uint256 public value; + + function increase() public { + value++; + } +} +``` + +```bash +# Let's deploy on Loomchain +yarn openzeppelin create +✓ Compiled contracts with solc 0.5.12 (commit.7709ece9) +? Pick a contract to instantiate Counter +? Pick a network development +✓ Added contract Counter +✓ Contract Counter deployed +All contracts have been deployed +? Do you want to call a function on the instance after creating it? Yes +? Select which function increase() +✓ Setting everything up to create contract instances +✓ Instance created at 0x1A3d94d6c0174d2f810BcE314a4cE516d640E139 +``` + +### Send-TX and Call + +Now we're going to call function on the `Counter` contract. + +```bash +# Send a transaction to the contract Counter +yarn openzeppelin send-tx +? Pick a network development +? Pick an instance Counter at 0x1A3d94d6c0174d2f810BcE314a4cE516d640E139 +? Select which function increase() +✓ Transaction successful. Transaction hash: 0x3fc115008028a93438495f0a89eed0e4a24ff8c14d3c4a81e7dad58ca1a6c61b +``` + +```bash +# Calling the functin value on contract Counter +yarn openzeppelin call +? Pick a network development +? Pick an instance Counter at 0x1A3d94d6c0174d2f810BcE314a4cE516d640E139 +? Select which function value() +✓ Method 'value()' returned: 2 +2 +``` + +### Upgrading Your Contract + +Next, we're going to upgrade the `Counter` contract; it now should be like + +```solidity +pragma solidity ^0.5.0; + +contract Counter { + uint256 public value; + + function increase(uint256 amount) public { + value += amount; + } +} +``` + +And call the comoamnd `upgrade` on `OpenZeppelin SDK` + +```bash +# Upgrading the contract Counter +yarn openzeppelin upgrade +? Pick a network development +✓ Compiled contracts with solc 0.5.12 (commit.7709ece9) +✓ Contract Counter deployed + All contracts have been deployed +? Which instances would you like to upgrade? All instances +✓ Instance upgraded at 0x1A3d94d6c0174d2f810BcE314a4cE516d640E139. Transaction receipt: 0x531f51faa5692952076f2ba7d630fc5176d9060ffadbf851558f9a6891f2fbdd ✓ Instance at 0x1A3d94d6c0174d2f810BcE314a4cE516d640E139 upgraded +``` + +Great! the contract `Counter` is updated, and the address stays the same, now let's call `send-tx` and `call` again in order to confirm the upgrade + +```bash +# Send a transaction to the contract Counter +yarn openzeppelin send-tx +? Pick a network development +? Pick an instance Counter at 0x1A3d94d6c0174d2f810BcE314a4cE516d640E139 +? Select which function increase(amount: uint256) +? amount (uint256): 10 +✓ Transaction successful. Transaction hash: 0x0c1c4d0edd33628a778185c290957273620a5f2756f8aea339e80d45612d1794 +``` + +And now let's check the value on the contract + +```bash +# Calling the functin value on contract Counter +yarn openzeppelin call +? Pick a network development +? Pick an instance Counter at 0x1A3d94d6c0174d2f810BcE314a4cE516d640E139 +? Select which function value() +✓ Method 'value()' returned: 12 +12 +``` + +Cool! The value is `12`, so it worked as expected. + +> Note: Is essential to have a good understanding of the nuances of an Upgradable Contract on using OpenZeppelin SDK, weren't to go deeper here, so we encourage you to check the OpenZeppelin SDK for further details From ff582972e71afe838c8b95a10afd9bc99afe9d24 Mon Sep 17 00:00:00 2001 From: Andrei Popescu Date: Mon, 21 Oct 2019 13:42:45 +0300 Subject: [PATCH 2/3] copyediting copyediting --- docs/.vuepress/config.js | 2 +- docs/.vuepress/locales/en.json | 2 +- docs/en/loom-js-openzeppelin-sdk.md | 199 ---------------- ...adable-smart-contracts-openzeppelin-sdk.md | 220 ++++++++++++++++++ ...adable-smart-contracts-openzeppelin-sdk.md | 220 ++++++++++++++++++ ...adable-smart-contracts-openzeppelin-sdk.md | 220 ++++++++++++++++++ ...adable-smart-contracts-openzeppelin-sdk.md | 220 ++++++++++++++++++ 7 files changed, 882 insertions(+), 201 deletions(-) delete mode 100644 docs/en/loom-js-openzeppelin-sdk.md create mode 100644 docs/en/upgradable-smart-contracts-openzeppelin-sdk.md create mode 100644 docs/ja/upgradable-smart-contracts-openzeppelin-sdk.md create mode 100644 docs/ko/upgradable-smart-contracts-openzeppelin-sdk.md create mode 100644 docs/zh-CN/upgradable-smart-contracts-openzeppelin-sdk.md diff --git a/docs/.vuepress/config.js b/docs/.vuepress/config.js index ef0dcd7eb..3c547bd50 100644 --- a/docs/.vuepress/config.js +++ b/docs/.vuepress/config.js @@ -86,7 +86,7 @@ module.exports = { children: [ ['/loom-js-quickstart', 'NodeJS & Browser Quick Start'], ['/web3js-loom-provider-truffle', 'Web3, LoomProvider and Truffle'], - ['/loom-js-openzeppelin-sdk', 'LoomProvider & OpenZeppelin SDK'] + ['/upgradable-smart-contracts-openzeppelin-sdk', 'Upgradable Smart Contracts with OpenZeppelin SDK'] ], }, { diff --git a/docs/.vuepress/locales/en.json b/docs/.vuepress/locales/en.json index b9ddadee0..c19c768a1 100644 --- a/docs/.vuepress/locales/en.json +++ b/docs/.vuepress/locales/en.json @@ -48,7 +48,7 @@ "Logging": "Logging", "loom-js-loom-provider-web3": "Loom.js + Web3.js", "Loom.js + Web3.js": "Loom.js + Web3.js", - "loom-js-openzeppelin-sdk": "Loom.js + OpenZeppelin SDK", + "loom-js-openzeppelin-sdk": "Upgradable Smart Contracts with OpenZeppelin SDK", "loom-js-quickstart": "NodeJS & Browser Quick Start", "NodeJS & Browser Quick Start": "NodeJS & Browser Quick Start", "loom-yaml": "Loom Yaml and Configuration options", diff --git a/docs/en/loom-js-openzeppelin-sdk.md b/docs/en/loom-js-openzeppelin-sdk.md deleted file mode 100644 index d45f0a17f..000000000 --- a/docs/en/loom-js-openzeppelin-sdk.md +++ /dev/null @@ -1,199 +0,0 @@ ---- -id: loom-js-openzeppelin-sdk -title: Loom.js + OpenZeppelin SDK -sidebar_label: Loom.js + OpenZeppelin SDK ---- - -# Overview - -The `LoomProvider` now has initial support to [OpenZeppelin SDK](https://docs.openzeppelin.com/sdk/2.5), also supporting [Upgradeable Contracts](https://docs.openzeppelin.com/sdk/2.5/writing-contracts), deployed on `Loomchain` (this requires the latest version of Loomchain) - -> Note: Is required to test with builds greater than 1313, to check your loomchain build you can always use the command `loom env` - -# Setup - -## Installing OpenZeppelin SDK and LoomTruffleProvider - -The first step is to install the `OpenZeppelin SDK,` first let's create a directory for the project and install the necessary packages: - -```bash -mkdir my-project -cd my-project - -# Setting up the packages -yarn add -D @openzeppelin/cli@2.5.3 js-sha256 loom-js@dev loom-truffle-provider@dev - -# Intialize OpenZeppelin SDK -yarn openzeppelin init my-project 1.0.0 -``` - -> Note: Here we're using the developer versions of loom-js and loom-truffle-provider - -## Setting Up LoomTruffleProvider - -The next step is to configure the `networks.js` file, and it should be something like that after a fresh install: - -```js -module.exports = { - networks: { - development: { - protocol: 'http', - host: 'localhost', - port: 8545, - gas: 5000000, - gasPrice: 5e9, - networkId: '*', - }, - }, -}; -``` - -Let's replace with: - -```js -const LoomTruffleProvider = require('loom-truffle-provider') -const { sha256 } = require('js-sha256') -const { CryptoUtils } = require('loom-js') - -function getLoomProviderWithMnemonic (seed, chainId, writeUrl, readUrl) { - const privateKeyUint8ArrayFromSeed = CryptoUtils.generatePrivateKeyFromSeed(new Uint8Array(sha256.array(seed))) - const privateKeyB64 = CryptoUtils.Uint8ArrayToB64(privateKeyUint8ArrayFromSeed) - return new LoomTruffleProvider(chainId, writeUrl, readUrl, privateKeyB64) -} - -module.exports = { - networks: { - development: { - provider: function() { - const chainId = 'default' - const writeUrl = 'http://127.0.0.1:46658/rpc' - const readUrl = 'http://127.0.0.1:46658/eth' - const seed = 'gravity top burden flip student usage spell purchase hundred improve check genre' - - const loomTruffleProvider = getLoomProviderWithMnemonic(seed, chainId, writeUrl, readUrl) - - // Quick implementation for web3_clientVersion - loomTruffleProvider.getProviderEngine().addCustomMethod('web3_clientVersion', () => { - return 'loom-js' - }) - - return loomTruffleProvider - }, - networkId: '*', - }, - }, -} -``` - -## Your First Contract - -> Note: The rest of this tutorial is almost identical to the OpenZeppelin tutorial; however, we're going to reproduce the same approach here - -Let's create the `Counter.sol` contract on directory `contracts` and run the command to deploy it on `Loomchain`; it should deploy the contract on `Loomchain`. - -```solidity -pragma solidity ^0.5.0; - -contract Counter { - uint256 public value; - - function increase() public { - value++; - } -} -``` - -```bash -# Let's deploy on Loomchain -yarn openzeppelin create -✓ Compiled contracts with solc 0.5.12 (commit.7709ece9) -? Pick a contract to instantiate Counter -? Pick a network development -✓ Added contract Counter -✓ Contract Counter deployed -All contracts have been deployed -? Do you want to call a function on the instance after creating it? Yes -? Select which function increase() -✓ Setting everything up to create contract instances -✓ Instance created at 0x1A3d94d6c0174d2f810BcE314a4cE516d640E139 -``` - -### Send-TX and Call - -Now we're going to call function on the `Counter` contract. - -```bash -# Send a transaction to the contract Counter -yarn openzeppelin send-tx -? Pick a network development -? Pick an instance Counter at 0x1A3d94d6c0174d2f810BcE314a4cE516d640E139 -? Select which function increase() -✓ Transaction successful. Transaction hash: 0x3fc115008028a93438495f0a89eed0e4a24ff8c14d3c4a81e7dad58ca1a6c61b -``` - -```bash -# Calling the functin value on contract Counter -yarn openzeppelin call -? Pick a network development -? Pick an instance Counter at 0x1A3d94d6c0174d2f810BcE314a4cE516d640E139 -? Select which function value() -✓ Method 'value()' returned: 2 -2 -``` - -### Upgrading Your Contract - -Next, we're going to upgrade the `Counter` contract; it now should be like - -```solidity -pragma solidity ^0.5.0; - -contract Counter { - uint256 public value; - - function increase(uint256 amount) public { - value += amount; - } -} -``` - -And call the comoamnd `upgrade` on `OpenZeppelin SDK` - -```bash -# Upgrading the contract Counter -yarn openzeppelin upgrade -? Pick a network development -✓ Compiled contracts with solc 0.5.12 (commit.7709ece9) -✓ Contract Counter deployed - All contracts have been deployed -? Which instances would you like to upgrade? All instances -✓ Instance upgraded at 0x1A3d94d6c0174d2f810BcE314a4cE516d640E139. Transaction receipt: 0x531f51faa5692952076f2ba7d630fc5176d9060ffadbf851558f9a6891f2fbdd ✓ Instance at 0x1A3d94d6c0174d2f810BcE314a4cE516d640E139 upgraded -``` - -Great! the contract `Counter` is updated, and the address stays the same, now let's call `send-tx` and `call` again in order to confirm the upgrade - -```bash -# Send a transaction to the contract Counter -yarn openzeppelin send-tx -? Pick a network development -? Pick an instance Counter at 0x1A3d94d6c0174d2f810BcE314a4cE516d640E139 -? Select which function increase(amount: uint256) -? amount (uint256): 10 -✓ Transaction successful. Transaction hash: 0x0c1c4d0edd33628a778185c290957273620a5f2756f8aea339e80d45612d1794 -``` - -And now let's check the value on the contract - -```bash -# Calling the functin value on contract Counter -yarn openzeppelin call -? Pick a network development -? Pick an instance Counter at 0x1A3d94d6c0174d2f810BcE314a4cE516d640E139 -? Select which function value() -✓ Method 'value()' returned: 12 -12 -``` - -Cool! The value is `12`, so it worked as expected. - -> Note: Is essential to have a good understanding of the nuances of an Upgradable Contract on using OpenZeppelin SDK, weren't to go deeper here, so we encourage you to check the OpenZeppelin SDK for further details diff --git a/docs/en/upgradable-smart-contracts-openzeppelin-sdk.md b/docs/en/upgradable-smart-contracts-openzeppelin-sdk.md new file mode 100644 index 000000000..b802a32bb --- /dev/null +++ b/docs/en/upgradable-smart-contracts-openzeppelin-sdk.md @@ -0,0 +1,220 @@ +--- +id: updradable-smart-contracts-with-openzeppelin-sdk +title: Upgradable Smart Contracts with OpenZeppelin SDK +sidebar_label: Upgradable Smart Contracts with OpenZeppelin SDK +--- + +## Purpose + +This guide will walk you through the process of deploying an [upgradable smart contract](https://docs.openzeppelin.com/sdk/2.5/writing-contracts) using the [OpenZeppelin SDK](https://docs.openzeppelin.com/sdk/). + +## Prerequisites + +- Node.js 10 or higher (recommended v10.15.3). You can download the installer from [this page](https://nodejs.org/en/download/) + +- Yarn. Follow [these instructions](https://yarnpkg.com/lang/en/docs/install/) to install Yarn on your computer. + +## Setting Things Up + +The first step is to install the `OpenZeppelin SDK`. In a terminal window, move into the directory where you want to save the new project, and enter the following commands: + +```bash +mkdir upgradable-contracts && cd upgradable-contracts + +# Setting up the packages +yarn add -D @openzeppelin/cli@2.5.3 js-sha256 loom-js@dev loom-truffle-provider@dev + +# Intialize OpenZeppelin SDK +yarn openzeppelin init upgradable-contracts 1.0.0 +``` + +> Note: We're using the developer versions of `loom-js` and `loom-truffle-provider`. + +The above command creates a bunch of directories and files. For the scope of this tutorial we'll be using: + +* The `networks.js`file which stores the deployment configuration. If you're familiar with Truffle, this file is similar to the `truffle-config.js` file. +* The `contracts` directory where we'll place our smart contracts. + +## Configuring LoomTruffleProvider + +Now, let's configure OpenZeppelin to use `loom-truffle-provider` and deploy to Loom Testnet (`extdev`). Replace the content of the `networks.js` file with the following snippet: + +```js +const LoomTruffleProvider = require('loom-truffle-provider') +const { sha256 } = require('js-sha256') +const { CryptoUtils } = require('loom-js') +function getLoomProviderWithMnemonic (seed, chainId, writeUrl, readUrl) { + const privateKeyUint8ArrayFromSeed = CryptoUtils.generatePrivateKeyFromSeed(new Uint8Array(sha256.array(seed))) + const privateKeyB64 = CryptoUtils.Uint8ArrayToB64(privateKeyUint8ArrayFromSeed) + return new LoomTruffleProvider(chainId, writeUrl, readUrl, privateKeyB64) +} +module.exports = { + networks: { + extdev: { + provider: function() { + const chainId = 'extdev-plasma-us1' + const writeUrl = 'http://extdev-plasma-us1.dappchains.com:80/rpc' + const readUrl = 'http://extdev-plasma-us1.dappchains.com:80/eth' + const seed = 'gravity top burden flip student usage spell purchase hundred improve check genre' + const loomTruffleProvider = getLoomProviderWithMnemonic(seed, chainId, writeUrl, readUrl) + // Quick implementation for web3_clientVersion + loomTruffleProvider.getProviderEngine().addCustomMethod('web3_clientVersion', () => { + return 'loom-js' + }) + return loomTruffleProvider + }, + networkId: '*', + }, + }, +} +``` + +## Your First Contract + +> Note: The next sections are using almost the same steps as the OpenZeppelin tutorial on working with upgradable contracts. However, to make this tutorial easier to follow, we'll also present these steps here. + +In the `contracts` directory, let's create a file called `Counter.sol` with the following content: + +```solidity +pragma solidity ^0.5.0; + +contract Counter { + uint256 public value; + + function increase() public { + value++; + } +} +``` + +Now let's deploy our smart contract and call the `increase` function by running the `yarn openzeppelin create` command and passing the following arguments: + +* The name of the smart contract (`Counter`) +* the `--network` flag with the name of the network we want to deploy our smart contract to (`extdev`) +* the `--init` flag with the name of the function we want to call once the contracts gets deployed (`increase`) + +```bash +yarn openzeppelin create Counter --network extdev --init increase +``` + + +``` +✓ Setting everything up to create contract instances +✓ Instance created at 0x879d15B21DE431E9520D6880E46Df374F7abB2a3 +0x879d15B21DE431E9520D6880E46Df374F7abB2a3 +✨ Done in 360.73s. +``` + +> The `--init` flag is very useful because you can't use constructors with the OpenZeppelin SDK. For more details, please refer to the [OpenZeppelin SDK Upgrades Pattern]https://docs.openzeppelin.com/sdk/2.5/pattern#the-constructor-caveat) page. + + +### Send-TX and Call + +Now that our contract is deployed, let's send a transaction by running the `yarn openzeppelin send-tx` and passing it the following flags: + +* `--network` with the name of the network we deployed our smart contract to (`extdev`). +* `--to` with the address of the `Counter` smart contract. In our example the address is `0x879d15B21DE431E9520D6880E46Df374F7abB2a3` but yours will be different. + +* `--method` with the name of the method we want to call (`increase`) + +Example: + +``` +yarn openzeppelin send-tx --network development --to 0x879d15B21DE431E9520D6880E46Df374F7abB2a3 --method increase +``` + +``` +yarn run v1.13.0 +warning package.json: No license field +$ /Users/andrei/upgradable-contracts/node_modules/.bin/openzeppelin send-tx --network development --to 0x879d15B21DE431E9520D6880E46Df374F7abB2a3 --method increase +✓ Transaction successful. Transaction hash: 0x32bea9c1f7d6fa8c522273bcae593c9ed1d072f93a256ba2a39a73b47b6203dd +✨ Done in 9.11s. +``` + +Now, let's call the `value` method: + +``` +yarn openzeppelin call --network extdev --to 0x879d15B21DE431E9520D6880E46Df374F7abB2a3 --method value +``` + +You should see something like the following printed out to the console: + +``` +yarn run v1.13.0 +warning package.json: No license field +$ /Users/andrei/upgradable-contracts/node_modules/.bin/openzeppelin call --network extdev --to 0x879d15B21DE431E9520D6880E46Df374F7abB2a3 --method value +✓ Method 'value' returned: 2 +2 +✨ Done in 3.11s. +``` + +### Upgrading Your Contract + +Next, let's upgrade the `Counter` smart contract. The `increase` function will now take the `amount` we want to increase the counter with as an argument. + +Replace the content of the `contracts/Counter.sol` file with the following code: + +```solidity +pragma solidity ^0.5.0; + +contract Counter { + uint256 public value; + + function increase(uint256 amount) public { + value += amount; + } +} +``` + +Next, let's run the `upgrade` command like this: + +```bash +# Upgrading the contract Counter +yarn openzeppelin upgrade Counter --network extdev +``` + +``` +✓ Compiled contracts with solc 0.5.12 (commit.7709ece9) +✓ Contract Counter deployed +All contracts have been deployed +? Do you want to call a function on the instance after upgrading it? No +✓ Instance upgraded at 0x879d15B21DE431E9520D6880E46Df374F7abB2a3. Transaction receipt: 0xbd665a22e6191664bad1dc8ab39db6d18c9abec3ba19139b1ee02c769b8f3bd3 +✓ Instance at 0x879d15B21DE431E9520D6880E46Df374F7abB2a3 upgraded +✨ Done in 66.80s. +``` + +> The above command will prompt you to call a function. Just answer `No` when prompted. We'll do this in the next step. + +Great! The `Counter` contract is updated, and the address is the same. Let's now run the `send-tx` and `call` function again to make sure the upgrade was successful. + +```bash +yarn openzeppelin send-tx --network extdev --to 0x879d15B21DE431E9520D6880E46Df374F7abB2a3 --method increase +``` + +At this point, you will be prompted to input the amount you want to increase the counter with: + +``` +? amount (uint256): +``` + +Just type `10` and then press `Enter`. + +> Note that we've just called `send-tx` with the same `--to` address and the SDK automatically ran the updated function that takes the `amount` as a parameter. + +At this point, the value stored on the blockchain should be `12`. Let's check with: + +```bash +yarn openzeppelin call --network extdev --to 0x879d15B21DE431E9520D6880E46Df374F7abB2a3 --method value +``` + +This will print something similar to the following: + +```bash +✓ Method 'value' returned: 12 +12 +✨ Done in 7.36s. +``` + +Cool! The result equals `12`, so it worked as expected. + +> Note: It is essential to have a good understanding of how upgradable smart contracts work. This, however, is not in the scope of this tutorial so we encourage you to check the OpenZeppelin SDK [official documentation](https://docs.openzeppelin.com/sdk/) to further your knowledge. diff --git a/docs/ja/upgradable-smart-contracts-openzeppelin-sdk.md b/docs/ja/upgradable-smart-contracts-openzeppelin-sdk.md new file mode 100644 index 000000000..b802a32bb --- /dev/null +++ b/docs/ja/upgradable-smart-contracts-openzeppelin-sdk.md @@ -0,0 +1,220 @@ +--- +id: updradable-smart-contracts-with-openzeppelin-sdk +title: Upgradable Smart Contracts with OpenZeppelin SDK +sidebar_label: Upgradable Smart Contracts with OpenZeppelin SDK +--- + +## Purpose + +This guide will walk you through the process of deploying an [upgradable smart contract](https://docs.openzeppelin.com/sdk/2.5/writing-contracts) using the [OpenZeppelin SDK](https://docs.openzeppelin.com/sdk/). + +## Prerequisites + +- Node.js 10 or higher (recommended v10.15.3). You can download the installer from [this page](https://nodejs.org/en/download/) + +- Yarn. Follow [these instructions](https://yarnpkg.com/lang/en/docs/install/) to install Yarn on your computer. + +## Setting Things Up + +The first step is to install the `OpenZeppelin SDK`. In a terminal window, move into the directory where you want to save the new project, and enter the following commands: + +```bash +mkdir upgradable-contracts && cd upgradable-contracts + +# Setting up the packages +yarn add -D @openzeppelin/cli@2.5.3 js-sha256 loom-js@dev loom-truffle-provider@dev + +# Intialize OpenZeppelin SDK +yarn openzeppelin init upgradable-contracts 1.0.0 +``` + +> Note: We're using the developer versions of `loom-js` and `loom-truffle-provider`. + +The above command creates a bunch of directories and files. For the scope of this tutorial we'll be using: + +* The `networks.js`file which stores the deployment configuration. If you're familiar with Truffle, this file is similar to the `truffle-config.js` file. +* The `contracts` directory where we'll place our smart contracts. + +## Configuring LoomTruffleProvider + +Now, let's configure OpenZeppelin to use `loom-truffle-provider` and deploy to Loom Testnet (`extdev`). Replace the content of the `networks.js` file with the following snippet: + +```js +const LoomTruffleProvider = require('loom-truffle-provider') +const { sha256 } = require('js-sha256') +const { CryptoUtils } = require('loom-js') +function getLoomProviderWithMnemonic (seed, chainId, writeUrl, readUrl) { + const privateKeyUint8ArrayFromSeed = CryptoUtils.generatePrivateKeyFromSeed(new Uint8Array(sha256.array(seed))) + const privateKeyB64 = CryptoUtils.Uint8ArrayToB64(privateKeyUint8ArrayFromSeed) + return new LoomTruffleProvider(chainId, writeUrl, readUrl, privateKeyB64) +} +module.exports = { + networks: { + extdev: { + provider: function() { + const chainId = 'extdev-plasma-us1' + const writeUrl = 'http://extdev-plasma-us1.dappchains.com:80/rpc' + const readUrl = 'http://extdev-plasma-us1.dappchains.com:80/eth' + const seed = 'gravity top burden flip student usage spell purchase hundred improve check genre' + const loomTruffleProvider = getLoomProviderWithMnemonic(seed, chainId, writeUrl, readUrl) + // Quick implementation for web3_clientVersion + loomTruffleProvider.getProviderEngine().addCustomMethod('web3_clientVersion', () => { + return 'loom-js' + }) + return loomTruffleProvider + }, + networkId: '*', + }, + }, +} +``` + +## Your First Contract + +> Note: The next sections are using almost the same steps as the OpenZeppelin tutorial on working with upgradable contracts. However, to make this tutorial easier to follow, we'll also present these steps here. + +In the `contracts` directory, let's create a file called `Counter.sol` with the following content: + +```solidity +pragma solidity ^0.5.0; + +contract Counter { + uint256 public value; + + function increase() public { + value++; + } +} +``` + +Now let's deploy our smart contract and call the `increase` function by running the `yarn openzeppelin create` command and passing the following arguments: + +* The name of the smart contract (`Counter`) +* the `--network` flag with the name of the network we want to deploy our smart contract to (`extdev`) +* the `--init` flag with the name of the function we want to call once the contracts gets deployed (`increase`) + +```bash +yarn openzeppelin create Counter --network extdev --init increase +``` + + +``` +✓ Setting everything up to create contract instances +✓ Instance created at 0x879d15B21DE431E9520D6880E46Df374F7abB2a3 +0x879d15B21DE431E9520D6880E46Df374F7abB2a3 +✨ Done in 360.73s. +``` + +> The `--init` flag is very useful because you can't use constructors with the OpenZeppelin SDK. For more details, please refer to the [OpenZeppelin SDK Upgrades Pattern]https://docs.openzeppelin.com/sdk/2.5/pattern#the-constructor-caveat) page. + + +### Send-TX and Call + +Now that our contract is deployed, let's send a transaction by running the `yarn openzeppelin send-tx` and passing it the following flags: + +* `--network` with the name of the network we deployed our smart contract to (`extdev`). +* `--to` with the address of the `Counter` smart contract. In our example the address is `0x879d15B21DE431E9520D6880E46Df374F7abB2a3` but yours will be different. + +* `--method` with the name of the method we want to call (`increase`) + +Example: + +``` +yarn openzeppelin send-tx --network development --to 0x879d15B21DE431E9520D6880E46Df374F7abB2a3 --method increase +``` + +``` +yarn run v1.13.0 +warning package.json: No license field +$ /Users/andrei/upgradable-contracts/node_modules/.bin/openzeppelin send-tx --network development --to 0x879d15B21DE431E9520D6880E46Df374F7abB2a3 --method increase +✓ Transaction successful. Transaction hash: 0x32bea9c1f7d6fa8c522273bcae593c9ed1d072f93a256ba2a39a73b47b6203dd +✨ Done in 9.11s. +``` + +Now, let's call the `value` method: + +``` +yarn openzeppelin call --network extdev --to 0x879d15B21DE431E9520D6880E46Df374F7abB2a3 --method value +``` + +You should see something like the following printed out to the console: + +``` +yarn run v1.13.0 +warning package.json: No license field +$ /Users/andrei/upgradable-contracts/node_modules/.bin/openzeppelin call --network extdev --to 0x879d15B21DE431E9520D6880E46Df374F7abB2a3 --method value +✓ Method 'value' returned: 2 +2 +✨ Done in 3.11s. +``` + +### Upgrading Your Contract + +Next, let's upgrade the `Counter` smart contract. The `increase` function will now take the `amount` we want to increase the counter with as an argument. + +Replace the content of the `contracts/Counter.sol` file with the following code: + +```solidity +pragma solidity ^0.5.0; + +contract Counter { + uint256 public value; + + function increase(uint256 amount) public { + value += amount; + } +} +``` + +Next, let's run the `upgrade` command like this: + +```bash +# Upgrading the contract Counter +yarn openzeppelin upgrade Counter --network extdev +``` + +``` +✓ Compiled contracts with solc 0.5.12 (commit.7709ece9) +✓ Contract Counter deployed +All contracts have been deployed +? Do you want to call a function on the instance after upgrading it? No +✓ Instance upgraded at 0x879d15B21DE431E9520D6880E46Df374F7abB2a3. Transaction receipt: 0xbd665a22e6191664bad1dc8ab39db6d18c9abec3ba19139b1ee02c769b8f3bd3 +✓ Instance at 0x879d15B21DE431E9520D6880E46Df374F7abB2a3 upgraded +✨ Done in 66.80s. +``` + +> The above command will prompt you to call a function. Just answer `No` when prompted. We'll do this in the next step. + +Great! The `Counter` contract is updated, and the address is the same. Let's now run the `send-tx` and `call` function again to make sure the upgrade was successful. + +```bash +yarn openzeppelin send-tx --network extdev --to 0x879d15B21DE431E9520D6880E46Df374F7abB2a3 --method increase +``` + +At this point, you will be prompted to input the amount you want to increase the counter with: + +``` +? amount (uint256): +``` + +Just type `10` and then press `Enter`. + +> Note that we've just called `send-tx` with the same `--to` address and the SDK automatically ran the updated function that takes the `amount` as a parameter. + +At this point, the value stored on the blockchain should be `12`. Let's check with: + +```bash +yarn openzeppelin call --network extdev --to 0x879d15B21DE431E9520D6880E46Df374F7abB2a3 --method value +``` + +This will print something similar to the following: + +```bash +✓ Method 'value' returned: 12 +12 +✨ Done in 7.36s. +``` + +Cool! The result equals `12`, so it worked as expected. + +> Note: It is essential to have a good understanding of how upgradable smart contracts work. This, however, is not in the scope of this tutorial so we encourage you to check the OpenZeppelin SDK [official documentation](https://docs.openzeppelin.com/sdk/) to further your knowledge. diff --git a/docs/ko/upgradable-smart-contracts-openzeppelin-sdk.md b/docs/ko/upgradable-smart-contracts-openzeppelin-sdk.md new file mode 100644 index 000000000..b802a32bb --- /dev/null +++ b/docs/ko/upgradable-smart-contracts-openzeppelin-sdk.md @@ -0,0 +1,220 @@ +--- +id: updradable-smart-contracts-with-openzeppelin-sdk +title: Upgradable Smart Contracts with OpenZeppelin SDK +sidebar_label: Upgradable Smart Contracts with OpenZeppelin SDK +--- + +## Purpose + +This guide will walk you through the process of deploying an [upgradable smart contract](https://docs.openzeppelin.com/sdk/2.5/writing-contracts) using the [OpenZeppelin SDK](https://docs.openzeppelin.com/sdk/). + +## Prerequisites + +- Node.js 10 or higher (recommended v10.15.3). You can download the installer from [this page](https://nodejs.org/en/download/) + +- Yarn. Follow [these instructions](https://yarnpkg.com/lang/en/docs/install/) to install Yarn on your computer. + +## Setting Things Up + +The first step is to install the `OpenZeppelin SDK`. In a terminal window, move into the directory where you want to save the new project, and enter the following commands: + +```bash +mkdir upgradable-contracts && cd upgradable-contracts + +# Setting up the packages +yarn add -D @openzeppelin/cli@2.5.3 js-sha256 loom-js@dev loom-truffle-provider@dev + +# Intialize OpenZeppelin SDK +yarn openzeppelin init upgradable-contracts 1.0.0 +``` + +> Note: We're using the developer versions of `loom-js` and `loom-truffle-provider`. + +The above command creates a bunch of directories and files. For the scope of this tutorial we'll be using: + +* The `networks.js`file which stores the deployment configuration. If you're familiar with Truffle, this file is similar to the `truffle-config.js` file. +* The `contracts` directory where we'll place our smart contracts. + +## Configuring LoomTruffleProvider + +Now, let's configure OpenZeppelin to use `loom-truffle-provider` and deploy to Loom Testnet (`extdev`). Replace the content of the `networks.js` file with the following snippet: + +```js +const LoomTruffleProvider = require('loom-truffle-provider') +const { sha256 } = require('js-sha256') +const { CryptoUtils } = require('loom-js') +function getLoomProviderWithMnemonic (seed, chainId, writeUrl, readUrl) { + const privateKeyUint8ArrayFromSeed = CryptoUtils.generatePrivateKeyFromSeed(new Uint8Array(sha256.array(seed))) + const privateKeyB64 = CryptoUtils.Uint8ArrayToB64(privateKeyUint8ArrayFromSeed) + return new LoomTruffleProvider(chainId, writeUrl, readUrl, privateKeyB64) +} +module.exports = { + networks: { + extdev: { + provider: function() { + const chainId = 'extdev-plasma-us1' + const writeUrl = 'http://extdev-plasma-us1.dappchains.com:80/rpc' + const readUrl = 'http://extdev-plasma-us1.dappchains.com:80/eth' + const seed = 'gravity top burden flip student usage spell purchase hundred improve check genre' + const loomTruffleProvider = getLoomProviderWithMnemonic(seed, chainId, writeUrl, readUrl) + // Quick implementation for web3_clientVersion + loomTruffleProvider.getProviderEngine().addCustomMethod('web3_clientVersion', () => { + return 'loom-js' + }) + return loomTruffleProvider + }, + networkId: '*', + }, + }, +} +``` + +## Your First Contract + +> Note: The next sections are using almost the same steps as the OpenZeppelin tutorial on working with upgradable contracts. However, to make this tutorial easier to follow, we'll also present these steps here. + +In the `contracts` directory, let's create a file called `Counter.sol` with the following content: + +```solidity +pragma solidity ^0.5.0; + +contract Counter { + uint256 public value; + + function increase() public { + value++; + } +} +``` + +Now let's deploy our smart contract and call the `increase` function by running the `yarn openzeppelin create` command and passing the following arguments: + +* The name of the smart contract (`Counter`) +* the `--network` flag with the name of the network we want to deploy our smart contract to (`extdev`) +* the `--init` flag with the name of the function we want to call once the contracts gets deployed (`increase`) + +```bash +yarn openzeppelin create Counter --network extdev --init increase +``` + + +``` +✓ Setting everything up to create contract instances +✓ Instance created at 0x879d15B21DE431E9520D6880E46Df374F7abB2a3 +0x879d15B21DE431E9520D6880E46Df374F7abB2a3 +✨ Done in 360.73s. +``` + +> The `--init` flag is very useful because you can't use constructors with the OpenZeppelin SDK. For more details, please refer to the [OpenZeppelin SDK Upgrades Pattern]https://docs.openzeppelin.com/sdk/2.5/pattern#the-constructor-caveat) page. + + +### Send-TX and Call + +Now that our contract is deployed, let's send a transaction by running the `yarn openzeppelin send-tx` and passing it the following flags: + +* `--network` with the name of the network we deployed our smart contract to (`extdev`). +* `--to` with the address of the `Counter` smart contract. In our example the address is `0x879d15B21DE431E9520D6880E46Df374F7abB2a3` but yours will be different. + +* `--method` with the name of the method we want to call (`increase`) + +Example: + +``` +yarn openzeppelin send-tx --network development --to 0x879d15B21DE431E9520D6880E46Df374F7abB2a3 --method increase +``` + +``` +yarn run v1.13.0 +warning package.json: No license field +$ /Users/andrei/upgradable-contracts/node_modules/.bin/openzeppelin send-tx --network development --to 0x879d15B21DE431E9520D6880E46Df374F7abB2a3 --method increase +✓ Transaction successful. Transaction hash: 0x32bea9c1f7d6fa8c522273bcae593c9ed1d072f93a256ba2a39a73b47b6203dd +✨ Done in 9.11s. +``` + +Now, let's call the `value` method: + +``` +yarn openzeppelin call --network extdev --to 0x879d15B21DE431E9520D6880E46Df374F7abB2a3 --method value +``` + +You should see something like the following printed out to the console: + +``` +yarn run v1.13.0 +warning package.json: No license field +$ /Users/andrei/upgradable-contracts/node_modules/.bin/openzeppelin call --network extdev --to 0x879d15B21DE431E9520D6880E46Df374F7abB2a3 --method value +✓ Method 'value' returned: 2 +2 +✨ Done in 3.11s. +``` + +### Upgrading Your Contract + +Next, let's upgrade the `Counter` smart contract. The `increase` function will now take the `amount` we want to increase the counter with as an argument. + +Replace the content of the `contracts/Counter.sol` file with the following code: + +```solidity +pragma solidity ^0.5.0; + +contract Counter { + uint256 public value; + + function increase(uint256 amount) public { + value += amount; + } +} +``` + +Next, let's run the `upgrade` command like this: + +```bash +# Upgrading the contract Counter +yarn openzeppelin upgrade Counter --network extdev +``` + +``` +✓ Compiled contracts with solc 0.5.12 (commit.7709ece9) +✓ Contract Counter deployed +All contracts have been deployed +? Do you want to call a function on the instance after upgrading it? No +✓ Instance upgraded at 0x879d15B21DE431E9520D6880E46Df374F7abB2a3. Transaction receipt: 0xbd665a22e6191664bad1dc8ab39db6d18c9abec3ba19139b1ee02c769b8f3bd3 +✓ Instance at 0x879d15B21DE431E9520D6880E46Df374F7abB2a3 upgraded +✨ Done in 66.80s. +``` + +> The above command will prompt you to call a function. Just answer `No` when prompted. We'll do this in the next step. + +Great! The `Counter` contract is updated, and the address is the same. Let's now run the `send-tx` and `call` function again to make sure the upgrade was successful. + +```bash +yarn openzeppelin send-tx --network extdev --to 0x879d15B21DE431E9520D6880E46Df374F7abB2a3 --method increase +``` + +At this point, you will be prompted to input the amount you want to increase the counter with: + +``` +? amount (uint256): +``` + +Just type `10` and then press `Enter`. + +> Note that we've just called `send-tx` with the same `--to` address and the SDK automatically ran the updated function that takes the `amount` as a parameter. + +At this point, the value stored on the blockchain should be `12`. Let's check with: + +```bash +yarn openzeppelin call --network extdev --to 0x879d15B21DE431E9520D6880E46Df374F7abB2a3 --method value +``` + +This will print something similar to the following: + +```bash +✓ Method 'value' returned: 12 +12 +✨ Done in 7.36s. +``` + +Cool! The result equals `12`, so it worked as expected. + +> Note: It is essential to have a good understanding of how upgradable smart contracts work. This, however, is not in the scope of this tutorial so we encourage you to check the OpenZeppelin SDK [official documentation](https://docs.openzeppelin.com/sdk/) to further your knowledge. diff --git a/docs/zh-CN/upgradable-smart-contracts-openzeppelin-sdk.md b/docs/zh-CN/upgradable-smart-contracts-openzeppelin-sdk.md new file mode 100644 index 000000000..b802a32bb --- /dev/null +++ b/docs/zh-CN/upgradable-smart-contracts-openzeppelin-sdk.md @@ -0,0 +1,220 @@ +--- +id: updradable-smart-contracts-with-openzeppelin-sdk +title: Upgradable Smart Contracts with OpenZeppelin SDK +sidebar_label: Upgradable Smart Contracts with OpenZeppelin SDK +--- + +## Purpose + +This guide will walk you through the process of deploying an [upgradable smart contract](https://docs.openzeppelin.com/sdk/2.5/writing-contracts) using the [OpenZeppelin SDK](https://docs.openzeppelin.com/sdk/). + +## Prerequisites + +- Node.js 10 or higher (recommended v10.15.3). You can download the installer from [this page](https://nodejs.org/en/download/) + +- Yarn. Follow [these instructions](https://yarnpkg.com/lang/en/docs/install/) to install Yarn on your computer. + +## Setting Things Up + +The first step is to install the `OpenZeppelin SDK`. In a terminal window, move into the directory where you want to save the new project, and enter the following commands: + +```bash +mkdir upgradable-contracts && cd upgradable-contracts + +# Setting up the packages +yarn add -D @openzeppelin/cli@2.5.3 js-sha256 loom-js@dev loom-truffle-provider@dev + +# Intialize OpenZeppelin SDK +yarn openzeppelin init upgradable-contracts 1.0.0 +``` + +> Note: We're using the developer versions of `loom-js` and `loom-truffle-provider`. + +The above command creates a bunch of directories and files. For the scope of this tutorial we'll be using: + +* The `networks.js`file which stores the deployment configuration. If you're familiar with Truffle, this file is similar to the `truffle-config.js` file. +* The `contracts` directory where we'll place our smart contracts. + +## Configuring LoomTruffleProvider + +Now, let's configure OpenZeppelin to use `loom-truffle-provider` and deploy to Loom Testnet (`extdev`). Replace the content of the `networks.js` file with the following snippet: + +```js +const LoomTruffleProvider = require('loom-truffle-provider') +const { sha256 } = require('js-sha256') +const { CryptoUtils } = require('loom-js') +function getLoomProviderWithMnemonic (seed, chainId, writeUrl, readUrl) { + const privateKeyUint8ArrayFromSeed = CryptoUtils.generatePrivateKeyFromSeed(new Uint8Array(sha256.array(seed))) + const privateKeyB64 = CryptoUtils.Uint8ArrayToB64(privateKeyUint8ArrayFromSeed) + return new LoomTruffleProvider(chainId, writeUrl, readUrl, privateKeyB64) +} +module.exports = { + networks: { + extdev: { + provider: function() { + const chainId = 'extdev-plasma-us1' + const writeUrl = 'http://extdev-plasma-us1.dappchains.com:80/rpc' + const readUrl = 'http://extdev-plasma-us1.dappchains.com:80/eth' + const seed = 'gravity top burden flip student usage spell purchase hundred improve check genre' + const loomTruffleProvider = getLoomProviderWithMnemonic(seed, chainId, writeUrl, readUrl) + // Quick implementation for web3_clientVersion + loomTruffleProvider.getProviderEngine().addCustomMethod('web3_clientVersion', () => { + return 'loom-js' + }) + return loomTruffleProvider + }, + networkId: '*', + }, + }, +} +``` + +## Your First Contract + +> Note: The next sections are using almost the same steps as the OpenZeppelin tutorial on working with upgradable contracts. However, to make this tutorial easier to follow, we'll also present these steps here. + +In the `contracts` directory, let's create a file called `Counter.sol` with the following content: + +```solidity +pragma solidity ^0.5.0; + +contract Counter { + uint256 public value; + + function increase() public { + value++; + } +} +``` + +Now let's deploy our smart contract and call the `increase` function by running the `yarn openzeppelin create` command and passing the following arguments: + +* The name of the smart contract (`Counter`) +* the `--network` flag with the name of the network we want to deploy our smart contract to (`extdev`) +* the `--init` flag with the name of the function we want to call once the contracts gets deployed (`increase`) + +```bash +yarn openzeppelin create Counter --network extdev --init increase +``` + + +``` +✓ Setting everything up to create contract instances +✓ Instance created at 0x879d15B21DE431E9520D6880E46Df374F7abB2a3 +0x879d15B21DE431E9520D6880E46Df374F7abB2a3 +✨ Done in 360.73s. +``` + +> The `--init` flag is very useful because you can't use constructors with the OpenZeppelin SDK. For more details, please refer to the [OpenZeppelin SDK Upgrades Pattern]https://docs.openzeppelin.com/sdk/2.5/pattern#the-constructor-caveat) page. + + +### Send-TX and Call + +Now that our contract is deployed, let's send a transaction by running the `yarn openzeppelin send-tx` and passing it the following flags: + +* `--network` with the name of the network we deployed our smart contract to (`extdev`). +* `--to` with the address of the `Counter` smart contract. In our example the address is `0x879d15B21DE431E9520D6880E46Df374F7abB2a3` but yours will be different. + +* `--method` with the name of the method we want to call (`increase`) + +Example: + +``` +yarn openzeppelin send-tx --network development --to 0x879d15B21DE431E9520D6880E46Df374F7abB2a3 --method increase +``` + +``` +yarn run v1.13.0 +warning package.json: No license field +$ /Users/andrei/upgradable-contracts/node_modules/.bin/openzeppelin send-tx --network development --to 0x879d15B21DE431E9520D6880E46Df374F7abB2a3 --method increase +✓ Transaction successful. Transaction hash: 0x32bea9c1f7d6fa8c522273bcae593c9ed1d072f93a256ba2a39a73b47b6203dd +✨ Done in 9.11s. +``` + +Now, let's call the `value` method: + +``` +yarn openzeppelin call --network extdev --to 0x879d15B21DE431E9520D6880E46Df374F7abB2a3 --method value +``` + +You should see something like the following printed out to the console: + +``` +yarn run v1.13.0 +warning package.json: No license field +$ /Users/andrei/upgradable-contracts/node_modules/.bin/openzeppelin call --network extdev --to 0x879d15B21DE431E9520D6880E46Df374F7abB2a3 --method value +✓ Method 'value' returned: 2 +2 +✨ Done in 3.11s. +``` + +### Upgrading Your Contract + +Next, let's upgrade the `Counter` smart contract. The `increase` function will now take the `amount` we want to increase the counter with as an argument. + +Replace the content of the `contracts/Counter.sol` file with the following code: + +```solidity +pragma solidity ^0.5.0; + +contract Counter { + uint256 public value; + + function increase(uint256 amount) public { + value += amount; + } +} +``` + +Next, let's run the `upgrade` command like this: + +```bash +# Upgrading the contract Counter +yarn openzeppelin upgrade Counter --network extdev +``` + +``` +✓ Compiled contracts with solc 0.5.12 (commit.7709ece9) +✓ Contract Counter deployed +All contracts have been deployed +? Do you want to call a function on the instance after upgrading it? No +✓ Instance upgraded at 0x879d15B21DE431E9520D6880E46Df374F7abB2a3. Transaction receipt: 0xbd665a22e6191664bad1dc8ab39db6d18c9abec3ba19139b1ee02c769b8f3bd3 +✓ Instance at 0x879d15B21DE431E9520D6880E46Df374F7abB2a3 upgraded +✨ Done in 66.80s. +``` + +> The above command will prompt you to call a function. Just answer `No` when prompted. We'll do this in the next step. + +Great! The `Counter` contract is updated, and the address is the same. Let's now run the `send-tx` and `call` function again to make sure the upgrade was successful. + +```bash +yarn openzeppelin send-tx --network extdev --to 0x879d15B21DE431E9520D6880E46Df374F7abB2a3 --method increase +``` + +At this point, you will be prompted to input the amount you want to increase the counter with: + +``` +? amount (uint256): +``` + +Just type `10` and then press `Enter`. + +> Note that we've just called `send-tx` with the same `--to` address and the SDK automatically ran the updated function that takes the `amount` as a parameter. + +At this point, the value stored on the blockchain should be `12`. Let's check with: + +```bash +yarn openzeppelin call --network extdev --to 0x879d15B21DE431E9520D6880E46Df374F7abB2a3 --method value +``` + +This will print something similar to the following: + +```bash +✓ Method 'value' returned: 12 +12 +✨ Done in 7.36s. +``` + +Cool! The result equals `12`, so it worked as expected. + +> Note: It is essential to have a good understanding of how upgradable smart contracts work. This, however, is not in the scope of this tutorial so we encourage you to check the OpenZeppelin SDK [official documentation](https://docs.openzeppelin.com/sdk/) to further your knowledge. From 2fa16e00d26d9eb2411e6442ee2587f84ca1e8b4 Mon Sep 17 00:00:00 2001 From: Andrei Popescu Date: Mon, 21 Oct 2019 17:16:48 +0300 Subject: [PATCH 3/3] a few updates a few updates --- ...adable-smart-contracts-openzeppelin-sdk.md | 62 +++++++++---------- ...adable-smart-contracts-openzeppelin-sdk.md | 62 +++++++++---------- ...adable-smart-contracts-openzeppelin-sdk.md | 62 +++++++++---------- ...adable-smart-contracts-openzeppelin-sdk.md | 62 +++++++++---------- 4 files changed, 124 insertions(+), 124 deletions(-) diff --git a/docs/en/upgradable-smart-contracts-openzeppelin-sdk.md b/docs/en/upgradable-smart-contracts-openzeppelin-sdk.md index b802a32bb..6bf981022 100644 --- a/docs/en/upgradable-smart-contracts-openzeppelin-sdk.md +++ b/docs/en/upgradable-smart-contracts-openzeppelin-sdk.md @@ -43,29 +43,27 @@ Now, let's configure OpenZeppelin to use `loom-truffle-provider` and deploy to L const LoomTruffleProvider = require('loom-truffle-provider') const { sha256 } = require('js-sha256') const { CryptoUtils } = require('loom-js') -function getLoomProviderWithMnemonic (seed, chainId, writeUrl, readUrl) { - const privateKeyUint8ArrayFromSeed = CryptoUtils.generatePrivateKeyFromSeed(new Uint8Array(sha256.array(seed))) - const privateKeyB64 = CryptoUtils.Uint8ArrayToB64(privateKeyUint8ArrayFromSeed) - return new LoomTruffleProvider(chainId, writeUrl, readUrl, privateKeyB64) +function getLoomProviderWithMnemonic(seed, chainId, writeUrl, readUrl) { + const privateKeyUint8ArrayFromSeed = CryptoUtils.generatePrivateKeyFromSeed(new Uint8Array(sha256.array(seed))) + const privateKeyB64 = CryptoUtils.Uint8ArrayToB64(privateKeyUint8ArrayFromSeed) + return new LoomTruffleProvider(chainId, writeUrl, readUrl, privateKeyB64) } module.exports = { - networks: { - extdev: { - provider: function() { - const chainId = 'extdev-plasma-us1' - const writeUrl = 'http://extdev-plasma-us1.dappchains.com:80/rpc' - const readUrl = 'http://extdev-plasma-us1.dappchains.com:80/eth' - const seed = 'gravity top burden flip student usage spell purchase hundred improve check genre' - const loomTruffleProvider = getLoomProviderWithMnemonic(seed, chainId, writeUrl, readUrl) - // Quick implementation for web3_clientVersion - loomTruffleProvider.getProviderEngine().addCustomMethod('web3_clientVersion', () => { - return 'loom-js' - }) - return loomTruffleProvider - }, - networkId: '*', - }, - }, + networks: { + extdev: { + provider: function () { + const chainId = 'extdev-plasma-us1' + const writeUrl = 'http://extdev-plasma-us1.dappchains.com:80/rpc' + const readUrl = 'http://extdev-plasma-us1.dappchains.com:80/eth' + const seed = 'gravity top burden flip student usage spell purchase hundred improve check genre' + const loomTruffleProvider = getLoomProviderWithMnemonic(seed, chainId, writeUrl, readUrl) + // Quick implementation for web3_clientVersion + loomTruffleProvider.getProviderEngine().addCustomMethod('web3_clientVersion', () => 'loom-js') + return loomTruffleProvider + }, + networkId: '*', + }, + }, } ``` @@ -82,7 +80,7 @@ contract Counter { uint256 public value; function increase() public { - value++; + value++; } } ``` @@ -105,12 +103,12 @@ yarn openzeppelin create Counter --network extdev --init increase ✨ Done in 360.73s. ``` -> The `--init` flag is very useful because you can't use constructors with the OpenZeppelin SDK. For more details, please refer to the [OpenZeppelin SDK Upgrades Pattern]https://docs.openzeppelin.com/sdk/2.5/pattern#the-constructor-caveat) page. +> The `--init` flag is very useful because you can't use constructors with the OpenZeppelin SDK. For more details, please refer to the [OpenZeppelin SDK Upgrades Pattern](https://docs.openzeppelin.com/sdk/2.5/pattern#the-constructor-caveat) page. -### Send-TX and Call +### The Send-TX Method -Now that our contract is deployed, let's send a transaction by running the `yarn openzeppelin send-tx` and passing it the following flags: +Now that your contract is deployed, let's send a transaction by running the `yarn openzeppelin send-tx` and passing it the following flags: * `--network` with the name of the network we deployed our smart contract to (`extdev`). * `--to` with the address of the `Counter` smart contract. In our example the address is `0x879d15B21DE431E9520D6880E46Df374F7abB2a3` but yours will be different. @@ -131,7 +129,9 @@ $ /Users/andrei/upgradable-contracts/node_modules/.bin/openzeppelin send-tx --ne ✨ Done in 9.11s. ``` -Now, let's call the `value` method: +### The `call` Method + +Next, let's use OpenZeppelin's `call` method to read the value stored on the blockchain: ``` yarn openzeppelin call --network extdev --to 0x879d15B21DE431E9520D6880E46Df374F7abB2a3 --method value @@ -155,14 +155,14 @@ Next, let's upgrade the `Counter` smart contract. The `increase` function will n Replace the content of the `contracts/Counter.sol` file with the following code: ```solidity -pragma solidity ^0.5.0; +pragma solidity ^ 0.5.0; contract Counter { - uint256 public value; + uint256 public value; - function increase(uint256 amount) public { - value += amount; - } + function increase(uint256 amount) public { + value += amount; + } } ``` diff --git a/docs/ja/upgradable-smart-contracts-openzeppelin-sdk.md b/docs/ja/upgradable-smart-contracts-openzeppelin-sdk.md index b802a32bb..6bf981022 100644 --- a/docs/ja/upgradable-smart-contracts-openzeppelin-sdk.md +++ b/docs/ja/upgradable-smart-contracts-openzeppelin-sdk.md @@ -43,29 +43,27 @@ Now, let's configure OpenZeppelin to use `loom-truffle-provider` and deploy to L const LoomTruffleProvider = require('loom-truffle-provider') const { sha256 } = require('js-sha256') const { CryptoUtils } = require('loom-js') -function getLoomProviderWithMnemonic (seed, chainId, writeUrl, readUrl) { - const privateKeyUint8ArrayFromSeed = CryptoUtils.generatePrivateKeyFromSeed(new Uint8Array(sha256.array(seed))) - const privateKeyB64 = CryptoUtils.Uint8ArrayToB64(privateKeyUint8ArrayFromSeed) - return new LoomTruffleProvider(chainId, writeUrl, readUrl, privateKeyB64) +function getLoomProviderWithMnemonic(seed, chainId, writeUrl, readUrl) { + const privateKeyUint8ArrayFromSeed = CryptoUtils.generatePrivateKeyFromSeed(new Uint8Array(sha256.array(seed))) + const privateKeyB64 = CryptoUtils.Uint8ArrayToB64(privateKeyUint8ArrayFromSeed) + return new LoomTruffleProvider(chainId, writeUrl, readUrl, privateKeyB64) } module.exports = { - networks: { - extdev: { - provider: function() { - const chainId = 'extdev-plasma-us1' - const writeUrl = 'http://extdev-plasma-us1.dappchains.com:80/rpc' - const readUrl = 'http://extdev-plasma-us1.dappchains.com:80/eth' - const seed = 'gravity top burden flip student usage spell purchase hundred improve check genre' - const loomTruffleProvider = getLoomProviderWithMnemonic(seed, chainId, writeUrl, readUrl) - // Quick implementation for web3_clientVersion - loomTruffleProvider.getProviderEngine().addCustomMethod('web3_clientVersion', () => { - return 'loom-js' - }) - return loomTruffleProvider - }, - networkId: '*', - }, - }, + networks: { + extdev: { + provider: function () { + const chainId = 'extdev-plasma-us1' + const writeUrl = 'http://extdev-plasma-us1.dappchains.com:80/rpc' + const readUrl = 'http://extdev-plasma-us1.dappchains.com:80/eth' + const seed = 'gravity top burden flip student usage spell purchase hundred improve check genre' + const loomTruffleProvider = getLoomProviderWithMnemonic(seed, chainId, writeUrl, readUrl) + // Quick implementation for web3_clientVersion + loomTruffleProvider.getProviderEngine().addCustomMethod('web3_clientVersion', () => 'loom-js') + return loomTruffleProvider + }, + networkId: '*', + }, + }, } ``` @@ -82,7 +80,7 @@ contract Counter { uint256 public value; function increase() public { - value++; + value++; } } ``` @@ -105,12 +103,12 @@ yarn openzeppelin create Counter --network extdev --init increase ✨ Done in 360.73s. ``` -> The `--init` flag is very useful because you can't use constructors with the OpenZeppelin SDK. For more details, please refer to the [OpenZeppelin SDK Upgrades Pattern]https://docs.openzeppelin.com/sdk/2.5/pattern#the-constructor-caveat) page. +> The `--init` flag is very useful because you can't use constructors with the OpenZeppelin SDK. For more details, please refer to the [OpenZeppelin SDK Upgrades Pattern](https://docs.openzeppelin.com/sdk/2.5/pattern#the-constructor-caveat) page. -### Send-TX and Call +### The Send-TX Method -Now that our contract is deployed, let's send a transaction by running the `yarn openzeppelin send-tx` and passing it the following flags: +Now that your contract is deployed, let's send a transaction by running the `yarn openzeppelin send-tx` and passing it the following flags: * `--network` with the name of the network we deployed our smart contract to (`extdev`). * `--to` with the address of the `Counter` smart contract. In our example the address is `0x879d15B21DE431E9520D6880E46Df374F7abB2a3` but yours will be different. @@ -131,7 +129,9 @@ $ /Users/andrei/upgradable-contracts/node_modules/.bin/openzeppelin send-tx --ne ✨ Done in 9.11s. ``` -Now, let's call the `value` method: +### The `call` Method + +Next, let's use OpenZeppelin's `call` method to read the value stored on the blockchain: ``` yarn openzeppelin call --network extdev --to 0x879d15B21DE431E9520D6880E46Df374F7abB2a3 --method value @@ -155,14 +155,14 @@ Next, let's upgrade the `Counter` smart contract. The `increase` function will n Replace the content of the `contracts/Counter.sol` file with the following code: ```solidity -pragma solidity ^0.5.0; +pragma solidity ^ 0.5.0; contract Counter { - uint256 public value; + uint256 public value; - function increase(uint256 amount) public { - value += amount; - } + function increase(uint256 amount) public { + value += amount; + } } ``` diff --git a/docs/ko/upgradable-smart-contracts-openzeppelin-sdk.md b/docs/ko/upgradable-smart-contracts-openzeppelin-sdk.md index b802a32bb..6bf981022 100644 --- a/docs/ko/upgradable-smart-contracts-openzeppelin-sdk.md +++ b/docs/ko/upgradable-smart-contracts-openzeppelin-sdk.md @@ -43,29 +43,27 @@ Now, let's configure OpenZeppelin to use `loom-truffle-provider` and deploy to L const LoomTruffleProvider = require('loom-truffle-provider') const { sha256 } = require('js-sha256') const { CryptoUtils } = require('loom-js') -function getLoomProviderWithMnemonic (seed, chainId, writeUrl, readUrl) { - const privateKeyUint8ArrayFromSeed = CryptoUtils.generatePrivateKeyFromSeed(new Uint8Array(sha256.array(seed))) - const privateKeyB64 = CryptoUtils.Uint8ArrayToB64(privateKeyUint8ArrayFromSeed) - return new LoomTruffleProvider(chainId, writeUrl, readUrl, privateKeyB64) +function getLoomProviderWithMnemonic(seed, chainId, writeUrl, readUrl) { + const privateKeyUint8ArrayFromSeed = CryptoUtils.generatePrivateKeyFromSeed(new Uint8Array(sha256.array(seed))) + const privateKeyB64 = CryptoUtils.Uint8ArrayToB64(privateKeyUint8ArrayFromSeed) + return new LoomTruffleProvider(chainId, writeUrl, readUrl, privateKeyB64) } module.exports = { - networks: { - extdev: { - provider: function() { - const chainId = 'extdev-plasma-us1' - const writeUrl = 'http://extdev-plasma-us1.dappchains.com:80/rpc' - const readUrl = 'http://extdev-plasma-us1.dappchains.com:80/eth' - const seed = 'gravity top burden flip student usage spell purchase hundred improve check genre' - const loomTruffleProvider = getLoomProviderWithMnemonic(seed, chainId, writeUrl, readUrl) - // Quick implementation for web3_clientVersion - loomTruffleProvider.getProviderEngine().addCustomMethod('web3_clientVersion', () => { - return 'loom-js' - }) - return loomTruffleProvider - }, - networkId: '*', - }, - }, + networks: { + extdev: { + provider: function () { + const chainId = 'extdev-plasma-us1' + const writeUrl = 'http://extdev-plasma-us1.dappchains.com:80/rpc' + const readUrl = 'http://extdev-plasma-us1.dappchains.com:80/eth' + const seed = 'gravity top burden flip student usage spell purchase hundred improve check genre' + const loomTruffleProvider = getLoomProviderWithMnemonic(seed, chainId, writeUrl, readUrl) + // Quick implementation for web3_clientVersion + loomTruffleProvider.getProviderEngine().addCustomMethod('web3_clientVersion', () => 'loom-js') + return loomTruffleProvider + }, + networkId: '*', + }, + }, } ``` @@ -82,7 +80,7 @@ contract Counter { uint256 public value; function increase() public { - value++; + value++; } } ``` @@ -105,12 +103,12 @@ yarn openzeppelin create Counter --network extdev --init increase ✨ Done in 360.73s. ``` -> The `--init` flag is very useful because you can't use constructors with the OpenZeppelin SDK. For more details, please refer to the [OpenZeppelin SDK Upgrades Pattern]https://docs.openzeppelin.com/sdk/2.5/pattern#the-constructor-caveat) page. +> The `--init` flag is very useful because you can't use constructors with the OpenZeppelin SDK. For more details, please refer to the [OpenZeppelin SDK Upgrades Pattern](https://docs.openzeppelin.com/sdk/2.5/pattern#the-constructor-caveat) page. -### Send-TX and Call +### The Send-TX Method -Now that our contract is deployed, let's send a transaction by running the `yarn openzeppelin send-tx` and passing it the following flags: +Now that your contract is deployed, let's send a transaction by running the `yarn openzeppelin send-tx` and passing it the following flags: * `--network` with the name of the network we deployed our smart contract to (`extdev`). * `--to` with the address of the `Counter` smart contract. In our example the address is `0x879d15B21DE431E9520D6880E46Df374F7abB2a3` but yours will be different. @@ -131,7 +129,9 @@ $ /Users/andrei/upgradable-contracts/node_modules/.bin/openzeppelin send-tx --ne ✨ Done in 9.11s. ``` -Now, let's call the `value` method: +### The `call` Method + +Next, let's use OpenZeppelin's `call` method to read the value stored on the blockchain: ``` yarn openzeppelin call --network extdev --to 0x879d15B21DE431E9520D6880E46Df374F7abB2a3 --method value @@ -155,14 +155,14 @@ Next, let's upgrade the `Counter` smart contract. The `increase` function will n Replace the content of the `contracts/Counter.sol` file with the following code: ```solidity -pragma solidity ^0.5.0; +pragma solidity ^ 0.5.0; contract Counter { - uint256 public value; + uint256 public value; - function increase(uint256 amount) public { - value += amount; - } + function increase(uint256 amount) public { + value += amount; + } } ``` diff --git a/docs/zh-CN/upgradable-smart-contracts-openzeppelin-sdk.md b/docs/zh-CN/upgradable-smart-contracts-openzeppelin-sdk.md index b802a32bb..6bf981022 100644 --- a/docs/zh-CN/upgradable-smart-contracts-openzeppelin-sdk.md +++ b/docs/zh-CN/upgradable-smart-contracts-openzeppelin-sdk.md @@ -43,29 +43,27 @@ Now, let's configure OpenZeppelin to use `loom-truffle-provider` and deploy to L const LoomTruffleProvider = require('loom-truffle-provider') const { sha256 } = require('js-sha256') const { CryptoUtils } = require('loom-js') -function getLoomProviderWithMnemonic (seed, chainId, writeUrl, readUrl) { - const privateKeyUint8ArrayFromSeed = CryptoUtils.generatePrivateKeyFromSeed(new Uint8Array(sha256.array(seed))) - const privateKeyB64 = CryptoUtils.Uint8ArrayToB64(privateKeyUint8ArrayFromSeed) - return new LoomTruffleProvider(chainId, writeUrl, readUrl, privateKeyB64) +function getLoomProviderWithMnemonic(seed, chainId, writeUrl, readUrl) { + const privateKeyUint8ArrayFromSeed = CryptoUtils.generatePrivateKeyFromSeed(new Uint8Array(sha256.array(seed))) + const privateKeyB64 = CryptoUtils.Uint8ArrayToB64(privateKeyUint8ArrayFromSeed) + return new LoomTruffleProvider(chainId, writeUrl, readUrl, privateKeyB64) } module.exports = { - networks: { - extdev: { - provider: function() { - const chainId = 'extdev-plasma-us1' - const writeUrl = 'http://extdev-plasma-us1.dappchains.com:80/rpc' - const readUrl = 'http://extdev-plasma-us1.dappchains.com:80/eth' - const seed = 'gravity top burden flip student usage spell purchase hundred improve check genre' - const loomTruffleProvider = getLoomProviderWithMnemonic(seed, chainId, writeUrl, readUrl) - // Quick implementation for web3_clientVersion - loomTruffleProvider.getProviderEngine().addCustomMethod('web3_clientVersion', () => { - return 'loom-js' - }) - return loomTruffleProvider - }, - networkId: '*', - }, - }, + networks: { + extdev: { + provider: function () { + const chainId = 'extdev-plasma-us1' + const writeUrl = 'http://extdev-plasma-us1.dappchains.com:80/rpc' + const readUrl = 'http://extdev-plasma-us1.dappchains.com:80/eth' + const seed = 'gravity top burden flip student usage spell purchase hundred improve check genre' + const loomTruffleProvider = getLoomProviderWithMnemonic(seed, chainId, writeUrl, readUrl) + // Quick implementation for web3_clientVersion + loomTruffleProvider.getProviderEngine().addCustomMethod('web3_clientVersion', () => 'loom-js') + return loomTruffleProvider + }, + networkId: '*', + }, + }, } ``` @@ -82,7 +80,7 @@ contract Counter { uint256 public value; function increase() public { - value++; + value++; } } ``` @@ -105,12 +103,12 @@ yarn openzeppelin create Counter --network extdev --init increase ✨ Done in 360.73s. ``` -> The `--init` flag is very useful because you can't use constructors with the OpenZeppelin SDK. For more details, please refer to the [OpenZeppelin SDK Upgrades Pattern]https://docs.openzeppelin.com/sdk/2.5/pattern#the-constructor-caveat) page. +> The `--init` flag is very useful because you can't use constructors with the OpenZeppelin SDK. For more details, please refer to the [OpenZeppelin SDK Upgrades Pattern](https://docs.openzeppelin.com/sdk/2.5/pattern#the-constructor-caveat) page. -### Send-TX and Call +### The Send-TX Method -Now that our contract is deployed, let's send a transaction by running the `yarn openzeppelin send-tx` and passing it the following flags: +Now that your contract is deployed, let's send a transaction by running the `yarn openzeppelin send-tx` and passing it the following flags: * `--network` with the name of the network we deployed our smart contract to (`extdev`). * `--to` with the address of the `Counter` smart contract. In our example the address is `0x879d15B21DE431E9520D6880E46Df374F7abB2a3` but yours will be different. @@ -131,7 +129,9 @@ $ /Users/andrei/upgradable-contracts/node_modules/.bin/openzeppelin send-tx --ne ✨ Done in 9.11s. ``` -Now, let's call the `value` method: +### The `call` Method + +Next, let's use OpenZeppelin's `call` method to read the value stored on the blockchain: ``` yarn openzeppelin call --network extdev --to 0x879d15B21DE431E9520D6880E46Df374F7abB2a3 --method value @@ -155,14 +155,14 @@ Next, let's upgrade the `Counter` smart contract. The `increase` function will n Replace the content of the `contracts/Counter.sol` file with the following code: ```solidity -pragma solidity ^0.5.0; +pragma solidity ^ 0.5.0; contract Counter { - uint256 public value; + uint256 public value; - function increase(uint256 amount) public { - value += amount; - } + function increase(uint256 amount) public { + value += amount; + } } ```