Skip to content

Commit

Permalink
fix(morpho): wrong label for supply position
Browse files Browse the repository at this point in the history
  • Loading branch information
julien-devatom committed Mar 28, 2024
1 parent 62521d6 commit 57da53b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/maths/shares.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
34 changes: 18 additions & 16 deletions src/morpho-blue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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();
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
27 changes: 11 additions & 16 deletions src/sdk/position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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();

Expand Down Expand Up @@ -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) {
Expand All @@ -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(
Expand All @@ -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())) {
Expand Down

0 comments on commit 57da53b

Please sign in to comment.