BSC main net simple gas prices dapp error

did anyone do the simple dapp in 3minutes tutorial on gas prices?, im connected to BSC mainet and connected metamask and everything is working except when i send and recieve real time transactions the Stats wont come up its just displaying empty arrays for user transactions and average gas prices . the transaction history doesnt even display inside metamask . with me send/ receiving bnb transactions image4

transactions i make are not even displaying in metamask
here is my code :

<!DOCTYPE html>

<html>

  <head>

      <title> Gas Price Dapp</title>

    <!-- 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>Gas Stats With Moralis</h1>

    <button id="btn-login">Moralis Login</button>

    <button id="btn-logout">Logout</button>

    <button id="btn-get-stats">Refresh Stats</button>

    <script>

        const NODE_URL = "https://speedy-nodes-nyc.moralis.io/81333ef42d97c2958c15467c/bsc/mainnet";

        const provider = new Web3.providers.HttpProvider(NODE_URL);

        const web3 = new Web3(provider);

      // connect to Moralis server

      Moralis.initialize("vto1JuctQSO3fQGQCx2UjZb6IrqRBYYPQ0VP7pkL");

      Moralis.serverURL = "https://rinctnejggwl.bigmoralis.com:2053/server";

          

            // add from here down

            async function login() {

              let user = Moralis.User.current();

              if (!user) {

                user = await Moralis.Web3.authenticate();

              }

              console.log("logged in user:", user);

            }

      

            async function logOut() {

              await Moralis.User.logOut();

              console.log("logged out");

            }

      

            document.getElementById("btn-login").onclick = login;

            document.getElementById("btn-logout").onclick = logOut;

            document.getElementById("btn-get-stats").onclick = getStats;

            async function getAverageGasPrices() {

  const results = await Moralis.Cloud.run("getAvgGas");

  console.log("average user gas prices:", results);

}

function getStats() {

  const user = Moralis.User.current();

  if (user) {

    getUserTransactions(user);

  }

  getAverageGasPrices();

}

async function getUserTransactions(user) {

  // create query

  const query = new Moralis.Query("EthTransactions");

  query.equalTo("from_address", user.get("ethAddress"));

   // subscribe to query updates ** add this**

   const subscription = await query.subscribe();

  handleNewTransaction(subscription);

  // run query

  const results = await query.find();

  console.log("user transactions:", results);

  async function handleNewTransaction(subscription) {

  // log each new transaction

  subscription.on("create", function(data) {

    console.log("new transaction: ", data);

  });

}

}

// get stats on page load

getStats();

        </script>

  </body>

</html>

Hi,
It looks like results = await Moralis.Cloud.run("getAvgGas"); doesn’t return anything.
What is the code for that particular getAvgGas cloud function?

What you see in MetaMask as transactions history doesn’t necessarily have a connection to Moralis, it depends on what speedy node has MetaMask configured.

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;
})

this is the speedy node i has configured with metamask https://speedy-nodes-nyc.moralis.io/81333ef42d97c2958c15467c/bsc/mainnet

Hi,
Probably you don’t want to look in EthTransactions table if you are connected to BSC in MetaMask.
You have BscTransactions table for Binance Smart Chain (BSC).
You can check what you have in Moralis Dashboard on those tables, if is nothing in those tables then that cloud function will not find any data to process.

Regarding the history of transactions in MetaMask, maybe MetaMask doesn’t look in what is the history of your transactions and only keeps log of new transactions (not sure of that).

Just adding reference to that tutorial: https://docs.moralis.io/guides/build-a-simple-dapp-in-3-minutes

AYYEE thank you , i changed the get user transactions function in my code and and cloud function
to Bsctransactions and its displaying everything properly in js console Thank you very much cryptokid

2 Likes