Skip to content

Commit

Permalink
Refactor perpetual test and add more test cases (#977)
Browse files Browse the repository at this point in the history
* refactor and add perpetual tests

* refactor close positions test and add more test cases

* add more test in perpetual close position

* update whitelist and dewhitelist tests

* refactor open consolidate test

* add test cases for open consolidate

* fix open_test
  • Loading branch information
99adarsh authored Nov 21, 2024
1 parent 210d25d commit 52612ca
Show file tree
Hide file tree
Showing 10 changed files with 1,117 additions and 469 deletions.
91 changes: 91 additions & 0 deletions x/perpetual/keeper/begin_blocker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package keeper_test

import (
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
ammtypes "github.com/elys-network/elys/x/amm/types"
leveragelpmodulekeeper "github.com/elys-network/elys/x/leveragelp/keeper"
leveragelpmoduletypes "github.com/elys-network/elys/x/leveragelp/types"
ptypes "github.com/elys-network/elys/x/parameter/types"
"github.com/elys-network/elys/x/perpetual/types"
)

func (suite *PerpetualKeeperTestSuite) TestBeginBlocker() {
Expand All @@ -28,3 +31,91 @@ func (suite *PerpetualKeeperTestSuite) TestBeginBlocker() {
suite.Require().NoError(err)
suite.app.PerpetualKeeper.BeginBlocker(suite.ctx)
}

func (suite *PerpetualKeeperTestSuite) TestComputeFundingRate() {
ammPool := ammtypes.Pool{
PoolId: 1,
PoolAssets: []ammtypes.PoolAsset{
{
Token: sdk.Coin{
Denom: "testAsset",
Amount: math.NewInt(100),
},
},
},
}
pool := types.NewPool(ammPool)
pool.PoolAssetsLong = []types.PoolAsset{
{
Liabilities: math.NewInt(0),
Custody: math.NewInt(0),
Collateral: math.NewInt(0),
AssetDenom: "testAsset",
},
}
pool.PoolAssetsShort = []types.PoolAsset{
{
Liabilities: math.NewInt(0),
Custody: math.NewInt(0),
Collateral: math.NewInt(0),
AssetDenom: "testAsset",
},
}

params := suite.app.PerpetualKeeper.GetParams(suite.ctx)
params.FixedFundingRate = math.LegacyMustNewDecFromStr("0.500000000000000000")
suite.app.PerpetualKeeper.SetParams(suite.ctx, &params)

testCases := []struct {
name string
expectLongRate math.LegacyDec
expectShortRate math.LegacyDec
Liabilities math.Int
Custody math.Int
Collateral math.Int
}{
{
"totalLongOpenInterest is zero",
math.LegacyNewDec(0),
math.LegacyNewDec(0),
math.NewInt(0),
math.NewInt(0),
math.NewInt(0),
},
{
"totalShortOpenInterest is zero",
math.LegacyNewDec(0),
math.LegacyNewDec(0),
math.NewInt(0),
math.NewInt(0),
math.NewInt(0),
},
{
"totalShortOpenInterest is less than totalLongOpenInterest",
math.LegacyMustNewDecFromStr("0.166666666666666666"),
math.LegacyNewDec(0),
math.NewInt(500),
math.NewInt(2000),
math.NewInt(1000),
},
{
"totalLongOpenInterest is less than totalShortOpenInterest",
math.LegacyNewDec(0),
math.LegacyMustNewDecFromStr("0.1"),
math.NewInt(1500),
math.NewInt(2000),
math.NewInt(1000),
},
}

for _, tc := range testCases {
suite.Run(tc.name, func() {
pool.PoolAssetsLong[0].Custody = tc.Custody
pool.PoolAssetsLong[0].Collateral = tc.Collateral
pool.PoolAssetsShort[0].Liabilities = tc.Liabilities
longRate, shortRate := suite.app.PerpetualKeeper.ComputeFundingRate(suite.ctx, pool)
suite.Require().Equal(tc.expectLongRate, longRate)
suite.Require().Equal(tc.expectShortRate, shortRate)
})
}
}
Loading

0 comments on commit 52612ca

Please sign in to comment.