Skip to content

Commit

Permalink
✨ irm: new parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
jgalat committed Jan 24, 2024
1 parent 78c3b9a commit 033b141
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 13 deletions.
42 changes: 36 additions & 6 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ type MarketUpdate @entity {
floatingAssets: BigInt!
floatingBorrowShares: BigInt!
floatingDebt: BigInt!
floatingBackupBorrowed: BigInt!
earningsAccumulator: BigInt!
}

Expand Down Expand Up @@ -167,12 +168,20 @@ type InterestRateModelSet @entity {
market: Bytes!
timestamp: Int!
interestRateModel: Bytes!
fixedCurveA: BigInt!
fixedCurveB: BigInt!
fixedMaxUtilization: BigInt!
floatingCurveA: BigInt!
floatingCurveB: BigInt!
floatingMaxUtilization: BigInt!
naturalUtilization: BigInt!
sigmoidSpeed: BigInt!
growthSpeed: BigInt!
maxRate: BigInt!
spreadFactor: BigInt!
timePreference: BigInt!
fixedAllocation: BigInt!
maturitySpeed: BigInt!
fixedCurveA: BigInt!
fixedCurveB: BigInt!
fixedMaxUtilization: BigInt!
}

type Account @entity {
Expand Down Expand Up @@ -261,18 +270,27 @@ type Market @entity {
lastMarketUpdate: Int
floatingAssets: BigInt!
floatingDebt: BigInt!
floatingBackupBorrowed: BigInt!
earningsAccumulator: BigInt!
# FloatingDebtUpdate
floatingUtilization: BigInt
lastFloatingDebtUpdate: Int!
# InterestRateModelSet
interestRateModel: Bytes!
fixedCurveA: BigInt!
fixedCurveB: BigInt!
fixedMaxUtilization: BigInt!
floatingCurveA: BigInt!
floatingCurveB: BigInt!
floatingMaxUtilization: BigInt!
naturalUtilization: BigInt!
sigmoidSpeed: BigInt!
growthSpeed: BigInt!
maxRate: BigInt!
spreadFactor: BigInt!
timePreference: BigInt!
fixedAllocation: BigInt!
maturitySpeed: BigInt!
fixedCurveA: BigInt!
fixedCurveB: BigInt!
fixedMaxUtilization: BigInt!
# AccumulatorAccrual
lastAccumulatorAccrual: Int!
# EarningsAccumulatorSmoothFactorSet
Expand Down Expand Up @@ -370,11 +388,23 @@ type MarketState @entity {
totalFloatingBorrowShares: BigInt
totalSupply: BigInt!
floatingDebt: BigInt
floatingBackupBorrowed: BigInt
earningsAccumulator: BigInt
floatingUtilization: BigInt
floatingCurveA: BigInt
floatingCurveB: BigInt
floatingMaxUtilization: BigInt
naturalUtilization: BigInt!
sigmoidSpeed: BigInt!
growthSpeed: BigInt!
maxRate: BigInt!
spreadFactor: BigInt!
timePreference: BigInt!
fixedAllocation: BigInt!
maturitySpeed: BigInt!
fixedCurveA: BigInt!
fixedCurveB: BigInt!
fixedMaxUtilization: BigInt!
lastAccumulatorAccrual: Int!
earningsAccumulatorSmoothFactor: BigInt
treasuryFeeRate: BigInt
Expand Down
40 changes: 33 additions & 7 deletions src/Market.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import savedAccount from './utils/savedAccount';
import loadAccount from './utils/loadAccount';
import loadMarket from './utils/loadMarket';
import fixedRate from './utils/fixedRate';
import orZero from './utils/orZero';
import toId from './utils/toId';

export function handleDeposit(event: DepositEvent): void {
Expand Down Expand Up @@ -289,23 +290,44 @@ export function handleInterestRateModelSet(event: InterestRateModelSetEvent): vo
entity.interestRateModel = event.params.interestRateModel;

const irm = InterestRateModelContract.bind(event.params.interestRateModel);
entity.fixedCurveA = irm.fixedCurveA();
entity.fixedCurveB = irm.fixedCurveB();
entity.fixedMaxUtilization = irm.fixedMaxUtilization();
entity.floatingCurveA = irm.floatingCurveA();
entity.floatingCurveB = irm.floatingCurveB();
entity.floatingMaxUtilization = irm.floatingMaxUtilization();

entity.naturalUtilization = orZero(irm.try_naturalUtilization());
entity.sigmoidSpeed = orZero(irm.try_sigmoidSpeed());
entity.growthSpeed = orZero(irm.try_growthSpeed());
entity.maxRate = orZero(irm.try_maxRate());
entity.spreadFactor = orZero(irm.try_spreadFactor());
entity.timePreference = orZero(irm.try_timePreference());
entity.fixedAllocation = orZero(irm.try_fixedAllocation());
entity.maturitySpeed = orZero(irm.try_maturitySpeed());

entity.fixedCurveA = irm.fixedCurveA();
entity.fixedCurveB = irm.fixedCurveB();
entity.fixedMaxUtilization = irm.fixedMaxUtilization();

entity.save();

const market = loadMarket(entity.market, event);
market.interestRateModel = entity.interestRateModel;
market.fixedCurveA = entity.fixedCurveA;
market.fixedCurveB = entity.fixedCurveB;
market.fixedMaxUtilization = entity.fixedMaxUtilization;
market.floatingCurveA = entity.floatingCurveA;
market.floatingCurveB = entity.floatingCurveB;
market.floatingMaxUtilization = entity.floatingMaxUtilization;

market.naturalUtilization = entity.naturalUtilization;
market.sigmoidSpeed = entity.sigmoidSpeed;
market.growthSpeed = entity.growthSpeed;
market.maxRate = entity.maxRate;
market.spreadFactor = entity.spreadFactor;
market.timePreference = entity.timePreference;
market.fixedAllocation = entity.fixedAllocation;
market.maturitySpeed = entity.maturitySpeed;

market.fixedCurveA = entity.fixedCurveA;
market.fixedCurveB = entity.fixedCurveB;
market.fixedMaxUtilization = entity.fixedMaxUtilization;

market.save();

saveMarketState(event, market);
Expand All @@ -328,6 +350,8 @@ export function handleTreasurySet(event: TreasurySetEvent): void {
}

export function handleMarketUpdate(event: MarketUpdateEvent): void {
const contract = Market.bind(event.address);

const entity = new MarketUpdate(toId(event));
entity.market = event.address;
entity.timestamp = event.params.timestamp.toU32();
Expand All @@ -336,6 +360,7 @@ export function handleMarketUpdate(event: MarketUpdateEvent): void {
entity.floatingBorrowShares = event.params.floatingBorrowShares;
entity.floatingDebt = event.params.floatingDebt;
entity.earningsAccumulator = event.params.earningsAccumulator;
entity.floatingBackupBorrowed = contract.floatingBackupBorrowed();
entity.save();

const market = loadMarket(entity.market, event);
Expand All @@ -344,8 +369,9 @@ export function handleMarketUpdate(event: MarketUpdateEvent): void {
market.floatingAssets = entity.floatingAssets;
market.totalFloatingBorrowShares = entity.floatingBorrowShares;
market.floatingDebt = entity.floatingDebt;
market.floatingBackupBorrowed = entity.floatingBackupBorrowed;
market.earningsAccumulator = entity.earningsAccumulator;
market.symbol = Market.bind(Address.fromBytes(entity.market)).symbol();
market.symbol = contract.symbol();
market.save();

saveMarketState(event, market);
Expand Down
10 changes: 10 additions & 0 deletions src/utils/loadMarket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Market } from '../../generated/schema';
import { ERC20 } from '../../generated/Auditor/ERC20';
import { Market as MarketContract } from '../../generated/Auditor/Market';
import { InterestRateModel } from '../../generated/Auditor/InterestRateModel';
import orZero from './orZero';

export default function loadMarket(market: Bytes, event: ethereum.Event): Market {
const id = market.toHexString();
Expand All @@ -23,6 +24,7 @@ export default function loadMarket(market: Bytes, event: ethereum.Event): Market
entity.lastAccumulatorAccrual = mkt.lastAccumulatorAccrual().toU32();
entity.floatingAssets = mkt.floatingAssets();
entity.floatingDebt = mkt.floatingDebt();
entity.floatingBackupBorrowed = mkt.floatingBackupBorrowed();
entity.earningsAccumulator = mkt.earningsAccumulator();
entity.decimals = mkt.decimals();
entity.symbol = mkt.symbol();
Expand Down Expand Up @@ -53,5 +55,13 @@ export default function loadMarket(market: Bytes, event: ethereum.Event): Market
? entity.floatingDebt.div(entity.floatingAssets)
: BigInt.zero();

entity.floatingNaturalUtilization = orZero(irm.try_floatingNaturalUtilization());
entity.sigmoidSpeed = orZero(irm.try_sigmoidSpeed());
entity.growthSpeed = orZero(irm.try_growthSpeed());
entity.maxRate = orZero(irm.try_maxRate());
entity.spreadFactor = orZero(irm.try_spreadFactor());
entity.timePreference = orZero(irm.try_timePreference());
entity.maturitySpeed = orZero(irm.try_maturitySpeed());

return entity;
}
6 changes: 6 additions & 0 deletions src/utils/orZero.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { BigInt, ethereum } from '@graphprotocol/graph-ts';

export default function orZero(result: ethereum.CallResult<BigInt>): BigInt {
if (result.reverted) return BigInt.zero();
return result.value;
}
12 changes: 12 additions & 0 deletions src/utils/saveMarketState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,23 @@ export default function saveMarketState(event: ethereum.Event, market: Market):
marketState.totalFloatingBorrowShares = market.totalFloatingBorrowShares;
marketState.floatingAssets = market.floatingAssets;
marketState.floatingDebt = market.floatingDebt;
marketState.floatingBackupBorrowed = market.floatingBackupBorrowed;
marketState.earningsAccumulator = market.earningsAccumulator;
marketState.floatingUtilization = market.floatingUtilization;
marketState.floatingCurveA = market.floatingCurveA;
marketState.floatingCurveB = market.floatingCurveB;
marketState.floatingMaxUtilization = market.floatingMaxUtilization;
marketState.naturalUtilization = market.naturalUtilization;
marketState.sigmoidSpeed = market.sigmoidSpeed;
marketState.growthSpeed = market.growthSpeed;
marketState.maxRate = market.maxRate;
marketState.spreadFactor = market.spreadFactor;
marketState.timePreference = market.timePreference;
marketState.fixedAllocation = market.fixedAllocation;
marketState.maturitySpeed = market.maturitySpeed;
marketState.fixedCurveA = market.fixedCurveA;
marketState.fixedCurveB = market.fixedCurveB;
marketState.fixedMaxUtilization = market.fixedMaxUtilization;
marketState.lastAccumulatorAccrual = market.lastAccumulatorAccrual;
marketState.earningsAccumulatorSmoothFactor = market.earningsAccumulatorSmoothFactor;
marketState.treasuryFeeRate = market.treasuryFeeRate;
Expand Down

0 comments on commit 033b141

Please sign in to comment.