How to fetch information to aggregate to Moralis server table

Hello,

I am setting up a Moralis server which is working great for the information that is in events! It’s setting all that data up in a nice table.

However, not all information in the contract is emitted and we need to add more information to the table. Could you please tell me how can I configure the server to call the contract for more information to add to the table

if you want to make requests to the contract, you can use runContractFunction:
https://docs.moralis.io/moralis-server/web3-sdk/native#runcontractfunction

you can use it from cloud code too

Thanks @cryptokid, would we be able to make the cloud function run on its own when the event is triggered? Or do we have to call it?

This tutorial shows that the cloud function must be called: https://www.youtube.com/watch?v=Pj-X7sNyw2E

you should handle both cases, you can use a hook like afterSave to process that event, and you can also make a job that runs every x minutes to process the events

1 Like

I wrote this but I have no idea how to debug it. Where can I see a log for any possible errors?

Moralis.Cloud.afterSave("tournamentsIssuedPolygon", async function(request) {
    const tournamentId = request.object.get("tournamentId");
    const ABI = [{
      "inputs": [
        {
          "internalType": "uint256",
          "name": "tournamentId",
          "type": "uint256"
        }
      ],
      "name": "tournaments",
      "outputs": [
        {
          "internalType": "bool",
          "name": "active",
          "type": "bool"
        },
        {
          "internalType": "bool",
          "name": "restrictContributors",
          "type": "bool"
        },
        {
          "internalType": "enum TournamentDAO.TournamentType",
          "name": "tournamentType",
          "type": "uint8"
        },
        {
          "internalType": "enum TournamentDAO.TokenVersion",
          "name": "tokenVersion",
          "type": "uint8"
        },
        {
          "internalType": "uint8",
          "name": "teamSize",
          "type": "uint8"
        },
        {
          "internalType": "uint256",
          "name": "maxTeams",
          "type": "uint256"
        },
        {
          "internalType": "address",
          "name": "token",
          "type": "address"
        },
        {
          "internalType": "address payable",
          "name": "organizer",
          "type": "address"
        },
        {
          "internalType": "uint256",
          "name": "balance",
          "type": "uint256"
        },
        {
          "internalType": "uint256",
          "name": "buyInFee",
          "type": "uint256"
        },
        {
          "internalType": "address",
          "name": "verifier",
          "type": "address"
        }
      ],
      "stateMutability": "view",
      "type": "function",
      "constant": true
  	}];

    const options = {
      chain: "mumbai",
      address: "0x1f37892605439062c4eC2BB46f62ebCAD98B23B5",
      function_name: "tournaments",
      abi: ABI,
      params: {tournamentId: tournamentId}
    };
    const tournament = await Moralis.Web3API.native.runContractFunction(options);
    request.object.set("organizer", tournament.organizer);
  	request.object.set("test", "testy");
    return request.object.save();
});

Hmmm it’s actually updating the new columns now! But only when it’s confirmed. Any idea why?

Hey @cryptokid any idea how to get this information with fewer block confirmations or to get it before it’s confirmed?

can you also try beforeSave?

I don’t have an idea why it wouldn’t trigger, it will not trigger for historical sync, but for real time sync I would expect to trigger

be careful on beforeSave because if it fails then the entry will not be saved

you can also add some logging with a syntax like logger.info("here") to see if it is really not getting called

also take care not to generate an infinite loop by saving the data in a hook related to saving