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:
- 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.
- 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 π
- 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.
https://www.youtube.com/watch?v=9gVK6fp3UOo
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).
- We are going to run the code by invoking
node
(in the terminal, in Visual Studio Code). If you want to stop your code, you can pressControl
andC
at the same time. - Before we are going to write our code, we are going to use the command line utility
npm
to download a package we are depending on. The npm command line utility was automatically installed NPM is an abbreviation for 'node package manager'. Many developers write open source helper libraries, tools and ready to use plugins. Developers can publish these libraries, tools and plugins to npmjs.com so other developers can easily use them by installing them with the npm command line utility. - While you can pick any file name to store your code, and run your code from that custom file name, the commonly used name for the entry JS file is
index.js
.
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:
https://www.youtube.com/watch?v=HwOVa5N1G78
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://fh.xrpl.ws
: a secure websocket connection (wss
) to fh.xrpl.ws
: my full history XRPL node.
https://www.youtube.com/watch?v=HL0bvqkU5hQ
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:
- Does something asynchronously, like in this case: setting up a connection.
- then() it takes whatever output the previous action gave, and passes this on to a a piece of code you can write (a function).
- 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:
- Convert the string to an integer (we can use the
parseInt(...)
function to do this) - Divide by 1000000 (we can simply divide an integer by appending
/ 1000000
)
Let's do this. Here goes nothing π
https://www.youtube.com/watch?v=OmwjtinAn3I
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.