[CI Skip] 0.29.4
This commit is contained in:
@@ -0,0 +1,548 @@
|
||||
|
||||
<!DOCTYPE HTML>
|
||||
<html lang="" >
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
||||
<title>Craft Extrinsic · Polkadot API Book</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="../07_transfer_dots/" />
|
||||
|
||||
|
||||
<link rel="prev" href="../05_read_storage/" />
|
||||
|
||||
|
||||
</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="header">Intro</li>
|
||||
|
||||
|
||||
|
||||
<li class="chapter " data-level="1.1" data-path="../../">
|
||||
|
||||
<a href="../../">
|
||||
|
||||
|
||||
Introduction
|
||||
|
||||
</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
|
||||
|
||||
|
||||
<li class="header">API</li>
|
||||
|
||||
|
||||
|
||||
<li class="chapter " data-level="2.1" data-path="../../api/">
|
||||
|
||||
<a href="../../api/">
|
||||
|
||||
|
||||
api
|
||||
|
||||
</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
<li class="chapter " data-level="2.2" data-path="../../api-format/">
|
||||
|
||||
<a href="../../api-format/">
|
||||
|
||||
|
||||
api-format
|
||||
|
||||
</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
<li class="chapter " data-level="2.3" data-path="../../api-provider/">
|
||||
|
||||
<a href="../../api-provider/">
|
||||
|
||||
|
||||
api-provider
|
||||
|
||||
</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
<li class="chapter " data-level="2.4" data-path="../../api-rx/">
|
||||
|
||||
<a href="../../api-rx/">
|
||||
|
||||
|
||||
api-rx
|
||||
|
||||
</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
<li class="chapter " data-level="2.5" data-path="../../type-extrinsics/">
|
||||
|
||||
<a href="../../type-extrinsics/">
|
||||
|
||||
|
||||
type-extrinsics
|
||||
|
||||
</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
<li class="chapter " data-level="2.6" data-path="../../type-jsonrpc/">
|
||||
|
||||
<a href="../../type-jsonrpc/">
|
||||
|
||||
|
||||
type-jsonrpc
|
||||
|
||||
</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
<li class="chapter " data-level="2.7" data-path="../../type-params/">
|
||||
|
||||
<a href="../../type-params/">
|
||||
|
||||
|
||||
type-params
|
||||
|
||||
</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
<li class="chapter " data-level="2.8" data-path="../../type-primitives/">
|
||||
|
||||
<a href="../../type-primitives/">
|
||||
|
||||
|
||||
type-primitives
|
||||
|
||||
</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
<li class="chapter " data-level="2.9" data-path="../../type-storage/">
|
||||
|
||||
<a href="../../type-storage/">
|
||||
|
||||
|
||||
type-storage
|
||||
|
||||
</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
|
||||
|
||||
|
||||
<li class="header">Examples</li>
|
||||
|
||||
|
||||
|
||||
<li class="chapter " data-level="3.1" data-path="../">
|
||||
|
||||
<a href="../">
|
||||
|
||||
|
||||
Overview
|
||||
|
||||
</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
<li class="chapter " data-level="3.2" data-path="../01_simple_connect/">
|
||||
|
||||
<a href="../01_simple_connect/">
|
||||
|
||||
|
||||
Simple connect
|
||||
|
||||
</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
<li class="chapter " data-level="3.3" data-path="../02_listen_to_blocks/">
|
||||
|
||||
<a href="../02_listen_to_blocks/">
|
||||
|
||||
|
||||
Listen to blocks
|
||||
|
||||
</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
<li class="chapter " data-level="3.4" data-path="../03_listen_to_balance_change/">
|
||||
|
||||
<a href="../03_listen_to_balance_change/">
|
||||
|
||||
|
||||
Listen to balance change
|
||||
|
||||
</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
<li class="chapter " data-level="3.5" data-path="../04_generate_account/">
|
||||
|
||||
<a href="../04_generate_account/">
|
||||
|
||||
|
||||
Generate Accounts
|
||||
|
||||
</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
<li class="chapter " data-level="3.6" data-path="../05_read_storage/">
|
||||
|
||||
<a href="../05_read_storage/">
|
||||
|
||||
|
||||
Read Storage
|
||||
|
||||
</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
<li class="chapter active" data-level="3.7" data-path="./">
|
||||
|
||||
<a href="./">
|
||||
|
||||
|
||||
Craft Extrinsic
|
||||
|
||||
</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
<li class="chapter " data-level="3.8" data-path="../07_transfer_dots/">
|
||||
|
||||
<a href="../07_transfer_dots/">
|
||||
|
||||
|
||||
Transfer DOTs
|
||||
|
||||
</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
|
||||
|
||||
|
||||
<li class="header">Contributing</li>
|
||||
|
||||
|
||||
|
||||
<li class="chapter " data-level="4.1" data-path="../../CONTRIBUTING.html">
|
||||
|
||||
<a href="../../CONTRIBUTING.html">
|
||||
|
||||
|
||||
Contributing guide
|
||||
|
||||
</a>
|
||||
|
||||
|
||||
|
||||
</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="../.." >Craft Extrinsic</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="craft-extrinsic">Craft Extrinsic</h1>
|
||||
<div class="paragraph">
|
||||
<p>In the current version, crafting an extrinsic is a rather cumbersome process. This sample guides you though it and shows how to tranfer DOTs from an account to another.</p>
|
||||
</div>
|
||||
<div class="listingblock">
|
||||
<div class="content">
|
||||
<pre class="highlight"><code class="language-javascript" data-lang="javascript"><span class="hljs-keyword">const</span> Api = <span class="hljs-built_in">require</span>(<span class="hljs-string">'@polkadot/api'</span>).default
|
||||
<span class="hljs-keyword">const</span> WsProvider = <span class="hljs-built_in">require</span>(<span class="hljs-string">'@polkadot/api-provider/ws'</span>).default
|
||||
<span class="hljs-keyword">const</span> extrinsics = <span class="hljs-built_in">require</span>(<span class="hljs-string">'@polkadot/extrinsics'</span>).default
|
||||
<span class="hljs-keyword">const</span> encodeExtrinsic = <span class="hljs-built_in">require</span>(<span class="hljs-string">'@polkadot/extrinsics/codec/encode'</span>).default
|
||||
<span class="hljs-keyword">const</span> encodeLength = <span class="hljs-built_in">require</span>(<span class="hljs-string">'@polkadot/extrinsics/codec/encode/length'</span>).default
|
||||
<span class="hljs-keyword">const</span> Keyring = <span class="hljs-built_in">require</span>(<span class="hljs-string">'@polkadot/util-keyring'</span>).default
|
||||
<span class="hljs-keyword">const</span> storage = <span class="hljs-built_in">require</span>(<span class="hljs-string">'@polkadot/storage'</span>).default
|
||||
<span class="hljs-keyword">const</span> BN = <span class="hljs-built_in">require</span>(<span class="hljs-string">'bn.js'</span>)
|
||||
|
||||
<span class="hljs-keyword">const</span> Encoder = <span class="hljs-keyword">new</span> TextEncoder()
|
||||
<span class="hljs-keyword">const</span> keyring = <span class="hljs-keyword">new</span> Keyring()
|
||||
<span class="hljs-keyword">const</span> provider = <span class="hljs-keyword">new</span> WsProvider(<span class="hljs-string">'ws://127.0.0.1:9944'</span>)
|
||||
<span class="hljs-keyword">const</span> api = <span class="hljs-keyword">new</span> Api(provider)
|
||||
|
||||
<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getAccountIndex</span> (<span class="hljs-params">address</span>) </span>{
|
||||
<span class="hljs-keyword">return</span> api.state.getStorage([storage.system.public.accountIndexOf, address])
|
||||
}
|
||||
|
||||
<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">transfer</span> (<span class="hljs-params">keyRingFrom, addressTo, amount</span>) </span>{
|
||||
<span class="hljs-keyword">const</span> accountIndex = <span class="hljs-keyword">await</span> getAccountIndex(keyRingFrom.address())
|
||||
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Current accountIndex: <span class="hljs-subst">${accountIndex}</span>`</span>)
|
||||
|
||||
<span class="hljs-comment">// encode the call for signing</span>
|
||||
<span class="hljs-keyword">const</span> message = encodeExtrinsic(
|
||||
keyRingFrom.publicKey(),
|
||||
accountIndex,
|
||||
extrinsics.staking.public.transfer,
|
||||
[addressTo, amount]
|
||||
)
|
||||
|
||||
<span class="hljs-comment">// get the signature</span>
|
||||
<span class="hljs-keyword">const</span> signature = keyRingFrom.sign(message)
|
||||
|
||||
<span class="hljs-comment">// encode the extrinsic for submission, adding the length (prefix) and the</span>
|
||||
<span class="hljs-comment">// signature (postfix)Rödl</span>
|
||||
<span class="hljs-keyword">const</span> encoded = encodeLength(
|
||||
<span class="hljs-comment">// When working with publicKeys, the prefix is 0xff for poc-2+</span>
|
||||
<span class="hljs-keyword">new</span> <span class="hljs-built_in">Uint8Array</span>([<span class="hljs-number">0xff</span>]),
|
||||
message,
|
||||
signature
|
||||
)
|
||||
|
||||
<span class="hljs-keyword">return</span> api.author.submitExtrinsic(encoded)
|
||||
}
|
||||
|
||||
<span class="hljs-keyword">const</span> Alice = keyring.addFromSeed(Encoder.encode(<span class="hljs-string">'Alice '</span>))
|
||||
<span class="hljs-keyword">const</span> addressBob = <span class="hljs-string">'5Gw3s7q4QLkSWwknsiPtjujPv3XM4Trxi5d4PgKMMk3gfGTE'</span>
|
||||
<span class="hljs-keyword">const</span> amount = <span class="hljs-keyword">new</span> BN(<span class="hljs-number">999</span>) <span class="hljs-comment">// on dev chain, the fee is 1, that makes it a round 1000</span>
|
||||
|
||||
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Crafting and sending an extrinsic for Alice to send Bob <span class="hljs-subst">${amount}</span> DOTs`</span>)
|
||||
transfer(Alice, addressBob, amount)
|
||||
.then(() => <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Sent'</span>))
|
||||
.catch((e) => <span class="hljs-built_in">console</span>.log(
|
||||
<span class="hljs-string">'HINT: Make sure you wait for the storage to update before sending the next extrinsic'</span>, e))
|
||||
.finally(_ => process.exit(<span class="hljs-number">0</span>))</code></pre>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</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="../05_read_storage/" class="navigation navigation-prev " aria-label="Previous page: Read Storage">
|
||||
<i class="fa fa-angle-left"></i>
|
||||
</a>
|
||||
|
||||
|
||||
<a href="../07_transfer_dots/" class="navigation navigation-next " aria-label="Next page: Transfer DOTs">
|
||||
<i class="fa fa-angle-right"></i>
|
||||
</a>
|
||||
|
||||
|
||||
|
||||
</div>
|
||||
|
||||
<script>
|
||||
var gitbook = gitbook || [];
|
||||
gitbook.push(function() {
|
||||
gitbook.page.hasChanged({"page":{"title":"Craft Extrinsic","level":"3.7","depth":1,"next":{"title":"Transfer DOTs","level":"3.8","depth":1,"path":"examples/07_transfer_dots/README.adoc","ref":"examples/07_transfer_dots/README.adoc","articles":[]},"previous":{"title":"Read Storage","level":"3.6","depth":1,"path":"examples/05_read_storage/README.adoc","ref":"examples/05_read_storage/README.adoc","articles":[]},"dir":"ltr"},"config":{"plugins":["asciidoc-include"],"root":"docs","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"},"pluginsConfig":{"asciidoc-include":{"syntax":"asciidoc"},"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}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Polkadot API Book","gitbook":">=2.4.0"},"file":{"path":"examples/06_craft_extrinsic/README.adoc","mtime":"2018-09-17T12:30:32.113Z","type":"asciidoc"},"gitbook":{"version":"3.2.3","time":"2018-09-17T12:37:27.221Z"},"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>
|
||||
|
||||
Reference in New Issue
Block a user