Now that we’ve covered the basics of Sophia, let’s take a deeper dive into some types and functions of this elegant smart contract language.
Quick recap: a contract is defined in Sophia by giving it a capitalized name; a state record is defined to hold all of the contract state varibles; Init function – remember to initialize all state variables with some data. Deploying a contract runs the init function.
NB – do not forget to indent your code blocks, brackets included!
Should you prefer a video guide on the matter, you have one right here:
Now for some functions!
Note that the preset examples in æstudio have other keywords as well – simple in nature, yet powerful in use. For example, entrypoint function could be called from outside the contract by anyone – so keep a close look on protecting them well.
Payable means that, when calling this function, you are allowed to send a long an Æ amount. The amount is calculated in ætos – æternity blockchains smallest subunit. There are library functions in the docs which allow for easy and convenient conversions. If you try to send Æ without having the payable mark will result in a contract call fail. Æ is stored within the contract and you can choose where to send it to, for example another wallet or another contract. Please note, contracts too need to be marked payable to be able to receive Æ with simple types of value transactions not including any function calls.
Stateful means that this function is allowed to alter the state variables of your contract. Again, you can see that action, live, in this video right here, by our very own Nikita! Useful to know: you can always write yourself a helper function for those variables you use very often while coding.
Finally, function declares a function that is callable only from within the contract, which makes it private. A private function can, of course, be stateful as well.
Note that, in order for Sophia contracts to function, all input parameters and return data from functions must be properly typed. Having that in mind…
Let’s look at some data types!
An address corresponds to a wallet or contract on the æternity blockchain.
Bits represent ones and zeroes and are passed as an array to your function code.
List, or what you might know as arrays. Its entry types all need to be same in Sophia.
Hash – an equivalent to any 32 bytes and exists to make it easier to use Sophias built in hash function.
Signature – Hash’s sibiling, an equivalent to any 64 bytes.
Hash and signature can be used for awesome things, such as checking if something was originally signed with a private key that corresponds to some address, without requiring the private key. (this too could be a highlight-vignette).>
Option. From syntaxes like typescript, you might know that parameters may be marked as optional – they may or may not get some value. In Sophia, option integer, for example, means that data can be either an integer or, simply, undefined. This integer, however, needs to be defined first if you wish to work with it inside your contract. You can do this with a switch construct that allows you to check if its a specific integer, some integer, or just undefined.
What do you think of Sophia types and functions? Let us know in the comments below and make sure to check out our Nikita’s awesome video on the subject!
And, check these out too:
Sophia documentation: https://aeternity.com/aesophia/latest/
JS SDK Setup and Examples: https://aeternity.com/aepp-sdk-js/latest/
Also, you can join the Sophia Discord channel: https://discord.gg/Pwfd46Pw