-
Notifications
You must be signed in to change notification settings - Fork 44
/
estimate_and_repay.go
39 lines (34 loc) · 1.31 KB
/
estimate_and_repay.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
package keeper
import (
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
ammtypes "github.com/elys-network/elys/x/amm/types"
"github.com/elys-network/elys/x/perpetual/types"
)
func (k Keeper) EstimateAndRepay(ctx sdk.Context, mtp types.MTP, pool types.Pool, ammPool ammtypes.Pool, amount math.Int, baseCurrency string) (math.Int, error) {
// init repay amount
var repayAmount math.Int
var err error
// if position is long, repay in collateral asset
if mtp.Position == types.Position_LONG {
custodyAmtTokenIn := sdk.NewCoin(mtp.CustodyAsset, amount)
repayAmount, err = k.EstimateSwap(ctx, custodyAmtTokenIn, mtp.CollateralAsset, ammPool)
if err != nil {
return sdk.ZeroInt(), err
}
} else if mtp.Position == types.Position_SHORT {
// if position is short, repay in trading asset
custodyAmtTokenIn := sdk.NewCoin(mtp.CustodyAsset, amount)
repayAmount, err = k.EstimateSwap(ctx, custodyAmtTokenIn, mtp.TradingAsset, ammPool)
if err != nil {
return sdk.ZeroInt(), err
}
} else {
return sdk.ZeroInt(), types.ErrInvalidPosition
}
// Note: Long settlement is done in trading asset. And short settlement in usdc in Repay function
if err := k.Repay(ctx, &mtp, &pool, ammPool, repayAmount, false, amount, baseCurrency); err != nil {
return sdk.ZeroInt(), err
}
return repayAmount, nil
}