<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Paulina Błaszkiewicz on Medium]]></title>
        <description><![CDATA[Stories by Paulina Błaszkiewicz on Medium]]></description>
        <link>https://medium.com/@paulina.blaszk?source=rss-5426a9f14a62------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*-AdTwrxH1N6naFLFfWsEcw.jpeg</url>
            <title>Stories by Paulina Błaszkiewicz on Medium</title>
            <link>https://medium.com/@paulina.blaszk?source=rss-5426a9f14a62------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 20:51:15 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@paulina.blaszk/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to create an upgradeable smart contract using OpenZeppelin SDK — example of fixing smart…]]></title>
            <link>https://medium.com/coinmonks/how-to-create-an-upgradeable-smart-contract-using-openzeppelin-sdk-example-of-fixing-smart-260dfbfd5bae?source=rss-5426a9f14a62------2</link>
            <guid isPermaLink="false">https://medium.com/p/260dfbfd5bae</guid>
            <category><![CDATA[solidity]]></category>
            <category><![CDATA[openzeppelin]]></category>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[ethereum]]></category>
            <category><![CDATA[smart-contracts]]></category>
            <dc:creator><![CDATA[Paulina Błaszkiewicz]]></dc:creator>
            <pubDate>Mon, 05 Aug 2019 09:04:26 GMT</pubDate>
            <atom:updated>2020-08-26T12:16:49.562Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to create an upgradeable smart contract using OpenZeppelin SDK</h3><h4>Example of fixing smart contract vulnerable to underflow/overflow attacks</h4><p>In this tutorial, we will use OpenZeppelin SDK to fix smart contract vulnerable to underflow/overflow attacks. Firstly we will prepare our environment and create the upgradeable smart contract with some bug and test it. Then we will update the code, upgrade the smart contract and finally, we will check if it is safe.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7wN5t9ILU0fpnhbMX2vtng.jpeg" /></figure><h3>Upgradeability in smart contracts</h3><p>In principle smart contracts are immutable. Once deployed to the network, they cannot be changed. Unfortunately in the smart contracts, like in every software, mistakes frequently happens. Sometimes they may cost millions of dollars… But don’t worry! Now we have OpenZeppelin SDK (former ZeppelinOS) — powerful tool to upgrade our smart contracts and fix mistakes. <em>OpenZeppelin SDK is a platform to develop, deploy and operate smart contract projects on Ethereum and every other EVM and eWASM-powered blockchain</em>as its <a href="https://docs.openzeppelin.com/sdk/2.5/">documentation </a>says. And one of its most important features is smart contracts upgradeability.</p><h3>Environment preparation</h3><p>You need to have installed essential software for creating and upgrading the smart contracts. You can find the instructions for installing Node.js and NPM and Ganache in my <a href="https://medium.com/@paulina.blaszk/how-to-write-and-deploy-first-smart-contract-in-solidity-0-5-x-and-truffle-5-x-x-3054ec5328ad">previous article</a>.</p><h3>OpenZeppelin SDK installation</h3><p>To install OpenZeppelin SDK globally we run a command:</p><pre>npm install -g @openzeppelin/cli</pre><p>To check if you have already installed OpenZeppelin SDK or to make sure that the installation process was successful you can verify the version of your software:</p><pre>oz --version</pre><p>In all OpenZeppelin SDK command I use oz which is shorthand from openzeppelin.</p><p>I am currently using OpenZeppelin SDK v2.5.1.</p><h3>Project setup</h3><p>We have to create a new directory for our project and then navigate to it:</p><pre>mkdir hello-oz<br>cd hello-oz</pre><h3>Project initiation</h3><p>In the beginning, we need to create the `package.json` file:</p><pre>npm init -y</pre><p>We run this command with -y flag to generate package.json without having it ask any questions - it will take default values. Of course, if you want to personalize it, you can just use npm init and answer given questions.</p><p>Next, we can finally init our first OpenZeppelin SDK project:</p><pre>oz init</pre><p>We will be asked for project name and version — we can use <em>hello-oz </em>name, pick version 1.0.0 and accept by pressing enter.</p><p>A command oz init creates file project.json in.openzepplin directory. It stores the general configuration of our project (manifest version, name of the initialized project, its version and information about our contracts that our project contains).</p><h3>Creating sample smart contract</h3><p>The problem with smart contracts immutability may especially arise when we find a bug in our code. Without upgradeability, we can do nothing with such a smart contract and it can be a great target of hacker attacks. For our example, we will create the smart contract vulnerable to underflow/overflow attack (you can read more about it <a href="https://consensys.github.io/smart-contract-best-practices/known_attacks/#integer-overflow-and-underflow">here</a>)</p><p>In the project.jsonfile, we can see that object storing our contracts is empty, so it is time to create our smart contract. First we add new file in <em>contracts </em>directory. We can call it HelloOz.sol. Our smart contract will contains:</p><ul><li>string state variable <em>name </em>(just for testing)</li><li>uint256 state variable <em>inc</em></li><li>uint256 state variable <em>dec</em></li><li>function <em>decrement</em>(that subtracts 1 from the choosen number)</li><li>function <em>increment</em>(that adds 1 to the choosen number)</li></ul><pre>pragma solidity ^0.5.0;</pre><pre>contract HelloOz {<br>  string public name;<br>  uint256 public dec;<br>  uint256 public inc;</pre><pre>  function decrement(uint256 x) public returns (uint256) {<br>      return dec = x - 1;<br>  }</pre><pre>  function increment(uint256 x) public returns (uint256) {<br>      return inc = x + 1;<br>  }<br>}</pre><p>In the upgradeable smart contract we use <em>init </em>function instead of <em>constructor</em>. Why? The <em>constructor </em>is executed when the smart contract instance is deployed. In the proxy-based upgradeability system, it would never happen. That is why we use <em>initializer</em>. To do so, at the beginning of HelloOz.sol, just after pragma, we have to import contract Initializable.solfrom OpenZeppelin Upgrades:</p><pre>import &quot;@openzeppelin/upgrades/contracts/Initializable.sol&quot;</pre><p>Of course, to use OpenZeppelin upgrades (former ZeppelinOS library zos-lib) we have to install it in our project directory, so we have to run a command:</p><pre>npm install <a href="http://twitter.com/openzeppelin/upgrades">@openzeppelin/upgrades</a> --save</pre><p>Now we can define our smart contract as <em>Initializable </em>and add <em>init </em>function.</p><p>The whole contract code is:</p><pre>pragma solidity ^0.5.0;<br>import &quot;@openzeppelin/upgrades/contracts/Initializable.sol&quot;;</pre><pre>contract HelloOz is Initializable {<br>    string public name;<br>    uint256 public dec;<br>    uint256 public inc;</pre><pre>    function initialize(string memory _name) initializer public {<br>        name = _name;<br>    }</pre><pre>    function decrement(uint256 x) public returns (uint256) {<br>        return dec = x - 1;<br>    }</pre><pre>    function increment(uint256 x) public returns (uint256) {<br>        return inc = x + 1;<br>    }<br>}</pre><p>We can check if our smart contract compiles properly using</p><pre>oz compile</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*X915vKGvuurVc8VT3EPAEA.png" /></figure><p>It is time for the best fun — let’s deploy our smart contract to the network, test it and fix the bug!</p><h3>Creating an upgradeable instance of smart contract</h3><p>We have to run our development network (Ganache). Now we can deploy upgradeable instance of our smart contract by:</p><pre>oz create</pre><p>After running this command we have to pick our contract name and network. The answer for question <em>Do you want to call a function on the instance after creating it? </em>is y and we can choose initialize function and enter the parameter. The result is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hhgNNlBpusSymtuudegxFw.png" /></figure><p>The “address” of your HelloOz proxy is the white address that we can see in the console and we will interact with it later.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/913/1*gC-v3vRm3hY6HDKoVPy1lg.png" /></figure><p>We can also find our smart contract in the bottom part of dev-&lt;number&gt;.json in .openzepplin directory</p><pre>&quot;proxies&quot;: {<br>    &quot;hello-oz/HelloOz&quot;: [<br>      {<br>        &quot;address&quot;: &quot;0x1161e67eFf76cb6F7Fb18d52F674D3e9C59dc950&quot;,<br>        &quot;version&quot;: &quot;1.0.0&quot;,<br>        &quot;implementation&quot;: &quot;0x64786cCD5C1C2572CEe97B09ed1F12fa0857a561&quot;,<br>        &quot;admin&quot;: &quot;0x1aA7817c55ACee80426e10eAB66320F686539880&quot;,<br>        &quot;kind&quot;: &quot;Upgradeable&quot;<br>      }<br>    ]<br>  }</pre><p>The “implementation” is the address of current smart contract version and it will change when we upgrade our smart contract.</p><h3>Interacting with our smart contract</h3><p>Now we can test how our smart contract works. To communicate with it we will use the openzeppelin send-tx and openzeppelin call. After running these commands we have to pick the network, instance, function and (when needed).</p><p>In the beginning, we can check values of our state variables:</p><pre>oz call</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XwFsX1LFjhS3n2w5ZJUdLw.png" /></figure><p>We can see that <em>name</em> was properly initialized and <em>dec and </em>ind returns 0.</p><p>Now we can check our <em>decrement</em> function with a simple number (for example with 5) by running :</p><pre>oz send-tx</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CqQ_dnmg0sjQmtqsQIDNkA.png" /></figure><p>We can do the same with <em>increment</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_c2QyJmcERko-4L47UWXhA.png" /></figure><p>On the face of it, our results look fine.</p><p>Now it is time to <em>decrement</em> function with 0:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HcRg1vBx0d6XTenRKP6zVw.png" /></figure><p>and <em>increment</em> with 115792089237316195423570985008687907853269984665640564039457584007913129639935 (which is maximum uint256 you can pass in Solidity — 2^256–1) :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iE_aj1jWpGD6ntH4-4APOQ.png" /></figure><p>The results are incorrect!</p><p><strong>Now we know that our smart contract has a bug and is vulnerable for attacks. So what to do? Don’t worry, we use ZeppelinOS so we can upgrade our code in few simple steps.</strong></p><h3>Upgrading contract</h3><p>How can we fix it? We should use SafeMath library in our smart contract for all arithmetic operations.</p><p>We will use OpenZeppelin contract:</p><pre>import &quot;@openzeppelin/contracts-ethereum-package/contracts/math/SafeMath.sol&quot;;</pre><p>To use it, we have to link the EVM package to our project:</p><pre>oz link @openzeppelin/contracts-ethereum-package</pre><p>Please notice that we import openzeppelin/contracts-ethereum-packagenot openzeppelin-solidityas in usual. What is the difference? contracts-ethereum-packageis the library of EVM packages, that have been already deployed to the blockchain ( you can read more about differences between contracts-ethereum-packageand openzeppelin-solidityin <a href="https://blog.openzeppelin.com/open-source-collaboration-in-the-blockchain-era-evm-packages/">this article</a>).</p><p>Our project.jsonfile is updated and contains a new object with dependencies:</p><pre>&quot;dependencies&quot;: {<br>  &quot;@openzeppelin/contracts-ethereum-package&quot;: &quot;^2.2.0&quot;<br>},</pre><p>Now we have to update the code of our HelloOz smart contract. We need to change the add and subtraction characters to the SafeMath functions.</p><pre>function decrement(uint256 x) public returns (uint256) {     <br>  return dec = x.sub(1);<br>}<br>function increment(uint256 x) public returns (uint256) {<br>  return inc = x.add(1);   <br>}</pre><p>We also have to add the statement below just under the state variables:</p><pre>using SafeMath for uint256;</pre><p>The code of our whole updated HelloOz<em>:</em></p><pre>pragma solidity ^0.5.0;<br>import &quot;@openzeppelin/upgrades/contracts/Initializable.sol&quot;;<br>import &quot;@openzeppelin/contracts-ethereum-package/contracts/math/SafeMath.sol&quot;;</pre><pre>contract HelloOz is Initializable {<br>    string public name;<br>    uint256 public dec;<br>    uint256 public inc;</pre><pre>    using SafeMath for uint256;</pre><pre>    function initialize(string memory _name) initializer public {<br>        name = _name;<br>    }</pre><pre>function decrement(uint256 x) public returns (uint256) {<br>        return dec = x.sub(1);<br>    }</pre><pre>function increment(uint256 x) public returns (uint256) {<br>        return dec = x.add(1);<br>    }<br>}</pre><p><strong>Important!</strong></p><p>When upgrading smart contract we <strong>cannot</strong>:</p><p>-change the type of existing variables,</p><p>-change the order in which variables are declared,</p><p>-remove the existing variable,</p><p>-introduce a new variable before the existing one.</p><p>You can read more about upgrades pattern at <a href="https://docs.openzeppelin.com/sdk/2.5/pattern">ZeppelinOS Documentation</a> .</p><p>We can update our project by using the command:</p><pre>oz upgrade</pre><p>Now we can test our smart contract again with openzeppelin send-tx (<em>decrement </em>with 0 and increment with 115792089237316195423570985008687907853269984665640564039457584007913129639935)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ix4uFbh3HeCE4z5dAW7okQ.png" /></figure><p>Both transactions have reverted!</p><p><strong>Thanks to OpenZeppelin SDK we have updated the code of our smart contract and we are protected from overflow/underflow attacks!</strong></p><p><strong>If you have any question, feel free to leave a comment.</strong></p><blockquote><a href="https://coincodecap.com/?utm_source=coinmonks">Get Best Software Deals Directly In Your Inbox</a></blockquote><figure><a href="https://coincodecap.com/?utm_source=coinmonks"><img alt="" src="https://cdn-images-1.medium.com/max/584/0*OJ-qb5G6i863msBB.png" /></a></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=260dfbfd5bae" width="1" height="1" alt=""><hr><p><a href="https://medium.com/coinmonks/how-to-create-an-upgradeable-smart-contract-using-openzeppelin-sdk-example-of-fixing-smart-260dfbfd5bae">How to create an upgradeable smart contract using OpenZeppelin SDK — example of fixing smart…</a> was originally published in <a href="https://medium.com/coinmonks">Coinmonks</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to compile and deploy Move transaction script on Libra local network?]]></title>
            <link>https://medium.com/featly/how-to-compile-and-deploy-move-transaction-script-on-libra-local-network-4b03e0ff5cbd?source=rss-5426a9f14a62------2</link>
            <guid isPermaLink="false">https://medium.com/p/4b03e0ff5cbd</guid>
            <category><![CDATA[language-moves]]></category>
            <category><![CDATA[libra-blockchain]]></category>
            <category><![CDATA[facebook-libra]]></category>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Paulina Błaszkiewicz]]></dc:creator>
            <pubDate>Tue, 30 Jul 2019 13:01:10 GMT</pubDate>
            <atom:updated>2019-07-30T13:02:09.396Z</atom:updated>
            <content:encoded><![CDATA[<p>In my <a href="https://medium.com/featly/move-language-for-libra-blockchain-how-to-run-tests-and-compile-scripts-modules-a8b466ee5621?source=friends_link&amp;sk=a013d812b6493d0c6b3bc53126ce53ed">previous article</a>, I’ve shown how to test and compile Move scripts or modules. But many people were asking<strong> how to deploy them to the network?</strong> On the Libra Forum Runtian from Calibra Team has described <a href="https://community.libra.org/t/compile-and-deploy-smart-contract-in-local-testnet/986/10?u=paulina">how to compile and deploy peer to peer transfer script on local testnet</a>. Using his answer, I have prepared detailed instruction how to step by step compile and deploy Move transaction script to the local Libra network.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NJOjEommvMTHxdJERZgwFQ.jpeg" /></figure><h3>1. Preparing environment</h3><p>At the beginning, we have to run our Libra blockchain. First, clone Libra from GitHub:</p><pre>git clone <a href="https://github.com/libra/libra.git">https://github.com/libra/libra.git</a> &amp;&amp; cd libra</pre><p>Checkout testnet branch:</p><pre>git checkout testnet</pre><p>Install dependencies:</p><pre>./scripts/dev_setup.sh</pre><p>Run local network of Libra blockchain:</p><pre>cargo run -p libra_swarm -- -s</pre><p>Now we can create two accounts to make peer to peer transfer possible — we will use following commands in libra%CLI</p><pre>// create first account<br>account create <br>//or<br>a c</pre><pre>// create second account<br>account create<br>//or<br>a c</pre><pre>//mint libra coins to first account (with &quot;0&quot; index)<br>account mint 0 1000<br>//or<br>a m 0 1000</pre><pre>​<br>//check account balance of first account (with &quot;0&quot; index)<br>query balance 0<br>//or<br>q b 0</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/728/1*WtAUWxPkVLjfLQIj5O6VRw.png" /></figure><p>Remember to change accounts numbers in the next steps!</p><h3>2. Preparing .mvir file with transaction script</h3><p>In our example we will use peer_to_peer_transfer.mvir script from <a href="https://github.com/libra/libra?source=post_page---------------------------">Libra GitHub page</a> - yo can find it in libra/language/stdlib/modules/transaction_scripts <a href="https://github.com/libra/libra/blob/master/language/stdlib/transaction_scripts/peer_to_peer_transfer.mvir">directory</a>.</p><pre>import 0x0.LibraAccount;<br>main (payee: address, amount: u64) {<br>  LibraAccount.pay_from_sender(move(payee), move(amount));<br>  return;<br>}</pre><p>Now we can open new terminal window and create test directory, where we copy our file (peer_to_peer_transfer.mvir)</p><pre>cd libra<br>mkdir test<br>cp ./language/stdlib/transaction_scripts/peer_to_peer_transfer.mvir ./test</pre><h3>3. Compiling transaction script</h3><p>We will use <a href="https://github.com/libra/libra/tree/master/language/compiler?source=post_page---------------------------">Move IR compiler</a> to compile our transaction script.</p><blockquote>The Move IR compiler compiles modules and scripts written in Move down to their respective bytecode representations</blockquote><p>First, we have to build compiler binary:</p><pre>cargo build --bin compiler</pre><p>You can see more info about IR compiler commands by running:</p><pre>./target/debug/compiler -h</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/670/1*ZEON6CqMB6RCX0l02yGf0A.png" /></figure><p>You can also go to ./target/debug/dierctory and run ./compilerinstead of ./target/debug/compilerfrom main directory (only remember to change files paths).</p><p>Now we can compile our transaction script and write it to file in our test directory by using the following command</p><pre>compiler -o &lt;output_path&gt; &lt;source_path&gt;</pre><p>In our case we run:</p><pre>./target/debug/compiler -o ./test/out.program ./test/peer_to_peer_transfer.mvir</pre><h3>4. Building transaction</h3><p>We will use transaction_builder to build our transaction. You can find in libra/language/transaction_builder<a href="https://github.com/libra/libra/tree/master/language/transaction_builder"> directory</a></p><p>In the beginning we will build transaction builder binary:</p><pre>cd language/transaction_builder/<br>cargo build --features=’build-binary’<br>cd ../..</pre><p>Next we can build transaction as:</p><pre>transaction_builder &lt;sender&gt; &lt;sequence_number&gt; &lt;program&gt; &lt;output&gt; --args &lt;payee&gt; &lt;amount&gt;</pre><p>So in our case command can look like following:</p><p><em>&lt;sender&gt;</em> — our first account (#0)</p><p><em>&lt;sequence_number&gt;</em> — sequence number of sender’s account. Our account was newly created so it will be 0. If you want to check your sequence number use in Libra CLI account la</p><p><em>&lt;program&gt;</em> — our out.program file</p><p><em>&lt;output&gt;</em> — newly created out.txn file</p><p><em>&lt;payee&gt;</em> — account number we want to transfer coins to, in our case it is second account. <strong>Important!</strong> remember to add 0x at the beginning</p><p><em>&lt;amount&gt;</em> — value of transaction in the smallest Libra unit (1 000 000 equals 1 Libra)</p><p>So in our case command can look like following:</p><pre>./target/debug/transaction_builder 609abf4e33bd8bbd72e02221b9d01bdbb84aa50cc472bb002c31cde4dcd3d593 0 ./test/out.program out.txn —-args 0x3783851e714802c5d2294a602ae4507be1b22b6b4be1c0d71e9f858e4038db24 1000000</pre><p>Of course you can also go to ./target/debug/dierctory and run ./transaction_builder instead of ./target/debug/transaction_builder from main directory (only remember to change files paths).</p><h3>5. Sending transaction</h3><p>In the end we can send transaction via client CLI, so we have to move to our terminal window with running %libra. We will load a RawTransaction from file and submit to the network, by:</p><pre>submitb &lt;sender&gt; &lt;path_to_raw_transaction&gt;</pre><p>In our case:</p><pre>submitb 609abf4e33bd8bbd72e02221b9d01bdbb84aa50cc472bb002c31cde4dcd3d593 out.txn</pre><p><strong>Important!</strong> If you didn’t change your branch from master to testnet you have to use command with <strong>dev</strong> in the beginning:</p><pre>dev submitb 609abf4e33bd8bbd72e02221b9d01bdbb84aa50cc472bb002c31cde4dcd3d593 out.txn</pre><p>Now we can check balances of our accounts:</p><pre>query balance 0<br>query balance 1</pre><p>If transfer succeeds balances should look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/382/1*-dbpgBhKrtJAfvppcYVNsw.png" /></figure><p><strong>And that’s all! In simmilar way you can compile and deploy more advanced transaction scripts. If you have any question, feel free to leave a comment.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4b03e0ff5cbd" width="1" height="1" alt=""><hr><p><a href="https://medium.com/featly/how-to-compile-and-deploy-move-transaction-script-on-libra-local-network-4b03e0ff5cbd">How to compile and deploy Move transaction script on Libra local network?</a> was originally published in <a href="https://medium.com/featly">Featly</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Overview of Move — a new language for Libra Blockchain]]></title>
            <link>https://medium.com/featly/overview-of-move-a-new-language-for-libra-blockchain-39fe99489fbc?source=rss-5426a9f14a62------2</link>
            <guid isPermaLink="false">https://medium.com/p/39fe99489fbc</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[language-moves]]></category>
            <category><![CDATA[facebook-libra]]></category>
            <category><![CDATA[libra-blockchain]]></category>
            <category><![CDATA[blockchain]]></category>
            <dc:creator><![CDATA[Paulina Błaszkiewicz]]></dc:creator>
            <pubDate>Thu, 25 Jul 2019 09:15:46 GMT</pubDate>
            <atom:updated>2019-07-25T09:15:46.046Z</atom:updated>
            <content:encoded><![CDATA[<h3>Overview of Move — a new language for Libra Blockchain</h3><p>Libra is permissioned blockchain proposed by Facebook. For transactions and smart contracts on this platform was created a special new language — Move. At the moment there is available Move intermediate representation (IR). In this article, you can read a short overview of key Move goals and features (modules, <em>transactions scripts</em>, <em>resource types</em>). It is based on the <a href="https://developers.libra.org/docs/move-overview">Libra documentation</a> and <a href="https://developers.libra.org/docs/assets/papers/libra-move-a-language-with-programmable-resources.pdf">Move Technical Paper</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MFYeHBnz73buW4OulZkxQQ.jpeg" /></figure><h3>1. Key Design goals of Move language</h3><h3>1.1. First-Class Resources</h3><p>In other smart contract languages (for example in Solidity for Ethereum Blockchain) resources are defined similarly as any other values. To ensure maximum safety, Move implements custom <em>resource types</em> that can never be copied, reused or discarded. So what we can do with <em>resource</em> is moving it between program storage locations.</p><h3>1.2 Flexibility</h3><p>Flexibility is added to Libra Blockchain via Move <em>transaction scripts</em> and modules. Move <em>transaction script</em> is the part of every Libra transaction and it allows customizable transactions. Modules, in turn, provide flexible code composition.</p><h3>1.3 Safety</h3><p>Move ensures security by using an intermediate solution between two approaches:</p><p>-using a high-level programming language with a compiler checking key properties</p><p>-using a low-level untyped assembly checking key properties at runtime.</p><p>In Move bytecode verifier checks on-chain the bytecode for resource, type and memory safety and then bytecode interpreter executes it directly. Thanks to this, Move ensures safety just like source languages, <em>but without adding the source compiler to the trusted computing base or the cost of compilation to the critical path for transaction execution.</em></p><h3>1.4 Verifiability</h3><p>Move supports lightweight on-chain verification of key safety properties and advanced off-chain static verification. Language properties which support static verification are: no dynamic dispatch, limited mutability and modularity.</p><h3>2. Main structures in Move language</h3><h3>2.1 Move Modules</h3><p>Move modules are the equivalent of smart contracts in other blockchain languages.</p><p>In Move module we can declare:</p><ul><li>structs of <em>resource types</em> :</li></ul><pre>module LibraCoin {<br>   <br>  resource T {<br>      value: u64,<br>  }<br>​<br>  resource MintCapability {}<br>​<br>  resource MarketCap {<br>      total_value: u64,<br>  }<br>   <br>  ...<br>}</pre><ul><li>“normal” structs:</li></ul><pre>module LibraAccount {<br>​<br>...<br>​<br>  struct SentPaymentEvent {<br>    payee: address,<br>    amount: u64,<br>  }<br> <br> ...</pre><pre>}</pre><ul><li>procedures that <em>encode the rules for creating, destroying, and updating its</em> <em>declared resources</em></li></ul><pre>module LibraCoin {<br>​<br>...<br>​<br> public market_cap(): u64 {<br>   let market_cap_ref: &amp;mut R#Self.MarketCap;<br>   market_cap_ref = borrow_global&lt;MarketCap&gt;(0x0);<br>   return *&amp;move(market_cap_ref).total_value;<br> }</pre><pre>...</pre><pre>}</pre><p>Reading data from the module and calling its procedures is possible through move transactions scripts.</p><h3>2.2 Move Scripts</h3><p>Move <em>transaction scripts</em> are used for updating the Libra Blockchain global storage. They can read the data from the modules and call its procedures. Typical Move script consists of:</p><pre>script:<br>  main() {<br>    return;<br>}</pre><p>In the beginning, before “script”, we often import two basic elements: LibraAccountand LibraCoin. LibraAccountis almost always needed and the use of LibraCoindepends on the type of transaction we want to create.</p><pre>import 0x0.LibraAccount;<br>import 0x0.LibraCoin;</pre><p>At first, in the procedure, we have to declare our local variables. In Move IR declaration i initialization are separated.</p><p>The following example comes from the <a href="http://The following example comes from the documentation">Libra documentation</a>. At first, we import our account and coin modules. Then we pass two parameters in `main` function and declare local variables. #R annotation in R#LibraCoin.T is &quot;Resource&quot; and it means that in the LibraCoin module we have the declaration of resource T (instead of struct T that would have annotation #V - for &quot;unrestricted Value&quot;).</p><p>Next we initialize our local variables using LibraAccount procedures — withdraw_from_sender and exists. Inside <em>if </em>statement we use Move built in function create_account.</p><p>Every time we use a variable we have to wrap it in with<em>copy </em>(leaving variable available for continued use) or<em> </em><em>move </em>(rendering variable unavailable)<em>. Resource types</em> variables can be only moved.<em> Unrestriced type </em>variables can be moved and copied.</p><pre>import 0x0.LibraAccount;<br>import 0x0.LibraCoin;<br>main(payee: address, amount: u64) {<br>  let coin: R#LibraCoin.T;<br>  let account_exists: bool;</pre><pre>  coin = LibraAccount.withdraw_from_sender(move(amount));</pre><pre>  account_exists = LibraAccount.exists(copy(payee));</pre><pre>  if (!move(account_exists)) {<br>    create_account(copy(payee));<br>  }</pre><pre>  LibraAccount.deposit(move(payee), move(coin));<br>  return;<br>}</pre><p>In the end of every procedure there must be a return statement.</p><h3>3. Libra Account and Libra Coin modules overview</h3><h3>3.1 Libra Account Module</h3><p>Every nonempty address has a LibraAccount.Tresource. We can interact with an account in two ways:</p><p>-read the data from the LibraAccount resource (LibraAccount.T)</p><p>-call the procedures of the LibraAccount module.</p><pre>module LibraAccount {<br>  import 0x0.LibraCoin;<br>  import 0x00.Hash;<br>​<br>  resource T {<br>      balance: R#LibraCoin.T,<br>       <br>      authentication_key: bytearray,<br>   <br>      sequence_number: u64,<br>   <br>      sent_events_count: u64,<br>   <br>      received_events_count: u64<br>  }<br>   <br>  ...<br>}</pre><p>As we can see every LibraAccounthas LibraAccount.Tresource which stores the following values:</p><p>-Libra Coin balance for the account</p><p>-authentication key</p><p>-sequence number (transactions counter)</p><p>-sent and received events count</p><p>The full code of LibraAccount module you can find on <a href="https://github.com/libra/libra">Libra GitHub page</a> in libra/language/stdlib/modules/ <a href="https://github.com/libra/libra/tree/master/language/stdlib/modules">directory</a>.</p><h3>3.2 Libra Coin Module</h3><p>Libra Coin is implemented as “normal” <em>resource type </em>LibraCoin.T</p><ul><li>LibraCoinis the name of the module</li><li>Tmeans the main type declared in this module (this is a Move naming convention)</li></ul><pre>module LibraCoin {<br>   <br>  resource T {<br>      value: u64,<br>  }</pre><pre>...<br>}</pre><p>The full code of LibraCoin module you can find on <a href="https://github.com/libra/libra">Libra GitHub page</a> in libra/language/stdlib/modules/ <a href="https://github.com/libra/libra/tree/master/language/stdlib/modules">directory</a>.</p><p>You can check how to test/compile Move scripts and modules in our <a href="https://medium.com/featly/move-language-for-libra-blockchain-how-to-run-tests-and-compile-scripts-modules-a8b466ee5621">previous article</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=39fe99489fbc" width="1" height="1" alt=""><hr><p><a href="https://medium.com/featly/overview-of-move-a-new-language-for-libra-blockchain-39fe99489fbc">Overview of Move — a new language for Libra Blockchain</a> was originally published in <a href="https://medium.com/featly">Featly</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Move language for Libra blockchain — how to run tests and compile scripts / modules ?]]></title>
            <link>https://medium.com/featly/move-language-for-libra-blockchain-how-to-run-tests-and-compile-scripts-modules-a8b466ee5621?source=rss-5426a9f14a62------2</link>
            <guid isPermaLink="false">https://medium.com/p/a8b466ee5621</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[facebook-libra]]></category>
            <category><![CDATA[libra-blockchain]]></category>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[language-moves]]></category>
            <dc:creator><![CDATA[Paulina Błaszkiewicz]]></dc:creator>
            <pubDate>Thu, 11 Jul 2019 13:03:24 GMT</pubDate>
            <atom:updated>2019-07-22T09:27:15.310Z</atom:updated>
            <content:encoded><![CDATA[<h3>Move language for Libra blockchain — how to run tests and compile scripts / modules ?</h3><p>Move is a new programming language for the Libra Blockchain. It enables implementation of smart contracts and custom transaction logic. Four key features of Move language are: first-class assets, flexibility, safety, and verifiability.</p><p>Now there is available Move intermediate representation (IR). It is very early and unstable version of the language and in the future it will be replaced with a higher-level Move language. But we can use Move IR to play with scripts and modules for Libra Blockchain. At the moment, as we can read in Libra documentation (<a href="https://developers.libra.org/docs/move-overview">https://developers.libra.org/docs/move-overview</a> ), there is no possibility to deploy Move scripts or modules to the testnet:</p><blockquote>Custom Move programs are not supported in the initial testnet release, but these features are available for you to try out locally</blockquote><p>But what can we do now is testing or compiling Move modules / scripts.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SGsBl1ukYbMENNqhC_AqkQ.jpeg" /></figure><h3>1. Preparing environment</h3><p>At the beginning we have to clone and setup Libra Core</p><pre>git clone <a href="https://github.com/libra/libra.git">https://github.com/libra/libra.git</a><br>​<br>cd libra<br>./scripts/dev_setup.sh</pre><h3>2. Testing Move modules</h3><h3>2.1 Preparing .mvir file for testing</h3><p><em>Due to a Medium issue — the github code is not displayed in Safari. Use a different browser to view all content</em></p><p>We will use example module from <a href="https://developers.libra.org/docs/move-overview#writing-modules">Libra docs</a>.</p><p>First we have to go to the test directory</p><p>libra/language/functional_tests/tests/testsuite/modules</p><p>and create a file move_test_module.mvir</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f3fd93ce9f2828f8ce497bd8e44f78c4/href">https://medium.com/media/f3fd93ce9f2828f8ce497bd8e44f78c4/href</a></iframe><p>At the end of the file, after the module, we have added empty script to make our file to compile properly:</p><pre>script:<br>  main() {<br>      return;<br>}</pre><p>In the end you can delete this part of the file and check what will happened.</p><p>Explanation of how to create modules and scripts is beyond the scope of this article. You can find many examples in libra/language/functional_tests/tests/testsuite/modules directory or in Libra docs:</p><ul><li><a href="https://developers.libra.org/docs/move-overview#writing-transaction-scripts">writing scripts</a></li><li><a href="https://developers.libra.org/docs/move-overview#writing-modules">writing modules</a></li></ul><h3>2.2 Running tests</h3><p>To start testing our file we need to run a command</p><pre>cargo test -p functional_tests &lt;file_name&gt;</pre><p>So in our case it will be</p><pre>cargo test -p functional_tests move_test_module.mvir</pre><p>As a result we get:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/728/1*psDMrdYyarlNKaNP3AwS-w.png" /></figure><h3>3. Compiling Move scripts and modules using Move IR Compiler</h3><h3>3.1 Move IR Compiler</h3><p>Instead of test you can use <a href="https://github.com/libra/libra/tree/master/language/compiler">Move IR compiler</a> directly:</p><blockquote>The Move IR compiler compiles modules and scripts written in Move down to their respective bytecode representations</blockquote><h3>3.2 Preparing .mvir files</h3><p>We will use our move_test_module.mvir file and we will also create another one with simple script from <a href="https://developers.libra.org/docs/move-overview#writing-transaction-scripts">Libra docs</a> - move_test_script.mvir</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f4e550de088f13e112c34fad31bbcc9f/href">https://medium.com/media/f4e550de088f13e112c34fad31bbcc9f/href</a></iframe><p>Now we can create for exampletest directory in main dir and copy both files there.</p><h3>3.3 Compiling scripts and modules</h3><p>To compile our files we need to run a command:</p><pre>cargo run -p compiler &lt;file_path&gt;</pre><p>So now we have to run it for our both files:</p><pre>cargo run -p compiler ./test/move_test_module.mvir<br>cargo run -p compiler ./test/move_test_script.mvir</pre><p>If the compilation was successful, as a result we will see our script/ module compiled to the Move bytecode:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/804/1*q14IrZxZEEl6dg9e0rf22g.png" /></figure><p>This article shows in a few simple steps how to test and compile Move modules /scripts. Now we are waiting for possibility to deploy them to the testnet!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a8b466ee5621" width="1" height="1" alt=""><hr><p><a href="https://medium.com/featly/move-language-for-libra-blockchain-how-to-run-tests-and-compile-scripts-modules-a8b466ee5621">Move language for Libra blockchain — how to run tests and compile scripts / modules ?</a> was originally published in <a href="https://medium.com/featly">Featly</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to create an upgradeable smart contract using ZeppelinOS — example of fixing smart contract…]]></title>
            <link>https://medium.com/coinmonks/how-to-create-an-upgradeable-smart-contract-using-zeppelinos-example-of-fixing-smart-contract-6f6612ed8df4?source=rss-5426a9f14a62------2</link>
            <guid isPermaLink="false">https://medium.com/p/6f6612ed8df4</guid>
            <category><![CDATA[openzeppelin]]></category>
            <category><![CDATA[solidity]]></category>
            <category><![CDATA[smart-contracts]]></category>
            <category><![CDATA[zeppelinos]]></category>
            <category><![CDATA[ethereum]]></category>
            <dc:creator><![CDATA[Paulina Błaszkiewicz]]></dc:creator>
            <pubDate>Tue, 23 Apr 2019 05:04:29 GMT</pubDate>
            <atom:updated>2020-08-26T13:35:51.211Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to create an upgradeable smart contract using ZeppelinOS — example of fixing smart contract vulnerable to underflow/overflow attacks</h3><p>In this tutorial we will use ZeppelinOS to fix smart contract vulnerable to underflow/overflow attacks. Firstly we will prepare our environment and create the upgradeable smart contract with some bug and test it. Then we will update the code, upgrade the smart contract and finally, we will check if it is safe.</p><p><a href="https://medium.com/@paulina.blaszk/how-to-create-an-upgradeable-smart-contract-using-openzeppelin-sdk-example-of-fixing-smart-260dfbfd5bae?sk=9d4785ea4f3db9d278df99eb1edc28f8">You can go to updated version of this article — with OpenZeppelin SDK v.2.5.2</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sUif8A5X81bOpkLssOTbcg.jpeg" /></figure><h3>Upgradeability in smart contracts</h3><p>In principle smart contracts are immutable. Once deployed to the network, they cannot be changed. Unfortunately in smart contracts, like in every software, mistakes frequently happens. Sometimes they may cost millions of dollars… But don’t worry! Now we have ZeppelinOS — powerful tool to upgrade our smart contracts and fix mistakes. <em>ZeppelinOS is a platform to develop, deploy and operate smart contract projects on Ethereum and every other EVM and eWASM-powered blockchain</em> as its <a href="https://docs.zeppelinos.org/docs/start.html">documentation</a> says. And one of its most important features is smart contracts upgradeability.</p><h3>Environment preparation</h3><p>You need to have installed essential software for creating and upgrading the smart contracts. You can find the instructions for installing Node.js and NPM, Truffle, Ganache in my <a href="https://medium.com/@paulina.blaszk/how-to-write-and-deploy-first-smart-contract-in-solidity-0-5-x-and-truffle-5-x-x-3054ec5328ad">previous article</a>.</p><h3>ZeppelinOS installation</h3><p>To install ZeppelinOS globally we run a command:</p><pre>npm install -g zos</pre><p>To check if you have already installed ZeppelinOs or to make sure that the installation process was successful you can verify the version of your software:</p><pre>zos --version</pre><p>I am currently using zos v2.2.3</p><h3>Project setup</h3><p>We have to create a new directory for our project and then navigate to it:</p><pre>mkdir hello-zos<br>cd hello-zos</pre><h3>Project initiation</h3><p>In the beginning, we need to create the package.json file:</p><pre>npm init -y</pre><p>We run this command with -y flag to generate package.json without having it ask any questions - it will take default values. Of course, if you want to personalize it, you can just use npm init and answer given questions.</p><p>Next, we can finally init our first ZeppelinOs project:</p><pre>npx zos init hello-zos</pre><p>If it asks us for version — we can accept 1.0.0 by pressing enter. A command zos init creates file zos.json in our directory. It stores the general configuration of our zos project (ZeppelinOS version, name of the initialized project, its version and information about our contracts that our project contains).</p><p>What is more, zos initalso initializes Truffle. This means that the standard structure of truffle project has been created in our hello-zosdirectory. Now there are two new directories (<em>contracts </em>and <em>migrations</em>) and <em>truffle-config.js </em>file. Remember to check if <em>truffle-config.js </em>is compatible with your Ganache configuration.</p><h3>Creating sample smart contract</h3><p>The problem with smart contracts immutability may especially arise when we find a bug in our code. Without upgradeability, we can do nothing with such a smart contract and it can be a great target of hacker attacks. For our example, we will create the smart contract vulnerable to underflow/overflow attack (you can read more about it <a href="https://consensys.github.io/smart-contract-best-practices/known_attacks/#integer-overflow-and-underflow">here</a>)</p><p>In the zos.json file, we can see that object storing our contracts is empty, so it is time to create our smart contract. First we add new file in <em>contracts</em> directory. We can call it HelloZos.sol. Our smart contract will contains:</p><ul><li>string state variable <em>name</em></li><li>uint256 state variable <em>MaxNumber</em></li><li>uint256 state variable <em>inc</em></li><li>uint256 state variable <em>dec</em></li><li>function <em>decrement</em> (that subtracts 1 from the choosen number)</li><li>function <em>increment</em> (that adds 1 to the choosen number)</li></ul><pre>pragma solidity ^0.5.0;</pre><pre>contract HelloZos {</pre><pre>    string public name;<br>    uint256 public maxNumber;<br>    uint256 public dec;<br>    uint256 public inc;</pre><pre>    function decrement(uint256 x) public returns (uint256) {<br>        return dec = x - 1;<br>    }</pre><pre>    function increment(uint256 x) public returns (uint256) {<br>        return inc = x + 1;<br>    }</pre><pre>}</pre><p>In the upgradeable smart contract we use <em>init</em> function instead of <em>constructor</em>. Why? The <em>constructor</em> is executed when the smart contract instance is deployed. In the proxy-based upgradeability system, it would never happen. That is why we use <em>initializer</em>. To do so, at the beginning of HelloZos.sol, just after pragma, we have to import contract Initializable.sol from ZeppelinOS Library:</p><pre>import &quot;zos-lib/contracts/Initializable.sol&quot;</pre><p>Of course, to use ZeppelinOS library we have to install it in our project directory, so we have to run a command:</p><pre>npm install zos-lib --save</pre><p>Now we can define our smart contract as <em>Initializable </em>and add <em>init</em> function.</p><p><em>Due to a Medium issue — the github code is not displayed in Safari. Use a different browser to view all content</em></p><p>The whole contract code is:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a2e2c9d5c1d131195fdd11b7458a49f7/href">https://medium.com/media/a2e2c9d5c1d131195fdd11b7458a49f7/href</a></iframe><p>It is time for the best fun — let’s deploy our smart contract to the network, test it and fix the bug!</p><h3>Registering smart contract in zos project</h3><p>First, we have to register the smart contract in our ZeppelinOS project. To do this we run a command:</p><pre>npx zos add HelloZos</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*au1N6XZ0tLa1OStjtT35ZA.png" /></figure><p>If we have more than one smart contract, we can add all their names at once (separated by spaces) or use --all flag.</p><p>Now our smart contracts (both: <em>HelloZos</em> and <em>Initializable</em>) are compiled (their artifacts are written to build/contracts directory). And we can find <em>HelloZos</em> contract under <em>contracts</em> filed in our zos.json</p><pre>{<br>  “zosversion”: “2.2”,<br>  “name”: “hello-zos”,<br>  “version”: “1.0.0”,<br>  “contracts”: { <br>    “HelloZos”: “HelloZos” <br>  }<br>}</pre><h3>Deploying smart contract to the network</h3><p>Then we deploy our smart contract to a specified network. In our case it is <em>Ganache</em> local network (make sure that it is running) and use a command:</p><pre>npx zos session --network local --from 0x9ea26a91A3eF090498827989d17FC0E7eEA0987f --expires 3600</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dmKZbZwXSHS2B7y_bVXcmA.png" /></figure><p>Remember to<strong> replace the address</strong> after --from flag with one of your Ganache accounts, but do not use the first one (account[0]). I usually choose the second (accounts[1]) or the last one (accounts[9]). You should also use network specified in your <em>truffle-config.js</em> (in our case it is <em>local</em>).</p><p>After these preparations we can push <em>HelloZos </em>smart contract<em> </em>to our local network by running a command:</p><pre>npx zos push</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*du3WnuRGepP32NrW2JdKHA.png" /></figure><p>It creates file <em>zos.dev-&lt;your-network-number&gt;.json</em></p><h3>Creating an upgradeable instance of smart contract</h3><p>We have just deployed <em>HelloZos</em> contract to the network, but it only implements the logic. If we want to interact with our smart contract we have to create its upgradeable instance by:</p><pre>npx zos create HelloZos --init initialize --args &#39;Paulina&#39;</pre><p>After “create” we add the name of our smart contract, then we call initialize function and we pass the arguments after --args flag (if there is more than one argument they should be separated with commas, without spaces)</p><p>The result is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1oF4kFd6MLhRgg4ADrk3BQ.png" /></figure><p>We can also find our smart contract in the bottom part of zos.dev-&lt;number&gt;.json</p><pre>&quot;proxies&quot;: {<br>    &quot;hello-zos/HelloZos&quot;: [<br>      {<br>        &quot;address&quot;: &quot;0xfe3d58BdB14d44d1759c1Eb04c91Ac93567dA4Ba&quot;,     <br>        &quot;version&quot;: &quot;1.0.0&quot;,<br>        &quot;implementation&quot;: &quot;0xf1208D91287aCFBf7c1d7b9c886218a1761f4E20&quot;<br>      }<br>    ]<br>}</pre><p>The address of your HelloZos proxy is the white address that we can see in the console and we will interact with it later. The “implementation” is the address of current smart contract version and it will change when we upgrade our smart contract.</p><h3>Interacting with our smart contract</h3><p>Now we can test how our smart contract works. To communicate with it we will use the truffle console. We open it with the command:</p><pre>npx truffle console --network local</pre><p>In the beginning, we will save the instance of <em>HelloZos</em> contract in the myContract variable:</p><pre>let abi = require(&quot;./build/contracts/HelloZos.json&quot;).abi</pre><pre>let myContract = new web3.eth.Contract(abi, &quot;0xd6e0095002f3B287A468203B87D6408f172e1CbF&quot;)</pre><p>Remember to <strong>replace the address </strong>above with address of your <em>HelloZos</em> proxy!</p><p>Now we can check values of our state variables:</p><pre>myContract.methods.name().call()</pre><pre>let max = await myContract.methods.maxNumber().call()</pre><pre>max</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8VFuOR6YfV81pDDeR9N1Xw.png" /></figure><p>Now we can check our functions with a simple number:</p><pre>myContract.methods.decrement(5).call()</pre><pre>myContract.methods.increment(5).call()</pre><p>On the face of it, everything looks fine:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*okqDPHXN_CdvY-Gp11_5ww.png" /></figure><p>Then we call our function with 0 and max number and check the results:</p><pre>myContract.methods.decrement(0).call()</pre><pre>myContract.methods.increment(max).call()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TQ10Kkod2MUkPFYl1dm6DQ.png" /></figure><p><strong>Now we know that our smart contract has a bug and is vulnerable for attacks. So what to do? Don’t worry, we use ZeppelinOS so we can upgrade our code in few simple steps.</strong></p><h3>Upgrading contract</h3><p>How can we fix it? We should use SafeMath library in our smart contract for all arithmetic operations.</p><p>We will use OpenZeppelin contract:</p><pre>import &quot;openzeppelin-eth/contracts/math/SafeMath.sol&quot;;</pre><p>Please notice that we import openzeppelin-eth not openzeppelin-solidity as in usual. What is the difference? Openzeppelin-eth is the library of EVM packages, that have been already deployed to the blockchain ( you can read more about differences between openzeppelin-eth and openzeppelin-solidity in <a href="https://blog.zeppelin.solutions/getting-started-with-openzeppelin-eth-a-new-stable-and-upgradeable-evm-package-576fb37297d0">this article</a>).</p><p>To use it, we have to link the EVM package to our project:</p><pre>npx zos link openzeppelin-eth</pre><p>Our zos.json file is updated and contains a new object with dependencies:</p><pre>“dependencies”: {<br>    “openzeppelin-eth”: “2.1.3”<br>}</pre><p>Now we have to update the code of our <em>HelloZos</em> smart contract. We need to change the add and subtraction characters to the SafeMath functions.</p><pre>function decrement(uint256 x) public returns (uint256) {     <br>    return dec = x.sub(1);<br>}</pre><pre>function increment(uint256 x) public returns (uint256) {<br>    return inc = x.add(1);   <br>}</pre><p>We also have to add the statement below just under the state variables:</p><pre>using SafeMath for uint256</pre><p><em>Due to a Medium issue — the github code is not displayed in Safari. Use a different browser to view all content</em></p><p>The code of our whole updated <em>HelloZos:</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c89ea6e0c0b7980222b5db555e006497/href">https://medium.com/media/c89ea6e0c0b7980222b5db555e006497/href</a></iframe><p><strong>Important!</strong></p><p>When upgrading smart contract we <strong>cannot</strong>:</p><p>-change the type of existing variables,</p><p>-change the order in which variables are declared,</p><p>-remove the existing variable,</p><p>-introduce a new variable before the existing one.</p><p>You can read more about upgrades pattern at <a href="https://docs.zeppelinos.org/docs/pattern.html">ZeppelinOS Documentation </a>.</p><p>But let’s go back to our brand new <em>HelloZos</em>. Now we can run the command:</p><pre>npx zos push --deploy-dependencies</pre><p>We use <em>--deploy-dependencies</em> to deploy EVM package to our local network (we don’t have to do it when we use the mainnet, ropsten, rinkeby or kovan because those packages are already deployed).</p><p>Finally we can update our project by using the command:</p><pre>npx zos update HelloZos</pre><p>Now we can test our smart contract again in the truffle console.</p><pre>npx truffle console --network local</pre><pre>let abi = require(&quot;./build/contracts/HelloZos.json&quot;).abi</pre><pre>let myContract = new web3.eth.Contract(abi, &quot;0xd6e0095002f3B287A468203B87D6408f172e1CbF&quot;)</pre><p>Remember to <strong>replace the address </strong>above with address of your <em>HelloZos</em> proxy!</p><p>Now we can check values of our state variables:</p><pre>let max = await myContract.methods.maxNumber().call()</pre><pre>myContract.methods.decrement(0).call()</pre><pre>myContract.methods.increment(max).call()</pre><p>When we try to overflow/underflow we will see</p><pre>Error: Returned error: VM Exception while processing transaction: revert</pre><p><strong>Thanks to ZeppelinOS we have updated the code of our smart contract and we are protected from overflow/underflow attacks!</strong></p><h4>You can find the whole project on my <a href="https://github.com/paulinablaszk/hello-zos">GitHub</a>.</h4><blockquote><a href="https://coincodecap.com/?utm_source=coinmonks">Get Best Software Deals Directly In Your Inbox</a></blockquote><figure><a href="https://coincodecap.com/?utm_source=coinmonks"><img alt="" src="https://cdn-images-1.medium.com/max/584/0*OJ-qb5G6i863msBB.png" /></a></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6f6612ed8df4" width="1" height="1" alt=""><hr><p><a href="https://medium.com/coinmonks/how-to-create-an-upgradeable-smart-contract-using-zeppelinos-example-of-fixing-smart-contract-6f6612ed8df4">How to create an upgradeable smart contract using ZeppelinOS — example of fixing smart contract…</a> was originally published in <a href="https://medium.com/coinmonks">Coinmonks</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to write and deploy first smart contract in Solidity 0.5.x and Truffle 5.x.x]]></title>
            <link>https://medium.com/@paulina.blaszk/how-to-write-and-deploy-first-smart-contract-in-solidity-0-5-x-and-truffle-5-x-x-3054ec5328ad?source=rss-5426a9f14a62------2</link>
            <guid isPermaLink="false">https://medium.com/p/3054ec5328ad</guid>
            <category><![CDATA[smart-contracts]]></category>
            <category><![CDATA[ethereum]]></category>
            <category><![CDATA[truffle]]></category>
            <category><![CDATA[solidity]]></category>
            <dc:creator><![CDATA[Paulina Błaszkiewicz]]></dc:creator>
            <pubDate>Sun, 10 Mar 2019 20:12:23 GMT</pubDate>
            <atom:updated>2019-09-20T10:09:42.547Z</atom:updated>
            <content:encoded><![CDATA[<p>If you want to start your adventure with the Solidity language, first you need to set up an essential development environment for creating smart contracts. We will use Truffle version 5.0.6 and Solidity 0.5.0.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Zc42ldHPlG3-2R7YcKT4vQ.jpeg" /><figcaption>Source: pexels.com</figcaption></figure><h3>1. Setting up smart contracts environment</h3><h4>1.1 Node.js and npm installation</h4><p>Node.js is installed in a package with npm and can be downloaded from <a href="https://nodejs.org/en/">https://nodejs.org/en/</a>.<br>If you do not remember whether you already have Node.js and npm, you want to check the version or make sure that the installation has been carried out correctly — run the console command:</p><pre>node -v<br>npm -v</pre><h4>1.2 Truffle installation</h4><p><a href="https://truffleframework.com/truffle">Truffle</a> is the most popular framework for Ethereum. To install Truffle globally on your computer run the command in the console</p><pre>npm install -g truffle</pre><p>or</p><pre>sudo npm install -g truffle</pre><p>Now we can check our Truffle version by running in the console:</p><pre>truffle version</pre><p>I’m using right now:</p><ul><li>Truffle v5.0.6 (core: 5.0.6)</li><li>Solidity v0.5.0 (solc-js)</li><li>Node v11.6.0</li></ul><p>The latest Truffle version you can check at <a href="https://www.npmjs.com/package/truffle">https://www.npmjs.com/package/truffle</a>.</p><h4>1.3. Ganache installation</h4><p>Ganache is a local blockchain network for development purposes. It is convenient to use and it has a friendly interface. You can download it from <a href="https://truffleframework.com/ganache">ganache page</a>.</p><h4>1.4 Code editor for smart contracts in Solidity</h4><p>To start coding, you need an editor with an extension for Solidity language, so that you can use syntax highlighting or snippets. Personally, I recommend:</p><ul><li><a href="https://code.visualstudio.com/"><em>Visual Studio Code</em></a> — with the <a href="https://marketplace.visualstudio.com/items?itemName=JuanBlanco.solidity"><em>solidity</em></a> extension (currently I use it),</li><li><a href="https://atom.io/"><em>Atom</em> </a>— with <a href="https://atom.io/packages/language-ethereum"><em>language-ethereum</em></a><em> </em>extension</li></ul><p>Of course, you can simply choose your favorite code editor — the most popular have available packages for Solidity, for example:</p><ul><li><a href="https://www.sublimetext.com/">Sublime text</a> — with <a href="https://packagecontrol.io/packages/Ethereum"><em>Ethereum</em></a> extension</li></ul><p>More examples of integration you can find in the <a href="https://solidity.readthedocs.io/en/v0.5.4/resources.html?highlight=extensions#solidity-integrations">Solidity documentation</a>.</p><h3>2. Creating a first smart contract</h3><p>Our environment for creating smart contracts is prepared, so it’s time to go to the most interesting part and write the first smart contract in Solidity.</p><p>We will create a very simple contract “Hello”. Our main assumptions:</p><ul><li>we have three state variables — the name, the age and the Ethereum address of the owner,</li><li>the owner can add/change the name and the age,</li><li>everyone can get the value of state variables.</li></ul><h4>2.1 Project initiation</h4><p>First, we create a folder and initiate Truffle there.</p><pre>mkdir hello-solidity</pre><pre>cd hello-solidity</pre><pre>truffle init</pre><p>If everything goes well, we receive information: <em>Unbox successful. Sweet!</em></p><p>In the folder of our project, we can find now files and folders generated during the initiation. Truffle project structure looks as follows:</p><p><strong>Folders:</strong></p><ul><li><em>contracts</em> — in this folder we place our smart contract files. Currently, there is a contract named <em>Migrations.sol</em>. It was created automatically and is responsible for the migration process in Truffle. Usually, there is no need to modify it.</li><li><em>migrations</em> — this folder contains the file <em>1_initial_migartion.js</em>, which is responsible for deploying the <em>Migrations.sol</em> contract. We will add here a file with configuration for our smart contract.</li><li><em>test</em> — according to the name we place test files here (information on how to write tests will be in one of the next entries)</li></ul><p><strong>Files:</strong></p><ul><li><em>truffle.config.js</em> — Truffle configuration file, where we place settings regarding, among others, blockchain networks that we use, the selected version of the solc compiler, etc. An example configuration is below.</li></ul><p><strong>2.2. Truffle configuration — truffle.config.js file</strong></p><p>This file contains basic configuration settings allowing for the creation and implementation of smart contracts. It may require minor modification depending on the settings in Ganache. The default network used by Truffle is development. We should check if the port is compatible with the one indicated by Ganache. If not — you should change the port in truffle.config.js to the correct one (or you can change the port in the Ganache settings).</p><p>In the truffle.config.js file, you can also choose the version of the compiler (solc) you want to use. In our case, we will set it to 0.5.0.</p><p><em>Due to a Medium issue — the github code is not displayed in Safari. Use a different browser to view all content</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/79e1a7c263c9be7f4b86f40c7c94b378/href">https://medium.com/media/79e1a7c263c9be7f4b86f40c7c94b378/href</a></iframe><h4>2.4 Choosing the compiler version — pragma solidity</h4><p>In the <em>Contracts folder</em>, we create a new file and call it <em>Hello.sol</em> (extension .sol applies to smart contracts in Solidity). At the very top, we start by specifying the version of the compiler that should be used to compile our file. We use <em>pragma solidity</em> expression.</p><pre>pragma solidity ^0.5.0;</pre><p>In our case, this means that the minimum version of the compiler needed to compile our smart contract is 0.5.0. “^” means we can’t use the version higher 0.5.X. If we use the compiler in version 0.6.X (or higher), it through an error. Why? When upgrading to version 0.5.1 or 0.5.7, the changes shouldn’t disrupt our contract running, but in the upgrade to 0.6.X changes can be breaking.</p><p><strong>Important!</strong> The specific version of the compiler in smart contract inform us what version of the compiler is needed to compile our code. It does not change the version used by Truffle compiler — to do this you need to change the settings for Truffle in the file <em>truffle.config.js</em></p><h4>2.5 Smart contract — name and state variables declaration</h4><p>Now we can finally write our <em>Hello</em> contract. We start with the word <em>“contract”</em> and then indicate its name. In our case, smart contract name coincides with the file name.</p><p>In the beginning, inside a smart contract, we start with state variables declarations. Solidity is a statically typed language. First, we define the type of the variable and then its name. In our case, it will be a state variable <em>owner </em>holding the Ethereum address of the contract owner and string <em>name</em> with our name.</p><pre>pragma solidity ^0.5.0;</pre><pre>contract Hello {</pre><pre>    address private owner;<br>    string private name;<br>}</pre><p>In the smart contract we can also define other types of variables:</p><ul><li>bool — logical type,</li><li>int or uint — numeric types,</li><li>contract,</li><li>array,</li><li>struct,</li><li>enum,</li><li>mapping.</li></ul><p>More information about data types, you can find in the <a href="https://solidity.readthedocs.io/en/v0.5.4/types.html">Solidity documentation.</a></p><h4>2.6 Constructor</h4><p>Below the variables, we define a constructor — function called when deploying a smart contract in a blockchain network. In our constructor, we assign to the “owner” the Ethereum address which called deploy. This is “msg.sender” that determine the sender of the current message. The constructor is optional. We do not need to use it in our smart contract if we do not want our contract to perform any activities immediately after creation.</p><pre>constructor () public {<br>    owner = msg.sender;<br>}</pre><h4>2.7 Types of visibility (public, private, external, internal)</h4><p>There are four visibility types: public, private, external and internal. We have to specify the type for all variables and functions. For simplicity, assume that in our smart contract all variables are private (it does not mean that they are invisible to other users of the blockchain), and all functions are public. <a href="https://solidity.readthedocs.io/en/v0.5.4/contracts.html#visibility-and-getters">The Solidity documentation</a> precisely explains all types.</p><h4>2.8 Smart contract functions</h4><p>In our contract, we will create the following functions:</p><ul><li>setName — sets a new value to the variable name</li></ul><pre>function setName (string memory _name) public {<br>    require (msg.sender == owner, “You are not the owner!”);<br>    name = _name;<br>}</pre><ul><li>getName — gets the value of the variable name</li></ul><pre>function getName() public view returns (string memory) {<br>    return name;<br>}</pre><ul><li>sayHello — gets your greeting text</li></ul><pre>function sayHello()<br>    public<br>    view<br>    returns (string memory, string memory)<br>{<br>    return (“Hello Solidity! My name is:”, name);<br>}</pre><p>For functions that do not modify any states — in our case, they will be <em>getName()</em> and <em>sayHello() </em>— we add the “view” type after the visibility type.</p><p>In addition, we specify a data location for the function parameters (passed or returned by the function):</p><ul><li>memory — its lifetime is limited to a function call,</li><li>storage — the place where the state variables are stored,</li><li>calldata — the location of special data.</li></ul><p>Our first smart contract is ready and looks like this:</p><p><em>Due to a Medium issue — the github code is not displayed in Safari. Use a different browser to view all content</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/efa0352668d21aa096ab51dbc9f805a0/href">https://medium.com/media/efa0352668d21aa096ab51dbc9f805a0/href</a></iframe><h3>3. Compiling and deploying smart contract. Implementation on the local Ganache blockchain</h3><h4>3.1 Adding a smart contract migration file</h4><p>In the migrations folder, we create a new file named <em>2_deploy_contracts.js</em> where we define the smart contract that we want to deploy during migrations. In our case, where we have one smart contract, the content of this file will look like this:</p><pre>var Hello = artifacts.require (“Hello.sol”);</pre><pre>module.exports = function (deployer) {<br>    deployer.deploy (Hello);<br>};</pre><h4>3.2 Smart contract deployment</h4><p>First of all, we need to make sure that our local blockchain network — Ganache is running. Then we go to the console and run the command:</p><pre>truffle compile</pre><p>In this way, we check whether our smart contract compiles correctly. If everything is correct, we see will such a message:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/684/1*6OcieMjuW4apdWFggl8ZEg.png" /></figure><p>We can check now that in the location of our project, <em>build</em> folder was created, during compilation. We can find there also <em>contracts</em> folder, containing compiled versions of our smart contracts in <em>.json</em> format.</p><p>If everything compiles correctly, we can deploy our smart contract to our Ganache blockchain using:</p><pre>truffle migrate</pre><p>This command starts migrations to run the deployment process. As a result, we receive information about the correct migration. We can find there, among others address of our smart contract.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*08LgOCm1LO8MEbmKrvGnbA.png" /></figure><p>During the work on a smart contract in a local network, I often use the command:</p><pre>truffle migrate --reset --compile-all</pre><p>It causes that after smart contracts modifications, we force to recompile and migrate all of the contracts (not only those with changes) — this command sometimes helps to avoid emerging errors.</p><h4>3.3 Interactions with the smart contract</h4><p>To communicate with our smart contract, we will use the Truffle console. We run it with the command:</p><pre>truffle console</pre><p>Now the <em>truffle (development)</em> mode should appear in our console (to exit we use <em>ctrl+c</em>).</p><p>In the beginning, we save the instance of Hello contract in the app variable:</p><pre> Hello.deployed().then((instance)=&gt;{app = instance;})</pre><p>Now, by entering the <em>app</em> in truffle console, we can display our smart contract instance.</p><p>Then we add our name to the state variable name:</p><pre>app.setName(“Paulina”)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*17FyVz96mBLM5Xq3TkMRXg.png" /></figure><p>We check the result of this operation by calling our next function<em> getName ()</em></p><pre>app.getName()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/858/1*j2xGYHgDUQmXzlBQQVe7uw.png" /></figure><p>We got the value of our name, so now we can say hello to Solidity:</p><pre>app.sayHello()</pre><p>and we receive</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_AZtfSCTYq3WbERUIE4C2A.png" /></figure><p>Great job! We have just created and deployed to the blockchain simple smart contract. If you have any question, feel free to leave a comment.</p><h4>You can find the whole project on my <a href="https://github.com/paulinablaszk/hello-solidity">GitHub</a>.</h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3054ec5328ad" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>