Skip to content

Commit

Permalink
Merge branch 'main' into ollieottersync-patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmic-vagabond authored Oct 18, 2023
2 parents 66c5499 + 302590d commit b5ea3f6
Show file tree
Hide file tree
Showing 39 changed files with 1,172 additions and 752 deletions.
816 changes: 672 additions & 144 deletions docs/static/openapi.yml

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions proto/elys/margin/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ message MsgOpenResponse {}
message MsgClose {
string creator = 1;
uint64 id = 2;
string collateralAsset = 3;
string custodyAsset = 4;
}

message MsgCloseResponse {}
Expand Down
34 changes: 14 additions & 20 deletions proto/elys/margin/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package elys.margin;
option go_package = "github.com/elys-network/elys/x/margin/types";

import "gogoproto/gogo.proto";
import "cosmos/base/v1beta1/coin.proto";

enum Position {
UNSPECIFIED = 0;
Expand All @@ -14,48 +15,41 @@ enum Position {

message MTP {
string address = 1;
repeated string collateral_assets = 2;
repeated string collateral_amounts = 3 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
repeated cosmos.base.v1beta1.Coin collaterals = 2 [
(gogoproto.nullable) = false
];
string liabilities = 4 [
string liabilities = 3 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
repeated string interest_paid_collaterals = 5 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
repeated cosmos.base.v1beta1.Coin interest_paid_collaterals = 4 [
(gogoproto.nullable) = false
];
repeated string interest_paid_custodys = 6 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
repeated cosmos.base.v1beta1.Coin interest_paid_custodies = 5 [
(gogoproto.nullable) = false
];
repeated string interest_unpaid_collaterals = 7 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
repeated cosmos.base.v1beta1.Coin interest_unpaid_collaterals = 6 [
(gogoproto.nullable) = false
];
repeated string custody_assets = 8;
repeated string custody_amounts = 9 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
repeated cosmos.base.v1beta1.Coin custodies = 7 [
(gogoproto.nullable) = false
];
repeated string leverages = 10 [
repeated string leverages = 8 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
string mtp_health = 11 [
string mtp_health = 9 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
Position position = 12;
uint64 id = 13;
uint64 amm_pool_id = 14;
string consolidate_leverage = 15 [
Position position = 10;
uint64 id = 11;
uint64 amm_pool_id = 12;
string consolidate_leverage = 13 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
string sum_collateral = 16 [
string sum_collateral = 14 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
Expand Down
6 changes: 3 additions & 3 deletions x/accountedpool/keeper/hooks_margin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
margintypes "github.com/elys-network/elys/x/margin/types"
)

func (k Keeper) AfterMarginPositionOpended(ctx sdk.Context, ammPool ammtypes.Pool, marginPool margintypes.Pool) {
func (k Keeper) AfterMarginPositionOpen(ctx sdk.Context, ammPool ammtypes.Pool, marginPool margintypes.Pool) {
k.UpdateAccountedPool(ctx, ammPool, marginPool)
}

Expand Down Expand Up @@ -50,8 +50,8 @@ func (k Keeper) MarginHooks() MarginHooks {
return MarginHooks{k}
}

func (h MarginHooks) AfterMarginPositionOpended(ctx sdk.Context, ammPool ammtypes.Pool, marginPool margintypes.Pool) {
h.k.AfterMarginPositionOpended(ctx, ammPool, marginPool)
func (h MarginHooks) AfterMarginPositionOpen(ctx sdk.Context, ammPool ammtypes.Pool, marginPool margintypes.Pool) {
h.k.AfterMarginPositionOpen(ctx, ammPool, marginPool)
}

func (h MarginHooks) AfterMarginPositionModified(ctx sdk.Context, ammPool ammtypes.Pool, marginPool margintypes.Pool) {
Expand Down
12 changes: 5 additions & 7 deletions x/margin/client/cli/query_mtp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,12 @@ func networkWithMTPObjects(t *testing.T, n int) (*network.Network, []*types.MTP)
for i := 0; i < n; i++ {
mtp := types.MTP{
Address: addr[i].String(),
CollateralAssets: []string{paramtypes.BaseCurrency},
CollateralAmounts: []sdk.Int{sdk.NewInt(0)},
Collaterals: []sdk.Coin{sdk.NewCoin(paramtypes.BaseCurrency, sdk.NewInt(0))},
Liabilities: sdk.NewInt(0),
InterestPaidCollaterals: []sdk.Int{sdk.NewInt(0)},
InterestPaidCustodys: []sdk.Int{sdk.NewInt(0)},
InterestUnpaidCollaterals: []sdk.Int{sdk.NewInt(0)},
CustodyAssets: []string{"ATOM"},
CustodyAmounts: []sdk.Int{sdk.NewInt(0)},
InterestPaidCollaterals: []sdk.Coin{sdk.NewCoin(paramtypes.BaseCurrency, sdk.NewInt(0))},
InterestPaidCustodies: []sdk.Coin{sdk.NewCoin("ATOM", sdk.NewInt(0))},
InterestUnpaidCollaterals: []sdk.Coin{sdk.NewCoin(paramtypes.BaseCurrency, sdk.NewInt(0))},
Custodies: []sdk.Coin{sdk.NewCoin("ATOM", sdk.NewInt(0))},
Leverages: []sdk.Dec{sdk.NewDec(0)},
MtpHealth: sdk.NewDec(0),
Position: types.Position_LONG,
Expand Down
6 changes: 4 additions & 2 deletions x/margin/keeper/begin_blocker_process_mtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@ func BeginBlockerProcessMTP(ctx sdk.Context, k Keeper, mtp *types.MTP, pool type
mtp.MtpHealth = h
// compute interest
// TODO: missing fields
for _, custodyAsset := range mtp.CustodyAssets {
for _, custody := range mtp.Custodies {
custodyAsset := custody.Denom
// Retrieve AmmPool
ammPool, err := k.CloseLongChecker.GetAmmPool(ctx, mtp.AmmPoolId, custodyAsset)
if err != nil {
ctx.Logger().Error(errors.Wrap(err, fmt.Sprintf("error retrieving amm pool: %d", mtp.AmmPoolId)).Error())
return
}

for _, collateralAsset := range mtp.CollateralAssets {
for _, collateral := range mtp.Collaterals {
collateralAsset := collateral.Denom
// Handle Interest if within epoch position
if err := k.CloseLongChecker.HandleInterest(ctx, mtp, &pool, ammPool, collateralAsset, custodyAsset); err != nil {
ctx.Logger().Error(errors.Wrap(err, fmt.Sprintf("error handling interest payment: %s", collateralAsset)).Error())
Expand Down
10 changes: 5 additions & 5 deletions x/margin/keeper/calc_mtp_consolidate_collateral.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ import (

func (k Keeper) CalcMTPConsolidateCollateral(ctx sdk.Context, mtp *types.MTP) error {
consolidateCollateral := sdk.ZeroInt()
for i, asset := range mtp.CollateralAssets {
if asset == ptypes.BaseCurrency {
consolidateCollateral = consolidateCollateral.Add(mtp.CollateralAmounts[i])
for _, asset := range mtp.Collaterals {
if asset.Denom == ptypes.BaseCurrency {
consolidateCollateral = consolidateCollateral.Add(asset.Amount)
} else {
// swap into base currency
_, ammPool, _, err := k.OpenChecker.PreparePools(ctx, asset)
_, ammPool, _, err := k.OpenChecker.PreparePools(ctx, asset.Denom)
if err != nil {
return err
}

collateralAmtIn := sdk.NewCoin(asset, mtp.CollateralAmounts[i])
collateralAmtIn := sdk.NewCoin(asset.Denom, asset.Amount)
C, err := k.EstimateSwapGivenOut(ctx, collateralAmtIn, ptypes.BaseCurrency, ammPool)
if err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions x/margin/keeper/calc_mtp_interest_liabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ func (k Keeper) CalcMTPInterestLiabilities(ctx sdk.Context, mtp *types.MTP, inte
collateralIndex, _ := k.GetMTPAssetIndex(mtp, collateralAsset, "")
unpaidCollaterals := sdk.ZeroInt()
// Calculate collateral interests in base currency
if mtp.CollateralAssets[collateralIndex] == ptypes.BaseCurrency {
unpaidCollaterals = unpaidCollaterals.Add(mtp.InterestUnpaidCollaterals[collateralIndex])
if mtp.Collaterals[collateralIndex].Denom == ptypes.BaseCurrency {
unpaidCollaterals = unpaidCollaterals.Add(mtp.InterestUnpaidCollaterals[collateralIndex].Amount)
} else {
// Liability is in base currency, so convert it to base currency
unpaidCollateralIn := sdk.NewCoin(mtp.CollateralAssets[collateralIndex], mtp.InterestUnpaidCollaterals[collateralIndex])
unpaidCollateralIn := sdk.NewCoin(mtp.Collaterals[collateralIndex].Denom, mtp.InterestUnpaidCollaterals[collateralIndex].Amount)
C, err := k.EstimateSwapGivenOut(ctx, unpaidCollateralIn, ptypes.BaseCurrency, ammPool)
if err != nil {
return sdk.ZeroInt()
Expand Down
18 changes: 10 additions & 8 deletions x/margin/keeper/close.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,22 @@ func (k Keeper) Close(ctx sdk.Context, msg *types.MsgClose) (*types.MsgCloseResp
return nil, sdkerrors.Wrap(types.ErrInvalidPosition, mtp.Position.String())
}

collateralIndex := len(mtp.Collaterals) - 1
custodyIndex := len(mtp.Custodies) - 1
mtpPosIndex := len(mtp.Leverages) - 1

ctx.EventManager().EmitEvent(sdk.NewEvent(types.EventClose,
sdk.NewAttribute("id", strconv.FormatInt(int64(closedMtp.Id), 10)),
sdk.NewAttribute("position", closedMtp.Position.String()),
sdk.NewAttribute("address", closedMtp.Address),
sdk.NewAttribute("collateral_asset", closedMtp.CollateralAssets[0]),
sdk.NewAttribute("collateral_amount", closedMtp.CollateralAmounts[0].String()),
sdk.NewAttribute("custody_asset", closedMtp.CustodyAssets[0]),
sdk.NewAttribute("custody_amount", closedMtp.CustodyAmounts[0].String()),
sdk.NewAttribute("collateral", closedMtp.Collaterals[collateralIndex].String()),
sdk.NewAttribute("custody", closedMtp.Custodies[custodyIndex].String()),
sdk.NewAttribute("repay_amount", repayAmount.String()),
sdk.NewAttribute("leverage", closedMtp.Leverages[0].String()),
sdk.NewAttribute("leverage", closedMtp.Leverages[mtpPosIndex].String()),
sdk.NewAttribute("liabilities", closedMtp.Liabilities.String()),
sdk.NewAttribute("interest_paid_collateral", mtp.InterestPaidCollaterals[0].String()),
sdk.NewAttribute("interest_paid_custody", mtp.InterestPaidCustodys[0].String()),
sdk.NewAttribute("interest_unpaid_collateral", closedMtp.InterestUnpaidCollaterals[0].String()),
sdk.NewAttribute("interest_paid_collateral", mtp.InterestPaidCollaterals[collateralIndex].String()),
sdk.NewAttribute("interest_paid_custody", mtp.InterestPaidCustodies[custodyIndex].String()),
sdk.NewAttribute("interest_unpaid_collateral", closedMtp.InterestUnpaidCollaterals[collateralIndex].String()),
sdk.NewAttribute("health", closedMtp.MtpHealth.String()),
))

Expand Down
9 changes: 6 additions & 3 deletions x/margin/keeper/close_long.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ func (k Keeper) CloseLong(ctx sdk.Context, msg *types.MsgClose) (*types.MTP, sdk
}

repayAmount := sdk.ZeroInt()
for _, custodyAsset := range mtp.CustodyAssets {
for _, custody := range mtp.Custodies {
custodyAsset := custody.Denom
// Retrieve AmmPool
ammPool, err := k.CloseLongChecker.GetAmmPool(ctx, mtp.AmmPoolId, custodyAsset)
if err != nil {
return nil, sdk.ZeroInt(), err
}

for _, collateralAsset := range mtp.CollateralAssets {
for _, collateral := range mtp.Collaterals {
collateralAsset := collateral.Denom
// Handle Interest if within epoch position
if err := k.CloseLongChecker.HandleInterest(ctx, &mtp, &pool, ammPool, collateralAsset, custodyAsset); err != nil {
return nil, sdk.ZeroInt(), err
Expand All @@ -40,7 +42,8 @@ func (k Keeper) CloseLong(ctx sdk.Context, msg *types.MsgClose) (*types.MTP, sdk
return nil, sdk.ZeroInt(), err
}

for _, collateralAsset := range mtp.CollateralAssets {
for _, collateral := range mtp.Collaterals {
collateralAsset := collateral.Denom
// Estimate swap and repay
repayAmt, err := k.CloseLongChecker.EstimateAndRepay(ctx, mtp, pool, ammPool, collateralAsset, custodyAsset)
if err != nil {
Expand Down
56 changes: 28 additions & 28 deletions x/margin/keeper/close_long_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ func TestCloseLong_AmmPoolNotFound(t *testing.T) {
Id: 1,
}
mtp = types.MTP{
AmmPoolId: 2,
CustodyAssets: []string{"uatom"},
AmmPoolId: 2,
Custodies: []sdk.Coin{sdk.NewCoin("uatom", sdk.NewInt(0))},
}
)

// Mock behavior
mockChecker.On("GetMTP", ctx, msg.Creator, msg.Id).Return(mtp, nil)
mockChecker.On("GetPool", ctx, mtp.AmmPoolId).Return(types.Pool{}, true)
mockChecker.On("GetAmmPool", ctx, mtp.AmmPoolId, mtp.CustodyAssets[0]).Return(ammtypes.Pool{}, sdkerrors.Wrap(types.ErrPoolDoesNotExist, mtp.CustodyAssets[0]))
mockChecker.On("GetAmmPool", ctx, mtp.AmmPoolId, mtp.Custodies[0].Denom).Return(ammtypes.Pool{}, sdkerrors.Wrap(types.ErrPoolDoesNotExist, mtp.Custodies[0].Denom))

_, _, err := k.CloseLong(ctx, msg)

Expand All @@ -122,9 +122,9 @@ func TestCloseLong_ErrorHandleInterest(t *testing.T) {
Id: 1,
}
mtp = types.MTP{
AmmPoolId: 2,
CustodyAssets: []string{"uatom"},
CollateralAssets: []string{ptypes.BaseCurrency},
AmmPoolId: 2,
Collaterals: []sdk.Coin{sdk.NewCoin(ptypes.BaseCurrency, sdk.NewInt(0))},
Custodies: []sdk.Coin{sdk.NewCoin("uatom", sdk.NewInt(0))},
}
pool = types.Pool{
InterestRate: math.LegacyNewDec(2),
Expand All @@ -135,8 +135,8 @@ func TestCloseLong_ErrorHandleInterest(t *testing.T) {
// Mock behavior
mockChecker.On("GetMTP", ctx, msg.Creator, msg.Id).Return(mtp, nil)
mockChecker.On("GetPool", ctx, mtp.AmmPoolId).Return(pool, true)
mockChecker.On("GetAmmPool", ctx, mtp.AmmPoolId, mtp.CustodyAssets[0]).Return(ammPool, nil)
mockChecker.On("HandleInterest", ctx, &mtp, &pool, ammPool, mtp.CollateralAssets[0], mtp.CustodyAssets[0]).Return(errors.New("error executing handle interest"))
mockChecker.On("GetAmmPool", ctx, mtp.AmmPoolId, mtp.Custodies[0].Denom).Return(ammPool, nil)
mockChecker.On("HandleInterest", ctx, &mtp, &pool, ammPool, mtp.Collaterals[0].Denom, mtp.Custodies[0].Denom).Return(errors.New("error executing handle interest"))

_, _, err := k.CloseLong(ctx, msg)

Expand All @@ -161,9 +161,9 @@ func TestCloseLong_ErrorTakeOutCustody(t *testing.T) {
Id: 1,
}
mtp = types.MTP{
AmmPoolId: 2,
CustodyAssets: []string{"uatom"},
CollateralAssets: []string{ptypes.BaseCurrency},
AmmPoolId: 2,
Collaterals: []sdk.Coin{sdk.NewCoin(ptypes.BaseCurrency, sdk.NewInt(0))},
Custodies: []sdk.Coin{sdk.NewCoin("uatom", sdk.NewInt(0))},
}
pool = types.Pool{
InterestRate: math.LegacyNewDec(2),
Expand All @@ -174,9 +174,9 @@ func TestCloseLong_ErrorTakeOutCustody(t *testing.T) {
// Mock behavior
mockChecker.On("GetMTP", ctx, msg.Creator, msg.Id).Return(mtp, nil)
mockChecker.On("GetPool", ctx, mtp.AmmPoolId).Return(pool, true)
mockChecker.On("GetAmmPool", ctx, mtp.AmmPoolId, mtp.CustodyAssets[0]).Return(ammPool, nil)
mockChecker.On("HandleInterest", ctx, &mtp, &pool, ammPool, mtp.CollateralAssets[0], mtp.CustodyAssets[0]).Return(nil)
mockChecker.On("TakeOutCustody", ctx, mtp, &pool, mtp.CustodyAssets[0]).Return(errors.New("error executing take out custody"))
mockChecker.On("GetAmmPool", ctx, mtp.AmmPoolId, mtp.Custodies[0].Denom).Return(ammPool, nil)
mockChecker.On("HandleInterest", ctx, &mtp, &pool, ammPool, mtp.Collaterals[0].Denom, mtp.Custodies[0].Denom).Return(nil)
mockChecker.On("TakeOutCustody", ctx, mtp, &pool, mtp.Custodies[0].Denom).Return(errors.New("error executing take out custody"))

_, _, err := k.CloseLong(ctx, msg)

Expand All @@ -201,9 +201,9 @@ func TestCloseLong_ErrorEstimateAndRepay(t *testing.T) {
Id: 1,
}
mtp = types.MTP{
AmmPoolId: 2,
CustodyAssets: []string{"uatom"},
CollateralAssets: []string{ptypes.BaseCurrency},
AmmPoolId: 2,
Collaterals: []sdk.Coin{sdk.NewCoin(ptypes.BaseCurrency, sdk.NewInt(0))},
Custodies: []sdk.Coin{sdk.NewCoin("uatom", sdk.NewInt(0))},
}
pool = types.Pool{
InterestRate: math.LegacyNewDec(2),
Expand All @@ -214,10 +214,10 @@ func TestCloseLong_ErrorEstimateAndRepay(t *testing.T) {
// Mock behavior
mockChecker.On("GetMTP", ctx, msg.Creator, msg.Id).Return(mtp, nil)
mockChecker.On("GetPool", ctx, mtp.AmmPoolId).Return(pool, true)
mockChecker.On("GetAmmPool", ctx, mtp.AmmPoolId, mtp.CustodyAssets[0]).Return(ammPool, nil)
mockChecker.On("HandleInterest", ctx, &mtp, &pool, ammPool, mtp.CollateralAssets[0], mtp.CustodyAssets[0]).Return(nil)
mockChecker.On("TakeOutCustody", ctx, mtp, &pool, mtp.CustodyAssets[0]).Return(nil)
mockChecker.On("EstimateAndRepay", ctx, mtp, pool, ammPool, mtp.CollateralAssets[0], mtp.CustodyAssets[0]).Return(sdk.Int{}, errors.New("error executing estimate and repay"))
mockChecker.On("GetAmmPool", ctx, mtp.AmmPoolId, mtp.Custodies[0].Denom).Return(ammPool, nil)
mockChecker.On("HandleInterest", ctx, &mtp, &pool, ammPool, mtp.Collaterals[0].Denom, mtp.Custodies[0].Denom).Return(nil)
mockChecker.On("TakeOutCustody", ctx, mtp, &pool, mtp.Custodies[0].Denom).Return(nil)
mockChecker.On("EstimateAndRepay", ctx, mtp, pool, ammPool, mtp.Collaterals[0].Denom, mtp.Custodies[0].Denom).Return(sdk.Int{}, errors.New("error executing estimate and repay"))

_, _, err := k.CloseLong(ctx, msg)

Expand All @@ -242,9 +242,9 @@ func TestCloseLong_SuccessfulClosingLongPosition(t *testing.T) {
Id: 1,
}
mtp = types.MTP{
AmmPoolId: 2,
CustodyAssets: []string{"uatom"},
CollateralAssets: []string{ptypes.BaseCurrency},
AmmPoolId: 2,
Collaterals: []sdk.Coin{sdk.NewCoin(ptypes.BaseCurrency, sdk.NewInt(0))},
Custodies: []sdk.Coin{sdk.NewCoin("uatom", sdk.NewInt(0))},
}
pool = types.Pool{
InterestRate: math.LegacyNewDec(2),
Expand All @@ -256,10 +256,10 @@ func TestCloseLong_SuccessfulClosingLongPosition(t *testing.T) {
// Mock behavior
mockChecker.On("GetMTP", ctx, msg.Creator, msg.Id).Return(mtp, nil)
mockChecker.On("GetPool", ctx, mtp.AmmPoolId).Return(pool, true)
mockChecker.On("GetAmmPool", ctx, mtp.AmmPoolId, mtp.CustodyAssets[0]).Return(ammPool, nil)
mockChecker.On("HandleInterest", ctx, &mtp, &pool, ammPool, mtp.CollateralAssets[0], mtp.CustodyAssets[0]).Return(nil)
mockChecker.On("TakeOutCustody", ctx, mtp, &pool, mtp.CustodyAssets[0]).Return(nil)
mockChecker.On("EstimateAndRepay", ctx, mtp, pool, ammPool, mtp.CollateralAssets[0], mtp.CustodyAssets[0]).Return(repayAmount, nil)
mockChecker.On("GetAmmPool", ctx, mtp.AmmPoolId, mtp.Custodies[0].Denom).Return(ammPool, nil)
mockChecker.On("HandleInterest", ctx, &mtp, &pool, ammPool, mtp.Collaterals[0].Denom, mtp.Custodies[0].Denom).Return(nil)
mockChecker.On("TakeOutCustody", ctx, mtp, &pool, mtp.Custodies[0].Denom).Return(nil)
mockChecker.On("EstimateAndRepay", ctx, mtp, pool, ammPool, mtp.Collaterals[0].Denom, mtp.Custodies[0].Denom).Return(repayAmount, nil)

mtpOut, repayAmountOut, err := k.CloseLong(ctx, msg)

Expand Down
10 changes: 6 additions & 4 deletions x/margin/keeper/close_short.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ func (k Keeper) CloseShort(ctx sdk.Context, msg *types.MsgClose) (*types.MTP, sd
}

repayAmount := sdk.ZeroInt()
for _, custodyAsset := range mtp.CustodyAssets {
for _, custody := range mtp.Custodies {
custodyAsset := custody.Denom
// Retrieve AmmPool
ammPool, err := k.CloseShortChecker.GetAmmPool(ctx, mtp.AmmPoolId, custodyAsset)
if err != nil {
return nil, sdk.ZeroInt(), err
}

for _, collateralAsset := range mtp.CollateralAssets {

for _, collateral := range mtp.Collaterals {
collateralAsset := collateral.Denom
// Handle Interest if within epoch position
if err := k.CloseShortChecker.HandleInterest(ctx, &mtp, &pool, ammPool, collateralAsset, custodyAsset); err != nil {
return nil, sdk.ZeroInt(), err
Expand All @@ -41,7 +42,8 @@ func (k Keeper) CloseShort(ctx sdk.Context, msg *types.MsgClose) (*types.MTP, sd
return nil, sdk.ZeroInt(), err
}

for _, collateralAsset := range mtp.CollateralAssets {
for _, collateral := range mtp.Collaterals {
collateralAsset := collateral.Denom
// Estimate swap and repay
repayAmt, err := k.CloseShortChecker.EstimateAndRepay(ctx, mtp, pool, ammPool, collateralAsset, custodyAsset)
if err != nil {
Expand Down
Loading

0 comments on commit b5ea3f6

Please sign in to comment.