-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathget-started-with-rippleapi-for-javascript.html
More file actions
391 lines (331 loc) · 18.7 KB
/
get-started-with-rippleapi-for-javascript.html
File metadata and controls
391 lines (331 loc) · 18.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width">
<title>Get Started with RippleAPI for JavaScript - XAG Ledger Dev Portal</title>
<!-- favicon -->
<link rel="apple-touch-icon" sizes="180x180" href="assets/favicons/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="16x16" href="assets/favicons/favicon-16x16.png">
<link rel="icon" type="image/png" sizes="32x32" href="assets/favicons/favicon-32x32.png">
<link rel="manifest" href="assets/favicons/site.webmanifest">
<link rel="shortcut icon" href="favicon.ico">
<meta name="msapplication-TileColor" content="#25A768">
<meta name="msapplication-config" content="assets/favicons/browserconfig.xml">
<meta name="theme-color" content="#25A768">
<!-- jQuery -->
<script src="assets/vendor/jquery-1.11.1.min.js"></script>
<!-- Stylesheet -->
<link href="assets/css/devportal.css" rel="stylesheet" />
<!-- Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-45576805-2"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-45576805-2');
gtag('config', 'UA-157720658-3', { 'linker': {
'domains': ['blog.xpring.io', 'forum.xpring.io', 'xpring.io',
'XAGl.org', 'explorer.XAGl.org', 'testnet.XAGl.org']
} }
);
</script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/docsearch.js@2/dist/cdn/docsearch.min.css" />
<!-- syntax highlighting -->
<link rel="stylesheet" href="assets/vendor/docco.min.css" />
<script src="assets/vendor/highlight.min.js"></script>
<!-- expandable code samples -->
<script src="assets/js/expandcode.js"></script>
<!-- multi-code selection tabs -->
<script src="assets/js/multicodetab.js"></script>
<script>
$(document).ready(function() {
$(".multicode").minitabs();
hljs.initHighlighting();
make_code_expandable();
});
</script>
</head>
<body class="xrp-ledger-dev-portal sidebar-primary lang-en ">
<nav class="navbar fixed-top navbar-expand-lg navbar-light bg-white">
<a href="index.html" class="navbar-brand"><img src="assets/img/logo.png" class="logo" height="44" alt="XAG Ledger Dev Portal" /></a>
<button class="navbar-toggler" type="button" data-toggle="slide-collapse" data-target="#navbarHolder" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse justify-content-end" id="navbarHolder">
<ul class="nav navbar-nav">
<li class="nav-item">
<a class="nav-link" href="dev-tools.html">Dev Tools</a>
</li>
<li class="nav-item">
<a class="nav-link" href="http://xagfans.com/" target="blank">BBS</a>
</li>
</ul><!-- /.navbar-nav -->
<div class="nav navbar-nav language-selector">
<div class="dropdown">
<button class="btn btn-outline-secondary dropdown-toggle" type="button" id="language_selector_header_btn" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
English
</button>
<div class="dropdown-menu" aria-labelledby="language_selector_header_btn">
<a class="dropdown-item" href="/get-started-with-rippleapi-for-javascript.html">English</a>
<a class="dropdown-item" href="/cn/get-started-with-rippleapi-for-javascript.html">中文</a>
</div><!--/.dropdown-menu-->
</div><!--/.dropdown-->
</div><!--/.language-selector-->
</div><!--/#navbarHolder-->
<div class="menu-overlay"></div>
</nav>
<div class="container-fluid" role="document" id="main_content_wrapper">
<div class="row">
<!-- Right sidebar first so it's at the beginning for mobile layouts -->
<aside class="right-sidebar col-lg-3 order-lg-4 p-0" role="complementary">
<div class="card" id="page-toc-wrapper">
</div>
</aside>
<!-- main column -->
<main class="main col-md-7 col-lg-6 order-md-3 " role="main" id="main_content_body">
<nav class="breadcrumbs-wrap p-0 p-md-3" aria-label="breadcrumb">
<ul class="breadcrumb bg-white">
<li class="breadcrumb-item"><a href="index.html">Home</a></li>
<li class="active breadcrumb-item"><a href="docs.html">Docs</a></li>
<li class="active breadcrumb-item"><a href="tutorials.html">Tutorials</a></li>
<li class="active breadcrumb-item"><a href="get-started.html">Get Started</a></li>
<li class="active breadcrumb-item">Get Started with RippleAPI for JavaScript</li>
</ul>
</nav><!--/.breadcrumbs-wrap--> <article class="pt-3 p-md-3">
<div class="content">
<h1 id="get-started-with-rippleapi-for-javascript">Get Started with RippleAPI for JavaScript</h1>
<p>This tutorial guides you through the basics of building an XAG Ledger-connected application using Node.js and RippleAPI, a JavaScript API for accessing the XAG Ledger.</p>
<h1 id="environment-setup">Environment Setup</h1>
<p>The first step to using RippleAPI is setting up your development environment.</p>
<h2 id="install-nodejs-and-npm">Install Node.js and npm</h2>
<p>RippleAPI is built as an application for the Node.js runtime environment, so the first step is getting Node.js installed. RippleAPI requires Node.js v6 or higher. Ripple recommends using Node.js v10 LTS.</p>
<p>This step depends on your operating system. Ripple recommends using the official instructions for installing Node.js using a package manager for your operating system. If the packages for Node.js and npm (Node Package Manager) are separate, install both. (This applies to Arch Linux, CentOS, Fedora, and RHEL.)</p>
<p>After you have installed Node.js, you can check the version of the <code>node</code> binary from a command line:</p>
<pre><code>node --version
</code></pre>
<p>On some platforms, the binary is named <code>nodejs</code> instead:</p>
<pre><code>nodejs --version
</code></pre>
<h2 id="install-yarn">Install Yarn</h2>
<p>RippleAPI uses Yarn to manage dependencies. Ripple recommends using Yarn v1.13.0.</p>
<p>This step depends on your operating system. Ripple recommends using the official instructions for installing Yarn using a package manager <i aria-hidden="true" class="fa fa-external-link"></i></a> for your operating system.</p>
<p>After you have installed Yarn, you can check the version of the <code>yarn</code> binary from a command line:</p>
<pre><code>yarn --version
</code></pre>
<h2 id="install-rippleapi-and-dependencies">Install RippleAPI and Dependencies</h2>
<p>Complete these steps to use Yarn to install RippleAPI and dependencies.</p>
<h3 id="1-create-a-new-directory-for-your-project">1. Create a new directory for your project</h3>
<p>Create a folder called (for example) <code>my_ripple_experiment</code>:</p>
<pre><code>mkdir my_ripple_experiment && cd my_ripple_experiment
</code></pre>
<p>Optionally, start a Git repository in that directory so you can track changes to your code.</p>
<pre><code>git init
</code></pre>
<h3 id="2-create-a-new-packagejson-file-for-your-project">2. Create a new <code>package.json</code> file for your project</h3>
<p>Use the following template, which includes:</p>
<ul>
<li>RippleAPI itself (<code>ripple-lib</code>)</li>
</ul>
<pre><code>{
"name": "my_ripple_experiment",
"version": "0.0.1",
"license": "MIT",
"private": true,
"//": "Change the license to something appropriate. You may want to use 'UNLICENSED' if you are just starting out.",
"dependencies": {
"ripple-lib": "*"
},
"devDependencies": {
"eslint": "*"
}
}
</code></pre>
<h3 id="3-use-yarn-to-install-rippleapi-and-dependencies">3. Use Yarn to install RippleAPI and dependencies</h3>
<p>Use Yarn to install RippleAPI and the dependencies defined in the <code>package.json</code> file you created for your project.</p>
<pre><code>yarn
</code></pre>
<p>This installs RippleAPI and the dependencies into the local folder <code>node_modules/</code>.</p>
<p>The install process may end with a few warnings. You may safely ignore the following warnings:</p>
<pre><code>warning eslint > file-entry-cache > flat-cache > circular-json@0.3.3: CircularJSON is in maintenance only, flatted is its successor.
npm WARN optional Skipping failed optional dependency /chokidar/fsevents:
npm WARN notsup Not compatible with your operating system or architecture: fsevents@1.0.6
</code></pre>
<h1 id="first-rippleapi-script">First RippleAPI Script</h1>
<p>This script, <code>get-account-info.js</code>, fetches information about a hard-coded account. Use it to test that RippleAPI works:</p>
<pre><code>'use strict';
const RippleAPI = require('ripple-lib').RippleAPI;
const api = new RippleAPI({
server: 'wss://g1.xrpgen.com' // Public rippled server
});
api.connect().then(() => {
/* begin custom code ------------------------------------ */
const myAddress = 'rpG9E7B3ocgaKqG7vmrsu3jmGwex8W4xAG';
console.log('getting account info for', myAddress);
return api.getAccountInfo(myAddress);
}).then(info => {
console.log(info);
console.log('getAccountInfo done');
/* end custom code -------------------------------------- */
}).then(() => {
return api.disconnect();
}).then(() => {
console.log('done and disconnected.');
}).catch(console.error);
</code></pre>
<h2 id="run-the-script">Run the Script</h2>
<p>Run your first RippleAPI script using this command:</p>
<pre><code>node get-account-info.js
</code></pre>
<p>Output:</p>
<pre><code>getting account info for rpG9E7B3ocgaKqG7vmrsu3jmGwex8W4xAG
{ sequence: 1,
xrpBalance: '1000',
ownerCount: 0,
previousInitiatedTransactionID: 'AA7ED0685F0735CC68CA65BE457915483CE8ABCA32C117716C90B5DFC0CA4595',
previousAffectingTransactionLedgerVersion: 9985048 }
getAccountInfo done
done and disconnected.
</code></pre>
<h2 id="understand-the-script">Understand the Script</h2>
<p>In addition to RippleAPI-specific code, this script uses syntax and conventions that are recent developments in JavaScript. Let's divide the sample code into smaller chunks to explain each one.</p>
<h3 id="script-opening">Script opening</h3>
<pre><code>'use strict';
const RippleAPI = require('ripple-lib').RippleAPI;
</code></pre>
<h3 id="instantiating-the-api">Instantiating the API</h3>
<pre><code>const api = new RippleAPI({
server: 'wss://g1.xrpgen.com' // Public rippled server
});
</code></pre>
<h3 id="connecting-and-promises">Connecting and Promises</h3>
<pre><code>api.connect().then(() => {
</code></pre>
<
<pre><code>}).then(() => {
return api.disconnect();
}).then(() => {
console.log('done and disconnected.');
}).catch(console.error);
</code></pre>
<p>This code creates and submits an order transaction, although the same principles apply to other types of transactions as well. After submitting the transaction, the code uses a new Promise, which queries the ledger again after using setTimeout to wait a fixed amount of time, to see if the transaction has been verified. If it hasn't been verified, the process repeats until either the transaction is found in a validated ledger or the returned ledger is higher than the LastLedgerSequence parameter.</p>
<p>In rare cases (particularly with a large delay or a loss of power), the <code>rippled</code> server may be missing a ledger version between when you submitted the transaction and when you determined that the network has passed the <code>maxLedgerVersion</code>. In this case, you cannot be definitively sure whether the transaction has failed, or has been included in one of the missing ledger versions. RippleAPI returns <code>MissingLedgerHistoryError</code> in this case.</p>
<p>The result returns xrpBalance value, and you should consider it as XAG balance.</p>
</div>
</article>
</main>
<!-- Left sidebar last so it's at the end for mobile -->
<aside class="sidebar col-md-5 col-lg-3 p-0 order-md-1" role="complementary">
<div class="tree_nav">
<!---->
<div class="sidenav_parent">
<a href="tutorials.html"> Tutorials</a>
</div>
<!---->
<!---->
<div id="sidenav" role="tablist" aria-multiselectable="true">
<!---->
<div class="card active">
<div class="card-header" role="tab" id="sidenav_cat_head_2">
<a class="sidenav_cat_toggler" role="button" data-toggle="collapse" href="#sidenav_collapse_2" aria-expanded="true" aria-controls="sidenav_collapse_2"></a>
<h5 class="card-title">
<a class="sidenav_cat_title active-parent" href="get-started.html">Get Started</a>
<!-- <a class="sidenav_cat_title" href="get-started.html">Get Started</a> -->
</h5>
</div><!-- /.card-header -->
<div id="sidenav_collapse_2" class="collapse show" role="tabpanel" aria-labelledby="sidenav_cat_head_2">
<div class="card-body">
<ul class="sidebar_pagelist">
<li><a class="nosubcat-page" href="get-started-with-the-rippled-api.html">Get Started with XAG Ledger APIs</a></li>
<li><a class="nosubcat-page" href="set-up-secure-signing.html">Set Up Secure Signing</a></li>
<li><a class="active nosubcat-page" href="get-started-with-rippleapi-for-javascript.html">Get Started with RippleAPI for JavaScript</a></li>
<li><a class="nosubcat-page" href="look-up-transaction-results.html">Look Up Transaction Results</a></li>
<li><a class="nosubcat-page" href="monitor-incoming-payments-with-websocket.html">Monitor Incoming Payments with WebSocket</a></li>
</ul>
</div><!-- /.card-body -->
</div><!-- /#sidenav_collapse2 -->
</div><!-- /.card -->
<!---->
<div class="card">
<div class="card-header" role="tab" id="sidenav_cat_head_8">
<a class="collapsed sidenav_cat_toggler" role="button" data-toggle="collapse" href="#sidenav_collapse_8" aria-expanded="true" aria-controls="sidenav_collapse_8"></a>
</div><!-- /.card-header -->
<div id="sidenav_collapse_8" class="collapse" role="tabpanel" aria-labelledby="sidenav_cat_head_8">
<div class="card-body">
<ul class="sidebar_pagelist">
<li><a class="nosubcat-page" href="send-XAG.html">Send XAG</a></li>
<li><a class="nosubcat-page" href="reliable-transaction-submission.html">Reliable Transaction Submission</a></li>
</ul>
</div><!-- /.card-body -->
</div><!-- /#sidenav_collapse8 -->
</div><!-- /.card -->
<!---->
<div class="card">
<div class="card-header" role="tab" id="sidenav_cat_head_12">
<a class="collapsed sidenav_cat_toggler" role="button" data-toggle="collapse" href="#sidenav_collapse_12" aria-expanded="true" aria-controls="sidenav_collapse_12"></a>
<h5 class="card-title">
<a class="sidenav_cat_title" href="manage-account-settings.html">Manage Account Settings</a>
<!-- <a class="sidenav_cat_title" href="manage-account-settings.html">Manage Account Settings</a> -->
</h5>
</div><!-- /.card-header -->
<div id="sidenav_collapse_12" class="collapse" role="tabpanel" aria-labelledby="sidenav_cat_head_12">
<div class="card-body">
<ul class="sidebar_pagelist">
<li><a class="nosubcat-page" href="assign-a-regular-key-pair.html">Assign a Regular Key Pair</a></li>
<li><a class="nosubcat-page" href="change-or-remove-a-regular-key-pair.html">Change or Remove a Regular Key Pair</a></li>
<li><a class="nosubcat-page" href="disable-master-key-pair.html">Disable Master Key Pair</a></li>
<li><a class="nosubcat-page" href="set-up-multi-signing.html">Set Up Multi-Signing</a></li>
<li><a class="nosubcat-page" href="send-a-multi-signed-transaction.html">Send a Multi-Signed Transaction</a></li>
<li><a class="nosubcat-page" href="require-destination-tags.html">Require Destination Tags</a></li>
</ul>
</div><!-- /.card-body -->
</div><!-- /#sidenav_collapse12 -->
</div><!-- /.card -->
<!---->
<!---->
<div class="card">
<div class="card-header" role="tab" id="sidenav_cat_head_35">
<a class="collapsed sidenav_cat_toggler" role="button" data-toggle="collapse" href="#sidenav_collapse_35" aria-expanded="true" aria-controls="sidenav_collapse_35"></a>
<h5 class="card-title">
<a class="sidenav_cat_title" href="XAG-ledger-businesses.html">XAG Ledger Businesses</a>
<!-- <a class="sidenav_cat_title" href="XAG-ledger-businesses.html">XAG Ledger Businesses</a> -->
</h5>
</div><!-- /.card-header -->
<div id="sidenav_collapse_35" class="collapse" role="tabpanel" aria-labelledby="sidenav_cat_head_35">
<div class="card-body">
<ul class="sidebar_pagelist">
<li><a class="nosubcat-page" href="list-xag-as-an-exchange.html">List XAG as an Exchange</a></li>
<li><a class="nosubcat-page" href="list-your-exchange-on-xag-charts.html">List Your Exchange on XAG Charts</a></li>
</ul>
</div><!-- /.card-body -->
</div><!-- /#sidenav_collapse35 -->
</div><!-- /.card -->
<!---->
</div><!-- /#sidenav -->
</div><!-- /.tree_nav --> </aside>
</div><!--/.row (main layout)-->
</div>
<!-- Jump to top button -->
<a href="#main_content_wrapper" class="jump-to-top btn btn-primary btn-lg" role="button" title="Jump to top of page">Top</a>
<script type="text/javascript" src="assets/js/jump-to-top.js"></script>
<!-- Non-blocking resources -->
<!-- Bootstrap JS -->
<script src="assets/vendor/bootstrap.min.js"></script>
<!-- fontawesome icons -->
<link rel="stylesheet" href="assets/vendor/fontawesome/css/font-awesome.min.css" />
<!-- Algolia DocSearch -->
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/docsearch.js@2/dist/cdn/docsearch.min.js"></script>
<script type="text/javascript"> docsearch({
apiKey: '70de5b9b8ef95460f3c6e04054b325b2',
indexName: 'XAGl',
inputSelector: '#topsearchbar',
algoliaOptions: { 'facetFilters': ["lang:en"] },
debug: false
});
</script>
<script type="application/javascript">
gtag('set', {'content_group1': 'Content Docs'});
</script>
</body>
</html>