Build a Simple dApp in 3 Mins - Historical Transactions (Part 3)
Build Your First Moralis dApp! This Guide Series Covers the Basics and How to Get Started Fast. In Part Three, We Create a Query to Get the User’s Past Transactions.

How to Get Historical Data

Getting the past transactions for the logged-in user is as simple as creating a query on the EthTransactions table.
1
const user = Moralis.User.current();
2
​
3
const query = new Moralis.Query("EthTransactions");
4
query.equalTo("from_address", user.get("ethAddress"));
5
const results = await query.find();
Copied!

Add a New Button

Add a new button to the HTML body beneath the other ones. You can use this to manually get the stats.
1
<button id="btn-get-stats">Refresh Stats</button>
Copied!

Create a Query and Call It

Add the following code to the <script> tag under the previous code.
1
document.getElementById("btn-get-stats").onclick = getStats;
2
​
3
function getStats() {
4
const user = Moralis.User.current();
5
if (user) {
6
getUserTransactions(user);
7
}
8
}
9
​
10
async function getUserTransactions(user) {
11
// create query
12
const query = new Moralis.Query("EthTransactions");
13
query.equalTo("from_address", user.get("ethAddress"));
14
​
15
// run query
16
const results = await query.find();
17
console.log("user transactions:", results);
18
}
19
​
20
// get stats on page load
21
getStats();
Copied!
And call getStats() from the login() function like so:
1
async function login() {
2
let user = Moralis.User.current();
3
if (!user) {
4
user = await Moralis.Web3.authenticate();
5
}
6
console.log("logged in user:", user);
7
8
// ** add this **
9
getStats();
10
}
Copied!

Check the Results

Save the changes. The dApp page should be updated automatically. You'll see something like this in the console:

Code So Far

1
<html>
2
<head>
3
<!-- Moralis SDK code -->
4
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/web3.min.js"></script>
5
<script src="https://unpkg.com/moralis/dist/moralis.js"></script>
6
</head>
7
<body>
8
<h1>Moralis Gas Stats</h1>
9
​
10
<button id="btn-login">Moralis Login</button>
11
<button id="btn-logout">Logout</button>
12
<button id="btn-get-stats">Refresh Stats</button>
13
​
14
<script>
15
// connect to Moralis server
16
const serverUrl = "https://9c8nrl6lfq8u.moralis.io:2053/server";
17
const appId = "TCDv7lnGwNN7RIhlj30wA69dnaamxtsmqcTnm3ch";
18
Moralis.start({ serverUrl, appId });
19
​
20
async function login() {
21
let user = Moralis.User.current();
22
if (!user) {
23
user = await Moralis.Web3.authenticate();
24
}
25
console.log("logged in user:", user);
26
getStats();
27
}
28
​
29
async function logOut() {
30
await Moralis.User.logOut();
31
console.log("logged out");
32
}
33
​
34
// bind button click handlers
35
document.getElementById("btn-login").onclick = login;
36
document.getElementById("btn-logout").onclick = logOut;
37
document.getElementById("btn-get-stats").onclick = getStats;
38
​
39
function getStats() {
40
const user = Moralis.User.current();
41
if (user) {
42
getUserTransactions(user);
43
}
44
}
45
​
46
async function getUserTransactions(user) {
47
// create query
48
const query = new Moralis.Query("EthTransactions");
49
query.equalTo("from_address", user.get("ethAddress"));
50
​
51
// run query
52
const results = await query.find();
53
console.log("user transactions:", results);
54
}
55
​
56
//get stats on page load
57
getStats();
58
</script>
59
</body>
60
</html>
Copied!

OMG!

Historical working! Yeaaaa... keep going!
​
Last modified 9d ago