<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>EOSIO &#8211; Deqode Blog</title>
	<atom:link href="https://deqode.com/blog/tag/eosio/feed/" rel="self" type="application/rss+xml" />
	<link>https://deqode.com/blog</link>
	<description>Articles about blockchain and the future of technology</description>
	<lastBuildDate>Tue, 22 Oct 2024 13:32:34 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.0.3</generator>

<image>
	<url>https://deqode.com/blog/wp-content/uploads/2019/02/cropped-favicon-32x32.png</url>
	<title>EOSIO &#8211; Deqode Blog</title>
	<link>https://deqode.com/blog</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>A Game of Chains: Guide to Smart Contracts on EOSIO</title>
		<link>https://deqode.com/blog/2019/02/12/eosio-smart-contracts/</link>
		<comments>https://deqode.com/blog/2019/02/12/eosio-smart-contracts/#respond</comments>
		<pubDate>Tue, 12 Feb 2019 16:48:35 +0000</pubDate>
		<dc:creator><![CDATA[Akhil Tiwari]]></dc:creator>
				<category><![CDATA[Tutorials and Guides]]></category>
		<category><![CDATA[blockchain]]></category>
		<category><![CDATA[blockchain technology]]></category>
		<category><![CDATA[EOSIO]]></category>

		<guid isPermaLink="false">https://deqode.com/blog/?p=3679</guid>
		<description><![CDATA[<p>EOSIO is a​ blockchain​ protocol powered by its native​ cryptocurrency​ EOS. The protocol emulates most of the attributes of a real computer including hardware (CPU(s) and GPU​(s) for processing, local/​ RAM​ memory, hard-disk storage) with the computing resources distributed equally among EOS cryptocurrency holders. EOSIO operates as a ​smart contract​ platform and decentralized operating system [...]</p>
<p>The post <a rel="nofollow" href="https://deqode.com/blog/2019/02/12/eosio-smart-contracts/">A Game of Chains: Guide to Smart Contracts on EOSIO</a> appeared first on <a rel="nofollow" href="https://deqode.com/blog">Deqode Blog</a>.</p>
]]></description>
				<content:encoded><![CDATA[
<p class="has-drop-cap">EOSIO is a​ blockchain​ protocol powered by its native​ cryptocurrency​ EOS. The protocol emulates most of the attributes of a real computer including hardware (CPU(s) and GPU​(s) for processing, local/​ RAM​ memory, hard-disk storage) with the computing resources distributed equally among EOS cryptocurrency holders. EOSIO operates as a ​smart contract​ platform and decentralized operating system intended for the deployment of industrial-scale decentralized applications through a ​ decentralized autonomous corporation​ model. </p>



<h4><g class="gr_ gr_3 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="3" data-gr-id="3">EOSIO</g> is an open source blockchain protocol. So, individuals and organizations can use it to set up their own blockchain networks.</h4>



<figure class="wp-block-image"><img src="https://deqode.com/blog/wp-content/uploads/1-1024x640.png" alt="" class="wp-image-3855" srcset="https://deqode.com/blog/wp-content/uploads/1-1024x640.png 1024w, https://deqode.com/blog/wp-content/uploads/1-300x188.png 300w, https://deqode.com/blog/wp-content/uploads/1-768x480.png 768w, https://deqode.com/blog/wp-content/uploads/1.png 1440w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>The first step is to clone eosio from <a href="https://github.com/EOSIO/eos" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)">https://github.com/EOSIO/eos</a> with –recursive flag.</p>



<p>Now, we&#8217;ll build EOSIO. EOSIO community gives an automated build script for the following environments:</p>



<ul><li>Amazon 2017.09 and higher</li><li>Centos 7</li><li>Fedora 25 and higher (Fedora 27 recommended)</li><li>Mint 18</li><li>Ubuntu 16.04 (Ubuntu 16.10 recommended)</li><li>Ubuntu 18.04</li><li>MacOS Darwin 10.12 and higher (MacOS 10.13.x recommended)</li></ul>



<p><g class="gr_ gr_89 gr-alert gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling ins-del multiReplace" id="89" data-gr-id="89">EOSIO</g> smart contracts are, usually, coded on C++. So, you have to specify a header file with the declaration of functions (that correspond to smart contract actions) and a .cpp source file with their definitions. This leads to the creation of .abi (Application Binary Interface), which holds all the actions that the smart contract has. In addition, external systems need this file for executing the smart contract operations.</p>



<h2><strong>EOSIOCPP</strong></h2>



<p><g class="gr_ gr_3 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="3" data-gr-id="3">EOSIO</g> provides and recommends a compilation tool called <g class="gr_ gr_4 gr-alert gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling" id="4" data-gr-id="4">eosiocpp</g>. <br>for the compilation of the smart contracts.</p>



<p><g class="gr_ gr_3 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="3" data-gr-id="3">EOSIO</g> ships with its standard c++ libraries, i.e., when you clone the source, you will notice a folder: {$PROJECT_ROOT}/contracts/libc++ ⇒ This directory has all the standard c++ library headers and this is where <g class="gr_ gr_4 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" id="4" data-gr-id="4">eosiocpp</g> looks for the included standard header files.</p>



<h2><strong>A detour to .wasm</strong></h2>



<p><g class="gr_ gr_3 gr-alert gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling ins-del multiReplace" id="3" data-gr-id="3">WebAssembly</g> (abbreviated Wasm) is a binary instruction format for a stack-based virtual machine, and has the following features:</p>



<ul><li>Designed as a portable target, high-level languages like C/C++/Rust use <g class="gr_ gr_167 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" id="167" data-gr-id="167">Wasm  for</g> compilation. It enables deployment on the web for client and server applications. </li><li>Secondly, it enables executing code nearly as fast as running native&nbsp;machine code. </li><li>Thirdly, it speeds up the performance of critical parts of web applications by complementing&nbsp;JavaScript, and so, enables&nbsp;web development&nbsp;in languages other than JavaScript.</li><li>Engineers from&nbsp;Mozilla,&nbsp;Microsoft,&nbsp;Google, and&nbsp;Apple develop Wasm at the&nbsp;World Wide Web Consortium&nbsp;(W3C).</li></ul>



<p><a href="https://webassembly.org" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)">Click here</a> to find out more about Wasm.&nbsp;</p>



<h2><strong>EOSIO smart contracts &#8211; a brief overview</strong></h2>



<p>If you have clicked on this article, you probably know a bit about smart contracts, but let us quickly establish a definition to proceed. Smart contracts are self-executing contracts. Here the buyer and seller &#8216;code&#8217; the terms of the agreement on <g class="gr_ gr_602 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Grammar only-ins replaceWithoutSep" id="602" data-gr-id="602">blockchain</g>. The code and the agreements contained therein exist across a distributed, decentralized&nbsp;blockchain&nbsp;network.&nbsp;Smart contracts allow trusted transactions and agreements among disparate, anonymous parties without the need for a central authority, legal system, or external enforcement mechanism. So, they render transactions traceable, transparent, and irreversible.</p>



<p>There are a couple of preliminary things that need to be carried on before we jump on the deploying our escrow smart contract. The official documentation page for EOSIO has this in a very clear and precise manner.</p>



<p>Suggested reading: <a href="https://deqode.com/blog/graphene-blockchain-tutorial-two/" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)">Multi-Node Setup and Smart Contracts in Graphene</a> </p>



