diff --git a/packages/indexer/src/messaging/priceWorker.ts b/packages/indexer/src/messaging/priceWorker.ts index f0d6470..23040fa 100644 --- a/packages/indexer/src/messaging/priceWorker.ts +++ b/packages/indexer/src/messaging/priceWorker.ts @@ -148,22 +148,33 @@ export class PriceWorker { private async run(params: PriceMessage) { const { depositId, originChainId } = params; - const relayHashInfo = await this.relayHashInfoRepository.findOne({ + const relayHashInfoArray = await this.relayHashInfoRepository.find({ where: { depositId, originChainId }, }); const deposit = await this.depositRepository.findOne({ where: { depositId, originChainId }, }); + // if we have multiple relays for same deposit, find hte one which matches the deposit hash + // the others would be invalid fills + const relayHashInfo = relayHashInfoArray.find( + (info) => info.depositTxHash === (deposit && deposit.transactionHash), + ); + + const errorMessage = + "Failed to retrieve relay hash information or deposit record from the database."; - // This is catastrophic, we dont want worker retrying if we cannot find this data + // we will keep retrying until found or we know there was a reorg if (!relayHashInfo || !deposit) { this.logger.error({ at: "PriceWorker", - message: - "Failed to retrieve relay hash information or deposit record from the database.", + message: errorMessage, ...params, }); - return; + throw new Error(errorMessage); + } + + // this is a case where we have a potential invalid fill, we should stop trying to process this + if (deposit.transactionHash !== relayHashInfo.depositTxHash) { } // if blockTimestamp doesnt exist, maybe we keep retrying till it does @@ -210,10 +221,24 @@ export class PriceWorker { }; const bridgeFee = PriceWorker.calculateBridgeFee(inputToken, outputToken); - relayHashInfo.bridgeFeeUsd = bridgeFee.toString(); - relayHashInfo.inputPriceUsd = inputTokenPrice; - relayHashInfo.outputPriceUsd = inputTokenPrice; - await this.relayHashInfoRepository.save(relayHashInfo); + const updatedFields: Partial = {}; + + if (relayHashInfo.bridgeFeeUsd !== bridgeFee.toString()) { + updatedFields.bridgeFeeUsd = bridgeFee.toString(); + } + if (relayHashInfo.inputPriceUsd !== inputTokenPrice) { + updatedFields.inputPriceUsd = inputTokenPrice; + } + if (relayHashInfo.outputPriceUsd !== outputTokenPrice) { + updatedFields.outputPriceUsd = outputTokenPrice; + } + + if (Object.keys(updatedFields).length > 0) { + await this.relayHashInfoRepository.update( + { depositId, originChainId }, + updatedFields, + ); + } } public async close() { return this.worker.close();