Skip to content Skip to sidebar Skip to footer

Blockchain Engineering: Solidity Tutorials -Role-3.

This is continuation of the blogs

Special Variables as well as Functions

There are particular variables as well as functions which ever exist in the global namespace.

Block and Transaction Properties

  • block.coinbase (address): current block miner'second address
  • block.difficulty (uint): current block difficulty
  • block.gaslimit (uint): electric current block gaslimit
  • block.number (uint): current block issue
  • block.blockhash (function(uint) returns (bytes32)): hash of the given block
  • block.timestamp (uint): current block timestamp
  • msg.information (bytes): complete calldata
  • msg.gas (uint): remaining gas
  • msg.sender (address): sender of the message (current phone call)
  • msg.value (uint): number of wei sent with the message
  • immediately (uint): current block timestamp (alias for block.timestamp)
  • tx.gasprice (uint): gas cost of the transaction
  • tx.origin (address): sender of the transaction (total telephone call chain)

Cryptographic Functions

  • sha3(...) returns (bytes32): compute the SHA3 hash of the (tightly packed) arguments
  • sha256(...) returns (bytes32): compute the SHA256 hash of the (tightly packed) arguments
  • ripemd160(...) returns (bytes20): compute RIPEMD of 256 the (tightly packed) arguments
  • ecrecover(bytes32, byte, bytes32, bytes32) returns (address): recover public cardinal from elliptic bend signature
In the to a higher place, "tightly packed" way that the arguments are concatenated without padding, 1.e.sha3("ab", "c") == sha3("abc") == sha3(0x616263) == sha3(6382179) = sha3(97, 98, 99). If padding is needed, explicit type conversions can be used.
  • this (current contract'sec type): the current contract, explicitly convertible to address
  • suicide(address): suicide the current contract, sending its funds to the given address
Furthermore, all functions of the electric current contract are callable straight including the current role.

Functions on addresses

It is possible to query the residual of an address using the belongings residual as well as to send Ether (inward units of wei) to an address using the post office:
address 10 = 0x123; if (ten.residual < 10 && address(this).balance >= 10) ten.send(ten); 
Furthermore, to interface with contracts that make non adhere to the ABI (like the classic NameReg contract), the role telephone call is provided which takes an arbitrary number of arguments of whatever type. These arguments are ABI-serialized (ane.e. besides padded to 32 bytes). One exception is the example where the get-go statement is encoded to precisely iv bytes. In this case, it is non padded to let the purpose of function signatures hither.
address nameReg = 0x72ba7d8e73fe8eb666ea66babc8116a41bfb10e2; nameReg.call("register", "MyName"); nameReg.phone call(bytes4(sha3("fun(uint256)")), a); 
Note that contracts inherit all members of address, and so it is possible to inquiry the remainder of the current contract using this.remainder.

Order of Evaluation of Expressions

The evaluation social club of expressions is non specified (more than formally, the order inwards which the children of 1 node inwards the aspect tree are evaluated is non specified, simply they are of course evaluated before the node itself). It is solely guaranteed that statements are executed inward guild and short-circuiting for boolean expressions is done.

Arrays

Both variably as well as fixed size arrays are supported inward storage in addition to every bit parameters of external functions:
contract ArrayContract    uint[two**xx] m_aLotOfIntegers;   bool[2][] m_pairsOfFlags;   role setAllFlagPairs(bool[2][] newPairs)      // assignment to array replaces the complete array     m_pairsOfFlags = newPairs;      function setFlagPair(uint index, bool flagA, bool flagB)      // access to a non-existing index will stop execution     m_pairsOfFlags[index][0] = flagA;     m_pairsOfFlags[index][1] = flagB;      part changeFlagArraySize(uint newSize)      // if the new size is smaller, removed array elements will be cleared     m_pairsOfFlags.length = newSize;      part clear()      // these clear the arrays completely     delete m_pairsOfFlags;     delete m_aLotOfIntegers;     // identical effect here     m_pairsOfFlags.length = 0;      bytes m_byteData;   function byteArrays(bytes information) external      // byte arrays ("bytes") are different as they are stored without padding,     // but can be treated identical to "uint8[]"     m_byteData = data;     m_byteData.length += 7;     m_byteData[3] = 8;     delete m_byteData[2];     

Structs

Solidity provides a fashion to define novel types inward the form of structs, which is shown inwards the next example:
contract CrowdFunding    struct Funder      address addr;     uint amount;      struct Campaign      address beneficiary;     uint fundingGoal;     uint numFunders;     uint amount;     mapping (uint => Funder) funders;      uint numCampaigns;   mapping (uint => Campaign) campaigns;   role newCampaign(address beneficiary, uint destination) returns (uint campaignID)      campaignID = numCampaigns++; // campaignID is return variable     Campaign c = campaigns[campaignID];  // assigns reference     c.beneficiary = beneficiary;     c.fundingGoal = goal;      office contribute(uint campaignID)      Campaign c = campaigns[campaignID];     Funder f = c.funders[c.numFunders++];     f.addr = msg.sender;     f.amount = msg.value;     c.amount += f.amount;      office checkGoalReached(uint campaignID) returns (bool reached)      Campaign c = campaigns[campaignID];     if (c.amount < c.fundingGoal)       return false;     c.beneficiary.send(c.amount);     c.amount = 0;     return true;     
The contract does not provide the full functionality of a crowdfunding contract, only it contains the basic concepts necessary to empathise structs. Struct types tin can live used as value types for mappings too they can itself contain mappings (fifty-fifty the struct itself can be the value type of the mapping, although it is non possible to include a struct equally is inside of itself). Note how in all the functions, a struct type is assigned to a local variable. This does not copy the struct but alone store a reference and then that assignments to members of the local variable actually write to the land.

Enums

Enums are another manner to create a user-defined type inward Solidity. They are explicitly convertible to in addition to from all integer types simply implicit conversion is not allowed. The variable of enum type tin live declared as constant.
contract examination      enum ActionChoices  GoLeft, GoRight, GoStraight, SitStill      ActionChoices choices;     ActionChoices constant defaultChoice = ActionChoices.GoStraight;     function setGoStraight()              choices = ActionChoices.GoStraight;          role getChoice() returns (uint)              return uint(choices);          function getDefaultChoice() returns (uint)              return uint(defaultChoice);