Skip to content

Commit

Permalink
test: add test for simulations
Browse files Browse the repository at this point in the history
  • Loading branch information
kingcre committed Oct 26, 2023
1 parent a5c4fb9 commit b4c8f0c
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 0 deletions.
139 changes: 139 additions & 0 deletions x/liquidamm/simulation/operations_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package simulation_test

import (
"math/rand"
"testing"

"github.com/stretchr/testify/suite"

sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"

chain "github.com/crescent-network/crescent/v5/app"
"github.com/crescent-network/crescent/v5/app/testutil"
utils "github.com/crescent-network/crescent/v5/types"
ammtypes "github.com/crescent-network/crescent/v5/x/amm/types"
"github.com/crescent-network/crescent/v5/x/liquidamm/keeper"
"github.com/crescent-network/crescent/v5/x/liquidamm/simulation"
"github.com/crescent-network/crescent/v5/x/liquidamm/types"
)

func TestSimTestSuite(t *testing.T) {
suite.Run(t, new(SimTestSuite))
}

type SimTestSuite struct {
testutil.TestSuite
keeper keeper.Keeper
}

func (s *SimTestSuite) SetupTest() {
s.TestSuite.SetupTest()
s.keeper = s.App.LiquidAMMKeeper
}

func (s *SimTestSuite) getTestingAccounts(r *rand.Rand, n int) []simtypes.Account {
accs := simtypes.RandomAccounts(r, n)

initAmt := s.App.StakingKeeper.TokensFromConsensusPower(s.Ctx, 200)
initCoins := sdk.NewCoins(
sdk.NewCoin(sdk.DefaultBondDenom, initAmt),
sdk.NewCoin("denom1", initAmt),
sdk.NewCoin("denom2", initAmt),
sdk.NewCoin("denom3", initAmt))

// add coins to the accounts
for _, acc := range accs {
acc := s.App.AccountKeeper.NewAccountWithAddress(s.Ctx, acc.Address)
s.App.AccountKeeper.SetAccount(s.Ctx, acc)
s.Require().NoError(chain.FundAccount(s.App.BankKeeper, s.Ctx, acc.GetAddress(), initCoins))
}

return accs
}

func (s *SimTestSuite) TestSimulateMsgMintShare() {
r := rand.New(rand.NewSource(0))
accs := s.getTestingAccounts(r, 50)

market := s.CreateMarket("denom1", "denom2")
pool := s.CreatePool(market.Id, utils.ParseDec("5"))
s.CreatePublicPosition(
pool.Id, utils.ParseDec("4.5"), utils.ParseDec("5.5"), utils.ParseDec("0.003"))

op := simulation.SimulateMsgMintShare(s.App.AccountKeeper, s.App.BankKeeper, s.App.AMMKeeper, s.keeper)
opMsg, futureOps, err := op(r, s.App.BaseApp, s.Ctx, accs, "")
s.Require().NoError(err)
s.Require().True(opMsg.OK)
s.Require().Len(futureOps, 0)

var msg types.MsgMintShare
types.ModuleCdc.MustUnmarshalJSON(opMsg.Msg, &msg)

s.Assert().Equal(types.TypeMsgMintShare, msg.Type())
s.Assert().Equal(types.ModuleName, msg.Route())
s.Assert().Equal("cosmos1r6vgn9cwpvja7448fg0fgglj63rcs6y84p8egu", msg.Sender)
s.Assert().Equal(uint64(1), msg.PublicPositionId)
s.AssertEqual(utils.ParseCoins("116930denom1,644687denom2"), msg.DesiredAmount)
}

func (s *SimTestSuite) TestSimulateMsgBurnShare() {
r := rand.New(rand.NewSource(0))
accs := s.getTestingAccounts(r, 50)

market := s.CreateMarket("denom1", "denom2")
pool := s.CreatePool(market.Id, utils.ParseDec("5"))
publicPosition := s.CreatePublicPosition(
pool.Id, utils.ParseDec("4.5"), utils.ParseDec("5.5"), utils.ParseDec("0.003"))
s.MintShare(accs[0].Address, publicPosition.Id, utils.ParseCoins("100_000000denom1,500_000000denom2"), true)

op := simulation.SimulateMsgBurnShare(
s.App.AccountKeeper, s.App.BankKeeper, s.keeper)
opMsg, futureOps, err := op(r, s.App.BaseApp, s.Ctx, accs, "")
s.Require().NoError(err)
s.Require().True(opMsg.OK)
s.Require().Len(futureOps, 0)

var msg types.MsgBurnShare
types.ModuleCdc.MustUnmarshalJSON(opMsg.Msg, &msg)

s.Assert().Equal(types.TypeMsgBurnShare, msg.Type())
s.Assert().Equal(types.ModuleName, msg.Route())
s.Assert().Equal("cosmos1tp4es44j4vv8m59za3z0tm64dkmlnm8wg2frhc", msg.Sender)
s.Assert().Equal(uint64(1), msg.PublicPositionId)
s.AssertEqual(utils.ParseCoin("672658281sb1"), msg.Share)
}

