-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path020-global-variables.sol
64 lines (51 loc) · 1.71 KB
/
020-global-variables.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
/**
* Global variables(special variables) in solidity
* These are variables that provide information about the blockchain
*
* As examples we have:
* msg.sender - sender of the message (current call),
* msg.value (uint) - number of wei sent with the message,
* block.timestamp - current block timestamp as seconds since unix epoch,
* block.number - current block number
*/
// helpful link to learn more about global variables and a lot of examples,
// their uses and warnings.
// https://docs.soliditylang.org/en/v0.8.15/units-and-global-variables.html
contract LedgerBalance {
mapping(address => uint256) balance;
// update the balance of the current sender, initiator of the transaction
function updateBalance(uint256 newBalance) public {
balance[msg.sender] = newBalance;
}
}
// update the balance from another contract
// interaction between two contracts
contract Updated {
function updateBalance() public {
// creating an instance of the LedgerBalance contract
LedgerBalance ledgerBalance = new LedgerBalance();
// updating the balance
ledgerBalance.updateBalance(40);
}
}
// Other global variables
contract SimpleStorage {
uint256 storeData;
uint256 difficulty;
uint256 timestamp;
uint256 number;
function set(uint256 x) public {
storeData = x;
// diffculty of the current block
difficulty = block.difficulty;
// time of interaction with the blockchain
timestamp = block.timestamp;
// block number
number = block.number;
}
function get() public view returns (uint256) {
return storeData;
}
}