-
Notifications
You must be signed in to change notification settings - Fork 44
/
close_long.go
56 lines (46 loc) · 1.53 KB
/
close_long.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package keeper
import (
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/elys-network/elys/x/perpetual/types"
)
func (k Keeper) CloseLong(ctx sdk.Context, msg *types.MsgClose, baseCurrency string) (*types.MTP, math.Int, error) {
// Retrieve MTP
mtp, err := k.CloseLongChecker.GetMTP(ctx, msg.Creator, msg.Id)
if err != nil {
return nil, sdk.ZeroInt(), err
}
if msg.Amount.GT(mtp.Custody) || msg.Amount.IsNegative() {
return nil, sdk.ZeroInt(), types.ErrInvalidCloseSize
}
// Retrieve Pool
pool, found := k.CloseLongChecker.GetPool(ctx, mtp.AmmPoolId)
if !found {
return nil, sdk.ZeroInt(), errorsmod.Wrap(types.ErrInvalidBorrowingAsset, "invalid pool id")
}
// Retrieve AmmPool
ammPool, err := k.CloseLongChecker.GetAmmPool(ctx, mtp.AmmPoolId, mtp.CustodyAsset)
if err != nil {
return nil, sdk.ZeroInt(), err
}
// Handle Borrow Interest if within epoch position
if err := k.CloseLongChecker.HandleBorrowInterest(ctx, &mtp, &pool, ammPool); err != nil {
return nil, sdk.ZeroInt(), err
}
// Take out custody
err = k.CloseLongChecker.TakeOutCustody(ctx, mtp, &pool, msg.Amount)
if err != nil {
return nil, sdk.ZeroInt(), err
}
// Estimate swap and repay
repayAmt, err := k.CloseLongChecker.EstimateAndRepay(ctx, mtp, pool, ammPool, msg.Amount, baseCurrency)
if err != nil {
return nil, sdk.ZeroInt(), err
}
// Hooks after perpetual position closed
if k.hooks != nil {
k.hooks.AfterPerpetualPositionClosed(ctx, ammPool, pool)
}
return &mtp, repayAmt, nil
}