Follow Received Payments
Last updated
Was this helpful?
Last updated
Was this helpful?
This tutorial shows how easy it is to use Expansion to watch for incoming payments on an using JavaScript and EventSource
. We will eschew using , the high-level helper library, to show that it is possible for you to perform this task on your own with whatever programming language you would like to use.
This tutorial assumes that you:
Have node.js installed locally on your machine.
Have curl installed locally on your machine.
Are running on Linux, macOS, or any other system that has access to a bash-like shell.
Are familiar with launching and running commands in a terminal.
In this tutorial we will learn:
How to create a new account.
How to fund your account using friendbot.
How to follow payments to your account using curl and EventSource.
Let's get started by building our project skeleton:
This should have created a package.json
in the follow_tutorial
directory. You can check that everything went well by running the following command:
Everything was successful if no output was generated from the above command. Now let's write a script to create a new account.
Create a new file named make_account.js
and paste the following text into it:
Save the file and run it:
Before our account can do anything it must be funded. Indeed, before an account is funded it does not truly exist!
The Bantu test network provides the Friendbot, a tool that developers can use to get testnet spirits for testing purposes. To fund your account, simply execute the following curl command:
Don't forget to replace the account id above with your own. If the request succeeds, you should see a response like:
After a few seconds, the Bantu network will perform consensus, close the ledger, and your account will have been created. Next up we will write a command that watches for new payments to your account and outputs a message to the terminal.
curl
As a result you will see something like:
EventStream
Another way to follow payments is writing a simple JS script that will stream payments and print them to console. Create stream_payments.js
file and paste the following code into it:
Now, run our script: node stream_payments.js
. You should see following output:
First, let's check our account sequence number so we can create a payment transaction. To do this we send a request to Expansion:
Sequence number can be found under the sequence
field. For our example, the current sequence number is 713226564141056
. Save your value somewhere.
Now, create make_payment.js
file and paste the following code into it, replacing the sequence number accordingly:
After running this script you should see a signed transaction blob. To submit this transaction we send it to Expansion or Bantu-core. But before we do, let's open a new console and start our previous script by node stream_payments.js
.
Now to send a transaction just use Expansion
To follow new payments connected to your account you simply need to send the Accept: text/event-stream
header to the endpoint.
Every time you receive a new payment you will get a new row of data. Payments is not the only endpoint that supports streaming. You can also stream transactions and operations .
Warning! EventSource
object does not reconnect for certain error types so it can stop working. If you need a reliable streaming connection please use our .
We now know how to get a stream of transactions to an account. Let's check if our solution actually works and if new payments appear. Let's watch as we send a payment () from our account to another account.
We use the create_account
operation because we are sending payment to a new, unfunded account. If we were sending payment to an account that is already funded, we would use the .