ethereum_smartcontract_intro
127 строк · 4.1 Кб
1// SPDX-License-Identifier: UNLICENSED
2pragma solidity >=0.8;
3
4// Uncomment this line to use console.log
5import "hardhat/console.sol";
6
7
8
9abstract contract DebugPrintContract {
10
11
12function GetContractName () internal virtual view returns (string memory);
13
14function PrintInfo () public payable {
15
16string memory msgStr = GetContractName();
17
18console.log ("[%s] gasleft = %s\n", msgStr, gasleft());
19console.log ("[%s] msg.sender = %s\n", msgStr, msg.sender);
20//console.log ("[%s] msg.sig = %s\n", msgStr, msg.sig);
21console.log ("[%s] msg.value = %s\n", msgStr, msg.value);
22//console.log ("[%s] msg.data = %s\n", msgStr, msg.data);
23
24console.log ("[%s] tx.gasprice = %s\n", msgStr, tx.gasprice);
25console.log ("[%s] tx.origin = %s\n", msgStr, tx.origin);
26
27console.log ("[%s] block.chainid = %s\n", msgStr, block.chainid);
28console.log ("[%s] block.coinbase = %s\n", msgStr, block.coinbase);
29//console.log ("[%s] block.difficulty = %s\n", msgStr, block.difficulty);
30console.log ("[%s] block.prevrandao = %s\n", msgStr, block.prevrandao);
31console.log ("[%s] block.gaslimit = %s\n", msgStr, block.gaslimit);
32console.log ("[%s] block.number = %s\n", msgStr, block.number);
33console.log ("[%s] block.timestamp = %s\n", msgStr, block.timestamp);
34console.log ("[%s] blockckhash(block.number) = %s\n", msgStr, uint(blockhash(block.number)));
35console.log ("[%s] blockckhash(block.number-1) = %s\n", msgStr, uint(blockhash(block.number-1)));
36console.log ("[%s] blockckhash(block.number+1) = %s\n", msgStr, uint(blockhash(block.number+1)));
37
38console.log ("[%s] codehash = %s\n", msgStr, uint(address(this).codehash));
39
40// При вызове из конструктора код будет пустым.
41PrintBytes (address(this).code);
42}
43
44function PrintBytes (bytes memory arr) public view {
45
46string memory msgStr = GetContractName();
47console.log ("[%s] arr: ", msgStr);
48/*
49string memory msgStr = contractName;
50console.log ("[%s] arr.length = %s\n", msgStr, arr.length);
51console.log ("[%s] arr\n", msgStr);
52for (uint i = 0; i < arr.length; ++i) {
53console.log ("%o\n", uint8(arr[i]));
54}
55console.log ("\n");
56*/
57
58console.logBytes (arr);
59}
60
61function DebugPrint (uint a) public view {
62string memory msgStr = GetContractName();
63console.log ("[%s] %s\n", msgStr, a);
64}
65
66function DebugPrint (string memory a) public view {
67string memory msgStr = GetContractName();
68console.log ("[%s] %s\n", msgStr, a);
69}
70
71function DebugPrint (string memory s, uint a) public view {
72string memory msgStr = GetContractName();
73console.log ("[%s] %s %s\n", msgStr, s, a);
74}
75
76function DebugPrint (string memory s, uint a, uint b) public pure {
77console.log ("%s %s %s\n", s, a, b);
78}
79
80function DebugPrint (string memory s, address a) public view {
81string memory msgStr = GetContractName();
82console.log ("[%s] %s %s\n", msgStr, s, a);
83}
84
85function DebugPrint (string memory s, address a, uint b) public pure {
86console.log ("%s %s\n", s, a, b);
87}
88
89}
90
91
92library DebugStorageContract {
93
94function ReadStorageValue (uint addr) public view returns (uint value) {
95
96assembly {
97value := sload (addr)
98}
99}
100
101function WriteStorageValue (uint addr, uint value) public returns (uint prevValue) {
102
103assembly {
104prevValue := sload (addr)
105sstore (addr, value)
106}
107}
108
109function GetStorageSlot (uint[] storage a) internal pure returns (uint slot) {
110assembly {
111slot := a.slot
112}
113}
114
115function PrintStorage (uint addr, uint count) public view {
116
117console.log ("%s %s\n", addr, count);
118
119for (uint i = addr; i < count; ++i) {
120uint value = uint(ReadStorageValue (i));
121console.log ("%s: ", i);
122console.logAddress (address(uint160(value)));
123}
124}
125
126
127}
128