<h2><strong>Escrow Smart Contract</strong></h2>



<p>An escrow is basically a buffer where the token (currency) involved in the transaction is temporarily held. It is held till we know that the transaction can be completed without dispute. So here, the operation under focus for this article is <g class="gr_ gr_12 gr-alert gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling ins-del multiReplace" id="12" data-gr-id="12">estransfer</g> ( i.e escrow transfer). This is the operation used to transfer the fund from the sender to the escrow. It is complemented by <g class="gr_ gr_102 gr-alert gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling ins-del multiReplace" id="102" data-gr-id="102">esrelease</g> which releases the funds from the escrow to the recipient.</p>



<p>First, define the class for your smart contract in the header file.</p>


<p>
<script src="https://gist.github.com/akhiltiwari-tr/4d4a3f5cf555d317a117190cc4ede3ea.js"></script>
</p>


<p>Now, all the member functions corresponding to the actions should be made public. The .abi file of the smart contracts should&nbsp;have their declaration.</p>



<p>Since our <g class="gr_ gr_6 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Punctuation only-ins replaceWithoutSep" id="6" data-gr-id="6">escrow</g> smart contracts also issues the tokens needed for transactions it needs to have a table to store the data corresponding to the issued token and the balance for <g class="gr_ gr_4 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del" id="4" data-gr-id="4">any</g> account of that token. This is done using the multi_index container provided by the <g class="gr_ gr_5 gr-alert gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling ins-del multiReplace" id="5" data-gr-id="5">EOSIO</g> library (which is <g class="gr_ gr_7 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling multiReplace" id="7" data-gr-id="7">modelled</g> after boost::multi_index_container) as follows:</p>


<p><script src="https://gist.github.com/akhiltiwari-tr/e76ee2802c92504e09f63c9d82034dfc.js"></script></p>


<h4><strong>Tables:</strong></h4>



