Setting up the æternity Javascript SDK

After covering the basics and the various Types and Functions, we’re going to be focusing on setting up the æternity Javascript SDK.

First thing you want to do is to make sure you have the latest version of the SDK installed. Then, get yourself an æternity account key par to get some Æ to use on the testnet. The easiest way to do this is to use the æternity CLI.

Remember to never use any public tool from the internet to generate critical things, for example handling mainnet Æ or interacting with important contracts.

After installation, simply run “æcli account generate 2” and hit “y” to have two key pairs generated. Following this, you can head on over to æternity’s testnet faucet, enter the wallet address public key you just generated, and have five test Æ delivered to your accounts. (this may take a few seconds, that is as long as it takes for the block to get mined).

For a cool, informative video on the matter, check out this one right here:

For full code example, head on over here .

SDK supports flavors which allow you to reduce your bundle size. However, if you wish to keep things simple, just go for the universal flavor.

Now, you define your variables that hold your public and private keys. These are used to instantiate the so-called memory accounts inside the applications. These accounts live only in memory and will be used for blockchain operations. Once the SDK is set up you can, of course, add more accounts on the go, later on.

PSA Kind Reminder: Never hardcode private keys in the web app .

Ok, now back to it.

Defining an async function will allow the Javascript to use away syntax. The SDK will need at least one node in order to be able to communicate with the æternity blockchain. This can be a private node, or any node for that matter, running on a network that your accounts have funds in. In the argument object of the SDK setup, you can provide an area of nodes to use.

The URL is supposed to point to a node for sending transactions and the internal URL for executing local smart contract code (which, theoretically, not all public nodes could allow). For the time being, you need to provide an URL to host a compiler which can either be the public one that æternity blockchain is running, or your own one which you can run locally on your machine in a docker container.

In accounts, you input the memory accounts you just defined, and address takes public key of the accounts you wish to use for any operations by default. To test your connection to the node just ask for the current block height.

Now, to test things, head over to Sophia IDE and copy your contract code (of course, you can use any example code). Create a template and just paste the code into it. Watch out however — IDEs tend to, from time to time, tamper with code indentations. To test this, just hit “undo” after pasting and you will be able to see if your code has been tampered with.

Next, you create a contract instance with the help of the SDK by acting as if you were executing your smart contracts init function. Actually, you send a transaction to deploy the corresponding smart contract to the æternity blockchain. This, implicitly, runs its init function with the parameters you pass. It is worth, though, locking the contract instance and see what useful information it has to offer about the contract you just deployed.

For the final step — let’s call it! Go ahead and create a try catch clause. For every function you define in your contract you have a member function inside your contracts instances methods member. Awesome feature here is that the SDK knows exactly which function just reads the data from your smart contract and which changes the state, and therefore requires a transaction to be done. If you wish to play things safe, you can explicitly state the behavior of every function call. Now, pass your parameters neatly formatted.

You can dry-run stateful functions for testing and overall curiosity! This will help you find out if they will be executed properly at that very moment.

If you wish to choose which of the previously added accounts, pass an option object to your call. You can also, while here, define the amount of ættos — the smallest subunit of Æ — to be sent along with your transaction. The same is possible for contracts deployment too! Just check the SDK for more neat tricks you can do in the options.

For more details and an in depth look into all of the material covered above, again, check out æeternity’s very own Nikita’s awesome video covering the topic! There, you have a step-by-step how-to on all of these points.

Read the full article on the æternity blog: Setting Up æternity Javascript SDK | by Djongo | æternity blog

2 Likes