@@ -25,8 +25,6 @@ func EndBlocker(ctx sdk.Context, k keeper.Keeper) {
2525 handleActiveLoans (ctx , k )
2626
2727 handleLiquidatedLoans (ctx , k )
28- handleDefaultedLoans (ctx , k )
29-
3028 handleRepayments (ctx , k )
3129}
3230
@@ -199,6 +197,9 @@ func handleActiveLoans(ctx sdk.Context, k keeper.Keeper) {
199197 loan .LiquidationId = liquidation .Id
200198 k .SetLoan (ctx , loan )
201199
200+ // add to liquidation queue
201+ k .AddToLiquidationQueue (ctx , loan .VaultAddress )
202+
202203 // trigger dlc event if not triggered yet
203204 if ! k .DLCKeeper ().GetEvent (ctx , loan .DlcEventId ).HasTriggered {
204205 k .DLCKeeper ().TriggerDLCEvent (ctx , loan .DlcEventId , outcomeIndex )
@@ -221,18 +222,18 @@ func handleActiveLoans(ctx sdk.Context, k keeper.Keeper) {
221222
222223// handleLiquidatedLoans handles liquidated loans
223224func handleLiquidatedLoans (ctx sdk.Context , k keeper.Keeper ) {
224- // get all liquidated loans
225- loans := k .GetLoans (ctx , types . LoanStatus_Liquidated )
225+ // get liquidated loans
226+ loans := k .GetLiquidatedLoans (ctx )
226227
227228 for _ , loan := range loans {
228- // check if the liquidation cet has been signed
229+ // get dlc meta
229230 dlcMeta := k .GetDLCMeta (ctx , loan .VaultAddress )
230- if len ( dlcMeta . LiquidationCet . SignedTxHex ) != 0 {
231- continue
232- }
231+
232+ // get liquidation cet and type
233+ cet , cetType := types . GetLiquidationCetAndType ( dlcMeta , loan . Status )
233234
234235 // check if the borrower adapted signatures already exist
235- if len (dlcMeta . LiquidationCet .BorrowerAdaptedSignatures ) == 0 {
236+ if len (cet .BorrowerAdaptedSignatures ) == 0 {
236237 // check if the event attestation has been submitted
237238 attestation := k .DLCKeeper ().GetAttestationByEvent (ctx , loan .DlcEventId )
238239 if attestation == nil {
@@ -243,94 +244,43 @@ func handleLiquidatedLoans(ctx sdk.Context, k keeper.Keeper) {
243244 adaptorSecret := eventSignature [32 :]
244245
245246 // decrypt the adaptor signatures
246- for _ , adaptorSignature := range dlcMeta . LiquidationCet .BorrowerAdaptorSignatures {
247+ for _ , adaptorSignature := range cet .BorrowerAdaptorSignatures {
247248 adaptorSignature , _ := hex .DecodeString (adaptorSignature )
248249 adaptedSignature := adaptor .Adapt (adaptorSignature , adaptorSecret )
249250
250251 // update the adapted signatures
251- dlcMeta . LiquidationCet .BorrowerAdaptedSignatures = append (
252- dlcMeta . LiquidationCet .BorrowerAdaptedSignatures ,
252+ cet .BorrowerAdaptedSignatures = append (
253+ cet .BorrowerAdaptedSignatures ,
253254 hex .EncodeToString (adaptedSignature ))
254255 }
255256 }
256257
257258 // build signed liquidation cet if both borrower adapted signatures(obviously exist) and DCM signatures already exist
258- if len (dlcMeta . LiquidationCet .DCMSignatures ) != 0 {
259- signedTx , txHash , err := types .BuildSignedCet (dlcMeta . LiquidationCet . Tx , loan .BorrowerAuthPubKey , dlcMeta . LiquidationCet . BorrowerAdaptedSignatures , loan .DCM , dlcMeta . LiquidationCet .DCMSignatures )
259+ if len (cet .DCMSignatures ) != 0 {
260+ signedTx , txHash , err := types .BuildSignedCet (cet . Tx , loan .BorrowerAuthPubKey , cet . BorrowerAdaptedSignatures , loan .DCM , cet .DCMSignatures )
260261 if err != nil {
261262 k .Logger (ctx ).Info ("failed to build signed liquidation cet" , "loan id" , loan .VaultAddress , "err" , err )
262263 } else {
263- dlcMeta .LiquidationCet .SignedTxHex = hex .EncodeToString (signedTx )
264-
265- // emit event
266- ctx .EventManager ().EmitEvent (
267- sdk .NewEvent (types .EventTypeGenerateSignedCet ,
268- sdk .NewAttribute (types .AttributeKeyLoanId , loan .VaultAddress ),
269- sdk .NewAttribute (types .AttributeKeyCetType , fmt .Sprintf ("%d" , types .CetType_LIQUIDATION )),
270- sdk .NewAttribute (types .AttributeKeyTxHash , txHash .String ()),
271- ),
272- )
273- }
274- }
275-
276- k .SetDLCMeta (ctx , loan .VaultAddress , dlcMeta )
277- }
278- }
279-
280- // handleDefaultedLoans handles defaulted loans
281- func handleDefaultedLoans (ctx sdk.Context , k keeper.Keeper ) {
282- // get all defaulted loans
283- loans := k .GetLoans (ctx , types .LoanStatus_Defaulted )
264+ cet .SignedTxHex = hex .EncodeToString (signedTx )
284265
285- for _ , loan := range loans {
286- // check if the default liquidation cet has been signed
287- dlcMeta := k .GetDLCMeta (ctx , loan .VaultAddress )
288- if len (dlcMeta .DefaultLiquidationCet .SignedTxHex ) != 0 {
289- continue
290- }
291-
292- // check if the borrower adapted signatures already exist
293- if len (dlcMeta .DefaultLiquidationCet .BorrowerAdaptedSignatures ) == 0 {
294- // check if the event attestation has been submitted
295- attestation := k .DLCKeeper ().GetAttestationByEvent (ctx , loan .DlcEventId )
296- if attestation == nil {
297- continue
298- }
299-
300- eventSignature , _ := hex .DecodeString (attestation .Signature )
301- adaptorSecret := eventSignature [32 :]
302-
303- // decrypt the adaptor signatures
304- for _ , adaptorSignature := range dlcMeta .DefaultLiquidationCet .BorrowerAdaptorSignatures {
305- adaptorSignature , _ := hex .DecodeString (adaptorSignature )
306- adaptedSignature := adaptor .Adapt (adaptorSignature , adaptorSecret )
307-
308- // update the adapted signatures
309- dlcMeta .DefaultLiquidationCet .BorrowerAdaptedSignatures = append (
310- dlcMeta .DefaultLiquidationCet .BorrowerAdaptedSignatures ,
311- hex .EncodeToString (adaptedSignature ))
312- }
313- }
314-
315- // build signed default liquidation cet if both borrower adapted signatures(obviously exist) and DCM signatures already exist
316- if len (dlcMeta .DefaultLiquidationCet .DCMSignatures ) != 0 {
317- signedTx , txHash , err := types .BuildSignedCet (dlcMeta .DefaultLiquidationCet .Tx , loan .BorrowerAuthPubKey , dlcMeta .DefaultLiquidationCet .BorrowerAdaptedSignatures , loan .DCM , dlcMeta .DefaultLiquidationCet .DCMSignatures )
318- if err != nil {
319- k .Logger (ctx ).Info ("failed to build signed default liquidation cet" , "loan id" , loan .VaultAddress , "err" , err )
320- } else {
321- dlcMeta .DefaultLiquidationCet .SignedTxHex = hex .EncodeToString (signedTx )
266+ // remove from the liquidation queue
267+ k .RemoveFromLiquidationQueue (ctx , loan .VaultAddress )
322268
323269 // emit event
324270 ctx .EventManager ().EmitEvent (
325271 sdk .NewEvent (types .EventTypeGenerateSignedCet ,
326272 sdk .NewAttribute (types .AttributeKeyLoanId , loan .VaultAddress ),
327- sdk .NewAttribute (types .AttributeKeyCetType , fmt .Sprintf ("%d" , types . CetType_DEFAULT_LIQUIDATION )),
273+ sdk .NewAttribute (types .AttributeKeyCetType , fmt .Sprintf ("%d" , cetType )),
328274 sdk .NewAttribute (types .AttributeKeyTxHash , txHash .String ()),
329275 ),
330276 )
331277 }
332278 }
333279
280+ // update liquidation cet
281+ types .UpdateLiquidationCet (dlcMeta , cetType , cet )
282+
283+ // update dlc meta
334284 k .SetDLCMeta (ctx , loan .VaultAddress , dlcMeta )
335285 }
336286}
0 commit comments