ponder-sh / ponder

A backend framework for crypto apps

Home Page:https://ponder.sh

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Bug]: Many items I have deleted still exist in database.

XmoDVO opened this issue · comments

description

ponder version: v0.3.8;
network: zksync-era mainnet;

This error occurs when ponder firstly do historical sync event logs (which means the realtime sync is ok). If I restart the project with npm run dev or npm run start after delete ponder_cache.db and ponder.db, the error may disappear.

for example, I first created a optionPosition at block 20687384 and then deleted it at block 20687681; but the option position is still there, not accurately deleted.

query

query OptionPositions {
  optionPositions(where: {id: "d78fcecc3e0ae29309b733b9d6e98526a6c78d8864e83d61677448803ba3878d-fc00dAC2-v1"}) {
    items {
      id
      startTime
      endTime
      createdBlockNumber
    }
  }
  historyItems(where: {actionId: "d78fcecc3e0ae29309b733b9d6e98526a6c78d8864e83d61677448803ba3878d-fc00dAC2-v1"}) {
    items {
      action
      actionId
      blockNumber
    }
  }
}

response

{
  "data": {
    "optionPositions": {
      "items": [
        {
          "id": "d78fcecc3e0ae29309b733b9d6e98526a6c78d8864e83d61677448803ba3878d-fc00dAC2-v1",
          "startTime": "1701736411",
          "endTime": "1701736711",
          "createdBlockNumber": "20687384"
        }
      ]
    },
    "historyItems": {
      "items": [
        {
          "action": 7,  // delete action
          "actionId": "d78fcecc3e0ae29309b733b9d6e98526a6c78d8864e83d61677448803ba3878d-fc00dAC2-v1",
          "blockNumber": "20687681"
        },
        {
          "action": 6, // create action
          "actionId": "d78fcecc3e0ae29309b733b9d6e98526a6c78d8864e83d61677448803ba3878d-fc00dAC2-v1",
          "blockNumber": "20687384"
        }
      ]
    }
  }
}

codes

import { ponder } from '@/generated';
import { zeroAddress } from 'viem';
import { HistoryItemAction } from './history';

interface OptionIdParams {
  key: string;
  pool: string;
  owner?: string;
  version?: number;
}

const getOptionId = ({ key, pool, version = 1 }: OptionIdParams) => {
  key = key.replace('0x', '');
  pool = pool.replace('0x', '').substring(0, 8);

  return `${key}-${pool}-v${version}`;
};

ponder.on('PoolContract:OptionPositionOpenedV2', async ({ event, context }) => {
  const { OptionPosition, HistoryItem } = context.db;
  const pool = event.log.address;
  const {
    key,
    owner,
    collateralToken,
    indexAsset,
    isLong,
    startTime,
    endTime,
    collateralAmount,
    strikePrice,
    reserveAmount,
    entryCumulativeFinalLong,
    entryCumulativeFinalShort,
    feeCollateralAmount,
  } = event.args;

  const { number, timestamp, hash } = event.block;

  const id = getOptionId({ key, pool });

  await OptionPosition.create({
    id,
    data: {
      network: 320,
      latestExecutedOrderKey: key,
      latestRequestedOrderKey: key,
      created: true,
      pool: pool,
      owner: owner,
      inToken: zeroAddress,
      collateralToken: collateralToken,
      indexAsset: indexAsset,
      isLong: isLong,
      startTime: startTime,
      endTime: endTime,
      collateralAmount: collateralAmount,
      strikePrice: strikePrice,
      reserveAmount: reserveAmount,
      entryCumulativeFinalLong: entryCumulativeFinalLong,
      entryCumulativeFinalShort: entryCumulativeFinalShort,
      feeCollateralAmount: feeCollateralAmount,
      createdBlockNumber: number,
      createdBlockTimestamp: timestamp,
      createdTransactionHash: hash,
    },
  });

  await HistoryItem.create({
    id: `${id}-option-opened`,
    data: {
      pool,
      network: 320,
      actionId: id,
      action: HistoryItemAction.OptionPositionOpened, // action 6
      owner,
      blockNumber: number,
      blockTimestamp: timestamp,
      transactionHash: hash,
    },
  });
});

ponder.on('PoolContract:OptionPositionClosed', async ({ event, context }) => {
  const { OptionPosition, HistoryItem } = context.db;
  const pool = event.log.address;
  const { key, owner } = event.args;

  const id = getOptionId({ key, pool });

  await OptionPosition.delete({
    id,
  });

  await HistoryItem.create({
    id: `${id}-option-closed`,
    data: {
      network: 320,
      pool,
      actionId: id,
      action: HistoryItemAction.OptionPositionClosed, // action 7
      owner,
      blockNumber: event.block.number,
      blockTimestamp: event.block.timestamp,
      transactionHash: event.transaction.hash,
    },
  });
});

This problem seems to be that when historical data is synchronized, the new block is executed first and the old block is executed later; or an error occurs during historical synchronization execution.

An usual error at first sync.