Advance Solidity concepts in detail - Part 1
- Avi
- Nov 23, 2020
- 4 min read
Why you should read this blog? > After reading this blog you will have an understanding of the following concepts:
⦾ Lifecycle of a Smart Contract,
⦾ Solidity Global Functions,
⦾ Events in Solidity
⦾Lifecycle of Smart Contract:
We know a contract is defined using the following four elements:
State Variables,
Functions,
Function Modifiers and
Events
Let's understand what happens at the time of installation of a smart contract over a network and what happens before a smart contract is destroyed:
Smart contract code:
contract CurrencyExchange{
//constructor with public visiblity
constructor() public {
}
//selfdestruct is a function to kill or delete the smart contract
//address parameter is used to send the balance ether of the current account to the address specified.
selfdestruct(address);
}Two major elements that define the birth and death of a smart contract in solidity are: ⦾Constructor and ⦾Selfdestruct Function
⦾Key points of Constructor: ⁃ Constructor is called when a contract is installed on a network.
⁃ Constructor is executed only once and never executed again.
⁃ Constructor is optional and only one constructor is allowed.
- After the constructor has executed, the final code of the contract is deployed to the blockchain. The code deployed does not include the constructor code or the internal functions only called from the constructor.
Constructor is defined as:
constructor(<parameters>) <visibility_type>
{ <code that you want to execute at time of contract deployment/installation> }
note: Solidity only allows the visibility type of a constructor to be public or internal. A constructor cannot be private or external.
⦾ Key points of Selfdestruct function:
⁃ selfdestruct function kills or deletes the entire smart contract from the blockchain.
⁃ this does not delete the contract account, and the contract account on which this contract code was deployed becomes a blank account.
Selfdestruct function is defined as:
selfdestruct(<address of the account to which the balance ethers will get transferred to>) ;
note: we need to pass an address of an account as a parameter to the selfdestruct function. This address is the address of the account to which all the balance ethers in the current contract account will be transferred.
⦾ Global Functions in Solidity:
As we know Ethereum Virtual machine is isolated from other applications or processes on the system, EVM has provided some inbuilt functions which are categorized as follows:
Address
Block
Transaction and
Message
⦾ Address: Any variable of the data type address can use the global functions defined in the Address context,
- balance: returns the balance of the address in Wei. e.g. address admin;
admin.balance; will return the balance of admin in Wei (return type uint256)

in the image above: function test1 will return the value in Wei and function test2 will return the value in Ether.
- transfer() : sends the given amount of Wei from the current account to the Address mentioned.
e.g. admin.transfer(uint256 x);
note: problem with the transfer function is that if an error occurs during the transaction, then the transaction fails.
- send(): the given amount of Wei from the current account to the Address mentioned.
e.g. admin.send(uint256 x);
return type of send() is bool.
- call(), staticcall() and delegatecall(): low-level functions. These functions don't go through the checks by the Solidity compiler, and, hence, it is advised not to use them unless absolutely necessary.
>call and staticcall functions work in a similar manner as the transfer or send function
delegatecall: A invokes B who makes a delegatecall to C, then the msg.sender in the delegatecall will be A and not B. This way we can preserve the original sender of the message.
⦾ Block:
- block.coinbase: returns the address of the miner that mined the current block.
- block.difficulty: returns the difficulty at the time when the current block was mined.
- block.timestamp: returns the timestamp at which the current block was mined.
- block.gaslimit: returns the total gaslimit of all the transactions mined in the current block.
- block.number: It returns the number of the newest block in the blockchain.
⦾ Transactions: used mainly to provide information about the transactions in the network.
- tx.gasprice: returns the gas price of the transaction sent by the sender as part of the transaction.
- tx.origin: returns the address of the original sender of the transaction.
⦾ Message:
- msg.value: returns the number of Wei that was sent with the message or the transaction.
- msg.sender: returns the immediate sender of the message or the transaction.
- msg.gasleft: returns the remaining gas for the transaction.
⦾ Events in Solidity:
An event in solidity is defined as:
e.g. event <event_name> (<event_parameters>)
We can call an Event using the emit keyword in the following manner:
e.g. emit <event_name> (<event_arguments>)
DApps, or anything connected to the Ethereum JSON-RPC API, can listen to these events and act accordingly. An Event can be indexed, so that the Event history is searchable later. We can use the indexed keyword before the parameter on the basis of which you want an Event to be indexed. This is useful in case someone wants to search the history and filter the Events on a particular index. These indexed parameters are stored inside the logs inside a special kind of data structure called 'topics'.




Comments