Moralis Subscription notification racing condition on update transaction confirmed

I Have a racing problem when using moralis subscription functionality for listening transaction on blockchain.

whenever i send a money to my friend’s address, the subscription will run twice simultaneously after the transaction was confirmed, though it correctly only run once to give me update that the transaction hasn’t confirmed yet. this case also reproduce able when my friend sending fund to my wallet address.

i have checked if maybe my address is registered more than once on the moralis dashboard but its not

here’s my piece of code, will run listener for each network of [ethereum, binance, and polygon] and this issue is found on binance network.

const subscribeToMoralisDB = async (
  dbName: string,
  callback: BlockchainTransactionCallback,
) => {
  const bscQuery = new Moralis.Query(dbName);
  const bscSubscription = await bscQuery
    .subscribe()
    .catch((error) => console.log(error));
  bscSubscription &&
    bscSubscription.on("update", (data) => {
      if (data.attributes.confirmed) {
        callback(data.attributes as BlockchainTransaction);
      }
    });
};

Can you log the data parameter to see what is there?
You could get that multiple times if the row is updating multiple times.

sure, let me log it again and provide the log data to you

Here’s the response,

modified Code

const subscribeToMoralisDBToken = async (
  dbName: string,
  callback: BlockchainTokenTransactionCallback
) => {
  console.log("start listening for token transaction");
  const bscQuery = new Moralis.Query(dbName);
  const network = determineNetwork(dbName);
  const bRPC = getBlockchainRPCInstance(network);
  const bscSubscription = await bscQuery
    .subscribe()
    .catch((error) => console.log(error));
  bscSubscription &&
    bscSubscription.on("update", async (data) => {
      console.log("token transaction");
      //check does the receiver a contract address or not
      const isContract = await isContractAddress(
        bRPC,
        data.attributes.to_address
      );
      if (data.attributes.confirmed && !isContract) {
        counter++;
        console.log("Transaction confirmed = ", counter);
        console.log("data = ", data.attributes)
        callback({ ...data.attributes, network } as BlockchainTokenTransaction);
      }
    });
};

the token transaction was fired 3 times for giving me notice if the tx notif is fired for the hasnt confirmed tx.

but i put counter Transaction confirmed = to count how many times the tx confirmed is fired

it seems that this was printed twice, like it was called twice somehow

this was called thrice for each network

image

here are the rest of the log when i start the local.

as you can see it run each the subscription for each schema once

can you try to run it once only for bsc and not for all the networks?

sure, here’s the result of running only BSC network


image

It looks like there is an update to the object, and that is why it is triggered twice, updatedAt seems different

what version of Moralis Server are you using? it is the nitro version?

ah yea, you’re right. i checked on the dashboard logs it indeed has 2 different updatedAt time

the version is 0.0.345 (Mainnet)
here’s our AppID = spc4oBmuQPMMUpYWmwclJTEewmflwAMabedxNzjs

you could try a nitro server too
I don’t know exactly why there are 2 updates, but you should take in consideration the possibility of db data being updated multiple times

so… this problem is not resolved from moralis side?

the subscription seems to work as expected, what seem to happen is that the row gets updated twice in a short period of time

yeeaa… the subscription is working as expected, to get notification from blockchain.

don’t you want to try to debug this problem from your code side?
since this problem is from moralis server showed on the logs,

it doesn’t look like a problem to me, it looks like a db row gets updated twice, that can happen, you can not count on that not happening

also when using nitro I would expect that to happen once when the row is created and once when it is updated