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

Add Collateral Manager V2 to Subgraph #136

Open
wants to merge 9 commits into
base: feature/collateral-manager-v2
Choose a base branch
from
2 changes: 1 addition & 1 deletion packages/subgraph/config/sepolia.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"enabled":true,"name":"tellerv2-sepolia","network":"sepolia","export_network_name":"sepolia","product":"studio","studio":{"owner":"0x1a76339211668a6939e1d6D13AB902bBef5D9ebc","network":"arbitrum-one"},"grafting":{"enabled":true,"base":"QmPska7bbF27sYxpp9v5Kw7wcJ3E4vGMFFx1mQWGTDVtgJ","block":4383095},"block_handler":{"enabled":true,"block":"4342453"},"contracts":{"teller_v2":{"enabled":true,"address":"0x50AF6e0A54F663467F8e3B6e76Ae60329c4c26Ef","block":"4342453"},"market_registry":{"enabled":true,"address":"0x6213d6444C871869C7c8eFBbfC6101a202A08E68","block":"4342460"},"lender_commitment":{"enabled":true,"address":"0x7FA4D022dF7f1ad9Ac6eF7e22578353C69502692","block":"4342462"},"lender_commitment_staging":{"enabled":true,"address":"0xd23F21685955e2d2637eEAAF6e870d1DB65ac2b7","block":"4342465"},"collateral_manager":{"enabled":true,"address":"0xC52A89eB4Ec84D777f9b6BcFd71F1C5Ce5033d08","block":"4342454"},"lender_manager":{"enabled":true,"address":"0x32436A77A36D0786E01fE850D4E6EA2Fa3C51a12","block":"4342471"},"market_liquidity_rewards":{"enabled":true,"address":"0x82B9a334867ee3d2022225faD083faC39B133128","block":"4342479"}}}
{"enabled":true,"name":"tellerv2-sepolia","network":"sepolia","export_network_name":"sepolia","product":"studio","studio":{"owner":"0x1a76339211668a6939e1d6D13AB902bBef5D9ebc","network":"arbitrum-one"},"grafting":{"enabled":true,"base":"QmawxxeN4j4f2rCAUXzED6MpSBNxF7rqZbDEm4HcPzY9ib","block":4464986},"block_handler":{"enabled":true,"block":"4342453"},"contracts":{"teller_v2":{"enabled":true,"address":"0x50AF6e0A54F663467F8e3B6e76Ae60329c4c26Ef","block":"4342453"},"market_registry":{"enabled":true,"address":"0x6213d6444C871869C7c8eFBbfC6101a202A08E68","block":"4342460"},"lender_commitment":{"enabled":true,"address":"0x7FA4D022dF7f1ad9Ac6eF7e22578353C69502692","block":"4342462"},"lender_commitment_staging":{"enabled":true,"address":"0xd23F21685955e2d2637eEAAF6e870d1DB65ac2b7","block":"4342465"},"collateral_manager":{"enabled":true,"address":"0xC52A89eB4Ec84D777f9b6BcFd71F1C5Ce5033d08","block":"4342454"},"collateral_manager_v2":{"enabled":true,"address":"0x82DC1036bF9E1078D9B38b2C7e2ebFBaEd0e7eD5","block":"4423473"},"lender_manager":{"enabled":true,"address":"0x32436A77A36D0786E01fE850D4E6EA2Fa3C51a12","block":"4342471"},"market_liquidity_rewards":{"enabled":true,"address":"0x82B9a334867ee3d2022225faD083faC39B133128","block":"4342479"}}}
2 changes: 1 addition & 1 deletion packages/subgraph/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@teller-protocol/v2-subgraph",
"license": "UNLICENSED",
"version": "0.4.21-4",
"version": "0.4.21-8",
"scripts": {
"deploy:prompt": "ts-node scripts/thegraph",
"create-local": "graph create --node http://localhost:8020/ teller-v2",
Expand Down
1 change: 1 addition & 0 deletions packages/subgraph/scripts/thegraph/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export interface INetworkConfig {
lender_commitment: INetworkContractConfig;
lender_commitment_staging: INetworkContractConfig;
collateral_manager: INetworkContractConfig;
collateral_manager_v2: INetworkContractConfig;
lender_manager: INetworkContractConfig;
market_liquidity_rewards: INetworkContractConfig;
};
Expand Down
113 changes: 0 additions & 113 deletions packages/subgraph/src/collateral-manager-v2/mapping.ts

This file was deleted.

32 changes: 0 additions & 32 deletions packages/subgraph/src/collateral-manager-v2/updaters.ts

This file was deleted.

20 changes: 18 additions & 2 deletions packages/subgraph/src/collateral-manager/mapping.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { store } from "@graphprotocol/graph-ts";

import {
CollateralClaimed,
CollateralCommitted,
Expand All @@ -7,9 +9,9 @@ import {
CollateralWithdrawn
} from "../../generated/CollateralManager/CollateralManager";
import { TellerV2 } from "../../generated/CollateralManager/TellerV2";
import { Bid } from "../../generated/schema";
import { Bid, BidCollateral } from "../../generated/schema";
import { updateCollateral } from "../collateral-manager/updaters";
import { BidStatus, bidStatusToEnum, isBidDefaulted } from "../helpers/bid";
import { BidStatus } from "../helpers/bid";
import { loadBidById, loadCollateral } from "../helpers/loaders";
import { updateBidStatus } from "../helpers/updaters";

Expand Down Expand Up @@ -108,6 +110,10 @@ export function handleCollateralWithdrawns(

/**
* Sets the bid status to `Liquidated` when the collateral is claimed from a defaulted loan.
* This was necessary for a small subset of loans that were liquidated before the
* we emitted the `LoanLiquidated` event.
*
* @deprecated CM V2 only emits CollateralWithdrawn events per collateral, so this is no longer needed.
* @param event
*/
export function handleCollateralClaimed(event: CollateralClaimed): void {
Expand All @@ -121,6 +127,16 @@ export function handleCollateralClaimed(event: CollateralClaimed): void {
if (tellerV2.getBidState(bid.bidId) !== BidStatus.Repaid) {
updateBidStatus(bid, BidStatus.Liquidated);
}

// Ensure the collateral status is set to `Withdrawn`
const bidCollaterals = changetype<BidCollateral[]>(
store.loadRelated("Bid", bid.id, "collateral")
);
for (let i = 0; i < bidCollaterals.length; i++) {
const collateral = bidCollaterals[i];
collateral.status = "Withdrawn";
collateral.save();
}
}

export function handleCollateralClaimeds(events: CollateralClaimed[]): void {
Expand Down
6 changes: 6 additions & 0 deletions packages/subgraph/src/collateral-manager/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { dataSource } from "@graphprotocol/graph-ts";

export function isV2(): boolean {
const ctx = dataSource.context();
return !!ctx.isSet("isV2") && ctx.getBoolean("isV2");
}
12 changes: 2 additions & 10 deletions packages/subgraph/src/helpers/loaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
TellerV2__bidsResultLoanDetailsStruct,
TellerV2__bidsResultTermsStruct
} from "../../generated/TellerV2/TellerV2";
import { isV2 } from "../collateral-manager/utils";

import { initTokenVolume } from "./intializers";

Expand Down Expand Up @@ -518,17 +519,8 @@ export function loadCollateral(
collateral.status = "";
collateral.receiver = Address.zero();
collateral.bid = bidId;
collateral.cmV2 = isV2();
collateral.save();

const bid = Bid.load(bidId)!;
let bidCollaterals = bid.collateral;
if (!bidCollaterals) {
bidCollaterals = [collateral.id];
} else {
bidCollaterals.push(collateral.id);
}
bid.collateral = bidCollaterals;
bid.save();
}
return collateral;
}
11 changes: 7 additions & 4 deletions packages/subgraph/src/helpers/updaters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
BigInt,
Entity,
ethereum,
store,
Value
} from "@graphprotocol/graph-ts";

Expand Down Expand Up @@ -124,10 +125,12 @@ export function getTokenVolumesForBid(bidId: string): TokenVolume[] {
}

const volumeCount = tokenVolumes.length;
const collateralIds = bid.collateral;
if (collateralIds) {
for (let j = 0; j < collateralIds.length; j++) {
const collateral = BidCollateral.load(collateralIds[j])!;
const bidCollaterals = changetype<BidCollateral[]>(
store.loadRelated("Bid", bidId, "collateral")
);
if (bidCollaterals) {
for (let j = 0; j < bidCollaterals.length; j++) {
const collateral = bidCollaterals[j];
const collateralToken = Token.load(collateral.token)!;
for (let i = 0; i < volumeCount; i++) {
const collateralTokenVolume = loadCollateralTokenVolume(
Expand Down
6 changes: 4 additions & 2 deletions packages/subgraph/src/liquidity-rewards/updaters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,11 +393,13 @@ function bidIsEligibleForReward(

let hasValidCollateral = false;

const bidCollaterals = bid.collateral;
const bidCollaterals = changetype<BidCollateral[]>(
store.loadRelated("Bid", bid.id, "collateral")
);

if (bidCollaterals) {
for (let i = 0; i < bidCollaterals.length; i++) {
const bidCollateral = BidCollateral.load(bidCollaterals[i])!;
const bidCollateral = bidCollaterals[i];

const principalToken = loadToken(bid.lendingTokenAddress);
let principalTokenDecimals = principalToken.decimals;
Expand Down
3 changes: 2 additions & 1 deletion packages/subgraph/src/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ type Bid @entity {
_lastTotalRepaidInterestAmount: BigInt!
paymentDefaultDuration: BigInt!
collateralEscrow: Bytes
collateral: [BidCollateral!]
collateral: [BidCollateral!]! @derivedFrom(field: "bid")
}

type BidCollateral @entity {
Expand All @@ -111,6 +111,7 @@ type BidCollateral @entity {
bid: Bid!
status: String
receiver: Bytes
cmV2: Boolean
}

type Token @entity {
Expand Down
43 changes: 43 additions & 0 deletions packages/subgraph/src/subgraph.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ dataSources:
handler: handleSetMarketPaymentType
file: ./src/market-registry/mapping.ts
{{/if}}

{{#if contracts.lender_commitment.enabled}}
- kind: ethereum/contract
name: LenderCommitmentForwarder
Expand Down Expand Up @@ -341,6 +342,7 @@ dataSources:
handler: handleUpdatedCommitmentBorrower
file: ./src/lender-commitment/mapping.ts
{{/if}}

{{#if contracts.collateral_manager.enabled}}
- kind: ethereum/contract
name: CollateralManager
Expand Down Expand Up @@ -381,6 +383,47 @@ dataSources:
handler: handleCollateralClaimed
file: ./src/collateral-manager/mapping.ts
{{/if}}
{{#if contracts.collateral_manager_v2.enabled}}
- kind: ethereum/contract
name: CollateralManagerV2
network: {{network}}
context:
isV2:
type: Bool
data: true
source:
{{#with contracts.collateral_manager_v2}}
address: '{{address}}'
abi: CollateralManagerV2
startBlock: {{block}}
{{/with}}
mapping:
kind: ethereum/events
apiVersion: 0.0.7
language: wasm/assemblyscript
entities:
- Bid
- BidCollateral
- Collateral
abis:
- name: CollateralManagerV2
file: ./abis/{{export_network_name}}_CollateralManagerV2.json
- name: TellerV2
file: ./abis/{{export_network_name}}_TellerV2.json
- name: ERC165
file: ../contracts/generated/artifacts/@openzeppelin/contracts/utils/introspection/ERC165.sol/ERC165.json
- name: IERC20Metadata
file: ../contracts/generated/artifacts/@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol/IERC20Metadata.json
eventHandlers:
- event: CollateralCommitted(uint256,uint8,address,uint256,uint256)
handler: handleCollateralCommitted
- event: CollateralDeposited(uint256,uint8,address,uint256,uint256)
handler: handleCollateralDeposited
- event: CollateralWithdrawn(uint256,uint8,address,uint256,uint256,address)
handler: handleCollateralWithdrawn
file: ./src/collateral-manager/mapping.ts
{{/if}}

{{#if contracts.market_liquidity_rewards.enabled}}
- kind: ethereum/contract
name: MarketLiquidityRewards
Expand Down