Solidity tutorial 3 "Basic syntax Part.2"

Ether and Time Units

A literal number can take a suffix of wei, finney, szabo or ether to convert between the subdenominations of Ether, where Ether currency numbers without a postfix are assumed to be "wei", e.g. 2 ether == 2000 finney evaluates to true.

Furthermore, suffixes of seconds, minutes, hours, days, weeks and years can be used to convert between units of time where seconds are the base unit and units are converted naively (i.e. a year is always exactly 365 days, etc.).

Control Structures

Most of the control structures from C/JavaScript are available in Solidity except for switch (not planned) and goto (note that it's called Solidity).

So there is: if, else, while, for, break, continue, return.

Note that there is no type conversion from non-boolean to boolean types as there is in C and JavaScript, so if (1) { ... } is not valid Solidity.

Function Calls

Functions of the current contract can be called directly, also recursively, as seen in this nonsensical example:

contract c { 

    function g(uint a) 
    returns (uint ret) {
         return f(); 
         } 
         
         function f() returns (uint ret) { 
             return g(7) + f
             }
}

The expression g(8) is also a valid function call, but this time, the function will be called via a message call and not directly via jumps.
When calling functions of other contracts, the amount of Wei sent with the call and the gas can be specified:

  • contract InfoFeed {function info() returns (uint ret) { return 42;}
  • contract Consumer {infoFeed feed; function setFeed(address addr) { feed = InfoFeed(addr); }
  function callFeed() { 
      feed.info.value(10).gas(800)(); 
      }

Do not forget that the expression InfoFeed(addr) also performs an explicit type conversion stating that "we know that the type of the contract at the given address is InfoFeed" and this does not execute a constructor.

Be careful in that feed.info.value(10).gas(800) only (locally) set the value and amount of gas sent with the function call and only the parentheses at the end perform the actual call.

Function call arguments can also be given by name, in any order:

contract

c {function f(uint key, uint value) { ... }
function g() {f({value: 2, key: 3});}

The names for function parameters and return parameters are optional.

contract test

function func(uint k, uint) returns(uint){return k};  

Special Variables and Functions

There are special variables and functions which always exist in the global namespace, some will be discussed in further articles.

Block and Transaction Properties:

  • block.coinbase (address): current block miner's address
  • block.difficulty (uint): current block difficulty
  • block.gaslimit (uint): current block gaslimit
  • block.number (uint): current block number
  • block.blockhash (function(uint) returns (bytes32)): hash of the given block
  • block.timestamp (uint): current block timestamp
  • msg.data (bytes): complete calldata
  • msg.gas (uint): remaining gas
  • msg.sender (address): sender of the message (current call)
  • msg.value (uint): number of wei sent with the message
  • now (uint): current block timestamp (alias for block.timestamp)
  • tx.gasprice (uint): gas price of the transaction
  • tx.origin (address): sender of the transaction (full call chain)

Next Lesson

Summary of Lessons

AUTHOR

READ NEXT

Boostlog is an online community for developers
who want to share ideas and grow each other.

Delete an article

Deleted articles are gone forever. Are you sure?