diff --git a/src/maths/shares.ts b/src/maths/shares.ts index a7f7793..6ee79fa 100644 --- a/src/maths/shares.ts +++ b/src/maths/shares.ts @@ -3,7 +3,7 @@ import { BigInt } from "@graphprotocol/graph-ts"; import { mulDivDown, mulDivUp } from "./maths"; const VIRTUAL_SHARES = BigInt.fromI32(10).pow(6); -const VIRTUAL_ASSETS = BigInt.zero(); +const VIRTUAL_ASSETS = BigInt.fromI32(1); export function toAssetsUp( shares: BigInt, diff --git a/src/morpho-blue.ts b/src/morpho-blue.ts index ae66e74..ee6050c 100644 --- a/src/morpho-blue.ts +++ b/src/morpho-blue.ts @@ -24,7 +24,7 @@ import { BadDebtRealization } from "../generated/schema"; import { createMarket, getMarket, getZeroMarket } from "./initializers/markets"; import { getProtocol } from "./initializers/protocol"; import { AccountManager } from "./sdk/account"; -import { BIGDECIMAL_WAD, PositionSide } from "./sdk/constants"; +import { BIGDECIMAL_WAD, BIGINT_WAD, PositionSide } from "./sdk/constants"; import { DataManager } from "./sdk/manager"; import { PositionManager } from "./sdk/position"; import { TokenManager } from "./sdk/token"; @@ -49,18 +49,20 @@ export function handleAccrueInterest(event: AccrueInterest): void { log.critical("Inconsistent fee data for market {}", [ market.id.toHexString(), ]); - const protocol = getProtocol(); - const feeRecipientAccount = new AccountManager( - protocol.feeRecipient - ).getAccount(); - const position = new PositionManager( - feeRecipientAccount, - market, - PositionSide.SUPPLIER - ); - // TODO: do not count the fee as a deposit in snapshots etc. - position.addSupplyPosition(event, event.params.feeShares); } + + const protocol = getProtocol(); + const feeRecipientAccount = new AccountManager( + protocol.feeRecipient + ).getAccount(); + const position = new PositionManager( + feeRecipientAccount, + market, + PositionSide.SUPPLIER + ); + const feeAmount = event.params.interest.times(market.fee).div(BIGINT_WAD); + // TODO: do not count the fee as a deposit in snapshots etc. + position.addSupplyPosition(event, event.params.feeShares, feeAmount); } market.save(); @@ -123,8 +125,6 @@ export function handleFlashLoan(event: FlashLoan): void { const market = getZeroMarket(event); const manager = new DataManager(market.id, event); - const token = new TokenManager(event.params.token, event); - manager.createFlashloan( event.params.token, event.params.caller, @@ -269,7 +269,8 @@ export function handleSupply(event: Supply): void { const position = positionManager.addSupplyPosition( event, - event.params.shares + event.params.shares, + event.params.assets ); market.totalSupply = market.totalSupply.plus(event.params.assets); @@ -316,7 +317,8 @@ export function handleWithdraw(event: Withdraw): void { const position = positionManager.reduceSupplyPosition( event, - event.params.shares + event.params.shares, + event.params.assets ); market.totalSupply = market.totalSupply.minus(event.params.assets); diff --git a/src/sdk/position.ts b/src/sdk/position.ts index 466373d..d35365a 100644 --- a/src/sdk/position.ts +++ b/src/sdk/position.ts @@ -97,7 +97,11 @@ export class PositionManager { this._countDailyActivePosition(positionCounter, event); return this._position!; } - addSupplyPosition(event: ethereum.Event, sharesSupplied: BigInt): Position { + addSupplyPosition( + event: ethereum.Event, + sharesSupplied: BigInt, + assets: BigInt + ): Position { let positionCounter = _PositionCounter.load(this._counterID); if (!positionCounter) { positionCounter = new _PositionCounter(this._counterID); @@ -115,15 +119,10 @@ export class PositionManager { position = this._createPosition( positionID, event, - TransactionType.DEPOSIT_COLLATERAL + TransactionType.DEPOSIT ); } - const amountSupplied = toAssetsDown( - sharesSupplied, - this._market.totalSupplyShares, - this._market.totalSupply - ); position.shares = position.shares ? position.shares!.plus(sharesSupplied) : sharesSupplied; @@ -136,8 +135,8 @@ export class PositionManager { position.balance = totalSupply; position.principal = position.principal - ? position.principal!.plus(amountSupplied) - : amountSupplied; + ? position.principal!.plus(assets) + : assets; position.depositCount += 1; position.save(); @@ -291,7 +290,8 @@ export class PositionManager { reduceSupplyPosition( event: ethereum.Event, - sharesWithdrawn: BigInt + sharesWithdrawn: BigInt, + assets: BigInt ): Position { let positionCounter = _PositionCounter.load(this._counterID); if (!positionCounter) { @@ -312,11 +312,6 @@ export class PositionManager { ]); return this._position!; } - const amountWithdrawn = toAssetsDown( - sharesWithdrawn, - this._market.totalSupplyShares, - this._market.totalSupply - ); position.shares = position.shares!.minus(sharesWithdrawn); const totalSupply = toAssetsDown( @@ -326,7 +321,7 @@ export class PositionManager { ); position.balance = totalSupply; - position.principal = position.principal!.minus(amountWithdrawn); + position.principal = position.principal!.minus(assets); position.withdrawCount += 1; this._position = position; if (position.shares!.equals(BigInt.zero())) {