-
Notifications
You must be signed in to change notification settings - Fork 16
/
App.js
109 lines (97 loc) · 2.66 KB
/
App.js
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
import { StatusBar } from 'expo-status-bar';
import React, { useEffect, useState } from 'react';
import { ActivityIndicator, FlatList, Text, View, StyleSheet } from 'react-native';
const nodeURL = "https://hugin.kryptokrona.se/json_api";
export default function App() {
const [isLoading, setLoading] = useState(true);
const [data, setData] = useState([]);
function fromHex(hex,str){
try{
str = decodeURIComponent(hex.replace(/(..)/g,'%$1'))
str = JSON.parse(str)
setData(str)
setLoading(false)
}
catch(e){
return
//str = hex
//console.log('invalid hex input: ' + hex)
}
return str
}
const getTransactions = (transactions) => {
// console.log(transactions);
for (let tx in transactions) {
// console.log(transactions[tx].hash);
fetch(nodeURL, {
method: 'POST',
body: JSON.stringify({
jsonrpc: '2.0',
method: 'f_transaction_json',
params: {hash: transactions[tx].hash}
})
})
.then((response) => response.json())
.then((json) => fromHex(json.result.tx.extra.substring(66)))
.catch((error) => console.error(error))
}
}
const getBlock = (blockHash) => {
fetch(nodeURL, {
method: 'POST',
body: JSON.stringify({
jsonrpc: '2.0',
method: 'f_block_json',
params: {hash: blockHash}
})
})
.then((response) => response.json())
.then((json) => getTransactions(json.result.block.transactions))
.catch((error) => console.error(error))
}
const getBlockHashes = (blockCount) => {
let i;
for (i = 0; i < 100; i++) {
fetch(nodeURL, {
method: 'POST',
body: JSON.stringify({
jsonrpc: '2.0',
method: 'on_getblockhash',
params: [blockCount - i]
})
})
.then((response) => response.json())
.then((json) => getBlock(json.result))
.catch((error) => console.error(error))
}
}
useEffect(() => {
fetch(nodeURL, {
method: 'POST',
body: JSON.stringify({
jsonrpc: '2.0',
method: 'getblockcount',
params: {}
})
})
.then((response) => response.json())
.then((json) => getBlockHashes(json.result.count))
.catch((error) => console.error(error))
//.finally(() => setLoading(false));
}, []);
return (
<View style={{ flex: 1, padding: 24 }}>
{isLoading ? <ActivityIndicator/> : (
<Text>{JSON.stringify(data)}</Text>
)}
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});