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!
Deploying a contract with the SDK is performed by calling the init method of the contract instance while the rest is done for you automatically. (Of course, this requires a properly set java SDK)
Should you prefer a video guide on the matter, you have one right here:
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…
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!
Of course, further discussions on the aeternity forum as well as the telegram group are more than welcome! Check out the entire æternity playlist about Sophia on YouTube, right here!
And, check these out too:
Sophia documentation: https://aeternity-sophia.readthedocs….
JS SDK Setup and Examples: https://aeternity-js-sdk.readthedocs….