1
1
<?php
2
- include "../../../api.php " ;
3
-
4
- const DEFAULT_NODE = "https://www.bitrequest.app:8020 " ;
5
- const MAX_HISTORY_COUNT = 15 ;
6
- const MAX_PENDING_COUNT = 15 ;
7
-
8
- // Main function to process Nano transactions
9
- function main () {
10
- $ payload = get_payload ();
11
- if (!$ payload ) {
12
- send_jsonresponse (["error " => "Invalid payload " ]);
13
- return ;
14
- }
15
-
16
- $ pl_array = json_decode ($ payload , true );
17
- if (!is_array ($ pl_array )) {
18
- send_jsonresponse (["error " => "Invalid payload format " ]);
19
- return ;
20
- }
21
-
22
- $ node = isset ($ pl_array ["node " ]) ? $ pl_array ["node " ] : DEFAULT_NODE ;
23
- $ account = isset ($ pl_array ["account " ]) ? $ pl_array ["account " ] : null ;
2
+ include "../../../api.php " ;
3
+
4
+ const DEFAULT_NODE = "https://www.bitrequest.app:8020 " ;
5
+ const MAX_HISTORY_COUNT = 15 ;
6
+ const MAX_PENDING_COUNT = 15 ;
7
+
8
+ // Main function to process Nano transactions
9
+ function main () {
10
+ $ payload = get_payload ();
11
+ if (!$ payload ) {
12
+ send_jsonresponse (["error " => "Invalid payload " ]);
13
+ return ;
14
+ }
24
15
25
- if (!$ account ) {
26
- send_jsonresponse (["error " => "Account not provided " ]);
27
- return ;
28
- }
16
+ $ pl_array = json_decode ($ payload , true );
17
+ if (!is_array ($ pl_array )) {
18
+ send_jsonresponse (["error " => "Invalid payload format " ]);
19
+ return ;
20
+ }
29
21
30
- $ headers = ["Content-Type: application/json " ];
22
+ $ node = isset ($ pl_array ["node " ]) ? $ pl_array ["node " ] : DEFAULT_NODE ;
23
+ $ account = isset ($ pl_array ["account " ]) ? $ pl_array ["account " ] : null ;
31
24
32
- $ merged_history = get_account_history ($ node , $ account , $ headers );
33
- $ transformed_pending_array = get_account_pending ($ node , $ account , $ headers );
25
+ if (!$ account ) {
26
+ send_jsonresponse (["error " => "Account not provided " ]);
27
+ return ;
28
+ }
34
29
35
- if (!empty ($ transformed_pending_array ) && !empty ($ merged_history )) {
36
- $ result = sort_array (merge_arrays ($ transformed_pending_array , $ merged_history ));
37
- } elseif (!empty ($ transformed_pending_array )) {
38
- $ result = sort_array ($ transformed_pending_array );
39
- } elseif (!empty ($ merged_history )) {
40
- $ result = sort_array ($ merged_history );
41
- } else {
42
- $ result = ["message " => "No transactions found " ];
43
- }
30
+ $ headers = ["Content-Type: application/json " ];
44
31
45
- send_jsonresponse ($ result );
32
+ $ merged_history = get_account_history ($ node , $ account , $ headers );
33
+ if (isset ($ merged_history ["error " ])) {
34
+ send_jsonresponse ($ merged_history );
35
+ return ;
46
36
}
47
-
48
- function get_payload () {
49
- return isset ($ _GET ["pl " ]) ? json_decode (base64_decode ($ _GET ["pl " ])) : null ;
37
+
38
+ $ transformed_pending_array = get_account_pending ($ node , $ account , $ headers );
39
+ if (isset ($ transformed_pending_array ["error " ])) {
40
+ send_jsonresponse ($ transformed_pending_array );
41
+ return ;
50
42
}
51
43
52
- function send_jsonresponse ($ data ) {
53
- header ("Content-Type: application/json " );
54
- echo json_encode ($ data , JSON_PRETTY_PRINT );
44
+ if (!empty ($ transformed_pending_array ) && !empty ($ merged_history )) {
45
+ $ result = sort_array (merge_arrays ($ transformed_pending_array , $ merged_history ));
46
+ } elseif (!empty ($ transformed_pending_array )) {
47
+ $ result = sort_array ($ transformed_pending_array );
48
+ } elseif (!empty ($ merged_history )) {
49
+ $ result = sort_array ($ merged_history );
50
+ } else {
51
+ $ result = ["message " => "No transactions found " ];
55
52
}
56
53
57
- function get_account_history ($ node , $ account , $ headers ) {
58
- $ history_payload = [
59
- "action " => "account_history " ,
60
- "account " => $ account ,
61
- "count " => MAX_HISTORY_COUNT ,
62
- "raw " => true
63
- ];
64
-
65
- $ history_result = api ($ node , json_encode ($ history_payload ), $ headers , null , null , null , null );
66
- $ history_data = json_decode (json_encode ($ history_result ), true ); // Consistent conversion
67
- $ history_data = isset ($ history_data ["br_result " ]) ? $ history_data ["br_result " ] : null ;
68
-
69
- if (!$ history_data || !empty ($ history_data ["error " ])) {
70
- return [];
71
- }
72
-
73
- $ history_blocks = isset ($ history_data ["history " ]) ? $ history_data ["history " ] : [];
74
- if (!is_array ($ history_blocks )) {
75
- return [];
76
- }
77
-
78
- $ historic_hashes = array_reduce ($ history_blocks , function ($ carry , $ block ) {
79
- if ((isset ($ block ["subtype " ]) && $ block ["subtype " ] === "receive " ) || isset ($ block ["receivable " ])) {
80
- $ carry [] = isset ($ block ["link " ]) ? $ block ["link " ] : null ;
81
- }
82
- return $ carry ;
83
- }, []);
84
-
85
- if (empty ($ historic_hashes )) {
86
- return $ history_blocks ;
87
- }
88
-
89
- $ history_blockInfo_payload = [
90
- "action " => "blocks_info " ,
91
- "json_block " => true ,
92
- "pending " => true ,
93
- "source " => true ,
94
- "hashes " => array_filter ($ historic_hashes )
95
- ];
96
-
97
- $ history_block_result = api ($ node , json_encode ($ history_blockInfo_payload ), $ headers , null , null , null , null );
98
- $ historyInfo_data = json_decode (json_encode ($ history_block_result ), true );
99
- $ historyInfo_data = isset ($ historyInfo_data ["br_result " ]) ? $ historyInfo_data ["br_result " ] : null ;
100
-
101
- if (!$ historyInfo_data || !empty ($ historyInfo_data ["error " ]) || !is_array ($ historyInfo_data )) {
102
- return $ history_blocks ;
103
- }
104
-
105
- $ transformed_historyarray = transform_object ($ historyInfo_data );
106
- return merge_timestamps ($ history_blocks , $ transformed_historyarray );
54
+ send_jsonresponse ($ result );
55
+ }
56
+
57
+ function get_payload () {
58
+ return isset ($ _GET ["pl " ]) ? json_decode (base64_decode ($ _GET ["pl " ])) : null ;
59
+ }
60
+
61
+ function send_jsonresponse ($ data ) {
62
+ header ("Content-Type: application/json " );
63
+ echo json_encode ($ data , JSON_PRETTY_PRINT );
64
+ }
65
+
66
+ function get_account_history ($ node , $ account , $ headers ) {
67
+ $ history_payload = [
68
+ "action " => "account_history " ,
69
+ "account " => $ account ,
70
+ "count " => MAX_HISTORY_COUNT ,
71
+ "raw " => true
72
+ ];
73
+
74
+ $ history_result = api ($ node , json_encode ($ history_payload ), $ headers , null , null , null , null );
75
+ $ history_data = json_decode (json_encode ($ history_result ), true ); // Consistent conversion
76
+ $ history_data = isset ($ history_data ["br_result " ]) ? $ history_data ["br_result " ] : null ;
77
+ if (isset ($ history_data ["error " ])) {
78
+ return $ history_data ;
79
+ }
80
+
81
+ if (!$ history_data || !empty ($ history_data ["error " ])) {
82
+ return [];
107
83
}
108
84
109
- function get_account_pending ($ node , $ account , $ headers ) {
110
- $ pending_payload = [
111
- "action " => "accounts_pending " ,
112
- "accounts " => [$ account ],
113
- "include_active " => true ,
114
- "count " => MAX_PENDING_COUNT ,
115
- ];
116
-
117
- sleep (1 ); // wait one second before next call to not exceed api proxy limits
118
-
119
- $ receivable_result = api ($ node , json_encode ($ pending_payload ), $ headers , null , null , null , null );
120
- $ pending_data = json_decode (json_encode ($ receivable_result ), true );
121
- $ pending_data = isset ($ pending_data ["br_result " ]) ? $ pending_data ["br_result " ] : null ;
85
+ $ history_blocks = isset ($ history_data ["history " ]) ? $ history_data ["history " ] : [];
86
+ if (!is_array ($ history_blocks )) {
87
+ return [];
88
+ }
122
89
123
- if (!$ pending_data ) {
124
- return [];
90
+ $ historic_hashes = array_reduce ($ history_blocks , function ($ carry , $ block ) {
91
+ if ((isset ($ block ["subtype " ]) && $ block ["subtype " ] === "receive " ) || isset ($ block ["receivable " ])) {
92
+ $ carry [] = isset ($ block ["link " ]) ? $ block ["link " ] : null ;
125
93
}
94
+ return $ carry ;
95
+ }, []);
126
96
127
- $ pending_blocks = isset ($ pending_data ["blocks " ][$ account ]) ? $ pending_data ["blocks " ][$ account ] : [];
128
- if (empty ($ pending_blocks )) {
129
- return [];
130
- }
97
+ if (empty ($ historic_hashes )) {
98
+ return $ history_blocks ;
99
+ }
131
100
132
- $ pendingblockinfo_payload = [
133
- "action " => "blocks_info " ,
134
- "json_block " => true ,
135
- "pending " => true ,
136
- "source " => true ,
137
- "hashes " => array_values ( $ pending_blocks )
138
- ];
101
+ $ history_blockInfo_payload = [
102
+ "action " => "blocks_info " ,
103
+ "json_block " => true ,
104
+ "pending " => true ,
105
+ "source " => true ,
106
+ "hashes " => array_filter ( $ historic_hashes )
107
+ ];
139
108
140
- $ pending_result = api ($ node , json_encode ($ pendingblockinfo_payload ), $ headers , null , null , null , null );
141
- $ pending_blockinfo_data = json_decode (json_encode ($ pending_result ), true );
142
- $ pending_blockinfo_data = isset ($ pending_blockinfo_data ["br_result " ]) ? $ pending_blockinfo_data ["br_result " ] : null ;
109
+ $ history_block_result = api ($ node , json_encode ($ history_blockInfo_payload ), $ headers , null , null , null , null );
110
+ $ historyInfo_data = json_decode (json_encode ($ history_block_result ), true );
111
+ $ historyInfo_data = isset ($ historyInfo_data ["br_result " ]) ? $ historyInfo_data ["br_result " ] : null ;
143
112
144
- return transform_object ($ pending_blockinfo_data );
113
+ if (!$ historyInfo_data || !empty ($ historyInfo_data ["error " ]) || !is_array ($ historyInfo_data )) {
114
+ return $ history_blocks ;
145
115
}
146
116
147
- // Transforms a blocks object into an array format, adding the hash as a property to each block
148
- function transform_object ($ obj ) {
149
- if (!isset ($ obj ["blocks " ]) || !is_array ($ obj ["blocks " ])) {
150
- return [];
151
- }
152
- return array_map (function ($ key , $ value ) {
153
- $ value ["hash " ] = $ key ;
154
- return $ value ;
155
- }, array_keys ($ obj ["blocks " ]), $ obj ["blocks " ]);
117
+ $ transformed_historyarray = transform_object ($ historyInfo_data );
118
+ return merge_timestamps ($ history_blocks , $ transformed_historyarray );
119
+ }
120
+
121
+ function get_account_pending ($ node , $ account , $ headers ) {
122
+ $ pending_payload = [
123
+ "action " => "accounts_pending " ,
124
+ "accounts " => [$ account ],
125
+ "include_active " => true ,
126
+ "count " => MAX_PENDING_COUNT ,
127
+ ];
128
+
129
+ sleep (1 ); // wait one second before next call to not exceed api proxy limits
130
+
131
+ $ receivable_result = api ($ node , json_encode ($ pending_payload ), $ headers , null , null , null , null );
132
+ $ pending_data = json_decode (json_encode ($ receivable_result ), true );
133
+ $ pending_data = isset ($ pending_data ["br_result " ]) ? $ pending_data ["br_result " ] : null ;
134
+ if (isset ($ pending_data ["error " ])) {
135
+ return $ pending_data ;
156
136
}
157
-
158
- // Merges timestamp information from history blocks into the main history array
159
- function merge_timestamps ($ history , $ history_blocks ) {
160
- if (empty ($ history_blocks )) {
161
- return $ history ;
162
- }
163
- $ lookup = array_column ($ history_blocks , "local_timestamp " , "hash " );
164
- return array_map (function ($ item ) use ($ lookup ) {
165
- if (isset ($ item ["link " ]) && isset ($ lookup [$ item ["link " ]])) {
166
- $ item ["local_timestamp " ] = $ lookup [$ item ["link " ]];
167
- }
168
- return $ item ;
169
- }, $ history );
137
+
138
+ if (!$ pending_data ) {
139
+ return [];
170
140
}
171
141
172
- // Merges two arrays safely, handling null values
173
- function merge_arrays ( $ array1 , $ array2 ) {
174
- return array_merge ( $ array1 ?? [], $ array2 ?? []) ;
142
+ $ pending_blocks = isset ( $ pending_data [ " blocks " ][ $ account ]) ? $ pending_data [ " blocks " ][ $ account ] : [];
143
+ if ( empty ( $ pending_blocks ) ) {
144
+ return [] ;
175
145
}
176
146
177
- // Sorts an array of transactions by timestamp in descending order (newest first)
178
- function sort_array ($ array ) {
179
- if (!is_array ($ array )) {
180
- return [];
147
+ $ pendingblockinfo_payload = [
148
+ "action " => "blocks_info " ,
149
+ "json_block " => true ,
150
+ "pending " => true ,
151
+ "source " => true ,
152
+ "hashes " => array_values ($ pending_blocks )
153
+ ];
154
+
155
+ $ pending_result = api ($ node , json_encode ($ pendingblockinfo_payload ), $ headers , null , null , null , null );
156
+ $ pending_blockinfo_data = json_decode (json_encode ($ pending_result ), true );
157
+ $ pending_blockinfo_data = isset ($ pending_blockinfo_data ["br_result " ]) ? $ pending_blockinfo_data ["br_result " ] : null ;
158
+
159
+ return transform_object ($ pending_blockinfo_data );
160
+ }
161
+
162
+ // Transforms a blocks object into an array format, adding the hash as a property to each block
163
+ function transform_object ($ obj ) {
164
+ if (!isset ($ obj ["blocks " ]) || !is_array ($ obj ["blocks " ])) {
165
+ return [];
166
+ }
167
+ return array_map (function ($ key , $ value ) {
168
+ $ value ["hash " ] = $ key ;
169
+ return $ value ;
170
+ }, array_keys ($ obj ["blocks " ]), $ obj ["blocks " ]);
171
+ }
172
+
173
+ // Merges timestamp information from history blocks into the main history array
174
+ function merge_timestamps ($ history , $ history_blocks ) {
175
+ if (empty ($ history_blocks )) {
176
+ return $ history ;
177
+ }
178
+ $ lookup = array_column ($ history_blocks , "local_timestamp " , "hash " );
179
+ return array_map (function ($ item ) use ($ lookup ) {
180
+ if (isset ($ item ["link " ]) && isset ($ lookup [$ item ["link " ]])) {
181
+ $ item ["local_timestamp " ] = $ lookup [$ item ["link " ]];
181
182
}
182
- usort ($ array , function ($ a , $ b ) {
183
- $ a_timestamp = isset ($ a ["local_timestamp " ]) ? $ a ["local_timestamp " ] : 0 ;
184
- $ b_timestamp = isset ($ b ["local_timestamp " ]) ? $ b ["local_timestamp " ] : 0 ;
185
- return $ b_timestamp - $ a_timestamp ;
186
- });
187
- return $ array ;
183
+ return $ item ;
184
+ }, $ history );
185
+ }
186
+
187
+ // Merges two arrays safely, handling null values
188
+ function merge_arrays ($ array1 , $ array2 ) {
189
+ return array_merge ($ array1 ?? [], $ array2 ?? []);
190
+ }
191
+
192
+ // Sorts an array of transactions by timestamp in descending order (newest first)
193
+ function sort_array ($ array ) {
194
+ if (!is_array ($ array )) {
195
+ return [];
188
196
}
189
-
190
- main ();
197
+ usort ($ array , function ($ a , $ b ) {
198
+ $ a_timestamp = isset ($ a ["local_timestamp " ]) ? $ a ["local_timestamp " ] : 0 ;
199
+ $ b_timestamp = isset ($ b ["local_timestamp " ]) ? $ b ["local_timestamp " ] : 0 ;
200
+ return $ b_timestamp - $ a_timestamp ;
201
+ });
202
+ return $ array ;
203
+ }
204
+
205
+ main ();
0 commit comments