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
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.
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).
- 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 press
Cat the same time.
- Before we are going to write our code, we are going to use the command line utility
npmto 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
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.
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 (
fh.xrpl.ws: my full history XRPL node.
Here's the code we just wrote:
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
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
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.