Skip to content

Commit 63fc110

Browse files
committed
Demo for code walkthrough functionality
1 parent 662aebb commit 63fc110

File tree

4 files changed

+99
-37
lines changed

4 files changed

+99
-37
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>XRPL.js Base Example</title>
5+
<!-- @chunk {"steps": ["import-web-tag"]} -->
6+
<script src="https://unpkg.com/xrpl/build/xrpl-latest-min.js"></script>
7+
<!-- @chunk-end -->
8+
</head>
9+
<body>
10+
<h1>XRPL.js Example</h1>
11+
<!-- @chunk {"steps": ["import-web-tag"]} -->
12+
<script>
13+
const xrpl = require("xrpl")
14+
</script>
15+
<!-- @chunk-end -->
16+
</body>
17+
</html>

_code-samples/get-started/js/base.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// In browsers, use a <script> tag. In Node.js, uncomment the following line:
2-
// const xrpl = require('xrpl')
2+
const xrpl = require('xrpl')
33

44
// Wrap code in an async function so we can use await
55
async function main() {

_code-samples/get-started/js/get-acct-info.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,46 @@
11
// Import the library
2+
// @chunk {"steps": ["import-node-tag"]}
23
const xrpl = require("xrpl")
4+
// @chunk-end
35

46
// Wrap code in an async function so we can use await
57
async function main() {
68

9+
// @chunk {"steps": ["connect-tag"]}
710
// Define the network client
811
const SERVER_URL = "wss://s.altnet.rippletest.net:51233/"
912
const client = new xrpl.Client(SERVER_URL)
1013
await client.connect()
14+
// @chunk-end
1115

16+
// @chunk {"steps": ["get-account-create-wallet-tag"]}
1217
// Create a wallet and fund it with the Testnet faucet:
1318
const fund_result = await client.fundWallet()
1419
const test_wallet = fund_result.wallet
1520
console.log(fund_result)
21+
// @chunk-end
1622

23+
// To generate keys only, uncomment the code below
24+
// @chunk {"steps": ["get-account-create-wallet-b-tag"]}
25+
// const test_wallet = xrpl.Wallet.generate()
26+
// @chunk-end
27+
28+
// To provide your own seed, replace the test_wallet value with the below
29+
// @chunk {"steps": ["get-account-create-wallet-c-tag"], "inputs": ["wallet-input"]}
30+
// const test_wallet = xrpl.Wallet.fromSeed({{wallet-input}})
31+
// @chunk-end
32+
33+
// @chunk {"steps": ["query-xrpl-tag"]}
1734
// Get info from the ledger about the address we just funded
1835
const response = await client.request({
1936
"command": "account_info",
2037
"account": test_wallet.address,
2138
"ledger_index": "validated"
2239
})
2340
console.log(response)
41+
// @chunk-end
2442

43+
// @chunk {"steps": ["listen-for-events-tag"]}
2544
// Listen to ledger close events
2645
client.request({
2746
"command": "subscribe",
@@ -30,9 +49,12 @@ async function main() {
3049
client.on("ledgerClosed", async (ledger) => {
3150
console.log(`Ledger #${ledger.ledger_index} validated with ${ledger.txn_count} transactions!`)
3251
})
52+
// @chunk-end
3353

54+
// @chunk {"steps": ["disconnect-tag"]}
3455
// Disconnect when done so Node.js can end the process
3556
await client.disconnect()
57+
// @chunk-end
3658
}
3759

3860
// call the async function

docs/tutorials/javascript/build-apps/get-started.md

Lines changed: 59 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ labels:
99
- Development
1010
showcase_icon: assets/img/logos/javascript.svg
1111
---
12+
13+
{% code-walkthrough
14+
filesets=[
15+
{
16+
"files": [ "/_code-samples/get-started/js/base.html", "/_code-samples/get-started/js/get-acct-info.js"],
17+
"downloadAssociatedFiles": ["/_code-samples/get-started/js/*"],
18+
}
19+
]
20+
%}
21+
1222
# Get Started Using JavaScript Library
1323

1424
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:
3242
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.
3343

3444

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-
4445
## Start Building
4546

4647
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:
5758

5859
How you load `xrpl.js` into your project depends on your development environment:
5960

61+
{% step id="import-web-tag" %}
6062
#### Web Browsers
6163

62-
Add a `<script>` tag such as the following to your HTML:
64+
Add a `<script>` tag such as the following to your HTML.
6365

64-
```html
66+
<!-- ```html
6567
<script src="https://unpkg.com/xrpl/build/xrpl-latest-min.js"></script>
66-
```
68+
``` -->
6769

68-
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.
6971

7072
This loads the module into the top level as `xrpl`.
73+
{% /step %}
7174

75+
{% step id="import-node-tag" %}
7276
#### Node.js
7377

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:
7579

7680
```sh
7781
npm install xrpl
7882
```
83+
This updates your `package.json` file, or creates a new one if it didn't already exist.
7984

80-
Then import the library:
81-
82-
```js
85+
Then, import the library in your code.
86+
<!-- ```js
8387
const xrpl = require("xrpl")
84-
```
85-
88+
``` -->
89+
{% /step %}
8690

8791
### 2. Connect to the XRP Ledger
8892

93+
{% step id="connect-tag" %}
8994
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.
9095

9196
{% 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 %}
9297

93-
{% code-snippet file="/_code-samples/get-started/js/base.js" language="js" /%}
98+
<!-- {% code-snippet file="/_code-samples/get-started/js/base.js" language="js" /%} -->
9499

95100
#### Connect to the XRP Ledger Mainnet
96101

97-
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:
98103

99104
* 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:
100105

101-
```
106+
```javascript
102107
const MY_SERVER = "ws://localhost:6006/"
103108
const client = new xrpl.Client(MY_SERVER)
104109
await client.connect()
@@ -108,44 +113,60 @@ The sample code in the previous section shows you how to connect to the Testnet,
108113

109114
* By using one of the available [public servers][]:
110115

111-
```
116+
```javascript
112117
const PUBLIC_SERVER = "wss://xrplcluster.com/"
113118
const client = new xrpl.Client(PUBLIC_SERVER)
114119
await client.connect()
115120
```
116-
121+
{% /step %}
117122

118123
### 3. Get Account
119124

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` class for handling the keys and address of an XRP Ledger account. On Testnet, you can fund a new account as shown in the example.
121127

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 %}
123130

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.
125133

126-
```js
134+
<!-- ```js
127135
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.
129141

130-
Or, if you already have a seed encoded in [base58][], you can make a `Wallet` instance from it like this:
142+
{% input id="wallet-input" label="Your Testnet seed key" placeholder="Enter seed key" /%}
131143

132-
```js
144+
<!-- ```js
133145
const test_wallet = xrpl.Wallet.fromSeed("sn3nxiW7v8KXzPzAqzyHXbSSKNuN9") // Test secret; don't use for real
134-
```
146+
``` -->
147+
{% /step %}
135148

136149
### 4. Query the XRP Ledger
137150

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 [WebSocket API](../../../references/http-websocket-apis/api-conventions/request-formatting.md).
141153

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 %}
142156

143157
### 5. Listen for Events
144158

159+
{% step id="listen-for-events-tag" %}
145160
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.
146161
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
148166
167+
{% step id="disconnect-tag" %}
168+
Disconnect when done so Node.js can end the process.
169+
{% /step %}
149170
150171
## Keep on Building
151172
@@ -171,3 +192,5 @@ Now that you know how to use `xrpl.js` to connect to the XRP Ledger, get an acco
171192
- [Transaction Formats](../../../references/protocol/transactions/index.md)
172193
173194
{% raw-partial file="/docs/_snippets/common-links.md" /%}
195+
196+
{% /code-walkthrough %}

0 commit comments

Comments
 (0)