Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/credit delegation #30

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions ethereum.subgraph.template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion externals/protocol-v2
Submodule protocol-v2 updated 200 files
23 changes: 12 additions & 11 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
Expand Down
19 changes: 19 additions & 0 deletions src/helpers/initializers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
ContractToPoolMapping,
Protocol,
ChainlinkENS,
CreditDelegationAllowance,
} from '../../generated/schema';
import {
PRICE_ORACLE_ASSET_PLATFORM_SIMPLE,
Expand Down Expand Up @@ -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;
}
86 changes: 58 additions & 28 deletions src/mapping/tokenization/tokenization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import {
STokenBalanceHistoryItem,
UserReserve,
Reserve,
StableTokenDelegatedAllowance,
VariableTokenDelegatedAllowance,
CreditDelegationAllowance,
CreditDelegationHistoryItem,
} from '../../../generated/schema';
import {
getOrInitAToken,
Expand All @@ -32,6 +32,7 @@ import {
getPriceOracleAsset,
getOrInitPriceOracle,
getOrInitReserveParamsHistoryItem,
getOrInitCreditDelegation,
} from '../../helpers/initializers';
import { zeroBI } from '../../utils/converters';
import { calculateUtilizationRate } from '../../helpers/reserve-logic';
Expand Down Expand Up @@ -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);
}