UNPKG

@parity/light.js

Version:

A high-level reactive JS library optimized for light clients

1,042 lines (443 loc) 30.4 kB
<!DOCTYPE HTML> <html lang="" > <head> <meta charset="UTF-8"> <meta content="text/html; charset=utf-8" http-equiv="Content-Type"> <title>Tutorial Part 5: Work with Contracts · GitBook</title> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="description" content=""> <meta name="generator" content="GitBook 3.2.3"> <link rel="stylesheet" href="../gitbook/style.css"> <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css"> <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css"> <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css"> <meta name="HandheldFriendly" content="true"/> <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png"> <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon"> <link rel="next" href="tutorial6-integrate-with-react.html" /> <link rel="prev" href="tutorial4-send-a-transaction.html" /> </head> <body> <div class="book"> <div class="book-summary"> <div id="book-search-input" role="search"> <input type="text" placeholder="Type to search" /> </div> <nav role="navigation"> <ul class="summary"> <li class="chapter " data-level="1.1" data-path="../"> <a href="../"> Introduction </a> </li> <li class="chapter " data-level="1.2" > <span> Getting started </span> <ul class="articles"> <li class="chapter " data-level="1.2.1" data-path="../getting-started/installation.html"> <a href="../getting-started/installation.html"> Installation </a> </li> <li class="chapter " data-level="1.2.2" data-path="../getting-started/does-it-work-with-a-full-node.html"> <a href="../getting-started/does-it-work-with-a-full-node.html"> Does this work with a full node? </a> </li> </ul> </li> <li class="chapter " data-level="1.3" > <span> Guides </span> <ul class="articles"> <li class="chapter " data-level="1.3.1" data-path="tutorial1-set-up-a-light-client.html"> <a href="tutorial1-set-up-a-light-client.html"> Tutorial Part 1: Set up a Light Client </a> </li> <li class="chapter " data-level="1.3.2" data-path="tutorial2-our-first-rpcobservable.html"> <a href="tutorial2-our-first-rpcobservable.html"> Tutorial Part 2: Our First RpcObservable </a> </li> <li class="chapter " data-level="1.3.3" data-path="tutorial3-manipulating-rpcobservables.html"> <a href="tutorial3-manipulating-rpcobservables.html"> Tutorial Part 3: Manipulating RpcObservables </a> </li> <li class="chapter " data-level="1.3.4" data-path="tutorial4-send-a-transaction.html"> <a href="tutorial4-send-a-transaction.html"> Tutorial Part 4: Send a Transaction </a> </li> <li class="chapter active" data-level="1.3.5" data-path="tutorial5-work-with-contracts.html"> <a href="tutorial5-work-with-contracts.html"> Tutorial Part 5: Work with Contracts </a> </li> <li class="chapter " data-level="1.3.6" data-path="tutorial6-integrate-with-react.html"> <a href="tutorial6-integrate-with-react.html"> Tutorial Part 6: Integrate with React </a> </li> <li class="chapter " data-level="1.3.7" data-path="redux-integration.md"> <span> Redux Integration </a> </li> <li class="chapter " data-level="1.3.8" data-path="angular-integration.md"> <span> Angular Integration </a> </li> </ul> </li> <li class="chapter " data-level="1.4" > <span> Concepts </span> <ul class="articles"> <li class="chapter " data-level="1.4.1" data-path="../concepts/light-client-development.html"> <a href="../concepts/light-client-development.html"> Light Client Development </a> </li> <li class="chapter " data-level="1.4.2" data-path="../concepts/reactive-programming.html"> <a href="../concepts/reactive-programming.html"> Reactive Programming </a> </li> <li class="chapter " data-level="1.4.3" data-path="../concepts/rpc-observables.html"> <a href="../concepts/rpc-observables.html"> RpcObservables </a> </li> <li class="chapter " data-level="1.4.4" data-path="../concepts/rpc-observables-properties.html"> <a href="../concepts/rpc-observables-properties.html"> RpcObservables Properties </a> </li> </ul> </li> <li class="chapter " data-level="1.5" > <span> API </span> <ul class="articles"> <li class="chapter " data-level="1.5.1" data-path="../api/modules/_api_.html"> <a href="../api/modules/_api_.html"> api </a> </li> <li class="chapter " data-level="1.5.2" > <span> FrequencyObservables </span> <ul class="articles"> <li class="chapter " data-level="1.5.2.1" data-path="../api/modules/_frequency_accounts_.html"> <a href="../api/modules/_frequency_accounts_.html"> frequency/accounts </a> </li> <li class="chapter " data-level="1.5.2.2" data-path="../api/modules/_frequency_blocks_.html"> <a href="../api/modules/_frequency_blocks_.html"> frequency/blocks </a> </li> <li class="chapter " data-level="1.5.2.3" data-path="../api/modules/_frequency_frequency_.html"> <a href="../api/modules/_frequency_frequency_.html"> frequency/frequency </a> </li> <li class="chapter " data-level="1.5.2.4" data-path="../api/modules/_frequency_health_.html"> <a href="../api/modules/_frequency_health_.html"> frequency/health </a> </li> <li class="chapter " data-level="1.5.2.5" data-path="../api/modules/_frequency_other_.html"> <a href="../api/modules/_frequency_other_.html"> frequency/other </a> </li> <li class="chapter " data-level="1.5.2.6" data-path="../api/modules/_frequency_time_.html"> <a href="../api/modules/_frequency_time_.html"> frequency/time </a> </li> <li class="chapter " data-level="1.5.2.7" data-path="../api/modules/_frequency_utils_createpubsubobservable_.html"> <a href="../api/modules/_frequency_utils_createpubsubobservable_.html"> frequency/utils/createPubsubObservable </a> </li> </ul> </li> <li class="chapter " data-level="1.5.3" > <span> RpcObservables </span> <ul class="articles"> <li class="chapter " data-level="1.5.3.1" data-path="../api/modules/_rpc_eth_.html"> <a href="../api/modules/_rpc_eth_.html"> rpc/eth </a> </li> <li class="chapter " data-level="1.5.3.2" data-path="../api/modules/_rpc_net_.html"> <a href="../api/modules/_rpc_net_.html"> rpc/net </a> </li> <li class="chapter " data-level="1.5.3.3" data-path="../api/modules/_rpc_other_makecontract_.html"> <a href="../api/modules/_rpc_other_makecontract_.html"> rpc/other/makeContract </a> <ul class="articles"> <li class="chapter " data-level="1.5.3.3.1" data-path="../api/interfaces/_rpc_other_makecontract_.makecontract.html"> <a href="../api/interfaces/_rpc_other_makecontract_.makecontract.html"> MakeContract </a> </li> </ul> </li> <li class="chapter " data-level="1.5.3.4" data-path="../api/modules/_rpc_other_post_.html"> <a href="../api/modules/_rpc_other_post_.html"> rpc/other/post </a> <ul class="articles"> <li class="chapter " data-level="1.5.3.4.1" data-path="../api/interfaces/_rpc_other_post_.postoptions.html"> <a href="../api/interfaces/_rpc_other_post_.postoptions.html"> PostOptions </a> </li> </ul> </li> <li class="chapter " data-level="1.5.3.5" data-path="../api/modules/_rpc_parity_.html"> <a href="../api/modules/_rpc_parity_.html"> rpc/parity </a> </li> <li class="chapter " data-level="1.5.3.6" data-path="../api/modules/_rpc_rpc_.html"> <a href="../api/modules/_rpc_rpc_.html"> rpc/rpc </a> </li> <li class="chapter " data-level="1.5.3.7" data-path="../api/modules/_rpc_utils_createrpc_.html"> <a href="../api/modules/_rpc_utils_createrpc_.html"> rpc/utils/createRpc </a> </li> </ul> </li> <li class="chapter " data-level="1.5.4" data-path="../api/modules/_types_.html"> <a href="../api/modules/_types_.html"> Types </a> <ul class="articles"> <li class="chapter " data-level="1.5.4.1" data-path="../api/interfaces/_types_.frequencyobservable.html"> <a href="../api/interfaces/_types_.frequencyobservable.html"> FrequencyObservable </a> </li> <li class="chapter " data-level="1.5.4.2" data-path="../api/interfaces/_types_.frequencyobservableoptions.html"> <a href="../api/interfaces/_types_.frequencyobservableoptions.html"> FrequencyObservableOptions </a> </li> <li class="chapter " data-level="1.5.4.3" data-path="../api/interfaces/_types_.makecontract.html"> <a href="../api/interfaces/_types_.makecontract.html"> MakeContract </a> </li> <li class="chapter " data-level="1.5.4.4" data-path="../api/interfaces/_types_.metadata.html"> <a href="../api/interfaces/_types_.metadata.html"> Metadata </a> </li> <li class="chapter " data-level="1.5.4.5" data-path="../api/interfaces/_types_.rpcobservable.html"> <a href="../api/interfaces/_types_.rpcobservable.html"> RpcObservable </a> </li> <li class="chapter " data-level="1.5.4.6" data-path="../api/interfaces/_types_.rpcobservableoptions.html"> <a href="../api/interfaces/_types_.rpcobservableoptions.html"> RpcObservableOptions </a> </li> <li class="chapter " data-level="1.5.4.7" data-path="../api/interfaces/_types_.txstatus.html"> <a href="../api/interfaces/_types_.txstatus.html"> TxStatus </a> </li> </ul> </li> <li class="chapter " data-level="1.5.5" > <span> Utils </span> <ul class="articles"> <li class="chapter " data-level="1.5.5.1" data-path="../api/modules/_utils_isloading_.md"> <span> utils/isLoading </a> </li> <li class="chapter " data-level="1.5.5.2" data-path="../api/modules/_utils_isobservable_.html"> <a href="../api/modules/_utils_isobservable_.html"> utils/isObservable </a> </li> <li class="chapter " data-level="1.5.5.3" data-path="../api/modules/_utils_operators_distinctreplayrefcount_.md"> <span> utils/operators/distinctReplayRefCount </a> </li> <li class="chapter " data-level="1.5.5.4" data-path="../api/modules/_utils_operators_distinctvalues_.html"> <a href="../api/modules/_utils_operators_distinctvalues_.html"> utils/operators/distinctValues </a> </li> <li class="chapter " data-level="1.5.5.5" data-path="../api/modules/_utils_operators_switchmappromise_.html"> <a href="../api/modules/_utils_operators_switchmappromise_.html"> utils/operators/switchMapPromise </a> </li> <li class="chapter " data-level="1.5.5.6" data-path="../api/modules/_utils_testhelpers_mockapi_.html"> <a href="../api/modules/_utils_testhelpers_mockapi_.html"> utils/testHelpers/mockApi </a> </li> <li class="chapter " data-level="1.5.5.7" data-path="../api/modules/_utils_testhelpers_mockrpc_.html"> <a href="../api/modules/_utils_testhelpers_mockrpc_.html"> utils/testHelpers/mockRpc </a> </li> </ul> </li> </ul> </li> <li class="divider"></li> <li> <a href="https://www.gitbook.com" target="blank" class="gitbook-link"> Published with GitBook </a> </li> </ul> </nav> </div> <div class="book-body"> <div class="body-inner"> <div class="book-header" role="navigation"> <!-- Title --> <h1> <i class="fa fa-circle-o-notch fa-spin"></i> <a href=".." >Tutorial Part 5: Work with Contracts</a> </h1> </div> <div class="page-wrapper" tabindex="-1" role="main"> <div class="page-inner"> <div id="book-search-results"> <div class="search-noresults"> <section class="normal markdown-section"> <h1 id="tutorial-part-5-work-with-contracts">Tutorial Part 5: Work with Contracts</h1> <p>Contract API basically comes in three pieces:</p> <ul> <li>there is inspection of the contract state through calling constant functions.</li> <li>there&apos;s state-changing transactions like transferring tokens to a counter-party.</li> <li>there&apos;s event reception and reporting that (usually) happen when such a state change occurs</li> </ul> <p>As of version v1.0.4 of <code>@parity/light.js</code>, the library only supports the two first functionalities. Let&apos;s see how they work. But before that, let&apos;s create a contract object.</p> <h2 id="our-first-contract">Our first contract</h2> <p>Let&apos;s deal with the ERC20 contract, which is quite possible the most famous contract. We&apos;ll use the <code>GavCoin</code> contract, a test ERC20 contract deployed on Kovan at the address <code>0x4733659a5cB7896A65c918Add6f59C5148FB5ffa</code>.</p> <p>The ABI of a classic ERC20 can be found in a lot of places, for example within the <a href="https://github.com/paritytech/js-libs/tree/master/packages/contracts" target="_blank"><code>@parity/contracts</code></a> npm module.</p> <p>So let&apos;s instantiate a contract object:</p> <pre><code class="lang-javascript"><span class="hljs-keyword">import</span> erc20Abi <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;@parity/contracts/lib/abi/eip20&apos;</span>; <span class="hljs-keyword">import</span> { makeContract } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;@parity/light.js&apos;</span>; <span class="hljs-keyword">const</span> gavcoinContract = makeContract( <span class="hljs-string">&apos;0x4733659a5cB7896A65c918Add6f59C5148FB5ffa&apos;</span>, erc20Abi ); </code></pre> <p>And done! <code>gavcoinContract</code> is now a javascript object, where all the keys are the functions in the contract with a <code>$</code> sign at the end:</p> <pre><code class="lang-javascript">{ abi: {...}, <span class="hljs-comment">// @parity/abi Abi object, only needed for advanced usage</span> address: <span class="hljs-string">&apos;0x4733659a5cB7896A65c918Add6f59C5148FB5ffa&apos;</span>, allowance$(): ..., approve$(): ..., balanceOf$(): ..., contractObject: {...}, <span class="hljs-comment">// @parity/api Contract object, only needed for advanced usage</span> totalSupply$(): ..., transfer$(): ..., transferFrom$(): ... } </code></pre> <p>The functions defined in the ABI are defined in this object too. We won&apos;t go into details about the <code>abi</code> and <code>contractObject</code> fields, but have a look at <a href="https://github.com/paritytech/js-libs/tree/master/packages/abi" target="_blank"><code>@parity/abi</code></a> and <a href="https://github.com/paritytech/js-libs/tree/master/packages/api" target="_blank"><code>@parity/api</code></a> respectively if interested.</p> <h2 id="calling-constant-functions">Calling constant functions</h2> <p>In the ERC20 ABI, the <code>balanceOf</code> function is a constant function, which means that it doesn&apos;t modify the Ethereum state. Calling such a function with return a RpcObservable that will update when the token balance of a account changes.</p> <pre><code class="lang-javascript">gavcoinContract .balanceOf$(<span class="hljs-string">&apos;0x407d73d8a49eeb85d32cf465507dd71d507100c1&apos;</span>) .subscribe(balance =&gt; <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;balance&apos;</span>, balance)); </code></pre> <p>See this snippet in action: <a href="https://codesandbox.io/s/mq8rwkqzxp" target="_blank">https://codesandbox.io/s/mq8rwkqzxp</a>.</p> <h2 id="calling-state-changing-function">Calling state-changing function</h2> <p>The usage is exactly the same as with constant functions, but the returned RpcObservable is the same as the one returned by <code>post$</code> (see <a href="tutorial4-send-a-transaction.html">previous chapter</a>).</p> <pre><code class="lang-javascript">gavcoinContract .transfer$( [ <span class="hljs-string">&apos;0x407d73d8a49eeb85d32cf465507dd71d507100c1&apos;</span>, <span class="hljs-comment">// The &quot;to&quot; address</span> <span class="hljs-keyword">new</span> BigNumber(<span class="hljs-number">2.01</span>) <span class="hljs-comment">// The amount to transfer</span> ], <span class="hljs-comment">// This 2nd argument is an options object containing the following fields:</span> { <span class="hljs-keyword">from</span>: <span class="hljs-string">&quot;0x...&quot;</span>, passphrase: <span class="hljs-string">&quot;mypassphrase&quot;</span>, gasPrice: ... } ) .subscribe(transfer =&gt; <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;transfer$&apos;</span>, transfer)); <span class="hljs-comment">// Logs:</span> <span class="hljs-comment">// transfer$ { signed: &apos;0x123...ff&apos; }</span> <span class="hljs-comment">// transfer$ { sent: &apos;0x456...ff&apos; }</span> <span class="hljs-comment">// transfer$ { confirmed: {/* receipt object */} }</span> </code></pre> </section> </div> <div class="search-results"> <div class="has-results"> <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1> <ul class="search-results-list"></ul> </div> <div class="no-results"> <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1> </div> </div> </div> </div> </div> </div> <a href="tutorial4-send-a-transaction.html" class="navigation navigation-prev " aria-label="Previous page: Tutorial Part 4: Send a Transaction"> <i class="fa fa-angle-left"></i> </a> <a href="tutorial6-integrate-with-react.html" class="navigation navigation-next " aria-label="Next page: Tutorial Part 6: Integrate with React"> <i class="fa fa-angle-right"></i> </a> </div> <script> var gitbook = gitbook || []; gitbook.push(function() { gitbook.page.hasChanged({"page":{"title":"Tutorial Part 5: Work with Contracts","level":"1.3.5","depth":2,"next":{"title":"Tutorial Part 6: Integrate with React","level":"1.3.6","depth":2,"path":"guides/tutorial6-integrate-with-react.md","ref":"guides/tutorial6-integrate-with-react.md","articles":[]},"previous":{"title":"Tutorial Part 4: Send a Transaction","level":"1.3.4","depth":2,"path":"guides/tutorial4-send-a-transaction.md","ref":"guides/tutorial4-send-a-transaction.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"guides/tutorial5-work-with-contracts.md","mtime":"2020-02-10T09:51:45.380Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-02-10T09:56:25.139Z"},"basePath":"..","book":{"language":""}}); }); </script> </div> <script src="../gitbook/gitbook.js"></script> <script src="../gitbook/theme.js"></script> <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script> <script src="../gitbook/gitbook-plugin-search/search.js"></script> <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script> <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script> <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script> <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script> </body> </html>