-
Notifications
You must be signed in to change notification settings - Fork 280
/
15_basic_info_getter.sol
234 lines (209 loc) · 5.49 KB
/
15_basic_info_getter.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
This is a demonstration of the various global variables available to contracts.
This list is probably not exhaustive, especially weeks and months from now. (9/2015)
*/
contract basicInfoGetter {
address creator;
function basicInfoGetter() public
{
creator = msg.sender;
}
function getCurrentMinerAddress() constant returns (address) // get CURRENT block miner's address,
{ // not necessarily the address of the miner when this block was born
return block.coinbase;
}
function getCurrentDifficulty() constant returns (uint)
{
return block.difficulty;
}
function getCurrentGaslimit() constant returns (uint) // the most gas that can be spent on any given transaction right now
{
return block.gaslimit;
}
function getCurrentBlockNumber() constant returns (uint)
{
return block.number;
}
function getBlockTimestamp() constant returns (uint) // returns current block timestamp in SECONDS (not ms) from epoch
{
return block.timestamp; // also "now" == "block.timestamp", as in "return now;"
}
function getMsgData() constant returns (bytes) // The data of a call to this function. Always returns "0xc8e7ca2e" for me.
{ // adding an input parameter would probably change it with each diff call?
return msg.data;
}
function getMsgSender() constant returns (address) // Returns the address of whomever made this call
{ // (i.e. not necessarily the creator of the contract)
return msg.sender;
}
function getMsgValue() constant returns (uint) // returns amt of wei sent with this call
{
return msg.value;
}
/*** A note about gas and gasprice:
Every transaction must specify a quantity of "gas" that it is willing to consume (called startgas),
and the fee that it is willing to pay per unit gas (gasprice). At the start of execution,
startgas * gasprice ether are removed from the transaction sender's account.
Whatever is not used is immediately refunded.
*/
function getMsgGas() constant returns (uint)
{
return msg.gas;
}
function getTxGasprice() constant returns (uint) // "gasprice" is the amount of gas the sender was *willing* to pay. 50000000 for me. (geth default)
{
return tx.gasprice;
}
function getTxOrigin() constant returns (address) // returns sender of the transaction
{ // What if there is a chain of calls? I think it returns the first sender, whoever provided the gas.
return tx.origin;
}
function getContractAddress() constant returns (address)
{
return this;
}
function getContractBalance() constant returns (uint)
{
return this.balance;
}
/**********
Standard kill() function to recover funds
**********/
function kill()
{
if (msg.sender == creator)
suicide(creator); // kills this contract and sends remaining funds back to creator
}
}
/*
var abi = [{
"constant": true,
"inputs": [],
"name": "getContractAddress",
"outputs": [{
"name": "",
"type": "address"
}],
"type": "function"
}, {
"constant": false,
"inputs": [],
"name": "kill",
"outputs": [{
"name": "",
"type": "bool"
}],
"type": "function"
}, {
"constant": true,
"inputs": [],
"name": "getContractBalance",
"outputs": [{
"name": "",
"type": "uint256"
}],
"type": "function"
}, {
"constant": true,
"inputs": [],
"name": "getCurrentBlockNumber",
"outputs": [{
"name": "",
"type": "uint256"
}],
"type": "function"
}, {
"constant": true,
"inputs": [],
"name": "getTxGasprice",
"outputs": [{
"name": "",
"type": "uint256"
}],
"type": "function"
}, {
"constant": true,
"inputs": [],
"name": "getBlockTimestamp",
"outputs": [{
"name": "",
"type": "uint256"
}],
"type": "function"
}, {
"constant": true,
"inputs": [],
"name": "getMsgSender",
"outputs": [{
"name": "",
"type": "address"
}],
"type": "function"
}, {
"constant": true,
"inputs": [],
"name": "getCurrentGaslimit",
"outputs": [{
"name": "",
"type": "uint256"
}],
"type": "function"
}, {
"constant": true,
"inputs": [],
"name": "getMsgGas",
"outputs": [{
"name": "",
"type": "uint256"
}],
"type": "function"
}, {
"constant": true,
"inputs": [],
"name": "getCurrentDifficulty",
"outputs": [{
"name": "",
"type": "uint256"
}],
"type": "function"
}, {
"constant": true,
"inputs": [],
"name": "getMsgValue",
"outputs": [{
"name": "",
"type": "uint256"
}],
"type": "function"
}, {
"constant": true,
"inputs": [],
"name": "getTxOrigin",
"outputs": [{
"name": "",
"type": "address"
}],
"type": "function"
}, {
"constant": true,
"inputs": [],
"name": "getMsgData",
"outputs": [{
"name": "",
"type": "bytes"
}],
"type": "function"
}, {
"constant": true,
"inputs": [],
"name": "getCurrentMinerAddress",
"outputs": [{
"name": "",
"type": "address"
}],
"type": "function"
}, {
"inputs": [],
"type": "constructor"
}];
*/