Sending an Aion Transaction with Nodesmith and Javascript

Samm Desmond
Nodesmith
Published in
4 min readNov 12, 2018

Using Nodesmith’s JSON RPC service, you can read and write state from the Aion network without needing to host and run your own Aion Node. In this article, we’ll focus on writing to the Aion network by sending a transaction using Javascript.

If you haven’t signed up for Nodesmith or created a project, check out our getting started post.

Introduction

Unlike the other JSON RPC endpoints, eth_sendRawTransaction takes in an opaque payload which is the raw signature of the transaction you wish to send. In order to use this endpoint, you need to do some work to sign the transaction using a private key so the network can accept the transaction. In this example, we’ll perform this signing using Javascript (this example specifically uses NodeJS to read files, but everything here is possible in just a browser). Once the signing is done, we can send a simple transaction which transfers value from one account to another. If you just want to download and run the sample yourself, you can find the sample code here.

Setup

There are a couple of necessary setup steps to take before we can start writing code.

Step 1. Create a Nodesmith account and project to get your API key. Get one here.

Step 2. For this example, we ‘re going to create an account which we will only use on the Testnet for demo purposes. First, download and install the Aion Desktop Wallet. Create a new Aion Account in the wallet and name it “Testnet”. Export the keystore file for the “Testnet” account. See our docs for more details. (You can also use a private key string if you have one of those).

Creating and exporting a keystore

Step 3. In order to send transactions, we need to have some Aion in the account we just created. We can do that with the Aion Mastery Testnet Faucet. Simply enter the address you just created into the chat and you’ll be given 1 Aion.

Requesting Testnet Aion

Step 4. Finally, before writing our code, we will create a blank project with some necessary JavaScript dependencies (aion-web3 and aion-keystore). We’ve provided a package.json file you can use to easily set this up. You’ll need node.js version 10 or higher installed, then run the following from a terminal or command prompt:

# Create and move into a new folder
mkdir aion_nodesmith_demo
cd aion_nodesmith_demo
# Download the package.json file
wget https://gist.githubusercontent.com/sdesmond46/3278f399f41e8eb3ff60c6c9c45f0679/raw/febb8fad12395ffbb7e224f0bc8eceb3d922e6d9/package.json
# Install the Javascript depencies
npm install

The Code

For this example, we’ll create a transferAion.js file in the directory we created above. This file exports a function that takes in some parameters to transfer Aion from one account to another, and returns a promise which will resolve once the value has been transferred.

We’ll break down the process of sending a transaction into 4 separate steps, adding a separate function to perform each task. The steps are:

  1. Set up the aion-web3 library to connect to Nodesmith using your API key.
  2. Use a private key string or a keystore file to unlock an account.
  3. Specify the parameters of the transaction you want to send (what address, how much value, gas price, etc.) and sign the transaction using the unlocked account.
  4. Send the raw transaction to the Aion network through Nodesmith and wait for confirmation that it was committed to the network.

Part 0: The finished transfer function

We’ll start with the finished transfer function which follows the steps listed above. This is what we’ll call later on to actually send our transaction.

Part 1. Getting a web3 library instance

This method uses the aion-web3 module to create a new web3 library we can use to communicate with the Aion network. It takes in the Nodesmith endpoint you can get from your Nodesmith Dashboard as the url, but it could also connect to a locally running Aion kernel.

Part 2. Extracting a Private Key

This method uses either an exported keystore file or a raw private key to get a private key which the web3 library can use for signing transactions.

Part 3. Defining and Signing the Transaction

This method builds up the information needed to send a transaction, and then signs it using the private key we extracted in a previous step.

Part 4. Sending the Signed Transaction and Waiting for Confirmation

The final method, takes the signed transaction and sends it to transaction. It then waits for receipt that the transaction went through.

Putting It All Together

Now that we have defined the function for transferring Aion, we should actually call it with all of our parameters and check to see that it works! In the example below, I’m using an exported keystore file to make the transfer. Be sure to change YOUR_API_KEY and privateKey or keystoreFilePath for your own use.

After we execute this script, we can go check the Aion dashboard for our transaction using the transaction hash which was printed out.

The Confirmed Transaction

‘Getting the code

The sample code here can also be downloaded and run as a Gist on Github.

Conclusion

In this article, we went over how to create a signed transaction and submit it to the Aion network, all without having to run your own Aion Kernel, thanks to Nodesmith! I hope it’s been useful. In the future, we’ll be publishing an article about deploying and interacting with smart contracts using our service.

--

--