subspace-site/getting-started.html

74 lines
31 KiB
HTML
Raw Normal View History

2020-01-17 22:18:10 +00:00
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Getting Started | SUBSPACE</title>
<meta name="description" content="Reactive ÐApp Development">
<link rel="apple-touch-icon" sizes="180x180" href="/assets/img/logo-small.png">
<link rel="icon" type="image/png" sizes="32x32" href="/assets/img/logo-small.png">
<link rel="icon" type="image/png" sizes="16x16" href="/assets/img/logo-small.png">
<link rel="shortcut icon" href="/assets/img/logo-small.png">
<meta name="theme-color" content="#ffffff">
2020-01-28 00:22:11 +00:00
<link rel="preload" href="/assets/css/0.styles.9a3d86bc.css" as="style"><link rel="preload" href="/assets/js/app.272ef0e5.js" as="script"><link rel="preload" href="/assets/js/2.ae31a69c.js" as="script"><link rel="preload" href="/assets/js/9.a48ad32f.js" as="script"><link rel="prefetch" href="/assets/js/10.3b1995f4.js"><link rel="prefetch" href="/assets/js/11.2a9f96fe.js"><link rel="prefetch" href="/assets/js/12.8f2e1280.js"><link rel="prefetch" href="/assets/js/13.c9d288f4.js"><link rel="prefetch" href="/assets/js/14.195a3661.js"><link rel="prefetch" href="/assets/js/15.50f3de3e.js"><link rel="prefetch" href="/assets/js/16.b2dc4bae.js"><link rel="prefetch" href="/assets/js/17.6e4d419c.js"><link rel="prefetch" href="/assets/js/18.7017f018.js"><link rel="prefetch" href="/assets/js/19.32de2763.js"><link rel="prefetch" href="/assets/js/3.13986f99.js"><link rel="prefetch" href="/assets/js/4.409ebe9b.js"><link rel="prefetch" href="/assets/js/5.3095c620.js"><link rel="prefetch" href="/assets/js/6.3def5758.js"><link rel="prefetch" href="/assets/js/7.b87cb3e3.js"><link rel="prefetch" href="/assets/js/8.37de77ca.js">
<link rel="stylesheet" href="/assets/css/0.styles.9a3d86bc.css">
2020-01-17 22:18:10 +00:00
</head>
<body>
2020-01-28 00:22:11 +00:00
<div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/assets/img/logo-small.png" alt="SUBSPACE" class="logo"> <span class="site-name can-hide">SUBSPACE</span></a> <div class="links"><!----> <nav class="nav-links can-hide"><div class="nav-item"><a href="/getting-started/" class="nav-link">Getting Started</a></div><div class="nav-item"><a href="/integrations-overview.html" class="nav-link">Integrations</a></div><div class="nav-item"><a href="/api.html" class="nav-link">API</a></div><div class="nav-item"><a href="https://github.com/status-im/subspace" target="_blank" rel="noopener noreferrer" class="nav-link external">
2020-01-17 22:18:10 +00:00
Github
2020-01-28 00:22:11 +00:00
<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/getting-started/" class="nav-link">Getting Started</a></div><div class="nav-item"><a href="/integrations-overview.html" class="nav-link">Integrations</a></div><div class="nav-item"><a href="/api.html" class="nav-link">API</a></div><div class="nav-item"><a href="https://github.com/status-im/subspace" target="_blank" rel="noopener noreferrer" class="nav-link external">
2020-01-17 22:18:10 +00:00
Github
2020-01-28 00:22:11 +00:00
<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav> <ul class="sidebar-links"><li><a href="/" class="sidebar-link">Home</a></li><li><a href="/how-it-works.html" class="sidebar-link">How it works?</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/getting-started.html" class="active sidebar-link">Getting Started</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/getting-started.html#installation" class="sidebar-link">Installation</a></li><li class="sidebar-sub-header"><a href="/getting-started.html#importing-the-library" class="sidebar-link">Importing the library</a></li><li class="sidebar-sub-header"><a href="/getting-started.html#connecting-to-a-web3-provider" class="sidebar-link">Connecting to a web3 provider</a></li><li class="sidebar-sub-header"><a href="/getting-started.html#reacting-to-data" class="sidebar-link">Reacting to data</a></li><li class="sidebar-sub-header"><a href="/getting-started.html#tracking-state" class="sidebar-link">Tracking state</a></li><li class="sidebar-sub-header"><a href="/getting-started.html#tracking-events" class="sidebar-link">Tracking events</a></li><li class="sidebar-sub-header"><a href="/getting-started.html#tracking-balances" class="sidebar-link">Tracking balances</a></li><li class="sidebar-sub-header"><a href="/getting-started.html#subscriptions" class="sidebar-link">Subscriptions</a></li><li class="sidebar-sub-header"><a href="/getting-started.html#cleanup" class="sidebar-link">Cleanup</a></li></ul></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>Integrations</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/integrations-overview.html" class="sidebar-link">Overview</a></li><li><a href="/react.html" class="sidebar-link">React</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/vue.html" class="sidebar-link">Vue</a><ul class="sidebar-sub-headers"></ul></li><li><section class="sidebar-group is-sub-group depth-1"><p class="sidebar-heading"><span>Redux</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/redux.html" class="sidebar-link">redux</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/redux-observable.html" class="sidebar-link">redux-observable</a></li></ul></section></li><li><a href="/reactive-graphql.html" class="sidebar-link">reactive-graphql</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/apollo-client.html" class="sidebar-link">apollo-client</a><ul class="sidebar-sub-headers"></ul></li></ul></section></li><li><a href="/tutorial.html" class="sidebar-link">Tutorial</a></li><li><a href="/api.html" class="sidebar-link">API</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/api.html#general" class="sidebar-link">General</a></li><li class="sidebar-sub-header"><a href="/api.html#contract-methods" class="sidebar-link">Contract methods</a></li><li class="sidebar-sub-header"><a href="/api.html#low-level-api-for-data-tracking" class="sidebar-link">Low level API for data tracking</a></li></ul></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="getting-started"><a href="#getting-started" aria-hidden="true" class="header-anchor">#</a> Getting Started</h1> <h2 id="installation"><a href="#installation" aria-hidden="true" class="header-anchor">#</a> Installation</h2> <p><strong>Subspace</strong> can be used in browser, node and native script environments. To get started install the package <code>@embarklabs/subspace</code> using <code>npm</code> or <code>yarn</code> by executing this command in your project directory:</p> <div class="language-bash line-numbers-mode"
<span class="token function">npm</span> <span class="token function">install</span> --save @embarklabs/subspace
2020-01-17 22:18:10 +00:00
<span class="token comment"># Using yarn</span>
2020-01-28 00:22:11 +00:00
<span class="token function">yarn</span> <span class="token function">add</span> @embarklabs/subspace
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h2 id="importing-the-library"><a href="#importing-the-library" aria-hidden="true" class="header-anchor">#</a> Importing the library</h2> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// ESM (might require babel / browserify)</span>
<span class="token keyword">import</span> Subspace <span class="token keyword">from</span> <span class="token string">'@embarklabs/subspace'</span><span class="token punctuation">;</span>
2020-01-17 22:18:10 +00:00
<span class="token comment">// CommonJS</span>
2020-01-28 00:22:11 +00:00
<span class="token keyword">const</span> Subspace <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'@embarklabs/subspace'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h2 id="connecting-to-a-web3-provider"><a href="#connecting-to-a-web3-provider" aria-hidden="true" class="header-anchor">#</a> Connecting to a web3 provider</h2> <p>To interact with the EVM, <strong>Subspace</strong> requires a valid Web3 provider.</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">const</span> subspace <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Subspace</span><span class="token punctuation">(</span>web3<span class="token punctuation">.</span>currentProvider<span class="token punctuation">)</span><span class="token punctuation">;</span>
2020-01-17 22:18:10 +00:00
<span class="token keyword">await</span> subspace<span class="token punctuation">.</span><span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
2020-01-28 00:22:11 +00:00
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>In addition to the provider, <code>Subspace</code> also accepts an <code>options</code> object with settings that can change its behavior:</p> <ul><li><code>dbFilename</code> - Name of the database where the information will be stored (default <code>'subspace.db'</code>)</li> <li><code>callInterval</code> - Interval of time in milliseconds to query a contract/address to determine changes in state or balance (default: <code>undefined</code>. Obtains data every block).</li> <li><code>disableSubscriptions</code> - <code>Subspace</code> by default will attempt to use websocket subscriptions if the current provider supports them, otherwise it will use polling because it asumes the provider is an <code>HttpProvider</code>. This functionality can be disabled by passing <code>true</code> to this option. (default: <code>false</code>)tionality can be forced by passing <code>true</code> to this option. (default: <code>undefined</code>)</li></ul> <h2 id="reacting-to-data"><a href="#reacting-to-data" aria-hidden="true" class="header-anchor">#</a> Reacting to data</h2> <p>Once it's initialized, you can use <strong>Subspace</strong>'s methods to track the contract state, events and balances. These functions return RxJS Observables which you can subscribe to, and obtain and transform the observed data via operators.</p> <div class="tip custom-block"><p class="custom-block-title">What is an Observable?</p> <p>The <code>Observable</code> type can be used to model push-based data sources such as DOM events, timer intervals, and sockets. In addition, observables are:</p> <ul><li>Compositional: Observables can be composed with higher-order combinators.</li> <li>Lazy: Observables do not start emitting data until an observer has subscribed.</li></ul></div> <h4 id="further-read"><a href="#further-read" aria-hidden="true" class="header-anchor">#</a> Further read</h4> <ul><li><a href="https://rxjs-dev.firebaseapp.com/guide/observable" target="_blank" rel="noopener noreferrer">RxJS Observables<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul> <h2 id="tracking-state"><a href="#tracking-state" aria-hidden="true" class="header-anchor">#</a> Tracking state</h2> <p>You can track changes to a contract state variable, by specifying the view function and arguments to call and query the contract.</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">const</span> stateObservable$ <span class="token operator">=</span> Contract<span class="token punctuation">.</span>methods<span class="token punctuation">.</span><span class="token function">functionName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">track</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><div class="tip custom-block"><p class="custom-block-title">Tracking the public variables of a contract</p> <p>State variables implicity create a <code>view</code> function when they're defined as <code>public</code>. The <code>functionName</code> would be the same as the variable name, and <code>functionArgs</code> would have a value when the type is a <code>mapping</code> or <code>array</code> (since these require an index value to query them).</p></div> <p>Example:</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">const</span> productTitle$ <span class="token operator">=</span> ProductList<span class="token punctuation">.</span>methods<span class="token punctuation">.</span><span class="token function">products</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">track</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token string">&quot;title&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
2020-01-17 22:18:10 +00:00
productTitle$<span class="token punctuation">.</span><span class="token function">subscribe</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">title</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;product title is &quot;</span> <span class="token operator">+</span> title<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
2020-01-28 00:22:11 +00:00
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>The subscriber will be triggered whenever the title changes</p> <h2 id="tracking-events"><a href="#tracking-events" aria-hidden="true" class="header-anchor">#</a> Tracking events</h2> <p>You can track events and react to their returned values.</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">const</span> eventObservable$ <span class="token operator">=</span> Contract<span class="token punctuation">.</span>event<span class="token punctuation">.</span><span class="token function">eventName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">track</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
2020-01-17 22:18:10 +00:00
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>Example:</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">const</span> rating$ <span class="token operator">=</span> Product<span class="token punctuation">.</span>events<span class="token punctuation">.</span><span class="token function">Rating</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">track</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token string">&quot;rating&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">pipe</span><span class="token punctuation">(</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token parameter">x</span> <span class="token operator">=&gt;</span> <span class="token function">parseInt</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
rating$<span class="token punctuation">.</span><span class="token function">subscribe</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">rating</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;rating received: &quot;</span> <span class="token operator">+</span> rating<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
2020-01-28 00:22:11 +00:00
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p><strong>Event Sourcing</strong></p> <p>You can easily do event sourcing with subspace.</p> <p>For e.g: if you needed to get the average rating of the last 5 events:</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> $average<span class="token punctuation">,</span> $latest <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">&quot;@embarklabs/subspace&quot;</span><span class="token punctuation">;</span>
2020-01-17 22:18:10 +00:00
<span class="token keyword">const</span> rating$ <span class="token operator">=</span> Product<span class="token punctuation">.</span>events<span class="token punctuation">.</span><span class="token function">Rating</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">track</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token string">&quot;rating&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">pipe</span><span class="token punctuation">(</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token parameter">x</span> <span class="token operator">=&gt;</span> <span class="token function">parseInt</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
rating$<span class="token punctuation">.</span><span class="token function">pipe</span><span class="token punctuation">(</span><span class="token function">$latest</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">$average</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">subscribe</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">rating</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;average rating of the last 5 events is &quot;</span> <span class="token operator">+</span> rating<span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
2020-01-28 00:22:11 +00:00
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h2 id="tracking-balances"><a href="#tracking-balances" aria-hidden="true" class="header-anchor">#</a> Tracking balances</h2> <p>You can also track changes in both ETH and ERC20 token balances for each mined block or time interval depending on the <code>callInterval</code> configured.</p> <p>Tracking ETH balance in an address:</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">const</span> address <span class="token operator">=</span> <span class="token string">&quot;0x0001020304050607080900010203040506070809&quot;</span><span class="token punctuation">;</span>
2020-01-17 22:18:10 +00:00
subspace<span class="token punctuation">.</span><span class="token function">trackBalance</span><span class="token punctuation">(</span>address<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">subscribe</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">balance</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;ETH balance is &quot;</span><span class="token punctuation">,</span> balance<span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>Tracking ETH balance in a Contract:</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>Contract<span class="token punctuation">.</span><span class="token function">trackBalance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">subscribe</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">balance</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;ETH balance is &quot;</span><span class="token punctuation">,</span> balance<span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>Tracking an ERC20 balance in a Contract:</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">const</span> tokenAddress <span class="token operator">=</span> <span class="token string">&quot;0x744d70fdbe2ba4cf95131626614a1763df805b9e&quot;</span><span class="token punctuation">;</span> <span class="token comment">// SNT Address</span>
<span class="token keyword">const</span> myBalanceObservable$ <span class="token operator">=</span> Contract<span class="token punctuation">.</span><span class="token function">trackBalance</span><span class="token punctuation">(</span>tokenAddress<span class="token punctuation">)</span><span class="token punctuation">;</span>
2020-01-28 00:22:11 +00:00
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><div class="warning custom-block"><p>Balances are returned as a string containing the value in <em>wei</em>.</p></div> <h2 id="subscriptions"><a href="#subscriptions" aria-hidden="true" class="header-anchor">#</a> Subscriptions</h2> <p>Once you have an <code>Observable</code>, you may receive a stream of data by creating a subscription. Subscriptions are triggered each time an observable emits a new value. These subscription receive a callback that must have a parameter which represents the value received from the observable (a contract state variable, an event, or the balance of an address); and they return an object representing the subscription.</p> <p>Subscriptions can be disposed by executing the method <code>unsubscribe()</code> liberating the resource held by it:</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">const</span> myBalanceObservable$ <span class="token operator">=</span> subspace<span class="token punctuation">.</span><span class="token function">trackBalance</span><span class="token punctuation">(</span>address<span class="token punctuation">,</span> tokenAddress<span class="token punctuation">)</span><span class="token punctuation">;</span>
2020-01-17 22:18:10 +00:00
<span class="token keyword">const</span> subscription <span class="token operator">=</span> myBalanceObservable$<span class="token punctuation">.</span><span class="token function">subscribe</span><span class="token punctuation">(</span><span class="token parameter">value</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;The balance is: &quot;</span><span class="token punctuation">,</span> value<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// ...</span>
subscription<span class="token punctuation">.</span><span class="token function">unsubscribe</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
2020-01-28 00:22:11 +00:00
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h4 id="further-read-2"><a href="#further-read-2" aria-hidden="true" class="header-anchor">#</a> Further read</h4> <ul><li><a href="https://rxjs-dev.firebaseapp.com/guide/subscription" target="_blank" rel="noopener noreferrer">RxJS Subscriptions<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul> <h2 id="cleanup"><a href="#cleanup" aria-hidden="true" class="header-anchor">#</a> Cleanup</h2> <p>If <strong>Subspace</strong> is not needed anymore, you need can invoke <code>close()</code> to dispose and perform the cleanup necessary to remove the internal subscriptions and interval timers created by <strong>Subspace</strong> during its normal execution, thus avoiding any potential memory leak.</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>subspace.close();
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><div class="warning custom-block"><p class="custom-block-title">What about subscriptions created with our observables?</p> <p>Any subscription created via the tracking methods must be unsubscribed manually (in the current version).</p></div></div> <footer class="page-edit"><!----> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
2020-01-17 22:18:10 +00:00
2020-01-28 00:22:11 +00:00
<a href="/how-it-works.html" class="prev">How it works?</a></span> <span class="next"><a href="/integrations-overview.html">Overview</a>
2020-01-17 22:18:10 +00:00
</span></p></div> </main></div><div class="global-ui"></div></div>
2020-01-28 00:22:11 +00:00
<script src="/assets/js/app.272ef0e5.js" defer></script><script src="/assets/js/2.ae31a69c.js" defer></script><script src="/assets/js/9.a48ad32f.js" defer></script>
2020-01-17 22:18:10 +00:00
</body>
</html>