Skip to content

Commit

Permalink
change saves to updates, handle invalid fills
Browse files Browse the repository at this point in the history
Signed-off-by: david <david@umaproject.org>
  • Loading branch information
daywiss committed Jan 6, 2025
1 parent 4e3756e commit 58703e7
Showing 1 changed file with 34 additions and 9 deletions.
43 changes: 34 additions & 9 deletions packages/indexer/src/messaging/priceWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<typeof relayHashInfo> = {};

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();
Expand Down

0 comments on commit 58703e7

Please sign in to comment.