Your First Look at Smart Contract Development for æternity

Daniel Chakraborty
Lumos Labs
Published in
7 min readNov 16, 2022

If you look at a top ten list of programming languages year after year, there’s no sign of one that seeks to implement pure functions. No, we’re not talking about procedural programming here.

In other words, there’s not a single functional programming language that finds favour with folks who code for a living to make it to a top ten list.

Despite the fact that this programming paradigm does away with a number of problems that conventional programming still cannot come to terms with.

Breaking Down the Functional Programming Paradigm

What makes functional programming interesting is the way it approaches the handling of ‘state’ and how concise yet powerful a block of code truly is.

However, functional programming isn’t just interesting from an academic standpoint: it’s a paradigm that potentially opens doors to parallel processing, thanks to the way it handles ‘state’.

Or more accurately, the way a program can easily sidestep the possible scenario of a ‘race condition’ that is very possible with conventional programming languages. Multi-threading becomes a breeze, quite frankly, since side effects are a thing of the past!

So, what constitutes a functional programming language?

Based on lambda calculus, a functional programming language is ‘declarative’ in nature. Think about how you would style a web page by using CSS, as shown below:

Declarative style in CSS

If you haven’t noticed this already, this style of programming doesn’t provide instructions on how a problem should be solved but merely what to solve. In the case above, this involves styling the h1, paragraph and the red_txt class with the listed fonts, colors and borders as specified.

Map, reduce and filter functions

That said, this lack of popularity of functional programming languages also arises in the Web3 space. Where Solidity holds sway when it comes to building smart contracts while little is known about a functional programming language much like Sophia.

Your First Look at Smart Contract Code in Sophia

Much like Solidity, Sophia also uses the contract keyword just as we would use the ‘class’ keyword in an object-oriented programming language like Java. So, much like a simple Solidity smart contract that holds state and performs certain functions, Sophia smart contracts do no differently.

As you can see in the Simple Storage contract written by Sophia below:

Simple Storage contract in Sophia

Along with using ‘type’ to declare the state of the contract, we have three functions in this code snippet: init, get and set, which perform the usual initialization, getter and setter operations. Finally, the put function updates the state that was initialized as the value of the variable number in the init function.

Clearly, as the contract name explicitly states, this is an example of how simple storage works in Sophia. But let’s do more than just store a number of type ‘integer’.

So, let’s look at the ubiquitous token smart contract in Sophia:

While the first line provides the compiler version, the next one declares the contract name as SimpleToken. As for defining the state of the contract, this will be done as a record, as in the block of code following the second line.

Now, the ‘entry point’ keyword that can be seen in the next four blocks of code can be called from outside the contract. In this case, this involves the functions name, balance, transfer as well as the init function too. The ‘stateful’ keyword in the transfer function indicates that state changes will occur.

Finally, the ‘put’ function, as mentioned earlier, updates the balances according to the transfer of the tokens made. Not before a couple of checks are made on the token balance value by using the ‘require’ keyword.

In looking at these two examples, you should have noticed the following benefits:

1: No side-effects
As you read through the 20-plus lines of code, there’s no chance of side effects given that all the variables’ values do not change once they are created, with the exception of the balances map that undergoes a change when the transfer function is called. In fact, the keyword stateful permits the function to make changes, in terms of balances. This is not possible in any other function as in the SimpleToken contract.

2: Strongly typed
A type in any paradigm of programming describes what the variable will hold. This could be primitive types such as string, integer and float. Or for that matter, even your own type, if you so choose to create one. If you look at all variables declared within the contract, it has a type assigned to it without exception. A couple of examples include account: address or name: string, as shown in the code above.

3: Shorter, readable code
Spanning about 20-plus lines of code, this Simple Token contract barely takes any time to go through. Even for first-timers to both Sophia and functional programming on the whole. This can be attributed to the fact that functional programming explicitly states what the program should do, rather than how it is supposed to achieve this objective.

Deploying Your First Sophia Smart Contract on the æternity Blockchain

In order to deploy and run your first Sophia smart contract, you’ll need to use the AEStudio tool along with the Simple Token code at this link.

Step 1

Open AEStudio at this link, to add your Simple Token smart contract code for execution. Access one of the examples smart contracts and delete the code. Add the Simple Token smart contract code in its place. Delete the other smart contract examples too by hitting ‘x’.

Result

Step 2

Locate the Settings section and select a Public Demo account with sufficient AE funds.

Result

Step 3

Look to the right of your screen and locate the Compiled Contract section.

Result

Step 4

Enter the initial balance of tokens along with adding a name and select Deploy.

Result

Step 5

Select the Simple Token smart contract in the Deployed Contracts section. It’s time to call the functions in the smart contract.

Result

Step 6

Select Call Locally in the name section.

Result

Step 7

Enter the address that you want to find the token balance for. In fact, you can enter the address of the smart contract itself to see how many tokens are available, which, for this demo, is 5000 tokens in all.

Result

As you can see, there’s a third function, namely transfer. In order to use this function, you’ll have to open SuperHero wallet accounts so as to perform such transactions. This is left as an exercise for those who are interested in exploring AEStudio further.

Sophia and smart contracts — Peas in a Pod?

Now, with the purpose for which Sophia has been designed, it’s been said that this language and smart contracts are like peas in a pod.

Given the aforementioned benefits of functional programming, this works in the favour of Sophia too, which is said to be immune to both overflow/underflow and reentrancy attacks that have proved to be expensive on the Ethereum platform.

In addition, the language offers additional features that make life easier for programmers. Particularly those who are familiar with the limitations that Solidity continues to struggle with. The more you work with Sophia, the more you’ll see this and which is outside the scope of this blog post.

So, do you think functional programming is a natural fit for smart contracts? Do share your thoughts in the comments section below.

If you’d also like to keep in touch with the latest updates in Web3 development as part of a growing community, you can join our Metaverse Discord Server below.

--

--

Daniel Chakraborty
Lumos Labs

Loves emerging tech, languages such as Python, JavaScript, Solidity & Haskell. Writes about Web3. Works at Lumos Labs.