<p>The two tables defined are accounts and stat. Firstly, different account objects make up the accounts table. Each <g class="gr_ gr_13 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" id="13" data-gr-id="13">hold</g> the balance for a different token. Secondly, t<g class="gr_ gr_93 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling multiReplace" id="93" data-gr-id="93">he stat</g> table is made up of currency_stats objects (defined by struct currency_stats), that holds a supply, a max_supply, and an issuer. Before moving on further, it is important to note that this contract will hold data into two different scopes. The accounts table is scoped to an <g class="gr_ gr_10 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="10" data-gr-id="10">EOSIO</g> account, and the <g class="gr_ gr_9 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del" id="9" data-gr-id="9">stat</g> table is scoped to a token symbol name.</p>



<p>According to the ‘eosio::multi_index’ definitions,&nbsp;<code>code</code>&nbsp;is the name of the account which has write permission and the<code>scope</code>&nbsp;is the account where the data gets stored.</p>



<h4>Scope</h4>



<p>The scope is essentially a way to compartmentalize data in a contract so that it is only accessible within a defined space. The accounts table uses EOSIO account as the scope of the token contract The accounts table is a multi-index container which holds multiple account objects. Token symbol indexes every account object which contains the token balance. When you query a users&#8217; accounts table using their scope it returns a list of all the tokens that the user has an existing balance.</p>



<p>This is our smart contract’s header file.</p>


<p><script src="https://gist.github.com/akhiltiwari-tr/fb4d335d8aff1664922905bc928e2c8f.js"></script></p>


<h4><strong>.cpp implementation</strong></h4>



<p>The .cpp source file has the actions implemented. Definition of our estransfer action starts off with some assertion checks:</p>



<p><script src="https://gist.github.com/akhiltiwari-tr/807900639328a29738c1f9374896cf28.js"></script></p>



<p>Firstly, we assert checks if the sender and receiver accounts are not same, then we check that the transaction has an authority of the sender, authority for a transaction can be specified by using -p flag with cleos.</p>



<p>Next, we fetch the table corresponding to the current token.</p>



<p><script src="https://gist.github.com/akhiltiwari-tr/49c03846fb31800ceaf1e165b28b61ad.js"></script></p>



<p>These lines notify the specified accounts after a successful transaction</p>



<p><script src="https://gist.github.com/akhiltiwari-tr/e65a962074f9a3ddd24a16fc3b08a0c2.js"></script></p>



<p>A few more checks follow this:</p>



<p><script src="https://gist.github.com/akhiltiwari-tr/6b9d3e180b1545f5e4552ed9afcfe874.js"></script></p>



<p>Finally, we call the two most important utility functions, add_balance, and sub_balance, the former adds the tokens to a specified account and the later deducts it.</p>


<p><script src="https://gist.github.com/akhiltiwari-tr/15aa69e85246a835ff4c757f6ee97a92"></script></p>


<h4>Let’s look at sub_balance in a little more detail</h4>



<p><script src="https://gist.github.com/akhiltiwari-tr/f3a28ee316eaecd7b58d1d7d9b6a506d.js"></script></p>



<p>First, we fetch the table of accounts objects, the account <g class="gr_ gr_216 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Grammar multiReplace" id="216" data-gr-id="216">being</g> that of the sender in <g class="gr_ gr_8 gr-alert gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling ins-del multiReplace" id="8" data-gr-id="8">estransfer</g>. A few minor checks follow this. If all checks pass, you should modify the account’s balance by subtracting the value to be transferred. Once, the number of tokens to be transferred are equal to the total available tokens in the accounts, we essentially delete the entry for the sender account from the accounts table.<br>Here is our escrow.<g class="gr_ gr_7 gr-alert gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling ins-del multiReplace" id="7" data-gr-id="7">cpp</g> file in entirety.</p>



<p><script src="https://gist.github.com/akhiltiwari-tr/ea0c9bf16b4bde2df04dd2bcd8d0d898.js"></script></p>



<p>So, this is the structure of the ABI file that documents the actions of the smart contract.</p>



<p><script src="https://gist.github.com/akhiltiwari-tr/d657b7e41b2c6e3d89faa8d437fee936.js"></script></p>



<p>Here is the escrow.abi file:</p>



<p><script src="https://gist.github.com/akhiltiwari-tr/334fbf74f8d752f71ec9cd307425ecba.js"></script></p>



<p>Working with our escrow smart contract using cleos (command line utility as provided by EOSIO).</p>



<h6>First, we need to set the contract and create a token</h6>



<figure class="wp-block-image"><img src="https://deqode.com/blog/wp-content/uploads/2-1024x196.png" alt="" class="wp-image-3847" srcset="https://deqode.com/blog/wp-content/uploads/2-1024x196.png 1024w, https://deqode.com/blog/wp-content/uploads/2-300x57.png 300w, https://deqode.com/blog/wp-content/uploads/2-768x147.png 768w, https://deqode.com/blog/wp-content/uploads/2.png 1406w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>Now, we can check at the moment if any account has some token balance for the token we just created, and the result should not surprise you.</p>



