![]() |
|
[Tut] Solidity Ether Units, Time Units, and Global Variables - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Solidity Ether Units, Time Units, and Global Variables (/thread-100247.html) |
[Tut] Solidity Ether Units, Time Units, and Global Variables - xSicKxBot - 11-17-2022 Solidity Ether Units, Time Units, and Global Variables 5/5 – (1 vote) With this article, we’re opening a new area of our study, that of units and globally available variables in Solidity. To begin with, we’ll learn about ether units and time units. After that, we’ll start with a block of subsections on special variables and functions, stretching through this and the next two articles. ![]() It’s part of our long-standing tradition to make this (and other) articles a faithful companion, or a supplement to the official Solidity documentation. Ether Units ![]() When mentioning Ether units of currency, we can express them with a literal number and a suffix of wei, gwei or ether. These suffixes specify a sub-denomination of Ether. We assume amounts written without a suffix as Wei. ![]() The purpose and effect of using sub-denomination is a multiplication of the denomination by a power (exponent) of ten. Code: assert(1 wei == 1);
assert(1 gwei == 1e9);
assert(1 ether == 1e18); Note: There can be found denominations, such as finney and szabo, but they were deprecated in Solidity v0.7.0.Time Units ![]() Solidity has a nice and natural way of expressing time units with suffixes, such as seconds, minutes, hours, days, and weeks. Seconds are the base unit, and units are considered to correspond to:
![]() When working with calendar calculations using these units, we should take extra care, because only some years have 365 days, and because of leap seconds, not even every day has 24 hours. Since leap seconds are unpredictable, an exact calendar always has to be updated by an external source (oracle), which motivated Solidity authors to remove the suffix years in Solidity v0.5.0. We should remember that these suffixes cannot be applied to variables, meaning if we want to interpret a function parameter expressed in days, we can easily do so like in the example below: Code: function f(uint start, uint daysAfter) public { if (block.timestamp >= start + daysAfter * 1 days) { // ... }
}The global namespace contains special variables and functions primarily used to provide us with information about the blockchain. They are also available utility functions for general use. ![]() Block and Transaction Properties The following is a list of block and transaction properties, as shown in the official Solidity documentation. Parentheses next to each block/transaction property define the Code: member
Note: We should expect the values of all members of Code: msgCode: msg.senderCode: msg.value Info: Off-chain computation is simply a computation that takes place outside a blockchain. Oracle networks can provide a trust-minimized form of off-chain computation to extend the capabilities of blockchains – this is known as oracle computation. (Chainlink)Contracts can be evaluated both off-chain and on-chain, i.e. in the context of a transaction included in a block. If a contract is evaluated off-chain, we should assume that Code: block.*Code: tx.* Note: It is suggested to avoid Code: block.timestampCode: blockhash Info: “The word canonical is used to indicate a particular choice from a number of possible conventions. This convention allows a mathematical object or class of objects to be uniquely identified or standardized.” (Wolfram.com)Only the recent 256 blocks’ hashes are available, due to scalability reasons. A hash of any older block will be zero. In Solidity versions prior to 0.5, the current Code: blockhash(...)Code: block.blockhash(...)Code: gasleft(...)Code: msg.gas(...)In Solidity v0.7.0 the Code: nowCode: block.timestampABI Encoding and Decoding Functions ![]() The following list contains ABI-appropriate functions for low-level interactions with EVM, as laid out in the official Solidity documentation:
Note: “These encoding functions can be used to craft data for external function calls without actually calling an external function. Furthermore, Code: keccak256(abi.encodePacked(a, b))Conclusion ![]() In this article, we learned about ether and time units, followed by special variables and functions. First, we introduced ether units and discussed the use of the main unit and its sub-denominations. Second, we introduced time units and discussed the possibilities of expressing time in different time units. Third, we took a closer look at the block and transaction properties, but also listed many of them with descriptions and notes on specific behaviors for a more thorough understanding. Fourth, we also touched on the topic of ABI encoding and decoding functions, described them, and gave a usage hint in a form of a note. What’s Next? This tutorial is part of our extended Solidity documentation with videos and more accessible examples and explanations. You can navigate the series here (all links open in a new tab): Prev Tutorial Syllabus Next Tutorial
|