Skip to content

Commit

Permalink
fix(#minor); aave v3 forks; fix flashloan revennue (#2517)
Browse files Browse the repository at this point in the history
  • Loading branch information
melotik authored Mar 28, 2024
1 parent 654ad48 commit b56a540
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 49 deletions.
28 changes: 14 additions & 14 deletions deployment/deployment.json
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@
"status": "prod",
"versions": {
"schema": "3.1.0",
"subgraph": "2.3.0",
"subgraph": "2.4.0",
"methodology": "1.1.0"
},
"files": {
Expand All @@ -362,7 +362,7 @@
"status": "prod",
"versions": {
"schema": "3.1.0",
"subgraph": "2.3.0",
"subgraph": "2.4.0",
"methodology": "1.1.0"
},
"files": {
Expand All @@ -388,7 +388,7 @@
"status": "prod",
"versions": {
"schema": "3.1.0",
"subgraph": "2.3.0",
"subgraph": "2.4.0",
"methodology": "1.1.0"
},
"files": {
Expand All @@ -414,7 +414,7 @@
"status": "prod",
"versions": {
"schema": "3.1.0",
"subgraph": "2.3.0",
"subgraph": "2.4.0",
"methodology": "1.1.0"
},
"files": {
Expand All @@ -440,7 +440,7 @@
"status": "prod",
"versions": {
"schema": "3.1.0",
"subgraph": "2.3.0",
"subgraph": "2.4.0",
"methodology": "1.1.0"
},
"files": {
Expand All @@ -466,7 +466,7 @@
"status": "prod",
"versions": {
"schema": "3.1.0",
"subgraph": "2.3.0",
"subgraph": "2.4.0",
"methodology": "1.1.0"
},
"files": {
Expand All @@ -492,7 +492,7 @@
"status": "dev",
"versions": {
"schema": "3.1.0",
"subgraph": "2.3.0",
"subgraph": "2.4.0",
"methodology": "1.1.0"
},
"files": {
Expand All @@ -514,7 +514,7 @@
"status": "prod",
"versions": {
"schema": "3.1.0",
"subgraph": "2.3.0",
"subgraph": "2.4.0",
"methodology": "1.1.0"
},
"files": {
Expand All @@ -540,7 +540,7 @@
"status": "prod",
"versions": {
"schema": "3.1.0",
"subgraph": "1.3.0",
"subgraph": "1.4.0",
"methodology": "1.1.0"
},
"files": {
Expand All @@ -566,7 +566,7 @@
"status": "prod",
"versions": {
"schema": "3.1.0",
"subgraph": "1.2.0",
"subgraph": "1.3.0",
"methodology": "1.0.0"
},
"files": {
Expand All @@ -592,7 +592,7 @@
"status": "prod",
"versions": {
"schema": "3.1.0",
"subgraph": "1.2.0",
"subgraph": "1.3.0",
"methodology": "1.0.0"
},
"files": {
Expand All @@ -618,7 +618,7 @@
"status": "prod",
"versions": {
"schema": "3.1.0",
"subgraph": "1.2.0",
"subgraph": "1.3.0",
"methodology": "1.0.0"
},
"files": {
Expand Down Expand Up @@ -3830,7 +3830,7 @@
"status": "prod",
"versions": {
"schema": "3.1.0",
"subgraph": "2.3.0",
"subgraph": "2.4.0",
"methodology": "1.0.0"
},
"files": {
Expand All @@ -3856,7 +3856,7 @@
"status": "prod",
"versions": {
"schema": "3.1.0",
"subgraph": "2.3.0",
"subgraph": "2.4.0",
"methodology": "1.0.0"
},
"files": {
Expand Down
54 changes: 19 additions & 35 deletions subgraphs/aave-forks/src/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1097,63 +1097,47 @@ export function _handleFlashLoan(
);
const tokenManager = new TokenManager(asset, event);
const amountUSD = tokenManager.getAmountUSD(amount);
const premiumUSDTotal = tokenManager.getAmountUSD(premiumAmount);
const flashloan = manager.createFlashloan(
asset,
account,
event.transaction.from,
amount,
amountUSD
);
const premiumUSDTotal = tokenManager.getAmountUSD(premiumAmount);
flashloan.feeAmount = premiumAmount;
flashloan.feeAmountUSD = premiumUSDTotal;
flashloan.save();

let reserveFactor = market.reserveFactor;
if (!reserveFactor) {
reserveFactor = BIGDECIMAL_ZERO;
}
const protocolRevenueShare = reserveFactor;
let premiumUSDToProtocol = premiumUSDTotal.times(protocolRevenueShare);
let premiumUSDToLP = premiumUSDTotal.minus(premiumUSDToProtocol);
const premiumRateTotal = flashloanPremium.premiumRateTotal;
let premiumRateToProtocol = premiumRateTotal.times(protocolRevenueShare);
let premiumRateToLP = premiumRateTotal.minus(premiumRateToProtocol);

let premiumUSDToProtocol = BIGDECIMAL_ZERO;
// according to https://github.com/aave/aave-v3-core/blob/29ff9b9f89af7cd8255231bc5faf26c3ce0fb7ce/contracts/interfaces/IPool.sol#L634
// premiumRateToProtocol is the percentage of premium to protocol
if (flashloanPremium.premiumRateToProtocol.gt(BIGDECIMAL_ZERO)) {
// according to https://github.com/aave/aave-v3-core/blob/29ff9b9f89af7cd8255231bc5faf26c3ce0fb7ce/contracts/interfaces/IPool.sol#L634
// premiumRateToProtocol is the percentage of premium to protocol
premiumUSDToProtocol = premiumUSDTotal
.times(flashloanPremium.premiumRateToProtocol)
.plus(premiumUSDToProtocol);
premiumRateToProtocol = premiumRateTotal
.times(flashloanPremium.premiumRateToProtocol)
.plus(premiumRateToProtocol);

// premium to LP
premiumUSDToLP = premiumUSDTotal.minus(premiumUSDToProtocol);
premiumRateToLP = premiumRateTotal.minus(premiumRateToProtocol);
// this part of the premium is transferred to the treasury and not
// accrued to liquidityIndex and thus no need to deduct
}

const feeToProtocol = manager.getOrUpdateFee(
FeeType.FLASHLOAN_PROTOCOL_FEE,
null,
premiumRateToProtocol
);

manager.addProtocolRevenue(premiumUSDToProtocol, feeToProtocol);
// premium to protocol = total premium * premiumRateToProtocol
premiumUSDToProtocol = premiumUSDTotal.times(
flashloanPremium.premiumRateToProtocol
);
const feeToProtocol = manager.getOrUpdateFee(
FeeType.FLASHLOAN_PROTOCOL_FEE,
null,
flashloanPremium.premiumRateToProtocol
);
manager.addProtocolRevenue(premiumUSDToProtocol, feeToProtocol);
}

// flashloan premium to LP is accrued in liquidityIndex and handled in
// _handleReserveDataUpdated;
// https://github.com/aave/aave-v3-core/blob/master/contracts/protocol/libraries/logic/FlashLoanLogic.sol#L233-L237
const premiumRateToLP = flashloanPremium.premiumRateTotal.minus(
flashloanPremium.premiumRateToProtocol
);
const feeToLP = manager.getOrUpdateFee(
FeeType.FLASHLOAN_LP_FEE,
null,
premiumRateToLP
);

const premiumUSDToLP = premiumUSDTotal.minus(premiumUSDToProtocol);
manager.addSupplyRevenue(premiumUSDToLP, feeToLP);
}

Expand Down

0 comments on commit b56a540

Please sign in to comment.