From e1636d0f13e352d4af23cf1c6b8caa957096a181 Mon Sep 17 00:00:00 2001 From: mantikoros Date: Sat, 8 Oct 2022 12:16:38 -0500 Subject: [PATCH] update metrics: fix divide by zero, elasticity NaN bug --- common/calculate-metrics.ts | 6 +++++- functions/src/update-metrics.ts | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/common/calculate-metrics.ts b/common/calculate-metrics.ts index 6cfb0421..9ad44522 100644 --- a/common/calculate-metrics.ts +++ b/common/calculate-metrics.ts @@ -97,7 +97,11 @@ export const computeBinaryCpmmElasticity = ( ) const resultNo = getCpmmProbability(poolN, pN) - return resultYes - resultNo + // handle AMM overflow + const safeYes = Number.isFinite(resultYes) ? resultYes : 1 + const safeNo = Number.isFinite(resultNo) ? resultNo : 0 + + return safeYes - safeNo } export const computeDpmElasticity = ( diff --git a/functions/src/update-metrics.ts b/functions/src/update-metrics.ts index 887dbbdc..4739dcc1 100644 --- a/functions/src/update-metrics.ts +++ b/functions/src/update-metrics.ts @@ -169,7 +169,7 @@ export async function updateMetricsCore() { return 0 } const contractRatio = - contract.flaggedByUsernames.length / (contract.uniqueBettorCount ?? 1) + contract.flaggedByUsernames.length / (contract.uniqueBettorCount || 1) return contractRatio })