Skip to content

Commit

Permalink
Fix div 0 error in convex
Browse files Browse the repository at this point in the history
  • Loading branch information
steegecs committed Jan 13, 2025
1 parent 2823115 commit 9c9c8b5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion deployment/deployment.json
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@
"status": "prod",
"versions": {
"schema": "1.3.0",
"subgraph": "1.2.3",
"subgraph": "1.4.0",
"methodology": "1.0.0"
},
"files": {
Expand Down
20 changes: 15 additions & 5 deletions subgraphs/convex-finance/src/Prices/routers/CurveRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,29 @@ export function getCurvePriceUsdc(
let basePrice = getBasePrice(curveLpTokenAddress, curveRegistry, network);
let virtualPrice = getVirtualPrice(curveLpTokenAddress);

// Return early if either price is zero to avoid division by zero
if (basePrice.usdPrice.equals(constants.BIGDECIMAL_ZERO) || virtualPrice.equals(constants.BIGDECIMAL_ZERO)) {
return new CustomPriceType();
}

let usdcDecimals = utils.getTokenDecimals(tokensMapping!.get("USDC")!);
let decimalsAdjustment = constants.DEFAULT_DECIMALS.minus(usdcDecimals);

let denominator = constants.BIGINT_TEN.pow(
decimalsAdjustment.plus(constants.DEFAULT_DECIMALS).toI32() as u8
).toBigDecimal();

// Check for zero denominator
if (denominator.equals(constants.BIGDECIMAL_ZERO)) {
return new CustomPriceType();
}

let price = virtualPrice
.times(basePrice.usdPrice)
.times(
constants.BIGINT_TEN.pow(decimalsAdjustment.toI32() as u8).toBigDecimal()
)
.div(
constants.BIGINT_TEN.pow(
decimalsAdjustment.plus(constants.DEFAULT_DECIMALS).toI32() as u8
).toBigDecimal()
);
.div(denominator);

return CustomPriceType.initialize(price, 6);
}
Expand Down

0 comments on commit 9c9c8b5

Please sign in to comment.