From 37f830f11e91a3a72196adb76a8e1fbd02426a1c Mon Sep 17 00:00:00 2001 From: Dhruv Chauhan Date: Thu, 9 Nov 2023 17:35:01 +0530 Subject: [PATCH 1/3] use strategyContract.totalShares for pool tvl --- deployment/deployment.json | 2 +- subgraphs/eigenlayer/schema.graphql | 1 + subgraphs/eigenlayer/src/common/events.ts | 1 + subgraphs/eigenlayer/src/common/metrics.ts | 9 +- subgraphs/eigenlayer/src/mappings/handlers.ts | 91 +++++++++++++++---- 5 files changed, 80 insertions(+), 24 deletions(-) diff --git a/deployment/deployment.json b/deployment/deployment.json index 54e2490d05..e8237662b3 100644 --- a/deployment/deployment.json +++ b/deployment/deployment.json @@ -9796,7 +9796,7 @@ "status": "prod", "versions": { "schema": "2.1.1", - "subgraph": "1.0.0", + "subgraph": "1.1.0", "methodology": "1.0.0" }, "files": { diff --git a/subgraphs/eigenlayer/schema.graphql b/subgraphs/eigenlayer/schema.graphql index 6af13e489a..508242df4f 100644 --- a/subgraphs/eigenlayer/schema.graphql +++ b/subgraphs/eigenlayer/schema.graphql @@ -771,6 +771,7 @@ type Withdraw implements Event @entity @transaction { " Block number of this event " blockNumber: BigInt! + blockNumberCompleted: BigInt " Timestamp of this event " timestamp: BigInt! diff --git a/subgraphs/eigenlayer/src/common/events.ts b/subgraphs/eigenlayer/src/common/events.ts index e8da7ef812..fdc8212ebe 100644 --- a/subgraphs/eigenlayer/src/common/events.ts +++ b/subgraphs/eigenlayer/src/common/events.ts @@ -137,6 +137,7 @@ export function updateWithdraw( ); withdrawEvent.hashCompleted = event.transaction.hash; withdrawEvent.completed = true; + withdrawEvent.blockNumberCompleted = event.block.number; withdrawEvent.save(); const account = getOrCreateAccount(accountAddress); diff --git a/subgraphs/eigenlayer/src/common/metrics.ts b/subgraphs/eigenlayer/src/common/metrics.ts index e857f8f526..c45edacb7a 100644 --- a/subgraphs/eigenlayer/src/common/metrics.ts +++ b/subgraphs/eigenlayer/src/common/metrics.ts @@ -26,19 +26,14 @@ export function updatePoolIsActive( export function updateTVL( poolAddress: Address, tokenAddress: Address, - isDeposit: boolean, - amount: BigInt, + balance: BigInt, event: ethereum.Event ): void { const protocol = getOrCreateProtocol(); const pool = getPool(poolAddress); const token = getOrCreateToken(tokenAddress, event); - if (isDeposit) { - pool.inputTokenBalances = [pool.inputTokenBalances[0].plus(amount)]; - } else { - pool.inputTokenBalances = [pool.inputTokenBalances[0].minus(amount)]; - } + pool.inputTokenBalances = [balance]; pool.inputTokenBalancesUSD = [ bigIntToBigDecimal(pool.inputTokenBalances[0]).times(token.lastPriceUSD!), ]; diff --git a/subgraphs/eigenlayer/src/mappings/handlers.ts b/subgraphs/eigenlayer/src/mappings/handlers.ts index ee693b3190..9474ccba17 100644 --- a/subgraphs/eigenlayer/src/mappings/handlers.ts +++ b/subgraphs/eigenlayer/src/mappings/handlers.ts @@ -4,6 +4,7 @@ import { BIGINT_ZERO, ETH_ADDRESS, INT_FOUR, + INT_ONE, INT_THREE, INT_TWO, INT_ZERO, @@ -163,12 +164,18 @@ export function handleDeposit(event: Deposit): void { const logTopicSignature = thisLog.topics.at(INT_ZERO); if (logTopicSignature.equals(TRANSFER_SIGNATURE)) { - const decoded = ethereum.decode(TRANSFER_DATA_TYPE, thisLog.data); - if (!decoded) continue; - - const logData = decoded.toTuple(); - amount = logData[INT_ZERO].toBigInt(); - break; + const logTopicTo = ethereum + .decode("address", thisLog.topics.at(INT_TWO))! + .toAddress(); + + if (logTopicTo.equals(Address.fromBytes(pool.id))) { + const decoded = ethereum.decode(TRANSFER_DATA_TYPE, thisLog.data); + if (!decoded) continue; + + const logData = decoded.toTuple(); + amount = logData[INT_ZERO].toBigInt(); + break; + } } } @@ -189,11 +196,34 @@ export function handleDeposit(event: Deposit): void { depositID, event ); + + let strategyContract = Strategy.bind(Address.fromBytes(pool.id)); + const totalSharesResult = strategyContract.try_totalShares(); + if (totalSharesResult.reverted) { + log.error( + "[handleDeposit] strategyContract.try_totalShares() reverted for strategy: {}", + [Address.fromBytes(pool.id).toHexString()] + ); + return; + } + let sharesToUnderlyingResult = strategyContract.try_sharesToUnderlying( + totalSharesResult.value + ); + if (sharesToUnderlyingResult.reverted) { + log.error( + "[handleDeposit] strategyContract.try_sharesToUnderlying() reverted for strategy: {} and value: {}", + [ + Address.fromBytes(pool.id).toHexString(), + totalSharesResult.value.toString(), + ] + ); + return; + } + updateTVL( Address.fromBytes(pool.id), Address.fromBytes(token.id), - true, - amount, + sharesToUnderlyingResult.value, event ); updateVolume( @@ -323,12 +353,18 @@ export function handleWithdrawalCompleted(event: WithdrawalCompleted): void { const logTopicSignature = thisLog.topics.at(INT_ZERO); if (logTopicSignature.equals(TRANSFER_SIGNATURE)) { - const decoded = ethereum.decode(TRANSFER_DATA_TYPE, thisLog.data); - if (!decoded) continue; - - const logData = decoded.toTuple(); - amount = logData[INT_ZERO].toBigInt(); - break; + const logTopicFrom = ethereum + .decode("address", thisLog.topics.at(INT_ONE))! + .toAddress(); + + if (logTopicFrom.equals(Address.fromBytes(poolID))) { + const decoded = ethereum.decode(TRANSFER_DATA_TYPE, thisLog.data); + if (!decoded) continue; + + const logData = decoded.toTuple(); + amount = logData[INT_ZERO].toBigInt(); + break; + } } } @@ -341,11 +377,34 @@ export function handleWithdrawalCompleted(event: WithdrawalCompleted): void { withdrawID, event ); + + let strategyContract = Strategy.bind(Address.fromBytes(poolID)); + const totalSharesResult = strategyContract.try_totalShares(); + if (totalSharesResult.reverted) { + log.error( + "[handleWithdrawalCompleted] strategyContract.try_totalShares() reverted for strategy: {}", + [Address.fromBytes(poolID).toHexString()] + ); + return; + } + let sharesToUnderlyingResult = strategyContract.try_sharesToUnderlying( + totalSharesResult.value + ); + if (sharesToUnderlyingResult.reverted) { + log.error( + "[handleWithdrawalCompleted] strategyContract.try_sharesToUnderlying() reverted for strategy: {} and value: {}", + [ + Address.fromBytes(poolID).toHexString(), + totalSharesResult.value.toString(), + ] + ); + return; + } + updateTVL( Address.fromBytes(poolID), Address.fromBytes(tokenID), - false, - amount, + sharesToUnderlyingResult.value, event ); updateVolume( From b58c35af554e4a93649b18de5097cf6c3e8df2f4 Mon Sep 17 00:00:00 2001 From: Dhruv Chauhan Date: Thu, 9 Nov 2023 17:45:04 +0530 Subject: [PATCH 2/3] fix lint --- subgraphs/eigenlayer/src/mappings/handlers.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/subgraphs/eigenlayer/src/mappings/handlers.ts b/subgraphs/eigenlayer/src/mappings/handlers.ts index 9474ccba17..5068b22187 100644 --- a/subgraphs/eigenlayer/src/mappings/handlers.ts +++ b/subgraphs/eigenlayer/src/mappings/handlers.ts @@ -197,7 +197,7 @@ export function handleDeposit(event: Deposit): void { event ); - let strategyContract = Strategy.bind(Address.fromBytes(pool.id)); + const strategyContract = Strategy.bind(Address.fromBytes(pool.id)); const totalSharesResult = strategyContract.try_totalShares(); if (totalSharesResult.reverted) { log.error( @@ -206,7 +206,7 @@ export function handleDeposit(event: Deposit): void { ); return; } - let sharesToUnderlyingResult = strategyContract.try_sharesToUnderlying( + const sharesToUnderlyingResult = strategyContract.try_sharesToUnderlying( totalSharesResult.value ); if (sharesToUnderlyingResult.reverted) { @@ -378,7 +378,7 @@ export function handleWithdrawalCompleted(event: WithdrawalCompleted): void { event ); - let strategyContract = Strategy.bind(Address.fromBytes(poolID)); + const strategyContract = Strategy.bind(Address.fromBytes(poolID)); const totalSharesResult = strategyContract.try_totalShares(); if (totalSharesResult.reverted) { log.error( @@ -387,7 +387,7 @@ export function handleWithdrawalCompleted(event: WithdrawalCompleted): void { ); return; } - let sharesToUnderlyingResult = strategyContract.try_sharesToUnderlying( + const sharesToUnderlyingResult = strategyContract.try_sharesToUnderlying( totalSharesResult.value ); if (sharesToUnderlyingResult.reverted) { From 8557dce368b6ee2d1d7b1c7d76f9832d2a69c4b6 Mon Sep 17 00:00:00 2001 From: Dhruv Chauhan Date: Tue, 21 Nov 2023 12:23:54 +0530 Subject: [PATCH 3/3] cleanup --- subgraphs/eigenlayer/src/common/getters.ts | 26 +++++++++- subgraphs/eigenlayer/src/mappings/handlers.ts | 51 ++----------------- 2 files changed, 30 insertions(+), 47 deletions(-) diff --git a/subgraphs/eigenlayer/src/common/getters.ts b/subgraphs/eigenlayer/src/common/getters.ts index 04a0eba8af..200fc5519b 100644 --- a/subgraphs/eigenlayer/src/common/getters.ts +++ b/subgraphs/eigenlayer/src/common/getters.ts @@ -1,4 +1,4 @@ -import { Address, Bytes, ethereum } from "@graphprotocol/graph-ts"; +import { Address, BigInt, Bytes, ethereum, log } from "@graphprotocol/graph-ts"; import { BIGDECIMAL_ZERO, @@ -30,6 +30,7 @@ import { UsageMetricsDailySnapshot, UsageMetricsHourlySnapshot, } from "../../generated/schema"; +import { Strategy } from "../../generated/StrategyManager/Strategy"; export function getOrCreateToken( tokenAddress: Address, @@ -375,3 +376,26 @@ export function getOrCreateActiveAccount(id: Bytes): ActiveAccount { } return account; } + +export function getPoolBalance(poolAddress: Address): BigInt { + const strategyContract = Strategy.bind(poolAddress); + const totalSharesResult = strategyContract.try_totalShares(); + if (totalSharesResult.reverted) { + log.error( + "[getPoolBalance] strategyContract.try_totalShares() reverted for strategy: {}", + [poolAddress.toHexString()] + ); + return BIGINT_ZERO; + } + const sharesToUnderlyingResult = strategyContract.try_sharesToUnderlying( + totalSharesResult.value + ); + if (sharesToUnderlyingResult.reverted) { + log.error( + "[getPoolBalance] strategyContract.try_sharesToUnderlying() reverted for strategy: {} and value: {}", + [poolAddress.toHexString(), totalSharesResult.value.toString()] + ); + return BIGINT_ZERO; + } + return sharesToUnderlyingResult.value; +} diff --git a/subgraphs/eigenlayer/src/mappings/handlers.ts b/subgraphs/eigenlayer/src/mappings/handlers.ts index 5068b22187..31511fbf42 100644 --- a/subgraphs/eigenlayer/src/mappings/handlers.ts +++ b/subgraphs/eigenlayer/src/mappings/handlers.ts @@ -20,6 +20,7 @@ import { getOrCreateAccount, getOrCreateToken, getPool, + getPoolBalance, } from "../common/getters"; import { updatePoolIsActive, @@ -197,33 +198,12 @@ export function handleDeposit(event: Deposit): void { event ); - const strategyContract = Strategy.bind(Address.fromBytes(pool.id)); - const totalSharesResult = strategyContract.try_totalShares(); - if (totalSharesResult.reverted) { - log.error( - "[handleDeposit] strategyContract.try_totalShares() reverted for strategy: {}", - [Address.fromBytes(pool.id).toHexString()] - ); - return; - } - const sharesToUnderlyingResult = strategyContract.try_sharesToUnderlying( - totalSharesResult.value - ); - if (sharesToUnderlyingResult.reverted) { - log.error( - "[handleDeposit] strategyContract.try_sharesToUnderlying() reverted for strategy: {} and value: {}", - [ - Address.fromBytes(pool.id).toHexString(), - totalSharesResult.value.toString(), - ] - ); - return; - } + const poolBalance = getPoolBalance(Address.fromBytes(pool.id)); updateTVL( Address.fromBytes(pool.id), Address.fromBytes(token.id), - sharesToUnderlyingResult.value, + poolBalance, event ); updateVolume( @@ -378,33 +358,12 @@ export function handleWithdrawalCompleted(event: WithdrawalCompleted): void { event ); - const strategyContract = Strategy.bind(Address.fromBytes(poolID)); - const totalSharesResult = strategyContract.try_totalShares(); - if (totalSharesResult.reverted) { - log.error( - "[handleWithdrawalCompleted] strategyContract.try_totalShares() reverted for strategy: {}", - [Address.fromBytes(poolID).toHexString()] - ); - return; - } - const sharesToUnderlyingResult = strategyContract.try_sharesToUnderlying( - totalSharesResult.value - ); - if (sharesToUnderlyingResult.reverted) { - log.error( - "[handleWithdrawalCompleted] strategyContract.try_sharesToUnderlying() reverted for strategy: {} and value: {}", - [ - Address.fromBytes(poolID).toHexString(), - totalSharesResult.value.toString(), - ] - ); - return; - } + const poolBalance = getPoolBalance(Address.fromBytes(poolID)); updateTVL( Address.fromBytes(poolID), Address.fromBytes(tokenID), - sharesToUnderlyingResult.value, + poolBalance, event ); updateVolume(