Coding & the XRP ledger. A beginners course.

It's magic! Those XRPL developers interacting live with the XRP ledger. Building blockchain apps.

Nope. Not magic. Those coders just learned how to do that πŸ˜‰ Some learned it a little faster because they already had coding experience, but they had to start without any coding experience as well.

I cannot code. It's too hard. I don't understand it, at all.

But you can. The basics are not hard. If you can solve easy riddles, think logically, you can definitely do some basic coding after a few minutes of studying this blog (OK, 30 minutes, maybe ;))

My computer is old, it cannot be used for coding, right?

Don't worry, your computer will be able to do this (as will you).

You are going to check your own XRP balance by coding a simple tool that interacts with the XRP ledger πŸ˜‰ 😎

And I am going to try to explain what to do, and what you are actually doing. Let's have some fun!

Where to start...

I guess this is the hardest part. Honestly. If you want to start coding (let's say you google β€œhow to start coding”) you'll find a million tutorials, how to's, video's and they will all tell you to download tool X, install application Y, start with programming language Z, and so on.

There is no right or wrong: it's partially preference and the programming language depends on what you want to code (and some programming languages can do almost the same, so again: preference, although some programming languages are better suited for specific purposes though).

For this tutorial I already decided on the language and editor we're going to use πŸ˜‰

A programming language

We're going to code Javascript (let's call it JS), and we're going to run the JS code on our computer using nodejs. If you just start coding JS, your computer won't know what to do with those coded JS files. Your computer needs nodejs to run your code.

I picked JS because:

  1. If you want to interact with the XRP ledger, there are already a lot of libraries, code samples and packages available in JS. I publish XRPL related JS code, libraries and samples almost on a weekly basis.
  2. The language is not that strict: it will not complain about a missing semicolon or an extra space in your code. We can worry about writing clean and consistent code later πŸ‘
  3. The basics are pretty straight forward.

Note: if you google javascript, nodejs, etc. you'll find a lot of contradicting, possibly outdated information. There are many different JS frameworks, dialects and ways to run JS code. It can run in your browser, on your computer, on a server, as an application, etc. It can be very confusing at times but I'll keep this tutorial as simple as possible by focussing only on the things we need to get you started.

Code editor and a way to run your code

You can use Notepad to write code, but there are code editors (applications) to make the life of developers a lot easier. Code editors are Notepad on steroids: they offer code completion, tooltips with information, automatic indentation, syntax highlighting, etc.

Again, there are several good code editors. We're going with Visual Studio Code because it's open source, fast, offers a great plugin ecosystem and it runs on Windows, OSX and Linux.

We'll run the JS code we're going to write using nodejs. In the next video I'll show/explain in 4 minutes how to install and configure your computer to do some actual coding 😎

The steps are similar for OSX / Linux users, except they can skip installing Git at 1:22 – 2:32, and skip configuring the terminal (default shell) at 3:44 – 4:05.

So now your computer is ready. Time to explain a little bit more and start coding!

Before we start coding

Before we are going to write a few lines of code, there are a few things I want to explain. These things will help you understand what's happening in a few minutes (when writing some code).

Now we're going to use Visual Studio Code, our editor, to create a folder on your computer, open the folder, and create our index.js file. We're going to open the terminal and verify our working directory as well, by issuing the pwd command (abbreviation for print working directory)

Practice: write a few simple lines of code and run it:

That wasn't that hard, was it? Now you know how to write a line and execute it, we're going to install a dependency using npm, called rippled-ws-client. This is a package I created to make a connection with a XRPL node, send a request and receive the response data from the XRPL node.

Using the rippled-ws-client package we will setup a websocket connection to my XRPL node. A server (like an XRPL node) allow websocket connections to be made by a client (like your JS code) to send and receive information. Websockets can stay open for a long period of time while allowing two way communication between the client and server.

So let's install the package using npm, and start using the package to connect to wss:// a secure websocket connection (wss) to my full history XRPL node.

Here's the code we just wrote:

The client returns a Promise. You can recognize a JS promise by the ...then()...catch()... syntax. A promise:

  1. Does something asynchronously, like in this case: setting up a connection.
  2. then() it takes whatever output the previous action gave, and passes this on to a a piece of code you can write (a function).
  3. Optionally: catch() an error. We didn't do this in this example.

The piece of code we just wrote can be explained like this:

After printing 'Connected' in the terminal, we call the close() method. The close() method is one of the methods available on the **connection** object. Another method available is send(), allowing you to send a JSON formatted command to the connected XRPL node.

Just like the rippled-ws-client returns a Promise with the connection, the method connection.send(...) returns a Promise with the response on your request from the connected XRPL node.

We are going to send an account_info command to the XRPL node. The format we use to send commands to the XRPL node over our websocket connection is called JSON: 'JavaScript Object Notation'. If you read the XRPL command documentation, you can find sample JSON formatted requests to send over websocket connections to an XRPL node.

Sample **account_info** command:

The sample contains the XRP TipBot account address, but you can/should of course enter your own XRPL account address.

We're going to get the account Balance from the response. The balance will be a string (instead of a integer) in drops. There are one million drops per XRP, so to show our XRPL account balance, we'll have to:

  1. Convert the string to an integer (we can use the parseInt(...) function to do this)
  2. Divide by 1000000 (we can simply divide an integer by appending / 1000000)

Let's do this. Here goes nothing πŸ˜‰

You just coded your own live connection to the XRP ledger, and got your balance from an XRPL node. Cool 😎 right!?

Well, that's it for now. You probably have some questions and we've barely scratched the surface of coding node, talking to XRPL nodes, etc. but we had to start somewhere, right?

I'll make some time in the future to do a few follow up blogs, picking up where we left off. Maybe some other community members will jump in to answer some questions πŸ˜‡?

There are some more code snippets below for Coil subscribers!

Cover photo by Baciu Tudor

Continue reading with a Coil membership.