Skip to content

Commit

Permalink
feat: added update collateral to genesis export and loads for test an…
Browse files Browse the repository at this point in the history
…d network migration
  • Loading branch information
matthiasmatt committed Nov 2, 2023
1 parent b10c7fa commit 41c9231
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 59 deletions.
9 changes: 6 additions & 3 deletions proto/nibiru/perp/v2/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ syntax = "proto3";

package nibiru.perp.v2;

import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "cosmos/base/v1beta1/coin.proto";
import "google/api/annotations.proto";
import "gogoproto/gogo.proto";
import "nibiru/perp/v2/state.proto";

option go_package = "github.com/NibiruChain/nibiru/x/perp/v2/types";
Expand All @@ -23,15 +23,18 @@ message GenesisState {
[ (gogoproto.nullable) = false ];

uint64 dnr_epoch = 6;

message TraderVolume {
string trader = 1;
uint64 epoch = 2;
string volume = 3 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];

// For testing purpose, we allow the collateral to be set at genesis
}
Collateral collateral = 11;

repeated TraderVolume trader_volumes = 7 [ (gogoproto.nullable) = false ];

Expand Down
1 change: 1 addition & 0 deletions x/common/testutil/genesis/perp_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ func AddPerpV2Genesis(gen app.GenesisState) app.GenesisState {
Amms: ammsv2,
Positions: []perpv2types.GenesisPosition{},
ReserveSnapshots: []perpv2types.ReserveSnapshot{},
Collateral: &perpv2types.DefaultTestingCollateralNotForProd,
}

gen[perpv2types.ModuleName] = app.MakeEncodingConfig().Marshaler.
Expand Down
24 changes: 12 additions & 12 deletions x/perp/v2/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (s *IntegrationTestSuite) SetupSuite() {
sdk.NewCoins(
sdk.NewInt64Coin(denoms.NIBI, 10e6),
sdk.NewInt64Coin(denoms.USDC, 1e3*common.TO_MICRO),
sdk.NewInt64Coin(denoms.NUSD, 5e3*common.TO_MICRO),
sdk.NewInt64Coin(types.DefaultTestingCollateralNotForProd.GetTFDenom(), 5e3*common.TO_MICRO),
),
val,
denoms.NIBI,
Expand Down Expand Up @@ -486,14 +486,14 @@ func (s *IntegrationTestSuite) TestRemoveMargin() {
s.T().Log("removing margin on user 0....")
_, err = s.network.ExecTxCmd(cli.RemoveMarginCmd(), s.users[0], []string{
asset.Registry.Pair(denoms.BTC, denoms.NUSD).String(),
fmt.Sprintf("%s%s", "10000000", denoms.NUSD),
fmt.Sprintf("%s%s", "10000000", types.DefaultTestingCollateralNotForProd.GetTFDenom()),
})
s.Contains(err.Error(), types.ErrBadDebt.Error())

s.T().Log("removing margin on user 0....")
_, err = s.network.ExecTxCmd(cli.RemoveMarginCmd(), s.users[0], []string{
asset.Registry.Pair(denoms.BTC, denoms.NUSD).String(),
fmt.Sprintf("%s%s", "1", denoms.NUSD),
fmt.Sprintf("%s%s", "1", types.DefaultTestingCollateralNotForProd.GetTFDenom()),
})
s.NoError(err)
s.NoError(s.network.WaitForNextBlock())
Expand Down Expand Up @@ -524,7 +524,7 @@ func (s *IntegrationTestSuite) TestX_AddMargin() {
name: "PASS: add margin to correct position",
args: []string{
asset.Registry.Pair(denoms.ETH, denoms.NUSD).String(),
fmt.Sprintf("10000%s", denoms.NUSD),
fmt.Sprintf("10000%s", types.DefaultTestingCollateralNotForProd.GetTFDenom()),
},
expectedCode: 0,
expectedMargin: sdk.NewDec(1_010_000),
Expand All @@ -534,7 +534,7 @@ func (s *IntegrationTestSuite) TestX_AddMargin() {
name: "fail: position not found",
args: []string{
asset.Registry.Pair(denoms.BTC, denoms.NUSD).String(),
fmt.Sprintf("10000%s", denoms.NUSD),
fmt.Sprintf("10000%s", types.DefaultTestingCollateralNotForProd.GetTFDenom()),
},
expectedCode: types.ErrPositionNotFound.ABCICode(),
expectFail: false,
Expand All @@ -559,7 +559,7 @@ func (s *IntegrationTestSuite) TestX_AddMargin() {
name: "fail: invalid pair",
args: []string{
"alisdhjal;dhao;sdh",
fmt.Sprintf("10000%s", denoms.NUSD),
fmt.Sprintf("10000%s", types.DefaultTestingCollateralNotForProd.GetTFDenom()),
},
expectFail: true,
},
Expand Down Expand Up @@ -626,7 +626,7 @@ func (s *IntegrationTestSuite) TestX_RemoveMargin() {
name: "PASS: remove margin to correct position",
args: []string{
asset.Registry.Pair(denoms.ETH, denoms.NUSD).String(),
fmt.Sprintf("10000%s", denoms.NUSD),
fmt.Sprintf("10000%s", types.DefaultTestingCollateralNotForProd.GetTFDenom()),
},
expectedCode: 0,
expectedMargin: sdk.NewDec(990_000),
Expand All @@ -636,7 +636,7 @@ func (s *IntegrationTestSuite) TestX_RemoveMargin() {
name: "fail: position not found",
args: []string{
asset.Registry.Pair(denoms.BTC, denoms.NUSD).String(),
fmt.Sprintf("10000%s", denoms.NUSD),
fmt.Sprintf("10000%s", types.DefaultTestingCollateralNotForProd.GetTFDenom()),
},
expectedCode: types.ErrPositionNotFound.ABCICode(),
expectFail: false,
Expand All @@ -661,7 +661,7 @@ func (s *IntegrationTestSuite) TestX_RemoveMargin() {
name: "fail: invalid pair",
args: []string{
"alisdhjal;dhao;sdh",
fmt.Sprintf("10000%s", denoms.NUSD),
fmt.Sprintf("10000%s", types.DefaultTestingCollateralNotForProd.GetTFDenom()),
},
expectFail: true,
},
Expand Down Expand Up @@ -704,7 +704,7 @@ func (s *IntegrationTestSuite) TestDonateToEcosystemFund() {
out, err := s.network.ExecTxCmd(
cli.DonateToEcosystemFundCmd(),
sdk.MustAccAddressFromBech32("nibi1w89pf5yq8ntjg89048qmtaz929fdxup0a57d8m"),
[]string{"100unusd"},
[]string{"100" + types.DefaultTestingCollateralNotForProd.GetTFDenom()},
)
s.NoError(err)
s.Require().EqualValues(abcitypes.CodeTypeOK, out.Code)
Expand All @@ -724,11 +724,11 @@ func (s *IntegrationTestSuite) TestDonateToEcosystemFund() {
testutilcli.ExecQuery(
s.network.Validators[0].ClientCtx,
bankcli.GetBalancesCmd(),
[]string{moduleAccountAddrPerpEF, "--denom", "unusd"},
[]string{moduleAccountAddrPerpEF, "--denom", types.DefaultTestingCollateralNotForProd.GetTFDenom()},
resp,
),
)
s.Require().EqualValues(sdk.NewInt64Coin("unusd", 100), *resp)
s.Require().EqualValues(sdk.NewInt64Coin(types.DefaultTestingCollateralNotForProd.GetTFDenom(), 100), *resp)
}

func (s *IntegrationTestSuite) TestQueryModuleAccount() {
Expand Down
9 changes: 9 additions & 0 deletions x/perp/v2/module/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState)
customDiscount.Discount.Fee,
)
}

if genState.Collateral != nil {
k.Admin.UpdateCollateral(ctx, *&genState.Collateral.Denom, *&genState.Collateral.ContractAddress)
}
}

// ExportGenesis returns the capability module's exported genesis.
Expand Down Expand Up @@ -134,5 +138,10 @@ func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
},
})
}

