HomeCoinsAeternity (AE)Sophia Smart Contracts 101. How to write in Sophia — Your first… | by...

Sophia Smart Contracts 101. How to write in Sophia — Your first… | by Djongo | Oct, 2021

- Advertisement -

Djongo

How to write in Sophia — Your first Token contract

Writing smart contracts for blockchain based applications is often described as no easy feat. One would have to know one’s way around robust, unfriendly, and hostile new surroundings which use weird syntax and strange constructs.

And this is true. Well, for the most part and in most cases. But — not with Sophia.

Sophia is a smart contract language, created to utilize the full potential of the æternity protocol. It has been specifically designed to offer developers a smooth coding experience fully customized for smart contracts implementation, while protecting contract designers against vulnerabilities that can be exploited.

æstudio is here to help you take a load off and focus on the big picture. Powered by sophisticated and easy-to-use Sophia smart contract language, æstudio allows developers on all levels of experience and expertise to write smart contracts with relative ease — and fast!

Of course, like last time, there is a rather awesome video on the subject:

Anyways, let’s take a quick look as to how to do all this.

How to do it?

You can start by accessing æstudio, navigating to a new tab, and deleting all of the example code. Now you can get cracking on your on smart contract!

The first thing you ought to do is to name the contract by using a name that starts with a capital letter.

Like all contracts, the smart contract has a state it is in. Sophia, being security-first, does not allow you to scatter your state variables across your code, unlike other languages, but asks you to put it in an object called “state”. The type name used for this is “record”. Note, please, a useful tip for writing in Sophia — it uses indentations instead of brackets.

Now, focusing on the three things every token should have: a name, total supply, and the balances.

Defining the variables of a contract helps you interact with three basic types of Sophia: The total supply of the token is an integer and its name is a string; the balances are stored in a map, which assign some input value of some type to an output value of some type.

Initializing these state variables is performed by the init function. (in Sophia, public functions are called entry points. Init executes only on deployment and can never be run again).

First, input the parameters needed to initialize the contract. Note that init not only initializes your contract, but also everything in your contracts state. You do this by returning a record containing data for everything in your state. (Remember that everything in your state must have an assigned value)

Call.caller is a keyword that always references the wallet address of a caller of a function, like, for example, the sender of a transaction. Peering under the hood for a quick moment: the init function is expected to return the value of the record type state, but seeing as how that’s always the case with init — it can be omitted in the return type definition.

A cool feature of Sophia is that it requires no return statement — your last expression is automatically the return statement. Again, for some cool examples of functions, check out this how-to explainer video made by æternity’s very own awesome Nikita!

Deploying a Token

On the right hand side in æstudio, you can see if your contract has successfully compiled. In this case, æstudio should be asking you for the values to pass to your contracts edit functions when being deployed to the æternity blockchain. Just enter the init parameters and hit the “deploy” button. At this point, æstudio does the heavy lifting and signs and sends the contract to be included into the æternity blockchain.

If at any point something goes awry, just log in with your Github account to get your personal testing accounts, as covered in the previous article and in this video.

And that’s it! Your first contract has been deployed, congratulations!

You can use æstudio further, to check your getter functions, check the token name from the smart contract, and by copy/pasting accounts addresses to check the account balance for any tokens you might have already sent.

Finally, what’s left, is to define a transfer function. Seeing as how this writes data and changes the state of a contract, it needs to be done via a stateful entry point. It takes at least two parameters — the recipient’s address and the amount. Of course, don’t forget to check if the center has the amount required for sending and make sure that the transfer amount is not a negative number.

Also, take note of Sophia’s built in “require” function.

This function requires the first parameter to be some true condition, or else the transaction executing the smart contract will fail and revert all changes done so far. Should all of the checks pass, we update the sender’s and recipient’s balance accordingly.

To put the intended state changes into effect, however, you need to perform them in Sophia’s built in “put” function.

The “put” function keeps the code clean and neat, and forces users to explicitly state their intentions about the overall state of the smart contract. This creates in environment in which choosing to change some local variables that ends up accidentally altering the entire contract is not an option.

Wrapping up the token contract, the last thing you need to do is to add the transferred tokens to the recipients balance. Now, you can deploy the contract!

There, that wasn’t hard, was it? Make sure to check out Nikita’s video covering the matter for a more in depth look at all the things we discussed here today! And, here’s the entire playlist æternity did on Sophia smart contract language.

What would you use your smart contract for would you run? Did you find this tutorial and the video helpful? Let us know in the comments below!

Also, be sure to check out this community made tutorial on how to build an aepp! Of course, further discussions on theæternity forum as well as our telegram group are more than welcome!

Get in touch: GitHub | Forum | Reddit | Twitter | YouTube| LinkedIn



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,548FollowersFollow
2,230SubscribersSubscribe

Most Popular

bitcoin
Bitcoin (BTC) $ 49,195.00
ethereum
Ethereum (ETH) $ 4,187.30
tether
Tether (USDT) $ 1.00
bitcoin-cash
Bitcoin Cash (BCH) $ 452.36
litecoin
Litecoin (LTC) $ 151.55
eos
EOS (EOS) $ 3.18
okb
OKB (OKB) $ 25.59
tezos
Tezos (XTZ) $ 3.97
leo-token
LEO Token (LEO) $ 3.53
cardano
Cardano (ADA) $ 1.34
monero
Monero (XMR) $ 197.44
stellar
Stellar (XLM) $ 0.272849
chainlink
Chainlink (LINK) $ 18.53
huobi-token
Huobi Token (HT) $ 8.82
tron
TRON (TRX) $ 0.084607
usd-coin
USD Coin (USDC) $ 1.00
dash
Dash (DASH) $ 132.77
neo
NEO (NEO) $ 28.22
iota
IOTA (MIOTA) $ 1.13
nem
NEM (XEM) $ 0.131366
zcash
Zcash (ZEC) $ 173.58
maker
Maker (MKR) $ 2,522.64
paxos-standard
Pax Dollar (USDP) $ 1.00
ethereum-classic
Ethereum Classic (ETC) $ 37.85
vechain
VeChain (VET) $ 0.087601
true-usd
TrueUSD (TUSD) $ 1.00
ftx-token
FTX Token (FTT) $ 42.29
kucoin-shares
KuCoin Token (KCS) $ 20.66
waves
Waves (WAVES) $ 19.20