In the previous blogpost we saw how one can write a smart contract for the Aeternity blockchain from scratch. Of course, smart contracts would not be so useful if it weren’t easy to deploy them to the blockchain. The Aeternity team has built the Forgae framework to make developers' lives easier. Forgae has a command line interface to setup new Aeternity projects, compile contracts, run unit tests, deploy compiled contracts, and start a local Aeternity node for development. In this post, we will go through the basic usage of Forgae to complete our development cycle for the Certification story.
Install and Setup
First, we need to install Node.js and its build tool npm. On macOS, we can use
brew to install Node.js
npm to install Forgae:
Secondly, we need to install Docker, since Forgae is using the Docker setup to spin up a local node. Installation instructions can be found on the Docker website.
Now, we need to create a project folder and run the initialization command:
This should print a result message, which may look like this:
The result of this command is a number of folders and files to quickly get started with Aeternity app development:
As we can see, Forgae creates a Node.js project with:
contractsfolder containing smart contracts in Sophia
- a Docker Compose file to start a local Aeternity node
It also generates a demo script and a smart contract, but we will remove them and
create new ones for our Certification project. Clean the
contracts folder and create two contracts based
on the source code of the previous post. The contracts folder should now look like this:
The initial setup is done!
Compilation is done through the Aeternity node. That means we need to start a local node first:
Once the Aeternity node is successfully started, we can compile our two contracts:
It should print the resulting (long) byte code. Make sure there are no errors in the terminal output.
Writing Tests in Mocha
First, clean the
test folder and create a new file with the name
examineeTest.js and the code below:
SDK modules and our own utility script for contract deployment. Then, we start with a standard clause of Mocha to define
a test. The first function
before prepares the state of this test: an Aeternity client to communicate with the node
and an instance of the ExamVerifier contract. Finally, we define a function to deploy an Examinee contract.
As we can see, this test compiles and deploys contracts on its own and later calls methods and checks
The main part of the test looks as follows:
In this test, we perform several calls to simulate a user scenario. To summarize the steps:
- Deploy Examinee contract
- Add training and assert points
- Assert that examinee cannot access the homework module yet
- Set Foundation Level exam for examinee
- Trigger ExamVerifier contract to calculate exam grade and store it in the blockchain
- Retrieve exam grade from the blockchain and store it in the Examinee contract
- Assert that the examinee can request the homework module now
- There is a global array of 10 wallets which can be used to simulate different user addresses in the network.
PromiseAPI and is thus fully asynchronous.
awaitcan be used to observe results.
- Returned values are encoded, and thus need to be decoded using
decodein order to compare them with test variables. The same applies to error message. They need to be decoded into a human-readable form.
- If we need to pass a contract address as a parameter to another contract, we will need to properly format it.
An example can be found in the
Although our integration tests are not exhaustive, they cover the main use case. Real production applications would require more tests and perhaps separate tests for each contract.
Once the tests are prepared, we can run them using the command below:
It should print the standard Mocha messages saying whether the tests are passing.
Forgae provides deployment scripts to easily publish smart contracts to a specific Aeternity network.
deployment folder and create a new file
deploy.js with the following content:
The above code looks similar to the test code which we have written earlier. First, we import a couple of
Aeternity SDK and Forgae modules, as well as our own script for contract deployment. The main deployment
instructions are in the
deploy function. Basically, we create an instance of
Deployer and deploy
both contracts with it. Finally, we print the Examinee contract address and the name of the network to which we deployed.
Note that the deployment script takes the network as a parameter as well as the private key of the account.
The latter is required to sign the contract creation transactions and is accountable for deployment expenses.
The network name and the key can be passed as options to the
We have finished our development cycle for the Certification story including smart contracts implementation, integration testing and deployment. Now we can stop the local node.
The Forgae framework covers the full cycle of the development flow. It is very helpful to have such a tool from the very beginning of an application development for any blockchain network. In case you know the Truffle framework from the Ethereum community, you may find many similarities in how Forgae works. This allows us to be quite productive in application development for Aeternity. For more information on different Forgae options and examples see the Aeternity tutorials.