Skip to content

Commit a4a6b88

Browse files
Merge pull request #248 from teller-protocol/subgraph/og-subgraph-liquidation-data
og subgraph liquidation data improvement
2 parents c743438 + 9a310dc commit a4a6b88

10 files changed

Lines changed: 113 additions & 14 deletions

File tree

packages/subgraph/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ graph codegen && graph build
7070

7171
6. deploy !
7272

73-
graph deploy teller-v-2-base --version-label 0.4.21-22
73+
graph deploy teller-v-2-base --version-label 0.4.21-25
7474

7575
graph deploy tellerv2-arbitrum --version-label 0.4.21-22
7676

packages/subgraph/config/base.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"enabled":true,"name":"tellerv2-base","network":"base","export_network_name":"base","product":"studio","studio":{"owner":"0x1A2bAA2257343119FB03FD448622456a0c4f2190","network":"mainnet"},"grafting":{"enabled":false,"base":"QmRZxQwGcHmYoeXkPRwWLwkPLzXhhJHF9AyrHM9jhRPTm5","block":"11680298"},"block_handler":{"enabled":true,"block":"2935370"},"contracts":{"teller_v2":{"enabled":true,"address":"0x5cfD3aeD08a444Be32839bD911Ebecd688861164","block":"2935370"},"market_registry":{"enabled":true,"address":"0x2bD9697bF0AB44bE5cA698fB5787d8F13ca48Ffc","block":"2935397"},"lender_commitment":{"enabled":true,"address":"0x84B550EE6959FA3F3A44498836F2A9473734ba78","block":"2935403"},"lender_commitment_staging":{"enabled":true,"address":"0xf236d5Cc4d45eA0eF223Bfdf9583e655f51C12fB","block":"4442684"},"lender_commitment_alpha":{"enabled":true,"address":"0xfA87381128aAF95fB637BbA0B760bA2f9970c2b5","block":"11395281"},"collateral_manager":{"enabled":true,"address":"0x71B04a8569914bCb99D5F95644CF6b089c826024","block":"2935376"},"lender_manager":{"enabled":true,"address":"0x5594f9EE0DdF1e2D21ac8125dfeA66fc4c85Cd01","block":"2936927"},"market_liquidity_rewards":{"enabled":true,"address":"0x18A6BcAd5e52cbecc34b987697FC7bE15eDF9599","block":"2936948"}}}
1+
{"enabled":true,"name":"tellerv2-base","network":"base","export_network_name":"base","product":"studio","studio":{"owner":"0x1A2bAA2257343119FB03FD448622456a0c4f2190","network":"mainnet"},"grafting":{"enabled":false,"base":"QmRZxQwGcHmYoeXkPRwWLwkPLzXhhJHF9AyrHM9jhRPTm5","block":"11680298"},"block_handler":{"enabled":false,"block":"2935370"},"contracts":{"teller_v2":{"enabled":true,"address":"0x5cfD3aeD08a444Be32839bD911Ebecd688861164","block":"2935370"},"market_registry":{"enabled":true,"address":"0x2bD9697bF0AB44bE5cA698fB5787d8F13ca48Ffc","block":"2935397"},"lender_commitment":{"enabled":true,"address":"0x84B550EE6959FA3F3A44498836F2A9473734ba78","block":"2935403"},"lender_commitment_staging":{"enabled":true,"address":"0xf236d5Cc4d45eA0eF223Bfdf9583e655f51C12fB","block":"4442684"},"lender_commitment_alpha":{"enabled":true,"address":"0xfA87381128aAF95fB637BbA0B760bA2f9970c2b5","block":"11395281"},"collateral_manager":{"enabled":true,"address":"0x71B04a8569914bCb99D5F95644CF6b089c826024","block":"2935376"},"lender_manager":{"enabled":true,"address":"0x5594f9EE0DdF1e2D21ac8125dfeA66fc4c85Cd01","block":"2936927"},"market_liquidity_rewards":{"enabled":true,"address":"0x18A6BcAd5e52cbecc34b987697FC7bE15eDF9599","block":"2936948"}}}

packages/subgraph/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"license": "UNLICENSED",
44
"version": "0.4.21-18",
55
"scripts": {
6-
"build": "graph build && graph codegen",
6+
"build": "graph codegen && graph build && graph codegen",
77
"deploy:prompt": "ts-node scripts/thegraph",
88
"create-local": "graph create --node http://localhost:8020/ teller-v2",
99
"remove-local": "graph remove --node http://localhost:8020/ teller-v2",

packages/subgraph/src/collateral-manager/mapping.ts

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
CollateralWithdrawn
88
} from "../../generated/CollateralManager/CollateralManager";
99
import { TellerV2 } from "../../generated/CollateralManager/TellerV2";
10-
import { Bid } from "../../generated/schema";
10+
import { Bid, CollateralDeposit, CollateralWithdrawal } from "../../generated/schema";
1111
import { updateCollateral } from "../collateral-manager/updaters";
1212
import { BidStatus, bidStatusToEnum, isBidDefaulted } from "../helpers/bid";
1313
import { loadBidById, loadCollateral } from "../helpers/loaders";
@@ -60,6 +60,18 @@ export function handleCollateralDeposited(event: CollateralDeposited): void {
6060
updateCollateral(collateral, event);
6161
collateral.status = "Deposited";
6262
collateral.save();
63+
64+
// Create CollateralDeposit entity
65+
const depositId = event.transaction.hash.toHex() + "-" + event.logIndex.toString();
66+
const deposit = new CollateralDeposit(depositId);
67+
deposit.bid = event.params._bidId.toString();
68+
deposit.collateralAddress = event.params._collateralAddress;
69+
deposit.amount = event.params._amount;
70+
deposit.tokenId = event.params._tokenId;
71+
deposit.collateralType = collateralTypeToString(event.params._type);
72+
deposit.timestamp = event.block.timestamp;
73+
deposit.transactionHash = event.transaction.hash.toHex();
74+
deposit.save();
6375
}
6476

