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

[TRA-507] Add tests for UpgradeIsolatedPerpetualToCross #2583

Closed
Closed
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
21 changes: 13 additions & 8 deletions protocol/testutil/keeper/listing.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/dydxprotocol/v4-chain/protocol/lib"
"github.com/dydxprotocol/v4-chain/protocol/mocks"
"github.com/dydxprotocol/v4-chain/protocol/testutil/constants"
assetskeeper "github.com/dydxprotocol/v4-chain/protocol/x/assets/keeper"
clobkeeper "github.com/dydxprotocol/v4-chain/protocol/x/clob/keeper"
perpetualskeeper "github.com/dydxprotocol/v4-chain/protocol/x/perpetuals/keeper"
priceskeeper "github.com/dydxprotocol/v4-chain/protocol/x/prices/keeper"
Expand Down Expand Up @@ -38,6 +39,9 @@ func ListingKeepers(
perpetualsKeeper *perpetualskeeper.Keeper,
clobKeeper *clobkeeper.Keeper,
marketMapKeeper *marketmapkeeper.Keeper,
assetsKeeper *assetskeeper.Keeper,
bankKeeper_out *bankkeeper.BaseKeeper,
subaccountsKeeper *subaccountskeeper.Keeper,
) {
ctx = initKeepers(
t, func(
Expand All @@ -64,13 +68,13 @@ func ListingKeepers(
db,
cdc,
)
bankKeeper, _ = createBankKeeper(stateStore, db, cdc, accountsKeeper)
bankKeeper_out, _ = createBankKeeper(stateStore, db, cdc, accountsKeeper)
stakingKeeper, _ := createStakingKeeper(
stateStore,
db,
cdc,
accountsKeeper,
bankKeeper,
bankKeeper_out,
)
statsKeeper, _ := createStatsKeeper(
stateStore,
Expand Down Expand Up @@ -114,7 +118,7 @@ func ListingKeepers(
epochsKeeper,
transientStoreKey,
)
assetsKeeper, _ := createAssetsKeeper(
assetsKeeper, _ = createAssetsKeeper(
stateStore,
db,
cdc,
Expand All @@ -127,19 +131,19 @@ func ListingKeepers(
rewardsKeeper, _ := createRewardsKeeper(
stateStore,
assetsKeeper,
bankKeeper,
bankKeeper_out,
feeTiersKeeper,
pricesKeeper,
indexerEventManager,
db,
cdc,
)
subaccountsKeeper, _ := createSubaccountsKeeper(
subaccountsKeeper, _ = createSubaccountsKeeper(
stateStore,
db,
cdc,
assetsKeeper,
bankKeeper,
bankKeeper_out,
perpetualsKeeper,
blockTimeKeeper,
transientStoreKey,
Expand All @@ -152,7 +156,7 @@ func ListingKeepers(
memClob,
assetsKeeper,
blockTimeKeeper,
bankKeeper,
bankKeeper_out,
feeTiersKeeper,
perpetualsKeeper,
pricesKeeper,
Expand Down Expand Up @@ -182,7 +186,8 @@ func ListingKeepers(
},
)

return ctx, keeper, storeKey, mockTimeProvider, pricesKeeper, perpetualsKeeper, clobKeeper, marketMapKeeper
return ctx, keeper, storeKey, mockTimeProvider, pricesKeeper, perpetualsKeeper,
clobKeeper, marketMapKeeper, assetsKeeper, bankKeeper_out, subaccountsKeeper
}

func createListingKeeper(
Expand Down
166 changes: 163 additions & 3 deletions protocol/x/listing/keeper/listing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import (
"testing"

testapp "github.com/dydxprotocol/v4-chain/protocol/testutil/app"
bank_testutil "github.com/dydxprotocol/v4-chain/protocol/testutil/bank"
testutil "github.com/dydxprotocol/v4-chain/protocol/testutil/util"

vaulttypes "github.com/dydxprotocol/v4-chain/protocol/x/vault/types"

asstypes "github.com/dydxprotocol/v4-chain/protocol/x/assets/types"

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
satypes "github.com/dydxprotocol/v4-chain/protocol/x/subaccounts/types"

Expand Down Expand Up @@ -62,7 +64,7 @@ func TestCreateMarket(t *testing.T) {
t.Run(
name, func(t *testing.T) {
mockIndexerEventManager := &mocks.IndexerEventManager{}
ctx, keeper, _, _, pricesKeeper, _, _, marketMapKeeper := keepertest.ListingKeepers(
ctx, keeper, _, _, pricesKeeper, _, _, marketMapKeeper, _, _, _ := keepertest.ListingKeepers(
t,
&mocks.BankKeeper{},
mockIndexerEventManager,
Expand Down Expand Up @@ -131,7 +133,7 @@ func TestCreatePerpetual(t *testing.T) {
t.Run(
name, func(t *testing.T) {
mockIndexerEventManager := &mocks.IndexerEventManager{}
ctx, keeper, _, _, pricesKeeper, perpetualsKeeper, _, marketMapKeeper := keepertest.ListingKeepers(
ctx, keeper, _, _, pricesKeeper, perpetualsKeeper, _, marketMapKeeper, _, _, _ := keepertest.ListingKeepers(
t,
&mocks.BankKeeper{},
mockIndexerEventManager,
Expand Down Expand Up @@ -217,7 +219,8 @@ func TestCreateClobPair(t *testing.T) {
t.Run(
name, func(t *testing.T) {
mockIndexerEventManager := &mocks.IndexerEventManager{}
ctx, keeper, _, _, pricesKeeper, perpetualsKeeper, clobKeeper, marketMapKeeper := keepertest.ListingKeepers(
ctx, keeper, _, _, pricesKeeper, perpetualsKeeper, clobKeeper, marketMapKeeper,
_, _, _ := keepertest.ListingKeepers(
t,
&mocks.BankKeeper{},
mockIndexerEventManager,
Expand Down Expand Up @@ -422,3 +425,160 @@ func TestDepositToMegavaultforPML(t *testing.T) {
)
}
}

func TestUpgradeIsolatedPerpetualToCross(t *testing.T) {
tests := map[string]struct {
perpetualId uint32
isolatedInsuranceFundBalance *big.Int
isolatedCollateralPoolBalance *big.Int
crossInsuranceFundBalance *big.Int
crossCollateralPoolBalance *big.Int

expectedErr string
}{
"success": {
perpetualId: 3, // isolated
isolatedInsuranceFundBalance: big.NewInt(1),
isolatedCollateralPoolBalance: big.NewInt(1),
crossInsuranceFundBalance: big.NewInt(1),
crossCollateralPoolBalance: big.NewInt(1),
expectedErr: "",
},
"success - empty isolated insurance fund": {
perpetualId: 3, // isolated
isolatedInsuranceFundBalance: big.NewInt(0),
isolatedCollateralPoolBalance: big.NewInt(1),
crossInsuranceFundBalance: big.NewInt(1),
crossCollateralPoolBalance: big.NewInt(1),
expectedErr: "",
},
"success - empty isolated collateral fund": {
perpetualId: 3, // isolated
isolatedInsuranceFundBalance: big.NewInt(1),
isolatedCollateralPoolBalance: big.NewInt(0),
crossInsuranceFundBalance: big.NewInt(1),
crossCollateralPoolBalance: big.NewInt(1),
expectedErr: "",
},
"success - empty isolated insurance fund + empty isolated collateral fund": {
perpetualId: 3, // isolated
isolatedInsuranceFundBalance: big.NewInt(0),
isolatedCollateralPoolBalance: big.NewInt(0),
crossInsuranceFundBalance: big.NewInt(1),
crossCollateralPoolBalance: big.NewInt(1),
expectedErr: "",
},
"failure - perpetual already has cross market type": {
perpetualId: 1, // cross
isolatedInsuranceFundBalance: big.NewInt(1),
isolatedCollateralPoolBalance: big.NewInt(1),
crossInsuranceFundBalance: big.NewInt(1),
crossCollateralPoolBalance: big.NewInt(1),
expectedErr: "perpetual 1 is not an isolated perpetual and cannot be upgraded to cross",
},
}

for name, tc := range tests {
t.Run(
name, func(t *testing.T) {
mockIndexerEventManager := &mocks.IndexerEventManager{}

ctx, keeper, _, _, pricesKeeper, perpetualsKeeper, _, _, assetsKeeper,
bankKeeper, subaccountsKeeper := keepertest.ListingKeepers(
t,
&mocks.BankKeeper{},
mockIndexerEventManager,
)

// Create the default markets.
keepertest.CreateTestMarkets(t, ctx, pricesKeeper)

// Create liquidity tiers.
keepertest.CreateTestLiquidityTiers(t, ctx, perpetualsKeeper)

// Create USDC asset.
err := keepertest.CreateUsdcAsset(ctx, assetsKeeper)
require.NoError(t, err)

// Create test perpetuals.
keepertest.CreateTestPerpetuals(t, ctx, perpetualsKeeper)

// Get addresses for isolated/cross insurance funds and collateral pools.
isolatedInsuranceFundAddr, err := perpetualsKeeper.GetInsuranceFundModuleAddress(ctx, tc.perpetualId)
require.NoError(t, err)

isolatedCollateralPoolAddr, err := subaccountsKeeper.GetCollateralPoolFromPerpetualId(ctx, tc.perpetualId)
require.NoError(t, err)

crossInsuranceFundAddr := perpetualtypes.InsuranceFundModuleAddress

crossCollateralPoolAddr := satypes.ModuleAddress

// Fund the isolated insurance account, cross insurance account,
// isolated collateral pool, and cross collateral pool.
fundingData := [][]interface{}{
{isolatedInsuranceFundAddr, tc.isolatedInsuranceFundBalance},
{crossInsuranceFundAddr, tc.crossInsuranceFundBalance},
{isolatedCollateralPoolAddr, tc.isolatedCollateralPoolBalance},
{crossCollateralPoolAddr, tc.crossCollateralPoolBalance},
}

for _, data := range fundingData {
addr := data[0].(sdk.AccAddress)
amount := data[1].(*big.Int)

if amount.Cmp(big.NewInt(0)) != 0 {
err = bank_testutil.FundAccount(
ctx,
addr,
sdk.Coins{
sdk.NewCoin(constants.Usdc.Denom, sdkmath.NewIntFromBigInt(amount)),
},
*bankKeeper,
)
require.NoError(t, err)
}
}

// Upgrade perpetual from isolated to cross.
err = keeper.UpgradeIsolatedPerpetualToCross(ctx, tc.perpetualId)
if tc.expectedErr != "" {
require.ErrorContains(t, err, tc.expectedErr)
return
}
require.NoError(t, err)

// Check perpetual market type has been upgraded to cross.
perpetual, err := perpetualsKeeper.GetPerpetual(ctx, tc.perpetualId)
require.NoError(t, err)
require.Equal(
t,
perpetualtypes.PerpetualMarketType_PERPETUAL_MARKET_TYPE_CROSS,
perpetual.Params.MarketType,
)

// Check expected balance for isolated/cross insurance funds and collateral pools.
expectedBalances := [][]interface{}{
{isolatedInsuranceFundAddr, big.NewInt(0)},
{crossInsuranceFundAddr, big.NewInt(0).Add(tc.isolatedInsuranceFundBalance, tc.crossInsuranceFundBalance)},
{isolatedCollateralPoolAddr, big.NewInt(0)},
{crossCollateralPoolAddr, big.NewInt(0).Add(tc.isolatedCollateralPoolBalance, tc.crossCollateralPoolBalance)},
}

for _, data := range expectedBalances {
addr := data[0].(sdk.AccAddress)
amount := data[1].(*big.Int)

require.Equal(
t,
sdk.NewCoin(
asstypes.AssetUsdc.Denom,
sdkmath.NewIntFromBigInt(amount),
),
bankKeeper.GetBalance(ctx, addr, asstypes.AssetUsdc.Denom),
)
}
},
)
}
}
9 changes: 7 additions & 2 deletions protocol/x/subaccounts/keeper/transfer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper

import (
"fmt"
"math/big"

errorsmod "cosmossdk.io/errors"
Expand Down Expand Up @@ -521,14 +522,18 @@ func (k Keeper) TransferIsolatedInsuranceFundToCross(ctx sdk.Context, perpetualI
return nil
}

_, exists := k.assetsKeeper.GetAsset(ctx, assettypes.AssetUsdc.Id)
if !exists {
return fmt.Errorf("USDC asset not found in state")
}

_, coinToTransfer, err := k.assetsKeeper.ConvertAssetToCoin(
ctx,
assettypes.AssetUsdc.Id,
isolatedInsuranceFundBalance,
)
if err != nil {
// Panic if USDC does not exist.
panic(err)
return err
}

isolatedInsuranceFundAddr, err := k.perpetualsKeeper.GetInsuranceFundModuleAddress(ctx, perpetualId)
Expand Down
Loading