-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: margin functions * fix: test cases * fix: mocks * test: fix test cases
- Loading branch information
1 parent
b337ad1
commit f4b06af
Showing
12 changed files
with
222 additions
and
53 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package keeper | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
"github.com/elys-network/elys/x/margin/types" | ||
ptypes "github.com/elys-network/elys/x/parameter/types" | ||
) | ||
|
||
func (k Keeper) CheckShortAssets(ctx sdk.Context, collateralAsset string, borrowAsset string) error { | ||
// You shouldn't be shorting the base currency (like USDC). | ||
if borrowAsset == ptypes.BaseCurrency { | ||
return sdkerrors.Wrap(types.ErrInvalidBorrowingAsset, "cannot short the base currency") | ||
} | ||
|
||
// If both the collateralAsset and borrowAsset are the same, it doesn't make sense. | ||
if collateralAsset == borrowAsset { | ||
return sdkerrors.Wrap(types.ErrInvalidCollateralAsset, "collateral asset cannot be the same as the borrowed asset in a short position") | ||
} | ||
|
||
// The collateral for a short must be the base currency. | ||
if collateralAsset != ptypes.BaseCurrency { | ||
return sdkerrors.Wrap(types.ErrInvalidCollateralAsset, "collateral asset for a short position must be the base currency") | ||
} | ||
|
||
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,59 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
"github.com/elys-network/elys/x/margin/keeper" | ||
"github.com/elys-network/elys/x/margin/types" | ||
"github.com/elys-network/elys/x/margin/types/mocks" | ||
"github.com/stretchr/testify/assert" | ||
|
||
ptypes "github.com/elys-network/elys/x/parameter/types" | ||
) | ||
|
||
func TestCheckShortAssets_InvalidAssets(t *testing.T) { | ||
// Setup the mock checker | ||
mockChecker := new(mocks.OpenShortChecker) | ||
|
||
// Create an instance of Keeper with the mock checker | ||
k := keeper.Keeper{ | ||
OpenShortChecker: mockChecker, | ||
} | ||
|
||
ctx := sdk.Context{} // mock or setup a context | ||
|
||
// Test invalid cases for short positions | ||
err := k.CheckShortAssets(ctx, ptypes.ATOM, ptypes.BaseCurrency) | ||
assert.True(t, errors.Is(err, sdkerrors.Wrap(types.ErrInvalidBorrowingAsset, "cannot short the base currency"))) | ||
|
||
err = k.CheckShortAssets(ctx, ptypes.ATOM, ptypes.ATOM) | ||
assert.True(t, errors.Is(err, sdkerrors.Wrap(types.ErrInvalidCollateralAsset, "collateral asset cannot be the same as the borrowed asset in a short position"))) | ||
|
||
err = k.CheckShortAssets(ctx, ptypes.ATOM, "btc") | ||
assert.True(t, errors.Is(err, sdkerrors.Wrap(types.ErrInvalidCollateralAsset, "collateral asset for a short position must be the base currency"))) | ||
|
||
// Expect no error | ||
mockChecker.AssertExpectations(t) | ||
} | ||
|
||
func TestCheckShortAssets_ValidAssets(t *testing.T) { | ||
// Setup the mock checker | ||
mockChecker := new(mocks.OpenShortChecker) | ||
|
||
// Create an instance of Keeper with the mock checker | ||
k := keeper.Keeper{ | ||
OpenShortChecker: mockChecker, | ||
} | ||
|
||
ctx := sdk.Context{} // mock or setup a context | ||
|
||
// Test valid case for short position | ||
err := k.CheckShortAssets(ctx, ptypes.BaseCurrency, ptypes.ATOM) | ||
assert.Nil(t, err) | ||
|
||
// Expect no error | ||
mockChecker.AssertExpectations(t) | ||
} |
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
Oops, something went wrong.