[SOLVED] Prevent object from beeing saved from within beforeSave trigger

I am trying to prevent objects to be saved or synced if the from or to address is Null Address by doing this inside the beforeSave of EthNFTTransfers. However, I still get all the objects saved that have null address in either from or to_address.

Doesnt the “return” after the check prevent it from continuation?

  const from_address = request.object.get("from_address");
  const to_address = request.object.get("to_address");

  if (
    from_address === "0x0000000000000000000000000000000000000000" ||
    to_address === "0x0000000000000000000000000000000000000000"
  )
    return;

You have to throw an error in order to not be saved

1 Like

ah perfect thank you! Just like “throw new Error(“somemessage”)” ?

They are still getting saved with these changes Ive made.

Moralis.Cloud.beforeSave("EthNFTTransfers", async request => {
  const from_address = request.object.get("from_address");
  const to_address = request.object.get("to_address");

  try {
    if (
      from_address == "0x0000000000000000000000000000000000000000" ||
      to_address == "0x0000000000000000000000000000000000000000"
    ) {
      throw new Error("Null Address detected");
    }

    let side;
    let group;
    
    const is_synced_address = await Moralis.Cloud.run("isSyncedAddress", {
      address: from_address
    });

    if (is_synced_address == from_address) {
      group = await Moralis.Cloud.run("getWalletGroupByAddress", {
        address: synced_address
      });
      side = "sell";
    } else {
      group = await Moralis.Cloud.run("getWalletGroupByAddress", {
        address: request.object.get("to_address")
      });
      side = "buy";
    }

    const metaData = await Moralis.Web3API.token.getNFTMetadata({
      address: request.object.get("token_address"),
      chain: "eth"
    });
    if (metaData) {
      request.object.set("name", metaData.name);
      request.object.set("symbol", metaData.symbol);
    }

    const query = new Moralis.Query("EthTransactions");
    query.equalTo("hash", request.object.get("transaction_hash"));
    const ethTransaction = await query.first();

    if (ethTransaction) {
      request.object.set("eth_value", ethTransaction.get("value"));
      request.object.set("usd", ethTransaction.get("usd"));
    }

    request.object.set("side", side);
    request.object.set("group", group);
  } catch (error) {
    logger.info("Error: " + error);
  }
});

I’ll have to check to see if it works as expected.

ok lmk please ty in advance

its still saving objects with null address in the collection, any update on this how to prevent them from beeing saved?

now I noticed that you have a try catch there in the code

did you try directly without that try for the part that checks from_address and to_address?

I think I solved this by replacing throw new Error with just throw “errormessage”

1 Like

Update, just noticed its still not working, have a bunch of null addresses beeing saved again.

Are you getting the throw message logged? Can we see what you’ve done currently?

@csidi90 I think you need to make sure that your code goes through the if condition, you can try console.log("check") inside the if statement to see that when the address is a NULL Address your code runs through the if statement.

Whenever a null address was detected inside the IF check, it is getting printed to the log “Null Address Detected” but it is still beeing saved even tho the if check is doing its job.

This is the current version I have for that function. This is logging all errors to console, but still proceeding to save the objects for whatever reason

Moralis.Cloud.beforeSave("EthNFTTransfers", async request => {
  const from_address = request.object.get("from_address");
  const to_address = request.object.get("to_address");

  try {
    if (
      from_address == "0x0000000000000000000000000000000000000000" ||
      to_address == "0x0000000000000000000000000000000000000000"
    ) {
      throw "Null Address detected";
    }

    let side;
    let group;

    const is_synced_address = await Moralis.Cloud.run("isSyncedAddress", {
      address: from_address
    });

    if (is_synced_address) {
      group = await Moralis.Cloud.run("getWalletGroupByAddress", {
        address: from_address
      });
      side = "sell";
    } else {
      group = await Moralis.Cloud.run("getWalletGroupByAddress", {
        address: request.object.get("to_address")
      });
      side = "buy";
    }
    if(!group) throw "no group for NFTTrade address";
    if (!group.startsWith("NFT")) throw "NFTTransfer for non NFT group found";

    const metaData = await Moralis.Web3API.token.getNFTMetadata({
      address: request.object.get("token_address"),
      chain: "eth"
    });
    if (metaData) {
      request.object.set("name", metaData.name);
      request.object.set("symbol", metaData.symbol);
    }

    const decodedLog = await Moralis.Cloud.run("parseNFTLogs", {
      transaction_hash: request.object.get("transaction_hash")
    });

    if (!decodedLog) throw "No OrdersMatched Event found for transaction";

    const eth_value = Number(web3.utils.fromWei(decodedLog.price));

    const ticker = await Moralis.Cloud.run("getTickerBySymbol", {
      symbol: "WETH"
    });
    const ticker_price = ticker.get("current_price");
    const usd_value = Number((eth_value * ticker_price).toFixed(5));

    logger.info("NFT ORDERSMATCHED DECODED LOG PRICE " + usd_value)
    request.object.set("eth_value", eth_value);
    request.object.set("usd", usd_value);
    request.object.set("side", side);
    request.object.set("group", group);
  } catch (error) {
    logger.info("Error: " + error);
  }
});

I did a test on my server and it looks like it also saved the data even if I used throw in beforeSave for EthNFTTransfers table.

how to overcome this

this was fixed in the latest version of the server