You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This tutorial guides you through the basics of building an XRP Ledger-connected application in JavaScript or TypeScript using the [`xrpl.js`](https://github.com/XRPLF/xrpl.js/) client library in either Node.js or web browsers.
@@ -32,15 +42,6 @@ In this tutorial, you'll learn:
32
42
To follow this tutorial, you should have some familiarity with writing code in JavaScript and managing small JavaScript projects. In browsers, any modern web browser with JavaScript support should work fine. In Node.js, **version 14** is recommended. Node.js versions 12 and 16 are also regularly tested.
33
43
34
44
35
-
## Install with npm
36
-
37
-
Start a new project by creating an empty folder, then move into that folder and use [NPM](https://www.npmjs.com/) to install the latest version of xrpl.js:
38
-
39
-
```sh
40
-
npm install xrpl
41
-
```
42
-
43
-
44
45
## Start Building
45
46
46
47
When you're working with the XRP Ledger, there are a few things you'll need to manage, whether you're adding XRP to your [account](../../../concepts/accounts/index.md), integrating with the [decentralized exchange](../../../concepts/tokens/decentralized-exchange/index.md), or [issuing tokens](../../../concepts/tokens/index.md). This tutorial walks you through basic patterns common to getting started with all of these use cases and provides sample code for implementing them.
@@ -57,48 +58,52 @@ Here are some steps you use in many XRP Ledger projects:
57
58
58
59
How you load `xrpl.js` into your project depends on your development environment:
59
60
61
+
{% step id="import-web-tag" %}
60
62
#### Web Browsers
61
63
62
-
Add a `<script>` tag such as the following to your HTML:
64
+
Add a `<script>` tag such as the following to your HTML.
You can load the library from a CDN as in the above example, or download a release and host it on your own website.
70
+
You can load the library from a CDN as in the example, or download a release and host it on your own website.
69
71
70
72
This loads the module into the top level as `xrpl`.
73
+
{% /step %}
71
74
75
+
{% step id="import-node-tag" %}
72
76
#### Node.js
73
77
74
-
Add the library using [npm](https://www.npmjs.com/). This updates your `package.json` file, or creates a new one if it didn't already exist:
78
+
Start a new project by creating an empty folder, then move into that folder and use [NPM](https://www.npmjs.com/) to install the latest version of xrpl.js:
75
79
76
80
```sh
77
81
npm install xrpl
78
82
```
83
+
This updates your `package.json` file, or creates a new one if it didn't already exist.
79
84
80
-
Then import the library:
81
-
82
-
```js
85
+
Then, import the library in your code.
86
+
<!-- ```js
83
87
const xrpl = require("xrpl")
84
-
```
85
-
88
+
```-->
89
+
{% /step %}
86
90
87
91
### 2. Connect to the XRP Ledger
88
92
93
+
{% step id="connect-tag" %}
89
94
To make queries and submit transactions, you need to connect to the XRP Ledger. To do this with `xrpl.js`, you create an instance of the `Client` class and use the `connect()` method.
90
95
91
96
{% admonition type="success" name="Tip" %}Many network functions in `xrpl.js` use [Promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) to return values asynchronously. The code samples here use the [`async/await` pattern](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await) to wait for the actual result of the Promises.{% /admonition %}
The sample code in the previous section shows you how to connect to the Testnet, which is one of the available [parallel networks](../../../concepts/networks-and-servers/parallel-networks.md). When you're ready to move to production, you'll need to connect to the XRP Ledger Mainnet. You can do that in two ways:
102
+
The sample code shows you how to connect to the Testnet, which is one of the available [parallel networks](../../../concepts/networks-and-servers/parallel-networks.md). When you're ready to move to production, you'll need to connect to the XRP Ledger Mainnet. You can do that in two ways:
98
103
99
104
* By [installing the core server](../../../infrastructure/installation/index.md) (`rippled`) and running a node yourself. The core server connects to the Mainnet by default, but you can [change the configuration to use Testnet or Devnet](../../../infrastructure/configuration/connect-your-rippled-to-the-xrp-test-net.md). [There are good reasons to run your own core server](../../../concepts/networks-and-servers/index.md#reasons-to-run-your-own-server). If you run your own server, you can connect to it like so:
100
105
101
-
```
106
+
```javascript
102
107
constMY_SERVER="ws://localhost:6006/"
103
108
constclient=newxrpl.Client(MY_SERVER)
104
109
awaitclient.connect()
@@ -108,44 +113,60 @@ The sample code in the previous section shows you how to connect to the Testnet,
108
113
109
114
* By using one of the available [public servers][]:
110
115
111
-
```
116
+
```javascript
112
117
const PUBLIC_SERVER = "wss://xrplcluster.com/"
113
118
const client = new xrpl.Client(PUBLIC_SERVER)
114
119
await client.connect()
115
120
```
116
-
121
+
{%/step %}
117
122
118
123
### 3. Get Account
119
124
120
-
The `xrpl.js` library has a `Wallet` class for handling the keys and address of an XRP Ledger account. On Testnet, you can fund a new account like this:
125
+
{% step id="get-account-create-wallet-tag"%}
126
+
The `xrpl.js` library has a `Wallet`classfor handling the keys and address of an XRP Ledger account. On Testnet, you can fund a newaccount as shown in the example.
121
127
122
-
{% code-snippet file="/_code-samples/get-started/js/get-acct-info.js" from="// Create a wallet" before="// Get info" language="js" /%}
128
+
<!-- {% code-snippet file="/_code-samples/get-started/js/get-acct-info.js" from="// Create a wallet" before="// Get info" language="js"/%} -->
129
+
{%/step %}
123
130
124
-
If you only want to generate keys, you can create a new `Wallet` instance like this:
131
+
{% step id="get-account-create-wallet-b-tag"%}
132
+
If you only want to generate keys, you can alternatively create a new`Wallet` instance.
125
133
126
-
```js
134
+
<!--```js
127
135
const test_wallet = xrpl.Wallet.generate()
128
-
```
136
+
```-->
137
+
{%/step %}
138
+
139
+
{% step id="get-account-create-wallet-c-tag"%}
140
+
Or, if you already have a seed encoded in [base58][], you can make a `Wallet` instance from it.
129
141
130
-
Or, if you already have a seed encoded in [base58][], you can make a `Wallet` instance from it like this:
const test_wallet = xrpl.Wallet.fromSeed("sn3nxiW7v8KXzPzAqzyHXbSSKNuN9") // Test secret; don't use for real
134
-
```
146
+
```-->
147
+
{%/step %}
135
148
136
149
### 4. Query the XRP Ledger
137
150
138
-
Use the Client's `request()` method to access the XRP Ledger's [WebSocket API](../../../references/http-websocket-apis/api-conventions/request-formatting.md). For example:
139
-
140
-
{% code-snippet file="/_code-samples/get-started/js/get-acct-info.js" from="// Get info" before="// Listen to ledger close events" language="js" /%}
151
+
{% step id="query-xrpl-tag"%}
152
+
Use the Client's `request()` method to access the XRP Ledger's [WebSocketAPI](../../../references/http-websocket-apis/api-conventions/request-formatting.md).
141
153
154
+
<!-- {% code-snippet file="/_code-samples/get-started/js/get-acct-info.js" from="// Get info" before="// Listen to ledger close events" language="js"/%} -->
155
+
{%/step %}
142
156
143
157
### 5. Listen for Events
144
158
159
+
{% step id="listen-for-events-tag"%}
145
160
You can set up handlers for various types of events in`xrpl.js`, such as whenever the XRP Ledger's [consensus process](../../../concepts/consensus-protocol/index.md) produces a new [ledger version](../../../concepts/ledgers/index.md). To do that, first call the [subscribe method][] to get the type of events you want, then attach an event handler using the `on(eventType, callback)` method of the client.
146
161
147
-
{% code-snippet file="/_code-samples/get-started/js/get-acct-info.js" from="// Listen to ledger close events" before="// Disconnect when done" language="js" /%}
162
+
<!-- {% code-snippet file="/_code-samples/get-started/js/get-acct-info.js" from="// Listen to ledger close events" before="// Disconnect when done" language="js" /%} -->
163
+
{% /step %}
164
+
165
+
### 6. Disconnect
148
166
167
+
{% step id="disconnect-tag" %}
168
+
Disconnect when done so Node.js can end the process.
169
+
{% /step %}
149
170
150
171
## Keep on Building
151
172
@@ -171,3 +192,5 @@ Now that you know how to use `xrpl.js` to connect to the XRP Ledger, get an acco
0 commit comments