Variáveis ​​especiais de solidity: informações de bloco e transação

Este artigo explicará algumas das variáveis ​​especiais do Solidity mais usadas com exemplos, além de suas funções no desenvolvimento de contratos inteligentes.

A primeira categoria de variáveis ​​especiais do Solidity são aquelas relacionadas a informações de bloco e transação.

Uma variável especial refere-se a uma variável disponível globalmente que contém informações sobre blockchains.

Essas variáveis ​​fornecem dados importantes sobre o estado atual do blockchain Ethereum, incluindo o número do bloco atual, o timestamp do bloco atual e o endereço do remetente de uma transação.

As variáveis ​​especiais estão listadas abaixo:

Variáveis ​​Especiais Visão geral
blockhash(uint blockNumber) retorna (bytes32) Hash de bloco – funciona apenas para os 256 blocos mais recentes, excluindo os blocos atuais.
block.coinbase (endereço pagável) Mostra o endereço dos mineiros recentes.
block.difficulty (uint) Exibe a dificuldade do bloco.
block.gaslimit (uint) Mostra o gaslimit utilizado pelo respectivo bloco.
block.number (uint) O número recente do bloco é impresso.
block.timestamp Tempo desde a época do bloco unix medido em segundos.
gasleft() retorna (uint256) Emite o gás restante do bloco/contrato.
msg.data (bytes calldata) Faz uma instância de dados para a msg.
msg.remetente (endereço a pagar) Mostra o remetente do bloco.
msg.sig (bytes4) Exibe os primeiros 4 bytes da msg.
msg.value (uint) Calcula o número de wei enviados pela mensagem.
agora (uint) Exibe a hora atual do bloco
tx.gasprice (uint) Exibe o preço do gás utilizado no bloco.
tx.origin (endereço a pagar) Mostra os remetentes de todo o blockchain.

O exemplo a seguir mostra o uso de variáveis ​​especiais:

Example: 

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// SPDX-License-Identifier: 3.0
pragma solidity ^0.8.0;
contract SolidityTest{
// It displays the blockhash of the number //
function get_Block_Hash(uint mrx_Blocknumber) public view returns (bytes32) {
return blockhash(mrx_Blocknumber);
}
// It displays the coin base of the block
function get_Block_coinbase() public view returns(address){
address mrx_Blocknumber=block.coinbase;
return mrx_Blocknumber;
}
// In order to obtain the timestamp of the block you can create the function as follow
function get_Block_timestamp() public view returns(uint){
return block.timestamp;
}
// if you want to find the block difficulty you can make a function for it as follow:
function get_Block_difficulty() public view returns(uint){
return block.difficulty;
}
// In latest version of solidity the difficulty keyword was replaced by prevrandao:
function get_Block_modified_Difficulty_Keyword() public view returns(uint){
return block.prevrandao;
}
// To find the gas limit of the block have a look at the function below:
function get_Block_gaslimit() public view returns(uint){
return block.gaslimit;
}
// THe block number could also be determined by uisng the function below:
function get_Block_Number() public view returns(uint){
return block.number;
}
// To check the gas left in the block we have created the method below:
function show_remaining_Gas() public view returns(uint256){
uint256 gas_remain=gasleft();
return gas_remain;
}
// To check the message data we can make a function below:
function msg_data_Result() public pure returns(bytes memory){
return msg.data;
}
// To check sender's address have a look at function below:
function show_msg_address() public view returns(address){
return msg.sender;
}
// To see first 4 bytes of the call data
function show_4_bytes() public pure returns(bytes4){
return msg.sig;
}
// To see the value of the message in ether we have created a function accordingly:
function get_value() public payable {
uint256 mrx;
mrx += msg.value;
}
// To calculate the price of the gas we can consider the function below:
function get_Gas_Value() public view returns(uint){
return tx.gasprice;
}
// Sender of the transaction is shown
function get_Sender_Origion() public view returns(address){
return tx.origin;
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>
Nota : se você achar difícil executar o código acima, dê uma olhada em nosso artigo Solidity First Application .


Conclusão

As variáveis ​​especiais Solidity são um recurso essencial que permite acessar e usar informações cruciais durante a execução do contrato inteligente.

Desde fornecer detalhes sobre o número do bloco atual até acessar o endereço do remetente da transação original, essas variáveis ​​especiais oferecem uma variedade de funcionalidades que tornam o desenvolvimento de contratos inteligentes no Ethereum mais eficiente e seguro.

Nós valorizamos o seu feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Assine a nossa newsletter
Digite seu e-mail para receber um resumo semanal de nossos melhores posts. Saber mais!
ícone