diff --git a/ethereum.subgraph.template.yaml b/ethereum.subgraph.template.yaml index da7673d..61e3d61 100644 --- a/ethereum.subgraph.template.yaml +++ b/ethereum.subgraph.template.yaml @@ -328,10 +328,8 @@ templates: - StableDebtToken - UserReserve abis: - # - name: StableDebtToken - # file: ./externals/protocol-v2/artifacts/contracts/protocol/tokenization/StableDebtToken.sol/StableDebtToken.json - name: StableDebtToken - file: ./constant-abis/StableDebtToken.json + file: ./externals/protocol-v2/artifacts/contracts/protocol/tokenization/StableDebtToken.sol/StableDebtToken.json - name: IERC20Detailed file: ./externals/protocol-v2/artifacts/contracts/dependencies/openzeppelin/contracts/IERC20Detailed.sol/IERC20Detailed.json - name: AaveIncentivesController @@ -359,10 +357,8 @@ templates: - StableDebtToken - UserReserve abis: - # - name: VariableDebtToken - # file: ./externals/protocol-v2/artifacts/contracts/protocol/tokenization/VariableDebtToken.sol/VariableDebtToken.json - name: VariableDebtToken - file: ./constant-abis/VariableDebtToken.json + file: ./externals/protocol-v2/artifacts/contracts/protocol/tokenization/VariableDebtToken.sol/VariableDebtToken.json - name: IERC20Detailed file: ./externals/protocol-v2/artifacts/contracts/dependencies/openzeppelin/contracts/IERC20Detailed.sol/IERC20Detailed.json - name: AaveIncentivesController diff --git a/externals/protocol-v2 b/externals/protocol-v2 index 2708551..53b2723 160000 --- a/externals/protocol-v2 +++ b/externals/protocol-v2 @@ -1 +1 @@ -Subproject commit 2708551bcf3afb28ee9798ccf7f3027ea0ecec10 +Subproject commit 53b27235ad2e1501854adcde72bf3b835bbe4def diff --git a/schema.graphql b/schema.graphql index 9e7a99e..48e2d4b 100644 --- a/schema.graphql +++ b/schema.graphql @@ -616,27 +616,31 @@ type STokenBalanceHistoryItem @entity { # #originationFee: BigInt! # timestamp: Int! #} - -type StableTokenDelegatedAllowance @entity { +type CreditDelegationHistoryItem @entity { """ - stable + fromuser address + touser address+ reserve address + delegationId + txHash """ id: ID! - fromUser: User! - toUser: User! amountAllowed: BigInt! userReserve: UserReserve! + timestamp: Int! + txHash: String! + creditDelegationAllowance: CreditDelegationAllowance! } -type VariableTokenDelegatedAllowance @entity { +type CreditDelegationAllowance @entity { """ - variable + fromuser address + touser address+ reserve address + rateMode + fromuser address + touser address+ reserve address """ id: ID! + rateMode: String! fromUser: User! toUser: User! amountAllowed: BigInt! userReserve: UserReserve! + timestamp: Int! + txHash: String! + history: [CreditDelegationHistoryItem!]! @derivedFrom(field: "creditDelegationAllowance") } type UserReserve @entity { @@ -672,10 +676,7 @@ type UserReserve @entity { Amount in currency units included as fee """ lastUpdateTimestamp: Int! - stableTokenDelegatedAllowances: [StableTokenDelegatedAllowance!]! - @derivedFrom(field: "userReserve") - variableTokenDelegatedAllowances: [VariableTokenDelegatedAllowance!]! - @derivedFrom(field: "userReserve") + creditDelegationAllowances: [CreditDelegationAllowance!]! @derivedFrom(field: "userReserve") aTokenBalanceHistory: [ATokenBalanceHistoryItem!]! @derivedFrom(field: "userReserve") vTokenBalanceHistory: [VTokenBalanceHistoryItem!]! @derivedFrom(field: "userReserve") sTokenBalanceHistory: [STokenBalanceHistoryItem!]! @derivedFrom(field: "userReserve") diff --git a/src/helpers/initializers.ts b/src/helpers/initializers.ts index 1258778..e81f619 100644 --- a/src/helpers/initializers.ts +++ b/src/helpers/initializers.ts @@ -15,6 +15,7 @@ import { ContractToPoolMapping, Protocol, ChainlinkENS, + CreditDelegationAllowance, } from '../../generated/schema'; import { PRICE_ORACLE_ASSET_PLATFORM_SIMPLE, @@ -388,3 +389,21 @@ export function createMapContractToPool(_contractAddress: Address, pool: string) contractToPoolMapping.pool = pool; contractToPoolMapping.save(); } + +export function getOrInitCreditDelegation( + delegatedAllowanceId: string, + fromUser: User, + toUser: User, + rateMode: string, + userReserve: UserReserve +): CreditDelegationAllowance { + let delegatedAllowance = CreditDelegationAllowance.load(delegatedAllowanceId); + if (delegatedAllowance == null) { + delegatedAllowance = new CreditDelegationAllowance(delegatedAllowanceId); + delegatedAllowance.fromUser = fromUser.id; + delegatedAllowance.toUser = toUser.id; + delegatedAllowance.rateMode = rateMode; + delegatedAllowance.userReserve = userReserve.id; + } + return delegatedAllowance as CreditDelegationAllowance; +} diff --git a/src/mapping/tokenization/tokenization.ts b/src/mapping/tokenization/tokenization.ts index 87caa15..7993401 100644 --- a/src/mapping/tokenization/tokenization.ts +++ b/src/mapping/tokenization/tokenization.ts @@ -19,8 +19,8 @@ import { STokenBalanceHistoryItem, UserReserve, Reserve, - StableTokenDelegatedAllowance, - VariableTokenDelegatedAllowance, + CreditDelegationAllowance, + CreditDelegationHistoryItem, } from '../../../generated/schema'; import { getOrInitAToken, @@ -32,6 +32,7 @@ import { getPriceOracleAsset, getOrInitPriceOracle, getOrInitReserveParamsHistoryItem, + getOrInitCreditDelegation, } from '../../helpers/initializers'; import { zeroBI } from '../../utils/converters'; import { calculateUtilizationRate } from '../../helpers/reserve-logic'; @@ -463,46 +464,75 @@ export function handleStableTokenBurn(event: STokenBurn): void { saveUserReserveSHistory(userReserve, event, event.params.avgStableRate); } +function saveCreditDelegationHistory( + delegation: CreditDelegationAllowance, + event: ethereum.Event +): void { + let historyItem = new CreditDelegationHistoryItem( + delegation.id + event.transaction.hash.toHexString() + ); + historyItem.amountAllowed = delegation.amountAllowed; + historyItem.userReserve = delegation.userReserve; + historyItem.timestamp = delegation.timestamp; + historyItem.txHash = event.transaction.hash.toHexString(); + historyItem.creditDelegationAllowance = delegation.id; + historyItem.save(); +} + export function handleStableTokenBorrowAllowanceDelegated(event: SBorrowAllowanceDelegated): void { - let fromUser = event.params.fromUser; - let toUser = event.params.toUser; + let fromUser = getOrInitUser(event.params.fromUser); + let toUser = getOrInitUser(event.params.toUser); let asset = event.params.asset; let amount = event.params.amount; - - let userReserve = getOrInitUserReserve(fromUser, asset, event); - + let userReserve = getOrInitUserReserve(event.params.fromUser, asset, event); let delegatedAllowanceId = - 'stable' + fromUser.toHexString() + toUser.toHexString() + asset.toHexString(); - let delegatedAllowance = StableTokenDelegatedAllowance.load(delegatedAllowanceId); - if (delegatedAllowance == null) { - delegatedAllowance = new StableTokenDelegatedAllowance(delegatedAllowanceId); - delegatedAllowance.fromUser = fromUser.toHexString(); - delegatedAllowance.toUser = toUser.toHexString(); - delegatedAllowance.userReserve = userReserve.id; - } + 'stable' + + event.params.fromUser.toHexString() + + event.params.toUser.toHexString() + + asset.toHexString(); + let delegatedAllowance = getOrInitCreditDelegation( + delegatedAllowanceId, + fromUser, + toUser, + 'Stable', + userReserve + ); + delegatedAllowance.timestamp = event.block.timestamp.toI32(); + delegatedAllowance.txHash = event.transaction.hash.toHexString(); delegatedAllowance.amountAllowed = amount; + userReserve.save(); + fromUser.save(); + toUser.save(); delegatedAllowance.save(); + saveCreditDelegationHistory(delegatedAllowance, event); } export function handleVariableTokenBorrowAllowanceDelegated( event: VBorrowAllowanceDelegated ): void { - let fromUser = event.params.fromUser; - let toUser = event.params.toUser; + let fromUser = getOrInitUser(event.params.fromUser); + let toUser = getOrInitUser(event.params.toUser); let asset = event.params.asset; let amount = event.params.amount; - - let userReserve = getOrInitUserReserve(fromUser, asset, event); - + let userReserve = getOrInitUserReserve(event.params.fromUser, asset, event); let delegatedAllowanceId = - 'variable' + fromUser.toHexString() + toUser.toHexString() + asset.toHexString(); - let delegatedAllowance = VariableTokenDelegatedAllowance.load(delegatedAllowanceId); - if (delegatedAllowance == null) { - delegatedAllowance = new VariableTokenDelegatedAllowance(delegatedAllowanceId); - delegatedAllowance.fromUser = fromUser.toHexString(); - delegatedAllowance.toUser = toUser.toHexString(); - delegatedAllowance.userReserve = userReserve.id; - } + 'variable' + + event.params.fromUser.toHexString() + + event.params.toUser.toHexString() + + asset.toHexString(); + let delegatedAllowance = getOrInitCreditDelegation( + delegatedAllowanceId, + fromUser, + toUser, + 'Variable', + userReserve + ); + delegatedAllowance.timestamp = event.block.timestamp.toI32(); + delegatedAllowance.txHash = event.transaction.hash.toHexString(); delegatedAllowance.amountAllowed = amount; + userReserve.save(); + fromUser.save(); + toUser.save(); delegatedAllowance.save(); + saveCreditDelegationHistory(delegatedAllowance, event); }