Skip to content

Commit

Permalink
feat: clone rates for tx entities
Browse files Browse the repository at this point in the history
  • Loading branch information
julien-devatom committed May 22, 2024
1 parent e0f30b5 commit eba528d
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 9 deletions.
7 changes: 4 additions & 3 deletions src/meta-morpho.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import {
} from "./sdk/metamorpho";
import { TokenManager } from "./sdk/token";
import { toMetaMorphoAssetsUp } from "./utils/metaMorphoUtils";
import { cloneRate } from "./utils/rate";

export function handleSubmitMarketRemoval(
event: SubmitMarketRemovalEvent
Expand Down Expand Up @@ -184,7 +185,7 @@ export function handleDeposit(event: DepositEvent): void {
deposit.shares = event.params.shares;
deposit.metaMorpho = mm.id;
deposit.metaMorphoPosition = position.id;
deposit.rate = mm.rate;
deposit.rate = cloneRate(mm.rate, event.block.timestamp).id;
deposit.save();
}

Expand Down Expand Up @@ -694,7 +695,7 @@ export function handleTransfer(event: TransferEvent): void {
transfer.metaMorphoPositionFrom = fromPosition.id;
transfer.metaMorphoPositionTo = toPosition.id;
transfer.metaMorpho = mm.id;
transfer.rate = mm.rate;
transfer.rate = cloneRate(mm.rate, event.block.timestamp).id;
transfer.save();
}

Expand Down Expand Up @@ -752,6 +753,6 @@ export function handleWithdraw(event: WithdrawEvent): void {
withdraw.shares = event.params.shares;
withdraw.metaMorpho = mm.id;
withdraw.metaMorphoPosition = position.id;
withdraw.rate = mm.rate;
withdraw.rate = cloneRate(mm.rate, event.block.timestamp).id;
withdraw.save();
}
70 changes: 64 additions & 6 deletions src/sdk/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
} from "../../generated/schema";
import { getMarket } from "../initializers/markets";
import { getProtocol } from "../initializers/protocol";
import { cloneRates } from "../utils/rate";

import { AccountManager } from "./account";
import {
Expand Down Expand Up @@ -229,7 +230,15 @@ export class DataManager {
deposit.account = position.account;
deposit.market = this._market.id;
deposit.position = position.id;
deposit.rates = this._market.rates;
const marketRates = this._market.rates;
if (!marketRates) {
deposit.rates = [];
} else {
deposit.rates = cloneRates(
this._market.rates!,
this._event.block.timestamp
);
}
deposit.asset = position.asset;
deposit.amount = amount;
deposit.amountUSD = amountUSD;
Expand Down Expand Up @@ -269,7 +278,17 @@ export class DataManager {
deposit.market = this._market.id;
deposit.position = position.id;
deposit.asset = position.asset;
deposit.rates = this._market.rates;

const marketRates = this._market.rates;
if (!marketRates) {
deposit.rates = [];
} else {
deposit.rates = cloneRates(
this._market.rates!,
this._event.block.timestamp
);
}

deposit.shares = shares;
deposit.amount = amount;
deposit.amountUSD = amountUSD;
Expand Down Expand Up @@ -305,7 +324,17 @@ export class DataManager {
withdraw.market = this._market.id;
withdraw.position = position.id;
withdraw.asset = position.asset;
withdraw.rates = this._market.rates;

const marketRates = this._market.rates;
if (!marketRates) {
withdraw.rates = [];
} else {
withdraw.rates = cloneRates(
this._market.rates!,
this._event.block.timestamp
);
}

withdraw.amount = amount;
withdraw.amountUSD = amountUSD;
withdraw.isCollateral = true;
Expand Down Expand Up @@ -349,7 +378,17 @@ export class DataManager {
withdraw.market = this._market.id;
withdraw.position = position.id;
withdraw.asset = this._market.borrowedToken;
withdraw.rates = this._market.rates;

const marketRates = this._market.rates;
if (!marketRates) {
withdraw.rates = [];
} else {
withdraw.rates = cloneRates(
this._market.rates!,
this._event.block.timestamp
);
}

withdraw.amount = amount;
withdraw.amountUSD = amountUSD;
withdraw.isCollateral = false;
Expand Down Expand Up @@ -384,7 +423,17 @@ export class DataManager {
borrow.market = this._market.id;
borrow.position = position.id;
borrow.asset = this._market.borrowedToken;
borrow.rates = this._market.rates;

const marketRates = this._market.rates;
if (!marketRates) {
borrow.rates = [];
} else {
borrow.rates = cloneRates(
this._market.rates!,
this._event.block.timestamp
);
}

borrow.amount = amount;
borrow.amountUSD = amountUSD;
borrow.shares = shares;
Expand Down Expand Up @@ -424,7 +473,16 @@ export class DataManager {
repay.position = position.id;
repay.asset = position.asset;

repay.rates = this._market.rates;
const marketRates = this._market.rates;
if (!marketRates) {
repay.rates = [];
} else {
repay.rates = cloneRates(
this._market.rates!,
this._event.block.timestamp
);
}

repay.amount = amount;
repay.shares = shares;
repay.amountUSD = amountUSD;
Expand Down
35 changes: 35 additions & 0 deletions src/utils/rate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { BigInt, log } from "@graphprotocol/graph-ts";

import { InterestRate } from "../../generated/schema";

export function cloneRate(rateId: string, timestamp: BigInt): InterestRate {
const rate = InterestRate.load(rateId);
if (!rate) {
log.critical("InterestRate {} not found", [rateId]);
return new InterestRate("");
}

const newRateId = rate.id + "-" + timestamp.toString();
let newRate = InterestRate.load(newRateId);
if (newRate) {
return newRate as InterestRate;
}

newRate = new InterestRate(newRateId);
newRate.rate = rate.rate;
newRate.market = rate.market;
newRate.side = rate.side;
newRate.type = rate.type;
newRate.save();

return newRate;
}

export function cloneRates(rateIds: string[], timestamp: BigInt): string[] {
const rates: string[] = [];
for (let i = 0; i < rateIds.length; i++) {
log.info("rateId: {}", [rateIds[i]]);
rates.push(cloneRate(rateIds[i], timestamp).id);
}
return rates;
}

0 comments on commit eba528d

Please sign in to comment.