From 1a7f07d172de4423930e77856bb4b1670dce75ec Mon Sep 17 00:00:00 2001 From: Andrew Schmidt Date: Wed, 9 Jun 2021 13:49:59 -0500 Subject: [PATCH 1/4] fix: update debt token abis --- ethereum.subgraph.template.yaml | 8 ++------ externals/protocol-v2 | 2 +- 2 files changed, 3 insertions(+), 7 deletions(-) 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 From a92148a430bcfbb947c2baad07819cb381573e9c Mon Sep 17 00:00:00 2001 From: Andrew Schmidt Date: Thu, 10 Jun 2021 09:55:36 -0500 Subject: [PATCH 2/4] fix: combine credit delegation into single entity --- schema.graphql | 21 ++++----------------- src/mapping/tokenization/tokenization.ts | 13 +++++++------ 2 files changed, 11 insertions(+), 23 deletions(-) diff --git a/schema.graphql b/schema.graphql index 9e7a99e..ccd6ebb 100644 --- a/schema.graphql +++ b/schema.graphql @@ -617,22 +617,12 @@ type STokenBalanceHistoryItem @entity { # timestamp: Int! #} -type StableTokenDelegatedAllowance @entity { +type CreditDelegationAllowance @entity { """ - stable + fromuser address + touser address+ reserve address - """ - id: ID! - fromUser: User! - toUser: User! - amountAllowed: BigInt! - userReserve: UserReserve! -} - -type VariableTokenDelegatedAllowance @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! @@ -672,10 +662,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/mapping/tokenization/tokenization.ts b/src/mapping/tokenization/tokenization.ts index 83f199f..6197ecf 100644 --- a/src/mapping/tokenization/tokenization.ts +++ b/src/mapping/tokenization/tokenization.ts @@ -19,8 +19,7 @@ import { STokenBalanceHistoryItem, UserReserve, Reserve, - StableTokenDelegatedAllowance, - VariableTokenDelegatedAllowance, + CreditDelegationAllowance, } from '../../../generated/schema'; import { getOrInitAToken, @@ -470,11 +469,12 @@ export function handleStableTokenBorrowAllowanceDelegated(event: SBorrowAllowanc let delegatedAllowanceId = 'stable' + fromUser.toHexString() + toUser.toHexString() + asset.toHexString(); - let delegatedAllowance = StableTokenDelegatedAllowance.load(delegatedAllowanceId); + let delegatedAllowance = CreditDelegationAllowance.load(delegatedAllowanceId); if (delegatedAllowance == null) { - delegatedAllowance = new StableTokenDelegatedAllowance(delegatedAllowanceId); + delegatedAllowance = new CreditDelegationAllowance(delegatedAllowanceId); delegatedAllowance.fromUser = fromUser.toHexString(); delegatedAllowance.toUser = toUser.toHexString(); + delegatedAllowance.rateMode = 'Stable'; delegatedAllowance.userReserve = userReserve.id; } delegatedAllowance.amountAllowed = amount; @@ -493,11 +493,12 @@ export function handleVariableTokenBorrowAllowanceDelegated( let delegatedAllowanceId = 'variable' + fromUser.toHexString() + toUser.toHexString() + asset.toHexString(); - let delegatedAllowance = VariableTokenDelegatedAllowance.load(delegatedAllowanceId); + let delegatedAllowance = CreditDelegationAllowance.load(delegatedAllowanceId); if (delegatedAllowance == null) { - delegatedAllowance = new VariableTokenDelegatedAllowance(delegatedAllowanceId); + delegatedAllowance = new CreditDelegationAllowance(delegatedAllowanceId); delegatedAllowance.fromUser = fromUser.toHexString(); delegatedAllowance.toUser = toUser.toHexString(); + delegatedAllowance.rateMode = 'Variable'; delegatedAllowance.userReserve = userReserve.id; } delegatedAllowance.amountAllowed = amount; From 9a24bb471b5192237563de372613018338eca4f8 Mon Sep 17 00:00:00 2001 From: Andrew Schmidt Date: Wed, 30 Jun 2021 11:45:55 -0500 Subject: [PATCH 3/4] feat: Credit delegation history --- schema.graphql | 17 +++++ src/helpers/initializers.ts | 19 +++++ src/mapping/tokenization/tokenization.ts | 88 ++++++++++++++++-------- 3 files changed, 96 insertions(+), 28 deletions(-) diff --git a/schema.graphql b/schema.graphql index ccd6ebb..f3417dc 100644 --- a/schema.graphql +++ b/schema.graphql @@ -616,6 +616,20 @@ type STokenBalanceHistoryItem @entity { # #originationFee: BigInt! # timestamp: Int! #} +type CreditDelegationHistoryItem @entity { + """ + delegationId + txHash + """ + id: ID! + rateMode: String! + fromUser: User! + toUser: User! + amountAllowed: BigInt! + userReserve: UserReserve! + timestamp: Int! + txHash: String! + allowance: CreditDelegationAllowance! +} type CreditDelegationAllowance @entity { """ @@ -627,6 +641,9 @@ type CreditDelegationAllowance @entity { toUser: User! amountAllowed: BigInt! userReserve: UserReserve! + timestamp: Int! + txHash: String! + history: [CreditDelegationHistoryItem!]! } type UserReserve @entity { 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 6197ecf..a40543b 100644 --- a/src/mapping/tokenization/tokenization.ts +++ b/src/mapping/tokenization/tokenization.ts @@ -20,6 +20,7 @@ import { UserReserve, Reserve, CreditDelegationAllowance, + CreditDelegationHistoryItem, } from '../../../generated/schema'; import { getOrInitAToken, @@ -31,6 +32,7 @@ import { getPriceOracleAsset, getOrInitPriceOracle, getOrInitReserveParamsHistoryItem, + getOrInitCreditDelegation, } from '../../helpers/initializers'; import { zeroBI } from '../../utils/converters'; import { calculateUtilizationRate } from '../../helpers/reserve-logic'; @@ -459,48 +461,78 @@ 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.rateMode = delegation.rateMode; + historyItem.toUser = delegation.toUser; + historyItem.fromUser = delegation.fromUser; + historyItem.amountAllowed = delegation.amountAllowed; + historyItem.userReserve = delegation.userReserve; + historyItem.timestamp = delegation.timestamp; + historyItem.txHash = event.transaction.hash.toHexString(); + historyItem.allowance = 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 = CreditDelegationAllowance.load(delegatedAllowanceId); - if (delegatedAllowance == null) { - delegatedAllowance = new CreditDelegationAllowance(delegatedAllowanceId); - delegatedAllowance.fromUser = fromUser.toHexString(); - delegatedAllowance.toUser = toUser.toHexString(); - delegatedAllowance.rateMode = 'Stable'; - 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; + saveCreditDelegationHistory(delegatedAllowance, event); + userReserve.save(); + fromUser.save(); + toUser.save(); delegatedAllowance.save(); } 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 = CreditDelegationAllowance.load(delegatedAllowanceId); - if (delegatedAllowance == null) { - delegatedAllowance = new CreditDelegationAllowance(delegatedAllowanceId); - delegatedAllowance.fromUser = fromUser.toHexString(); - delegatedAllowance.toUser = toUser.toHexString(); - delegatedAllowance.rateMode = 'Variable'; - 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; + saveCreditDelegationHistory(delegatedAllowance, event); + userReserve.save(); + fromUser.save(); + toUser.save(); delegatedAllowance.save(); } From e7a844f1d616ae3f15711029e9c184fa845a9628 Mon Sep 17 00:00:00 2001 From: Andrew Schmidt Date: Thu, 1 Jul 2021 08:33:28 -0500 Subject: [PATCH 4/4] fix: credit delegation history issue --- schema.graphql | 7 ++----- src/mapping/tokenization/tokenization.ts | 9 +++------ 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/schema.graphql b/schema.graphql index f3417dc..48e2d4b 100644 --- a/schema.graphql +++ b/schema.graphql @@ -621,14 +621,11 @@ type CreditDelegationHistoryItem @entity { delegationId + txHash """ id: ID! - rateMode: String! - fromUser: User! - toUser: User! amountAllowed: BigInt! userReserve: UserReserve! timestamp: Int! txHash: String! - allowance: CreditDelegationAllowance! + creditDelegationAllowance: CreditDelegationAllowance! } type CreditDelegationAllowance @entity { @@ -643,7 +640,7 @@ type CreditDelegationAllowance @entity { userReserve: UserReserve! timestamp: Int! txHash: String! - history: [CreditDelegationHistoryItem!]! + history: [CreditDelegationHistoryItem!]! @derivedFrom(field: "creditDelegationAllowance") } type UserReserve @entity { diff --git a/src/mapping/tokenization/tokenization.ts b/src/mapping/tokenization/tokenization.ts index 799a711..7993401 100644 --- a/src/mapping/tokenization/tokenization.ts +++ b/src/mapping/tokenization/tokenization.ts @@ -471,14 +471,11 @@ function saveCreditDelegationHistory( let historyItem = new CreditDelegationHistoryItem( delegation.id + event.transaction.hash.toHexString() ); - historyItem.rateMode = delegation.rateMode; - historyItem.toUser = delegation.toUser; - historyItem.fromUser = delegation.fromUser; historyItem.amountAllowed = delegation.amountAllowed; historyItem.userReserve = delegation.userReserve; historyItem.timestamp = delegation.timestamp; historyItem.txHash = event.transaction.hash.toHexString(); - historyItem.allowance = delegation.id; + historyItem.creditDelegationAllowance = delegation.id; historyItem.save(); } @@ -503,11 +500,11 @@ export function handleStableTokenBorrowAllowanceDelegated(event: SBorrowAllowanc delegatedAllowance.timestamp = event.block.timestamp.toI32(); delegatedAllowance.txHash = event.transaction.hash.toHexString(); delegatedAllowance.amountAllowed = amount; - saveCreditDelegationHistory(delegatedAllowance, event); userReserve.save(); fromUser.save(); toUser.save(); delegatedAllowance.save(); + saveCreditDelegationHistory(delegatedAllowance, event); } export function handleVariableTokenBorrowAllowanceDelegated( @@ -533,9 +530,9 @@ export function handleVariableTokenBorrowAllowanceDelegated( delegatedAllowance.timestamp = event.block.timestamp.toI32(); delegatedAllowance.txHash = event.transaction.hash.toHexString(); delegatedAllowance.amountAllowed = amount; - saveCreditDelegationHistory(delegatedAllowance, event); userReserve.save(); fromUser.save(); toUser.save(); delegatedAllowance.save(); + saveCreditDelegationHistory(delegatedAllowance, event); }