77 CollateralWithdrawn
88} from "../../generated/CollateralManager/CollateralManager" ;
99import { TellerV2 } from "../../generated/CollateralManager/TellerV2" ;
10- import { Bid } from "../../generated/schema" ;
10+ import { Bid , CollateralDeposit , CollateralWithdrawal } from "../../generated/schema" ;
1111import { updateCollateral } from "../collateral-manager/updaters" ;
1212import { BidStatus , bidStatusToEnum , isBidDefaulted } from "../helpers/bid" ;
1313import { 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
6577export 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+
101137export 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 */
113149export 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
0 commit comments