-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtradeinfo.js
51 lines (44 loc) · 1.42 KB
/
tradeinfo.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
import React, { useEffect, useState } from "react";
function TradeInfo() {
const [price, setPrice] = useState(null);
const [tradeDecision, setTradeDecision] = useState(null);
useEffect(() => {
async function fetchData() {
const bitcoinPrice = await fetchPrice("bitcoin");
const decision = await fetchTradeDecision("bitcoin");
setPrice(bitcoinPrice);
setTradeDecision(decision);
}
fetchData();
}, []);
if (!price || !tradeDecision) return <p>Loading trade data...</p>;
return (
<div>
<h2>Bitcoin Trade Info</h2>
<p>Price: ${price}</p>
<p>Decision: {tradeDecision.action}</p>
</div>
);
}
async function fetchPrice(coinId) {
try {
const response = await fetch(`https://hodlbot-api-bmcmdhccf5hmgahy.eastus2-01.azurewebsites.net${coinId}`);
if (!response.ok) throw new Error("Failed to fetch price");
const data = await response.json();
return data[coinId].usd;
} catch (error) {
console.error("Error fetching price:", error);
return null;
}
}
async function fetchTradeDecision(coinId) {
try {
const response = await fetch(`https://hodlbot-api-bmcmdhccf5hmgahy.eastus2-01.azurewebsites.net/trade/${coinId}`);
if (!response.ok) throw new Error("Failed to fetch trade decision");
return await response.json();
} catch (error) {
console.error("Error fetching trade decision:", error);
return null;
}
}
export default TradeInfo;