ethereum_smartcontract_intro

Форк
0
127 строк · 4.1 Кб
1
// SPDX-License-Identifier: UNLICENSED
2
pragma solidity >=0.8;
3

4
// Uncomment this line to use console.log
5
import "hardhat/console.sol";
6

7

8

9
abstract contract DebugPrintContract {
10

11

12
    function GetContractName () internal virtual view returns (string memory);
13

14
    function PrintInfo () public payable {
15

16
        string memory msgStr = GetContractName();
17

18
        console.log ("[%s] gasleft = %s\n", msgStr, gasleft());
19
        console.log ("[%s] msg.sender = %s\n", msgStr, msg.sender);
20
        //console.log ("[%s] msg.sig = %s\n", msgStr, msg.sig);
21
        console.log ("[%s] msg.value = %s\n", msgStr, msg.value);
22
        //console.log ("[%s] msg.data = %s\n", msgStr, msg.data);
23

24
        console.log ("[%s] tx.gasprice = %s\n", msgStr, tx.gasprice);
25
        console.log ("[%s] tx.origin = %s\n", msgStr, tx.origin);
26

27
        console.log ("[%s] block.chainid = %s\n", msgStr, block.chainid);
28
        console.log ("[%s] block.coinbase = %s\n", msgStr, block.coinbase);
29
        //console.log ("[%s] block.difficulty = %s\n", msgStr, block.difficulty);
30
        console.log ("[%s] block.prevrandao = %s\n", msgStr, block.prevrandao);
31
        console.log ("[%s] block.gaslimit = %s\n", msgStr, block.gaslimit);
32
        console.log ("[%s] block.number = %s\n", msgStr, block.number);
33
        console.log ("[%s] block.timestamp = %s\n", msgStr, block.timestamp);
34
        console.log ("[%s] blockckhash(block.number) = %s\n", msgStr, uint(blockhash(block.number)));
35
        console.log ("[%s] blockckhash(block.number-1) = %s\n", msgStr, uint(blockhash(block.number-1)));
36
        console.log ("[%s] blockckhash(block.number+1) = %s\n", msgStr, uint(blockhash(block.number+1)));
37

38
        console.log ("[%s] codehash = %s\n", msgStr, uint(address(this).codehash));
39
        
40
        // При вызове из конструктора код будет пустым.
41
        PrintBytes (address(this).code);
42
    }
43

44
    function PrintBytes (bytes memory arr) public view {
45
        
46
        string memory msgStr = GetContractName();
47
        console.log ("[%s] arr: ", msgStr);
48
        /*
49
        string memory msgStr = contractName;
50
        console.log ("[%s] arr.length = %s\n", msgStr, arr.length);
51
        console.log ("[%s] arr\n", msgStr);
52
        for (uint i = 0; i < arr.length; ++i) {
53
            console.log ("%o\n", uint8(arr[i]));
54
        }
55
        console.log ("\n");
56
        */
57

58
        console.logBytes (arr);
59
    }
60

61
    function DebugPrint (uint a) public view {
62
        string memory msgStr = GetContractName();
63
        console.log ("[%s] %s\n", msgStr, a);
64
    }
65

66
    function DebugPrint (string memory a) public view {
67
        string memory msgStr = GetContractName();
68
        console.log ("[%s] %s\n", msgStr, a);
69
    }
70

71
    function DebugPrint (string memory s, uint a) public view {
72
        string memory msgStr = GetContractName();
73
        console.log ("[%s] %s %s\n", msgStr, s, a);
74
    }
75

76
    function DebugPrint (string memory s, uint a, uint b) public pure {
77
        console.log ("%s %s %s\n", s, a, b);
78
    }
79

80
    function DebugPrint (string memory s, address a) public view {
81
        string memory msgStr = GetContractName();
82
        console.log ("[%s] %s %s\n", msgStr, s, a);
83
    }
84

85
    function DebugPrint (string memory s, address a, uint b) public pure {
86
        console.log ("%s %s\n", s, a, b);
87
    }
88

89
}
90

91

92
library DebugStorageContract {
93

94
    function ReadStorageValue (uint addr) public view returns (uint value) {
95

96
        assembly {
97
                value := sload (addr)
98
            }
99
    }
100

101
    function WriteStorageValue (uint addr, uint value) public returns (uint prevValue) {
102
        
103
        assembly {
104
            prevValue := sload (addr)
105
            sstore (addr, value)
106
        }
107
    }
108

109
    function GetStorageSlot (uint[] storage a) internal pure returns (uint slot) {
110
        assembly {
111
            slot := a.slot
112
        }
113
    }
114

115
    function PrintStorage (uint addr, uint count) public view {
116

117
        console.log ("%s %s\n", addr, count);
118

119
        for (uint i = addr; i < count; ++i) {
120
            uint value = uint(ReadStorageValue (i));
121
            console.log ("%s: ", i);
122
            console.logAddress (address(uint160(value)));
123
        }
124
    }
125

126

127
}
128

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.