@parity/light.js
Version:
A high-level reactive JS library optimized for light clients
1,024 lines (425 loc) • 29.9 kB
HTML
<html lang="" >
<head>
<meta charset="UTF-8">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>Tutorial Part 4: Send a Transaction · 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="tutorial5-work-with-contracts.html" />
<link rel="prev" href="tutorial3-manipulating-rpcobservables.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 active" 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 " 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 4: Send a Transaction</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-4-send-a-transaction">Tutorial Part 4: Send a Transaction</h1>
<p>So far we have only seen RpcObservables that read data from the blockchain, we have not yet had any impact on the blockchain's state - we have not yet sent a transaction.</p>
<h2 id="the-post-rpcobservable">The <code>post$</code> RpcObservable</h2>
<h3 id="arguments">Arguments</h3>
<p>Creating a new transaction is a simple affair. The function <code>post$</code> takes as first argument an object describing the transaction. There are several keys it may contain:</p>
<ul>
<li><code>to</code> The recipient of the transaction; undefined or null indicates this is a contract-creation transaction.</li>
<li><code>from</code> The sender of the transaction; must be an account which the user controls. Will default to bonds.me.</li>
<li><code>value</code> The amount of ether to send to the recipient or endow any created contract.</li>
<li><code>condition</code> A condition on which to predicate the distribution, but not the approval/signing, of the transaction. This is an object which contains one of two keys: block (which dictates the minimum block number before distribution) and time (the same but for block timestamp).</li>
<li><code>gas</code> The amount of gas to supply with the transaction. By default, it will attempt to estimate the amount of gas to supply. Such that the transaction succeeds without exception/error.</li>
<li><code>gasPrice</code> The price (in Wei) per unit of gas. Will default to something sensible (in Parity's case, this is the median of the distribution formed from the last several hundred transactions).</li>
<li><code>nonce</code> The sender nonce of the transaction. Will default to the latest known value.</li>
</ul>
<p>The second argument describes options to pass to this function. It is an object with the following keys:</p>
<ul>
<li><code>passphrase</code>: The Parity Ethereum passphrase for your <code>from</code> account. If you prefer to sign the transaction yourself, you can use the <code>postRaw$</code> function instead.</li>
<li><code>estimate</code> (optional): Will estimate the amount of gas required for this transaction first.</li>
</ul>
<h3 id="return-value">Return Value</h3>
<p>The value to which the RpcObservable evaluates reflects the ongoing status of the transaction as it moves through the process of getting finalised. It is always an object with a single key/value. It can be:</p>
<ul>
<li><code>{"estimating": true}</code> The amount of gas required for this transaction to execute is being determined (only fired if <code>estimate</code> is set to <code>true</code>).</li>
<li><code>{"estimated": value}</code> The amount of gas required for this transaction to execute has been determined as value; the user is about to be asked (only fired if <code>estimate</code> is set to <code>true</code>).</li>
<li><code>{"signed": hash}</code> The transaction has been signed with the given passphrase. It is now ready to be sent to the network for inclusion in a new block.</li>
<li><code>{"sent": hash}</code> This is the hash of the transaction.</li>
<li><code>{"confirmed": receipt}</code> The transaction has been confirmed into a block. The receipt of the transaction is provided as receipt, giving information concerning its operation, including any logs.</li>
<li><code>{"failed": error}</code> The transaction has failed. Generally this is because the user did not approve the transaction or otherwise signing could not take place. error is a string which contains any details regarding the situation.</li>
</ul>
<h3 id="example">Example</h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> { post$ } <span class="hljs-keyword">from</span> <span class="hljs-string">'@parity/light.js'</span>;
post$({
<span class="hljs-keyword">from</span>: <span class="hljs-string">'0x921ceff422ef827110ac9dde154fbae2ac4eec9d'</span>,
to: <span class="hljs-string">'0x180fbce524fd79b4af8dccf83809acd9bc95fd1a'</span>,
value: <span class="hljs-number">100</span> * <span class="hljs-number">1e15</span> <span class="hljs-comment">// value in wei</span>
}, {
passphrase: <span class="hljs-string">'mypassphrase'</span>
}).subscribe(<span class="hljs-built_in">console</span>.log);
<span class="hljs-comment">// Logs:</span>
<span class="hljs-comment">// { signed: '0x123...ff' }</span>
<span class="hljs-comment">// { sent: '0x456...ff' }</span>
<span class="hljs-comment">// { 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="tutorial3-manipulating-rpcobservables.html" class="navigation navigation-prev " aria-label="Previous page: Tutorial Part 3: Manipulating RpcObservables">
<i class="fa fa-angle-left"></i>
</a>
<a href="tutorial5-work-with-contracts.html" class="navigation navigation-next " aria-label="Next page: Tutorial Part 5: Work with Contracts">
<i class="fa fa-angle-right"></i>
</a>
</div>
<script>
var gitbook = gitbook || [];
gitbook.push(function() {
gitbook.page.hasChanged({"page":{"title":"Tutorial Part 4: Send a Transaction","level":"1.3.4","depth":2,"next":{"title":"Tutorial Part 5: Work with Contracts","level":"1.3.5","depth":2,"path":"guides/tutorial5-work-with-contracts.md","ref":"guides/tutorial5-work-with-contracts.md","articles":[]},"previous":{"title":"Tutorial Part 3: Manipulating RpcObservables","level":"1.3.3","depth":2,"path":"guides/tutorial3-manipulating-rpcobservables.md","ref":"guides/tutorial3-manipulating-rpcobservables.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/tutorial4-send-a-transaction.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>