This guide will walk you through on how you can seamlessly retrieve and display wallet transactions by using the wallet history API.
Prerequisites
- Node.js and npm installed
- Basic knowledge of JavaScript, React, and Express.js
Step-by-step guide
Step 1: Initialization
- Create a new directory for the project:Bash1
mkdir wallet-history && cd wallet-history - Initialize a new Node.js project:Bash1
npm init -y - Install Express, CORS, and Axios:Bash1
npm install express cors axios - Install dotenv:
Then create a new file called .env and add your api key:Bash1npm install dotenvBash1API_KEY=YOUR_1INCH_API_KEY - Create a new file
api.jsand set up a basic Express server by pasting into it the following:
JavaScript
12345678910111213141516171819202122232425262728293031323334353637383940414243
// Import required packages
const express = require("express");
const axios = require("axios");
const dotenv = require("dotenv");
const cors = require("cors");
const path = require("path");
// Initialize the app and load environment variables from .env
dotenv.config({ path: path.resolve(__dirname, ".env") });
const app = express();
// Enable CORS for all routes
app.use(cors());
const BASE_URL = "https://api.1inch.com/history/v2.0/history";
// Endpoint to fetch wallet transaction history
app.get("/api/:address/history", async (req, res) => {
const address = req.params.address;
const limit = req.query.limit || 10;
try {
const constructedUrl = `${BASE_URL}/${address}/events?chainId=${1}&limit=${limit}`;
const response = await axios.get(constructedUrl, {
headers: {
Authorization: `Bearer ${process.env.API_KEY}` // Use API key from .env
}
});
// Send the response data back to the client
res.json(response.data);
} catch (error) {
console.error("Error fetching wallet transactions:", error);
res.status(500).json({ error: "An error occurred while fetching data" });
}
});
// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
Add an endpoint to fetch wallet transactions and don't forget to replace API_KEY with your 1inch Business API key:
JavaScript12345678910111213141516171819202122const BASE_URL = "https://api.1inch.com/history/v2.0/history"; app.get("/api/:address/history", async (req, res) => { const address = req.params.address; const limit = req.query.limit || 10; try { const constructedUrl = `${BASE_URL}/${address}/events?chainId=${1}&limit=${limit}`; const response = await axios.get(constructedUrl, { headers: { Authorization: `Bearer ${process.env.API_KEY}` } }); // Send the data from the API back to the client res.json(response.data.items); } catch (error) { console.error("Axios Error: ", error.response); res.status(500).json({ error: "Failed to fetch wallet transactions" }); } });
Step 2: Setting up React frontend
Create a new React application:
Bash1npx create-react-app clientNavigate to the React application directory:
Bash1cd clientCreate a
History.jscomponent inside thesrcdirectory with the following content:JavaScript123456789101112131415161718192021222324252627282930313233343536373839404142434445import React, { useState, useEffect } from "react"; const History = ({ address }) => { const [transactions, setTransactions] = useState([]); const [isLoading, setLoading] = useState(false); useEffect(() => { const fetchData = async () => { setLoading(true); const response = await fetch(`http://localhost:5001/api/${address}/history`); if (!response.ok) { console.log("Fetch history error", response); return; } const transactions = await response.json(); setTransactions(transactions); setLoading(false); }; if (isLoading) { return; } fetchData(); }, [address]); if (isLoading) { return <div>Loading...</div>; } return ( <ul> {transactions.map((tx) => ( <li key={tx.id}> <a href={`https://etherscan.io/tx/${tx.details.txHash}`}>{tx.details.type}</a> </li> ))} </ul> ); }; export default History;Import and use
Historyinsrc/App.js:JavaScript12345678910111213141516171819202122232425262728293031323334import React, { useState } from "react"; import "./App.css"; import History from "./History"; function App() { const [inputValue, setSearchTerm] = useState(""); const [address, setAddress] = useState(); const handleChange = (event) => { setSearchTerm(event.target.value); }; const handleSubmit = (event) => { event.preventDefault(); setAddress(inputValue); }; return ( <div className="App"> <header> <form onSubmit={handleSubmit}> <input type="text" value={inputValue} placeholder="search by address" onChange={handleChange} /> <button type="submit">Search</button> </form> </header> {address && <h1>Ethereum history of {address}</h1>} {address && <History address={address} />} </div> ); } export default App;
Step 3: Running the project
- Start the Express server:Bash1
node api.js - In a new terminal, navigate to the client directory and start the React app:
Now, you can view your wallet transaction history at http://localhost:3000.Bash1cd client
Described above is a basic setup and you can expand upon this by adding more features, error handling, and styling to get to production.