Cranberry’s architecture, a crash course to dApp development with Aion

August
5 min readApr 29, 2019

--

If you are a developer who is learning how to create decentralized applications (dApps) on the Aion network, you have come to the right place. In this post, we will go through the Cranberry architecture, one of the first active dApps on the Aion network. We will walk through the stack, tools and services used to create Cranberry. We will also deep dive into the development of PLAT, an ATS token launched in conjunction with Cranberry.

Cranberry is a loyalty program for Aion network miners and PLAT is the token used to reward loyal miners. PLAT is to Cranberry, what air miles is to the Air Miles program. Once a miner earns PLAT, they can use the token to participate in our weekly draw which gives them the chance to earn AION to reward their loyalty to the network. Cranberry’s architecture includes a frontend, backend and smart contracts, which is where the ATS architecture lives.

The frontend

React was an obvious choice for us when deciding how to build the front-end. It is a framework our whole team is familiar with, plus it has a handful of libraries we could easily use for dApp development. One of these is React-Pack by Northern Block, which creates a simple skeleton dApp for you which helps to kickstart the development process.

AIWA
On top of the normal server-client architecture, most dApps’ frontend require an extra connection to interact with blockchain, and AIWA was that ‘extra connection’ for Cranberry. In order to take part in Cranberry, users first need a secure wallet that can hold their PLAT tokens and they also need to use that wallet to send PLAT to our smart contract on the blockchain. AIWA, a Chrome wallet developed by BlockX Labs specially for the Aion blockchain, was created to provide that functionality. AIWA allow miners to generate a secure wallet to hold their PLAT rewards and also allows them to interact with the Cranberry smart contract (send their PLAT along with details of the draw). One of AIWA’s biggest value-adds is that users do not have to worry about securely signing and sending transaction with private keys because AIWA handles all of it.

For our more experienced developers, all this is to say that AIWA automatically injects aion-web3 in your browser so that your dApp can interact with the blockchain. If you want to skip AIWA, you can import another npm package into your React project. Keep in mind however that users will not be able to sign transaction easily, which is why we used AIWA.

The backend

Similarly to React in the frontend, NodeJS and MySQL were the obvious choices for our backend system. Our whole team is familiar with these technologies, thus they enabled us to execute quickly and get Cranberry up and running.

Nodesmith
Similarly to our front end, our backend has to interact with the blockchain at times, and the team behind Nodesmith gave us a neat solution. Keep in mind that Nodesmith is neither a NodeJS architecture nor a NodeJS library. Nodesmith is a service that provides decentralized node infrastructure for the Aion network, allowing our backend system to interact with the blockchain via REST API. Essentially, you don’t need to run your own node. When you sign up with Nodesmith, you get a URL with a unique API key that you can use to query the blockchain through Nodesmith service. Their service connects to both Aion Mastery Testnet and Aion Mainnet.

The Smart Contracts

When working on Cranberry, we explored our options by answering 3 questions

  1. Is a custom token necessary?
  2. If it were, which token standard would we follow?
  3. How should we structure our smart contracts to best suit our dApp?

The result of our exploration gave birth to 2 smart contracts:

  1. A Platypus smart contract to issue PLAT tokens, and;
  2. A Cranberry smart contract to facilitate Cranberry rewards.

In this article, we will focus on the Platypus contract.

Platypus (PLAT)
When creating Cranberry, we knew that we have to give users something to reward their loyalty to the network. We had 2 choices: either use AION coin, or create a new custom tokens. If we were to use AION coin, it could impact the inflation policy of the whole network. We also knew that we had plans to expand Cranberry and provide other ways to incentivize loyalty and AION coin would be prohibitive, because it was limited to only the Aion Network. Therefore, we decided that a new custom token was necessary. That token is PLAT, which is currently a token on the Aion blockchain but has the potentially to be a cross-chain token in the future.

ERC-20 to the Aion Token Standard (ATS)
At first, we followed ERC20 token standard, because it is a widely accepted standard in the crypto community. However, ERC20 had its limits.

  • Aion does not support assembly language with Solidity smart contract, thus we were unable to provide inter-smart-contract interaction between PLAT and Cranberry smart contracts with ERC20
  • ERC20 does not provide cross-chain capability, which means we will have to come up with some half-baked solutions in the future to extend our token to other networks.

After using ERC20 for a few sprints, we were disappointed with the workarounds needed to solve the problems stated above. So we decided to explore the Aion Token Standard (ATS), the first proposed and accepted standard for fungible tokens in the Aion network. It’s based on ERC-777 (from Ethereum), with additional functions that manage Aion’s cross-chain capability. It also provided a brilliant hook structure that support inter-smart-contract interaction natively within Aion network. ATS turned out our to be a natural fit to our project so we didn’t think twice.

Handling token ‘payments’ to your smart contract with ATS
At the heart of ATS is the token registry, which is a smart contract itself deployed by the Aion Foundation. This keeps track of the methods implemented by other smart contracts, the most important one being “tokensReceived”.

When users send PLAT tokens to our Cranberry smart contract, the PLAT contract checks if the ‘to’ address (the Cranberry contract) is implementing the ‘tokensReceived’ method. This check is done easily through the ATS token registry. Since the Cranberry smart contract implemented the “tokenReceived” method, this method is invoked and subsequently creates tickets for the users that send in PLAT tokens.

Below is a diagram of how the process works.

And that is how we implemented our inter-smart-contract interaction easily through ATS. If you’d like to sign up for Cranberry and see the dApp in action, here’s the link again. Also keep a look-out for our future dApps by following us on Medium and Twitter.

Questions? Ask us on reddit

--

--