Skip to content

Commit

Permalink
Subgraph: preserve the magnitude when flooring the rates
Browse files Browse the repository at this point in the history
  • Loading branch information
bpierre committed Sep 4, 2024
1 parent 308e622 commit 043d610
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions subgraph/src/TroveManager.mapping.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Address, BigInt, dataSource, log } from "@graphprotocol/graph-ts";
import { Address, BigInt, dataSource } from "@graphprotocol/graph-ts";
import { InterestRateBracket, Trove } from "../generated/schema";
import { TroveNFT } from "../generated/templates/TroveManager/TroveNFT";
import {
Expand All @@ -16,8 +16,10 @@ let OP_CLOSE_TROVE = 1;
// let OP_LIQUIDATE = 5;
// let OP_REDEEM_COLLATERAL = 6;

let _1e15 = BigInt.fromI32(10).pow(15);
let _1e16 = BigInt.fromI32(10).pow(16);
function floorToDecimals(value: BigInt, decimals: u8): BigInt {
let factor = BigInt.fromI32(10).pow(18 - decimals);
return value.div(factor).times(factor);
}

export function handleTroveOperation(event: TroveOperationEvent): void {
let id = event.params._troveId;
Expand All @@ -31,7 +33,7 @@ export function handleTroveOperation(event: TroveOperationEvent): void {
trove.closedAt = event.block.timestamp;

// update rate bracket
let rateFloored = event.params._annualInterestRate.div(_1e15).times(_1e16);
let rateFloored = floorToDecimals(event.params._annualInterestRate, 3);
let rateBracket = InterestRateBracket.load(rateFloored.toString());
if (rateBracket) {
rateBracket.totalDebt = rateBracket.totalDebt.minus(trove.debt);
Expand Down Expand Up @@ -63,8 +65,8 @@ export function handleTroveUpdated(event: TroveUpdatedEvent): void {
let context = dataSource.context();

// previous & new rates, floored to the nearest 0.1% (rate brackets)
let prevRateFloored = trove ? trove.interestRate.div(_1e15).times(_1e16) : null;
let rateFloored = event.params._annualInterestRate.div(_1e15).times(_1e16);
let prevRateFloored = trove ? floorToDecimals(trove.interestRate, 3) : null;
let rateFloored = floorToDecimals(event.params._annualInterestRate, 3);

// create trove if it doesn't exist
if (!trove) {
Expand Down

0 comments on commit 043d610

Please sign in to comment.