6577
export function handleCollateralDepositeds(
@@ -81,6 +93,19 @@ export function handleCollateralWithdrawn(event: CollateralWithdrawn): void {
8193
collateral.receiver = event.params._recipient;
8294
collateral.status = "Withdrawn";
8395
collateral.save();
96+
97+
// Create CollateralWithdrawal entity
98+
const withdrawalId = event.transaction.hash.toHex() + "-" + event.logIndex.toString();
99+
const withdrawal = new CollateralWithdrawal(withdrawalId);
100+
withdrawal.bid = event.params._bidId.toString();
101+
withdrawal.collateralAddress = event.params._collateralAddress;
102+
withdrawal.amount = event.params._amount;
103+
withdrawal.tokenId = event.params._tokenId;
104+
withdrawal.collateralType = collateralTypeToString(event.params._type);
105+
withdrawal.recipient = event.params._recipient;
106+
withdrawal.timestamp = event.block.timestamp;
107+
withdrawal.transactionHash = event.transaction.hash.toHex();
108+
withdrawal.save();
84109
}
85110

86111
/**
@@ -98,6 +123,17 @@ function collateralTypeToTokenType(type: i32): i32 {
98123
return i32.add(type, 1);
99124
}
100125

126+
/**
127+
* Converts the collateral type enum to a string representation.
128+
* @param type
129+
*/
130+
function collateralTypeToString(type: i32): string {
131+
if (type == 0) return "ERC20";
132+
if (type == 1) return "ERC721";
133+
if (type == 2) return "ERC1155";
134+
return "UNKNOWN";
135+
}
136+
101137
export function handleCollateralWithdrawns(
102138
events: CollateralWithdrawn[]
103139
): void {
@@ -107,7 +143,7 @@ export function handleCollateralWithdrawns(
107143
}
108144

109145
/**
110-
* Sets the bid status to `Liquidated` when the collateral is claimed from a defaulted loan.
146+
* Sets the bid status to `Claimed` when the collateral is claimed from a defaulted loan.
111147
* @param event
112148
*/
113149
export function handleCollateralClaimed(event: CollateralClaimed): void {
@@ -116,10 +152,10 @@ export function handleCollateralClaimed(event: CollateralClaimed): void {
116152
const collateralManager = CollateralManager.bind(event.address);
117153
const tellerV2 = TellerV2.bind(collateralManager.tellerV2());
118154

119-
// If the bid is not Repaid, then it means the lender has liquidated the loan
120-
// without making a payment. In this case, we set the bid status to `Liquidated`.
155+
// If the bid is not Repaid, then it means the lender has claimed collateral
156+
// without making a payment. In this case, we set the bid status to `Claimed`.
121157
if (tellerV2.getBidState(bid.bidId) !== BidStatus.Repaid) {
122-
updateBidStatus(bid, BidStatus.Liquidated);
158+
updateBidStatus(bid, BidStatus.Claimed);
123159
}
124160
}
125161

packages/subgraph/src/helpers/bid.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ export enum BidStatus {
1515
Late,
1616
Defaulted,
1717

18-
Closed
18+
Closed,
19+
Claimed
1920
}
2021

21-
export const BidStatusValues = new Array<string>(10);
22+
export const BidStatusValues = new Array<string>(12);
2223
BidStatusValues[BidStatus.None] = "";
2324
BidStatusValues[BidStatus.Submitted] = "Submitted";
2425
BidStatusValues[BidStatus.Cancelled] = "Cancelled";
@@ -31,6 +32,9 @@ BidStatusValues[BidStatus.DueSoon] = "Due Soon";
3132
BidStatusValues[BidStatus.Late] = "Late";
3233
BidStatusValues[BidStatus.Defaulted] = "Defaulted";
3334

35+
BidStatusValues[BidStatus.Closed] = "Closed";
36+
BidStatusValues[BidStatus.Claimed] = "Claimed";
37+
3438
export function bidStatusToEnum(status: string): BidStatus {
3539
return BidStatusValues.indexOf(status);
3640
}

packages/subgraph/src/helpers/loaders.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,12 @@ export function loadLoanStatusCount(
188188
loans.liquidated = [];
189189
loans.liquidatedCount = BigInt.zero();
190190

191+
loans.closed = [];
192+
loans.closedCount = BigInt.zero();
193+
194+
loans.claimed = [];
195+
loans.claimedCount = BigInt.zero();
196+
191197
loans.save();
192198
}
193199

packages/subgraph/src/helpers/updaters.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ export function updateBidOnPayment(
287287
): void {
288288
if (paymentEventType == PaymentEventType.Liquidated) {
289289
updateBidStatus(bid, BidStatus.Liquidated);
290-
} else if (bidStatusToEnum(bid.status) != BidStatus.Liquidated) {
290+
} else if (bidStatusToEnum(bid.status) != BidStatus.Liquidated && bidStatusToEnum(bid.status) != BidStatus.Claimed) {
291291
if (paymentEventType == PaymentEventType.Repayment) {
292292
updateBidStatus(bid, BidStatus.Accepted);
293293
} else if (paymentEventType == PaymentEventType.Repaid) {

packages/subgraph/src/mapping.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Address, BigInt } from "@graphprotocol/graph-ts";
22

33
import { Transfer } from "../generated/LenderManager/LenderManager";
4-
import { Bid, FundedTx, Lender } from "../generated/schema";
4+
import { Bid, FundedTx, Lender, Liquidation } from "../generated/schema";
55
import {
66
AcceptedBid,
77
CancelledBid,
@@ -226,6 +226,15 @@ export function handleLoanLiquidated(event: LoanLiquidated): void {
226226
bid.save();
227227

228228
updateBidOnPayment(bid, event, PaymentEventType.Liquidated);
229+
230+
// Create Liquidation entity
231+
const liquidationId = event.transaction.hash.toHex() + "-" + event.logIndex.toString();
232+
const liquidation = new Liquidation(liquidationId);
233+
liquidation.bid = event.params.bidId.toString();
234+
liquidation.liquidator = event.params.liquidator;
235+
liquidation.timestamp = event.block.timestamp;
236+
liquidation.transactionHash = event.transaction.hash.toHex();
237+
liquidation.save();
229238
}
230239

231240
export function handleLoanLiquidateds(events: LoanLiquidated[]): void {

packages/subgraph/src/schema.graphql

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ type Bid @entity(immutable: false) {
9292
commitmentId: String
9393

9494
payments: [Payment!]! @derivedFrom(field: "bid")
95+
collateralDeposits: [CollateralDeposit!]! @derivedFrom(field: "bid")
96+
collateralWithdrawals: [CollateralWithdrawal!]! @derivedFrom(field: "bid")
97+
liquidations: [Liquidation!]! @derivedFrom(field: "bid")
9598
totalRepaidPrincipal: BigInt!
9699
totalRepaidInterest: BigInt!
97100
_lastTotalRepaidAmount: BigInt!
@@ -139,6 +142,37 @@ type Payment @entity(immutable: false) {
139142
status: String!
140143
}
141144

145+
type CollateralDeposit @entity(immutable: false) {
146+
id: ID!
147+
bid: Bid!
148+
collateralAddress: Bytes!
149+
amount: BigInt!
150+
tokenId: BigInt!
151+
collateralType: String!
152+
timestamp: BigInt!
153+
transactionHash: String!
154+
}
155+
156+
type CollateralWithdrawal @entity(immutable: false) {
157+
id: ID!
158+
bid: Bid!
159+
collateralAddress: Bytes!
160+
amount: BigInt!
161+
tokenId: BigInt!
162+
collateralType: String!
163+
recipient: Bytes!
164+
timestamp: BigInt!
165+
transactionHash: String!
166+
}
167+
168+
type Liquidation @entity(immutable: false) {
169+
id: ID!
170+
bid: Bid!
171+
liquidator: Bytes!
172+
timestamp: BigInt!
173+
transactionHash: String!
174+
}
175+
142176
type User @entity(immutable: false) {
143177
id: ID!
144178
firstInteractionDate: BigInt!
@@ -227,6 +261,12 @@ interface ILoanStatusCount {
227261

228262
liquidated: [Bid!]!
229263
liquidatedCount: BigInt!
264+
265+
closed: [Bid!]!
266+
closedCount: BigInt!
267+
268+
claimed: [Bid!]!
269+
claimedCount: BigInt!
230270
}
231271

232272
type LoanStatusCount implements ILoanStatusCount @entity(immutable: false) {
@@ -262,6 +302,12 @@ type LoanStatusCount implements ILoanStatusCount @entity(immutable: false) {
262302
liquidated: [Bid!]!
263303
liquidatedCount: BigInt!
264304

305+
closed: [Bid!]!
306+
closedCount: BigInt!
307+
308+
claimed: [Bid!]!
309+
claimedCount: BigInt!
310+
265311
## Entity Relationships ##
266312
_protocol: Protocol
267313
_market: MarketPlace

packages/subgraph/src/subgraph.handlebars

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
file: ../contracts/generated/artifacts/@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol/IERC20Metadata.json
1010
{{/inline}}
1111
specVersion: 1.2.0
12-
indexerHints:
13-
prune: never
1412
schema:
1513
file: ./src/schema.graphql
1614
{{#if grafting.enabled}}

0 commit comments

Comments
 (0)