MCPcopy Create free account
hub / github.com/Sifchain/sifnode / CloseLong

Method CloseLong

x/margin/keeper/msg_server.go:238–305  ·  view source on GitHub ↗
(ctx sdk.Context, msg *types.MsgClose)

Source from the content-addressed store, hash-verified

236}
237
238func (k msgServer) CloseLong(ctx sdk.Context, msg *types.MsgClose) (*types.MTP, sdk.Uint, error) {
239 mtp, err := k.GetMTP(ctx, msg.Signer, msg.Id)
240 if err != nil {
241 return nil, sdk.ZeroUint(), err
242 }
243
244 var pool clptypes.Pool
245
246 nativeAsset := types.GetSettlementAsset()
247 if types.StringCompare(mtp.CollateralAsset, nativeAsset) {
248 pool, err = k.ClpKeeper().GetPool(ctx, mtp.CustodyAsset)
249 if err != nil {
250 return nil, sdk.ZeroUint(), sdkerrors.Wrap(clptypes.ErrPoolDoesNotExist, mtp.CustodyAsset)
251 }
252 } else {
253 pool, err = k.ClpKeeper().GetPool(ctx, mtp.CollateralAsset)
254 if err != nil {
255 return nil, sdk.ZeroUint(), sdkerrors.Wrap(clptypes.ErrPoolDoesNotExist, mtp.CollateralAsset)
256 }
257 }
258
259 epochLength := k.GetEpochLength(ctx)
260 epochPosition := GetEpochPosition(ctx, epochLength)
261 if epochPosition > 0 {
262 interestPayment := CalcMTPInterestLiabilities(&mtp, pool.InterestRate, epochPosition, epochLength)
263
264 finalInterestPayment := k.HandleInterestPayment(ctx, interestPayment, &mtp, &pool)
265
266 if types.StringCompare(mtp.CollateralAsset, nativeAsset) { // custody is external, payment is custody
267 pool.BlockInterestExternal = pool.BlockInterestExternal.Add(finalInterestPayment)
268 } else { // custody is native, payment is custody
269 pool.BlockInterestNative = pool.BlockInterestNative.Add(finalInterestPayment)
270 }
271
272 mtp.MtpHealth, err = k.UpdateMTPHealth(ctx, mtp, pool)
273 if err != nil {
274 return nil, sdk.ZeroUint(), err
275 }
276 }
277
278 err = k.TakeOutCustody(ctx, mtp, &pool)
279 if err != nil {
280 return nil, sdk.ZeroUint(), err
281 }
282 repayAmount, err := k.CLPSwap(ctx, mtp.CustodyAmount, mtp.CollateralAsset, pool)
283 if err != nil {
284 return nil, sdk.ZeroUint(), err
285 }
286
287 err = k.Repay(ctx, &mtp, &pool, repayAmount, false)
288 if err != nil {
289 return nil, sdk.ZeroUint(), err
290 }
291
292 // if types.StringCompare(mtp.CollateralAsset, nativeAsset) {
293 // res, stop := k.ClpKeeper().SingleExternalBalanceModuleAccountCheck(mtp.CustodyAsset)(ctx)
294 // if stop {
295 // return nil, sdk.ZeroUint(), sdkerrors.Wrap(clptypes.ErrBalanceModuleAccountCheck, res)

Callers 1

CloseMethod · 0.95

Calls 12

GetEpochPositionFunction · 0.85
AddMethod · 0.80
GetMTPMethod · 0.65
GetPoolMethod · 0.65
ClpKeeperMethod · 0.65
GetEpochLengthMethod · 0.65
HandleInterestPaymentMethod · 0.65
UpdateMTPHealthMethod · 0.65
TakeOutCustodyMethod · 0.65
CLPSwapMethod · 0.65
RepayMethod · 0.65

Tested by

no test coverage detected