<figure class="wp-block-image"><img src="https://deqode.com/blog/wp-content/uploads/3-1024x98.png" alt="" class="wp-image-3848" srcset="https://deqode.com/blog/wp-content/uploads/3.png 1024w, https://deqode.com/blog/wp-content/uploads/3-300x29.png 300w, https://deqode.com/blog/wp-content/uploads/3-768x74.png 768w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>Post the setting of escrow smart contract and the creation of token balance, we can push the action for issuing this token to some user “ned”.</p>



<figure class="wp-block-image"><img src="https://deqode.com/blog/wp-content/uploads/4-1024x96.png" alt="" class="wp-image-3850" srcset="https://deqode.com/blog/wp-content/uploads/4-1024x96.png 1024w, https://deqode.com/blog/wp-content/uploads/4-300x28.png 300w, https://deqode.com/blog/wp-content/uploads/4-768x72.png 768w, https://deqode.com/blog/wp-content/uploads/4.png 1215w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>Finally, when we check the account for “ned” we can see the token balance for this account</p>



<figure class="wp-block-image"><img src="https://deqode.com/blog/wp-content/uploads/5-1024x108.png" alt="" class="wp-image-3851" srcset="https://deqode.com/blog/wp-content/uploads/5-1024x108.png 1024w, https://deqode.com/blog/wp-content/uploads/5-300x32.png 300w, https://deqode.com/blog/wp-content/uploads/5-768x81.png 768w, https://deqode.com/blog/wp-content/uploads/5.png 1436w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<h6>We are in a situation to transfer funds from “ned” to “jon” (R+L=J)</h6>



<figure class="wp-block-image"><img src="https://deqode.com/blog/wp-content/uploads/6-1024x210.png" alt="" class="wp-image-3852" srcset="https://deqode.com/blog/wp-content/uploads/6-1024x210.png 1024w, https://deqode.com/blog/wp-content/uploads/6-300x62.png 300w, https://deqode.com/blog/wp-content/uploads/6-768x158.png 768w, https://deqode.com/blog/wp-content/uploads/6.png 1330w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>Now, we have used the account “escrow” with which we had deployed the smart contract as our Escrow for this transaction.</p>



<p>Then, we can check the balance for “escrow”</p>



<figure class="wp-block-image"><img src="https://deqode.com/blog/wp-content/uploads/7.png" alt="" class="wp-image-3853" srcset="https://deqode.com/blog/wp-content/uploads/7.png 906w, https://deqode.com/blog/wp-content/uploads/7-300x50.png 300w, https://deqode.com/blog/wp-content/uploads/7-768x128.png 768w, https://deqode.com/blog/wp-content/uploads/7-900x151.png 900w" sizes="(max-width: 906px) 100vw, 906px" /></figure>



<p>Finally, this is the final table structure after pushing the release action.</p>



<figure class="wp-block-image"><img src="https://deqode.com/blog/wp-content/uploads/8.png" alt="" class="wp-image-3854" srcset="https://deqode.com/blog/wp-content/uploads/8.png 1014w, https://deqode.com/blog/wp-content/uploads/8-300x79.png 300w, https://deqode.com/blog/wp-content/uploads/8-768x201.png 768w" sizes="(max-width: 1014px) 100vw, 1014px" /></figure>



<p>That&#8217;s the end of this tutorial. For any doubts, please email the author at aktiwari@deqode.com. For developing an application based on <g class="gr_ gr_5 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="5" data-gr-id="5">EOSIO</g>, <a href="https://deqode.com/contact" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)">please drop your email here</a>.</p>



<p>Must read: <a href="https://deqode.com/blog/blockchain-scalability-one/" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)">Understanding Blockchain, Its Scalability Issues, and their Solutions</a></p>



<h2>References</h2>



<p>One stop developer’s doc: <a href="https://developers.eos.io/" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)">https://developers.eos.io/</a></p>



<p>Resource for pushing actions using <g class="gr_ gr_3 gr-alert gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling" id="3" data-gr-id="3">eosjs</g> lib: <a href="https://steemit.com/devs/@eos-asia/eos-smart-contracts-part-1-getting-started-ping-equivalent-in-eos" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)">Steemit</a><br></p>
<p>The post <a rel="nofollow" href="https://deqode.com/blog/2019/02/12/eosio-smart-contracts/">A Game of Chains: Guide to Smart Contracts on EOSIO</a> appeared first on <a rel="nofollow" href="https://deqode.com/blog">Deqode Blog</a>.</p>
]]></content:encoded>
			<wfw:commentRss>https://deqode.com/blog/2019/02/12/eosio-smart-contracts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/


Served from: blog.deqode.com @ 2026-10-18 01:32:40 by W3 Total Cache
-->