Smart Contracts in Solidity

Smart Contracts in Solidity


A smart contract is code located at a specific address  within the Ethereum blockchain. Smart contracts run inside an Ethereum virtual machine (EVM). The code it executes does not have access to the network, the file system, or other processes. It only has limited access to other smart contracts that are located on the blockchain.

There are two types of accounts:

  • External accounts that are controlled by key pairs (public/private)
  • Smart contract accounts that are controlled by the smart contract source code.

A smart contract account is created at the time the smart contract is deployed on the blockchain. Each account has a balance in units of wei (ether) that can be modified by sending transactions that include a value. A transaction is a message that is sent from one account to another. It can include binary data (payload) and ether. A call is a message that does not involve ether. For example, a call to a method of a smart contract.

Each transaction is charged with an amount of gas, which is consumed during its execution. The transaction creator sets the gas price, and you must pay a total = gas price * used gas from your ether account. If there is leftover gas when carrying out the transaction, it will be returned to you after execution.

Each smart contract account has a persistent memory region called "storage". It also has another region of memory that is created on each message call. Message calls are used to invoke smart contracts from other smart contracts or to send ether to non-smart contract accounts. They are similar to transactions.

Smart contracts can load dynamic code from another address. The "storage", address and balance refer to the smart contract that makes the call. This makes it possible to implement libraries in solidity. Moreover, smart contracts can  also store logs in a special region of the blockchain, and which are used in event management. Smart contracts cannot access logs after they are created, but they can be viewed from outside the blockchain, for example from a "thin client".

We can think of a smart contract as a class in object-oriented programming. The attributes would be the state variables and the properties would be the functions that can modify these state variables. It would also have its constructor (only one is allowed) although it is optional.

To define the compiler version of a smart contract, use the pragma statement with the solidity version and then contract :

pragma solidity ^0.8.4;

contract SaveData {
uint data;

function set(uint x) public {
data = x;
}

function get() public constant returns (uint) {
return data;
}
}


In this example smart contract doesn't do anything special, it just saves data that is an integer and anyone could call the set method to change it. Within a smart contract we usually define events, data structures and enums. Functions can have modifiers that change the behavior. For example visibility: as private or public.

Variables can also have visibility, so a private variable is visible to the rest of the blockchain actors, but is not accessible from the rest of the smart contracts. A public variable can be read by the rest of the smart contracts and actors. State variables are permanently stored in the smart contract.

Comments are written with // for a single line or with /* .... */ for multiple lines  and solidity source code files typically have the .sol extension and use the pep8 style guide inherited from Python .

Inheritance

Smart contracts can inherit from each other with the is statement. Multiple inheritance is allowed.

contract mycontract is contractfather {
variables
functions
}


New Operator and Destruction


A smart contract can create another smart contract with the new statement. Ether can be sent when creating the smart contract by calling the value() method after the constructor. A smart contract can be destroyed with the selfdestruct(address) call, sending the accumulated ether to that address. It is the only way to remove a smart contract from the blockchain. Although it is always advisable to have a state variable to deactivate it that causes throws to be launched when invoking its functions, so the ether will be returned. It is advisable to save the msg.sender in the constructor and then pass it as an argument to selfdestruct.

Abstract Contracts and Interfaces


They are smart contracts that have some functions without implementation. That is, the name of the function, the input and output parameters, and it ends with a semicolon are defined. These smart contracts are not compiled, they are used as base smart contracts for other smart contracts that inherit them. Though, Interfaces are similar to abstract smart contracts, but they cannot have any functions implemented. They also cannot inherit from other smart contracts or interfaces, they cannot define a constructor, variables, structs, or enums and are used in the smart contracts that inherit them. 


Thats all for today, in the later posts I will go into more details but I'll try to keep it simple and more practical, so stay with me and don't forget to like and follow!

How do you rate this article?

12



Blockchain Development
Blockchain Development

A blog that covers everything that's happening in crypto world.

Publish0x

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.