From b56a54015c605d3c27af976c0b137b9b34c037f3 Mon Sep 17 00:00:00 2001 From: Dylan Melotik <60583747+melotik@users.noreply.github.com> Date: Thu, 28 Mar 2024 10:43:21 -0700 Subject: [PATCH] fix(#minor); aave v3 forks; fix flashloan revennue (#2517) --- deployment/deployment.json | 28 +++++++-------- subgraphs/aave-forks/src/mapping.ts | 54 ++++++++++------------------- 2 files changed, 33 insertions(+), 49 deletions(-) diff --git a/deployment/deployment.json b/deployment/deployment.json index f3dc8fecc4..5dd5d1e728 100644 --- a/deployment/deployment.json +++ b/deployment/deployment.json @@ -336,7 +336,7 @@ "status": "prod", "versions": { "schema": "3.1.0", - "subgraph": "2.3.0", + "subgraph": "2.4.0", "methodology": "1.1.0" }, "files": { @@ -362,7 +362,7 @@ "status": "prod", "versions": { "schema": "3.1.0", - "subgraph": "2.3.0", + "subgraph": "2.4.0", "methodology": "1.1.0" }, "files": { @@ -388,7 +388,7 @@ "status": "prod", "versions": { "schema": "3.1.0", - "subgraph": "2.3.0", + "subgraph": "2.4.0", "methodology": "1.1.0" }, "files": { @@ -414,7 +414,7 @@ "status": "prod", "versions": { "schema": "3.1.0", - "subgraph": "2.3.0", + "subgraph": "2.4.0", "methodology": "1.1.0" }, "files": { @@ -440,7 +440,7 @@ "status": "prod", "versions": { "schema": "3.1.0", - "subgraph": "2.3.0", + "subgraph": "2.4.0", "methodology": "1.1.0" }, "files": { @@ -466,7 +466,7 @@ "status": "prod", "versions": { "schema": "3.1.0", - "subgraph": "2.3.0", + "subgraph": "2.4.0", "methodology": "1.1.0" }, "files": { @@ -492,7 +492,7 @@ "status": "dev", "versions": { "schema": "3.1.0", - "subgraph": "2.3.0", + "subgraph": "2.4.0", "methodology": "1.1.0" }, "files": { @@ -514,7 +514,7 @@ "status": "prod", "versions": { "schema": "3.1.0", - "subgraph": "2.3.0", + "subgraph": "2.4.0", "methodology": "1.1.0" }, "files": { @@ -540,7 +540,7 @@ "status": "prod", "versions": { "schema": "3.1.0", - "subgraph": "1.3.0", + "subgraph": "1.4.0", "methodology": "1.1.0" }, "files": { @@ -566,7 +566,7 @@ "status": "prod", "versions": { "schema": "3.1.0", - "subgraph": "1.2.0", + "subgraph": "1.3.0", "methodology": "1.0.0" }, "files": { @@ -592,7 +592,7 @@ "status": "prod", "versions": { "schema": "3.1.0", - "subgraph": "1.2.0", + "subgraph": "1.3.0", "methodology": "1.0.0" }, "files": { @@ -618,7 +618,7 @@ "status": "prod", "versions": { "schema": "3.1.0", - "subgraph": "1.2.0", + "subgraph": "1.3.0", "methodology": "1.0.0" }, "files": { @@ -3830,7 +3830,7 @@ "status": "prod", "versions": { "schema": "3.1.0", - "subgraph": "2.3.0", + "subgraph": "2.4.0", "methodology": "1.0.0" }, "files": { @@ -3856,7 +3856,7 @@ "status": "prod", "versions": { "schema": "3.1.0", - "subgraph": "2.3.0", + "subgraph": "2.4.0", "methodology": "1.0.0" }, "files": { diff --git a/subgraphs/aave-forks/src/mapping.ts b/subgraphs/aave-forks/src/mapping.ts index a461be761b..7e59df8c39 100644 --- a/subgraphs/aave-forks/src/mapping.ts +++ b/subgraphs/aave-forks/src/mapping.ts @@ -1097,7 +1097,6 @@ 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, @@ -1105,55 +1104,40 @@ export function _handleFlashLoan( 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); }