<?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 Sarvesh Jain on Medium]]></title>
        <description><![CDATA[Stories by Sarvesh Jain on Medium]]></description>
        <link>https://medium.com/@sarvesh.sgsits?source=rss-b02931bf8a47------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*nD5WVAQwxi-l7oCR_mmHvA.jpeg</url>
            <title>Stories by Sarvesh Jain on Medium</title>
            <link>https://medium.com/@sarvesh.sgsits?source=rss-b02931bf8a47------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 21:41:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sarvesh.sgsits/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[Executable Transactions: Transaction Inside Transaction (Part-1)]]></title>
            <link>https://medium.com/coinmonks/executable-transactions-transaction-inside-transaction-part-1-96084c5119ca?source=rss-b02931bf8a47------2</link>
            <guid isPermaLink="false">https://medium.com/p/96084c5119ca</guid>
            <category><![CDATA[executable-transaction]]></category>
            <category><![CDATA[smart-contracts]]></category>
            <category><![CDATA[eip-1077]]></category>
            <category><![CDATA[cryptocurrency]]></category>
            <category><![CDATA[ethereum]]></category>
            <dc:creator><![CDATA[Sarvesh Jain]]></dc:creator>
            <pubDate>Sat, 06 Apr 2019 06:27:56 GMT</pubDate>
            <atom:updated>2020-08-26T13:44:51.180Z</atom:updated>
            <content:encoded><![CDATA[<p>The increasing popularity of ethereum blockchain has attracted many businesses to leverage blockchain for their existing consumer applications. However, one fundamental challenge that businesses face is to educate their consumers about blockchain, public-private keys management, gas consumption and what not!</p><p>Imagine for every action that consumer performs on the application, they need to sign a transaction using meta mask, think about gas consumption and think about ether in the account for paying gas.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*D0L3qxMBqzfYVsQ5xTB5JQ.jpeg" /><figcaption>Blockchain end user be like</figcaption></figure><p>Executable transactions, also called ether-less transaction or transaction insider a transaction, is a very effective approach to simplify user interaction with the blockchain.</p><p>Entities involved in the executable transaction:<br>1. <strong>An end user with ether-less key</strong>: End-user has an ether-less key, which is used to sign a transaction.</p><p>2. <strong>Transaction Relayer</strong>: Transaction relayer has a job to submit the signed transaction on the blockchain on behalf of someone else. Transaction relayer actually pays gas for the transaction. Usually, the transaction is submitted to a smart contract which executes the executable transaction.</p><p>3. <strong>A smart contract: </strong>Responsibility of smart contract is to verify the signature of the end user and execute the executable transaction submitted by relayer. It also calculates the gas consumption of the transaction so that gas station can refund amount to relayer at the end of the transaction.</p><p>4.<strong> Gas station</strong>: It’s a smart contract which holds tokens/eth. These tokens are used to refund gas paid by transaction relayer to execute a transaction on behalf of the end user.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iH7TrEAm22AOjm8t-zzEKQ.png" /></figure><p><strong>The sequence of actions: <br></strong>1. End-user signs a transaction with an ether-less key registered with the smart contract.</p><p>2. End user sends the transaction to a relayer to submit to the blockchain.</p><p>3. Relayer creates a new transaction(calling smart contact method) which wraps end user’s transaction.</p><p>4. Relayer submits the transaction to the blockchain and pays for the gas.</p><p>5. Smart contract verifies the signature of the end user.</p><p>6. Smart contract tracks the gas consumption and executes the end user’s transaction.</p><p>7. Smart contract asks gas station to refund the consumed gas to the relayer.</p><p>This approach solves major blockchain UX problems like:<br>1. The user doesn’t need to think in terms of ether. <br>2. The user doesn’t need to use extensions like meta-mask to sign the transaction. Signing with the ether-less key can easily be encapsulated with app logic.</p><p>I would discuss the implementation details of this approach in <strong>part-2 </strong>of this blog. Stay tuned. 🙌</p><p>Happy Coding 😇</p><p><a href="https://www.linkedin.com/in/jainsarvesh/">linkedin.com/in/jainsarvesh</a> <br><a href="http://medium.com/@sarvesh.sgsits%20https://twitter.com/sarvesh_jain_">twitter.com/sarvesh_jain_</a><br><a href="https://medium.com/@sarvesh.sgsits">medium.com/@sarvesh.sgsits</a></p><p>References: <br><a href="https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1077.md">https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1077.md</a><br><a href="https://ethereum-magicians.org/t/eip-1077-executable-signed-message/405">https://ethereum-magicians.org/t/eip-1077-executable-signed-message/405</a></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=96084c5119ca" width="1" height="1" alt=""><hr><p><a href="https://medium.com/coinmonks/executable-transactions-transaction-inside-transaction-part-1-96084c5119ca">Executable Transactions: Transaction Inside Transaction (Part-1)</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[All you should know about libraries in solidity]]></title>
            <link>https://medium.com/coinmonks/all-you-should-know-about-libraries-in-solidity-dd8bc953eae7?source=rss-b02931bf8a47------2</link>
            <guid isPermaLink="false">https://medium.com/p/dd8bc953eae7</guid>
            <category><![CDATA[solidity]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[ethereum]]></category>
            <category><![CDATA[libraries]]></category>
            <category><![CDATA[dapps]]></category>
            <dc:creator><![CDATA[Sarvesh Jain]]></dc:creator>
            <pubDate>Fri, 21 Sep 2018 03:58:41 GMT</pubDate>
            <atom:updated>2022-06-26T11:23:00.098Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Ugp4QQOB6XvJwi-L" /><figcaption>Photo by <a href="https://unsplash.com/@inakihxz?utm_source=medium&amp;utm_medium=referral">Iñaki del Olmo</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>It’s very important to know about the libraries in solidity while writing Dapps. In simple words, a library is the reusable piece of code which is deployed once and shared many times.</p><p>But libraries are not just limited to reusability, there are few other areas where ethereum developers are using the library feature. This post aims to touch all those topics starting from basics.</p><p>Let’s start with a simple library for mathematical operation. The SafeMath library described below contains basic arithmetic operation which takes 2 unsigned integer as input and returns the arithmetic operation result.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d2f7dc1af846a2a076fe51436dfeb3ea/href">https://medium.com/media/d2f7dc1af846a2a076fe51436dfeb3ea/href</a></iframe><blockquote>“Ideally, libraries are not meant to change state of contract, it should only be used to perform simple operations based on input and returns result”</blockquote><p>Before going in depth, let’s cover few prerequisite for better understanding:</p><ol><li><strong>What is a contract in solidity?</strong> : Technically, smart contract is a basic building block of a Dapp. In ethereum, smart contract has address like external user account which can be used to make interaction with contract like calling method, sending ether etc. Each contract has these four properties:</li></ol><ul><li>Nonce : It’s a count of number of transaction triggered from an account.</li><li>Balance: It’s a number that tell about amount of ether this particular address holds</li><li>Storage root: Contract can store data, it’s a root of tree which stores data from this contract</li><li>Codehash : It’s hashed value of contract code.</li></ul><p>2.<strong> </strong><a href="https://solidity.readthedocs.io/en/v0.4.24/types.html?highlight=pure%20view#function-types"><strong>Function types</strong></a><strong>:</strong> Solidity has following function types:</p><ul><li>Internal: This type of function is only called from inside of contract</li><li>External:This type of function is only called from outside of contract</li><li>Public: This type of function is called from outside of contract as well inside.</li><li>Pure: This kind of function neither reads nor write to storage of contract.</li><li>View: This kind of function can only read from storage but can’t write from storage.</li><li>Payable: A contract function can only accept ether if it’s marked as payable.</li></ul><p><strong>How library works?</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/962/1*b9pSrgh16kQgj6e8yWj0RA.png" /></figure><p>In blockchain, transaction can change state of smart contract. There are various kinds of state change that can happen in a contract:</p><ul><li>Sending ether: It will update balance of contract</li><li>Changing data in contract: It will change storage root.</li></ul><blockquote>“Whenever user sends a transaction to a smart contract which is internally using library contract then state change happens on smart contract not in library contract. This is possible with delegate call feature of EVM”</blockquote><p><a href="http://solidity.readthedocs.io/en/develop/introduction-to-smart-contracts.html?highlight=delegatecall#delegatecall-callcode-and-libraries"><strong>Delegate Call:</strong></a></p><p>Paraphrased from the <a href="http://solidity.readthedocs.io/en/develop/introduction-to-smart-contracts.html?highlight=delegatecall#delegatecall-callcode-and-libraries">solidity docs</a>:</p><p><em>“Delegatecall is identical to a message call apart from the fact that the code at the target address is executed in the context of the calling contract and </em>msg.sender <em>and </em>msg.value<em> do not change their values.</em></p><p><em>This means that a contract can dynamically load code from a different address at runtime. Storage, current address and balance still refer to the calling contract, only the code is taken from the called address.”</em></p><p>This low-level function has been very useful as it’s the backbone for implementing libraries.</p><p><strong>Deployment of libraries:</strong></p><p>Library deployment is a bit different from regular smart contract deployment. There are two scenarios in the library deployment:</p><ol><li><strong>Embedded Library:</strong> If a smart contract is consuming a library which have only <strong>internal functions</strong> than EVM simply embeds library into the contract. Instead of using delegate call to call a function, it simply uses JUMP statement(normal method call). There is no need to separately deploy library in this scenario.</li><li><strong>Linked Library :</strong> On the flip side, if a library contain <strong>public or external functions</strong> then library needs to be deployed. Deployment of library will generate unique address in blockchain. This address needs to be linked with calling contract.</li></ol><p>Let’s consider simple example to understand linked libraries.</p><p>Below code snippet shows a ERC20 contract with transfer function. There are two methods in SafeMath library “sub” and “add” used in transfer function which are <strong>external</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5d61f3a2c8eb221aaa0ec4077d8fbefe/href">https://medium.com/media/5d61f3a2c8eb221aaa0ec4077d8fbefe/href</a></iframe><p>There are two interesting observations to make.</p><ol><li>SafeMath library has external functions.</li></ol><p>2. String given below is part of the bytecode generated after compilation of ERC20 contract. This bytes code contains reference of SafeMath __SafeMath______________________________. This means that the bytes of ERC20 can’t be deployed before performing linkage step. Linking essentially means replacing library reference in the byte code with it’s address.</p><p>0x608060405234801561001057600080fd5b50610343806100206000396000f300608060405260043610610041576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063a9059cbb14610046575b600080fd5b34801561005257600080fd5b50610091600480360381019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803590602001909291905050506100ab565b604051808215151515815260200191505060405180910390f35b60008060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205473__SafeMath______________________________63b67d77c59091846040518363ffffffff167c0100000000000000000000000000000000000</p><p><strong>How to link library contract during deployment?</strong></p><ol><li>Deploy library : I have deployed SafeMath on ropsten and it’s address is <a href="https://ropsten.etherscan.io/address/0x40189fb71f54a3ad0370620dfb095382859eb095">0x40189fb71f54a3ad0370620dfb095382859eb095</a></li><li>Link deployed library with the contract. Below command can be used to link SafeMath library with ERC20 contract.</li></ol><p><em>solc ERC20.sol — libraries “SafeMath:0x40189fb71f54a3ad0370620dfb095382859eb095” — bin</em></p><p>After successful linking, SafeMath references will be removed from the bytecode and contract is ready for deployment.</p><p><strong><em>‘Using for’</em> in library:</strong></p><p>In solidity <strong>using X for Y </strong>directive means, library function of X is attached with type Y.</p><p>For instance,<strong> Using SafeMath for uint256</strong>.</p><p>SafeMath functions like add, sub, mul and div are now bound with type uint256.</p><p>“One condition which should be taken care is, library functions will receive the object they are called on as their first parameter”</p><p><strong>For example: </strong>In ERC-20 contract mentioned above. Using for directive is using for SafeMath for type uint256.</p><pre><strong>using </strong>SafeMath <strong>for uint256</strong>;<br><br><strong>uint256 </strong>a = 10;<br><strong>uint256 </strong>b= 10;<br><br><strong>uint256 </strong>= a.add(b);</pre><p>Here <strong>add</strong> function is available from SafeMath. SafeMath library functions are bound with uint256.</p><p>Great!!! 🙌</p><p>I hope, this article helped you to understand libraries in solidity.</p><p>Happy Coding 😇</p><p><a href="http://demystifyblockchain.com/">Demystify Blockchain | Future is decentralised internet</a></p><p><a href="https://www.linkedin.com/in/jainsarvesh/">linkedin.com/in/jainsarvesh</a><br><a href="https://medium.com/@sarvesh.sgsits">medium.com/@sarvesh.sgsits</a></p><blockquote>Join Coinmonks<a href="https://t.me/coincodecap"> Telegram Channel</a> and<a href="https://www.youtube.com/c/coinmonks/videos"> Youtube Channel</a> get daily <a href="http://coincodecap.com/">Crypto News</a></blockquote><h4>Also, Read</h4><ul><li><a href="https://medium.com/coinmonks/top-10-crypto-copy-trading-platforms-for-beginners-d0c37c7d698c">Copy Trading</a> | <a href="https://medium.com/coinmonks/crypto-tax-software-ed4b4810e338">Crypto Tax Software</a></li><li><a href="https://coincodecap.com/grid-trading">Grid Trading</a> | <a href="https://medium.com/coinmonks/the-best-cryptocurrency-hardware-wallets-of-2020-e28b1c124069">Crypto Hardware Wallet</a></li><li><a href="http://Top 4 Telegram Channels for Crypto Traders">Crypto Telegram Signals</a> | <a href="https://medium.com/coinmonks/crypto-trading-bot-c2ffce8acb2a">Crypto Trading Bot</a></li><li><a href="https://medium.com/coinmonks/crypto-exchange-dd2f9d6f3769">Best Crypto Exchange</a> | <a href="https://medium.com/coinmonks/bitcoin-exchange-in-india-7f1fe79715c9">Best Crypto Exchange in India</a></li><li><a href="https://coincodecap.com/bitget-review">Bitget Review</a> |<a href="https://coincodecap.com/gemini-vs-blockfi"> Gemini vs BlockFi</a> |<a href="https://coincodecap.com/okex-futures-trading"> OKEx Futures Trading</a></li><li><a href="https://coincodecap.com/crypto-trading-bots-in-the-us">Best Crypto Trading Bots in the US</a> |<a href="https://coincodecap.com/changelly-review"> Changelly Review</a></li><li><a href="https://coincodecap.com/crypto-arbitrage-in-india">Earn Passive Income Using Crypto Arbitrage In India</a></li><li><a href="https://coincodecap.com/huobi-review">Huobi Review</a> |<a href="https://coincodecap.com/okex-margin-trading"> OKEx Margin Trading</a> |<a href="https://coincodecap.com/futures-trading"> Futures Trading</a></li><li><a href="https://medium.com/coinmonks/best-crypto-apis-for-developers-5efe3a597a9f">Best Crypto APIs</a> for Developers</li><li>Best <a href="https://medium.com/coinmonks/top-5-crypto-lending-platforms-in-2020-that-you-need-to-know-a1b675cec3fa">Crypto Lending Platform</a></li><li>An ultimate guide to <a href="https://medium.com/coinmonks/leveraged-token-3f5257808b22">Leveraged Token</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dd8bc953eae7" width="1" height="1" alt=""><hr><p><a href="https://medium.com/coinmonks/all-you-should-know-about-libraries-in-solidity-dd8bc953eae7">All you should know about libraries in solidity</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[What the hack is Memory and Storage in Solidity?]]></title>
            <link>https://medium.com/coinmonks/what-the-hack-is-memory-and-storage-in-solidity-6b9e62577305?source=rss-b02931bf8a47------2</link>
            <guid isPermaLink="false">https://medium.com/p/6b9e62577305</guid>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[solidity]]></category>
            <category><![CDATA[ethereum]]></category>
            <category><![CDATA[memories]]></category>
            <category><![CDATA[storage]]></category>
            <dc:creator><![CDATA[Sarvesh Jain]]></dc:creator>
            <pubDate>Wed, 13 Jun 2018 16:02:54 GMT</pubDate>
            <atom:updated>2022-06-25T16:08:10.080Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/490/1*3Ivk20SDKGksv3dGaPkisQ.jpeg" /></figure><p>One of the most confusing concepts in solidity is how to effectively use memory and storage.</p><p>This article aims to provide a basic understanding of memory and storage and also demonstrate it’s effective usage with some examples.</p><blockquote><a href="https://coincodecap.com/">Discover and review best Blockchain softwares</a></blockquote><p>If you are new to solidity, please read the below articles first:</p><p><a href="http://demystifyblockchain.com/2018/06/29/getting-started-with-ethereum-blockchain-development-part-1/"><strong>Getting Started with ethereum blockchain development: part-1</strong></a></p><p><a href="http://demystifyblockchain.com/2018/06/28/getting-started-with-ethereum-blockchain-development/"><strong>Getting Started with ethereum blockchain development: Part-2</strong></a></p><p>Before we move ahead, let’s understand the different kind of storage that solidity has:</p><p><strong>Storage:</strong> The persistent memory that every account has is called<strong> storage</strong>. Storage is a key-value store where keys and values are both 32 bytes.</p><p><strong>Memory:</strong> Memory is a byte-<strong>array</strong>, which hold the data in it until the execution of the function. Memory starts with zero-size and can be expanded in 32-byte chunks by simply accessing or storing memory at indices greater than its current size. <br>To save gas, it is always recommended to shrink it’s size whenever possible.</p><p><strong>Stack</strong>: It is used to hold small local variables. It costs same as memory, but can only hold a limited amount of values.</p><h3><strong>Gas Consumption while consuming space</strong></h3><p>This section of the article assumes that reader has a basic understanding of gas consumption in ethereum, if not refer below link.👇</p><p><a href="https://ethereum.stackexchange.com/questions/3/what-is-meant-by-the-term-gas"><strong><em>What is gas in ethereum?</em></strong></a></p><p>Everything you do on ethereum blockchain will cost you some gas. Let’s try to compare gas consumption in storage and memory.</p><p><strong>Storage Gas consumption: </strong>Storage gas cost is easy to understand:</p><ul><li>It costs 20,000 gas when a value is set to non-zero from zero</li><li>It costs 5,000 gas when writing to existing storage or setting a value to zero.</li><li>5,000 gas<a href="https://ethereum.stackexchange.com/q/594/42"> </a>is refunded when a non-zero value is set to zero.</li></ul><p>Gas savings from packing storage is still possible, such as fitting 2 <a href="http://solidity.readthedocs.io/en/v0.4.21/types.html#integers">uint128</a> values within a single key, instead of using 2 keys.</p><p><strong>Memory Gas consumption: </strong>The gas cost of memory expansion is defined in the <a href="http://yellowpaper.io/">Yellow Paper</a> as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/324/0*oKdhvMaKWWo71Umk" /><figcaption>Gas consumption in memory expansion</figcaption></figure><p><strong>G_memory</strong> is 3, <strong>a</strong> is the number of 256-bit chunk words allocated.</p><p>For a =<strong>512 chunks</strong>, Gas consumption will be (3 * 512 + (512 * 512)/512) = <strong>2560 units</strong></p><p><strong><em>Note: Cost of memory usage is not very significant as compared to storage. It’s a good practice to always use memory to perform intermediate calculations and store results in storage.</em></strong></p><h3><strong>Where </strong>are<strong> variables stored?</strong></h3><p>For each type of variable, there exists default storage location::</p><ul><li>State variables are always in storage.</li><li>Function arguments are always in memory.</li><li>Local variables of struct, array or mapping type reference are stored in storage by default.</li><li>Local variables of a value type (uint, int etc) are stored in the stack.</li></ul><p><strong>Let’s deep dive more into this and understand one more important concept around memory and storage !!!</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9dadde39111daf5b3520f4c067e03b91/href">https://medium.com/media/9dadde39111daf5b3520f4c067e03b91/href</a></iframe><p><strong>Contract description:</strong></p><ul><li><strong>Contract variable</strong>: Mapping to store key-value pair as user Id and User type.<br><em> mapping(uint =&gt; User) users;</em></li><li><strong>Methods: </strong>Followings are methods in the contract:</li></ul><p>I. <strong>addUser</strong>: It takes uint id, uint balance as the argument and adds a user to mapping.</p><p>II. <strong>updateBalance</strong>:<strong> </strong>It takes uint id, uint balance as the argument and updates balance of the user.</p><p>III. <strong>getBalance</strong>: It takes uint id as an argument and returns the balance of the user.</p><ul><li><strong>Call Contract Method</strong>: You can run above contract in <a href="http://remix.ethereum.org/#optimize=false&amp;version=soljson-v0.4.24+commit.e67f0147.js">online Remix IDE</a></li></ul><p>I. Create a user by calling <strong>addUser</strong> method with arguments (id = 1 and balance = 10)</p><p>II. Update balance of user by calling <strong>updateBalance </strong>method with arguments(id = 1 and balance = 20)</p><ul><li><strong>Expected Output</strong>: Get the balance of user by calling <strong>getBalance </strong>method with an argument (id = 1). What do you think is the expected value? Have a guess? Is it 2<strong>0 or 10</strong> and why?🤔</li></ul><h3>Output is 10 not 20. 😯</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*ai7XZuVDZqtpxbbCzMqV_g.jpeg" /></figure><p><strong>Explanation: </strong>In solidity, when storage type variable is assigned to memory type it creates a new copy of data. If any update is done on copied data, it will not reflect in the original copy.</p><p>Can you now guess, what change you need to do in <strong>updateBalance</strong> method to make it work? 🤔</p><p>Change line 17 in the contract. Changing storage location from memory to storage will not create a new copy of data, rather it will create a pointer to storage. Hence any change in user object will be reflected in the storage itself.</p><p><em>User </em><strong><em>storage</em></strong><em> user = users[id];</em></p><p>Bingo!!! You now have a good understanding of memory and storage in the solidity 🙌</p><p>In the next few articles. I will explain about EVM internals and Merkel Pertrica trees.</p><p>Happy Coding 😇</p><p><a href="https://www.linkedin.com/in/jainsarvesh/">linkedin.com/in/jainsarvesh</a><br><a href="https://medium.com/@sarvesh.sgsits">medium.com/@sarvesh.sgsits</a></p><blockquote>Join Coinmonks<a href="https://t.me/coincodecap"> Telegram Channel</a> and<a href="https://www.youtube.com/c/coinmonks/videos"> Youtube Channel</a> get daily <a href="http://coincodecap.com/">Crypto News</a></blockquote><h4>Also, Read</h4><ul><li><a href="https://medium.com/coinmonks/top-10-crypto-copy-trading-platforms-for-beginners-d0c37c7d698c">Copy Trading</a> | <a href="https://medium.com/coinmonks/crypto-tax-software-ed4b4810e338">Crypto Tax Software</a></li><li><a href="https://coincodecap.com/grid-trading">Grid Trading</a> | <a href="https://medium.com/coinmonks/the-best-cryptocurrency-hardware-wallets-of-2020-e28b1c124069">Crypto Hardware Wallet</a></li><li><a href="http://Top 4 Telegram Channels for Crypto Traders">Crypto Telegram Signals</a> | <a href="https://medium.com/coinmonks/crypto-trading-bot-c2ffce8acb2a">Crypto Trading Bot</a></li><li><a href="https://coincodecap.com/bookmap-review-2021-best-trading-software">Bookmap Review</a> |<a href="https://coincodecap.com/crypto-exchange-usa"> 5 Best Crypto Exchanges in the USA</a></li><li><a href="https://coincodecap.com/ftx-futures-trading">How to trade Futures on FTX Exchange</a> |<a href="https://coincodecap.com/okex-vs-binance"> OKEx vs Binance</a></li><li><a href="https://coincodecap.com/coinloan-review">CoinLoan Review</a> |<a href="https://medium.com/coinmonks/youhodler-4-easy-ways-to-make-money-98969b9689f2"> YouHodler Review</a> |<a href="https://coincodecap.com/blockfi-review"> BlockFi Review</a></li><li><a href="https://coincodecap.com/profittradingapp-for-binance">ProfitTradingApp for Binance Review</a> |<a href="https://coincodecap.com/xt-com-review"> XT.COM Review</a></li><li><a href="https://coincodecap.com/smithbot-review">SmithBot Review</a> |<a href="https://coincodecap.com/free-open-source-trading-bots"> 4 Best Free Open Source Trading Bots</a></li><li><a href="https://medium.com/coinmonks/crypto-exchange-dd2f9d6f3769">Best Crypto Exchange</a> | <a href="https://medium.com/coinmonks/bitcoin-exchange-in-india-7f1fe79715c9">Best Crypto Exchange in India</a></li><li><a href="https://medium.com/coinmonks/best-crypto-apis-for-developers-5efe3a597a9f">Best Crypto APIs</a> for Developers</li><li>Best <a href="https://medium.com/coinmonks/top-5-crypto-lending-platforms-in-2020-that-you-need-to-know-a1b675cec3fa">Crypto Lending Platform</a></li><li>An ultimate guide to <a href="https://medium.com/coinmonks/leveraged-token-3f5257808b22">Leveraged Token</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6b9e62577305" width="1" height="1" alt=""><hr><p><a href="https://medium.com/coinmonks/what-the-hack-is-memory-and-storage-in-solidity-6b9e62577305">What the hack is Memory and Storage in Solidity?</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[Getting Started with ethereum blockchain development]]></title>
            <link>https://medium.com/coinmonks/getting-started-with-ethereum-blockchain-development-921eb42975e6?source=rss-b02931bf8a47------2</link>
            <guid isPermaLink="false">https://medium.com/p/921eb42975e6</guid>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[solidity]]></category>
            <category><![CDATA[ethereum]]></category>
            <category><![CDATA[smart-contracts]]></category>
            <category><![CDATA[web3]]></category>
            <dc:creator><![CDATA[Sarvesh Jain]]></dc:creator>
            <pubDate>Thu, 31 May 2018 05:50:21 GMT</pubDate>
            <atom:updated>2018-07-19T03:18:42.112Z</atom:updated>
            <content:encoded><![CDATA[<h3>Getting Started with ethereum blockchain development: Part-2</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3TXtVNSmzrOy7JxAa1QxAw.png" /></figure><p>In the first blog<a href="http://demystifyblockchain.com/2018/06/29/getting-started-with-ethereum-blockchain-development-part-1/"> Getting Started with ethereum blockchain development: Part-1</a>, I have demonstrated how to create simple smart contract and get it deployed on ganache-cli. I have also demonstrated, how to write truffle tests for the contract. You can find code in <a href="https://github.com/sarveshgs/firstBlockchainApp">Github.</a></p><p>In this article, I will demonstrate how to write interaction layer to interact with smart contract. We will use <a href="https://www.npmjs.com/package/web3">web3</a> npm package to write interaction layer. This article assumes that you already have setup your local machine for blockchain development by steps mentioned in <a href="http://demystifyblockchain.com/2018/06/29/getting-started-with-ethereum-blockchain-development-part-1/">part-1</a> of this article.</p><p>Let’s write interaction layer to interact with ‘<strong>Counter</strong>’ <a href="https://github.com/sarveshgs/firstBlockchainApp/blob/master/contracts/Counter.sol">contract</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c50563f6046d0c926b49142f0ef1f028/href">https://medium.com/media/c50563f6046d0c926b49142f0ef1f028/href</a></iframe><p>Let’s perform some more setup before we move ahead:</p><ol><li><strong>Setup Codebase: </strong>If you have followed part-1, you can skip this step. Otherwise checkout code from github.</li></ol><pre><br><strong> <em>git clone </em></strong><a href="https://github.com/sarveshgs/firstBlockchainApp.git"><strong><em>https://github.com/sarveshgs/firstBlockchainApp.git</em></strong><em><br></em></a><em> </em><strong><em>git checkout tutorial-part-1</em></strong></pre><ol><li><strong>Initialise node project:</strong> Use npm to initialise node project.</li></ol><pre><strong><em> npm init</em></strong></pre><p>3.<a href="https://web3js.readthedocs.io/en/1.0/"><strong>Web3</strong></a><strong>: </strong>Web3 is a library which helps you to interact with a local or remote ethereum node, using a HTTP or IPC connection. You can use npm to install it.</p><pre><strong><em>npm install web3 — save</em></strong></pre><h3><strong>Let’s get started with some coding</strong></h3><ol><li><strong>Deploy Contract: </strong>Let’s deploy Counter.sol contract which will create a contract address. Contract address is needed by Web3 to make interactions.</li></ol><p>I. Run ganache-cli: Setup ethereum node by running ganache-cli.</p><h3><strong><em>ganache-cli</em></strong></h3><p>II. Unlock Account: Unlock first account of created by ganache-cli.a.</p><p>Open truffle console:</p><pre>  <strong><em>truffle console</em></strong></pre><p>b. Unlock Account: Use first account address and private key printed on console when ganache-cli is ran to unlock account. This unlock account is used to deploy contract.</p><pre><strong><em>web3.personal.unlockAccount(firstAccountAdress,firstPrivateKey,15000)</em></strong></pre><p>III. Deploy contract: You can use truffle to deploy contract. Note down contract address underlined with red line in the image.</p><pre><em> </em><strong><em>truffle migrate</em></strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*7WJFF4jl3ao5M3Za." /></figure><p>2.<strong> Writing Js script to interact with contract: </strong>After completing first step successfully, you should now have contract address. We will pass this address to web3 to identify contract deployed on ethereum node.</p><p>I. Create a folder in your project structure named as ‘<strong>interaction</strong>’ and ‘<strong>interaction.js</strong>’ in interaction folder.</p><p>II. Make web3 connection with local ethereum node: ganache-cli is running at <a href="http://localhost:8545/"><em>http://localhost:8545</em></a></p><pre><strong>const </strong>Web3 = require(&#39;web3&#39;);<br><strong>const </strong>web3 = <strong>new </strong>Web3(<strong>new </strong>Web3.providers.HttpProvider(&quot;http://localhost:8545&quot;));</pre><p>III. Initialise contract: To initialise contract, we need two things</p><ul><li>Contract address: You must be having a contract address after deploying contract successfully in step-1</li><li>ABI: ABI stands for <em>application binary interface</em>. Basically it’s metadata of your contract in json format. You can find abi in ‘<strong><em>build</em>’</strong> folder of your project. It is generated after compilation of contracts.</li></ul><pre><strong>const </strong>metadata = require(&#39;../build/contracts/Counter&#39;);<br><strong>const </strong>abi = metadata.abi;<br><strong>const </strong>contractAddress = &quot;0x42f18096c12db0b85ce999dbe5cceb4316f1e0be&quot;;<br><strong>const </strong>counter = <strong>new </strong>web3.eth.Contract(abi, contractAddress);</pre><p>IV. Calling contract methods: Last thing, which needs to be done is to call contract method. Calling a contract method will form a transaction in ethereum blockchain. Once the transaction is executed, it will return a transaction receipt. This receipt can be used to retrieve return value from method and events.</p><p>In below code snippet, we are calling ‘<a href="https://github.com/sarveshgs/firstBlockchainApp/blob/master/contracts/Counter.sol">incrementCounter</a>’ method and listening for ‘on receipt receive event’</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ca47a29448192393b136e5837c4df870/href">https://medium.com/media/ca47a29448192393b136e5837c4df870/href</a></iframe><p>V. Run interaction.js: Run interaction.js using node.</p><p><strong><em>node interaction/interaction.js</em></strong></p><p>Great you now know, how to write smart contract and interaction layer to interact with contract methods using web3.</p><p>Full source code is available on Github:<a href="https://github.com/sarveshgs/firstBlockchainApp">sarveshgs/firstBlockchainApp</a></p><p><a href="https://github.com/sarveshgs/firstBlockchainApp">sarveshgs/firstBlockchainApp</a></p><p>Thank you for reading both blogs, I hope it gave you some basic understanding on how to write the dapps.</p><p>In next few blog, I have explained about gas consumption and effective memory management.</p><h4><a href="http://demystifyblockchain.com/2018/06/29/what-the-hack-is-memory-and-storage-in-solidity/">What the hack is Memory and Storage in Solidity?</a></h4><p>Stay tuned, Write back or post comments for queries and suggestion.</p><p>Happy Coding :)</p><p><a href="https://www.linkedin.com/in/jainsarvesh/">linkedin.com/in/jainsarvesh</a><br><a href="https://medium.com/@sarvesh.sgsits">medium.com/@sarvesh.sgsits</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=921eb42975e6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/coinmonks/getting-started-with-ethereum-blockchain-development-921eb42975e6">Getting Started with ethereum blockchain development</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[Getting Started with ethereum blockchain development: part-1]]></title>
            <link>https://medium.com/coinmonks/getting-started-with-ethereum-blockchain-development-part-1-d6543b441bea?source=rss-b02931bf8a47------2</link>
            <guid isPermaLink="false">https://medium.com/p/d6543b441bea</guid>
            <category><![CDATA[smart-contracts]]></category>
            <category><![CDATA[truffle]]></category>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[ethereum]]></category>
            <dc:creator><![CDATA[Sarvesh Jain]]></dc:creator>
            <pubDate>Sun, 27 May 2018 14:16:28 GMT</pubDate>
            <atom:updated>2020-01-22T10:24:58.374Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*jtd-S0OXhmb3u7VK." /><figcaption>Photo by <a href="https://unsplash.com/@thoughtcatalog?utm_source=medium&amp;utm_medium=referral">Thought Catalog</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><strong>This article assumes that the reader has basic understanding of blockchain vocabulary:</strong></p><ul><li><a href="https://www.coindesk.com/information/what-is-blockchain-technology/"><strong>What is Blockchain?</strong></a></li><li><a href="https://www.coindesk.com/information/what-is-ethereum/"><strong>What is Ethereum?</strong></a></li><li><a href="https://blockgeeks.com/guides/dapps/"><strong>What is Dapp?</strong></a></li><li><a href="https://www.coindesk.com/information/how-ethereum-works/"><strong>What is EVM?</strong></a></li><li><a href="https://www.coindesk.com/information/ethereum-smart-contracts-work/"><strong>What is Smart Contract?</strong></a></li></ul><p>In this section, I will try to write basic smart contract and get it deployed on local blockchain.</p><p>Smart contract is written in solidity language. Before writing smart contract let’s setup machine for local development.</p><blockquote><a href="https://coincodecap.com/">Discover and review best Blockchain softwares</a></blockquote><ol><li><strong>Node environment</strong>: Node installer is available <a href="https://nodejs.org/en/download/">here</a>. You can also use package manager like brew to install node.</li></ol><pre>Install using Brew: <strong><em>brew install node@8</em></strong></pre><p>2. <a href="http://solidity.readthedocs.io/en/v0.4.21/installing-solidity.html"><strong>Solc Compiler</strong></a>: Smart contract are compiled by <em>Solc compiler. </em>There are <a href="http://solidity.readthedocs.io/en/v0.4.21/installing-solidity.html">different ways</a> to install Solc compiler. You can also use npm to install it.</p><pre><strong><em>npm install -g solc</em></strong></pre><p><a href="https://geth.ethereum.org/">3.<strong> Go Ethereum</strong></a>:<strong> </strong>Go Ethereum which is referred as geth is command line interface to run ethereum node implemented in Go. You can install geth from <a href="https://geth.ethereum.org/downloads/">here</a>.</p><p>4. <a href="http://truffleframework.com/"><strong>Truffle framework</strong></a><strong>: </strong>Truffle is development environment and test framework for ethereum. You can install truffle using npm.</p><pre><strong><em>npm install -g truffle</em></strong></pre><p>5. <strong>Ganache</strong>: What Ganache does is simple, it creates a virtual Ethereum blockchain, and it generates some fake accounts that we will use during development.</p><pre><strong><em>npm install -g ganache-cli</em></strong></pre><p><strong>Bingo!!</strong> Now your local machine is setup for blockchain development.</p><p>Let’s create truffle project for your first blockchain application.</p><p>We can use truffle command to initialize project: <strong><em>truffle init</em></strong></p><p>Following file and folders will be created:</p><ul><li><strong>contracts: </strong>Folder to keep solidity contracts</li><li><strong>migrations: </strong>JS scripts to deploy solidity contracts</li><li><strong>test: </strong>Truffle test cases</li><li><strong>truffle.js:</strong> Truffle config file</li></ul><p><strong>Writing Smart Contract:</strong></p><ol><li><strong>Add truffle config file:</strong> Add below config to truffle.js. In below config, we are declaring development network, which will connect ethereum node running on localhost and port 8545.</li></ol><pre>module.exports = {<br>  networks: {<br>    development: {<br>      host: &quot;127.0.0.1&quot;,<br>      port: 8545,<br>      network_id: &quot;*&quot; // Match any network id<br>    }<br>  }<br>};</pre><p>2. <strong>Creating a contract</strong>: Lets create a simple contract ‘counter.sol’ file in contract folder.</p><p>Counter contract defines following things:</p><p>I<strong>. Events</strong>:</p><p><strong>What are events in Solidity</strong>: Events are dispatched signals that smart contracts can fire. DApps, or anything connected to Ethereum JSON-RPC API(like node js application), can listen to these events and act accordingly.</p><p>Counter contract defines two events-</p><ul><li>CounterIncrementedEvent</li><li>CounterDecrementedEvent</li></ul><p>II. <strong>Variable</strong>: Like other programming languages, variable is something that holds data.</p><p>III. <strong>Methods</strong>: Counter contract defines three methods to increment, decrement and get counter value.</p><pre><strong>pragma </strong>solidity ^0.4.23;</pre><pre><strong>contract </strong>Counter {<br>    <br>    <strong>event </strong>CounterIncrementedEvent(<strong>int </strong>count);<br>    <strong>event </strong>CounterDecrementedEvent(<strong>int </strong>count);</pre><pre>    <strong>int private </strong>count = 0;</pre><pre>    <strong>function </strong>incrementCounter() <strong>public </strong>{<br>        count += 1;<br>        <strong>emit </strong>CounterIncrementedEvent(count);<br>    }</pre><pre>    <strong>function </strong>decrementCounter() <strong>public </strong>{<br>        count -= 1;<br>        <strong>emit </strong>CounterDecrementedEvent(count);<br>    }</pre><pre>    <strong>function </strong>getCount() <strong>public constant returns </strong>(<strong>int</strong>) {<br>        <strong>return </strong>count;<br>    }<br>}</pre><p>3. <strong>Deployment Script </strong>— Add deployment script named as ‘2_initial_migration.js’ for Counter.sol</p><pre><strong>var </strong>Counter = artifacts.require(&quot;./Counter.sol&quot;);</pre><pre>module.exports = <strong>function</strong>(deployer) {<br>  deployer.deploy(Counter);<br>};</pre><p>4. <strong>Write Truffle Test cases: </strong>Create test file named as counter_test.js for Counter contract in test folder.</p><pre><strong>const </strong>Counter = artifacts.require(&quot;Counter&quot;);</pre><pre>contract(&#39;Counter test&#39;, <strong>async </strong>(accounts) =&gt; {<br>  <strong>let </strong>instance;</pre><pre>  before(<strong>async </strong>() =&gt; {<br>    instance = <strong>await </strong>Counter.deployed();//deploy contract<br>  });</pre><pre>  it(&quot;Initial value of counter should be zero&quot;, <strong>async </strong>() =&gt; {<br>    <strong>let </strong>count = <strong>await </strong>instance.getCount.call({from: accounts[0]});<br>    assert.equal(count, 0);<br>  });</pre><pre>});</pre><p>5.<strong> Running test cases</strong></p><ul><li><strong>Run ganache-cli</strong>: Run below command to run ganache</li></ul><p><strong><em>Ganache-cli</em></strong></p><ul><li><strong>Run truffle tests</strong>: In separate Window, run truffle test cases using below command.</li></ul><p><strong><em>truffle test</em></strong></p><p>Yayy!!! You have deployed and tested your first smart contract.</p><p>6. <strong>Add more tests:</strong></p><p>i. <strong>Add test for Increment Counter :</strong></p><pre>it(&quot;Should increment counter&quot;, <strong>async </strong>() =&gt; {<br>  <strong>await </strong>instance.incrementCounter({from: accounts[0]});<br>  <strong>let </strong>count = <strong>await </strong>instance.getCount.call({from: accounts[0]});<br>  assert.equal(count, 1);<br>});</pre><p>ii. <strong>Add test to verify if event is emitted on Increment Counter:</strong></p><pre>it(&quot;Should emit event on increment counter&quot;, <strong>async </strong>() =&gt; {<br>  <strong>let </strong>reciept = <strong>await </strong>instance.incrementCounter({from: accounts[0]});<br>  assert.equal(reciept.logs.length, 1);<br>  assert.equal(reciept.logs[0].args.count, 2);</pre><pre>});</pre><p>Now you can try adding tests for Decrement Counter :)</p><p>Full source code is available on Github: <a href="https://github.com/sarveshgs/firstBlockchainApp">sarveshgs/firstBlockchainApp</a></p><p><a href="https://github.com/sarveshgs/firstBlockchainApp">sarveshgs/firstBlockchainApp</a></p><p>In part 2 of this blog, I will demonstrate how to write interaction layer in node js to interact with smart contract using web3.</p><p><a href="http://demystifyblockchain.com/2018/06/28/getting-started-with-ethereum-blockchain-development/"><strong>Getting Started with ethereum blockchain development: Part-2</strong></a></p><p><a href="https://medium.com/coinmonks/getting-started-with-ethereum-blockchain-development-921eb42975e6">Getting Started with ethereum blockchain development</a></p><p>Happy Coding :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d6543b441bea" width="1" height="1" alt=""><hr><p><a href="https://medium.com/coinmonks/getting-started-with-ethereum-blockchain-development-part-1-d6543b441bea">Getting Started with ethereum blockchain development: part-1</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>
    </channel>
</rss>