-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
145 lines (124 loc) · 3.84 KB
/
functions.php
File metadata and controls
145 lines (124 loc) · 3.84 KB
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
<?php
//Time constants
define("_TS_SEC", 1);
define("_TS_MIN", _TS_SEC * 60);
define("_TS_HOUR", _TS_MIN * 60);
define("_TS_DAY", _TS_HOUR * 24);
define("_TS_WEEK", _TS_DAY * 7);
define("_TS_YEAR", _TS_DAY * 365);
//crypto constants
define("SAT", 0.00000001);
function curl_post($url, $data, $headers = array())
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
function curl_get($url, $headers = array())
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
function http_rpc_query($method, $params, $cache_time = RPC_CACHE) {
global $_CHAIN, $_REDIS;
$req = json_encode(array("jsonrpc" => "1.0", "method" => $method, "params" => $params, "id" => 1));
$headers = array(
"Content-Type: text/plain",
"Authorization: Basic " . base64_encode($_CHAIN->rpc_user . ":" . $_CHAIN->rpc_password)
);
if($cache_time == -1 ) {
$res = json_decode(curl_post($_CHAIN->rpc_url, $req, $headers));
} else {
$cache_key = sprintf("hs:exp:%s:rpc-cache:%s:%s", $_CHAIN->name, $method, implode("-", $params));
$cache = $_REDIS->get($cache_key);
if($cache == false) {
$raw = curl_post($_CHAIN->rpc_url, $req, $headers);
if($cache_time == 0) {
$_REDIS->set($cache_key, $raw);
} else {
$_REDIS->setEx($cache_key, $cache_time, $raw);
}
} else {
$raw = $cache;
}
$res = json_decode($raw);
if(isset($res->error) && $cache != false) {
$_REDIS->del($cache_key); //reset the cache if there was an error
}
}
return $res->result;
}
function get_url($path) {
return sprintf(BASE_URL, $path);
}
function format_bytes($bytes, $prec = 2)
{
if ($bytes >= 1073741824) {
return number_format($bytes / 1073741824, $prec) . ' GiB';
} elseif ($bytes >= 1048576) {
return number_format($bytes / 1048576, $prec) . ' MiB';
} elseif ($bytes >= 1024) {
return number_format($bytes / 1024, $prec) . ' KiB';
}
return $bytes . ' B';
}
function format_metric($val, $prec = 2)
{
if($val >= 1e+18) {
return number_format($val / 1e+18, $prec) . ' E';
} elseif ($val >= 1e+15) {
return number_format($val / 1e+15, $prec) . ' P';
} elseif ($val >= 1e+12) {
return number_format($val / 1e+12, $prec) . ' T';
} elseif ($val >= 1e+9) {
return number_format($val / 1e+9, $prec) . ' G';
} elseif ($val >= 1e+6) {
return number_format($val / 1e+6, $prec) . ' M';
} elseif ($val >= 1e+3) {
return number_format($val / 1e+3, $prec) . ' k';
}
return number_format($val, $prec);
}
function format_timespan($from, $prec = 0, $to = 0) {
if($to == 0) {
$to = time();
}
$dif = floatval($to) - floatval($from);
if($dif >= _TS_YEAR) {
return number_format($dif / _TS_YEAR, $prec) . " years";
} elseif ($dif >= _TS_WEEK) {
return number_format($dif / _TS_WEEK, $prec) . " weeks";
} elseif ($dif >= _TS_DAY) {
return number_format($dif / _TS_DAY, $prec) . " days";
} elseif ($dif >= _TS_HOUR) {
return number_format($dif /_TS_HOUR, $prec) . " hours";
} elseif ($dif >= _TS_MIN) {
return number_format($dif / _TS_MIN, $prec) . " mins";
} else {
return number_format($dif, $prec) . " secs";
}
}
function format_coin($val, $prec = 8) {
return number_format($val, $prec);
}
function format_money($val) {
if($val < 0.01) {
return number_format($val, 4);
} else {
return number_format($val, 2);
}
}
?>