Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import ConvertPi from "./ConvertPi";
import SwapCrypto from "./SwapCrypto";

function App() {
return (
<div>
<ConvertPi />
<SwapCrypto />
</div>
);
}

export default App;
34 changes: 34 additions & 0 deletions ConvertPi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { useState } from "react";
import axios from "axios";

const ConvertPi = () => {
const [currency, setCurrency] = useState("usd");
const [amount, setAmount] = useState(1);
const [converted, setConverted] = useState(null);

const convertPi = async () => {
try {
const response = await axios.get(`http://localhost:5000/convert/${currency}`);
setConverted(response.data["pi-network"][currency] * amount);
} catch (error) {
console.error("Error fetching exchange rate:", error);
}
};

return (
<div>
<h2>Konversi Pi Coin</h2>
<input type="number" value={amount} onChange={(e) => setAmount(e.target.value)} />
<select value={currency} onChange={(e) => setCurrency(e.target.value)}>
<option value="usd">USD</option>
<option value="eur">EUR</option>
<option value="btc">BTC</option>
<option value="eth">ETH</option>
</select>
<button onClick={convertPi}>Konversi</button>
{converted !== null && <p>Hasil: {converted} {currency.toUpperCase()}</p>}
</div>
);
};

export default ConvertPi;
40 changes: 40 additions & 0 deletions SwapCrypto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { useState } from "react";
import axios from "axios";

const SwapCrypto = () => {
const [from, setFrom] = useState("pi-network");
const [to, setTo] = useState("btc");
const [amount, setAmount] = useState(1);
const [swapResult, setSwapResult] = useState(null);

const swapCrypto = async () => {
try {
const response = await axios.get(`http://localhost:5000/swap/${from}/${to}`);
setSwapResult(amount * response.data.rate);
} catch (error) {
console.error("Error fetching swap rate:", error);
}
};

return (
<div>
<h2>Swap Kripto</h2>
<input type="number" value={amount} onChange={(e) => setAmount(e.target.value)} />
<select value={from} onChange={(e) => setFrom(e.target.value)}>
<option value="pi-network">Pi Coin</option>
<option value="bitcoin">Bitcoin</option>
<option value="ethereum">Ethereum</option>
</select>
<span>➡</span>
<select value={to} onChange={(e) => setTo(e.target.value)}>
<option value="pi-network">Pi Coin</option>
<option value="bitcoin">Bitcoin</option>
<option value="ethereum">Ethereum</option>
</select>
<button onClick={swapCrypto}>Tukar</button>
{swapResult !== null && <p>Hasil: {swapResult} {to.toUpperCase()}</p>}
</div>
);
};

export default SwapCrypto; yg
39 changes: 39 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require("dotenv").config();
const express = require("express");
const axios = require("axios");
const cors = require("cors");

const app = express();
app.use(express.json());
app.use(cors());

const COINGECKO_API = "https://api.coingecko.com/api/v3/simple/price";

// Mendapatkan nilai tukar Pi Coin ke mata uang lain
app.get("/convert/:currency", async (req, res) => {
try {
const currency = req.params.currency.toLowerCase();
const response = await axios.get(`${COINGECKO_API}?ids=pi-network&vs_currencies=${currency}`);
res.json(response.data);
} catch (error) {
res.status(500).json({ error: "Gagal mendapatkan nilai tukar" });
}
});

// Swap antara dua mata uang kripto
app.get("/swap/:from/:to", async (req, res) => {
try {
const { from, to } = req.params;
const response = await axios.get(`${COINGECKO_API}?ids=${from},${to}&vs_currencies=usd`);

const fromPrice = response.data[from].usd;
const toPrice = response.data[to].usd;

const swapRate = fromPrice / toPrice;
res.json({ rate: swapRate });
} catch (error) {
res.status(500).json({ error: "Gagal mendapatkan nilai tukar" });
}
});

app.listen(5000, () => console.log("Server berjalan di port 5000"));