Hi guys ; im trying to complete first recomended tutorial:
build simple dapp in 3 min.
Can not get the gas neither historic transaction data …
What I got so far is:
1 My server for testnet on moralis,
2 Created the js file using my server credentials.
3 Created the cloud function.
4 Mi account in testnet blockchain with some balance.
5 Using metamask I sended some money.
5 Can confir transactions exists on moralis server dashboard.
But …in the dapp.
When i click on “refresh stats” button ; I got an empty array and no error at all.
How can I debug this ?
thanks.
Hi, you need to share some code so we will be able to debug, like your cloud function code, your javascript code.
If you get an empty array maybe it returns an empty array that could function.
You can use logger.info(JSON.stringify(variable_name))
in order to do logging in a cloud function and then you can see those logs in server dashboard in logs.
Thanks , you are right … this is my code:
(it is copy and paste from tutorial):
<html>
<head>
<!-- Moralis SDK code -->
<script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>
<script src="https://unpkg.com/moralis/dist/moralis.js"></script>
</head>
<body>
<h1>Moralis Gas Stats</h1>
<button id="btn-login">Moralis Login</button>
<button id="btn-logout">Logout</button>
<button id="btn-get-stats">Refresh Stats</button>
<!-- stats will go here -->
<ul id="gas-stats"></ul>
<script>
// connect to Moralis server
const serverUrl = "https://nq41squpqoyb.usemoralis.com:2053/server";
const appId = "HeZNhbPRR7YtZ6vaCaEEmcWtwjIdwrtAd8AxyCQH";
Moralis.start({ serverUrl, appId });
// LOG IN WITH METAMASK
async function login() {
let user = Moralis.User.current();
if (!user) {
user = await Moralis.authenticate();
}
console.log("logged in user:", user);
getStats();
}
// LOG OUT
async function logOut() {
await Moralis.User.logOut();
console.log("logged out");
}
// bind button click handlers
document.getElementById("btn-login").onclick = login;
document.getElementById("btn-logout").onclick = logOut;
document.getElementById("btn-get-stats").onclick = getStats;
// refresh stats
function getStats() {
const user = Moralis.User.current();
if (user) {
getUserTransactions(user);
}
getAverageGasPrices();
}
// HISTORICAL TRANSACTIONS
async function getUserTransactions(user) {
// create query
const query = new Moralis.Query("EthTransactions");
query.equalTo("from_address", user.get("ethAddress"));
// subscribe to query updates
const subscription = await query.subscribe();
handleNewTransaction(subscription);
// run query
const results = await query.find();
console.log("user transactions:", results);
}
// REAL-TIME TRANSACTIONS
async function handleNewTransaction(subscription) {
// log each new transaction
subscription.on("create", function (data) {
console.log("new transaction: ", data);
});
}
// CLOUD FUNCTION
async function getAverageGasPrices() {
const results = await Moralis.Cloud.run("getAvgGas");
console.log("average user gas prices:", results);
renderGasStats(results);
}
function renderGasStats(data) {
const container = document.getElementById("gas-stats");
container.innerHTML = data
.map(function (row, rank) {
return `<li>#${rank + 1}: ${Math.round(row.avgGas)} gwei</li>`;
})
.join("");
}
//get stats on page load
getStats();
</script>
</body>
</html>
And this is my cloud function:
Moralis.Cloud.define("getAvgGas", async function (request) {
const query = new Moralis.Query("EthTransactions");
const pipeline = [
{
group: {
// group by "from_address"
objectId: "$from_address",
// add computed property avgGas
// get average and convert wei to gwei
avgGas: { $avg: { $divide: ["$gas_price", 1000000000]} },
},
},
{ sort: { avgGas: -1 } }, // sort by avgGas high to low
{ limit: 10 }, // only return top 10 results
];
// the master key is required for aggregate queries
const results = await query.aggregate(pipeline, { useMasterKey: true });
return results;
})
I will try adding some logger lines as you suggested.
Thanks for your words.
Leandro.
you say that you have transactions in your dashboard in EthTransactions
table?
If you are in a different testnet chain and not a testnet specific to eth then it may be a different table name.
Exactly , my table now is BscTransactions.
After change it on the queries I have some data.
Thanks!!!