HomeCoinsAeternity (AE)Types and Functions in Sophia. Now that we’ve covered the basics of…...

Types and Functions in Sophia. Now that we’ve covered the basics of… | by Djongo | Oct, 2021

- Advertisement -

Djongo
Types and Functions in Sophia. Now that we’ve covered the basics of… | by Djongo | Oct, 2021
Getting round the basics!

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:

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.

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.

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….

Read the original article here

- Advertisement -
Mr Bitcointehttps://www.bitcointe.com/
“Fact You Need To Know About Cryptocurrency - The first Bitcoin purchase was for pizza.” ― Mohsin Jameel
462FansLike
76FollowersFollow
4,567FollowersFollow
5,261FollowersFollow
1,560FollowersFollow
2,230SubscribersSubscribe

Most Popular

bitcoin
Bitcoin (BTC) $ 41,697.00
ethereum
Ethereum (ETH) $ 3,099.55
tether
Tether (USDT) $ 0.998973
bitcoin-cash
Bitcoin Cash (BCH) $ 369.91
litecoin
Litecoin (LTC) $ 137.03
eos
EOS (EOS) $ 2.75
okb
OKB (OKB) $ 25.16
tezos
Tezos (XTZ) $ 3.90
leo-token
LEO Token (LEO) $ 3.68
cardano
Cardano (ADA) $ 1.34
monero
Monero (XMR) $ 201.74
stellar
Stellar (XLM) $ 0.246574
chainlink
Chainlink (LINK) $ 21.66
huobi-token
Huobi Token (HT) $ 9.95
tron
TRON (TRX) $ 0.06918
usd-coin
USD Coin (USDC) $ 0.998375
dash
Dash (DASH) $ 127.50
neo
NEO (NEO) $ 23.95
iota
IOTA (MIOTA) $ 1.05
nem
NEM (XEM) $ 0.112585
zcash
Zcash (ZEC) $ 126.58
maker
Maker (MKR) $ 1,998.50
paxos-standard
Pax Dollar (USDP) $ 0.997299
ethereum-classic
Ethereum Classic (ETC) $ 31.49
vechain
VeChain (VET) $ 0.071041
true-usd
TrueUSD (TUSD) $ 0.997304
ftx-token
FTX Token (FTT) $ 46.66
kucoin-shares
KuCoin Token (KCS) $ 19.96
waves
Waves (WAVES) $ 13.29