-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
168 lines (154 loc) · 4.11 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
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
import React from 'react';
import { Text, useColorScheme, StyleSheet } from "react-native";
import { VStack, Box, Heading, NativeBaseProvider } from "native-base";
import Icon from 'react-native-vector-icons/FontAwesome5';
import { format } from "date-fns";
import Toast from 'react-native-toast-message';
export function PriceTile(props) {
return (
<Box width="90%" h="27%" bg={props.bgColor} rounded="lg" shadow={3}>
<Text style={styles.titleText}>
{props.title}
</Text>
<Box style={styles.priceBox}>
<Text style={styles.priceText}>
{props.price}
</Text>
<Icon name={props.iconName} size={30} color="white" style={styles.arrowIcon} />
</Box>
<Text style={styles.descText}>
{props.description}
</Text>
</Box>
);
}
export class GasPrice extends React.Component {
constructor(props) {
super(props);
this.state = {
result: {
instant: {
feeCap: 100.01,
maxPriorityFee: 100.01
},
fast: {
feeCap: 100.01,
maxPriorityFee: 100.01
},
eco: {
feeCap: 100.01,
maxPriorityFee: 100.01
},
baseFee: 100.01,
ethPrice: 4000.01
},
lastUpdatedTime: "Never",
instantIcon: "minus",
fastIcon: "minus",
ecoIcon: "minus",
theme: props.theme,
}
}
componentDidMount() {
this.interval = setInterval(async () => {
this.fetch();
}, 3000);
}
componentWillUnmount() {
clearInterval(this.interval);
}
computeIconDelta(prev, curr) {
if (Math.ceil(curr) > Math.ceil(prev)) {
return "arrow-up";
} else if (Math.ceil(curr) < Math.ceil(prev)) {
return "arrow-down";
} else {
return "minus";
}
}
fetch() {
var context = this;
fetch('https://api.gasprice.io/v1/estimates')
.then((response) => response.json())
.then((json) => {
var prevInstant = this.state.result.instant.feeCap;
var prevFast = this.state.result.fast.feeCap;
var prevEco = this.state.result.eco.feeCap;
var newInstant = json.result.instant.feeCap;
var newFast = json.result.fast.feeCap;
var newEco = json.result.eco.feeCap;
var instantIcon = this.computeIconDelta(prevInstant, newInstant);
var fastIcon = this.computeIconDelta(prevFast, newFast);
var ecoIcon = this.computeIconDelta(prevEco, newEco);
context.setState({
result: json.result,
lastUpdatedTime: format(new Date(), "MMMM do, yyyy h:mm:ss a"),
instantIcon: instantIcon,
fastIcon: fastIcon,
ecoIcon: ecoIcon,
});
})
.catch((error) => {
Toast.show({
type: 'error',
text1: 'Refresh failed. Please try again.',
});
console.error(error);
});
}
render() {
return (
<>
<NativeBaseProvider>
<Heading textAlign="center" mb="8" mt="8" color={this.state.theme === 'dark' ? "#FFFFFF" : "#343434"}>Gas Price</Heading>
<VStack space={4} alignItems="center">
<PriceTile bgColor="#F54634" title="Instant" price={Math.ceil(this.state.result.instant.feeCap)} iconName={this.state.instantIcon} description="Almost-guaranteed next block inclusion" />
<PriceTile bgColor="#005FF9" title="Fast" price={Math.ceil(this.state.result.fast.feeCap)} iconName={this.state.fastIcon} description="Useful if not minting NFTs" />
<PriceTile bgColor="#00C66B" title="Eco" price={Math.ceil(this.state.result.eco.feeCap)} iconName={this.state.ecoIcon} description="Usually confirmed within an hour" />
<Text style={styles.lastUpdatedText}>Last updated: {this.state.lastUpdatedTime}</Text>
</VStack>
</NativeBaseProvider>
<Toast />
</>
);
}
}
export default App = () => {
const colorScheme = useColorScheme();
return (
<GasPrice theme={colorScheme} />
);
};
const styles = StyleSheet.create({
titleText: {
fontSize: 26,
fontWeight: "normal",
color: "white",
paddingLeft: 8,
paddingTop: 4,
},
priceText: {
fontSize: 68,
fontWeight: "bold",
color: "white",
textAlign: "center",
},
descText: {
fontSize: 16,
fontWeight: "normal",
color: "white",
textAlign: "center",
paddingTop: 12,
},
lastUpdatedText: {
fontSize: 16,
},
arrowIcon: {
alignSelf: 'center',
paddingStart: 8,
},
priceBox: {
flexDirection: 'row',
justifyContent: 'center',
},
});