func (s *SimTestSuite) TestSimulateMsgPlaceBid() {
r := rand.New(rand.NewSource(0))
accs := s.getTestingAccounts(r, 50)

market := s.CreateMarket("denom1", "denom2")
pool := s.CreatePool(market.Id, utils.ParseDec("5"))
publicPosition := s.CreatePublicPosition(
pool.Id, utils.ParseDec("4.5"), utils.ParseDec("5.5"), utils.ParseDec("0.003"))
farmingPoolAddr := s.FundedAccount(100, utils.ParseCoins("10000_000000stake"))
s.CreatePublicFarmingPlan(
"Farming plan", farmingPoolAddr, farmingPoolAddr, []ammtypes.FarmingRewardAllocation{
ammtypes.NewFarmingRewardAllocation(pool.Id, utils.ParseCoins("100_000000stake")),
}, utils.ParseTime("0001-01-01T00:00:00Z"), utils.ParseTime("9999-12-31T00:00:00Z"))
s.MintShare(accs[0].Address, publicPosition.Id, utils.ParseCoins("100_000000denom1,500_000000denom2"), true)
s.NextBlock()
s.AdvanceRewardsAuctions()

op := simulation.SimulateMsgPlaceBid(
s.App.AccountKeeper, s.App.BankKeeper, s.keeper)
opMsg, futureOps, err := op(r, s.App.BaseApp, s.Ctx, accs, "")
s.Require().NoError(err)
s.Require().True(opMsg.OK)
s.Require().Len(futureOps, 0)

var msg types.MsgPlaceBid
types.ModuleCdc.MustUnmarshalJSON(opMsg.Msg, &msg)

s.Assert().Equal(types.TypeMsgPlaceBid, msg.Type())
s.Assert().Equal(types.ModuleName, msg.Route())
s.Assert().Equal("cosmos1tp4es44j4vv8m59za3z0tm64dkmlnm8wg2frhc", msg.Sender)
s.Assert().Equal(uint64(1), msg.PublicPositionId)
s.AssertEqual(utils.ParseCoin("70093sb1"), msg.Share)
}
34 changes: 34 additions & 0 deletions x/liquidamm/simulation/proposals_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package simulation_test

import (
"math/rand"

utils "github.com/crescent-network/crescent/v5/types"
"github.com/crescent-network/crescent/v5/x/liquidamm/simulation"
)

func (s *SimTestSuite) TestProposalContents() {
r := rand.New(rand.NewSource(1))
accs := s.getTestingAccounts(r, 50)

market := s.CreateMarket("denom1", "denom2")
s.CreatePool(market.Id, utils.ParseDec("5"))

// execute ProposalContents function
weightedProposalContent := simulation.ProposalContents(s.App.AMMKeeper, s.keeper)
s.Require().Len(weightedProposalContent, 1)
w0 := weightedProposalContent[0]

// tests w0 interface:
s.Require().Equal(simulation.OpWeightSubmitPublicPositionCreateProposal, w0.AppParamsKey())
s.Require().Equal(simulation.DefaultWeightPublicPositionCreateProposal, w0.DefaultWeight())

// NOTE: currently the proposal is not returned. instead, it's run
// directly inside the proposal generator.
w0.ContentSimulatorFn()(r, s.Ctx, accs)

publicPosition, found := s.keeper.GetPublicPosition(s.Ctx, 1)
s.Require().True(found)
s.Assert().Equal(int32(30000), publicPosition.LowerTick)
s.Assert().Equal(int32(52500), publicPosition.UpperTick)
}

0 comments on commit b4c8f0c

Please sign in to comment.