Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: improve test coverage for type tests in perp v2 #1518

Merged
merged 16 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [#1500](https://github.com/NibiruChain/nibiru/pull/1500) - refactor(perp): clean up reverse market order mechanics
* [#1502](https://github.com/NibiruChain/nibiru/pull/1502) - feat: add ledger build support
* [#1517](https://github.com/NibiruChain/nibiru/pull/1517) - test: add more tests to x/hooks
* [#1518](https://github.com/NibiruChain/nibiru/pull/1518) - test: add more tests to x/perp

### Features

Expand Down
2 changes: 1 addition & 1 deletion x/common/dec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestSqrtDec(t *testing.T) {
}{
// --------------------------------------------------------------------
// Cases: 1 or higher
{dec: sdk.NewDec(1), sqrtDec: sdk.NewDec(1)},
{dec: sdk.OneDec(), sqrtDec: sdk.OneDec()},
{dec: sdk.NewDec(4), sqrtDec: sdk.NewDec(2)},
{dec: sdk.NewDec(250_000), sqrtDec: sdk.NewDec(500)},
{dec: sdk.NewDec(4_819_136_400), sqrtDec: sdk.NewDec(69_420)},
Expand Down
2 changes: 1 addition & 1 deletion x/common/testutil/cli/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ func New(l Logger, baseDir string, cfg Config) (*Network, error) {
}

memo := fmt.Sprintf("%s@%s:%s", nodeIDs[i], p2pURL.Hostname(), p2pURL.Port())
fee := sdk.NewCoins(sdk.NewCoin(fmt.Sprintf("%stoken", nodeDirName), sdk.NewInt(0)))
fee := sdk.NewCoins(sdk.NewCoin(fmt.Sprintf("%stoken", nodeDirName), sdk.ZeroInt()))
txBuilder := cfg.TxConfig.NewTxBuilder()
err = txBuilder.SetMsgs(createValMsg)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion x/inflation/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func validateExponentialCalculation(i interface{}) error {
}

// validate reduction factor
if v.R.GT(sdk.NewDec(1)) {
if v.R.GT(sdk.OneDec()) {
return fmt.Errorf("reduction factor cannot be greater than 1")
}

Expand Down
10 changes: 5 additions & 5 deletions x/oracle/integration/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,19 @@ func (s *IntegrationTestSuite) TestSuccessfulVoting() {
// then the number picked is the one in the middle always.
prices := []map[asset.Pair]sdk.Dec{
{
"nibi:usdc": sdk.MustNewDecFromStr("1"),
"nibi:usdc": sdk.OneDec(),
"btc:usdc": sdk.MustNewDecFromStr("100203.0"),
},
{
"nibi:usdc": sdk.MustNewDecFromStr("1"),
"nibi:usdc": sdk.OneDec(),
"btc:usdc": sdk.MustNewDecFromStr("100150.5"),
},
{
"nibi:usdc": sdk.MustNewDecFromStr("1"),
"nibi:usdc": sdk.OneDec(),
"btc:usdc": sdk.MustNewDecFromStr("100200.9"),
},
{
"nibi:usdc": sdk.MustNewDecFromStr("1"),
"nibi:usdc": sdk.OneDec(),
"btc:usdc": sdk.MustNewDecFromStr("100300.9"),
},
}
Expand All @@ -88,7 +88,7 @@ func (s *IntegrationTestSuite) TestSuccessfulVoting() {
gotPrices := s.currentPrices()
require.Equal(s.T(),
map[asset.Pair]sdk.Dec{
"nibi:usdc": sdk.MustNewDecFromStr("1"),
"nibi:usdc": sdk.OneDec(),
"btc:usdc": sdk.MustNewDecFromStr("100200.9"),
},
gotPrices,
Expand Down
6 changes: 3 additions & 3 deletions x/oracle/types/ballot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ func TestToCrossRate(t *testing.T) {
expected: sdk.NewDec(16),
},
{
base: sdk.NewDec(0),
base: sdk.ZeroDec(),
quote: sdk.NewDec(100),
expected: sdk.NewDec(16),
},
{
base: sdk.NewDec(1600),
quote: sdk.NewDec(0),
quote: sdk.ZeroDec(),
expected: sdk.NewDec(16),
},
}
Expand Down Expand Up @@ -196,7 +196,7 @@ func TestPBWeightedMedian(t *testing.T) {
[]int64{},
[]int64{},
[]bool{true, true, true, true},
sdk.NewDec(0),
sdk.ZeroDec(),
},
{
// not sorted
Expand Down
4 changes: 2 additions & 2 deletions x/perp/v2/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func (s *IntegrationTestSuite) TestMarketOrdersAndCloseCmd() {
s.EqualValues(sdk.NewDec(2*common.TO_MICRO), queryResp.Position.OpenNotional)
s.EqualValues(sdk.MustNewDecFromStr("1999999.999999999999998000"), queryResp.PositionNotional)
s.EqualValues(sdk.MustNewDecFromStr("-0.000000000000002000"), queryResp.UnrealizedPnl)
s.EqualValues(sdk.NewDec(1), queryResp.MarginRatio)
s.EqualValues(sdk.OneDec(), queryResp.MarginRatio)

s.T().Log("C. open position with 2x leverage and zero baseAmtLimit")
txResp, err = testutilcli.ExecTx(s.network, cli.MarketOrderCmd(), user, []string{
Expand Down Expand Up @@ -384,7 +384,7 @@ func (s *IntegrationTestSuite) TestPartialCloseCmd() {
s.EqualValues(sdk.NewDec(12e6), queryResp.Position.OpenNotional)
s.EqualValues(sdk.MustNewDecFromStr("12000000"), queryResp.PositionNotional)
s.EqualValues(sdk.ZeroDec(), queryResp.UnrealizedPnl)
s.EqualValues(sdk.NewDec(1), queryResp.MarginRatio)
s.EqualValues(sdk.OneDec(), queryResp.MarginRatio)

s.T().Log("Partially close the position")
txResp, err = testutilcli.ExecTx(s.network, cli.PartialCloseCmd(), user, []string{
Expand Down
36 changes: 18 additions & 18 deletions x/perp/v2/keeper/clearing_house_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func TestMarketOrder(t *testing.T) {
).
When(
MoveToNextBlock(),
MarketOrderFails(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(1), sdk.NewDec(1), sdk.ZeroDec(),
MarketOrderFails(alice, pairBtcNusd, types.Direction_LONG, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec(),
types.ErrMarginRatioTooLow,
),
).
Expand Down Expand Up @@ -255,7 +255,7 @@ func TestMarketOrder(t *testing.T) {
).
When(
MoveToNextBlock(),
MarketOrderFails(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(1), sdk.NewDec(1), sdk.ZeroDec(),
MarketOrderFails(alice, pairBtcNusd, types.Direction_LONG, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec(),
types.ErrMarginRatioTooLow,
),
).
Expand Down Expand Up @@ -527,7 +527,7 @@ func TestMarketOrder(t *testing.T) {
).
When(
MoveToNextBlock(),
MarketOrderFails(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(1), sdk.NewDec(1), sdk.ZeroDec(),
MarketOrderFails(alice, pairBtcNusd, types.Direction_SHORT, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec(),
types.ErrMarginRatioTooLow,
),
).
Expand Down Expand Up @@ -617,7 +617,7 @@ func TestMarketOrder(t *testing.T) {
).
When(
MoveToNextBlock(),
MarketOrderFails(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(1), sdk.NewDec(1), sdk.ZeroDec(),
MarketOrderFails(alice, pairBtcNusd, types.Direction_LONG, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec(),
types.ErrMarginRatioTooLow,
),
).
Expand Down Expand Up @@ -993,7 +993,7 @@ func TestMarketOrderError(t *testing.T) {
LastUpdatedBlockNumber: 1,
},
side: types.Direction_LONG,
margin: sdk.NewInt(1),
margin: sdk.OneInt(),
leverage: sdk.OneDec(),
baseLimit: sdk.ZeroDec(),
expectedErr: types.ErrMarginRatioTooLow,
Expand Down Expand Up @@ -1023,7 +1023,7 @@ func TestMarketOrderError(t *testing.T) {
traderFunds: sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 1020)),
initialPosition: nil,
side: types.Direction_SHORT,
margin: sdk.NewInt(0),
margin: sdk.ZeroInt(),
leverage: sdk.NewDec(10),
baseLimit: sdk.NewDec(10_000),
expectedErr: types.ErrInputQuoteAmtNegative,
Expand All @@ -1034,7 +1034,7 @@ func TestMarketOrderError(t *testing.T) {
initialPosition: nil,
side: types.Direction_SHORT,
margin: sdk.NewInt(1000),
leverage: sdk.NewDec(0),
leverage: sdk.ZeroDec(),
baseLimit: sdk.NewDec(10_000),
expectedErr: types.ErrUserLeverageNegative,
},
Expand All @@ -1055,7 +1055,7 @@ func TestMarketOrderError(t *testing.T) {
side: types.Direction_LONG,
margin: sdk.NewInt(100),
leverage: sdk.NewDec(16),
baseLimit: sdk.NewDec(0),
baseLimit: sdk.ZeroDec(),
expectedErr: types.ErrLeverageIsTooHigh,
},
}
Expand Down Expand Up @@ -1815,7 +1815,7 @@ func TestUpdateSwapInvariant(t *testing.T) {
FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(100000)))),
).
When(
MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(10_000_000_000), sdk.NewDec(1), sdk.ZeroDec()),
MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.ZeroDec()),
ClosePosition(alice, pairBtcNusd),
).
Then(
Expand All @@ -1830,7 +1830,7 @@ func TestUpdateSwapInvariant(t *testing.T) {
FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(100000)))),
).
When(
MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(10_000_000_000), sdk.NewDec(1), sdk.NewDec(10_000_000_000_000)),
MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.NewDec(10_000_000_000_000)),
ClosePosition(alice, pairBtcNusd),
).
Then(
Expand All @@ -1845,7 +1845,7 @@ func TestUpdateSwapInvariant(t *testing.T) {
FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(100_000_000)))),
).
When(
MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(10_000_000_000), sdk.NewDec(1), sdk.ZeroDec()),
MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.ZeroDec()),
EditSwapInvariant(pairBtcNusd, startingSwapInvariant.MulInt64(100)),
AMMShouldBeEqual(
pairBtcNusd,
Expand All @@ -1865,7 +1865,7 @@ func TestUpdateSwapInvariant(t *testing.T) {
FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(100_000_000)))),
).
When(
MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(10_000_000_000), sdk.NewDec(1), sdk.ZeroDec()),
MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.ZeroDec()),
EditSwapInvariant(pairBtcNusd, startingSwapInvariant.MulInt64(100)),
AMMShouldBeEqual(
pairBtcNusd,
Expand All @@ -1886,7 +1886,7 @@ func TestUpdateSwapInvariant(t *testing.T) {
FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(100_000_000)))),
).
When(
MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(10_000_000_000), sdk.NewDec(1), sdk.ZeroDec()),
MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.ZeroDec()),
EditSwapInvariant(pairBtcNusd, startingSwapInvariant.Mul(sdk.MustNewDecFromStr("0.1"))),
AMMShouldBeEqual(
pairBtcNusd,
Expand All @@ -1906,7 +1906,7 @@ func TestUpdateSwapInvariant(t *testing.T) {
FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(100_000_000)))),
).
When(
MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(10_000_000_000), sdk.NewDec(1), sdk.ZeroDec()),
MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.ZeroDec()),
EditSwapInvariant(pairBtcNusd, startingSwapInvariant.Mul(sdk.MustNewDecFromStr("0.1"))),
AMMShouldBeEqual(
pairBtcNusd,
Expand All @@ -1927,8 +1927,8 @@ func TestUpdateSwapInvariant(t *testing.T) {
FundAccount(bob, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(10_200_000_000)))),
).
When(
MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(10_000_000_000), sdk.NewDec(1), sdk.ZeroDec()),
MarketOrder(bob, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(10_000_000_000), sdk.NewDec(1), sdk.NewDec(10_000_000_000_000)),
MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.ZeroDec()),
MarketOrder(bob, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.NewDec(10_000_000_000_000)),

EditSwapInvariant(pairBtcNusd, startingSwapInvariant.MulInt64(100)),
AMMShouldBeEqual(
Expand Down Expand Up @@ -1958,8 +1958,8 @@ func TestUpdateSwapInvariant(t *testing.T) {
FundAccount(bob, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(10_200_000_000)))),
).
When(
MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(10_000_000_000), sdk.NewDec(1), sdk.ZeroDec()),
MarketOrder(bob, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(10_000_000_000), sdk.NewDec(1), sdk.NewDec(10_000_000_000_000)),
MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.ZeroDec()),
MarketOrder(bob, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.NewDec(10_000_000_000_000)),

EditSwapInvariant(pairBtcNusd, startingSwapInvariant.Mul(sdk.MustNewDecFromStr("0.1"))),
AMMShouldBeEqual(
Expand Down
Loading
Loading