From 7c9cbd142a3d9bdbace0b97a6f3452e32e50d716 Mon Sep 17 00:00:00 2001 From: mbthiery Date: Thu, 21 Dec 2023 12:02:59 -0500 Subject: [PATCH] Add guard to avoid primary key conflict --- src/knex/maxSupply.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/knex/maxSupply.ts b/src/knex/maxSupply.ts index 5e5fa1b..a13f602 100644 --- a/src/knex/maxSupply.ts +++ b/src/knex/maxSupply.ts @@ -1,4 +1,5 @@ import { epochFromDate } from "@/app/stats/utils" +import { isEqual } from "date-fns" import { Knex } from "knex" type MaxSupplyRecord = { @@ -65,7 +66,13 @@ export class MaxSupply { // true when first recording of the day happens between treasury and HST emissions (isSameDay && (latest?.supply || 0) < record.supply) ) { - await this.addRecord(record) + // guard to avoid duplicate entry for primary key + if ( + !isEqual(record.recorded_at, latest?.recorded_at || 0) && + !isEqual(record.recorded_at, latestBurn?.recorded_at || 0) + ) { + await this.addRecord(record) + } if (record.hnt_burned === BigInt(0)) return record if (!latest && !latestBurn) return record }