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 Javascript SDK)
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!
Firstly — strings , integers , and boolean (which are probably familiar to you already, from other languages). These are defined and passed to your function call as they are in javascript.
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.
Bytes — you define how many bytes you need and pass the corresponding area of bytes as strings to your function by via the javascript SDK.
Maps — these map date of a certain type to exactly one other piece of data of a certain type. Maps can be, in Sophia, both input and return data for functions, and could be nested almost endlessly deep. In the javascript SDK, pass a map with unique key value pairs.
Records — the key thing to know about records, which might remind you of objects, is that you may have many instances of that record type. The names of the keys, however, are fixed — just as the types of their values are. When you pass a record as a function call parameter using the javascript SDK, use an object and always be careful to match the expected key names and the corresponding value types correctly. The definition in your contract expects this.
List , or what you might know as arrays. Its entry types all need to be same in Sophia.
Tuple — these are like lists, but they can hold values of any type. However, there are possible sticked lists. In javascript you just pass an array.
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.
Read the full article on the æternity blog: Types and Functions in Sophia. Now that we’ve covered the basics of… | by Djongo | æternity blog