Solidity Syntax - Using For, Error Handling, Global Variables
In this tutorial, we will cover Solidity syntax, specifically on using the using - for
keyword, error handling, global variables, mathematical and cryptographic functions, and contract-related keywords.
Using - For​
The using A for B;
syntax in Solidity can be used to attach library functions to any type. For example, the following code attaches a library function called add
from the arithmatic
library to the uint
type:
library arithmatic {
function add(uint _a, uint _b) returns (uint) {
return _a + _b;
}
}
contract C {
using arithmatic for uint;
uint sum;
function f(uint _a) {
sum = _a.add(3);
}
}
In the above code, the library arithmatic
contains a function called add
that takes two uint
arguments and returns their sum. We then use the using arithmatic for uint
syntax to attach the add
function to the uint
type. This allows us to call the add
function on any uint
variable, as shown in the f
function.
Error Handling​
Solidity provides three functions for error handling: assert
, require
, and revert
.
assert(bool condition)
: throws if the condition is not met. This function is used for internal errors that should never occur, and its failure indicates a bug in the code.require(bool condition)
: throws if the condition is not met. This function is used for errors in inputs or external components.revert()
: abort execution and revert state changes. This function is used to explicitly trigger a revert without any error message.
function sendHalf(address addr) payable returns (uint balance) {
require(msg.value % 2 == 0); // Only allow even numbers
uint balanceBeforeTransfer = this.balance;
addr.transfer(msg.value / 2);
assert(this.balance == balanceBeforeTransfer - msg.value / 2);
return this.balance;
}
In the above code, the require
function checks that the msg.value
is an even number. If it is not, the function will throw an error and the transaction will be reverted. The assert
function checks that the balance of the contract before and after the transfer is as expected. If it is not, the function will throw an error and the transaction will be reverted.
Global Variables​
Solidity provides several global variables that can be used in smart contract development. These include block variables and transaction variables.
Block Variables​
block.blockhash(uint blockNumber) returns (bytes32)
: hash of the given block - only works for the 256 most recent blocks excluding currentblock.coinbase (address)
: current block miner’s addressblock.difficulty (uint)
: current block difficultyblock.gaslimit (uint)
: current block gaslimitblock.number (uint)
: current block numberblock.timestamp (uint)
: current block timestamp as seconds since unix epochnow (uint)
: current block timestamp (alias forblock.timestamp
)
Transaction Variables​
msg.data (bytes)
: complete calldatamsg.gas (uint)
: remaining gasmsg.sender (address)
: sender of the message (current call)msg.sig (bytes4)
: first four bytes of the calldata (i.e. function identifier)msg.value (uint)
: number of wei sent with the messagetx.gasprice (uint)
: gas price of the transactiontx.origin (address)
: sender of the transaction (full call chain)
Mathematical and Cryptographic Functions​
Solidity provides several built-in mathematical and cryptographic functions that can be used in smart contract development. Some of these functions include:
addmod(uint x, uint y, uint k) returns (uint)
: compute (x + y) % k where the addition is performed with arbitrary precision and does not wrap around at 2^256.mulmod(uint x, uint y, uint k) returns (uint)
: compute (x * y) % k where the multiplication is performed with arbitrary precision and does not wrap around at 2^256.keccak256(...) returns (bytes32)
: compute the Ethereum-SHA-3 (Keccak-256) hash of the (tightly packed) arguments.sha256(...) returns (bytes32)
: compute the SHA-256 hash of the (tightly packed) arguments.sha3(...) returns (bytes32)
: alias to keccak256.ripemd160(...) returns (bytes20)
: compute RIPEMD-160 hash of the (tightly packed) arguments.ecrecover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) returns (address)
: recover the address associated with the public key from elliptic curve signature or return zero on error (example usage).
Contract Related​
Solidity also provides several keywords that are related to contracts.
this (current contract’s type)
: the current contract, explicitly convertible to Address.selfdestruct(address recipient)
: destroy the current contract, sending its funds to the given Address.suicide(address recipient)
: alias to selfdestruct. Soon to be deprecated.
In this tutorial, we have covered Solidity syntax, including using the using - for
keyword, error handling, global variables, mathematical and cryptographic functions, and contract-related keywords. Solidity provides a powerful set of tools for building smart contracts on the Ethereum blockchain.