-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: conflict with main * chore: update same asset position checker * chore: multiple assets per position close, handle interest * chore: update open consolidate long to manage assets * fix: go mod that breaks build * fix: unit tests in close long, short and open long * fix: nullify for mtp position initialization and compare * fix: remove nullify in preparing mtp testdata * refactor: additional short logic + test fix * fix: unit margin unit test failure --------- Co-authored-by: Cosmic Vagabond <121588426+cosmic-vagabond@users.noreply.github.com>
- Loading branch information
1 parent
93225cf
commit efafc3e
Showing
46 changed files
with
3,051 additions
and
813 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package keeper | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/elys-network/elys/x/margin/types" | ||
ptypes "github.com/elys-network/elys/x/parameter/types" | ||
) | ||
|
||
func (k Keeper) CalcMTPConsolidateCollateral(ctx sdk.Context, mtp *types.MTP) error { | ||
consolidateCollateral := sdk.ZeroInt() | ||
for i, asset := range mtp.CollateralAssets { | ||
if asset == ptypes.USDC { | ||
consolidateCollateral = consolidateCollateral.Add(mtp.CollateralAmounts[i]) | ||
} else { | ||
// swap into usdc | ||
_, ammPool, _, err := k.OpenChecker.PreparePools(ctx, asset) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
collateralAmtIn := sdk.NewCoin(asset, mtp.CollateralAmounts[i]) | ||
C, err := k.EstimateSwapGivenOut(ctx, collateralAmtIn, ptypes.USDC, ammPool) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
consolidateCollateral = consolidateCollateral.Add(C) | ||
} | ||
} | ||
|
||
mtp.SumCollateral = consolidateCollateral | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package keeper | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/elys-network/elys/x/margin/types" | ||
) | ||
|
||
func (k Keeper) CalcMTPConsolidateLiability(ctx sdk.Context, mtp *types.MTP) { | ||
if mtp.SumCollateral.IsZero() { | ||
return | ||
} | ||
|
||
leverage := mtp.Liabilities.Quo(mtp.SumCollateral) | ||
mtp.ConsolidateLeverage = sdk.NewDecFromInt(leverage) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package keeper | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/elys-network/elys/x/margin/types" | ||
) | ||
|
||
func (k Keeper) CheckSamePosition(ctx sdk.Context, msg *types.MsgOpen) *types.MTP { | ||
mtps := k.GetAllMTPs(ctx) | ||
for _, mtp := range mtps { | ||
if mtp.Address == msg.Creator && mtp.Position == msg.Position { | ||
return &mtp | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"testing" | ||
|
||
tmproto "github.com/cometbft/cometbft/proto/tendermint/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
simapp "github.com/elys-network/elys/app" | ||
"github.com/elys-network/elys/x/margin/types" | ||
ptypes "github.com/elys-network/elys/x/parameter/types" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCheckSameAssets_NewPosition(t *testing.T) { | ||
app := simapp.InitElysTestApp(true) | ||
ctx := app.BaseApp.NewContext(true, tmproto.Header{}) | ||
|
||
k := app.MarginKeeper | ||
|
||
mtp := types.NewMTP("creator", ptypes.USDC, ptypes.ATOM, types.Position_LONG, sdk.NewDec(5), 1) | ||
k.SetMTP(ctx, mtp) | ||
|
||
msg := &types.MsgOpen{ | ||
Creator: "creator", | ||
CollateralAsset: ptypes.ATOM, | ||
CollateralAmount: sdk.NewInt(100), | ||
BorrowAsset: ptypes.ATOM, | ||
Position: types.Position_SHORT, | ||
Leverage: sdk.NewDec(1), | ||
} | ||
|
||
mtp = k.CheckSamePosition(ctx, msg) | ||
|
||
// Expect no error | ||
assert.Nil(t, mtp) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.