Skip to content

Commit 3281fae

Browse files
committed
optimize dlc event and cet query
1 parent 98f7ac7 commit 3281fae

File tree

8 files changed

+577
-407
lines changed

8 files changed

+577
-407
lines changed

api/side/lending/query.pulsar.go

Lines changed: 356 additions & 281 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/static/openapi.yml

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

proto/side/lending/query.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ message QueryDlcEventCountResponse {
133133

134134
message QueryLoanCetInfosRequest {
135135
string loan_id = 1;
136+
string collateral_amount = 2;
136137
}
137138

138139
message QueryLoanCetInfosResponse {

x/lending/keeper/dlc.go

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import (
88
"github.com/btcsuite/btcd/btcutil/psbt"
99
"github.com/btcsuite/btcd/txscript"
1010

11+
sdkmath "cosmossdk.io/math"
1112
sdk "github.com/cosmos/cosmos-sdk/types"
1213

14+
dlctypes "github.com/sideprotocol/side/x/dlc/types"
1315
"github.com/sideprotocol/side/x/lending/types"
1416
)
1517

@@ -132,34 +134,50 @@ func (k Keeper) UpdateDLCMeta(ctx sdk.Context, loanId string, depositTxs []*psbt
132134
}
133135

134136
// UpdateDLCEvent updates the dlc event of the given loan
135-
func (k Keeper) UpdateDLCEvent(ctx sdk.Context, loanId string) {
136-
loan := k.GetLoan(ctx, loanId)
137-
137+
func (k Keeper) UpdateDLCEvent(ctx sdk.Context, loan *types.Loan) {
138138
dlcEvent := k.dlcKeeper.GetEvent(ctx, loan.DlcEventId)
139139

140140
// update description
141-
dlcEvent.Description = fmt.Sprintf("DLC event for loan %s", loanId)
141+
dlcEvent.Description = fmt.Sprintf("DLC event for loan %s", loan.VaultAddress)
142+
143+
liquidatedOutcome := "" // will be populated once the liquidation price is available
144+
defaultLiquidatedOutcome := fmt.Sprintf("%d", loan.MaturityTime)
145+
repaidOutcome := "Repaid"
142146

143147
// update outcomes
144148
dlcEvent.Outcomes = []string{
145-
"Liquidated",
146-
"Default liquidated",
147-
"Repaid",
149+
liquidatedOutcome,
150+
defaultLiquidatedOutcome,
151+
repaidOutcome,
148152
}
149153

150154
k.dlcKeeper.SetEvent(ctx, dlcEvent)
151155
}
152156

157+
// UpdateDLCEventLiquidatedOutcome populates the 'liquidated' outcome for the dlc event with the specified liquidation price
158+
func (k Keeper) UpdateDLCEventLiquidatedOutcome(ctx sdk.Context, loan *types.Loan, dlcEvent *dlctypes.DLCEvent, liquidationPrice sdkmath.LegacyDec) {
159+
pool := k.GetPool(ctx, loan.PoolId)
160+
161+
dlcEvent.Outcomes[types.LiquidatedOutcomeIndex] = types.FormatPriceWithPair(liquidationPrice, types.GetPricePair(pool.Config))
162+
}
163+
153164
// GetCetInfos gets the related cet infos of the given loan
154165
// Assume that the loan exists
155-
func (k Keeper) GetCetInfos(ctx sdk.Context, loanId string) ([]*types.CetInfo, error) {
166+
func (k Keeper) GetCetInfos(ctx sdk.Context, loanId string, collateralAmount sdk.Coin) ([]*types.CetInfo, error) {
156167
loan := k.GetLoan(ctx, loanId)
157168
dlcMeta := k.GetDLCMeta(ctx, loanId)
158169

159170
liquidationScript, liquidationScriptControlBlock, _ := types.UnwrapLeafScript(dlcMeta.LiquidationScript)
160171
repaymentScript, repaymentScriptControlBlock, _ := types.UnwrapLeafScript(dlcMeta.RepaymentScript)
161172

162173
dlcEvent := k.dlcKeeper.GetEvent(ctx, loan.DlcEventId)
174+
if len(dlcEvent.Outcomes[types.LiquidatedOutcomeIndex]) == 0 {
175+
// calculate the liquidation price
176+
liquidationPrice := k.GetLiquidationPrice(ctx, loan, collateralAmount.Amount)
177+
178+
// update the dlc event outcome
179+
k.UpdateDLCEventLiquidatedOutcome(ctx, loan, dlcEvent, liquidationPrice)
180+
}
163181

164182
liquidationCetInfo, _ := types.GetCetInfo(dlcEvent, types.LiquidatedOutcomeIndex, liquidationScript, liquidationScriptControlBlock)
165183
defaultLiquidationCetInfo, _ := types.GetCetInfo(dlcEvent, types.DefaultLiquidatedOutcomeIndex, liquidationScript, liquidationScriptControlBlock)

x/lending/keeper/loan.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,17 @@ func (k Keeper) GetRepayment(ctx sdk.Context, loanId string) *types.Repayment {
222222
return &repayment
223223
}
224224

225+
// GetLiquidationPrice gets the liquidation price of the given loan according to the specified collateral amount
226+
func (k Keeper) GetLiquidationPrice(ctx sdk.Context, loan *types.Loan, collateralAmount sdkmath.Int) sdkmath.LegacyDec {
227+
pool := k.GetPool(ctx, loan.PoolId)
228+
229+
collateralDecimals := int(pool.Config.CollateralAsset.Decimals)
230+
borrowDecimals := int(pool.Config.LendingAsset.Decimals)
231+
collateralIsBaseAsset := pool.Config.CollateralAsset.IsBasePriceAsset
232+
233+
return types.GetLiquidationPrice(collateralAmount, collateralDecimals, loan.BorrowAmount.Amount, borrowDecimals, loan.Maturity, loan.BorrowAPR, k.GetBlocksPerYear(ctx), pool.Config.LiquidationThreshold, collateralIsBaseAsset)
234+
}
235+
225236
// GetCurrentBorrowIndex gets the current borrow index of the given loan
226237
// Assume that the loan maturity exists in the pool tranches
227238
func (k Keeper) GetCurrentBorrowIndex(ctx sdk.Context, loan *types.Loan) sdkmath.LegacyDec {

x/lending/keeper/msg_server_loan.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func (m msgServer) Apply(goCtx context.Context, msg *types.MsgApply) (*types.Msg
122122
m.SetDLCMeta(ctx, loan.VaultAddress, dlcMeta)
123123

124124
// update dlc event
125-
m.UpdateDLCEvent(ctx, loan.VaultAddress)
125+
m.UpdateDLCEvent(ctx, loan)
126126

127127
ctx.EventManager().EmitEvent(
128128
sdk.NewEvent(types.EventTypeApply,
@@ -160,9 +160,6 @@ func (m msgServer) SubmitCets(goCtx context.Context, msg *types.MsgSubmitCets) (
160160
return nil, errorsmod.Wrap(types.ErrInvalidLoanStatus, "loan non requested")
161161
}
162162

163-
pool := m.GetPool(ctx, loan.PoolId)
164-
poolConfig := pool.Config
165-
166163
vaultPkScript, _ := types.GetPkScriptFromAddress(loan.VaultAddress)
167164

168165
depositTxs := []*psbt.Packet{}
@@ -186,7 +183,12 @@ func (m msgServer) SubmitCets(goCtx context.Context, msg *types.MsgSubmitCets) (
186183
return nil, errorsmod.Wrap(types.ErrInsufficientCollateral, "collateral amount can not be zero")
187184
}
188185

186+
// calculate liquidation price
187+
liquidationPrice := m.GetLiquidationPrice(ctx, loan, collateralAmount)
188+
189+
// update dlc event outcome
189190
dlcEvent := m.dlcKeeper.GetEvent(ctx, loan.DlcEventId)
191+
m.UpdateDLCEventLiquidatedOutcome(ctx, loan, dlcEvent, liquidationPrice)
190192

191193
// verify cets
192194
if err := types.VerifyCets(depositTxs, vaultPkScript, loan.BorrowerPubKey, loan.BorrowerAuthPubKey, loan.DCM, dlcEvent, msg.LiquidationCet, msg.LiquidationAdaptorSignatures, msg.DefaultLiquidationAdaptorSignatures, msg.RepaymentCet, msg.RepaymentSignatures); err != nil {
@@ -214,20 +216,16 @@ func (m msgServer) SubmitCets(goCtx context.Context, msg *types.MsgSubmitCets) (
214216
}
215217
}
216218

217-
collateralDecimals := int(poolConfig.CollateralAsset.Decimals)
218-
borrowDecimals := int(poolConfig.LendingAsset.Decimals)
219-
collateralIsBaseAsset := poolConfig.CollateralAsset.IsBasePriceAsset
220-
221-
// calculate liquidation price
222-
liquidationPrice := types.GetLiquidationPrice(collateralAmount, collateralDecimals, loan.BorrowAmount.Amount, borrowDecimals, loan.Maturity, loan.BorrowAPR, m.GetBlocksPerYear(ctx), poolConfig.LiquidationThreshold, collateralIsBaseAsset)
223-
224219
// update loan
225220
loan.Authorizations = append(loan.Authorizations, *authorization)
226221
loan.CollateralAmount = collateralAmount
227222
loan.LiquidationPrice = liquidationPrice
228223
loan.Status = types.LoanStatus_Authorized
229224
m.SetLoan(ctx, loan)
230225

226+
// update dlc event
227+
m.dlcKeeper.SetEvent(ctx, dlcEvent)
228+
231229
return &types.MsgSubmitCetsResponse{}, nil
232230
}
233231

x/lending/keeper/queries.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,23 @@ func (k Keeper) LoanCetInfos(goCtx context.Context, req *types.QueryLoanCetInfos
213213
return nil, status.Error(codes.InvalidArgument, "loan does not exist")
214214
}
215215

216-
cetInfos, err := k.GetCetInfos(ctx, req.LoanId)
216+
loan := k.GetLoan(ctx, req.LoanId)
217+
218+
var err error
219+
var collateralAmount sdk.Coin
220+
221+
if loan.LiquidationPrice.IsZero() {
222+
collateralAmount, err = sdk.ParseCoinNormalized(req.CollateralAmount)
223+
if err != nil {
224+
return nil, status.Error(codes.InvalidArgument, err.Error())
225+
}
226+
227+
if !collateralAmount.IsPositive() {
228+
return nil, status.Error(codes.InvalidArgument, "collateral amount must be positive")
229+
}
230+
}
231+
232+
cetInfos, err := k.GetCetInfos(ctx, req.LoanId, collateralAmount)
217233
if err != nil {
218234
return nil, status.Error(codes.Internal, err.Error())
219235
}

0 commit comments

Comments
 (0)