diff --git a/x/auction/keeper/dutch_test.go b/x/auction/keeper/dutch_test.go index 8cb773a6a..f6a2c5282 100644 --- a/x/auction/keeper/dutch_test.go +++ b/x/auction/keeper/dutch_test.go @@ -91,11 +91,11 @@ func (s *KeeperTestSuite) TestLinearPriceFunction() { tau := sdk.NewIntFromUint64(60) dur := sdk.NewInt(0) for n := 0; n <= 60; n++ { - fmt.Println("top tau dur seconds") - fmt.Println(top, tau, dur) - fmt.Println("price") - price := getPriceFromLinearDecreaseFunction(top, tau, dur) - fmt.Println(price) + //fmt.Println("top tau dur seconds") + //fmt.Println(top, tau, dur) + //fmt.Println("price") + //price := getPriceFromLinearDecreaseFunction(top, tau, dur) + //fmt.Println(price) dur = dur.Add(sdk.NewInt(1)) } diff --git a/x/collector/keeper/collector_test.go b/x/collector/keeper/collector_test.go index 01570a3ee..7b4f917bb 100644 --- a/x/collector/keeper/collector_test.go +++ b/x/collector/keeper/collector_test.go @@ -54,13 +54,13 @@ func (s *KeeperTestSuite) AddAppAsset() { {Name: "CMDX", Denom: "ucmdx", Decimals: 1000000, - IsOnchain: true}, {Name: "CMST", + IsOnChain: true}, {Name: "CMST", Denom: "ucmst", Decimals: 1000000, - IsOnchain: true}, {Name: "HARBOR", + IsOnChain: true}, {Name: "HARBOR", Denom: "uharbor", Decimals: 1000000, - IsOnchain: true}, + IsOnChain: true}, } err = assetKeeper.AddAssetRecords(*ctx, msg2...) s.Require().NoError(err) diff --git a/x/tokenmint/keeper/keeper_test.go b/x/tokenmint/keeper/keeper_test.go new file mode 100644 index 000000000..4b6b4c15d --- /dev/null +++ b/x/tokenmint/keeper/keeper_test.go @@ -0,0 +1,104 @@ +package keeper_test + +import ( + "fmt" + collectorTypes "github.com/comdex-official/comdex/x/collector/types" + "time" + + "github.com/stretchr/testify/suite" + "testing" + + sdk "github.com/cosmos/cosmos-sdk/types" + tmproto "github.com/tendermint/tendermint/proto/tendermint/types" + + chain "github.com/comdex-official/comdex/app" + assetKeeper "github.com/comdex-official/comdex/x/asset/keeper" + auctionKeeper "github.com/comdex-official/comdex/x/auction/keeper" + collectorKeeper "github.com/comdex-official/comdex/x/collector/keeper" + tokenmintKeeper "github.com/comdex-official/comdex/x/tokenmint/keeper" + tokenmintTypes "github.com/comdex-official/comdex/x/tokenmint/types" +) + +type KeeperTestSuite struct { + suite.Suite + + app *chain.App + ctx sdk.Context + assetKeeper assetKeeper.Keeper + collectorKeeper collectorKeeper.Keeper + auctionKeeper auctionKeeper.Keeper + tokenmintKeeper tokenmintKeeper.Keeper + querier tokenmintKeeper.QueryServer + msgServer tokenmintTypes.MsgServer +} + +func TestKeeperTestSuite(t *testing.T) { + suite.Run(t, new(KeeperTestSuite)) +} + +func (s *KeeperTestSuite) SetupTest() { + s.app = chain.Setup(false) + s.ctx = s.app.BaseApp.NewContext(false, tmproto.Header{}) + s.collectorKeeper = s.app.CollectorKeeper + s.assetKeeper = s.app.AssetKeeper + s.auctionKeeper = s.app.AuctionKeeper + s.tokenmintKeeper = s.app.TokenmintKeeper + s.querier = tokenmintKeeper.QueryServer{Keeper: s.tokenmintKeeper} + s.msgServer = tokenmintKeeper.NewMsgServer(s.tokenmintKeeper) +} + +// +//// Below are just shortcuts to frequently-used functions. +//func (s *KeeperTestSuite) getBalances(addr sdk.AccAddress) sdk.Coins { +// return s.app.bankKeeper.GetAllBalances(s.ctx, addr) +//} +// +func (s *KeeperTestSuite) getBalance(addr string, denom string) (coin sdk.Coin, err error) { + addr1, err := sdk.AccAddressFromBech32(addr) + if err != nil { + return coin, err + } + return s.app.BankKeeper.GetBalance(s.ctx, addr1, denom), nil +} + +// +//func (s *KeeperTestSuite) sendCoins(fromAddr, toAddr sdk.AccAddress, amt sdk.Coins) { +// s.T().Helper() +// err := s.app.bankKeeper.SendCoins(s.ctx, fromAddr, toAddr, amt) +// s.Require().NoError(err) +//} +// +//func (s *KeeperTestSuite) nextBlock() { +// liquidity.EndBlocker(s.ctx, s.keeper) +// liquidity.BeginBlocker(s.ctx, s.keeper) +//} +// +//// Below are useful helpers to write test code easily. +//func (s *KeeperTestSuite) addr(addrNum int) sdk.AccAddress { +// addr := make(sdk.AccAddress, 20) +// binary.PutVarint(addr, int64(addrNum)) +// return addr +//} + +func (s *KeeperTestSuite) fundAddr(addr string, amt sdk.Coin) { + s.T().Helper() + err := s.app.BankKeeper.MintCoins(s.ctx, collectorTypes.ModuleName, sdk.NewCoins(amt)) + s.Require().NoError(err) + addr1, err := sdk.AccAddressFromBech32(addr) + err = s.app.BankKeeper.SendCoinsFromModuleToAccount(s.ctx, collectorTypes.ModuleName, addr1, sdk.NewCoins(amt)) + s.Require().NoError(err) +} + +func (s *KeeperTestSuite) advanceseconds(dur int64) { + s.ctx = s.ctx.WithBlockTime(s.ctx.BlockTime().Add(time.Second * time.Duration(dur))) + fmt.Println(s.ctx.BlockTime()) +} + +// ParseCoins parses and returns sdk.Coins. +func ParseCoin(s string) sdk.Coin { + coins, err := sdk.ParseCoinNormalized(s) + if err != nil { + panic(err) + } + return coins +} diff --git a/x/tokenmint/keeper/mint_test.go b/x/tokenmint/keeper/mint_test.go new file mode 100644 index 000000000..c59887253 --- /dev/null +++ b/x/tokenmint/keeper/mint_test.go @@ -0,0 +1,371 @@ +package keeper_test + +import ( + assetTypes "github.com/comdex-official/comdex/x/asset/types" + "github.com/comdex-official/comdex/x/tokenmint/keeper" + tokenmintTypes "github.com/comdex-official/comdex/x/tokenmint/types" + sdk "github.com/cosmos/cosmos-sdk/types" +) + +func (s *KeeperTestSuite) AddAppAsset() { + userAddress := "cosmos1q7q90qsl9g0gl2zz0njxwv2a649yqrtyxtnv3v" + genesisSupply := sdk.NewIntFromUint64(9000000) + assetKeeper, ctx := &s.assetKeeper, &s.ctx + msg1 := []assetTypes.AppMapping{{ + Name: "cswap", + ShortName: "cswap", + MinGovDeposit: sdk.NewIntFromUint64(10000000), + GovTimeInSeconds: 900, + GenesisToken: []assetTypes.MintGenesisToken{ + { + 3, + &genesisSupply, + true, + userAddress, + }, + { + 2, + &genesisSupply, + true, + userAddress, + }, + }, + }, + { + Name: "commodo", + ShortName: "commodo", + MinGovDeposit: sdk.NewIntFromUint64(10000000), + GovTimeInSeconds: 900, + GenesisToken: []assetTypes.MintGenesisToken{ + { + 3, + &genesisSupply, + true, + userAddress, + }, + }, + }, + } + err := assetKeeper.AddAppMappingRecords(*ctx, msg1...) + s.Require().NoError(err) + + msg2 := []assetTypes.Asset{ + {Name: "CMDX", + Denom: "ucmdx", + Decimals: 1000000, + IsOnChain: true}, {Name: "CMST", + Denom: "ucmst", + Decimals: 1000000, + IsOnChain: true}, {Name: "HARBOR", + Denom: "uharbor", + Decimals: 1000000, + IsOnChain: true}, + } + err = assetKeeper.AddAssetRecords(*ctx, msg2...) + s.Require().NoError(err) + +} + +//for _, tc := range []struct { +// name string +// msg collectorTypes.LookupTableParams +//}{ +// {"Add collector-lookup-params", +// collectorTypes.LookupTableParams{ +// "addAsset", +// "addingAsset", +// []collectorTypes.CollectorLookupTable{ +// {AppId: 1}, +// }, +// }}, +//} { +// s.Run(tc.name, func() { +// +// }) +//} + +func (s *KeeperTestSuite) TestMsgMintNewTokens() { + userAddress := "cosmos1q7q90qsl9g0gl2zz0njxwv2a649yqrtyxtnv3v" + tokenmintKeeper, ctx := &s.tokenmintKeeper, &s.ctx + wctx := sdk.WrapSDKContext(*ctx) + s.AddAppAsset() + server := keeper.NewMsgServer(*tokenmintKeeper) + for _, tc := range []struct { + name string + msg tokenmintTypes.MsgMintNewTokensRequest + expectedError bool + }{ + { + "Mint New Tokens : App ID : 1, Asset ID : 3", + tokenmintTypes.MsgMintNewTokensRequest{ + From: userAddress, + AppId: 1, + AssetId: 3, + }, + false, + }, + { + "Mint New Tokens : App ID : 1, Asset ID : 2", + tokenmintTypes.MsgMintNewTokensRequest{ + From: userAddress, + AppId: 1, + AssetId: 2, + }, + false, + }, + { + "Duplicate Failure Mint New Tokens : App ID : 1, Asset ID : 3", + tokenmintTypes.MsgMintNewTokensRequest{ + From: userAddress, + AppId: 1, + AssetId: 3, + }, + true, + }, + { + "Mint New Tokens : App ID : 2, Asset ID : 3", + tokenmintTypes.MsgMintNewTokensRequest{ + From: userAddress, + AppId: 2, + AssetId: 3, + }, + false, + }, + } { + s.Run(tc.name, func() { + genesisSupply := sdk.NewIntFromUint64(9000000) + asset, found := tokenmintKeeper.GetAsset(*ctx, tc.msg.AssetId) + s.Require().True(found) + previousCoin, err := s.getBalance(userAddress, asset.Denom) + _, err = server.MsgMintNewTokens(wctx, &tc.msg) + if tc.expectedError { + s.Require().Error(err) + } else { + s.Require().NoError(err) + currentCoin, err := s.getBalance(userAddress, asset.Denom) + s.Require().NoError(err) + ActualAmountMinted := currentCoin.Amount.Sub(previousCoin.Amount) + s.Require().Equal(ActualAmountMinted, genesisSupply) + req := tokenmintTypes.QueryTokenMintedByAppAndAssetRequest{ + AppId: tc.msg.AppId, + AssetId: tc.msg.AssetId, + } + res, err := s.querier.QueryTokenMintedByAppAndAsset(wctx, &req) + s.Require().NoError(err) + s.Require().Equal(res.MintedTokens.AssetId, tc.msg.AssetId) + s.Require().Equal(res.MintedTokens.GenesisSupply, ActualAmountMinted) + s.Require().Equal(res.MintedTokens.CurrentSupply, ActualAmountMinted) + } + + }) + } + result := s.tokenmintKeeper.GetTotalTokenMinted(*ctx) + //validates no. of apps + s.Require().Equal(len(result), 2) + //validates no of assets under app id 1 + s.Require().Equal(len(result[0].MintedTokens), 2) + //validates no of assets under app id 2 + s.Require().Equal(len(result[1].MintedTokens), 1) +} + +func (s *KeeperTestSuite) TestMintNewTokensForApp() { + userAddress := "cosmos1q7q90qsl9g0gl2zz0njxwv2a649yqrtyxtnv3v" + tokenmintKeeper, ctx := &s.tokenmintKeeper, &s.ctx + wctx := sdk.WrapSDKContext(*ctx) + s.TestMsgMintNewTokens() + for _, tc := range []struct { + name string + appID uint64 + assetID uint64 + address string + mintAmount sdk.Int + expectedError bool + }{ + { + "Mint New Tokens : App ID : 1, Asset ID : 2", + 1, + 2, + userAddress, + sdk.NewIntFromUint64(423), + false, + }, + } { + s.Run(tc.name, func() { + asset, found := tokenmintKeeper.GetAsset(*ctx, tc.assetID) + s.Require().True(found) + previousCoin, err := s.getBalance(userAddress, asset.Denom) + s.Require().NoError(err) + req := tokenmintTypes.QueryTokenMintedByAppAndAssetRequest{ + AppId: tc.appID, + AssetId: tc.assetID, + } + beforeTokenMint, err := s.querier.QueryTokenMintedByAppAndAsset(wctx, &req) + s.Require().NoError(err) + err = tokenmintKeeper.MintNewTokensForApp(*ctx, tc.appID, tc.assetID, tc.address, tc.mintAmount) + if tc.expectedError { + s.Require().Error(err) + } else { + s.Require().NoError(err) + currentCoin, err := s.getBalance(userAddress, asset.Denom) + s.Require().NoError(err) + ActualAmountMinted := currentCoin.Amount.Sub(previousCoin.Amount) + s.Require().Equal(ActualAmountMinted, tc.mintAmount) + req := tokenmintTypes.QueryTokenMintedByAppAndAssetRequest{ + AppId: tc.appID, + AssetId: tc.assetID, + } + res, err := s.querier.QueryTokenMintedByAppAndAsset(wctx, &req) + s.Require().NoError(err) + s.Require().Equal(res.MintedTokens.AssetId, tc.assetID) + s.Require().Equal(res.MintedTokens.GenesisSupply, beforeTokenMint.MintedTokens.GenesisSupply) + s.Require().Equal(tc.mintAmount, ActualAmountMinted) + s.Require().Equal(res.MintedTokens.CurrentSupply, beforeTokenMint.MintedTokens.CurrentSupply.Add(tc.mintAmount)) + } + }) + + } +} + +func (s *KeeperTestSuite) TestBurnTokensForApp() { + userAddress := "cosmos1q7q90qsl9g0gl2zz0njxwv2a649yqrtyxtnv3v" + tokenmintKeeper, ctx := &s.tokenmintKeeper, &s.ctx + wctx := sdk.WrapSDKContext(*ctx) + s.TestMsgMintNewTokens() + for _, tc := range []struct { + name string + appID uint64 + assetID uint64 + address string + sendAmount sdk.Int + burnAmount sdk.Int + expectedError bool + }{ + { + "Burn Tokens : App ID : 1, Asset ID : 2", + 1, + 2, + userAddress, + sdk.NewIntFromUint64(423), + sdk.NewIntFromUint64(423), + false, + }, + { + "Burn Tokens Insuffient balance failure: App ID : 1, Asset ID : 2", + 1, + 2, + userAddress, + sdk.NewIntFromUint64(422), + sdk.NewIntFromUint64(423), + true, + }, + } { + s.Run(tc.name, func() { + asset, found := tokenmintKeeper.GetAsset(*ctx, tc.assetID) + s.Require().True(found) + sender, err := sdk.AccAddressFromBech32(tc.address) + s.Require().NoError(err) + err = s.app.BankKeeper.SendCoinsFromAccountToModule(*ctx, sender, "tokenmint", sdk.NewCoins(sdk.NewCoin(asset.Denom, tc.sendAmount))) + s.Require().NoError(err) + req := tokenmintTypes.QueryTokenMintedByAppAndAssetRequest{ + AppId: tc.appID, + AssetId: tc.assetID, + } + beforeTokenMint, err := s.querier.QueryTokenMintedByAppAndAsset(wctx, &req) + s.Require().NoError(err) + beforeTokenMintBalance := s.auctionKeeper.GetModuleAccountBalance(*ctx, "tokenmint", asset.Denom) + err = tokenmintKeeper.BurnTokensForApp(*ctx, tc.appID, tc.assetID, tc.burnAmount) + if tc.expectedError { + s.Require().Error(err) + } else { + s.Require().NoError(err) + afterTokenMintBalance := s.auctionKeeper.GetModuleAccountBalance(*ctx, "tokenmint", asset.Denom) + s.Require().NoError(err) + req := tokenmintTypes.QueryTokenMintedByAppAndAssetRequest{ + AppId: tc.appID, + AssetId: tc.assetID, + } + res, err := s.querier.QueryTokenMintedByAppAndAsset(wctx, &req) + s.Require().NoError(err) + s.Require().Equal(res.MintedTokens.AssetId, tc.assetID) + s.Require().Equal(res.MintedTokens.GenesisSupply, beforeTokenMint.MintedTokens.GenesisSupply) + s.Require().Equal(res.MintedTokens.CurrentSupply.Add(tc.burnAmount), beforeTokenMint.MintedTokens.CurrentSupply) + s.Require().Equal(beforeTokenMintBalance.Sub(afterTokenMintBalance), tc.burnAmount) + } + }) + + } +} + +func (s *KeeperTestSuite) TestBurnGovTokensForApp() { + userAddress := "cosmos1q7q90qsl9g0gl2zz0njxwv2a649yqrtyxtnv3v" + tokenmintKeeper, ctx := &s.tokenmintKeeper, &s.ctx + wctx := sdk.WrapSDKContext(*ctx) + s.TestMsgMintNewTokens() + for _, tc := range []struct { + name string + appID uint64 + assetID uint64 + address string + burnAmount sdk.Int + expectedError bool + }{ + { + "Burn Gov Tokens For App: App ID : 1, Asset ID : 2", + 1, + 2, + userAddress, + sdk.NewIntFromUint64(423), + false, + }, + { + "Burn Gov Tokens For App Insuffient balance failure: App ID : 1, Asset ID : 2", + 1, + 2, + userAddress, + sdk.NewIntFromUint64(9000001), + true, + }, + } { + s.Run(tc.name, func() { + asset, found := tokenmintKeeper.GetAsset(*ctx, tc.assetID) + s.Require().True(found) + sender, err := sdk.AccAddressFromBech32(tc.address) + s.Require().NoError(err) + + req := tokenmintTypes.QueryTokenMintedByAppAndAssetRequest{ + AppId: tc.appID, + AssetId: tc.assetID, + } + beforeTokenMint, err := s.querier.QueryTokenMintedByAppAndAsset(wctx, &req) + s.Require().NoError(err) + beforeUserBalance, err := s.getBalance(tc.address, asset.Denom) + s.Require().NoError(err) + beforeTokenMintBalance := s.auctionKeeper.GetModuleAccountBalance(*ctx, "tokenmint", asset.Denom) + err = tokenmintKeeper.BurnGovTokensForApp(*ctx, tc.appID, sender, sdk.NewCoin(asset.Denom, tc.burnAmount)) + + if tc.expectedError { + s.Require().Error(err) + } else { + s.Require().NoError(err) + afterTokenMintBalance := s.auctionKeeper.GetModuleAccountBalance(*ctx, "tokenmint", asset.Denom) + s.Require().NoError(err) + afterUserBalance, err := s.getBalance(tc.address, asset.Denom) + s.Require().NoError(err) + req := tokenmintTypes.QueryTokenMintedByAppAndAssetRequest{ + AppId: tc.appID, + AssetId: tc.assetID, + } + res, err := s.querier.QueryTokenMintedByAppAndAsset(wctx, &req) + s.Require().NoError(err) + currentSupply, found := s.tokenmintKeeper.GetAssetDataInTokenMintByAppSupply(*ctx, tc.appID, tc.assetID) + s.Require().True(found) + s.Require().Equal(res.MintedTokens.AssetId, tc.assetID) + s.Require().Equal(res.MintedTokens.GenesisSupply, beforeTokenMint.MintedTokens.GenesisSupply) + s.Require().Equal(sdk.NewInt(currentSupply).Add(tc.burnAmount), beforeTokenMint.MintedTokens.CurrentSupply) + s.Require().Equal(beforeTokenMintBalance, afterTokenMintBalance) + s.Require().Equal(beforeUserBalance.Amount.Sub(afterUserBalance.Amount), tc.burnAmount) + } + }) + + } +} diff --git a/x/tokenmint/keeper/params_test.go b/x/tokenmint/keeper/params_test.go index 9f833d22f..2cce1d29a 100644 --- a/x/tokenmint/keeper/params_test.go +++ b/x/tokenmint/keeper/params_test.go @@ -1,18 +1,19 @@ package keeper_test -import ( - "testing" +// import ( +// "testing" - testkeeper "github.com/comdex-official/comdex/testutil/keeper" - "github.com/comdex-official/comdex/x/tokenmint/types" - "github.com/stretchr/testify/require" -) +// testkeeper "github.com/comdex-official/comdex/testutil/keeper" +// "github.com/comdex-official/comdex/x/tokenmint/types" +// "github.com/stretchr/testify/require" +// ) -func TestGetParams(t *testing.T) { - k, ctx := testkeeper.TokenmintKeeper(t) - params := types.DefaultParams() - - k.SetParams(ctx, params) - - require.EqualValues(t, params, k.GetParams(ctx)) -} +// +//func TestGetParams(t *testing.T) { +// k, ctx := testkeeper.TokenmintKeeper(t) +// params := types.DefaultParams() +// +// k.SetParams(ctx, params) +// +// require.EqualValues(t, params, k.GetParams(ctx)) +//} diff --git a/x/tokenmint/keeper/query_server.go b/x/tokenmint/keeper/query_server.go index d0316c9d5..ca4e041e6 100644 --- a/x/tokenmint/keeper/query_server.go +++ b/x/tokenmint/keeper/query_server.go @@ -8,20 +8,20 @@ import ( ) var ( - _ types.QueryServer = (*queryServer)(nil) + _ types.QueryServer = (*QueryServer)(nil) ) -type queryServer struct { +type QueryServer struct { Keeper } func NewQueryServer(k Keeper) types.QueryServer { - return &queryServer{ + return &QueryServer{ Keeper: k, } } -func (q *queryServer) QueryAllTokenMintedForAllApps(c context.Context, req *types.QueryAllTokenMintedForAllAppsRequest) (*types.QueryAllTokenMintedForAllAppsResponse, error) { +func (q *QueryServer) QueryAllTokenMintedForAllApps(c context.Context, req *types.QueryAllTokenMintedForAllAppsRequest) (*types.QueryAllTokenMintedForAllAppsResponse, error) { var ( ctx = sdk.UnwrapSDKContext(c) ) @@ -32,7 +32,7 @@ func (q *queryServer) QueryAllTokenMintedForAllApps(c context.Context, req *type }, nil } -func (q *queryServer) QueryTokenMintedByApp(c context.Context, req *types.QueryTokenMintedByAppRequest) (*types.QueryTokenMintedByAppResponse, error) { +func (q *QueryServer) QueryTokenMintedByApp(c context.Context, req *types.QueryTokenMintedByAppRequest) (*types.QueryTokenMintedByAppResponse, error) { var ( ctx = sdk.UnwrapSDKContext(c) ) @@ -46,7 +46,7 @@ func (q *queryServer) QueryTokenMintedByApp(c context.Context, req *types.QueryT }, nil } -func (q *queryServer) QueryTokenMintedByAppAndAsset(c context.Context, req *types.QueryTokenMintedByAppAndAssetRequest) (*types.QueryTokenMintedByAppAndAssetResponse, error) { +func (q *QueryServer) QueryTokenMintedByAppAndAsset(c context.Context, req *types.QueryTokenMintedByAppAndAssetRequest) (*types.QueryTokenMintedByAppAndAssetResponse, error) { var ( ctx = sdk.UnwrapSDKContext(c) )