collateral, err := k.Collateral.Get(ctx)
if err != nil {
genesis.Collateral = &collateral
}
return genesis
}
2 changes: 2 additions & 0 deletions x/perp/v2/module/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func RunTestGenesis(t *testing.T, tc TestCase) {
pair := asset.Registry.Pair(denoms.BTC, denoms.NUSD)

// create some params
app.PerpKeeperV2.Admin.UpdateCollateral(ctx, "unusd", types.DefaultTestingCollateralNotForProd.ContractAddress)
app.PerpKeeperV2.SaveMarket(ctx, *mock.TestMarket())
app.PerpKeeperV2.MarketLastVersion.Insert(ctx, pair, types.MarketLastVersion{Version: 1})
app.PerpKeeperV2.SaveAMM(ctx, *mock.TestAMMDefault())
Expand Down Expand Up @@ -118,6 +119,7 @@ func RunTestGenesis(t *testing.T, tc TestCase) {
require.Equal(t, genState.CustomDiscounts, genStateAfterInit.CustomDiscounts)
require.Equal(t, genState.GlobalDiscount, genStateAfterInit.GlobalDiscount)
require.Equal(t, genState.TraderVolumes, genStateAfterInit.TraderVolumes)
require.Equal(t, genState.Collateral, genStateAfterInit.Collateral)
}

func TestNewAppModuleBasic(t *testing.T) {
Expand Down
150 changes: 106 additions & 44 deletions x/perp/v2/types/genesis.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions x/perp/v2/types/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func TestDefaultGenesis(t *testing.T) {
require.Empty(t, genesis.Amms)
require.Empty(t, genesis.Positions)
require.Empty(t, genesis.ReserveSnapshots)
require.Empty(t, genesis.Collateral)
}

func TestGenesisValidate(t *testing.T) {
Expand Down

0 comments on commit 41c9231

Please sign in to comment.